legible 0.4.3

A Rust port of Mozilla's Readability.js for extracting 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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! Content scoring logic for Readability.

use crate::constants::{flags::*, is_div_to_p_elem, is_phrasing_elem, regexps};
use crate::dom::{NodeDataStore, NodeStats, get_tag_name, has_tag_name};
use crate::selectors::Selectors;
use dom_query::{Node, NodeData};

/// Check if a URL is a hash URL (starts with '#' and has content after it).
/// Equivalent to the regex `^#.+` but avoids regex overhead.
#[inline]
fn is_hash_url(s: &str) -> bool {
    s.starts_with('#') && s.len() > 1
}

/// Get or compute stats for a node, caching the result.
pub fn get_or_compute_stats(node: &Node<'_>, store: &mut NodeDataStore) -> NodeStats {
    if let Some(stats) = store.get_stats(&node.id) {
        return *stats;
    }

    // Avoid populating the cache for every inline descendant of small subtrees.
    // Those nodes are unlikely to be queried independently, so a single direct
    // scan is cheaper than many hash-table insertions.
    if let Some(stats) = stats_for_small_subtree(node, 64) {
        store.set_stats(node.id, stats);
        return stats;
    }

    // Compute the subtree bottom-up so each text node is scanned once even when
    // stats are later requested for several nested ancestors.
    let mut stack = vec![(*node, false)];
    while let Some((current, expanded)) = stack.pop() {
        if store.get_stats(&current.id).is_some() {
            continue;
        }

        if !expanded {
            stack.push((current, true));
            stack.extend(
                current
                    .children_it(true)
                    .filter(|child| store.get_stats(&child.id).is_none())
                    .map(|child| (child, false)),
            );
            continue;
        }

        let mut stats = current
            .query(|tree_node| match &tree_node.data {
                NodeData::Text { contents } => stats_for_text(contents),
                _ => NodeStats::default(),
            })
            .unwrap_or_default();

        for child in current.children_it(false) {
            if let Some(child_stats) = store.get_stats(&child.id) {
                append_stats(&mut stats, child_stats);
            }
        }
        stats.has_sentence_end = stats.has_sentence_break || stats.ends_with_dot;
        store.set_stats(current.id, stats);
    }

    store.get_stats(&node.id).copied().unwrap_or_default()
}

fn stats_for_small_subtree(node: &Node<'_>, max_nodes: usize) -> Option<NodeStats> {
    let mut stats = NodeStats::default();

    for (index, descendant) in std::iter::once(*node)
        .chain(node.descendants_it())
        .enumerate()
    {
        if index == max_nodes {
            return None;
        }
        descendant.query(|tree_node| {
            if let NodeData::Text { contents } = &tree_node.data {
                append_stats(&mut stats, &stats_for_text(contents));
            }
        });
    }

    Some(stats)
}

fn stats_for_text(text: &str) -> NodeStats {
    let mut stats = NodeStats {
        has_text: !text.is_empty(),
        starts_with_whitespace: text.starts_with(char::is_whitespace),
        ends_with_whitespace: text.ends_with(char::is_whitespace),
        ..NodeStats::default()
    };
    let mut previous_was_whitespace = true;
    let mut last_was_dot = false;

    for c in text.chars() {
        if c.is_whitespace() {
            stats.has_sentence_break |= last_was_dot;
            last_was_dot = false;
            if !previous_was_whitespace {
                stats.text_length += 1;
                previous_was_whitespace = true;
            }
        } else {
            stats.has_non_whitespace = true;
            last_was_dot = c == '.';
            stats.comma_count += usize::from(
                c == ','
                    || (c as u32 >= 0x0600
                        && matches!(
                            c,
                            '\u{060C}'
                                | '\u{FE50}'
                                | '\u{FE10}'
                                | '\u{FE11}'
                                | '\u{2E41}'
                                | '\u{2E34}'
                                | '\u{2E32}'
                                | '\u{FF0C}'
                        )),
            );
            stats.text_length += 1;
            previous_was_whitespace = false;
        }
    }

    if previous_was_whitespace && stats.text_length > 0 {
        stats.text_length -= 1;
    }
    stats.ends_with_dot = last_was_dot;
    stats.has_sentence_end = stats.has_sentence_break || stats.ends_with_dot;
    stats
}

