Skip to main content

blitz_dom/
html.rs

1use crate::{BaseDocument, Document, DocumentConfig, DocumentMutator, PlainDocument};
2use blitz_traits::node_id::NodeId;
3
4pub trait HtmlParserProvider {
5    fn parse_inner_html<'m, 'doc>(
6        &self,
7        mutr: &'m mut DocumentMutator<'doc>,
8        element_id: NodeId,
9        html: &str,
10    );
11
12    /// Parse a fragment. `allow_declarative_shadow_roots` is set for
13    /// `setHTMLUnsafe` and clear for `innerHTML`.
14    ///
15    /// The default ignores the flag and parses the way `innerHTML` does, so a
16    /// parser without declarative shadow roots keeps working.
17    fn parse_fragment<'m, 'doc>(
18        &self,
19        mutr: &'m mut DocumentMutator<'doc>,
20        element_id: NodeId,
21        html: &str,
22        allow_declarative_shadow_roots: bool,
23    ) {
24        let _ = allow_declarative_shadow_roots;
25        self.parse_inner_html(mutr, element_id, html);
26    }
27
28    /// Parse a full HTML document (e.g. the contents of an `<iframe>`).
29    ///
30    /// The default implementation ignores the HTML and returns an empty document.
31    fn parse_document(&self, html: &str, config: DocumentConfig) -> Box<dyn Document> {
32        let _ = html;
33        Box::new(PlainDocument(BaseDocument::new(config)))
34    }
35}
36
37pub struct DummyHtmlParserProvider;
38impl HtmlParserProvider for DummyHtmlParserProvider {
39    fn parse_inner_html<'m, 'doc>(
40        &self,
41        mutr: &'m mut DocumentMutator<'doc>,
42        element_id: NodeId,
43        html: &str,
44    ) {
45        let _ = mutr;
46        let _ = element_id;
47        let _ = html;
48        // Do nothing for now
49        //
50        // TODO: do something:
51        // - Print warning?
52        // - Parse HTML as plain text?
53    }
54}