decruft 0.1.1

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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use std::sync::LazyLock;

use ego_tree::NodeId;
use regex::Regex;
use scraper::{Html, Node};

use crate::dom;

static CONTENT_CLASS_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)(content|article|post)")
        .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #4: added admonition, image, img, font, figure, figcaption, pre, table
static LIKELY_CONTENT_CLASS_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)(admonition|article|content|entry|figcaption|",
        r"figure|font|image|img|main|post|pre|story|table)"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #7: each nav indicator wrapped with \b word boundaries
static NAV_INDICATORS_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)",
        r"(\b(?:advertisement)\b|\b(?:all rights reserved)\b|",
        r"\b(?:banner)\b|\b(?:cookie)\b|",
        r"\b(?:comments)\b|\b(?:copyright)\b|",
        r"\b(?:follow me)\b|\b(?:follow us)\b|\b(?:footer)\b|",
        r"\b(?:header)\b|\b(?:homepage)\b|\b(?:login)\b|",
        r"\b(?:menu)\b|\b(?:more articles)\b|",
        r"\b(?:more like this)\b|\b(?:most read)\b|",
        r"\b(?:nav)\b|\b(?:navigation)\b|",
        r"\b(?:newsletter)\b|\b(?:popular)\b|\b(?:privacy)\b|",
        r"\b(?:recommended)\b|\b(?:register)\b|",
        r"\b(?:related)\b|\b(?:responses)\b|\b(?:share)\b|",
        r"\b(?:sidebar)\b|\b(?:sign in)\b|\b(?:sign up)\b|",
        r"\b(?:signup)\b|\b(?:social)\b|\b(?:sponsored)\b|",
        r"\b(?:subscribe)\b|\b(?:terms)\b|\b(?:trending)\b)"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #8: add homepage, popular, privacy, recommended, rights, terms,
// trending; remove popup, promo; change sponsor to sponsored
static NON_CONTENT_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)",
        r"(advert|ad-|ads|banner|cookie|copyright|footer|",
        r"header|homepage|menu|nav|newsletter|popular|privacy|",
        r"recommended|related|rights|share|sidebar|social|",
        r"sponsored|subscribe|terms|trending|widget)"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #3: case-sensitive, match capital letter after "By "
static BYLINE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\bBy\s+[A-Z]").unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Loose date regex for byline detection (no year required)
static DATE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)",
        r"(\d{1,2}[/\-]\d{1,2}[/\-]\d{2,4}|",
        r"\b(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)",
        r"[a-z]*\s+\d{1,2})"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #1: strict date regex for scoring (requires 4-digit year)
static CONTENT_DATE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)\b(?:",
        r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)",
        r"[a-z]*\s+\d{1,2},?\s+\d{4}",
        r"|",
        r"\d{1,2}(?:st|nd|rd|th)?\s+",
        r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)",
        r"[a-z]*,?\s+\d{4}",
        r")\b"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #2: author regex requires trailing name characters
static AUTHOR_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)\b(?:by|written by|author:)\s+[A-Za-z\s]+\b")
        .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

/// Labels that indicate article metadata (author lists, citation info,
/// submission dates, subject classifications, DOIs).
static METADATA_LABEL_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"(?i)^[\s\n]*(Authors?:?|By |Subjects?:|Keywords?:|",
        r"Cite as:?|DOI:?|Published:?|Submitted:?|Date:?|",
        r"Citation:?|Abstract:)"
    ))
    .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

/// Class/ID patterns that indicate article metadata elements.
static METADATA_CLASS_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)(authors|citation|subjects|keywords|doi|submission)")
        .unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

static SENTENCE_PUNCT_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"[.!?]").unwrap_or_else(|_| Regex::new("a]^").expect("infallible fallback"))
});