fn append_stats(stats: &mut NodeStats, child: &NodeStats) {
    if !child.has_text {
        return;
    }

    if !stats.has_text {
        *stats = *child;
        return;
    }

    stats.has_sentence_break |=
        child.has_sentence_break || (stats.ends_with_dot && child.starts_with_whitespace);
    if stats.has_non_whitespace
        && child.has_non_whitespace
        && (stats.ends_with_whitespace || child.starts_with_whitespace)
    {
        stats.text_length += 1;
    }
    stats.text_length += child.text_length;
    stats.comma_count += child.comma_count;
    stats.has_non_whitespace |= child.has_non_whitespace;
    stats.ends_with_whitespace = child.ends_with_whitespace;
    stats.ends_with_dot = child.ends_with_dot;
    stats.has_sentence_end = stats.has_sentence_break || stats.ends_with_dot;
}

/// Compute the initial readability data for a node without storing it.
/// Used with NodeDataStore::initialize_if_absent for single-lookup initialization.
pub fn compute_initial_readability_data(
    node: &Node<'_>,
    flags: u32,
) -> crate::dom::ReadabilityData {
    let initial_score = match get_tag_name(node).as_deref() {
        Some("DIV") => 5.0,
        Some("PRE") | Some("TD") | Some("BLOCKQUOTE") => 3.0,
        Some("ADDRESS") | Some("OL") | Some("UL") | Some("DL") | Some("DD") | Some("DT")
        | Some("LI") | Some("FORM") => -3.0,
        Some("H1") | Some("H2") | Some("H3") | Some("H4") | Some("H5") | Some("H6")
        | Some("TH") => -5.0,
        _ => 0.0,
    };

    let class_weight = get_class_weight(node, flags);
    crate::dom::ReadabilityData::with_score(initial_score + class_weight as f64)
}

/// Initialize a node with readability data and initial score based on tag.
pub fn initialize_node(node: &Node<'_>, store: &mut NodeDataStore, flags: u32) {
    store.set(node.id, compute_initial_readability_data(node, flags));
}

/// Get the class/id weight of an element.
/// Positive weight for content-like classes, negative for non-content.
/// Uses RegexSet for efficient single-pass matching.
pub fn get_class_weight(node: &Node<'_>, flags: u32) -> i32 {
    if (flags & FLAG_WEIGHT_CLASSES) == 0 {
        return 0;
    }

    let mut weight: i32 = 0;

    // Check class name using RegexSet for 2 matches in single pass
    if let Some(class_name) = node.attr("class") {
        let class_str = class_name.as_ref();
        if !class_str.is_empty() {
            let matches = regexps::CLASS_WEIGHT_SET.matches(class_str);
            if matches.matched(0) {
                weight -= 25; // NEGATIVE matched
            }
            if matches.matched(1) {
                weight += 25; // POSITIVE matched
            }
        }
    }

    // Check ID using RegexSet for 2 matches in single pass
    if let Some(id) = node.attr("id") {
        let id_str = id.as_ref();
        if !id_str.is_empty() {
            let matches = regexps::CLASS_WEIGHT_SET.matches(id_str);
            if matches.matched(0) {
                weight -= 25; // NEGATIVE matched
            }
            if matches.matched(1) {
                weight += 25; // POSITIVE matched
            }
        }
    }

    weight
}

/// Check if a node has non-whitespace inner text, without allocating a String.
/// This is an optimized alternative to `!get_inner_text(n, false).is_empty()`.
pub fn has_non_empty_inner_text(node: &Node<'_>) -> bool {
    has_non_whitespace_text(node)
}

/// Get the inner text of a node, optionally normalizing whitespace.
pub fn get_inner_text(node: &Node<'_>, normalize_spaces: bool) -> String {
    let text = node.text();
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return String::new();
    }
    if normalize_spaces {
        normalize_whitespace(trimmed)
    } else if trimmed.as_ptr() == text.as_ptr() && trimmed.len() == text.len() {
        // Text is already trimmed — avoid allocation when callers need ownership
        text.to_string()
    } else {
        trimmed.to_string()
    }
}

/// Collapse runs of 2+ whitespace characters into a single space.
/// Returns the original string (as a new allocation) if no collapsing is needed.
fn normalize_whitespace(s: &str) -> String {
    // Quick pre-check: only allocate a new string if there are consecutive whitespace chars
    let needs_normalize = s
        .as_bytes()
        .windows(2)
        .any(|w| w[0].is_ascii_whitespace() && w[1].is_ascii_whitespace())
        || s.bytes().any(|b| b == b'\t' || b == b'\n' || b == b'\r');
    if !needs_normalize {
        return s.to_string();
    }
    let mut result = String::with_capacity(s.len());
    let mut prev_ws = false;
    for c in s.chars() {
        if c.is_whitespace() {
            if !prev_ws {
                result.push(' ');
            }
            prev_ws = true;
        } else {
            result.push(c);
            prev_ws = false;
        }
    }
    result
}

