decruft 0.1.3

Extract clean, readable content from web pages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::fmt::Write;

use ego_tree::NodeId;
use scraper::{Html, Node, Selector};

/// Get the text content of a node and its descendants.
#[must_use]
pub fn text_content(html: &Html, node_id: NodeId) -> String {
    let mut text = String::new();
    collect_text(html, node_id, &mut text);
    text
}

fn collect_text(html: &Html, node_id: NodeId, buf: &mut String) {
    let node_ref = html.tree.get(node_id);
    let Some(node_ref) = node_ref else { return };
    match node_ref.value() {
        Node::Text(t) => buf.push_str(t),
        Node::Element(_) => {
            for child in node_ref.children() {
                collect_text(html, child.id(), buf);
            }
        }
        _ => {}
    }
}

/// Get the outer HTML of a node.
#[must_use]
pub fn outer_html(html: &Html, node_id: NodeId) -> String {
    let node_ref = html.tree.get(node_id);
    let Some(node_ref) = node_ref else {
        return String::new();
    };
    serialize_node(html, node_id, node_ref.value())
}

fn serialize_node(html: &Html, node_id: NodeId, node: &Node) -> String {
    match node {
        Node::Document => {
            let mut out = String::new();
            let Some(node_ref) = html.tree.get(node_id) else {
                return out;
            };
            for child in node_ref.children() {
                out.push_str(&serialize_node(html, child.id(), child.value()));
            }
            out
        }
        Node::Text(t) => text_escape(t),
        Node::Element(el) => {
            let tag = el.name.local.as_ref();
            let mut out = format!("<{tag}");
            for (name, value) in el.attrs() {
                let _ = write!(out, " {name}=\"{}\"", attr_escape(value));
            }

            if is_void_element(tag) {
                out.push('>');
                return out;
            }
            out.push('>');

            let Some(node_ref) = html.tree.get(node_id) else {
                return out;
            };
            for child in node_ref.children() {
                out.push_str(&serialize_node(html, child.id(), child.value()));
            }
            let _ = write!(out, "</{tag}>");
            out
        }
        Node::Comment(c) => format!("<!--{}-->", c.comment),
        _ => String::new(),
    }
}

/// Get the inner HTML of a node.
#[must_use]
pub fn inner_html(html: &Html, node_id: NodeId) -> String {
    let Some(node_ref) = html.tree.get(node_id) else {
        return String::new();
    };
    let mut out = String::new();
    for child in node_ref.children() {
        out.push_str(&serialize_node(html, child.id(), child.value()));
    }
    out
}

fn text_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn attr_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

fn is_void_element(tag: &str) -> bool {
    matches!(
        tag,
        "area"
            | "base"
            | "br"
            | "col"
            | "embed"
            | "hr"
            | "img"
            | "input"
            | "link"
            | "meta"
            | "param"
            | "source"
            | "track"
            | "wbr"
    )
}

/// Remove a node from the tree by detaching it.
pub fn remove_node(html: &mut Html, node_id: NodeId) {
    if let Some(mut node) = html.tree.get_mut(node_id) {
        node.detach();
    }
}

/// Set an attribute on an element node.
pub fn set_attr(html: &mut Html, node_id: NodeId, name: &str, value: &str) {
    let Some(mut node_mut) = html.tree.get_mut(node_id) else {
        return;
    };
    let Node::Element(el) = node_mut.value() else {
        return;
    };
    let qn =
        markup5ever::QualName::new(None, markup5ever::ns!(), markup5ever::LocalName::from(name));
    el.attrs.retain(|(n, _)| n != &qn);
    el.attrs
        .push((qn, markup5ever::tendril::StrTendril::from(value)));
}

/// Select element IDs matching a CSS selector that are descendants
/// of `ancestor_id`.
#[must_use]
pub fn select_within(html: &Html, ancestor_id: NodeId, selector_str: &str) -> Vec<NodeId> {
    let Ok(sel) = Selector::parse(selector_str) else {
        return Vec::new();
    };
    html.select(&sel)
        .filter(|el| is_ancestor(html, el.id(), ancestor_id))
        .map(|el| el.id())
        .collect()
}

/// Get an attribute value from an element node.
pub fn get_attr(html: &Html, node_id: NodeId, attr: &str) -> Option<String> {
    let node_ref = html.tree.get(node_id)?;
    if let Node::Element(el) = node_ref.value() {
        el.attr(attr).map(String::from)
    } else {
        None
    }
}

/// Check if a node is an element with the given tag name.
#[must_use]
pub fn is_tag(html: &Html, node_id: NodeId, tag: &str) -> bool {
    let Some(node_ref) = html.tree.get(node_id) else {
        return false;
    };
    if let Node::Element(el) = node_ref.value() {
        el.name.local.as_ref() == tag
    } else {
        false
    }
}

