domq 0.5.0

Tiny, zero-dependency, forgiving HTML parser: turn messy real-world HTML into a clean DOM tree (and JSON). WASM-first, no_std-friendly.
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
//! The streaming API (`domq::events`): the same recovery as the tree
//! builder, without the tree. The central guarantee, checked on arbitrary
//! input: replaying the events into a tree gives exactly the tree `parse`
//! builds — same nodes, attributes, text, source ranges, `to_html()` and
//! diagnostics — and `End` events always balance `Start` events.

use std::borrow::Cow;
use std::ops::Range;

use domq::{Event, NodeKind, NodeRef, parse};
use proptest::prelude::*;

// --- a minimal tree, built only from events ---------------------------------

#[derive(Debug, Clone, PartialEq)]
enum Kind {
    Element {
        name: String,
        attrs: Vec<(String, String)>,
    },
    Text(String),
    Comment(String),
    Doctype(String),
}

#[derive(Debug)]
struct Node {
    kind: Kind,
    range: Range<usize>,
    children: Vec<usize>,
}

#[derive(Debug, Default)]
struct Replay {
    nodes: Vec<Node>,
    roots: Vec<usize>,
}

/// Replay `html`'s events into a [`Replay`] tree, checking on the way that
/// every `End` closes the innermost open `Start` of the same name.
fn replay(html: &str) -> Result<Replay, TestCaseError> {
    let mut tree = Replay::default();
    let mut open: Vec<usize> = Vec::new();
    let add = |tree: &mut Replay, open: &[usize], kind: Kind, range: Range<usize>| {
        let id = tree.nodes.len();
        tree.nodes.push(Node {
            kind,
            range,
            children: Vec::new(),
        });
        match open.last() {
            Some(&p) => tree.nodes[p].children.push(id),
            None => tree.roots.push(id),
        }
        id
    };
    for (event, range) in domq::events(html) {
        prop_assert!(range.start <= range.end && range.end <= html.len());
        prop_assert!(
            html.get(range.clone()).is_some(),
            "range {:?} not on char boundaries",
            range
        );
        match event {
            Event::Start { name, attrs } => {
                let attrs = attrs
                    .into_iter()
                    .map(|(k, v)| (k.into_owned(), v.into_owned()))
                    .collect();
                let kind = Kind::Element {
                    name: name.into_owned(),
                    attrs,
                };
                let id = add(&mut tree, &open, kind, range);
                open.push(id);
            }
            Event::End { name, implied } => {
                let Some(id) = open.pop() else {
                    return Err(TestCaseError::fail(format!("unbalanced End {name}")));
                };
                let node = &mut tree.nodes[id];
                match &node.kind {
                    Kind::Element {
                        name: open_name, ..
                    } => prop_assert_eq!(open_name, &name),
                    _ => unreachable!("only elements are opened"),
                }
                if implied {
                    prop_assert!(range.is_empty(), "implied End with range {:?}", range);
                } else {
                    prop_assert!(html[range.clone()].starts_with("</"));
                }
                node.range.end = range.end;
            }
            Event::Text(t) => {
                prop_assert!(!t.is_empty());
                add(&mut tree, &open, Kind::Text(t.into_owned()), range);
            }
            Event::Comment(c) => {
                add(&mut tree, &open, Kind::Comment(c.to_string()), range);
            }
            Event::Doctype(d) => {
                add(&mut tree, &open, Kind::Doctype(d.to_string()), range);
            }
        }
    }
    prop_assert!(open.is_empty(), "Start without End: {:?}", open);
    Ok(tree)
}

fn kind_of(n: NodeRef<'_>) -> Kind {
    match n.kind() {
        NodeKind::Element { tag } => Kind::Element {
            name: tag.to_string(),
            attrs: n
                .attributes()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
        },
        NodeKind::Text(t) => Kind::Text(t.to_string()),
        NodeKind::Comment(c) => Kind::Comment(c.to_string()),
        NodeKind::Doctype(d) => Kind::Doctype(d.to_string()),
    }
}

