html5ever/tree_builder/
mod.rs

1// Copyright 2014-2017 The html5ever Project Developers. See the
2// COPYRIGHT file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! The HTML5 tree builder.
11
12pub use crate::interface::{create_element, ElemName, ElementFlags, Tracer, TreeSink};
13pub use crate::interface::{AppendNode, AppendText, Attribute, NodeOrText};
14pub use crate::interface::{LimitedQuirks, NoQuirks, Quirks, QuirksMode};
15pub use markup5ever::interface::tree_builder::create_element_with_flags;
16
17use self::types::*;
18
19use crate::tendril::StrTendril;
20use crate::{ExpandedName, LocalName, Namespace, QualName};
21
22use crate::tokenizer;
23use crate::tokenizer::states as tok_state;
24use crate::tokenizer::{Doctype, EndTag, StartTag, Tag, TokenSink, TokenSinkResult};
25
26use std::borrow::Cow::{self, Borrowed};
27use std::cell::{Cell, Ref, RefCell};
28use std::collections::VecDeque;
29use std::iter::{Enumerate, Rev};
30use std::{fmt, slice};
31
32use crate::tokenizer::states::RawKind;
33use crate::tree_builder::tag_sets::*;
34use crate::util::str::to_escaped_string;
35use log::{debug, log_enabled, warn, Level};
36use markup5ever::{expanded_name, local_name, namespace_prefix, ns};
37
38#[macro_use]
39mod tag_sets;
40
41mod data;
42mod rules;
43mod types;
44
45/// Tree builder options, with an impl for Default.
46#[derive(Copy, Clone)]
47pub struct TreeBuilderOpts {
48    /// Report all parse errors described in the spec, at some
49    /// performance penalty? Default: false
50    pub exact_errors: bool,
51
52    /// Is scripting enabled?
53    ///
54    /// This affects how `<noscript>` elements are parsed:
55    ///   - If scripting **is** enabled then the contents of a `<noscript>` element are parsed as a single text node
56    ///   - If scriping is **not** enabled then the contents of a `<noscript>` element are parsed as a normal tree of nodes
57    pub scripting_enabled: bool,
58
59    /// Is this document being parsed from the `srcdoc` attribute of an `<iframe>` element?
60    ///
61    /// This affects heuristics that infer `QuirksMode` from `<!DOCTYPE>`.
62    pub iframe_srcdoc: bool,
63
64    /// Should we drop the DOCTYPE (if any) from the tree?
65    pub drop_doctype: bool,
66
67    /// Initial TreeBuilder quirks mode. Default: NoQuirks
68    pub quirks_mode: QuirksMode,
69}
70
71impl Default for TreeBuilderOpts {
72    fn default() -> TreeBuilderOpts {
73        TreeBuilderOpts {
74            exact_errors: false,
75            scripting_enabled: true,
76            iframe_srcdoc: false,
77            drop_doctype: false,
78            quirks_mode: NoQuirks,
79        }
80    }
81}
82
83/// The HTML tree builder.
84pub struct TreeBuilder<Handle, Sink> {
85    /// Options controlling the behavior of the tree builder.
86    opts: TreeBuilderOpts,
87
88    /// Consumer of tree modifications.
89    pub sink: Sink,
90
91    /// Insertion mode.
92    mode: Cell<InsertionMode>,
93
94    /// Original insertion mode, used by Text and InTableText modes.
95    orig_mode: Cell<Option<InsertionMode>>,
96
97    /// Stack of template insertion modes.
98    template_modes: RefCell<Vec<InsertionMode>>,
99
100    /// Pending table character tokens.
101    pending_table_text: RefCell<Vec<(SplitStatus, StrTendril)>>,
102
103    /// Quirks mode as set by the parser.
104    /// FIXME: can scripts etc. change this?
105    quirks_mode: Cell<QuirksMode>,
106
107    /// The document node, which is created by the sink.
108    doc_handle: Handle,
109
110    /// Stack of open elements, most recently added at end.
111    open_elems: RefCell<Vec<Handle>>,
112
113    /// List of active formatting elements.
114    active_formatting: RefCell<Vec<FormatEntry<Handle>>>,
115
116    //§ the-element-pointers
117    /// Head element pointer.
118    head_elem: RefCell<Option<Handle>>,
119
120    /// Form element pointer.
121    form_elem: RefCell<Option<Handle>>,
122
123    /// Frameset-ok flag.
124    frameset_ok: Cell<bool>,
125
126    /// Ignore a following U+000A LINE FEED?
127    ignore_lf: Cell<bool>,
128
129    /// Is foster parenting enabled?
130    foster_parenting: Cell<bool>,
131
132    /// The context element for the fragment parsing algorithm.
133    context_elem: RefCell<Option<Handle>>,
134
135    /// Track current line
136    current_line: Cell<u64>,
137    // WARNING: If you add new fields that contain Handles, you
138    // must add them to trace_handles() below to preserve memory
139    // safety!
140    //
141    // FIXME: Auto-generate the trace hooks like Servo does.
142}
143
144impl<Handle, Sink> TreeBuilder<Handle, Sink>
145where
146    Handle: Clone,
147    Sink: TreeSink<Handle = Handle>,
148{
149    /// Create a new tree builder which sends tree modifications to a particular `TreeSink`.
150    ///
151    /// The tree builder is also a `TokenSink`.
152    pub fn new(sink: Sink, opts: TreeBuilderOpts) -> TreeBuilder<Handle, Sink> {
153        let doc_handle = sink.get_document();
154        TreeBuilder {
155            opts,
156            sink,
157            mode: Cell::new(InsertionMode::Initial),
158            orig_mode: Cell::new(None),
159            template_modes: Default::default(),
160            pending_table_text: Default::default(),
161            quirks_mode: Cell::new(opts.quirks_mode),
162            doc_handle,
163            open_elems: Default::default(),
164            active_formatting: Default::default(),
165            head_elem: Default::default(),
166            form_elem: Default::default(),
167            frameset_ok: Cell::new(true),
168            ignore_lf: Default::default(),
169            foster_parenting: Default::default(),
170            context_elem: Default::default(),
171            current_line: Cell::new(1),
172        }
173    }
174
175    /// Create a new tree builder which sends tree modifications to a particular `TreeSink`.
176    /// This is for parsing fragments.
177    ///
178    /// The tree builder is also a `TokenSink`.
179    pub fn new_for_fragment(
180        sink: Sink,
181        context_elem: Handle,
182        form_elem: Option<Handle>,
183        opts: TreeBuilderOpts,
184    ) -> TreeBuilder<Handle, Sink> {
185        let doc_handle = sink.get_document();
186        let context_is_template =
187            sink.elem_name(&context_elem).expanded() == expanded_name!(html "template");
188        let template_modes = if context_is_template {
189            RefCell::new(vec![InsertionMode::InTemplate])
190        } else {
191            RefCell::new(vec![])
192        };
193
194        let tb = TreeBuilder {
195            opts,
196            sink,
197            mode: Cell::new(InsertionMode::Initial),
198            orig_mode: Cell::new(None),
199            template_modes,
200            pending_table_text: Default::default(),
201            quirks_mode: Cell::new(opts.quirks_mode),
202            doc_handle,
203            open_elems: Default::default(),
204            active_formatting: Default::default(),
205            head_elem: Default::default(),
206            form_elem: RefCell::new(form_elem),
207            frameset_ok: Cell::new(true),
208            ignore_lf: Default::default(),
209            foster_parenting: Default::default(),
210            context_elem: RefCell::new(Some(context_elem)),
211            current_line: Cell::new(1),
212        };
213
214        // https://html.spec.whatwg.org/multipage/#parsing-html-fragments
215        // 5. Let root be a new html element with no attributes.
216        // 6. Append the element root to the Document node created above.
217        // 7. Set up the parser's stack of open elements so that it contains just the single element root.
218        tb.create_root(vec![]);
219        // 10. Reset the parser's insertion mode appropriately.
220        let old_insertion_mode = tb.reset_insertion_mode();
221        tb.mode.set(old_insertion_mode);
222
223        tb
224    }
225
226    // https://html.spec.whatwg.org/multipage/#concept-frag-parse-context
227    // Step 4. Set the state of the HTML parser's tokenization stage as follows:
228    pub fn tokenizer_state_for_context_elem(
229        &self,
230        context_element_allows_scripting: bool,
231    ) -> tok_state::State {
232        let context_elem = self.context_elem.borrow();
233        let elem = context_elem.as_ref().expect("no context element");
234        let elem_name = self.sink.elem_name(elem);
235        let name = match elem_name.expanded() {
236            ExpandedName {
237                ns: &ns!(html),
238                local,
239            } => local,
240            _ => return tok_state::Data,
241        };
242        match *name {
243            local_name!("title") | local_name!("textarea") => tok_state::RawData(tok_state::Rcdata),
244
245            local_name!("style")
246            | local_name!("xmp")
247            | local_name!("iframe")
248            | local_name!("noembed")
249            | local_name!("noframes") => tok_state::RawData(tok_state::Rawtext),
250
251            local_name!("script") => tok_state::RawData(tok_state::ScriptData),
252
253            local_name!("noscript") => {
254                if context_element_allows_scripting {
255                    tok_state::RawData(tok_state::Rawtext)
256                } else {
257                    tok_state::Data
258                }
259            },
260
261            local_name!("plaintext") => tok_state::Plaintext,
262
263            _ => tok_state::Data,
264        }
265    }
266
267    /// Call the `Tracer`'s `trace_handle` method on every `Handle` in the tree builder's
268    /// internal state. This is intended to support garbage-collected DOMs.
269    pub fn trace_handles(&self, tracer: &dyn Tracer<Handle = Handle>) {
270        tracer.trace_handle(&self.doc_handle);
271        for e in &*self.open_elems.borrow() {
272            tracer.trace_handle(e);
273        }
274
275        for e in &*self.active_formatting.borrow() {
276            if let FormatEntry::Element(handle, _) = e {
277                tracer.trace_handle(handle);
278            }
279        }
280
281        if let Some(head_elem) = self.head_elem.borrow().as_ref() {
282            tracer.trace_handle(head_elem);
283        }
284
285        if let Some(form_elem) = self.form_elem.borrow().as_ref() {
286            tracer.trace_handle(form_elem);
287        }
288
289        if let Some(context_elem) = self.context_elem.borrow().as_ref() {
290            tracer.trace_handle(context_elem);
291        }
292    }
293
294    #[allow(dead_code)]
295    fn dump_state(&self, label: String) {
296        println!("dump_state on {label}");
297        print!("    open_elems:");
298        for node in self.open_elems.borrow().iter() {
299            let name = self.sink.elem_name(node);
300            match *name.ns() {
301                ns!(html) => print!(" {}", name.local_name()),
302                _ => panic!(),
303            }
304        }
305        println!();
306        print!("    active_formatting:");
307        for entry in self.active_formatting.borrow().iter() {
308            match entry {
309                &FormatEntry::Marker => print!(" Marker"),
310                FormatEntry::Element(h, _) => {
311                    let name = self.sink.elem_name(h);
312                    match *name.ns() {
313                        ns!(html) => print!(" {}", name.local_name()),
314                        _ => panic!(),
315                    }
316                },
317            }
318        }
319        println!();
320    }
321
322    fn debug_step(&self, mode: InsertionMode, token: &Token) {
323        if log_enabled!(Level::Debug) {
324            debug!(
325                "processing {} in insertion mode {:?}",
326                to_escaped_string(token),
327                mode
328            );
329        }
330    }
331
332    fn process_to_completion(&self, mut token: Token) -> TokenSinkResult<Handle> {
333        // Queue of additional tokens yet to be processed.
334        // This stays empty in the common case where we don't split whitespace.
335        let mut more_tokens = VecDeque::new();
336
337        loop {
338            let should_have_acknowledged_self_closing_flag = matches!(
339                token,
340                Token::Tag(Tag {
341                    self_closing: true,
342                    kind: StartTag,
343                    ..
344                })
345            );
346            let result = if self.is_foreign(&token) {
347                self.step_foreign(token)
348            } else {
349                let mode = self.mode.get();
350                self.step(mode, token)
351            };
352            match result {
353                ProcessResult::Done => {
354                    if should_have_acknowledged_self_closing_flag {
355                        self.sink
356                            .parse_error(Borrowed("Unacknowledged self-closing tag"));
357                    }
358                    let Some(new_token) = more_tokens.pop_front() else {
359                        return tokenizer::TokenSinkResult::Continue;
360                    };
361                    token = new_token;
362                },
363                ProcessResult::DoneAckSelfClosing => {
364                    let Some(new_token) = more_tokens.pop_front() else {
365                        return tokenizer::TokenSinkResult::Continue;
366                    };
367                    token = new_token;
368                },
369                ProcessResult::Reprocess(m, t) => {
370                    self.mode.set(m);
371                    token = t;
372                },
373                ProcessResult::ReprocessForeign(t) => {
374                    token = t;
375                },
376                ProcessResult::SplitWhitespace(mut buf) => {
377                    let p = buf.pop_front_char_run(|c| c.is_ascii_whitespace());
378                    let Some((first, is_ws)) = p else {
379                        return tokenizer::TokenSinkResult::Continue;
380                    };
381                    let status = if is_ws {
382                        SplitStatus::Whitespace
383                    } else {
384                        SplitStatus::NotWhitespace
385                    };
386                    token = Token::Characters(status, first);
387
388                    if buf.len32() > 0 {
389                        more_tokens.push_back(Token::Characters(SplitStatus::NotSplit, buf));
390                    }
391                },
392                ProcessResult::Script(node) => {
393                    assert!(more_tokens.is_empty());
394                    return tokenizer::TokenSinkResult::Script(node);
395                },
396                ProcessResult::ToPlaintext => {
397                    assert!(more_tokens.is_empty());
398                    return tokenizer::TokenSinkResult::Plaintext;
399                },
400                ProcessResult::ToRawData(k) => {
401                    assert!(more_tokens.is_empty());
402                    return tokenizer::TokenSinkResult::RawData(k);
403                },
404                ProcessResult::EncodingIndicator(encoding) => {
405                    return tokenizer::TokenSinkResult::EncodingIndicator(encoding)
406                },
407            }
408        }
409    }
410
411    /// Are we parsing a HTML fragment?
412    pub fn is_fragment(&self) -> bool {
413        self.context_elem.borrow().is_some()
414    }
415
416    /// https://html.spec.whatwg.org/multipage/#appropriate-place-for-inserting-a-node
417    fn appropriate_place_for_insertion(
418        &self,
419        override_target: Option<Handle>,
420    ) -> InsertionPoint<Handle> {
421        use self::tag_sets::*;
422
423        declare_tag_set!(foster_target = "table" "tbody" "tfoot" "thead" "tr");
424        let target = override_target.unwrap_or_else(|| self.current_node().clone());
425        if !(self.foster_parenting.get() && self.elem_in(&target, foster_target)) {
426            if self.html_elem_named(&target, local_name!("template")) {
427                // No foster parenting (inside template).
428                let contents = self.sink.get_template_contents(&target);
429                return InsertionPoint::LastChild(contents);
430            } else {
431                // No foster parenting (the common case).
432                return InsertionPoint::LastChild(target);
433            }
434        }
435
436        // Foster parenting
437        let open_elems = self.open_elems.borrow();
438        let mut iter = open_elems.iter().rev().peekable();
439        while let Some(elem) = iter.next() {
440            if self.html_elem_named(elem, local_name!("template")) {
441                let contents = self.sink.get_template_contents(elem);
442                return InsertionPoint::LastChild(contents);
443            } else if self.html_elem_named(elem, local_name!("table")) {
444                return InsertionPoint::TableFosterParenting {
445                    element: elem.clone(),
446                    prev_element: (*iter.peek().unwrap()).clone(),
447                };
448            }
449        }
450        let html_elem = self.html_elem();
451        InsertionPoint::LastChild(html_elem.clone())
452    }
453
454    fn insert_at(&self, insertion_point: InsertionPoint<Handle>, child: NodeOrText<Handle>) {
455        match insertion_point {
456            InsertionPoint::LastChild(parent) => self.sink.append(&parent, child),
457            InsertionPoint::BeforeSibling(sibling) => {
458                self.sink.append_before_sibling(&sibling, child)
459            },
460            InsertionPoint::TableFosterParenting {
461                element,
462                prev_element,
463            } => self
464                .sink
465                .append_based_on_parent_node(&element, &prev_element, child),
466        }
467    }
468}
469
470impl<Handle, Sink> TokenSink for TreeBuilder<Handle, Sink>
471where
472    Handle: Clone,
473    Sink: TreeSink<Handle = Handle>,
474{
475    type Handle = Handle;
476
477    fn process_token(&self, token: tokenizer::Token, line_number: u64) -> TokenSinkResult<Handle> {
478        if line_number != self.current_line.get() {
479            self.sink.set_current_line(line_number);
480        }
481        let ignore_lf = self.ignore_lf.take();
482
483        // Handle `ParseError` and `DoctypeToken`; convert everything else to the local `Token` type.
484        let token = match token {
485            tokenizer::ParseError(e) => {
486                self.sink.parse_error(e);
487                return tokenizer::TokenSinkResult::Continue;
488            },
489
490            tokenizer::DoctypeToken(dt) => {
491                if self.mode.get() == InsertionMode::Initial {
492                    let (err, quirk) = data::doctype_error_and_quirks(&dt, self.opts.iframe_srcdoc);
493                    if err {
494                        self.sink.parse_error(if self.opts.exact_errors {
495                            Cow::from(format!("Bad DOCTYPE: {dt:?}"))
496                        } else {
497                            Cow::from("Bad DOCTYPE")
498                        });
499                    }
500                    let Doctype {
501                        name,
502                        public_id,
503                        system_id,
504                        force_quirks: _,
505                    } = dt;
506                    if !self.opts.drop_doctype {
507                        self.sink.append_doctype_to_document(
508                            name.unwrap_or(StrTendril::new()),
509                            public_id.unwrap_or(StrTendril::new()),
510                            system_id.unwrap_or(StrTendril::new()),
511                        );
512                    }
513                    self.set_quirks_mode(quirk);
514
515                    self.mode.set(InsertionMode::BeforeHtml);
516                    return tokenizer::TokenSinkResult::Continue;
517                } else {
518                    self.sink.parse_error(if self.opts.exact_errors {
519                        Cow::from(format!("DOCTYPE in insertion mode {:?}", self.mode.get()))
520                    } else {
521                        Cow::from("DOCTYPE in body")
522                    });
523                    return tokenizer::TokenSinkResult::Continue;
524                }
525            },
526
527            tokenizer::TagToken(x) => Token::Tag(x),
528            tokenizer::CommentToken(x) => Token::Comment(x),
529            tokenizer::NullCharacterToken => Token::NullCharacter,
530            tokenizer::EOFToken => Token::Eof,
531
532            tokenizer::CharacterTokens(mut x) => {
533                if ignore_lf && x.starts_with("\n") {
534                    x.pop_front(1);
535                }
536                if x.is_empty() {
537                    return tokenizer::TokenSinkResult::Continue;
538                }
539                Token::Characters(SplitStatus::NotSplit, x)
540            },
541        };
542
543        self.process_to_completion(token)
544    }
545
546    fn end(&self) {
547        for elem in self.open_elems.borrow_mut().drain(..).rev() {
548            self.sink.pop(&elem);
549        }
550    }
551
552    fn adjusted_current_node_present_but_not_in_html_namespace(&self) -> bool {
553        !self.open_elems.borrow().is_empty()
554            && *self.sink.elem_name(&self.adjusted_current_node()).ns() != ns!(html)
555    }
556}
557
558pub fn html_elem<Handle>(open_elems: &[Handle]) -> &Handle {
559    &open_elems[0]
560}
561
562struct ActiveFormattingView<'a, Handle: 'a> {
563    data: Ref<'a, Vec<FormatEntry<Handle>>>,
564}
565
566impl<'a, Handle: 'a> ActiveFormattingView<'a, Handle> {
567    fn iter(&'a self) -> impl Iterator<Item = (usize, &'a Handle, &'a Tag)> + 'a {
568        ActiveFormattingIter {
569            iter: self.data.iter().enumerate().rev(),
570        }
571    }
572}
573
574pub struct ActiveFormattingIter<'a, Handle: 'a> {
575    iter: Rev<Enumerate<slice::Iter<'a, FormatEntry<Handle>>>>,
576}
577
578impl<'a, Handle> Iterator for ActiveFormattingIter<'a, Handle> {
579    type Item = (usize, &'a Handle, &'a Tag);
580    fn next(&mut self) -> Option<(usize, &'a Handle, &'a Tag)> {
581        match self.iter.next() {
582            None | Some((_, &FormatEntry::Marker)) => None,
583            Some((i, FormatEntry::Element(h, t))) => Some((i, h, t)),
584        }
585    }
586}
587
588pub enum PushFlag {
589    Push,
590    NoPush,
591}
592
593enum Bookmark<Handle> {
594    Replace(Handle),
595    InsertAfter(Handle),
596}
597
598macro_rules! qualname {
599    ("", $local:tt) => {
600        QualName {
601            prefix: None,
602            ns: ns!(),
603            local: local_name!($local),
604        }
605    };
606    ($prefix: tt $ns:tt $local:tt) => {
607        QualName {
608            prefix: Some(namespace_prefix!($prefix)),
609            ns: ns!($ns),
610            local: local_name!($local),
611        }
612    };
613}
614
615#[doc(hidden)]
616impl<Handle, Sink> TreeBuilder<Handle, Sink>
617where
618    Handle: Clone,
619    Sink: TreeSink<Handle = Handle>,
620{
621    fn unexpected<T: fmt::Debug>(&self, _thing: &T) -> ProcessResult<Handle> {
622        self.sink.parse_error(if self.opts.exact_errors {
623            Cow::from(format!(
624                "Unexpected token {} in insertion mode {:?}",
625                to_escaped_string(_thing),
626                self.mode.get()
627            ))
628        } else {
629            Cow::from("Unexpected token")
630        });
631        ProcessResult::Done
632    }
633
634    fn assert_named(&self, node: &Handle, name: LocalName) {
635        assert!(self.html_elem_named(node, name));
636    }
637
638    /// Iterate over the active formatting elements (with index in the list) from the end
639    /// to the last marker, or the beginning if there are no markers.
640    fn active_formatting_end_to_marker(&self) -> ActiveFormattingView<'_, Handle> {
641        ActiveFormattingView {
642            data: self.active_formatting.borrow(),
643        }
644    }
645
646    fn position_in_active_formatting(&self, element: &Handle) -> Option<usize> {
647        self.active_formatting
648            .borrow()
649            .iter()
650            .position(|n| match n {
651                FormatEntry::Marker => false,
652                FormatEntry::Element(ref handle, _) => self.sink.same_node(handle, element),
653            })
654    }
655
656    fn set_quirks_mode(&self, mode: QuirksMode) {
657        self.quirks_mode.set(mode);
658        self.sink.set_quirks_mode(mode);
659    }
660
661    fn stop_parsing(&self) -> ProcessResult<Handle> {
662        ProcessResult::Done
663    }
664
665    //§ parsing-elements-that-contain-only-text
666    // Switch to `Text` insertion mode, save the old mode, and
667    // switch the tokenizer to a raw-data state.
668    // The latter only takes effect after the current / next
669    // `process_token` of a start tag returns!
670    fn to_raw_text_mode(&self, k: RawKind) -> ProcessResult<Handle> {
671        self.orig_mode.set(Some(self.mode.get()));
672        self.mode.set(InsertionMode::Text);
673        ProcessResult::ToRawData(k)
674    }
675
676    // The generic raw text / RCDATA parsing algorithm.
677    fn parse_raw_data(&self, tag: Tag, k: RawKind) -> ProcessResult<Handle> {
678        self.insert_element_for(tag);
679        self.to_raw_text_mode(k)
680    }
681    //§ END
682
683    fn current_node(&self) -> Ref<'_, Handle> {
684        Ref::map(self.open_elems.borrow(), |elems| {
685            elems.last().expect("no current element")
686        })
687    }
688
689    fn adjusted_current_node(&self) -> Ref<'_, Handle> {
690        if self.open_elems.borrow().len() == 1 {
691            let context_elem = self.context_elem.borrow();
692            let ctx = Ref::filter_map(context_elem, |e| e.as_ref());
693            if let Ok(ctx) = ctx {
694                return ctx;
695            }
696        }
697        self.current_node()
698    }
699
700    fn current_node_in<TagSet>(&self, set: TagSet) -> bool
701    where
702        TagSet: Fn(ExpandedName) -> bool,
703    {
704        set(self.sink.elem_name(&self.current_node()).expanded())
705    }
706
707    // Insert at the "appropriate place for inserting a node".
708    fn insert_appropriately(&self, child: NodeOrText<Handle>, override_target: Option<Handle>) {
709        let insertion_point = self.appropriate_place_for_insertion(override_target);
710        self.insert_at(insertion_point, child);
711    }
712
713    fn adoption_agency(&self, subject: LocalName) {
714        // 1.
715        if self.current_node_named(subject.clone())
716            && self
717                .position_in_active_formatting(&self.current_node())
718                .is_none()
719        {
720            self.pop();
721            return;
722        }
723
724        // 2. 3. 4.
725        for _ in 0..8 {
726            // 5.
727            // We clone the Handle and Tag so they don't cause an immutable borrow of self.
728            let maybe_fmt_entry = self
729                .active_formatting_end_to_marker()
730                .iter()
731                .find(|&(_, _, tag)| tag.name == subject)
732                .map(|(i, h, t)| (i, h.clone(), t.clone()));
733
734            let Some((fmt_elem_index, fmt_elem, fmt_elem_tag)) = maybe_fmt_entry else {
735                return self.process_end_tag_in_body(Tag {
736                    kind: EndTag,
737                    name: subject,
738                    self_closing: false,
739                    attrs: vec![],
740                    had_duplicate_attributes: false,
741                });
742            };
743
744            let Some(fmt_elem_stack_index) = self
745                .open_elems
746                .borrow()
747                .iter()
748                .rposition(|n| self.sink.same_node(n, &fmt_elem))
749            else {
750                self.sink
751                    .parse_error(Borrowed("Formatting element not open"));
752                self.active_formatting.borrow_mut().remove(fmt_elem_index);
753                return;
754            };
755
756            // 7.
757            if !self.in_scope(default_scope, |n| self.sink.same_node(&n, &fmt_elem)) {
758                self.sink
759                    .parse_error(Borrowed("Formatting element not in scope"));
760                return;
761            }
762
763            // 8.
764            if !self.sink.same_node(&self.current_node(), &fmt_elem) {
765                self.sink
766                    .parse_error(Borrowed("Formatting element not current node"));
767            }
768
769            // 9.
770            let maybe_furthest_block = self
771                .open_elems
772                .borrow()
773                .iter()
774                .enumerate()
775                .skip(fmt_elem_stack_index)
776                .find(|&(_, open_element)| self.elem_in(open_element, special_tag))
777                .map(|(i, h)| (i, h.clone()));
778
779            let Some((furthest_block_index, furthest_block)) = maybe_furthest_block else {
780                // 10.
781                self.open_elems.borrow_mut().truncate(fmt_elem_stack_index);
782                self.active_formatting.borrow_mut().remove(fmt_elem_index);
783                return;
784            };
785
786            // 11.
787            let common_ancestor = self.open_elems.borrow()[fmt_elem_stack_index - 1].clone();
788
789            // 12.
790            let mut bookmark = Bookmark::Replace(fmt_elem.clone());
791
792            // 13.
793            let mut node;
794            let mut node_index = furthest_block_index;
795            let mut last_node = furthest_block.clone();
796
797            // 13.1.
798            let mut inner_counter = 0;
799            loop {
800                // 13.2.
801                inner_counter += 1;
802
803                // 13.3.
804                node_index -= 1;
805                node = self.open_elems.borrow()[node_index].clone();
806
807                // 13.4.
808                if self.sink.same_node(&node, &fmt_elem) {
809                    break;
810                }
811
812                // 13.5.
813                if inner_counter > 3 {
814                    self.position_in_active_formatting(&node)
815                        .map(|position| self.active_formatting.borrow_mut().remove(position));
816                    self.open_elems.borrow_mut().remove(node_index);
817                    continue;
818                }
819
820                let Some(node_formatting_index) = self.position_in_active_formatting(&node) else {
821                    // 13.6.
822                    self.open_elems.borrow_mut().remove(node_index);
823                    continue;
824                };
825
826                // 13.7.
827                let tag = match self.active_formatting.borrow()[node_formatting_index] {
828                    FormatEntry::Element(ref h, ref t) => {
829                        assert!(self.sink.same_node(h, &node));
830                        t.clone()
831                    },
832                    FormatEntry::Marker => panic!("Found marker during adoption agency"),
833                };
834                // FIXME: Is there a way to avoid cloning the attributes twice here (once on their
835                // own, once as part of t.clone() above)?
836                let new_element = create_element_with_flags(
837                    &self.sink,
838                    QualName::new(None, ns!(html), tag.name.clone()),
839                    tag.attrs.clone(),
840                    tag.had_duplicate_attributes,
841                );
842                self.open_elems.borrow_mut()[node_index] = new_element.clone();
843                self.active_formatting.borrow_mut()[node_formatting_index] =
844                    FormatEntry::Element(new_element.clone(), tag);
845                node = new_element;
846
847                // 13.8.
848                if self.sink.same_node(&last_node, &furthest_block) {
849                    bookmark = Bookmark::InsertAfter(node.clone());
850                }
851
852                // 13.9.
853                self.sink.remove_from_parent(&last_node);
854                self.sink.append(&node, AppendNode(last_node.clone()));
855
856                // 13.10.
857                last_node = node.clone();
858
859                // 13.11.
860            }
861
862            // 14.
863            self.sink.remove_from_parent(&last_node);
864            self.insert_appropriately(AppendNode(last_node.clone()), Some(common_ancestor));
865
866            // 15.
867            // FIXME: Is there a way to avoid cloning the attributes twice here (once on their own,
868            // once as part of t.clone() above)?
869            let new_element = create_element_with_flags(
870                &self.sink,
871                QualName::new(None, ns!(html), fmt_elem_tag.name.clone()),
872                fmt_elem_tag.attrs.clone(),
873                fmt_elem_tag.had_duplicate_attributes,
874            );
875            let new_entry = FormatEntry::Element(new_element.clone(), fmt_elem_tag);
876
877            // 16.
878            self.sink.reparent_children(&furthest_block, &new_element);
879
880            // 17.
881            self.sink
882                .append(&furthest_block, AppendNode(new_element.clone()));
883
884            // 18.
885            // FIXME: We could probably get rid of the position_in_active_formatting() calls here
886            // if we had a more clever Bookmark representation.
887            match bookmark {
888                Bookmark::Replace(to_replace) => {
889                    let index = self
890                        .position_in_active_formatting(&to_replace)
891                        .expect("bookmark not found in active formatting elements");
892                    self.active_formatting.borrow_mut()[index] = new_entry;
893                },
894                Bookmark::InsertAfter(previous) => {
895                    let index = self
896                        .position_in_active_formatting(&previous)
897                        .expect("bookmark not found in active formatting elements")
898                        + 1;
899                    self.active_formatting.borrow_mut().insert(index, new_entry);
900                    let old_index = self
901                        .position_in_active_formatting(&fmt_elem)
902                        .expect("formatting element not found in active formatting elements");
903                    self.active_formatting.borrow_mut().remove(old_index);
904                },
905            }
906
907            // 19.
908            self.remove_from_stack(&fmt_elem);
909            let new_furthest_block_index = self
910                .open_elems
911                .borrow()
912                .iter()
913                .position(|n| self.sink.same_node(n, &furthest_block))
914                .expect("furthest block missing from open element stack");
915            self.open_elems
916                .borrow_mut()
917                .insert(new_furthest_block_index + 1, new_element);
918
919            // 20.
920        }
921    }
922
923    fn push(&self, elem: &Handle) {
924        self.open_elems.borrow_mut().push(elem.clone());
925    }
926
927    fn pop(&self) -> Handle {
928        let elem = self
929            .open_elems
930            .borrow_mut()
931            .pop()
932            .expect("no current element");
933
934        self.sink.pop(&elem);
935        elem
936    }
937
938    fn remove_from_stack(&self, elem: &Handle) {
939        let position = self
940            .open_elems
941            .borrow()
942            .iter()
943            .rposition(|x| self.sink.same_node(elem, x));
944        if let Some(position) = position {
945            self.open_elems.borrow_mut().remove(position);
946            self.sink.pop(elem);
947        }
948    }
949
950    fn is_marker_or_open(&self, entry: &FormatEntry<Handle>) -> bool {
951        match *entry {
952            FormatEntry::Marker => true,
953            FormatEntry::Element(ref node, _) => self
954                .open_elems
955                .borrow()
956                .iter()
957                .rev()
958                .any(|n| self.sink.same_node(n, node)),
959        }
960    }
961
962    /// <https://html.spec.whatwg.org/#reconstruct-the-active-formatting-elements>
963    fn reconstruct_active_formatting_elements(&self) {
964        {
965            let active_formatting = self.active_formatting.borrow();
966
967            // Step 1. If there are no entries in the list of active formatting elements,
968            // then there is nothing to reconstruct; stop this algorithm.
969            let Some(last) = active_formatting.last() else {
970                return;
971            };
972
973            // Step 2. If the last (most recently added) entry in the list of active formatting elements is a marker,
974            // or if it is an element that is in the stack of open elements, then there is nothing to reconstruct;
975            // stop this algorithm.
976            if self.is_marker_or_open(last) {
977                return;
978            }
979        }
980
981        // Step 3. Let entry be the last (most recently added) element in the list of active formatting elements.
982        // NOTE: We track the index of the element instead
983        let mut entry_index = self.active_formatting.borrow().len() - 1;
984        loop {
985            // Step 4. Rewind: If there are no entries before entry in the list of active formatting elements,
986            // then jump to the step labeled create.
987            if entry_index == 0 {
988                break;
989            }
990
991            // Step 5. Let entry be the entry one earlier than entry in the list of active formatting elements.
992            entry_index -= 1;
993
994            // Step 6. If entry is neither a marker nor an element that is also in the stack of open elements,
995            // go to the step labeled rewind.
996            // Step 7. Advance: Let entry be the element one later than entry in the list
997            // of active formatting elements.
998            if self.is_marker_or_open(&self.active_formatting.borrow()[entry_index]) {
999                entry_index += 1;
1000                break;
1001            }
1002        }
1003
1004        loop {
1005            // Step 8. Create: Insert an HTML element for the token for which the element entry was created,
1006            // to obtain new element.
1007            let tag = match self.active_formatting.borrow()[entry_index] {
1008                FormatEntry::Element(_, ref t) => t.clone(),
1009                FormatEntry::Marker => {
1010                    panic!("Found marker during formatting element reconstruction")
1011                },
1012            };
1013
1014            // FIXME: Is there a way to avoid cloning the attributes twice here (once on their own,
1015            // once as part of t.clone() above)?
1016            let new_element = self.insert_element(
1017                PushFlag::Push,
1018                ns!(html),
1019                tag.name.clone(),
1020                tag.attrs.clone(),
1021                tag.had_duplicate_attributes,
1022            );
1023
1024            // Step 9. Replace the entry for entry in the list with an entry for new element.
1025            self.active_formatting.borrow_mut()[entry_index] =
1026                FormatEntry::Element(new_element, tag);
1027
1028            // Step 10. If the entry for new element in the list of active formatting elements is
1029            // not the last entry in the list, return to the step labeled advance.
1030            if entry_index == self.active_formatting.borrow().len() - 1 {
1031                break;
1032            }
1033            entry_index += 1;
1034        }
1035    }
1036
1037    /// Get the first element on the stack, which will be the <html> element.
1038    fn html_elem(&self) -> Ref<'_, Handle> {
1039        Ref::map(self.open_elems.borrow(), |elems| &elems[0])
1040    }
1041
1042    /// Get the second element on the stack, if it's a HTML body element.
1043    fn body_elem(&self) -> Option<Ref<'_, Handle>> {
1044        if self.open_elems.borrow().len() <= 1 {
1045            return None;
1046        }
1047
1048        let node = Ref::map(self.open_elems.borrow(), |elems| &elems[1]);
1049        if self.html_elem_named(&node, local_name!("body")) {
1050            Some(node)
1051        } else {
1052            None
1053        }
1054    }
1055
1056    /// Signal an error depending on the state of the stack of open elements at
1057    /// the end of the body.
1058    fn check_body_end(&self) {
1059        declare_tag_set!(body_end_ok =
1060            "dd" "dt" "li" "optgroup" "option" "p" "rp" "rt" "tbody" "td" "tfoot" "th"
1061            "thead" "tr" "body" "html");
1062
1063        for elem in self.open_elems.borrow().iter() {
1064            let error = {
1065                let elem_name = self.sink.elem_name(elem);
1066                let name = elem_name.expanded();
1067                if body_end_ok(name) {
1068                    continue;
1069                }
1070
1071                if self.opts.exact_errors {
1072                    Cow::from(format!("Unexpected open tag {name:?} at end of body"))
1073                } else {
1074                    Cow::from("Unexpected open tag at end of body")
1075                }
1076            };
1077            self.sink.parse_error(error);
1078            // FIXME: Do we keep checking after finding one bad tag?
1079            // The spec suggests not.
1080            return;
1081        }
1082    }
1083
1084    fn in_scope<TagSet, Pred>(&self, scope: TagSet, pred: Pred) -> bool
1085    where
1086        TagSet: Fn(ExpandedName) -> bool,
1087        Pred: Fn(Handle) -> bool,
1088    {
1089        for node in self.open_elems.borrow().iter().rev() {
1090            if pred(node.clone()) {
1091                return true;
1092            }
1093            if scope(self.sink.elem_name(node).expanded()) {
1094                return false;
1095            }
1096        }
1097
1098        // supposed to be impossible, because <html> is always in scope
1099
1100        false
1101    }
1102
1103    fn elem_in<TagSet>(&self, elem: &Handle, set: TagSet) -> bool
1104    where
1105        TagSet: Fn(ExpandedName) -> bool,
1106    {
1107        set(self.sink.elem_name(elem).expanded())
1108    }
1109
1110    fn html_elem_named(&self, elem: &Handle, name: LocalName) -> bool {
1111        let elem_name = self.sink.elem_name(elem);
1112        *elem_name.ns() == ns!(html) && *elem_name.local_name() == name
1113    }
1114
1115    fn in_html_elem_named(&self, name: LocalName) -> bool {
1116        self.open_elems
1117            .borrow()
1118            .iter()
1119            .any(|elem| self.html_elem_named(elem, name.clone()))
1120    }
1121
1122    fn current_node_named(&self, name: LocalName) -> bool {
1123        self.html_elem_named(&self.current_node(), name)
1124    }
1125
1126    fn in_scope_named<TagSet>(&self, scope: TagSet, name: LocalName) -> bool
1127    where
1128        TagSet: Fn(ExpandedName) -> bool,
1129    {
1130        self.in_scope(scope, |elem| self.html_elem_named(&elem, name.clone()))
1131    }
1132
1133    /// <https://html.spec.whatwg.org/#generate-implied-end-tags>
1134    fn generate_implied_end_tags<TagSet>(&self, set: TagSet)
1135    where
1136        TagSet: Fn(ExpandedName) -> bool,
1137    {
1138        loop {
1139            {
1140                let open_elems = self.open_elems.borrow();
1141                let Some(elem) = open_elems.last() else {
1142                    return;
1143                };
1144                let elem_name = self.sink.elem_name(elem);
1145                if !set(elem_name.expanded()) {
1146                    return;
1147                }
1148            }
1149            self.pop();
1150        }
1151    }
1152
1153    fn generate_implied_end_except(&self, except: LocalName) {
1154        self.generate_implied_end_tags(|p| {
1155            if *p.ns == ns!(html) && *p.local == except {
1156                false
1157            } else {
1158                cursory_implied_end(p)
1159            }
1160        });
1161    }
1162    //§ END
1163
1164    // Pop elements until the current element is in the set.
1165    fn pop_until_current<TagSet>(&self, tag_set: TagSet)
1166    where
1167        TagSet: Fn(ExpandedName) -> bool,
1168    {
1169        while !self.current_node_in(&tag_set) {
1170            self.open_elems.borrow_mut().pop();
1171        }
1172    }
1173
1174    // Pop elements until an element from the set has been popped.  Returns the
1175    // number of elements popped.
1176    fn pop_until<P>(&self, pred: P) -> usize
1177    where
1178        P: Fn(ExpandedName) -> bool,
1179    {
1180        let mut n = 0;
1181        loop {
1182            n += 1;
1183            match self.open_elems.borrow_mut().pop() {
1184                None => break,
1185                Some(elem) => {
1186                    if pred(self.sink.elem_name(&elem).expanded()) {
1187                        break;
1188                    }
1189                },
1190            }
1191        }
1192        n
1193    }
1194
1195    /// Pop element until an element with the given name has been popped.
1196    fn pop_until_named(&self, name: LocalName) -> usize {
1197        self.pop_until(|p| *p.ns == ns!(html) && *p.local == name)
1198    }
1199
1200    /// Pop elements until one with the specified name has been popped.
1201    /// Signal an error if it was not the first one.
1202    fn expect_to_close(&self, name: LocalName) {
1203        if self.pop_until_named(name.clone()) != 1 {
1204            self.sink.parse_error(if self.opts.exact_errors {
1205                Cow::from(format!("Unexpected open element while closing {name:?}"))
1206            } else {
1207                Cow::from("Unexpected open element")
1208            });
1209        }
1210    }
1211
1212    fn close_p_element(&self) {
1213        declare_tag_set!(implied = [cursory_implied_end] - "p");
1214        self.generate_implied_end_tags(implied);
1215        self.expect_to_close(local_name!("p"));
1216    }
1217
1218    fn close_p_element_in_button_scope(&self) {
1219        if self.in_scope_named(button_scope, local_name!("p")) {
1220            self.close_p_element();
1221        }
1222    }
1223
1224    // Check <input> tags for type=hidden
1225    fn is_type_hidden(&self, tag: &Tag) -> bool {
1226        match tag
1227            .attrs
1228            .iter()
1229            .find(|&at| at.name.expanded() == expanded_name!("", "type"))
1230        {
1231            None => false,
1232            Some(at) => at.value.eq_ignore_ascii_case("hidden"),
1233        }
1234    }
1235
1236    fn foster_parent_in_body(&self, token: Token) -> ProcessResult<Handle> {
1237        warn!("foster parenting not implemented");
1238        self.foster_parenting.set(true);
1239        let res = self.step(InsertionMode::InBody, token);
1240        // FIXME: what if res is Reprocess?
1241        self.foster_parenting.set(false);
1242        res
1243    }
1244
1245    fn process_chars_in_table(&self, token: Token) -> ProcessResult<Handle> {
1246        declare_tag_set!(table_outer = "table" "tbody" "tfoot" "thead" "tr");
1247        if self.current_node_in(table_outer) {
1248            assert!(self.pending_table_text.borrow().is_empty());
1249            self.orig_mode.set(Some(self.mode.get()));
1250            ProcessResult::Reprocess(InsertionMode::InTableText, token)
1251        } else {
1252            self.sink.parse_error(if self.opts.exact_errors {
1253                Cow::from(format!(
1254                    "Unexpected characters {} in table",
1255                    to_escaped_string(&token)
1256                ))
1257            } else {
1258                Cow::from("Unexpected characters in table")
1259            });
1260            self.foster_parent_in_body(token)
1261        }
1262    }
1263
1264    // https://html.spec.whatwg.org/multipage/#reset-the-insertion-mode-appropriately
1265    fn reset_insertion_mode(&self) -> InsertionMode {
1266        let open_elems = self.open_elems.borrow();
1267        for (i, mut node) in open_elems.iter().enumerate().rev() {
1268            let last = i == 0usize;
1269            let context_elem = self.context_elem.borrow();
1270            if let (true, Some(ctx)) = (last, context_elem.as_ref()) {
1271                node = ctx;
1272            }
1273            let elem_name = self.sink.elem_name(node);
1274            let name = match elem_name.expanded() {
1275                ExpandedName {
1276                    ns: &ns!(html),
1277                    local,
1278                } => local,
1279                _ => continue,
1280            };
1281            match *name {
1282                local_name!("td") | local_name!("th") => {
1283                    if !last {
1284                        return InsertionMode::InCell;
1285                    }
1286                },
1287                local_name!("tr") => return InsertionMode::InRow,
1288                local_name!("tbody") | local_name!("thead") | local_name!("tfoot") => {
1289                    return InsertionMode::InTableBody;
1290                },
1291                local_name!("caption") => return InsertionMode::InCaption,
1292                local_name!("colgroup") => return InsertionMode::InColumnGroup,
1293                local_name!("table") => return InsertionMode::InTable,
1294                local_name!("template") => return *self.template_modes.borrow().last().unwrap(),
1295                local_name!("head") => {
1296                    if !last {
1297                        return InsertionMode::InHead;
1298                    }
1299                },
1300                local_name!("body") => return InsertionMode::InBody,
1301                local_name!("frameset") => return InsertionMode::InFrameset,
1302                local_name!("html") => match *self.head_elem.borrow() {
1303                    None => return InsertionMode::BeforeHead,
1304                    Some(_) => return InsertionMode::AfterHead,
1305                },
1306
1307                _ => (),
1308            }
1309        }
1310        InsertionMode::InBody
1311    }
1312
1313    fn close_the_cell(&self) {
1314        self.generate_implied_end_tags(cursory_implied_end);
1315        if self.pop_until(td_th) != 1 {
1316            self.sink
1317                .parse_error(Borrowed("expected to close <td> or <th> with cell"));
1318        }
1319        self.clear_active_formatting_to_marker();
1320    }
1321
1322    fn append_text(&self, text: StrTendril) -> ProcessResult<Handle> {
1323        self.insert_appropriately(AppendText(text), None);
1324        ProcessResult::Done
1325    }
1326
1327    fn append_comment(&self, text: StrTendril) -> ProcessResult<Handle> {
1328        let comment = self.sink.create_comment(text);
1329        self.insert_appropriately(AppendNode(comment), None);
1330        ProcessResult::Done
1331    }
1332
1333    fn append_comment_to_doc(&self, text: StrTendril) -> ProcessResult<Handle> {
1334        let comment = self.sink.create_comment(text);
1335        self.sink.append(&self.doc_handle, AppendNode(comment));
1336        ProcessResult::Done
1337    }
1338
1339    fn append_comment_to_html(&self, text: StrTendril) -> ProcessResult<Handle> {
1340        let open_elems = self.open_elems.borrow();
1341        let target = html_elem(&open_elems);
1342        let comment = self.sink.create_comment(text);
1343        self.sink.append(target, AppendNode(comment));
1344        ProcessResult::Done
1345    }
1346
1347    //§ creating-and-inserting-nodes
1348    fn create_root(&self, attrs: Vec<Attribute>) {
1349        let elem = create_element(
1350            &self.sink,
1351            QualName::new(None, ns!(html), local_name!("html")),
1352            attrs,
1353        );
1354        self.push(&elem);
1355        self.sink.append(&self.doc_handle, AppendNode(elem));
1356        // FIXME: application cache selection algorithm
1357    }
1358
1359    /// <https://html.spec.whatwg.org/multipage/#create-an-element-for-the-token>
1360    fn insert_element(
1361        &self,
1362        push: PushFlag,
1363        ns: Namespace,
1364        name: LocalName,
1365        attrs: Vec<Attribute>,
1366        had_duplicate_attributes: bool,
1367    ) -> Handle {
1368        declare_tag_set!(form_associatable =
1369            "button" "fieldset" "input" "object"
1370            "output" "select" "textarea" "img");
1371
1372        declare_tag_set!(listed = [form_associatable] - "img");
1373
1374        // Step 7.
1375        let qname = QualName::new(None, ns, name);
1376        let elem = create_element_with_flags(
1377            &self.sink,
1378            qname.clone(),
1379            attrs.clone(),
1380            had_duplicate_attributes,
1381        );
1382
1383        let insertion_point = self.appropriate_place_for_insertion(None);
1384        let (node1, node2) = match insertion_point {
1385            InsertionPoint::LastChild(ref p) | InsertionPoint::BeforeSibling(ref p) => {
1386                (p.clone(), None)
1387            },
1388            InsertionPoint::TableFosterParenting {
1389                ref element,
1390                ref prev_element,
1391            } => (element.clone(), Some(prev_element.clone())),
1392        };
1393
1394        // Step 12.
1395        if form_associatable(qname.expanded())
1396            && self.form_elem.borrow().is_some()
1397            && !self.in_html_elem_named(local_name!("template"))
1398            && !(listed(qname.expanded())
1399                && attrs
1400                    .iter()
1401                    .any(|a| a.name.expanded() == expanded_name!("", "form")))
1402        {
1403            let form = self.form_elem.borrow().as_ref().unwrap().clone();
1404            self.sink
1405                .associate_with_form(&elem, &form, (&node1, node2.as_ref()));
1406        }
1407
1408        self.insert_at(insertion_point, AppendNode(elem.clone()));
1409
1410        match push {
1411            PushFlag::Push => self.push(&elem),
1412            PushFlag::NoPush => (),
1413        }
1414        // FIXME: Remove from the stack if we can't append?
1415        elem
1416    }
1417
1418    fn insert_element_for(&self, tag: Tag) -> Handle {
1419        self.insert_element(
1420            PushFlag::Push,
1421            ns!(html),
1422            tag.name,
1423            tag.attrs,
1424            tag.had_duplicate_attributes,
1425        )
1426    }
1427
1428    fn insert_and_pop_element_for(&self, tag: Tag) -> Handle {
1429        self.insert_element(
1430            PushFlag::NoPush,
1431            ns!(html),
1432            tag.name,
1433            tag.attrs,
1434            tag.had_duplicate_attributes,
1435        )
1436    }
1437
1438    fn insert_phantom(&self, name: LocalName) -> Handle {
1439        self.insert_element(PushFlag::Push, ns!(html), name, vec![], false)
1440    }
1441
1442    /// <https://html.spec.whatwg.org/multipage/parsing.html#insert-an-element-at-the-adjusted-insertion-location>
1443    fn insert_foreign_element(
1444        &self,
1445        tag: Tag,
1446        ns: Namespace,
1447        only_add_to_element_stack: bool,
1448    ) -> Handle {
1449        let adjusted_insertion_location = self.appropriate_place_for_insertion(None);
1450        let qname = QualName::new(None, ns, tag.name.clone());
1451        let elem = create_element_with_flags(
1452            &self.sink,
1453            qname.clone(),
1454            tag.attrs.clone(),
1455            tag.had_duplicate_attributes,
1456        );
1457
1458        if !only_add_to_element_stack {
1459            self.insert_at(adjusted_insertion_location, AppendNode(elem.clone()));
1460        }
1461
1462        self.push(&elem);
1463
1464        elem
1465    }
1466    //§ END
1467
1468    /// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead>
1469    ///
1470    /// A start tag whose tag name is "template"
1471    fn should_attach_declarative_shadow(&self, tag: &Tag) -> bool {
1472        let adjusted_insertion_location = self.appropriate_place_for_insertion(None);
1473
1474        let (intended_parent, _node2) = match adjusted_insertion_location {
1475            InsertionPoint::LastChild(ref p) | InsertionPoint::BeforeSibling(ref p) => {
1476                (p.clone(), None)
1477            },
1478            InsertionPoint::TableFosterParenting {
1479                ref element,
1480                ref prev_element,
1481            } => (element.clone(), Some(prev_element.clone())),
1482        };
1483
1484        // template start tag's shadowrootmode is not in the none state
1485        let is_shadow_root_mode = tag.attrs.iter().any(|attr| {
1486            attr.name.local == local_name!("shadowrootmode")
1487                && (attr.value.as_ref() == "open" || attr.value.as_ref() == "closed")
1488        });
1489
1490        // Check if intended_parent's document allows declarative shadow roots
1491        let allow_declarative_shadow_roots =
1492            self.sink.allow_declarative_shadow_roots(&intended_parent);
1493
1494        // the adjusted current node is not the topmost element in the stack of open elements
1495        let adjusted_current_node_not_topmost = match self.open_elems.borrow().first() {
1496            // The stack grows downwards; the topmost node on the stack is the first one added to the stack
1497            // The current node is the bottommost node in this stack of open elements.
1498            //
1499            // (1) The adjusted current node is the context element if the parser was created as part of the HTML fragment parsing algorithm
1500            // and the stack of open elements has only one element in it (fragment case);
1501            // (2) otherwise, the adjusted current node is the current node (the bottomost node)
1502            //
1503            // => adjusted current node != topmost element in the stack when the stack size > 1
1504            Some(_) => self.open_elems.borrow().len() > 1,
1505            None => true,
1506        };
1507
1508        is_shadow_root_mode && allow_declarative_shadow_roots && adjusted_current_node_not_topmost
1509    }
1510
1511    /// <https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead>
1512    ///
1513    /// A start tag whose tag name is "template"
1514    fn attach_declarative_shadow(
1515        &self,
1516        tag: &Tag,
1517        shadow_host: &Handle,
1518        template: &Handle,
1519    ) -> bool {
1520        self.sink
1521            .attach_declarative_shadow(shadow_host, template, &tag.attrs)
1522    }
1523
1524    fn create_formatting_element_for(&self, tag: Tag) -> Handle {
1525        // FIXME: This really wants unit tests.
1526        let mut first_match = None;
1527        let mut matches = 0usize;
1528        for (i, _, old_tag) in self.active_formatting_end_to_marker().iter() {
1529            if tag.equiv_modulo_attr_order(old_tag) {
1530                first_match = Some(i);
1531                matches += 1;
1532            }
1533        }
1534
1535        if matches >= 3 {
1536            self.active_formatting
1537                .borrow_mut()
1538                .remove(first_match.expect("matches with no index"));
1539        }
1540
1541        let elem = self.insert_element(
1542            PushFlag::Push,
1543            ns!(html),
1544            tag.name.clone(),
1545            tag.attrs.clone(),
1546            tag.had_duplicate_attributes,
1547        );
1548        self.active_formatting
1549            .borrow_mut()
1550            .push(FormatEntry::Element(elem.clone(), tag));
1551        elem
1552    }
1553
1554    fn clear_active_formatting_to_marker(&self) {
1555        loop {
1556            match self.active_formatting.borrow_mut().pop() {
1557                None | Some(FormatEntry::Marker) => break,
1558                _ => (),
1559            }
1560        }
1561    }
1562
1563    fn process_end_tag_in_body(&self, tag: Tag) {
1564        // Look back for a matching open element.
1565        let mut match_idx = None;
1566        for (i, elem) in self.open_elems.borrow().iter().enumerate().rev() {
1567            if self.html_elem_named(elem, tag.name.clone()) {
1568                match_idx = Some(i);
1569                break;
1570            }
1571
1572            if self.elem_in(elem, special_tag) {
1573                self.sink
1574                    .parse_error(Borrowed("Found special tag while closing generic tag"));
1575                return;
1576            }
1577        }
1578
1579        let Some(match_idx) = match_idx else {
1580            // I believe this is impossible, because the root
1581            // <html> element is in special_tag.
1582            self.unexpected(&tag);
1583            return;
1584        };
1585
1586        self.generate_implied_end_except(tag.name.clone());
1587
1588        if match_idx != self.open_elems.borrow().len() - 1 {
1589            // mis-nested tags
1590            self.unexpected(&tag);
1591        }
1592        self.open_elems.borrow_mut().truncate(match_idx);
1593    }
1594
1595    fn handle_misnested_a_tags(&self, tag: &Tag) {
1596        let Some(node) = self
1597            .active_formatting_end_to_marker()
1598            .iter()
1599            .find(|&(_, n, _)| self.html_elem_named(n, local_name!("a")))
1600            .map(|(_, n, _)| n.clone())
1601        else {
1602            return;
1603        };
1604
1605        self.unexpected(tag);
1606        self.adoption_agency(local_name!("a"));
1607        self.position_in_active_formatting(&node)
1608            .map(|index| self.active_formatting.borrow_mut().remove(index));
1609        self.remove_from_stack(&node);
1610    }
1611
1612    //§ tree-construction
1613    fn is_foreign(&self, token: &Token) -> bool {
1614        if let Token::Eof = *token {
1615            return false;
1616        }
1617
1618        if self.open_elems.borrow().is_empty() {
1619            return false;
1620        }
1621
1622        let current = self.adjusted_current_node();
1623        let elem_name = self.sink.elem_name(&current);
1624        let name = elem_name.expanded();
1625        if let ns!(html) = *name.ns {
1626            return false;
1627        }
1628
1629        if mathml_text_integration_point(name) {
1630            match *token {
1631                Token::Characters(..) | Token::NullCharacter => return false,
1632                Token::Tag(Tag {
1633                    kind: StartTag,
1634                    ref name,
1635                    ..
1636                }) if !matches!(*name, local_name!("mglyph") | local_name!("malignmark")) => {
1637                    return false;
1638                },
1639                _ => (),
1640            }
1641        }
1642
1643        if svg_html_integration_point(name) {
1644            match *token {
1645                Token::Characters(..) | Token::NullCharacter => return false,
1646                Token::Tag(Tag { kind: StartTag, .. }) => return false,
1647                _ => (),
1648            }
1649        }
1650
1651        if let expanded_name!(mathml "annotation-xml") = name {
1652            match *token {
1653                Token::Tag(Tag {
1654                    kind: StartTag,
1655                    name: local_name!("svg"),
1656                    ..
1657                }) => return false,
1658                Token::Characters(..)
1659                | Token::NullCharacter
1660                | Token::Tag(Tag { kind: StartTag, .. }) => {
1661                    return !self
1662                        .sink
1663                        .is_mathml_annotation_xml_integration_point(&self.adjusted_current_node());
1664                },
1665                _ => {},
1666            };
1667        }
1668
1669        true
1670    }
1671    //§ END
1672
1673    fn enter_foreign(&self, mut tag: Tag, ns: Namespace) -> ProcessResult<Handle> {
1674        match ns {
1675            ns!(mathml) => self.adjust_mathml_attributes(&mut tag),
1676            ns!(svg) => self.adjust_svg_attributes(&mut tag),
1677            _ => (),
1678        }
1679        self.adjust_foreign_attributes(&mut tag);
1680
1681        if tag.self_closing {
1682            self.insert_element(
1683                PushFlag::NoPush,
1684                ns,
1685                tag.name,
1686                tag.attrs,
1687                tag.had_duplicate_attributes,
1688            );
1689            ProcessResult::DoneAckSelfClosing
1690        } else {
1691            self.insert_element(
1692                PushFlag::Push,
1693                ns,
1694                tag.name,
1695                tag.attrs,
1696                tag.had_duplicate_attributes,
1697            );
1698            ProcessResult::Done
1699        }
1700    }
1701
1702    fn adjust_svg_tag_name(&self, tag: &mut Tag) {
1703        let Tag { ref mut name, .. } = *tag;
1704        match *name {
1705            local_name!("altglyph") => *name = local_name!("altGlyph"),
1706            local_name!("altglyphdef") => *name = local_name!("altGlyphDef"),
1707            local_name!("altglyphitem") => *name = local_name!("altGlyphItem"),
1708            local_name!("animatecolor") => *name = local_name!("animateColor"),
1709            local_name!("animatemotion") => *name = local_name!("animateMotion"),
1710            local_name!("animatetransform") => *name = local_name!("animateTransform"),
1711            local_name!("clippath") => *name = local_name!("clipPath"),
1712            local_name!("feblend") => *name = local_name!("feBlend"),
1713            local_name!("fecolormatrix") => *name = local_name!("feColorMatrix"),
1714            local_name!("fecomponenttransfer") => *name = local_name!("feComponentTransfer"),
1715            local_name!("fecomposite") => *name = local_name!("feComposite"),
1716            local_name!("feconvolvematrix") => *name = local_name!("feConvolveMatrix"),
1717            local_name!("fediffuselighting") => *name = local_name!("feDiffuseLighting"),
1718            local_name!("fedisplacementmap") => *name = local_name!("feDisplacementMap"),
1719            local_name!("fedistantlight") => *name = local_name!("feDistantLight"),
1720            local_name!("fedropshadow") => *name = local_name!("feDropShadow"),
1721            local_name!("feflood") => *name = local_name!("feFlood"),
1722            local_name!("fefunca") => *name = local_name!("feFuncA"),
1723            local_name!("fefuncb") => *name = local_name!("feFuncB"),
1724            local_name!("fefuncg") => *name = local_name!("feFuncG"),
1725            local_name!("fefuncr") => *name = local_name!("feFuncR"),
1726            local_name!("fegaussianblur") => *name = local_name!("feGaussianBlur"),
1727            local_name!("feimage") => *name = local_name!("feImage"),
1728            local_name!("femerge") => *name = local_name!("feMerge"),
1729            local_name!("femergenode") => *name = local_name!("feMergeNode"),
1730            local_name!("femorphology") => *name = local_name!("feMorphology"),
1731            local_name!("feoffset") => *name = local_name!("feOffset"),
1732            local_name!("fepointlight") => *name = local_name!("fePointLight"),
1733            local_name!("fespecularlighting") => *name = local_name!("feSpecularLighting"),
1734            local_name!("fespotlight") => *name = local_name!("feSpotLight"),
1735            local_name!("fetile") => *name = local_name!("feTile"),
1736            local_name!("feturbulence") => *name = local_name!("feTurbulence"),
1737            local_name!("foreignobject") => *name = local_name!("foreignObject"),
1738            local_name!("glyphref") => *name = local_name!("glyphRef"),
1739            local_name!("lineargradient") => *name = local_name!("linearGradient"),
1740            local_name!("radialgradient") => *name = local_name!("radialGradient"),
1741            local_name!("textpath") => *name = local_name!("textPath"),
1742            _ => (),
1743        }
1744    }
1745
1746    fn adjust_attributes<F>(&self, tag: &mut Tag, mut map: F)
1747    where
1748        F: FnMut(LocalName) -> Option<QualName>,
1749    {
1750        for &mut Attribute { ref mut name, .. } in &mut tag.attrs {
1751            if let Some(replacement) = map(name.local.clone()) {
1752                *name = replacement;
1753            }
1754        }
1755    }
1756
1757    fn adjust_svg_attributes(&self, tag: &mut Tag) {
1758        self.adjust_attributes(tag, |k| match k {
1759            local_name!("attributename") => Some(qualname!("", "attributeName")),
1760            local_name!("attributetype") => Some(qualname!("", "attributeType")),
1761            local_name!("basefrequency") => Some(qualname!("", "baseFrequency")),
1762            local_name!("baseprofile") => Some(qualname!("", "baseProfile")),
1763            local_name!("calcmode") => Some(qualname!("", "calcMode")),
1764            local_name!("clippathunits") => Some(qualname!("", "clipPathUnits")),
1765            local_name!("diffuseconstant") => Some(qualname!("", "diffuseConstant")),
1766            local_name!("edgemode") => Some(qualname!("", "edgeMode")),
1767            local_name!("filterunits") => Some(qualname!("", "filterUnits")),
1768            local_name!("glyphref") => Some(qualname!("", "glyphRef")),
1769            local_name!("gradienttransform") => Some(qualname!("", "gradientTransform")),
1770            local_name!("gradientunits") => Some(qualname!("", "gradientUnits")),
1771            local_name!("kernelmatrix") => Some(qualname!("", "kernelMatrix")),
1772            local_name!("kernelunitlength") => Some(qualname!("", "kernelUnitLength")),
1773            local_name!("keypoints") => Some(qualname!("", "keyPoints")),
1774            local_name!("keysplines") => Some(qualname!("", "keySplines")),
1775            local_name!("keytimes") => Some(qualname!("", "keyTimes")),
1776            local_name!("lengthadjust") => Some(qualname!("", "lengthAdjust")),
1777            local_name!("limitingconeangle") => Some(qualname!("", "limitingConeAngle")),
1778            local_name!("markerheight") => Some(qualname!("", "markerHeight")),
1779            local_name!("markerunits") => Some(qualname!("", "markerUnits")),
1780            local_name!("markerwidth") => Some(qualname!("", "markerWidth")),
1781            local_name!("maskcontentunits") => Some(qualname!("", "maskContentUnits")),
1782            local_name!("maskunits") => Some(qualname!("", "maskUnits")),
1783            local_name!("numoctaves") => Some(qualname!("", "numOctaves")),
1784            local_name!("pathlength") => Some(qualname!("", "pathLength")),
1785            local_name!("patterncontentunits") => Some(qualname!("", "patternContentUnits")),
1786            local_name!("patterntransform") => Some(qualname!("", "patternTransform")),
1787            local_name!("patternunits") => Some(qualname!("", "patternUnits")),
1788            local_name!("pointsatx") => Some(qualname!("", "pointsAtX")),
1789            local_name!("pointsaty") => Some(qualname!("", "pointsAtY")),
1790            local_name!("pointsatz") => Some(qualname!("", "pointsAtZ")),
1791            local_name!("preservealpha") => Some(qualname!("", "preserveAlpha")),
1792            local_name!("preserveaspectratio") => Some(qualname!("", "preserveAspectRatio")),
1793            local_name!("primitiveunits") => Some(qualname!("", "primitiveUnits")),
1794            local_name!("refx") => Some(qualname!("", "refX")),
1795            local_name!("refy") => Some(qualname!("", "refY")),
1796            local_name!("repeatcount") => Some(qualname!("", "repeatCount")),
1797            local_name!("repeatdur") => Some(qualname!("", "repeatDur")),
1798            local_name!("requiredextensions") => Some(qualname!("", "requiredExtensions")),
1799            local_name!("requiredfeatures") => Some(qualname!("", "requiredFeatures")),
1800            local_name!("specularconstant") => Some(qualname!("", "specularConstant")),
1801            local_name!("specularexponent") => Some(qualname!("", "specularExponent")),
1802            local_name!("spreadmethod") => Some(qualname!("", "spreadMethod")),
1803            local_name!("startoffset") => Some(qualname!("", "startOffset")),
1804            local_name!("stddeviation") => Some(qualname!("", "stdDeviation")),
1805            local_name!("stitchtiles") => Some(qualname!("", "stitchTiles")),
1806            local_name!("surfacescale") => Some(qualname!("", "surfaceScale")),
1807            local_name!("systemlanguage") => Some(qualname!("", "systemLanguage")),
1808            local_name!("tablevalues") => Some(qualname!("", "tableValues")),
1809            local_name!("targetx") => Some(qualname!("", "targetX")),
1810            local_name!("targety") => Some(qualname!("", "targetY")),
1811            local_name!("textlength") => Some(qualname!("", "textLength")),
1812            local_name!("viewbox") => Some(qualname!("", "viewBox")),
1813            local_name!("viewtarget") => Some(qualname!("", "viewTarget")),
1814            local_name!("xchannelselector") => Some(qualname!("", "xChannelSelector")),
1815            local_name!("ychannelselector") => Some(qualname!("", "yChannelSelector")),
1816            local_name!("zoomandpan") => Some(qualname!("", "zoomAndPan")),
1817            _ => None,
1818        });
1819    }
1820
1821    fn adjust_mathml_attributes(&self, tag: &mut Tag) {
1822        self.adjust_attributes(tag, |k| match k {
1823            local_name!("definitionurl") => Some(qualname!("", "definitionURL")),
1824            _ => None,
1825        });
1826    }
1827
1828    fn adjust_foreign_attributes(&self, tag: &mut Tag) {
1829        self.adjust_attributes(tag, |k| match k {
1830            local_name!("xlink:actuate") => Some(qualname!("xlink" xlink "actuate")),
1831            local_name!("xlink:arcrole") => Some(qualname!("xlink" xlink "arcrole")),
1832            local_name!("xlink:href") => Some(qualname!("xlink" xlink "href")),
1833            local_name!("xlink:role") => Some(qualname!("xlink" xlink "role")),
1834            local_name!("xlink:show") => Some(qualname!("xlink" xlink "show")),
1835            local_name!("xlink:title") => Some(qualname!("xlink" xlink "title")),
1836            local_name!("xlink:type") => Some(qualname!("xlink" xlink "type")),
1837            local_name!("xml:lang") => Some(qualname!("xml" xml "lang")),
1838            local_name!("xml:space") => Some(qualname!("xml" xml "space")),
1839            local_name!("xmlns") => Some(qualname!("" xmlns "xmlns")),
1840            local_name!("xmlns:xlink") => Some(qualname!("xmlns" xmlns "xlink")),
1841            _ => None,
1842        });
1843    }
1844
1845    fn foreign_start_tag(&self, mut tag: Tag) -> ProcessResult<Handle> {
1846        let current_ns = self
1847            .sink
1848            .elem_name(&self.adjusted_current_node())
1849            .ns()
1850            .clone();
1851        match current_ns {
1852            ns!(mathml) => self.adjust_mathml_attributes(&mut tag),
1853            ns!(svg) => {
1854                self.adjust_svg_tag_name(&mut tag);
1855                self.adjust_svg_attributes(&mut tag);
1856            },
1857            _ => (),
1858        }
1859        self.adjust_foreign_attributes(&mut tag);
1860        if tag.self_closing {
1861            // FIXME(#118): <script /> in SVG
1862            self.insert_element(
1863                PushFlag::NoPush,
1864                current_ns,
1865                tag.name,
1866                tag.attrs,
1867                tag.had_duplicate_attributes,
1868            );
1869            ProcessResult::DoneAckSelfClosing
1870        } else {
1871            self.insert_element(
1872                PushFlag::Push,
1873                current_ns,
1874                tag.name,
1875                tag.attrs,
1876                tag.had_duplicate_attributes,
1877            );
1878            ProcessResult::Done
1879        }
1880    }
1881
1882    fn unexpected_start_tag_in_foreign_content(&self, tag: Tag) -> ProcessResult<Handle> {
1883        self.unexpected(&tag);
1884        while !self.current_node_in(|n| {
1885            *n.ns == ns!(html) || mathml_text_integration_point(n) || svg_html_integration_point(n)
1886        }) {
1887            self.pop();
1888        }
1889        self.step(self.mode.get(), Token::Tag(tag))
1890    }
1891}