/// Get the tag name of an element node.
#[must_use]
pub fn tag_name(html: &Html, node_id: NodeId) -> Option<String> {
    let node_ref = html.tree.get(node_id)?;
    if let Node::Element(el) = node_ref.value() {
        Some(el.name.local.as_ref().to_string())
    } else {
        None
    }
}

/// Get all element node IDs matching a CSS selector.
#[must_use]
pub fn select_ids(html: &Html, selector_str: &str) -> Vec<NodeId> {
    let Ok(sel) = Selector::parse(selector_str) else {
        return Vec::new();
    };
    html.select(&sel).map(|el| el.id()).collect()
}

/// Count words in text, with CJK awareness.
#[must_use]
pub fn count_words(text: &str) -> usize {
    let mut count = 0usize;
    let mut in_word = false;

    for ch in text.chars() {
        if is_cjk(ch) {
            count += 1;
            in_word = false;
        } else if ch.is_whitespace() {
            in_word = false;
        } else if !in_word {
            in_word = true;
            count += 1;
        }
    }
    count
}

fn is_cjk(ch: char) -> bool {
    let c = ch as u32;
    // Hiragana, Katakana, CJK Extension A, CJK Unified, CJK Compat, Hangul
    (0x3040..=0x309F).contains(&c)
        || (0x30A0..=0x30FF).contains(&c)
        || (0x3400..=0x4DBF).contains(&c)
        || (0x4E00..=0x9FFF).contains(&c)
        || (0xF900..=0xFAFF).contains(&c)
        || (0xAC00..=0xD7AF).contains(&c)
}

/// Count words in HTML by stripping tags.
#[must_use]
pub fn count_words_html(html_str: &str) -> usize {
    let text = strip_tags(html_str);
    let decoded = decode_entities(&text);
    count_words(&decoded)
}

/// Strip HTML tags and decode common HTML entities, producing plain
/// text suitable for display.
#[must_use]
pub fn strip_html_tags(html: &str) -> String {
    let stripped = strip_tags(html);
    decode_entities(&stripped)
}

fn strip_tags(html: &str) -> String {
    let mut result = String::with_capacity(html.len());
    let mut in_tag = false;
    for ch in html.chars() {
        if ch == '<' {
            in_tag = true;
        } else if ch == '>' {
            in_tag = false;
            result.push(' ');
        } else if !in_tag {
            result.push(ch);
        }
    }
    result
}

fn decode_entities(s: &str) -> String {
    s.replace("&nbsp;", " ")
        .replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
}