/// The replayed tree and `parse`'s tree are the same, node for node.
fn assert_same_tree(html: &str, tree: &Replay) -> Result<(), TestCaseError> {
    let dom = parse(html);
    // Pre-order walks of both, side by side (explicit stacks: inputs can nest
    // deeply).
    let mut ours: Vec<(Kind, Option<Range<usize>>, usize)> = Vec::new();
    let mut stack: Vec<NodeRef<'_>> = dom.roots().collect();
    stack.reverse();
    while let Some(n) = stack.pop() {
        ours.push((kind_of(n), n.source_range(), n.children().count()));
        let mut kids: Vec<_> = n.children().collect();
        kids.reverse();
        stack.extend(kids);
    }
    let mut theirs: Vec<(Kind, Option<Range<usize>>, usize)> = Vec::new();
    let mut stack: Vec<usize> = tree.roots.iter().rev().copied().collect();
    while let Some(id) = stack.pop() {
        let n = &tree.nodes[id];
        theirs.push((n.kind.clone(), Some(n.range.clone()), n.children.len()));
        stack.extend(n.children.iter().rev());
    }
    prop_assert_eq!(ours, theirs);
    Ok(())
}

// --- serializing the replayed tree with `to_html`'s rules -------------------

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

fn escape(out: &mut String, s: &str, attr: bool) {
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '"' if attr => out.push_str("&quot;"),
            '<' if !attr => out.push_str("&lt;"),
            '>' if !attr => out.push_str("&gt;"),
            c => out.push(c),
        }
    }
}

fn to_html(tree: &Replay) -> String {
    enum Step {
        Open(usize, bool),
        Close(String),
    }
    let mut out = String::new();
    let mut stack: Vec<Step> = tree
        .roots
        .iter()
        .rev()
        .map(|&r| Step::Open(r, false))
        .collect();
    while let Some(step) = stack.pop() {
        match step {
            Step::Close(tag) => {
                out.push_str("</");
                out.push_str(&tag);
                out.push('>');
            }
            Step::Open(id, raw_parent) => match &tree.nodes[id].kind {
                Kind::Element { name, attrs } => {
                    out.push('<');
                    out.push_str(name);
                    for (k, v) in attrs {
                        out.push(' ');
                        out.push_str(k);
                        if !v.is_empty() {
                            out.push_str("=\"");
                            escape(&mut out, v, true);
                            out.push('"');
                        }
                    }
                    out.push('>');
                    if is_void(name) {
                        continue;
                    }
                    stack.push(Step::Close(name.clone()));
                    let raw = matches!(name.as_str(), "script" | "style");
                    for &c in tree.nodes[id].children.iter().rev() {
                        stack.push(Step::Open(c, raw));
                    }
                }
                Kind::Text(t) if raw_parent => out.push_str(t),
                Kind::Text(t) => escape(&mut out, t, false),
                Kind::Comment(c) => {
                    out.push_str("<!--");
                    out.push_str(c);
                    out.push_str("-->");
                }
                Kind::Doctype(d) => {
                    out.push_str("<!doctype ");
                    out.push_str(d);
                    out.push('>');
                }
            },
        }
    }
    out
}

/// Every guarantee at once, for one input.
fn check(html: &str) -> Result<(), TestCaseError> {
    let tree = replay(html)?;
    assert_same_tree(html, &tree)?;
    let dom = parse(html);
    prop_assert_eq!(to_html(&tree), dom.to_html());
    let mut events = domq::events(html);
    for _ in events.by_ref() {}
    prop_assert_eq!(events.errors(), dom.errors());
    Ok(())
}

// --- property tests ---------------------------------------------------------

/// HTML-ish soup from a small alphabet of structural characters (as in
/// `tests/properties.rs`).
fn html_soup() -> impl Strategy<Value = String> {
    let alphabet = vec![
        '<', '>', '/', '=', '"', '\'', ' ', '\n', '!', '-', '?', '&', ';', '#', '[', ']', 'a', 'b',
        'p', 'l', 'i', '1', '\u{0}', '🦀',
    ];
    proptest::collection::vec(proptest::sample::select(alphabet), 0..300)
        .prop_map(|v| v.into_iter().collect())
}