// Fix #9: social profile regex with negative lookahead (fancy_regex)
static SOCIAL_PROFILE_RE: LazyLock<fancy_regex::Regex> = LazyLock::new(|| {
    fancy_regex::Regex::new(concat!(
        r"(?i)\b(",
        r"linkedin\.com/(in|company)/|",
        r"twitter\.com/(?!intent\b)\w|",
        r"x\.com/(?!intent\b)\w|",
        r"facebook\.com/(?!share\b)\w|",
        r"instagram\.com/\w|",
        r"threads\.net/\w|",
        r"mastodon\.\w",
        r")"
    ))
    .unwrap_or_else(|_| fancy_regex::Regex::new("a]^").expect("infallible fallback"))
});

const FOOTNOTE_REF_SELECTOR: &str = "a[href^=\"#fn\"], sup.reference";

const FOOTNOTE_LIST_SELECTOR: &str = "ol.footnotes, div.footnotes";

fn class_and_id(html: &Html, node_id: NodeId) -> String {
    let class = dom::get_attr(html, node_id, "class").unwrap_or_default();
    let id = dom::get_attr(html, node_id, "id").unwrap_or_default();
    format!("{class} {id}")
}

fn count_commas(text: &str) -> usize {
    text.chars().filter(|&c| c == ',').count()
}

/// Check if element has right-aligned styling.
fn is_right_aligned(html: &Html, node_id: NodeId) -> bool {
    if let Some(align) = dom::get_attr(html, node_id, "align")
        && align.eq_ignore_ascii_case("right")
    {
        return true;
    }
    if let Some(style) = dom::get_attr(html, node_id, "style") {
        let lower = style.to_ascii_lowercase();
        return lower.contains("float:right")
            || lower.contains("float: right")
            || lower.contains("text-align:right")
            || lower.contains("text-align: right");
    }
    false
}

/// Fix #11: check if the scored element itself is a `<td>` inside a
/// layout table (width>400), with align=center or content class,
/// and is not the first or last child of its parent.
fn is_layout_table_cell(html: &Html, node_id: NodeId) -> bool {
    let Some(tag) = dom::tag_name(html, node_id) else {
        return false;
    };
    if tag != "td" {
        return false;
    }
    // Check align=center or content class
    let has_center =
        dom::get_attr(html, node_id, "align").is_some_and(|a| a.eq_ignore_ascii_case("center"));
    let class_id = class_and_id(html, node_id);
    if !has_center && !CONTENT_CLASS_RE.is_match(&class_id) {
        return false;
    }
    // Must not be first or last child
    if let Some(parent_id) = dom::parent_element(html, node_id) {
        let siblings = dom::child_elements(html, parent_id);
        if siblings.first() == Some(&node_id) || siblings.last() == Some(&node_id) {
            return false;
        }
        // Check containing table has width > 400
        if let Some(table_tag) = dom::tag_name(html, parent_id)
            && table_tag == "table"
        {
            return table_has_large_width(html, parent_id);
        }
        // Walk up to find the table
        let mut cur = Some(parent_id);
        while let Some(id) = cur {
            if dom::tag_name(html, id).as_deref() == Some("table") {
                return table_has_large_width(html, id);
            }
            cur = dom::parent_element(html, id);
        }
    }
    false
}

fn table_has_large_width(html: &Html, table_id: NodeId) -> bool {
    dom::get_attr(html, table_id, "width")
        .and_then(|w| w.trim_end_matches("px").parse::<u32>().ok())
        .is_some_and(|w| w > 400)
}