/// Get the link density of an element with optional pre-extracted text.
/// Use this when you already have the inner text to avoid redundant extraction.
pub fn get_link_density_with_text(
    node: &Node<'_>,
    node_text: Option<&str>,
    _selectors: &Selectors,
) -> f64 {
    let text_length = match node_text {
        Some(t) => t.chars().count(),
        None => node.normalized_char_count(),
    };
    if text_length == 0 {
        return 0.0;
    }

    let mut link_length = 0.0;

    for link in node
        .descendants_it()
        .filter(|descendant| has_tag_name(descendant, "a"))
    {
        // Check href directly without allocating a new String
        let coefficient = match link.attr("href") {
            Some(href) if is_hash_url(href.as_ref()) => 0.3,
            _ => 1.0,
        };
        link_length += link.normalized_char_count() as f64 * coefficient;
    }

    link_length / text_length as f64
}

/// Get the link density of an element (ratio of link text to total text).
pub fn get_link_density(node: &Node<'_>, selectors: &Selectors) -> f64 {
    get_link_density_with_text(node, None, selectors)
}

/// Get the link density using a pre-computed parent text length.
/// Caches link text stats for efficiency.
pub fn get_link_density_cached(
    node: &Node<'_>,
    parent_text_length: usize,
    store: &mut NodeDataStore,
    _selectors: &Selectors,
) -> f64 {
    if parent_text_length == 0 {
        return 0.0;
    }

    let mut link_length = 0.0;

    for link in node
        .descendants_it()
        .filter(|descendant| has_tag_name(descendant, "a"))
    {
        // Get or compute stats for the link
        let link_stats = get_or_compute_stats(&link, store);

        // Check href directly without allocating a new String
        let coefficient = match link.attr("href") {
            Some(href) if is_hash_url(href.as_ref()) => 0.3,
            _ => 1.0,
        };
        link_length += link_stats.text_length as f64 * coefficient;
    }

    link_length / parent_text_length as f64
}

/// Check if a node is whitespace.
pub fn is_whitespace(node: &Node<'_>) -> bool {
    if node.is_text() {
        let text = node.text();
        return text.trim().is_empty();
    }
    if node.is_element()
        && let Some(tag) = get_tag_name(node)
    {
        return tag == "BR";
    }
    false
}

/// Check if a node qualifies as phrasing content.
pub fn is_phrasing_content(node: &Node<'_>) -> bool {
    is_phrasing_content_depth(node, 0)
}

fn is_phrasing_content_depth(node: &Node<'_>, depth: u32) -> bool {
    if node.is_text() {
        return true;
    }

    if let Some(tag) = get_tag_name(node) {
        if is_phrasing_elem(&tag) {
            return true;
        }

        // A, DEL, INS are phrasing content if all their children are.
        // Depth-limited to prevent excessive recursion on pathological DOMs.
        if (tag == "A" || tag == "DEL" || tag == "INS") && depth < 10 {
            return node
                .children()
                .iter()
                .all(|child| is_phrasing_content_depth(child, depth + 1));
        }
    }

    false
}

/// Wrap consecutive phrasing content in P tags by moving existing nodes.
/// This handles cases where text is placed directly inside DIVs without P tags.
pub fn wrap_phrasing_content_in_p(div: &Node<'_>) {
    let children: Vec<_> = div.children();
    let mut i = 0;

    while i < children.len() {
        let child = &children[i];

        // If this is phrasing content, collect consecutive phrasing content nodes
        if is_phrasing_content(child) {
            let mut j = i;
            let mut has_content = false;

            // Collect all consecutive phrasing content
            while j < children.len() && is_phrasing_content(&children[j]) {
                let node = &children[j];
                has_content |= !node.is_text() || !node.text().trim().is_empty();
                j += 1;
            }

            // Only wrap if we collected content (not just whitespace)
            if has_content {
                // Trim leading/trailing whitespace using index tracking - O(n) instead of O(n²)
                let mut start = i;
                let mut end = j;

                // Trim leading whitespace nodes
                while start < end && is_whitespace(&children[start]) {
                    start += 1;
                }

                // Trim trailing whitespace nodes
                while start < end && is_whitespace(&children[end - 1]) {
                    end -= 1;
                }

                // Only wrap if we still have content after trimming
                if start < end
                    && let Some(first_node) = children.get(start)
                {
                    let p = div.tree.new_element("p");
                    first_node.insert_before(&p);

                    for node in &children[start..end] {
                        p.append_child(node);
                    }

                    for node in children[i..start].iter().chain(children[end..j].iter()) {
                        node.remove_from_parent();
                    }
                }
            }

            i = j;
        } else {
            i += 1;
        }
    }
}