/// Markup built from real tags, so implicit closes, void elements, raw text,
/// mismatched and stray end tags, entities and attributes all come up.
fn html_pieces() -> impl Strategy<Value = String> {
    let pieces = vec![
        "<div>",
        "</div>",
        "<DIV class=a>",
        "<p>",
        "</p>",
        "<li>",
        "<ul>",
        "</ul>",
        "<b>",
        "</b>",
        "</i>",
        "<br>",
        "<br/>",
        "<img src=x alt='a &amp; b'>",
        "<div/>",
        "<!--c-->",
        "<!doctype html>",
        "text",
        " ",
        "&amp;",
        "&bogus;",
        "&#169;",
        "<td>",
        "<tr>",
        "<table>",
        "</table>",
        "<script>if (a < b) x = '&amp;';</script>",
        "<style>p{}</style>",
        "<title>a &amp; b</title>",
        "<textarea>&lt;</textarea>",
        "<head>",
        "<meta charset=utf-8>",
        "</head>",
        "<colgroup>",
        "<col>",
        "<option>",
        "<select>",
        "<h1>",
        "<h2>",
        "<![CDATA[x<y]]>",
        "<?php ?>",
        "é",
        "<a href=\"/x?a=1&b=2\">",
        "</a>",
        "<p a=1 a=2 B=3>",
        "< x",
        "</>",
        "<x-y:z.w>",
    ];
    proptest::collection::vec(proptest::sample::select(pieces), 0..40).prop_map(|v| v.concat())
}

proptest! {
    #![proptest_config(ProptestConfig { cases: 2048, ..ProptestConfig::default() })]

    #[test]
    fn events_replay_to_the_parsed_tree_on_soup(s in html_soup()) {
        check(&s)?;
    }

    #[test]
    fn events_replay_to_the_parsed_tree_on_markup(s in html_pieces()) {
        check(&s)?;
    }

    #[test]
    fn events_never_panic_on_any_text(s in ".{0,200}") {
        check(&s)?;
    }
}

// --- focused cases ----------------------------------------------------------

fn collect(html: &str) -> Vec<(Event<'_>, Range<usize>)> {
    domq::events(html).collect()
}

fn start<'a>(name: &'a str, attrs: &[(&'a str, &'a str)]) -> Event<'a> {
    Event::Start {
        name: Cow::Borrowed(name),
        attrs: attrs
            .iter()
            .map(|&(k, v)| (Cow::Borrowed(k), Cow::Borrowed(v)))
            .collect(),
    }
}

fn end(name: &str, implied: bool) -> Event<'_> {
    Event::End {
        name: Cow::Borrowed(name),
        implied,
    }
}

fn text(t: &str) -> Event<'_> {
    Event::Text(Cow::Borrowed(t))
}

#[test]
fn implicit_closes_void_elements_and_entities() {
    let html = "<ul><li>a &amp; b<li>c<br></ul>";
    assert_eq!(
        collect(html),
        [
            (start("ul", &[]), 0..4),
            (start("li", &[]), 4..8),
            (text("a & b"), 8..17),
            (end("li", true), 17..17), // closed by the next <li>
            (start("li", &[]), 17..21),
            (text("c"), 21..22),
            (start("br", &[]), 22..26),
            (end("br", true), 26..26), // void: ends with its start tag
            (end("li", true), 26..26), // closed by </ul>
            (end("ul", false), 26..31),
        ]
    );
}

#[test]
fn mismatched_and_stray_end_tags() {
    // </div> closes the <b> it skips over; </i> matches nothing and is dropped.
    let html = "<div><b>x</i></div>";
    assert_eq!(
        collect(html),
        [
            (start("div", &[]), 0..5),
            (start("b", &[]), 5..8),
            (text("x"), 8..9),
            (end("b", true), 13..13),
            (end("div", false), 13..19),
        ]
    );
    let mut events = domq::events(html);
    for _ in events.by_ref() {}
    let kinds: Vec<_> = events.errors().iter().map(|e| e.kind.clone()).collect();
    assert_eq!(
        kinds,
        [
            domq::ErrorKind::StrayEndTag,
            domq::ErrorKind::MismatchedEndTag
        ]
    );
}