/// Score an element for content likelihood.
/// Higher = more likely to be main content.
#[expect(clippy::cast_precision_loss)]
pub fn score_element(html: &Html, node_id: NodeId) -> f64 {
    let text = dom::text_content(html, node_id);
    let words = dom::count_words(&text);
    let paragraphs = dom::descendant_elements_by_tag(html, node_id, "p").len();
    let commas = count_commas(&text);
    let images = dom::descendant_elements_by_tag(html, node_id, "img").len();

    let mut score = words as f64;
    score += paragraphs as f64 * 10.0;
    score += commas as f64;

    let image_density = images as f64 / words.max(1) as f64;
    score -= image_density * 3.0;

    let class_id = class_and_id(html, node_id);
    if CONTENT_CLASS_RE.is_match(&class_id) {
        score += 15.0;
    }

    // Use pre-computed text to avoid redundant tree walk
    let ratio = dom::link_density_with_text(html, node_id, &text);
    score *= 1.0 - ratio.min(0.5);

    score += score_alignment_and_metadata(html, node_id, &text);
    score += score_footnotes_and_tables(html, node_id);

    score
}

/// Bonus for right-alignment, date patterns, and author patterns.
fn score_alignment_and_metadata(html: &Html, node_id: NodeId, text: &str) -> f64 {
    let mut bonus = 0.0;
    if is_right_aligned(html, node_id) {
        bonus += 5.0;
    }
    // Fix #1: use strict date regex requiring 4-digit year
    if CONTENT_DATE_RE.is_match(text) {
        bonus += 10.0;
    }
    if AUTHOR_RE.is_match(text) {
        bonus += 10.0;
    }
    bonus
}

/// Bonus for footnotes, layout table cells, and penalty for nested
/// tables.
#[expect(clippy::cast_precision_loss)]
fn score_footnotes_and_tables(html: &Html, node_id: NodeId) -> f64 {
    let mut bonus = 0.0;
    if dom::has_descendant_matching(html, node_id, FOOTNOTE_REF_SELECTOR) {
        bonus += 10.0;
    }
    if dom::has_descendant_matching(html, node_id, FOOTNOTE_LIST_SELECTOR) {
        bonus += 10.0;
    }
    let nested_tables = dom::descendant_elements_by_tag(html, node_id, "table").len();
    bonus -= nested_tables as f64 * 5.0;
    // Fix #11: check if element itself is a layout table cell
    if is_layout_table_cell(html, node_id) {
        bonus += 10.0;
    }
    bonus
}

/// Find the best-scoring element from a list, above
/// `min_score` threshold.
/// Check if an element is likely content (should be preserved).
pub fn is_likely_content(html: &Html, node_id: NodeId) -> bool {
    // Cheap attribute checks first
    if let Some(role) = dom::get_attr(html, node_id, "role")
        && (role == "article" || role == "main" || role == "contentinfo")
    {
        return true;
    }

    let class_id = class_and_id(html, node_id);
    if LIKELY_CONTENT_CLASS_RE.is_match(&class_id) {
        return true;
    }

    // Protect article metadata inside <main>/<article> from removal
    if is_article_metadata(html, node_id) && is_inside_main_or_article(html, node_id) {
        return true;
    }

    if has_structural_content(html, node_id) {
        return true;
    }

    // Compute text once and reuse
    let text = dom::text_content(html, node_id);
    let word_count = dom::count_words(&text);

    // Fix #5: nav heading rejection with 200-999 word tier
    if word_count < 1000 && has_nav_heading(html, node_id) {
        if word_count < 200 {
            return false;
        }
        let link_density = dom::link_density_with_text(html, node_id, &text);
        if link_density > 0.2 {
            return false;
        }
    }
    if is_card_grid(html, node_id) {
        return false;
    }
    if word_count < 80 && has_social_profile_links(html, node_id) {
        return false;
    }

    if word_count > 100 {
        return true;
    }

    let paragraph_count = dom::descendant_elements_by_tag(html, node_id, "p").len();
    let list_item_count = dom::descendant_elements_by_tag(html, node_id, "li").len();

    // Fix #6: words > 30 with at least one content block
    if word_count > 30 && (paragraph_count + list_item_count) > 0 {
        return true;
    }
    if word_count > 50 && (paragraph_count + list_item_count) > 1 {
        return true;
    }
    if word_count >= 10
        && SENTENCE_PUNCT_RE.is_match(&text)
        && dom::link_density_with_text(html, node_id, &text) < 0.1
    {
        return true;
    }

    false
}

