ps-blitz-html 0.5.1

Blitz HTML parser
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
//! An implementation for Html5ever's sink trait, allowing us to parse HTML into a DOM.

use html5ever::ParseOpts;
use html5ever::tokenizer::TokenizerOpts;
use html5ever::tree_builder::TreeBuilderOpts;
use std::borrow::Cow;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::sync::Arc;

use blitz_dom::node::{Attribute, MarkupNode, NodeFlags};
use blitz_dom::{DocumentMutator, HtmlParserProvider, NodeId};
use html5ever::{
    QualName,
    tendril::{StrTendril, TendrilSink},
    tree_builder::{ElementFlags, NodeOrText, QuirksMode, TreeSink},
};

/// Convert an html5ever Attribute, which uses tendril for its value, to a blitz
/// Attribute, which interns its value.
///
/// This is the highest-volume construction site in the engine: every attribute
/// of every element of every parsed document arrives here. It is therefore
/// where interning pays off most, because a document's repeated `class` strings
/// collapse as the tree is built rather than after it.
fn html5ever_to_blitz_attr(attr: html5ever::Attribute) -> Attribute {
    Attribute {
        name: attr.name,
        value: attr.value.as_ref().into(),
    }
}

#[derive(Copy, Clone, Default, Debug)]
pub struct HtmlProvider;

impl HtmlParserProvider for HtmlProvider {
    fn parse_inner_html<'m2, 'doc2>(
        &self,
        mutr: &'m2 mut DocumentMutator<'doc2>,
        element_id: NodeId,
        html: &str,
    ) {
        DocumentHtmlParser::parse_inner_html_into_mutator(mutr, element_id, html, false);
    }

    fn parse_fragment<'m2, 'doc2>(
        &self,
        mutr: &'m2 mut DocumentMutator<'doc2>,
        element_id: NodeId,
        html: &str,
        allow_declarative_shadow_roots: bool,
    ) {
        DocumentHtmlParser::parse_inner_html_into_mutator(
            mutr,
            element_id,
            html,
            allow_declarative_shadow_roots,
        );
    }

    fn parse_document(
        &self,
        html: &str,
        config: blitz_dom::DocumentConfig,
    ) -> Box<dyn blitz_dom::Document> {
        Box::new(crate::HtmlDocument::from_html(html, config))
    }
}

pub struct DocumentHtmlParser<'m, 'doc> {
    document_mutator: RefCell<&'m mut DocumentMutator<'doc>>,

    /// Errors that occurred during parsing.
    pub errors: RefCell<Vec<Cow<'static, str>>>,

    /// The document's quirks mode.
    pub quirks_mode: Cell<QuirksMode>,
    pub is_xml: bool,
    document_id: Option<NodeId>,
    parsing_document: bool,
    /// Document parse and `setHTMLUnsafe` allow declarative shadow roots.
    /// `innerHTML`, `DOMParser`, and `insertAdjacentHTML` do not.
    allow_declarative_shadow_roots: bool,
}

impl<'m, 'doc> DocumentHtmlParser<'m, 'doc> {
    #[track_caller]
    /// Get a mutable borrow of the DocumentMutator
    fn mutr(&self) -> RefMut<'_, &'m mut DocumentMutator<'doc>> {
        self.document_mutator.borrow_mut()
    }

    fn adopt_created(&self, node_id: NodeId) {
        if let Some(document_id) = self.document_id {
            self.mutr().adopt_node(node_id, document_id);
        }
    }
}

impl<'m, 'doc> DocumentHtmlParser<'m, 'doc> {
    pub fn new(mutr: &'m mut DocumentMutator<'doc>) -> DocumentHtmlParser<'m, 'doc> {
        DocumentHtmlParser {
            document_mutator: RefCell::new(mutr),
            errors: RefCell::new(Vec::new()),
            quirks_mode: Cell::new(QuirksMode::NoQuirks),
            is_xml: false,
            document_id: None,
            parsing_document: true,
            allow_declarative_shadow_roots: true,
        }
    }