/// Escape a string for safe use inside an HTML attribute value.
#[must_use]
pub fn html_attr_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('"', "&quot;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

/// Escape text for safe use in HTML content.
#[must_use]
pub fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Check if an element has a specific CSS class.
#[must_use]
pub fn has_class(html: &Html, node_id: NodeId, class: &str) -> bool {
    let Some(node_ref) = html.tree.get(node_id) else {
        return false;
    };
    let Node::Element(el) = node_ref.value() else {
        return false;
    };
    el.attr("class")
        .is_some_and(|c| c.split_whitespace().any(|cls| cls == class))
}

/// Get the parent element's node ID.
#[must_use]
pub fn parent_element(html: &Html, node_id: NodeId) -> Option<NodeId> {
    let node_ref = html.tree.get(node_id)?;
    let parent = node_ref.parent()?;
    if matches!(parent.value(), Node::Element(_)) {
        Some(parent.id())
    } else {
        None
    }
}

/// Check if `ancestor_id` is an ancestor of `node_id`.
#[must_use]
pub fn is_ancestor(html: &Html, node_id: NodeId, ancestor_id: NodeId) -> bool {
    let mut current = node_id;
    loop {
        let Some(node_ref) = html.tree.get(current) else {
            return false;
        };
        let Some(parent) = node_ref.parent() else {
            return false;
        };
        if parent.id() == ancestor_id {
            return true;
        }
        current = parent.id();
    }
}

/// Get all child element node IDs.
#[must_use]
pub fn child_elements(html: &Html, node_id: NodeId) -> Vec<NodeId> {
    let Some(node_ref) = html.tree.get(node_id) else {
        return Vec::new();
    };
    node_ref
        .children()
        .filter(|c| matches!(c.value(), Node::Element(_)))
        .map(|c| c.id())
        .collect()
}

/// Compute link density: ratio of text inside `<a>` tags to total text.
#[must_use]
pub fn link_density(html: &Html, node_id: NodeId) -> f64 {
    let total_text = text_content(html, node_id);
    link_density_with_text(html, node_id, &total_text)
}

/// Compute link density using pre-computed total text to avoid
/// redundant tree walks.
#[must_use]
pub fn link_density_with_text(html: &Html, node_id: NodeId, total_text: &str) -> f64 {
    let total_len = total_text.trim().len();
    if total_len == 0 {
        return 0.0;
    }
    let mut link_len = 0usize;
    for a_id in descendant_elements_by_tag(html, node_id, "a") {
        link_len += text_content(html, a_id).trim().len();
    }
    #[expect(clippy::cast_precision_loss)]
    let ratio = link_len as f64 / total_len as f64;
    ratio
}

/// Find all descendant elements with a specific tag.
#[must_use]
pub fn descendant_elements_by_tag(html: &Html, node_id: NodeId, tag: &str) -> Vec<NodeId> {
    let mut result = Vec::new();
    collect_descendants_by_tag(html, node_id, tag, &mut result);
    result
}

fn collect_descendants_by_tag(html: &Html, node_id: NodeId, tag: &str, result: &mut Vec<NodeId>) {
    let Some(node_ref) = html.tree.get(node_id) else {
        return;
    };
    for child in node_ref.children() {
        if let Node::Element(el) = child.value()
            && el.name.local.as_ref() == tag
        {
            result.push(child.id());
        }
        collect_descendants_by_tag(html, child.id(), tag, result);
    }
}

/// Get all descendant element IDs.
#[must_use]
pub fn all_descendant_elements(html: &Html, node_id: NodeId) -> Vec<NodeId> {
    let mut result = Vec::new();
    collect_all_descendants(html, node_id, &mut result);
    result
}

fn collect_all_descendants(html: &Html, node_id: NodeId, result: &mut Vec<NodeId>) {
    let Some(node_ref) = html.tree.get(node_id) else {
        return;
    };
    for child in node_ref.children() {
        if matches!(child.value(), Node::Element(_)) {
            result.push(child.id());
        }
        collect_all_descendants(html, child.id(), result);
    }
}

/// Check if any descendant of `node_id` matches a CSS selector.
#[must_use]
pub fn has_descendant_matching(html: &Html, node_id: NodeId, selector_str: &str) -> bool {
    let Ok(sel) = Selector::parse(selector_str) else {
        return false;
    };
    html.select(&sel)
        .any(|el| is_ancestor(html, el.id(), node_id))
}

/// Check if the element itself matches a CSS selector.
#[must_use]
pub fn element_matches(html: &Html, node_id: NodeId, selector_str: &str) -> bool {
    let Ok(sel) = Selector::parse(selector_str) else {
        return false;
    };
    html.select(&sel).any(|el| el.id() == node_id)
}

/// Check if element or any ancestor matches a CSS selector.
#[must_use]
pub fn self_or_ancestor_matches(html: &Html, node_id: NodeId, selector_str: &str) -> bool {
    let Ok(sel) = Selector::parse(selector_str) else {
        return false;
    };
    let matching_ids: Vec<NodeId> = html.select(&sel).map(|el| el.id()).collect();
    let mut current = Some(node_id);
    while let Some(id) = current {
        if matching_ids.contains(&id) {
            return true;
        }
        current = parent_element(html, id);
    }
    false
}

/// Collect href attribute values from descendant `<a>` elements.
#[must_use]
pub fn collect_link_hrefs(html: &Html, node_id: NodeId) -> Vec<String> {
    let mut hrefs = Vec::new();
    for a_id in descendant_elements_by_tag(html, node_id, "a") {
        if let Some(href) = get_attr(html, a_id, "href") {
            hrefs.push(href);
        }
    }
    hrefs
}

/// Generate a CSS selector path for a node (for debug output).
#[must_use]
pub fn selector_path(html: &Html, node_id: NodeId) -> String {
    let mut parts = Vec::new();
    let mut current = Some(node_id);
    while let Some(id) = current {
        let Some(node_ref) = html.tree.get(id) else {
            break;
        };
        if let Node::Element(el) = node_ref.value() {
            let tag = el.name.local.as_ref();
            let mut part = tag.to_string();
            if let Some(id_attr) = el.attr("id") {
                let _ = write!(part, "#{id_attr}");
            } else if let Some(class_attr) = el.attr("class") {
                let first_class = class_attr.split_whitespace().next();
                if let Some(cls) = first_class {
                    let _ = write!(part, ".{cls}");
                }
            }
            parts.push(part);
        }
        current = node_ref
            .parent()
            .filter(|p| matches!(p.value(), Node::Element(_)))
            .map(|p| p.id());
    }
    parts.reverse();
    parts.join(" > ")
}