/// Check if an element looks like article metadata (author lists,
/// citation info, submission dates, subject classifications).
///
/// Returns true if the element's text starts with a metadata label
/// pattern OR its class/id contains metadata keywords.
pub fn is_article_metadata(html: &Html, node_id: NodeId) -> bool {
    let class_id = class_and_id(html, node_id);
    if METADATA_CLASS_RE.is_match(&class_id) {
        return true;
    }
    let text = dom::text_content(html, node_id);
    METADATA_LABEL_RE.is_match(&text)
}

/// Check if an element is inside a `<main>` or `<article>` ancestor.
fn is_inside_main_or_article(html: &Html, node_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 let Node::Element(el) = parent.value() {
            let tag = el.name.local.as_ref();
            if tag == "main" || tag == "article" {
                return true;
            }
        }
        current = parent.id();
    }
}

/// Check if element contains a heading with navigation-like text.
fn has_nav_heading(html: &Html, node_id: NodeId) -> bool {
    let heading_tags = ["h1", "h2", "h3", "h4", "h5", "h6"];
    for tag in &heading_tags {
        for h_id in dom::descendant_elements_by_tag(html, node_id, tag) {
            let h_text = dom::text_content(html, h_id);
            if NAV_INDICATORS_RE.is_match(&h_text) {
                return true;
            }
        }
    }
    false
}

/// Fix #10: detect card grid pattern.
/// Only check h2-h4, skip if words < 3 or >= 500, subtract heading
/// word count from total before computing prose-per-heading.
#[expect(clippy::cast_precision_loss)]
fn is_card_grid(html: &Html, node_id: NodeId) -> bool {
    let text = dom::text_content(html, node_id);
    let total_words = dom::count_words(&text);
    if !(3..500).contains(&total_words) {
        return false;
    }
    let mut heading_ids = Vec::new();
    for tag in &["h2", "h3", "h4"] {
        heading_ids.extend(dom::descendant_elements_by_tag(html, node_id, tag));
    }
    if heading_ids.len() < 3 {
        return false;
    }
    let image_count = dom::descendant_elements_by_tag(html, node_id, "img").len();
    if image_count < 2 {
        return false;
    }
    let heading_words: usize = heading_ids
        .iter()
        .map(|&h_id| dom::count_words(&dom::text_content(html, h_id)))
        .sum();
    let prose_words = total_words.saturating_sub(heading_words);
    let words_per_heading = prose_words as f64 / heading_ids.len() as f64;
    words_per_heading < 20.0
}

/// Fix #9: check social profile links with full regex including
/// negative lookahead for intent/share URLs.
fn has_social_profile_links(html: &Html, node_id: NodeId) -> bool {
    let hrefs = dom::collect_link_hrefs(html, node_id);
    hrefs
        .iter()
        .any(|href| SOCIAL_PROFILE_RE.is_match(href).unwrap_or(false))
}

/// Check if any descendant has a structural content tag.
/// Uses a single tree walk instead of separate per-tag searches.
fn has_structural_content(html: &Html, node_id: NodeId) -> bool {
    has_descendant_with_tag(
        html,
        node_id,
        &[
            "pre",
            "table",
            "figure",
            "picture",
            "code",
            "blockquote",
            "math",
            "mjx-container",
        ],
    )
}

/// Walk descendants once, checking if any match the given tag names.
fn has_descendant_with_tag(html: &Html, node_id: NodeId, tags: &[&str]) -> bool {
    let Some(node_ref) = html.tree.get(node_id) else {
        return false;
    };
    for child in node_ref.children() {
        if let Node::Element(el) = child.value() {
            let tag = el.name.local.as_ref();
            if tags.contains(&tag) {
                return true;
            }
            // Also check for math classes
            if let Some(class) = el.attr("class")
                && (class.contains("katex") || class.contains("MathJax"))
            {
                return true;
            }
        }
        if has_descendant_with_tag(html, child.id(), tags) {
            return true;
        }
    }
    false
}