/// Check if an element has no content.
pub fn is_element_without_content(node: &Node<'_>) -> bool {
    if !node.is_element() {
        return false;
    }

    if has_non_whitespace_text(node) {
        return false;
    }

    // Check direct element children without allocating an intermediate Vec.
    node.children_it(false)
        .filter(|child| child.is_element())
        .all(|child| has_tag_name(&child, "BR") || has_tag_name(&child, "HR"))
}

/// Check if this node has only whitespace and a single element with given tag.
pub fn has_single_tag_inside_element(node: &Node<'_>, tag: &str) -> bool {
    let mut found_element = false;

    for child in node.children_it(false) {
        if child.is_element() {
            if found_element || !has_tag_name(&child, tag) {
                return false;
            }
            found_element = true;
        } else if child.is_text()
            && child
                .text()
                .as_ref()
                .ends_with(|c: char| !c.is_whitespace())
        {
            return false;
        }
    }

    found_element
}

/// Check if an element has any children that are block-level elements.
pub fn has_child_block_element(node: &Node<'_>) -> bool {
    node.descendants_it()
        .filter(|child| child.is_element())
        .any(|child| get_tag_name(&child).is_some_and(|tag| is_div_to_p_elem(&tag)))
}

/// Check if a node is probably visible (not hidden).
pub fn is_probably_visible(node: &Node<'_>) -> bool {
    // Check style attribute for display:none or visibility:hidden,
    // ignoring case and whitespace variations. Both patterns are checked
    // in a single pass through the style string.
    if let Some(style) = node.attr("style") {
        let style_str = style.as_ref();
        if has_hidden_style(style_str) {
            return false;
        }
    }

    // Check for hidden attribute
    if node.has_attr("hidden") {
        return false;
    }

    // Check aria-hidden, but allow fallback-image class
    if let Some(aria_hidden) = node.attr("aria-hidden")
        && aria_hidden.as_ref() == "true"
    {
        if let Some(class) = node.attr("class") {
            if !class.as_ref().contains("fallback-image") {
                return false;
            }
        } else {
            return false;
        }
    }

    true
}

/// Check if a node is a valid byline element.
pub fn is_valid_byline(node: &Node<'_>, match_string: &str) -> bool {
    let is_byline_attr = node.attr("rel").is_some_and(|rel| rel.as_ref() == "author")
        || node
            .attr("itemprop")
            .is_some_and(|ip| ip.as_ref().contains("author"))
        || regexps::BYLINE.is_match(match_string);

    if !is_byline_attr {
        return false;
    }

    let text = node.text();
    let trimmed = text.trim();
    // Short-circuit: a UTF-8 char is at most 4 bytes, so < 400 bytes means < 100 chars.
    !trimmed.is_empty() && trimmed.len() < 400 && trimmed.chars().count() < 100
}

/// Check if node is image or contains exactly one image.
pub fn is_single_image(node: &Node<'_>) -> bool {
    let mut current = *node;
    let mut checked_text = false;

    loop {
        let n = current;
        if let Some(tag) = get_tag_name(&n)
            && tag == "IMG"
        {
            return true;
        }

        // If the outer subtree has no text, none of the nested single-child
        // wrappers can have text either. Avoid rescanning the same subtree at
        // each level.
        if !checked_text {
            if has_non_whitespace_text(&n) {
                return false;
            }
            checked_text = true;
        }

        let mut children = n.children_it(false).filter(|child| child.is_element());
        let Some(child) = children.next() else {
            return false;
        };
        if children.next().is_some() {
            return false;
        }

        current = child;
    }
}

/// Check whether a node or any descendant text node contains non-whitespace text.
/// This avoids constructing the full concatenated descendant text when callers only
/// need an emptiness check.
fn has_non_whitespace_text(node: &Node<'_>) -> bool {
    if node.is_text() {
        return node.text().chars().any(|c| !c.is_whitespace());
    }

    node.descendants_it().any(|descendant| {
        descendant.is_text() && descendant.text().chars().any(|c| !c.is_whitespace())
    })
}