    /// Parse into a detached document. The caller's MIME type selects the
    /// parser independently of declarations in the source.
    pub fn parse_inert_into_mutator(
        mutr: &mut DocumentMutator<'_>,
        document_id: NodeId,
        source: &str,
        is_xml: bool,
    ) -> Vec<Cow<'static, str>> {
        let mut sink = DocumentHtmlParser::new(mutr);
        sink.document_id = Some(document_id);
        sink.is_xml = is_xml;
        sink.allow_declarative_shadow_roots = false;
        if is_xml {
            let errors = xml5ever::driver::parse_document(sink, Default::default()).one(source);
            Self::repair_xml_prolog(mutr, document_id, source);
            errors
        } else {
            let opts = ParseOpts {
                tokenizer: TokenizerOpts::default(),
                tree_builder: TreeBuilderOpts {
                    exact_errors: false,
                    scripting_enabled: false,
                    iframe_srcdoc: false,
                    drop_doctype: false,
                    quirks_mode: QuirksMode::NoQuirks,
                },
            };
            html5ever::parse_document(sink, opts).one(source)
        }
    }

    /// Undo two xml5ever tokenizer habits that XML documents must not show:
    /// the XML declaration surfaces as a `<?xml ...?>` processing instruction,
    /// and DOCTYPE names are ASCII-lowercased (HTML's rule, not XML's). The
    /// declaration is dropped and the name restored from the source.
    fn repair_xml_prolog(mutr: &mut DocumentMutator<'_>, document_id: NodeId, source: &str) {
        let children: Vec<NodeId> = mutr
            .doc
            .get_node(document_id)
            .map(|node| node.children.to_vec())
            .unwrap_or_default();
        let name = source.find("<!DOCTYPE").map(|start| {
            source[start + "<!DOCTYPE".len()..]
                .trim_start()
                .split(|c: char| c.is_whitespace() || c == '[' || c == '>')
                .next()
                .unwrap_or_default()
                .to_owned()
        });
        for child in children {
            let markup = mutr
                .doc
                .get_node(child)
                .and_then(|node| node.markup.clone());
            match markup.as_deref() {
                Some(MarkupNode::ProcessingInstruction { target }) if target == "xml" => {
                    mutr.remove_node(child);
                }
                Some(MarkupNode::Doctype {
                    name: parsed,
                    public_id,
                    system_id,
                }) => {
                    if let Some(original) = &name
                        && original.eq_ignore_ascii_case(parsed)
                        && original != parsed
                    {
                        let repaired = MarkupNode::Doctype {
                            name: original.clone(),
                            public_id: public_id.clone(),
                            system_id: system_id.clone(),
                        };
                        if let Some(node) = mutr.doc.get_node_mut(child) {
                            node.markup = Some(Arc::new(repaired));
                        }
                    }
                }
                _ => {}
            }
        }
    }

    /// Detects documents without an XML or DOCTYPE declaration whose root `<html>` element
    /// declares the XHTML namespace (e.g. `<html xmlns="http://www.w3.org/1999/xhtml">`)
    fn root_element_has_xhtml_namespace(html: &str) -> bool {
        let rest = html.trim_start_matches('\u{feff}').trim_start();
        let Some(rest) = rest.strip_prefix("<html") else {
            return false;
        };
        let Some(tag_end) = rest.find('>') else {
            return false;
        };
        rest[..tag_end].contains("xmlns=\"http://www.w3.org/1999/xhtml\"")
            || rest[..tag_end].contains("xmlns='http://www.w3.org/1999/xhtml'")
    }

    pub fn is_xhtml_document(html: &str) -> bool {
        html.starts_with("<?xml")
            || html.starts_with("<!DOCTYPE") && {
                let first_line = html.lines().next().unwrap();
                first_line.contains("XHTML") || first_line.contains("xhtml")
            }
            || Self::root_element_has_xhtml_namespace(html)
    }

    pub fn parse_into_mutator<'a, 'd>(mutr: &'a mut DocumentMutator<'d>, html: &str) {
        let mut sink = DocumentHtmlParser::new(mutr);

        let is_xhtml_doc = Self::is_xhtml_document(html);

        if is_xhtml_doc {
            // Parse as XHTML
            sink.is_xml = true;
            xml5ever::driver::parse_document(sink, Default::default())
                .from_utf8()
                .read_from(&mut html.as_bytes())
                .unwrap();
        } else {
            // Parse as HTML
            sink.is_xml = false;
            let opts = ParseOpts {
                tokenizer: TokenizerOpts::default(),
                tree_builder: TreeBuilderOpts {
                    exact_errors: false,
                    scripting_enabled: true, // scripting runs, so noscript contents are text
                    iframe_srcdoc: false,
                    drop_doctype: false,
                    quirks_mode: QuirksMode::NoQuirks,
                },
            };
            html5ever::parse_document(sink, opts)
                .from_utf8()
                .read_from(&mut html.as_bytes())
                .unwrap();
        }
    }

    /// Parse HTML into a new document node. Declarative shadow roots attach.
    /// Scripts stay inert, matching `Document.parseHTMLUnsafe`.
    pub fn parse_html_unsafe_into_mutator(
        mutr: &mut DocumentMutator<'_>,
        document_id: NodeId,
        source: &str,
    ) -> Vec<Cow<'static, str>> {
        let mut sink = DocumentHtmlParser::new(mutr);
        sink.document_id = Some(document_id);
        sink.is_xml = false;
        sink.allow_declarative_shadow_roots = true;
        let opts = ParseOpts {
            tokenizer: TokenizerOpts::default(),
            tree_builder: TreeBuilderOpts {
                exact_errors: false,
                scripting_enabled: false,
                iframe_srcdoc: false,
                drop_doctype: false,
                quirks_mode: QuirksMode::NoQuirks,
            },
        };
        html5ever::parse_document(sink, opts).one(source)
    }

    pub fn parse_inner_html_into_mutator<'a, 'd>(
        mutr: &'a mut DocumentMutator<'d>,
        element_id: NodeId,
        html: &str,
        allow_declarative_shadow_roots: bool,
    ) {
        let mut sink = DocumentHtmlParser::new(mutr);
        sink.parsing_document = false;
        sink.allow_declarative_shadow_roots = allow_declarative_shadow_roots;

        let opts = ParseOpts {
            tokenizer: TokenizerOpts::default(),
            tree_builder: TreeBuilderOpts {
                exact_errors: false,
                scripting_enabled: true, // scripting runs, so noscript contents are text
                iframe_srcdoc: false,
                drop_doctype: true,
                quirks_mode: QuirksMode::NoQuirks,
            },
        };
        html5ever::driver::parse_fragment_for_element(sink, opts, element_id, false, None)
            .from_utf8()
            .read_from(&mut html.as_bytes())
            .unwrap();

        // html5ever creates a new fragment root node under the document node and parses the nodes into that fragment root.
        // So here we move the children of the fragment root to the context's insertion target and then drop the fragment root.
        let document_id = mutr.doc.root_node().id;
        let fragment_root_id = mutr.last_child_id(document_id).unwrap();
        let child_ids = mutr.child_ids(fragment_root_id);
        let target_id = mutr
            .doc
            .get_node(element_id)
            .and_then(|node| node.element_data())
            .and_then(|element| element.template_contents)
            .unwrap_or(element_id);
        mutr.append_children(target_id, &child_ids);
        mutr.remove_and_drop_node(fragment_root_id);
    }
}

