html5-parser 0.4.0

A Rust WHATWG HTML5 tokenizer and tree-construction implementation
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
// Public API surface: `parse` plus the read-only tree types
// (`Document`/`NodeId`/`NodeKind`/`Attribute`/`Position`/`Node`/`Children`)
// needed to walk its output — just enough for html-conform's
// `src/infoset.rs::normalize()` to consume, per Step 1 of this crate's
// two-stage scope (see `CLAUDE.md`). `Tokenizer`/`TreeBuilder` and
// everything else stay crate-internal; there's no commitment to their
// shape yet. See plan/DECISIONS.md.

mod document;
mod entities;
mod tokenizer;
mod tree_builder;

pub use document::{Attribute, Children, Document, Node, NodeId, NodeKind};
pub use tokenizer::{ParseError, ParseErrorKind, Position};

use tokenizer::Tokenizer;
use tree_builder::TreeBuilder;

/// [`parse`]'s return value: the parsed [`Document`] tree plus every
/// WHATWG "parse error" (§13.2.2) encountered along the way. Both fields
/// together, not a `Result` — parse errors are never fatal, `document`
/// is always complete regardless of how many occurred (see
/// [`ParseError`]'s doc comment).
#[derive(Debug)]
pub struct ParseResult {
    pub document: Document,
    pub errors: Vec<ParseError>,
}

/// The driver loop (§13.2 "Parsing HTML documents", the "tokenization and
/// tree construction" step): parses `input` into a [`Document`] tree with
/// per-node source positions, feeding it through the tokenizer and handing
/// each token to the tree builder, applying the two pieces of feedback
/// tree construction sends back to the tokenizer — a state switch
/// (`Tokenizer::switch_to`, for RCDATA/RAWTEXT/script-data/PLAINTEXT
/// elements) and the foreign-content flag (`Tokenizer::set_in_foreign_content`,
/// consulted only by CDATA-section handling).
///
/// The tokenizer's iterator yields exactly one `Eof` token and then ends
/// (`None`) on the next call, so the loop needs no separate condition for
/// *when* to stop feeding it tokens. `TreeBuilder::stop_parsing` (§13.2.7
/// "The end") still runs once, explicitly, right after — its one
/// tree-shape-relevant step ("pop all the nodes off the stack of open
/// elements") isn't implied by the loop simply ending.
///
/// Returns [`ParseResult`], not a bare [`Document`], as of Phase 07
/// (`plan/07-parse-errors.md`) — `errors` covers every tokenizer-level
/// parse error (`src/tokenizer.rs`'s `error()` call sites) plus, as of
/// Phase 08 (`plan/08-tree-construction-errors.md`), the
/// tree-construction-level (§13.2.6) conditions listed there. Both
/// sources are merged and sorted by source position, so `errors` is
/// always in document order regardless of which stage produced each
/// entry (the two stages interleave: the tokenizer runs ahead of the
/// tree builder token by token).
pub fn parse(input: &str) -> ParseResult {
    let mut tokenizer = Tokenizer::new(input);
    let mut tree_builder = TreeBuilder::new();
    while let Some(token) = tokenizer.next() {
        if let Some(state) = tree_builder.process_token(&token.kind, token.position) {
            tokenizer.switch_to(state);
        }
        tokenizer.set_in_foreign_content(tree_builder.is_in_foreign_content());
    }
    tree_builder.stop_parsing();
    let mut errors = tokenizer.take_errors();
    errors.append(&mut tree_builder.take_errors());
    // Stable, so two errors reported at the same position keep their
    // relative order (tokenizer-first, matching the order the stages
    // actually observe a given token).
    errors.sort_by_key(|error| error.position.byte_offset);
    ParseResult {
        document: tree_builder.into_document(),
        errors,
    }
}

#[cfg(test)]
mod tests {
    use super::parse;
    use crate::document::{Document, NodeId, NodeKind};
    use crate::tree_builder::{HTML_NAMESPACE, MATHML_NAMESPACE, SVG_NAMESPACE};

    /// Navigates root -> html -> body, the common starting point for most
    /// of this module's tree-shape assertions.
    fn body_of(document: &Document) -> NodeId {
        let root = document.root();
        // Skip a possible leading DOCTYPE sibling to find the html
        // element, root's first *element* child rather than its first
        // child outright.
        let html = document
            .children(root)
            .find(|&node| matches!(document.node(node).kind, NodeKind::Element { .. }))
            .unwrap();
        document.children(html).nth(1).unwrap()
    }

    #[test]
    fn parses_a_minimal_document_into_the_expected_tree_shape() {
        let document = parse(
            "<!DOCTYPE html><html><head><title>Hi</title></head><body><p>Hello</p></body></html>",
        )
        .document;

        let root = document.root();
        let root_children: Vec<_> = document.children(root).collect();
        assert_eq!(root_children.len(), 2);
        assert_eq!(
            document.node(root_children[0]).kind,
            NodeKind::Doctype {
                name: Some("html".to_owned()),
                public_identifier: Some(String::new()),
                system_identifier: Some(String::new()),
            }
        );

        let html = root_children[1];
        let html_children: Vec<_> = document.children(html).collect();
        assert_eq!(html_children.len(), 2);
        let (head, body) = (html_children[0], html_children[1]);

        let title = document.children(head).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(title).kind else {
            unreachable!()
        };
        assert_eq!(name, "title");
        let title_text = document.children(title).next().unwrap();
        assert_eq!(
            document.node(title_text).kind,
            NodeKind::Text {
                content: "Hi".to_owned()
            }
        );

        let p = document.children(body).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(p).kind else {
            unreachable!()
        };
        assert_eq!(name, "p");
        let p_text = document.children(p).next().unwrap();
        assert_eq!(
            document.node(p_text).kind,
            NodeKind::Text {
                content: "Hello".to_owned()
            }
        );
    }