/// Check if a style string contains "display:none" or "visibility:hidden"
/// (case-insensitive, whitespace-tolerant). Scans the string once for both patterns.
fn has_hidden_style(haystack: &str) -> bool {
    let hbytes = haystack.as_bytes();
    let hlen = hbytes.len();
    if hlen == 0 {
        return false;
    }

    // Both patterns contain no whitespace and are lowercase. We scan for
    // 'd' (display) or 'v' (visibility) as starting anchors.
    let display_pat: &[u8] = b"display:none";
    let vis_pat: &[u8] = b"visibility:hidden";

    let mut i = 0;
    while i < hlen {
        let b = hbytes[i].to_ascii_lowercase();
        let needle = if b == b'd' {
            display_pat
        } else if b == b'v' {
            vis_pat
        } else {
            i += 1;
            continue;
        };

        let needle_len = needle.len();
        if i + needle_len > hlen {
            i += 1;
            continue;
        }

        let mut hi = i;
        let mut ni = 0;
        let mut matches = true;
        while ni < needle_len && hi < hlen {
            if hbytes[hi].is_ascii_whitespace() {
                hi += 1;
                continue;
            }
            if hbytes[hi].to_ascii_lowercase() != needle[ni] {
                matches = false;
                break;
            }
            hi += 1;
            ni += 1;
        }
        if matches && ni == needle_len {
            return true;
        }
        i += 1;
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use dom_query::Document;

    fn concatenated_stats(text: &str) -> NodeStats {
        let mut expected = NodeStats::default();
        let mut previous_was_whitespace = true;
        let mut last_was_dot = false;
        for c in text.chars() {
            if c.is_whitespace() {
                expected.has_sentence_end |= last_was_dot;
                last_was_dot = false;
                if !previous_was_whitespace {
                    expected.text_length += 1;
                    previous_was_whitespace = true;
                }
            } else {
                last_was_dot = c == '.';
                expected.comma_count += usize::from(
                    c == ','
                        || matches!(
                            c,
                            '\u{060C}'
                                | '\u{FE50}'
                                | '\u{FE10}'
                                | '\u{FE11}'
                                | '\u{2E41}'
                                | '\u{2E34}'
                                | '\u{2E32}'
                                | '\u{FF0C}'
                        ),
                );
                expected.text_length += 1;
                previous_was_whitespace = false;
            }
        }
        if previous_was_whitespace && expected.text_length > 0 {
            expected.text_length -= 1;
        }
        expected.has_sentence_end |= last_was_dot;
        expected
    }

    fn assert_stats_match(node: &Node<'_>, store: &mut NodeDataStore) {
        let expected = concatenated_stats(&node.text());
        let actual = get_or_compute_stats(node, store);
        assert_eq!(actual.text_length, expected.text_length);
        assert_eq!(actual.comma_count, expected.comma_count);
        assert_eq!(actual.has_sentence_end, expected.has_sentence_end);
    }

    #[test]
    fn cached_node_stats_match_concatenated_text_semantics() {
        let cases = [
            "<div>  alpha,<span> beta.</span>\n<strong>gamma\u{060c}</strong>  </div>",
            "<div><span>not.</span><span>ended</span></div>",
            "<div><span>end.</span><i> </i><span>next</span></div>",
            "<div> \n <span>one</span><i></i> <b>two</b> </div>",
        ];

        for html in cases {
            let doc = Document::from(html);
            let node = doc.select("div").nodes().first().copied().unwrap();
            assert_stats_match(&node, &mut NodeDataStore::new());
        }
    }

    #[test]
    fn bottom_up_stats_match_large_mixed_subtree() {
        let mut html = String::from("<div> leading.");
        for index in 0..70 {
            match index % 4 {
                0 => html.push_str("<span> word,</span>"),
                1 => html.push_str("<i> </i>"),
                2 => html.push_str("<b>sentence.</b>\n"),
                _ => html.push_str("<em>joined</em><strong>text</strong>"),
            }
        }
        html.push_str(" trailing\u{060c}</div>");

        let doc = Document::from(html);
        let node = doc.select("div").nodes().first().copied().unwrap();
        assert!(node.descendants_it().count() > 64);
        assert_stats_match(&node, &mut NodeDataStore::new());
    }

    #[test]
    fn clearing_stats_recomputes_a_mutated_large_subtree() {
        let mut html = String::from("<div>");
        for _ in 0..70 {
            html.push_str("<span>cached text, </span>");
        }
        html.push_str("</div>");

        let doc = Document::from(html);
        let node = doc.select("div").nodes().first().copied().unwrap();
        let removed = doc.select("span").nodes().last().copied().unwrap();
        let mut store = NodeDataStore::new();

        let before = get_or_compute_stats(&node, &mut store);
        removed.remove_from_parent();
        let expected = concatenated_stats(&node.text());
        assert_ne!(before.text_length, expected.text_length);
        assert_eq!(
            get_or_compute_stats(&node, &mut store).text_length,
            before.text_length
        );

        store.clear_stats();
        assert_stats_match(&node, &mut store);
    }
}