#[test]
fn attributes_are_lowercased_decoded_and_kept_in_order() {
    let html = r#"<A HREF="/x?a=1&amp;b=2" Download data-x='&lt;' href=dup>"#;
    let events = collect(html);
    assert_eq!(
        events[0].0,
        start(
            "a",
            &[
                ("href", "/x?a=1&b=2"),
                ("download", ""),
                ("data-x", "<"),
                ("href", "dup")
            ]
        )
    );
    // Open at the end of the input: implied End there.
    assert_eq!(events[1], (end("a", true), html.len()..html.len()));
}

#[test]
fn raw_text_is_not_decoded_but_rcdata_is() {
    let html = "<script>a &amp;&lt; b</script><title>a &amp; b</title><p>&nbsp;<![CDATA[<x>]]>";
    let texts: Vec<_> = collect(html)
        .into_iter()
        .filter_map(|(e, _)| match e {
            Event::Text(t) => Some(t.into_owned()),
            _ => None,
        })
        .collect();
    assert_eq!(texts, ["a &amp;&lt; b", "a & b", "\u{a0}", "<x>"]);
}

#[test]
fn self_closing_comments_doctypes() {
    let html = "<!DOCTYPE html ><div/><!--hi-->";
    assert_eq!(
        collect(html),
        [
            (Event::Doctype("html"), 0..16),
            (start("div", &[]), 16..22),
            (end("div", true), 22..22),
            (Event::Comment("hi"), 22..31),
        ]
    );
}

#[test]
fn text_closes_an_open_head() {
    let html = "<head><title>t</title>Hello";
    let names: Vec<_> = collect(html)
        .into_iter()
        .map(|(e, r)| match e {
            Event::Start { name, .. } => format!("+{name}@{}", r.start),
            Event::End { name, .. } => format!("-{name}@{}", r.end),
            Event::Text(t) => format!("{t:?}"),
            _ => String::new(),
        })
        .collect();
    assert_eq!(
        names,
        [
            "+head@0",
            "+title@6",
            "\"t\"",
            "-title@22",
            "-head@22",
            "\"Hello\""
        ]
    );
}

#[test]
fn borrowed_where_possible() {
    // Lower-case names and entity-free text borrow from the input.
    for (event, _) in domq::events("<p class=x>plain</p>") {
        match event {
            Event::Start { name, attrs } => {
                assert!(matches!(name, Cow::Borrowed(_)));
                assert!(
                    attrs
                        .iter()
                        .all(|(k, v)| matches!((k, v), (Cow::Borrowed(_), Cow::Borrowed(_))))
                );
            }
            Event::End { name, .. } => assert!(matches!(name, Cow::Borrowed(_))),
            Event::Text(t) => assert!(matches!(t, Cow::Borrowed(_))),
            _ => {}
        }
    }
}

#[test]
fn deep_nesting_streams_without_recursion() {
    let n = 100_000;
    let html = "<div>".repeat(n) + &"</x>".repeat(n) + &"</div>".repeat(n);
    let mut depth = 0usize;
    let mut max = 0usize;
    let mut events = domq::events(&html);
    for (event, _) in events.by_ref() {
        match event {
            Event::Start { .. } => {
                depth += 1;
                max = max.max(depth);
            }
            Event::End { implied, .. } => {
                assert!(!implied);
                depth -= 1;
            }
            _ => unreachable!(),
        }
    }
    assert_eq!((depth, max), (0, n));
    assert_eq!(events.errors().len(), n); // every </x> is stray
    // Fused: stays exhausted.
    assert!(events.next().is_none());
}

#[test]
fn bounded_end_tag_search_agrees_with_parse() {
    // End tags reach the 256 innermost and 8 outermost open elements; both
    // sides of each bound, plus unknown entities (reported at their `&`).
    let wrap = "<div>".repeat(8);
    for k in [254, 255, 256, 257, 1000] {
        let b = "<b>".repeat(k);
        for html in [
            format!("{wrap}<main>{b}</main><p>x&bogus;</p>"),
            format!("<html><body>{wrap}{b}</div></body></html>tail"),
            format!("<body>{b}<a title='&copy'>&ampx</a></body>&nosuch;"),
        ] {
            check(&html).unwrap();
        }
    }
}

#[test]
fn the_empty_document_has_no_events() {
    assert!(collect("").is_empty());
    let mut events = domq::events("");
    assert!(events.next().is_none());
    assert!(events.errors().is_empty());
}