/// Score a non-content block. Negative score = should be removed.
#[expect(clippy::cast_precision_loss)]
pub fn score_non_content(html: &Html, node_id: NodeId) -> f64 {
    // Fix #12: skip if element itself, any ancestor, or any descendant
    // matches footnote list selectors
    if dom::self_or_ancestor_matches(html, node_id, FOOTNOTE_LIST_SELECTOR)
        || dom::has_descendant_matching(html, node_id, FOOTNOTE_LIST_SELECTOR)
    {
        return 0.0;
    }

    let text = dom::text_content(html, node_id);
    let word_count = dom::count_words(&text);

    if word_count < 3 {
        return 0.0;
    }

    let mut score = count_commas(&text) as f64;

    let nav_matches = NAV_INDICATORS_RE.find_iter(&text).count();
    score -= nav_matches as f64 * 10.0;

    // Reuse text for link density computation
    if dom::link_density_with_text(html, node_id, &text) > 0.5 {
        score -= 15.0;
    }

    score = apply_link_heavy_penalty(html, node_id, &text, score);

    let class_id = class_and_id(html, node_id);
    let pattern_matches = NON_CONTENT_RE.find_iter(&class_id).count();
    score -= pattern_matches as f64 * 8.0;

    if word_count < 15 && BYLINE_RE.is_match(&text) && DATE_RE.is_match(&text) {
        score -= 10.0;
    }

    let links = dom::descendant_elements_by_tag(html, node_id, "a").len();
    let lists = dom::descendant_elements_by_tag(html, node_id, "ul").len()
        + dom::descendant_elements_by_tag(html, node_id, "ol").len();
    if lists > 0 && links > lists * 3 {
        score -= 10.0;
    }

    score += score_social_and_card_penalties(html, node_id, word_count);

    // Counteract penalties for elements that look like article metadata
    // inside <main>/<article> (author lists, citation info, dates).
    if word_count < 50
        && is_inside_main_or_article(html, node_id)
        && is_article_metadata(html, node_id)
    {
        score += 20.0;
    }

    score
}

/// Additional non-content penalties for social links and card grids.
fn score_social_and_card_penalties(html: &Html, node_id: NodeId, word_count: usize) -> f64 {
    let mut penalty = 0.0;
    if word_count < 80 && has_social_profile_links(html, node_id) {
        penalty -= 15.0;
    }
    if is_card_grid(html, node_id) {
        penalty -= 15.0;
    }
    penalty
}

fn apply_link_heavy_penalty(html: &Html, node_id: NodeId, text: &str, mut score: f64) -> f64 {
    let links = dom::descendant_elements_by_tag(html, node_id, "a");
    let words = dom::count_words(text);
    if links.len() > 1 && words < 80 && dom::link_density_with_text(html, node_id, text) > 0.8 {
        score -= 15.0;
    }
    score
}