impl<'m, 'doc> TreeSink for DocumentHtmlParser<'m, 'doc> {
    type Output = Vec<Cow<'static, str>>;

    // we use the ID of the nodes in the tree as the handle
    type Handle = NodeId;

    type ElemName<'a>
        = Ref<'a, QualName>
    where
        Self: 'a;

    fn finish(self) -> Self::Output {
        #[cfg(feature = "tracing")]
        for error in self.errors.borrow().iter() {
            tracing::error!("{error}");
        }
        self.errors.into_inner()
    }

    fn parse_error(&self, msg: Cow<'static, str>) {
        self.errors.borrow_mut().push(msg);
    }

    fn get_document(&self) -> Self::Handle {
        self.document_id
            .unwrap_or_else(|| self.document_mutator.borrow().doc.root_node().id)
    }

    fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> Self::ElemName<'a> {
        Ref::map(self.document_mutator.borrow(), |docm| {
            docm.element_name(*target)
                .expect("TreeSink::elem_name called on a node which is not an element!")
        })
    }

    fn create_element(
        &self,
        name: QualName,
        attrs: Vec<html5ever::Attribute>,
        _flags: ElementFlags,
    ) -> Self::Handle {
        let inert_script = self.document_id.is_some() && name.local.as_ref() == "script";
        let attrs = attrs.into_iter().map(html5ever_to_blitz_attr).collect();
        let id = self.mutr().create_element(name, attrs);
        self.adopt_created(id);
        if inert_script {
            self.mutr()
                .doc
                .get_node_mut(id)
                .expect("new parser element missing")
                .flags
                .insert(NodeFlags::IS_PARSER_INERT_SCRIPT);
        }
        id
    }

    fn create_comment(&self, text: StrTendril) -> Self::Handle {
        let id = self.mutr().create_comment_node(&text);
        self.adopt_created(id);
        id
    }

    fn create_pi(&self, target: StrTendril, data: StrTendril) -> Self::Handle {
        if self.document_id.is_none() {
            return self.mutr().create_comment_node("");
        }
        let id = self.mutr().create_comment_node(&data);
        self.adopt_created(id);
        self.mutr()
            .doc
            .get_node_mut(id)
            .expect("new processing instruction missing")
            .markup = Some(Arc::new(MarkupNode::ProcessingInstruction {
            target: target.to_string(),
        }));
        id
    }

    fn append(&self, parent_id: &Self::Handle, child: NodeOrText<Self::Handle>) {
        match child {
            NodeOrText::AppendNode(id) => self.mutr().append_children(*parent_id, &[id]),
            // If content to append is text, first attempt to append it to the last child of parent.
            // Else create a new text node and append it to the parent
            NodeOrText::AppendText(text) => {
                let last_child_id = self.mutr().last_child_id(*parent_id);
                let has_appended = if let Some(id) = last_child_id {
                    self.mutr().append_text_to_node(id, &text).is_ok()
                } else {
                    false
                };
                if !has_appended {
                    let new_child_id = self.mutr().create_text_node(&text);
                    self.adopt_created(new_child_id);
                    self.mutr().append_children(*parent_id, &[new_child_id]);
                }
            }
        }
    }

    // Note: The tree builder promises we won't have a text node after the insertion point.
    // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Stacking_contexts
    fn append_before_sibling(&self, sibling_id: &Self::Handle, new_node: NodeOrText<Self::Handle>) {
        match new_node {
            NodeOrText::AppendNode(id) => self.mutr().insert_nodes_before(*sibling_id, &[id]),
            // If content to append is text, first attempt to append it to the node before sibling_node
            // Else create a new text node and insert it before sibling_node
            NodeOrText::AppendText(text) => {
                let previous_sibling_id = self.mutr().previous_sibling_id(*sibling_id);
                let has_appended = if let Some(id) = previous_sibling_id {
                    self.mutr().append_text_to_node(id, &text).is_ok()
                } else {
                    false
                };
                if !has_appended {
                    let new_child_id = self.mutr().create_text_node(&text);
                    self.adopt_created(new_child_id);
                    self.mutr()
                        .insert_nodes_before(*sibling_id, &[new_child_id]);
                }
            }
        };
    }

    fn append_based_on_parent_node(
        &self,
        element: &Self::Handle,
        prev_element: &Self::Handle,
        child: NodeOrText<Self::Handle>,
    ) {
        if self.mutr().node_has_parent(*element) {
            self.append_before_sibling(element, child);
        } else {
            self.append(prev_element, child);
        }
    }

    fn append_doctype_to_document(
        &self,
        name: StrTendril,
        public_id: StrTendril,
        system_id: StrTendril,
    ) {
        let document_id = self.get_document();
        let mut mutr = self.mutr();
        let id = mutr.create_comment_node("");
        mutr.adopt_node(id, document_id);
        mutr.doc
            .get_node_mut(id)
            .expect("new doctype missing")
            .markup = Some(Arc::new(MarkupNode::Doctype {
            name: name.to_string(),
            public_id: public_id.to_string(),
            system_id: system_id.to_string(),
        }));
        mutr.append_children(document_id, &[id]);
    }

    fn get_template_contents(&self, target: &Self::Handle) -> Self::Handle {
        // While a declarative template is open, its children belong to the
        // shadow root. The association is on the document: a streaming parse
        // builds a fresh parser for every tree-sink call.
        #[cfg(feature = "shadow-dom")]
        if let Some(shadow) = self.mutr().declarative_shadow_for_template(*target) {
            return shadow;
        }
        self.mutr().template_contents(*target)
    }

    fn allow_declarative_shadow_roots(&self, _intended_parent: &Self::Handle) -> bool {
        // The feature gate is compile-time. Leaving the flag true without the
        // shadow-dom feature would make html5ever build an off-tree template
        // and then a second, inserted one.
        self.allow_declarative_shadow_roots && cfg!(feature = "shadow-dom")
    }

    fn attach_declarative_shadow(
        &self,
        location: &Self::Handle,
        template: &Self::Handle,
        attrs: &[html5ever::Attribute],
    ) -> bool {
        #[cfg(not(feature = "shadow-dom"))]
        {
            let _ = (location, template, attrs);
            return false;
        }
        #[cfg(feature = "shadow-dom")]
        {
            let mut mode = None;
            let mut clonable = false;
            let mut serializable = false;
            let mut delegates_focus = false;
            for attr in attrs {
                let name = attr.name.local.as_ref();
                if name == "shadowrootmode" {
                    let value = attr.value.as_ref();
                    if value.eq_ignore_ascii_case("open") {
                        mode = Some(blitz_dom::node::ShadowRootMode::Open);
                    } else if value.eq_ignore_ascii_case("closed") {
                        mode = Some(blitz_dom::node::ShadowRootMode::Closed);
                    }
                } else if name == "shadowrootclonable" {
                    clonable = true;
                } else if name == "shadowrootserializable" {
                    serializable = true;
                } else if name == "shadowrootdelegatesfocus" {
                    delegates_focus = true;
                }
            }
            let Some(mode) = mode else {
                return false;
            };
            self.mutr().try_attach_declarative_shadow(
                *location,
                *template,
                mode,
                clonable,
                serializable,
                delegates_focus,
            )
        }
    }

    fn pop(&self, node: &Self::Handle) {
        // html5ever inserts a declarative candidate off-tree. On failure it
        // pops that node and inserts a different element for the same tag, so
        // the off-tree node has to be dropped here or it leaks. On success the
        // template is never inserted; the end-tag pop drops it after its
        // children have gone to the shadow root.
        let drop_template = {
            let mutr = self.mutr();
            mutr.doc.get_node(*node).is_some_and(|current| {
                current.parent.is_none()
                    && current
                        .element_data()
                        .is_some_and(|element| element.name.local.as_ref() == "template")
            })
        };
        if !drop_template {
            return;
        }
        #[cfg(feature = "shadow-dom")]
        self.mutr().forget_declarative_template(*node);
        self.mutr().remove_and_drop_node(*node);
    }

    fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
        x == y
    }

    fn set_quirks_mode(&self, mode: QuirksMode) {
        self.quirks_mode.set(mode);
        if self.parsing_document {
            self.mutr().doc.set_document_quirks_mode(match mode {
                QuirksMode::NoQuirks => 0,
                QuirksMode::LimitedQuirks => 1,
                QuirksMode::Quirks => 2,
            });
        }
    }

    fn add_attrs_if_missing(&self, target: &Self::Handle, attrs: Vec<html5ever::Attribute>) {
        let attrs = attrs.into_iter().map(html5ever_to_blitz_attr).collect();
        self.mutr().add_attrs_if_missing(*target, attrs);
    }

    fn remove_from_parent(&self, target: &Self::Handle) {
        self.mutr().remove_node(*target);
    }

    fn reparent_children(&self, old_parent_id: &Self::Handle, new_parent_id: &Self::Handle) {
        self.mutr()
            .reparent_children(*old_parent_id, *new_parent_id);
    }
}

#[test]
fn parses_some_html() {
    use blitz_dom::{BaseDocument, DocumentConfig};

    let html = "<!DOCTYPE html><html><body><h1>hello world</h1></body></html>";
    let mut doc = BaseDocument::new(DocumentConfig::default());
    let mut mutr = doc.mutate();
    let sink = DocumentHtmlParser::new(&mut mutr);

    html5ever::parse_document(sink, Default::default())
        .from_utf8()
        .read_from(&mut html.as_bytes())
        .unwrap();

    drop(mutr);
    doc.print_tree()

    // Now our tree should have some nodes in it
}