    #[test]
    fn parses_implied_html_head_body_when_missing() {
        let document = parse("<p>Hello</p>").document;

        let root = document.root();
        assert_eq!(document.children(root).count(), 1);
        let html = document.children(root).next().unwrap();
        let html_children: Vec<_> = document.children(html).collect();
        assert_eq!(html_children.len(), 2);
        let body = html_children[1];

        let p = document.children(body).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(p).kind else {
            unreachable!()
        };
        assert_eq!(name, "p");
    }

    #[test]
    fn rcdata_element_content_is_not_parsed_as_markup() {
        let document = parse("<title><b>not bold</b></title>").document;

        let root = document.root();
        let html = document.children(root).next().unwrap();
        let head = document.children(html).next().unwrap();
        let title = document.children(head).next().unwrap();
        let text = document.children(title).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "<b>not bold</b>".to_owned()
            }
        );
    }

    #[test]
    fn parse_syncs_in_foreign_content_for_cdata_sections() {
        // Exercises the one piece of driver-loop wiring no lower-level
        // test can reach: Tokenizer::set_in_foreign_content is only
        // ever called from here, based on the *real* TreeBuilder state
        // after processing the <svg> start tag.
        let document = parse("<svg><![CDATA[hello]]></svg>").document;

        let root = document.root();
        let html = document.children(root).next().unwrap();
        let body = document.children(html).nth(1).unwrap();
        let svg = document.children(body).next().unwrap();
        let content = document.children(svg).next().unwrap();
        assert_eq!(
            document.node(content).kind,
            NodeKind::Text {
                content: "hello".to_owned()
            }
        );
    }

    #[test]
    fn cdata_outside_foreign_content_becomes_a_bogus_comment() {
        let document = parse("<p><![CDATA[hello]]></p>").document;

        let root = document.root();
        let html = document.children(root).next().unwrap();
        let body = document.children(html).nth(1).unwrap();
        let p = document.children(body).next().unwrap();
        let content = document.children(p).next().unwrap();
        assert_eq!(
            document.node(content).kind,
            NodeKind::Comment {
                content: "[CDATA[hello]]".to_owned()
            }
        );
    }

    // The test matrix from plan/03-tree-construction.md's "Testmatrix"
    // step: cases ported from html-conform's own src/infoset.rs test
    // matrix (so the eventual switch from its current HTML5-parsing
    // dependency to this crate is behavior-preserving), plus spec-
    // derived cases html-conform doesn't cover at all (adoption agency,
    // quirks mode's effect on tree shape, tables without explicit
    // tbody/tr) that the phase's exit criteria call out explicitly.

    #[test]
    fn optional_end_tags_produce_sibling_li_elements() {
        // Ported from html-conform's optional_end_tags_produce_sibling_elements.
        let document = parse("<ul><li>a<li>b</ul>").document;
        let body = body_of(&document);
        let ul = document.children(body).next().unwrap();
        let items: Vec<_> = document.children(ul).collect();
        assert_eq!(items.len(), 2);
        for (&li, expected_text) in items.iter().zip(["a", "b"]) {
            let NodeKind::Element { name, .. } = &document.node(li).kind else {
                unreachable!()
            };
            assert_eq!(name, "li");
            let text = document.children(li).next().unwrap();
            assert_eq!(
                document.node(text).kind,
                NodeKind::Text {
                    content: expected_text.to_owned()
                }
            );
        }
    }

    #[test]
    fn svg_element_keeps_svg_namespace_end_to_end() {
        // Ported from html-conform's svg_elements_keep_svg_namespace.
        let document = parse("<svg><circle/></svg>").document;
        let body = body_of(&document);
        let svg = document.children(body).next().unwrap();
        assert_eq!(
            document.node(svg).kind,
            NodeKind::Element {
                name: "svg".to_owned(),
                namespace: Some(SVG_NAMESPACE.to_owned()),
                attributes: vec![],
            }
        );
        let circle = document.children(svg).next().unwrap();
        assert_eq!(
            document.node(circle).kind,
            NodeKind::Element {
                name: "circle".to_owned(),
                namespace: Some(SVG_NAMESPACE.to_owned()),
                attributes: vec![],
            }
        );
    }

    #[test]
    fn mathml_element_keeps_mathml_namespace_end_to_end() {
        // Ported from html-conform's mathml_elements_keep_mathml_namespace.
        let document = parse("<math><mi>x</mi></math>").document;
        let body = body_of(&document);
        let math = document.children(body).next().unwrap();
        assert_eq!(
            document.node(math).kind,
            NodeKind::Element {
                name: "math".to_owned(),
                namespace: Some(MATHML_NAMESPACE.to_owned()),
                attributes: vec![],
            }
        );
        let mi = document.children(math).next().unwrap();
        assert_eq!(
            document.node(mi).kind,
            NodeKind::Element {
                name: "mi".to_owned(),
                namespace: Some(MATHML_NAMESPACE.to_owned()),
                attributes: vec![],
            }
        );
        let text = document.children(mi).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "x".to_owned()
            }
        );
    }

    #[test]
    fn script_content_is_not_tokenized_as_markup() {
        // Ported from html-conform's script_and_style_content_normalize_to_plain_text.
        let document = parse("<script>1 < 2;</script>").document;
        let root = document.root();
        let html = document.children(root).next().unwrap();
        let head = document.children(html).next().unwrap();
        let script = document.children(head).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(script).kind else {
            unreachable!()
        };
        assert_eq!(name, "script");
        let text = document.children(script).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "1 < 2;".to_owned()
            }
        );
    }

    #[test]
    fn style_content_is_not_tokenized_as_markup() {
        // Ported from html-conform's script_and_style_content_normalize_to_plain_text.
        let document = parse("<style>a{color:red}</style>").document;
        let root = document.root();
        let html = document.children(root).next().unwrap();
        let head = document.children(html).next().unwrap();
        let style = document.children(head).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(style).kind else {
            unreachable!()
        };
        assert_eq!(name, "style");
        let text = document.children(style).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "a{color:red}".to_owned()
            }
        );
    }

    #[test]
    fn named_character_references_resolve_to_decoded_text() {
        // Ported from html-conform's named_entities_resolve_to_decoded_text.
        let document = parse("<p>&amp; &copy;</p>").document;
        let body = body_of(&document);
        let p = document.children(body).next().unwrap();
        let text = document.children(p).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "& \u{a9}".to_owned()
            }
        );
    }

    #[test]
    fn custom_element_gets_html_namespace_like_any_plain_element() {
        // Ported from html-conform's custom_element_gets_xhtml_namespace_like_any_plain_element.
        let document = parse("<my-widget>hi</my-widget>").document;
        let body = body_of(&document);
        let widget = document.children(body).next().unwrap();
        assert_eq!(
            document.node(widget).kind,
            NodeKind::Element {
                name: "my-widget".to_owned(),
                namespace: Some(HTML_NAMESPACE.to_owned()),
                attributes: vec![],
            }
        );
        let text = document.children(widget).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "hi".to_owned()
            }
        );
    }

    #[test]
    fn xml_lang_attribute_stays_a_literal_unnamespaced_attribute_name() {
        // On a plain HTML element (not inside SVG/MathML foreign
        // content), general HTML5 parsing never namespace-splits
        // xml:lang - "adjust foreign attributes" (§13.2.6.1) only
        // applies while inserting a foreign element. html-conform's own
        // xml:lang tests are about its RELAX-NG-schema adapter's own
        // remapping on top of this, a schema-validation-specific
        // concern, not a general parsing fact this crate should assert
        // - this just verifies the parser's own (unremapped) output.
        let document = parse(r#"<p xml:lang="de">hi</p>"#).document;
        let body = body_of(&document);
        let p = document.children(body).next().unwrap();
        let NodeKind::Element { attributes, .. } = &document.node(p).kind else {
            unreachable!()
        };
        assert_eq!(attributes.len(), 1);
        assert_eq!(attributes[0].name, "xml:lang");
        assert_eq!(attributes[0].value, "de");
        assert_eq!(attributes[0].namespace, None);
    }

    #[test]
    fn table_without_tbody_or_tr_gets_them_synthesized() {
        // Spec-derived case (§13.2.6.4.9/.13/.14's implied-tag rules),
        // not covered by html-conform's own test matrix at all.
        let document = parse("<table><td>x</td></table>").document;
        let body = body_of(&document);
        let table = document.children(body).next().unwrap();
        let tbody = document.children(table).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(tbody).kind else {
            unreachable!()
        };
        assert_eq!(name, "tbody");
        let tr = document.children(tbody).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(tr).kind else {
            unreachable!()
        };
        assert_eq!(name, "tr");
        let td = document.children(tr).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(td).kind else {
            unreachable!()
        };
        assert_eq!(name, "td");
        let text = document.children(td).next().unwrap();
        assert_eq!(
            document.node(text).kind,
            NodeKind::Text {
                content: "x".to_owned()
            }
        );
    }

    #[test]
    fn adoption_agency_spec_example_misnested_b_i_tags() {
        // §13.2.10.1 "Misnested tags: <b><i></b></i>" - the spec's own
        // fully worked, non-normative example, including its final DOM
        // tree spelled out in prose: html > head, body > p > [#text:1,
        // b > [#text:2, i > #text:3], i > #text:4, #text:5]. Not
        // covered by html-conform's own test matrix at all.
        let document = parse("<p>1<b>2<i>3</b>4</i>5</p>").document;
        let body = body_of(&document);
        let p = document.children(body).next().unwrap();
        let p_children: Vec<_> = document.children(p).collect();
        assert_eq!(p_children.len(), 4);
        assert_eq!(
            document.node(p_children[0]).kind,
            NodeKind::Text {
                content: "1".to_owned()
            }
        );

        let b = p_children[1];
        let NodeKind::Element { name, .. } = &document.node(b).kind else {
            unreachable!()
        };
        assert_eq!(name, "b");
        let b_children: Vec<_> = document.children(b).collect();
        assert_eq!(b_children.len(), 2);
        assert_eq!(
            document.node(b_children[0]).kind,
            NodeKind::Text {
                content: "2".to_owned()
            }
        );
        let inner_i = b_children[1];
        let NodeKind::Element { name, .. } = &document.node(inner_i).kind else {
            unreachable!()
        };
        assert_eq!(name, "i");
        let inner_i_text = document.children(inner_i).next().unwrap();
        assert_eq!(
            document.node(inner_i_text).kind,
            NodeKind::Text {
                content: "3".to_owned()
            }
        );

        let outer_i = p_children[2];
        let NodeKind::Element { name, .. } = &document.node(outer_i).kind else {
            unreachable!()
        };
        assert_eq!(name, "i");
        let outer_i_text = document.children(outer_i).next().unwrap();
        assert_eq!(
            document.node(outer_i_text).kind,
            NodeKind::Text {
                content: "4".to_owned()
            }
        );

        // The trailing "5" lands as p's own child, not inside the
        // reconstructed i - </i> closes before it arrives.
        assert_eq!(
            document.node(p_children[3]).kind,
            NodeKind::Text {
                content: "5".to_owned()
            }
        );
    }

    #[test]
    fn adoption_agency_spec_example_indirectly_nests_two_a_elements_via_table_misnesting() {
        // The spec's own example, quoted directly in §13.2.6.4.7's <a>
        // start-tag rule: "In the non-conforming stream <a href="a">
        // a<table><a href="b">b</table>x, the first a element would be
        // closed upon seeing the second one, and the "x" character
        // would be inside a link to "b", not to "a" [...] The result is
        // that the two a elements are indirectly nested inside each
        // other." Not covered by html-conform's own test matrix.
        let document = parse(r#"<a href="a">a<table><a href="b">b</table>x"#).document;
        let body = body_of(&document);
        let body_children: Vec<_> = document.children(body).collect();
        assert_eq!(body_children.len(), 2);

        let a1 = body_children[0];
        let a1_children: Vec<_> = document.children(a1).collect();
        assert_eq!(a1_children.len(), 3);
        assert_eq!(
            document.node(a1_children[0]).kind,
            NodeKind::Text {
                content: "a".to_owned()
            }
        );
        let a2 = a1_children[1];
        let NodeKind::Element { name, .. } = &document.node(a2).kind else {
            unreachable!()
        };
        assert_eq!(name, "a");
        let a2_text = document.children(a2).next().unwrap();
        assert_eq!(
            document.node(a2_text).kind,
            NodeKind::Text {
                content: "b".to_owned()
            }
        );
        let table = a1_children[2];
        let NodeKind::Element { name, .. } = &document.node(table).kind else {
            unreachable!()
        };
        assert_eq!(name, "table");
        assert_eq!(document.children(table).count(), 0);

        // The final "x" lands in a *fresh* a element (href="b",
        // reconstructed from the active formatting elements list),
        // a sibling of a1 - not a1 itself.
        let a3 = body_children[1];
        let NodeKind::Element { name, .. } = &document.node(a3).kind else {
            unreachable!()
        };
        assert_eq!(name, "a");
        let a3_text = document.children(a3).next().unwrap();
        assert_eq!(
            document.node(a3_text).kind,
            NodeKind::Text {
                content: "x".to_owned()
            }
        );
    }

    #[test]
    fn quirks_mode_changes_whether_table_closes_an_open_p_element() {
        // Spec-derived case, not covered by html-conform's own test
        // matrix (which drops DOCTYPE/quirks-mode entirely) - the one
        // place quirks mode actually shapes the produced tree
        // (§13.2.6.4.7's <table> start-tag rule).
        let no_quirks = parse("<!DOCTYPE html><p><table></table>").document;
        let body = body_of(&no_quirks);
        let children: Vec<_> = no_quirks.children(body).collect();
        assert_eq!(children.len(), 2);
        let NodeKind::Element { name, .. } = &no_quirks.node(children[0]).kind else {
            unreachable!()
        };
        assert_eq!(name, "p");
        assert_eq!(no_quirks.children(children[0]).count(), 0);
        let NodeKind::Element { name, .. } = &no_quirks.node(children[1]).kind else {
            unreachable!()
        };
        assert_eq!(name, "table");

        let quirks = parse("<p><table></table>").document; // no DOCTYPE at all -> quirks mode
        let body = body_of(&quirks);
        let children: Vec<_> = quirks.children(body).collect();
        assert_eq!(children.len(), 1);
        let NodeKind::Element { name, .. } = &quirks.node(children[0]).kind else {
            unreachable!()
        };
        assert_eq!(name, "p");
        let p_children: Vec<_> = quirks.children(children[0]).collect();
        assert_eq!(p_children.len(), 1);
        let NodeKind::Element { name, .. } = &quirks.node(p_children[0]).kind else {
            unreachable!()
        };
        assert_eq!(name, "table");
    }

    // The next two tests pin the minimal reproductions of two infinite
    // loops the html5lib-tests conformance corpus (tests/html5lib_conformance.rs)
    // surfaced — both fixed in `tree_builder.rs`. Neither asserts much
    // about the resulting tree shape; the property under test is that
    // `parse` returns at all (a regression would hang this test rather
    // than fail it cleanly, same as it hung `cargo test` before the fix).

    #[test]
    fn end_tag_that_walks_out_of_foreign_content_does_not_loop_forever() {
        // `</a>` while the current node is `<svg>` (no HTML-special
        // element between them) sends foreign content's "any other end
        // tag" rule (§13.2.6.5) all the way up to the first HTML-namespace
        // ancestor without ever popping anything itself — it must then
        // hand off to that insertion mode's own HTML-content rules
        // directly, not via `TokenOutcome::Reprocess` (which re-checks
        // foreign-content dispatch against the very node whose
        // foreign-namespace-ness never changed, looping forever).
        let document = parse("<a><svg></a>").document;
        let body = body_of(&document);
        assert_eq!(document.children(body).count(), 1);
    }

    #[test]
    fn template_end_tag_resets_a_stale_insertion_mode() {
        // `<template>` inside `<thead>` implicitly opens a `<tr><td>`
        // (this crate treats `<template>` as a plain element — no
        // template insertion-modes stack, see README.md's "Known
        // limitations" — so the insertion mode active for that implicit
        // `<td>`, `InCell`, is never restored). `</template>` then pops
        // `<td>`/`<tr>`/`<template>` off the stack without resetting the
        // insertion mode; the next token (`</table>`) processed under
        // the still-`InCell` mode assumes a `td`/`th` remains on the
        // stack, which no longer holds — `close_the_cell` would then
        // pop the now-empty stack forever looking for one.
        let document = parse("<table><thead><template><td></template></table>").document;
        let body = body_of(&document);
        assert_eq!(document.children(body).count(), 1);
    }

    #[test]
    fn frameset_document_replaces_body_and_ignores_stray_text() {
        // html5lib-tests' tests2.dat#5: a bare `<frameset>` after the
        // DOCTYPE, with trailing character data "in frameset" mode's
        // "anything else" rule drops entirely (no `<body>` at all in
        // the result — frameset and body are mutually exclusive).
        let document = parse("<!DOCTYPE html><frameset>test").document;

        let root = document.root();
        let root_children: Vec<_> = document.children(root).collect();
        assert_eq!(root_children.len(), 2);
        assert_eq!(
            document.node(root_children[0]).kind,
            NodeKind::Doctype {
                name: Some("html".to_owned()),
                public_identifier: Some(String::new()),
                system_identifier: Some(String::new()),
            }
        );
        let html = root_children[1];

        let html_children: Vec<_> = document.children(html).collect();
        assert_eq!(html_children.len(), 2);
        let NodeKind::Element { name, .. } = &document.node(html_children[0]).kind else {
            unreachable!()
        };
        assert_eq!(name, "head");
        let frameset = html_children[1];
        let NodeKind::Element { name, .. } = &document.node(frameset).kind else {
            unreachable!()
        };
        assert_eq!(name, "frameset");
        assert_eq!(document.children(frameset).count(), 0);
    }

    #[test]
    fn template_content_is_a_separate_fragment_from_the_template_element() {
        // html5lib-tests' template.dat#0: `<template>`'s real content
        // model (§13.2.6.4.4/.16) — its child is a `DocumentFragment`
        // ("template contents"), not the text directly.
        let document = parse("<body><template>Hello</template>").document;
        let body = body_of(&document);

        let template = document.children(body).next().unwrap();
        let NodeKind::Element { name, .. } = &document.node(template).kind else {
            unreachable!()
        };
        assert_eq!(name, "template");

        let template_children: Vec<_> = document.children(template).collect();
        assert_eq!(template_children.len(), 1);
        let content = template_children[0];
        assert_eq!(document.node(content).kind, NodeKind::DocumentFragment);

        let content_children: Vec<_> = document.children(content).collect();
        assert_eq!(content_children.len(), 1);
        assert_eq!(
            document.node(content_children[0]).kind,
            NodeKind::Text {
                content: "Hello".to_owned()
            }
        );
    }

    #[test]
    fn selected_option_content_is_mirrored_into_selectedcontent() {
        // html5lib-tests' webkit02.dat#47: the explicitly `selected`
        // option (not the first one) is the one mirrored, and — since
        // it's the last token in the input — only `stop_parsing`'s
        // final pop of the still-open `<option>` makes that observable.
        let document =
            parse("<select><button><selectedcontent></button><option>X<option selected>Y").document;
        let body = body_of(&document);

        let select = document.children(body).next().unwrap();
        let button = document.children(select).next().unwrap();
        let selectedcontent = document.children(button).next().unwrap();
        let selectedcontent_children: Vec<_> = document.children(selectedcontent).collect();
        assert_eq!(selectedcontent_children.len(), 1);
        assert_eq!(
            document.node(selectedcontent_children[0]).kind,
            NodeKind::Text {
                content: "Y".to_owned()
            }
        );
    }
}

/// Phase 08 (`plan/08-tree-construction-errors.md`): one minimal
/// trigger per tree-construction [`ParseErrorKind`] variant, mirroring
/// Phase 07's per-variant table test for the tokenizer-level kinds.
///
/// Each case asserts the expected kind is *present*, not that it's the
/// only one — several of these inputs legitimately raise more than one
/// error (an unclosed `<div>`, for instance, is both a stray-end-tag
/// trigger and an unclosed-element-at-EOF trigger), and pinning the
/// exact multiset would make the tests brittle without testing anything
/// more.
#[cfg(test)]
mod tree_construction_error_tests {
    use super::parse;
    use crate::tokenizer::ParseErrorKind;

    fn kinds(input: &str) -> Vec<ParseErrorKind> {
        parse(input)
            .errors
            .into_iter()
            .map(|error| error.kind)
            .collect()
    }

    #[track_caller]
    fn assert_raises(input: &str, expected: ParseErrorKind) {
        let raised = kinds(input);
        assert!(
            raised.contains(&expected),
            "expected {expected:?} for {input:?}, got {raised:?}"
        );
    }

    #[track_caller]
    fn assert_does_not_raise(input: &str, unexpected: ParseErrorKind) {
        let raised = kinds(input);
        assert!(
            !raised.contains(&unexpected),
            "expected no {unexpected:?} for {input:?}, got {raised:?}"
        );
    }

    /// §13.2.6.4.7's "close a p element": the `<div>` closes the open
    /// `<p>`, but `<span>` (not in the implied-end-tag set) is still
    /// open when it does.
    #[test]
    fn implied_p_end_tag_with_unclosed_elements() {
        assert_raises(
            "<!doctype html><p><span><div>",
            ParseErrorKind::ImpliedEndTagWithUnclosedElements,
        );
        // ...but a `<p>` that is itself the current node closes cleanly.
        assert_does_not_raise(
            "<!doctype html><p>text<div>",
            ParseErrorKind::ImpliedEndTagWithUnclosedElements,
        );
    }

    /// Note the explicit `<body>`: a bare `</p>` straight after the
    /// DOCTYPE is still in "before html", whose *own* "any other end
    /// tag" rule ignores it (reported as `StrayEndTag`, see
    /// `stray_end_tags_before_body` below) before "in body" ever sees it.
    #[test]
    fn p_end_tag_without_p_in_button_scope() {
        assert_raises(
            "<!doctype html><body></p>",
            ParseErrorKind::EndTagPWithoutPInButtonScope,
        );
        assert_does_not_raise(
            "<!doctype html><p>text</p>",
            ParseErrorKind::EndTagPWithoutPInButtonScope,
        );
    }

    /// §13.2.6.4.7's "any other end tag", step 3: `body` is in the
    /// special category, so the walk up the stack stops there.
    #[test]
    fn stray_end_tag_with_no_matching_open_element() {
        assert_raises("<!doctype html><body></span>", ParseErrorKind::StrayEndTag);
        assert_does_not_raise("<!doctype html><span>x</span>", ParseErrorKind::StrayEndTag);
    }

    #[test]
    fn end_tag_br() {
        assert_raises("<!doctype html></br>", ParseErrorKind::EndTagBr);
    }

    /// §13.2.5: a self-closing start tag whose handling rule never
    /// acknowledges the flag. `<div>` is not a void element; `<br>` is.
    #[test]
    fn self_closing_syntax_on_a_non_void_element() {
        assert_raises(
            "<!doctype html><div/></div>",
            ParseErrorKind::NonVoidHtmlElementStartTagWithTrailingSolidus,
        );
        assert_does_not_raise(
            "<!doctype html><br/>",
            ParseErrorKind::NonVoidHtmlElementStartTagWithTrailingSolidus,
        );
        // Foreign content acknowledges it too (§13.2.6.5).
        assert_does_not_raise(
            "<!doctype html><svg><rect/></svg>",
            ParseErrorKind::NonVoidHtmlElementStartTagWithTrailingSolidus,
        );
    }

    /// §13.2.6.4.7's end-of-file rule: `div` is not in the "may still be
    /// open" list, `p` is.
    #[test]
    fn eof_with_unclosed_elements() {
        assert_raises(
            "<!doctype html><div>",
            ParseErrorKind::EofWithUnclosedElements,
        );
        assert_does_not_raise(
            "<!doctype html><p>text",
            ParseErrorKind::EofWithUnclosedElements,
        );
    }

    /// §13.2.6.4.8: EOF while still inside a RAWTEXT/RCDATA/script
    /// element's text.
    #[test]
    fn eof_in_text_mode() {
        assert_raises(
            "<!doctype html><script>var x = 1;",
            ParseErrorKind::EofInTextMode,
        );
        assert_does_not_raise(
            "<!doctype html><script>var x = 1;</script>",
            ParseErrorKind::EofInTextMode,
        );
    }

    #[test]
    fn start_tag_image() {
        assert_raises(
            "<!doctype html><image src=x>",
            ParseErrorKind::StartTagImage,
        );
        assert_does_not_raise("<!doctype html><img src=x>", ParseErrorKind::StartTagImage);
    }

    #[test]
    fn nested_form() {
        assert_raises("<!doctype html><form><form>", ParseErrorKind::NestedForm);
        assert_does_not_raise(
            "<!doctype html><form></form><form>",
            ParseErrorKind::NestedForm,
        );
    }

    #[test]
    fn start_tag_table_while_a_table_is_open() {
        assert_raises(
            "<!doctype html><table><table></table></table>",
            ParseErrorKind::StartTagTableInTable,
        );
        assert_does_not_raise(
            "<!doctype html><table></table><table></table>",
            ParseErrorKind::StartTagTableInTable,
        );
    }

    /// §13.2.6.4.9's "anything else" — the foster-parenting fallback.
    #[test]
    fn misplaced_token_in_table() {
        assert_raises(
            "<!doctype html><table><select></select></table>",
            ParseErrorKind::MisplacedTokenInTable,
        );
        assert_raises(
            "<!doctype html><table><input></table>",
            ParseErrorKind::MisplacedTokenInTable,
        );
        assert_does_not_raise(
            "<!doctype html><table><tr><td>x</td></tr></table>",
            ParseErrorKind::MisplacedTokenInTable,
        );
    }

    /// §13.2.6.4.10: reported once per non-whitespace character run, not
    /// once per character.
    #[test]
    fn non_space_characters_in_table() {
        let raised = kinds("<!doctype html><table>text</table>");
        assert_eq!(
            raised
                .iter()
                .filter(|kind| **kind == ParseErrorKind::NonSpaceCharactersInTable)
                .count(),
            1,
            "got {raised:?}"
        );
        assert_does_not_raise(
            "<!doctype html><table>   </table>",
            ParseErrorKind::NonSpaceCharactersInTable,
        );
    }

    #[test]
    fn stray_end_tag_in_table() {
        assert_raises(
            "<!doctype html><table></tr></table>",
            ParseErrorKind::StrayEndTagInTable,
        );
    }

    /// §13.2.6.4.17's "anything else" — any non-whitespace content once
    /// `</body>` has been seen.
    #[test]
    fn token_after_body() {
        assert_raises(
            "<!doctype html><body></body>text",
            ParseErrorKind::TokenAfterBody,
        );
        assert_raises(
            "<!doctype html><body></body><p>x</p>",
            ParseErrorKind::TokenAfterBody,
        );
        assert_does_not_raise(
            "<!doctype html><body></body>\n",
            ParseErrorKind::TokenAfterBody,
        );
    }

    /// A second DOCTYPE, anywhere after the "initial" insertion mode has
    /// already consumed the first one.
    #[test]
    fn stray_doctype() {
        assert_raises(
            "<!doctype html><title>t</title><!doctype html>",
            ParseErrorKind::StrayDoctype,
        );
        assert_raises(
            "<!doctype html><body>x<!doctype html>",
            ParseErrorKind::StrayDoctype,
        );
        assert_does_not_raise(
            "<!doctype html><title>t</title>",
            ParseErrorKind::StrayDoctype,
        );
    }

    /// A well-formed document raises no tree-construction error at all —
    /// guards the new ignore-the-token reports against firing on valid
    /// markup (explicit `html`/`head`/`body` tags, a full table, nested
    /// formatting elements closed in order).
    #[test]
    fn valid_document_raises_no_errors() {
        let raised = kinds(
            "<!doctype html><html lang=en><head><title>t</title></head><body>\
             <h1>x</h1><ul><li>a</li></ul><dl><dt>t</dt><dd>d</dd></dl>\
             <form><p><b><i>x</i></b> <a href=/>y</a></p></form>\
             <table><caption>c</caption><colgroup><col></colgroup>\
             <thead><tr><th>h</th></tr></thead><tbody><tr><td>x</td></tr></tbody></table>\
             <template><div></div></template><select><option>o</option></select>\
             </body></html>\n",
        );
        assert!(raised.is_empty(), "got {raised:?}");
    }

    /// §13.2.6.4.7: every "in body" end-tag rule that ignores the token
    /// when no matching element is in scope ("this is a parse error;
    /// ignore the token").
    #[test]
    fn stray_end_tags_with_no_element_in_scope() {
        for input in [
            "<!doctype html><p>x</p></div>",
            "<!doctype html><p>x</p></header>",
            "<!doctype html><p>x</p></li>",
            "<!doctype html><p>x</p></dd>",
            "<!doctype html><p>x</p></h2>",
            "<!doctype html><p>x</p></form>",
            "<!doctype html><p>x</p></object>",
            // `object` is a scope boundary, so `body` is not in scope.
            "<!doctype html><object></body></object>",
            "<!doctype html><object></html></object>",
        ] {
            assert_raises(input, ParseErrorKind::StrayEndTag);
        }
        // Any open heading satisfies a heading end tag, whatever its rank.
        assert_does_not_raise("<!doctype html><h1>x</h2>", ParseErrorKind::StrayEndTag);
        assert_does_not_raise(
            "<!doctype html><ul><li>x</li></ul>",
            ParseErrorKind::StrayEndTag,
        );
    }

    /// "Any other end tag: Parse error. Ignore the token." in "before
    /// html" (§13.2.6.4.2), "before head" (.3), "in head" (.4), "after
    /// head" (.6) and "in template" (.16), plus "in head"'s `</template>`
    /// with no template open.
    #[test]
    fn stray_end_tags_before_body() {
        for input in [
            "<!doctype html></p>",
            "<!doctype html><html></div>",
            "<!doctype html><head></div></head>",
            "<!doctype html><head></template></head>",
            "<!doctype html><head></head></div>",
            "<!doctype html><template></div></template>",
        ] {
            assert_raises(input, ParseErrorKind::StrayEndTag);
        }
        assert_does_not_raise(
            "<!doctype html><html><head></head><body></body></html>",
            ParseErrorKind::StrayEndTag,
        );
    }

    /// Start tags the spec ignores (or merges into an existing element)
    /// with a parse error: `html`/`body`/`frameset` in body, a second
    /// `head`, table-structure tags outside a table, a nested `select`.
    #[test]
    fn stray_start_tag() {
        for input in [
            "<!doctype html><body><html lang=en>",
            "<!doctype html><head><html>",
            "<!doctype html><body><body>",
            "<!doctype html><body><frameset>",
            "<!doctype html><head><head>",
            "<!doctype html><head></head><head>",
            "<!doctype html><body><td>x",
            "<!doctype html><body><tr>",
            "<!doctype html><select><select>",
        ] {
            assert_raises(input, ParseErrorKind::StrayStartTag);
        }
        assert_does_not_raise(
            "<!doctype html><html><head></head><body><table><tr><td>x</td></tr></table>",
            ParseErrorKind::StrayStartTag,
        );
    }

    /// §13.2.6.4.7: an `a` start tag while an `a` is still in the list of
    /// active formatting elements, or `nobr` while one is in scope.
    #[test]
    fn nested_formatting_element() {
        assert_raises(
            "<!doctype html><a href=x><a href=y>",
            ParseErrorKind::NestedFormattingElement,
        );
        assert_raises(
            "<!doctype html><nobr><nobr>",
            ParseErrorKind::NestedFormattingElement,
        );
        assert_does_not_raise(
            "<!doctype html><a href=x>x</a><a href=y>y</a>",
            ParseErrorKind::NestedFormattingElement,
        );
    }

    /// Adoption agency algorithm step 4.6: the formatting element being
    /// closed is not the current node. Step 4.4 then reports the
    /// already-closed `</i>` as a stray end tag.
    #[test]
    fn misnested_formatting_element() {
        let input = "<!doctype html><p><b><i>x</b></i></p>";
        assert_raises(input, ParseErrorKind::MisnestedFormattingElement);
        assert_raises(input, ParseErrorKind::StrayEndTag);
        assert_does_not_raise(
            "<!doctype html><p><b><i>x</i></b></p>",
            ParseErrorKind::MisnestedFormattingElement,
        );
    }

    /// Adoption agency algorithm step 4.5: the `b` is open, but the
    /// `table` between it and the current node is a scope boundary.
    #[test]
    fn formatting_element_not_in_scope() {
        assert_raises(
            "<!doctype html><b><table></b></table>",
            ParseErrorKind::FormattingElementNotInScope,
        );
        assert_does_not_raise(
            "<!doctype html><b>x</b>",
            ParseErrorKind::FormattingElementNotInScope,
        );
    }

    /// The table insertion modes' ignore-the-token end-tag rules
    /// (§13.2.6.4.9, .11–.15).
    #[test]
    fn stray_end_tags_in_table_modes() {
        for input in [
            "<!doctype html><table><caption></td></caption></table>",
            "<!doctype html><table><colgroup></col></colgroup></table>",
            "<!doctype html><table><tbody></tr></tbody></table>",
            "<!doctype html><table><tbody></thead></tbody></table>",
            "<!doctype html><table><tr></td></tr></table>",
            "<!doctype html><table><tr><td></caption></td></tr></table>",
            "<!doctype html><table><tr><td></thead></td></tr></table>",
        ] {
            assert_raises(input, ParseErrorKind::StrayEndTagInTable);
        }
        // "In table"'s `form` start tag is a parse error either way.
        assert_raises(
            "<!doctype html><table><form></table>",
            ParseErrorKind::MisplacedTokenInTable,
        );
    }

    /// §13.2.6.5: a DOCTYPE inside foreign content is ignored with a
    /// parse error, like everywhere else after the "initial" mode.
    #[test]
    fn stray_doctype_in_foreign_content() {
        assert_raises(
            "<!doctype html><svg><!doctype html></svg>",
            ParseErrorKind::StrayDoctype,
        );
    }

    /// The merged error list stays in document order even though the two
    /// stages produce entries independently (`parse`'s own sort).
    #[test]
    fn errors_from_both_stages_are_merged_in_document_order() {
        let errors = parse("<!doctype html><p>&notAnEntity;<span><div></p>").errors;
        assert!(
            errors.len() >= 2,
            "expected both a tokenizer and a tree-construction error, got {errors:?}"
        );
        assert!(
            errors
                .windows(2)
                .all(|pair| pair[0].position.byte_offset <= pair[1].position.byte_offset),
            "not in document order: {errors:?}"
        );
    }
}