#[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests {
    use super::*;
    use scraper::Selector;

    /// Helper: parse a document and return the first element matching
    /// the given CSS selector.
    fn first_match(html: &Html, sel: &str) -> NodeId {
        let selector = Selector::parse(sel).unwrap();
        html.select(&selector).next().unwrap().id()
    }

    // ── score_element ───────────────────────────────────────────────

    #[test]
    fn score_element_high_for_paragraph_rich_div() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">
            <p>First paragraph with enough words to count as real prose content here.</p>
            <p>Second paragraph also containing substantial text for scoring purposes.</p>
            <p>Third paragraph providing additional content weight to this element.</p>
            <p>Fourth paragraph ensuring the word count is well above the threshold needed.</p>
            <p>Fifth paragraph, the final one, driving the total score even higher now.</p>
            </div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        let score = score_element(&doc, node);
        assert!(
            score > 50.0,
            "div with 5 paragraphs of text should score > 50, got {score}"
        );
    }

    #[test]
    fn score_element_low_for_nav_with_links() {
        let doc = Html::parse_document(
            r#"<html><body><nav id="target">
            <a href="/a">Link A</a>
            <a href="/b">Link B</a>
            <a href="/c">Link C</a>
            </nav></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        let score = score_element(&doc, node);
        assert!(
            score < 30.0,
            "nav with only links should score low, got {score}"
        );
    }

    // ── is_likely_content ───────────────────────────────────────────

    #[test]
    fn is_likely_content_article_with_text() {
        let words = "word ".repeat(150);
        let html_str =
            format!(r#"<html><body><article id="target"><p>{words}</p></article></body></html>"#);
        let doc = Html::parse_document(&html_str);
        let node = first_match(&doc, "#target");
        assert!(is_likely_content(&doc, node));
    }

    #[test]
    fn is_likely_content_nav_with_links_is_false() {
        let doc = Html::parse_document(
            r#"<html><body><nav id="target">
            <a href="/a">Link</a><a href="/b">Link</a><a href="/c">Link</a>
            </nav></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        assert!(!is_likely_content(&doc, node));
    }

    #[test]
    fn is_likely_content_div_with_content_class() {
        let doc = Html::parse_document(
            r#"<html><body>
            <div id="target" class="content">
                <p>Some paragraph text here with enough words to matter for scoring.</p>
            </div>
            </body></html>"#,
        );
        let node = first_match(&doc, "#target");
        assert!(is_likely_content(&doc, node));
    }

    #[test]
    fn is_likely_content_card_grid_is_false() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">
            <h2>Card One</h2><img src="a.jpg"><span>brief</span>
            <h3>Card Two</h3><img src="b.jpg"><span>brief</span>
            <h4>Card Three</h4><img src="c.jpg"><span>brief</span>
            </div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        assert!(!is_likely_content(&doc, node));
    }

    // ── score_non_content ───────────────────────────────────────────

    #[test]
    fn score_non_content_negative_for_high_link_density() {
        let mut links = String::new();
        for i in 0..20 {
            std::fmt::Write::write_fmt(
                &mut links,
                format_args!(r#"<a href="/p{i}">Link number {i} text</a> "#),
            )
            .expect("write to String is infallible");
        }
        let html_str =
            format!(r#"<html><body><div id="target"><p>{links}</p></div></body></html>"#);
        let doc = Html::parse_document(&html_str);
        let node = first_match(&doc, "#target");
        let score = score_non_content(&doc, node);
        assert!(
            score < 0.0,
            "high link density should produce negative score, got {score}"
        );
    }

    #[test]
    fn score_non_content_negative_for_copyright_text() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">
            <p>All rights reserved. Copyright 2025 Example Inc.</p>
            </div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        let score = score_non_content(&doc, node);
        assert!(
            score < 0.0,
            "div with 'all rights reserved' should score negative, got {score}"
        );
    }

    #[test]
    fn score_non_content_non_negative_for_regular_text() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">
            <p>This is a regular paragraph with normal prose content, discussing
            topics of interest. It contains commas, sentences, and reasonable text
            that should not be penalized by the scoring algorithm.</p>
            </div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        let score = score_non_content(&doc, node);
        assert!(
            score >= 0.0,
            "regular paragraph text should have non-negative score, got {score}"
        );
    }

    // ── is_article_metadata ─────────────────────────────────────────

    #[test]
    fn is_article_metadata_with_authors_label() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">Authors: Name1, Name2</div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        assert!(is_article_metadata(&doc, node));
    }

    #[test]
    fn is_article_metadata_false_for_subscribe_text() {
        let doc = Html::parse_document(
            r#"<html><body><div id="target">Click here to subscribe</div></body></html>"#,
        );
        let node = first_match(&doc, "#target");
        assert!(!is_article_metadata(&doc, node));
    }
}