script/dom/html/
htmltemplateelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix};
7use js::rust::HandleObject;
8
9use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
10use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
11use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::{DomRoot, MutNullableDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::document::Document;
16use crate::dom::documentfragment::DocumentFragment;
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{CloneChildrenFlag, Node, NodeTraits};
19use crate::dom::virtualmethods::VirtualMethods;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct HTMLTemplateElement {
24    htmlelement: HTMLElement,
25
26    /// <https://html.spec.whatwg.org/multipage/#template-contents>
27    contents: MutNullableDom<DocumentFragment>,
28}
29
30impl HTMLTemplateElement {
31    fn new_inherited(
32        local_name: LocalName,
33        prefix: Option<Prefix>,
34        document: &Document,
35    ) -> HTMLTemplateElement {
36        HTMLTemplateElement {
37            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
38            contents: MutNullableDom::new(None),
39        }
40    }
41
42    pub(crate) fn new(
43        local_name: LocalName,
44        prefix: Option<Prefix>,
45        document: &Document,
46        proto: Option<HandleObject>,
47        can_gc: CanGc,
48    ) -> DomRoot<HTMLTemplateElement> {
49        let n = Node::reflect_node_with_proto(
50            Box::new(HTMLTemplateElement::new_inherited(
51                local_name, prefix, document,
52            )),
53            document,
54            proto,
55            can_gc,
56        );
57
58        n.upcast::<Node>().set_weird_parser_insertion_mode();
59        n
60    }
61
62    pub(crate) fn set_contents(&self, document_fragment: Option<&DocumentFragment>) {
63        self.contents.set(document_fragment);
64    }
65}
66
67#[expect(unused_doc_comments)]
68impl HTMLTemplateElementMethods<crate::DomTypeHolder> for HTMLTemplateElement {
69    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootmode>
70    make_enumerated_getter!(
71        ShadowRootMode,
72        "shadowrootmode",
73        "open" | "closed",
74        missing => "",
75        invalid => ""
76    );
77
78    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootmode>
79    make_atomic_setter!(SetShadowRootMode, "shadowrootmode");
80
81    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootdelegatesfocus>
82    make_bool_getter!(ShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
83
84    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootdelegatesfocus>
85    make_bool_setter!(SetShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
86
87    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable>
88    make_bool_getter!(ShadowRootClonable, "shadowrootclonable");
89
90    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable>
91    make_bool_setter!(SetShadowRootClonable, "shadowrootclonable");
92
93    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable>
94    make_bool_getter!(ShadowRootSerializable, "shadowrootserializable");
95
96    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable>
97    make_bool_setter!(SetShadowRootSerializable, "shadowrootserializable");
98
99    /// <https://html.spec.whatwg.org/multipage/#dom-template-content>
100    fn Content(&self, can_gc: CanGc) -> DomRoot<DocumentFragment> {
101        self.contents.or_init(|| {
102            // https://html.spec.whatwg.org/multipage/#template-contents
103            // Step 1. Let document be the template element's node document's appropriate template contents owner document.
104            let doc = self.owner_document();
105            // Step 2. Create a DocumentFragment object whose node document is document and host is the template element.
106            let document_fragment = doc
107                .appropriate_template_contents_owner_document(can_gc)
108                .CreateDocumentFragment(can_gc);
109            document_fragment.set_host(self.upcast());
110            // Step 3. Set the template element's template contents to the newly created DocumentFragment object.
111            document_fragment
112        })
113    }
114}
115
116impl VirtualMethods for HTMLTemplateElement {
117    fn super_type(&self) -> Option<&dyn VirtualMethods> {
118        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
119    }
120
121    /// <https://html.spec.whatwg.org/multipage/#template-adopting-steps>
122    fn adopting_steps(&self, old_doc: &Document, can_gc: CanGc) {
123        self.super_type().unwrap().adopting_steps(old_doc, can_gc);
124        // Step 1.
125        let doc = self
126            .owner_document()
127            .appropriate_template_contents_owner_document(CanGc::note());
128        // Step 2.
129        Node::adopt(self.Content(CanGc::note()).upcast(), &doc, can_gc);
130    }
131
132    /// <https://html.spec.whatwg.org/multipage/#the-template-element:concept-node-clone-ext>
133    fn cloning_steps(
134        &self,
135        copy: &Node,
136        maybe_doc: Option<&Document>,
137        clone_children: CloneChildrenFlag,
138        can_gc: CanGc,
139    ) {
140        self.super_type()
141            .unwrap()
142            .cloning_steps(copy, maybe_doc, clone_children, can_gc);
143        if clone_children == CloneChildrenFlag::DoNotCloneChildren {
144            // Step 1.
145            return;
146        }
147        let copy = copy.downcast::<HTMLTemplateElement>().unwrap();
148        // Steps 2-3.
149        let copy_contents = DomRoot::upcast::<Node>(copy.Content(CanGc::note()));
150        let copy_contents_doc = copy_contents.owner_doc();
151        for child in self.Content(CanGc::note()).upcast::<Node>().children() {
152            let copy_child = Node::clone(
153                &child,
154                Some(&copy_contents_doc),
155                CloneChildrenFlag::CloneChildren,
156                None,
157                CanGc::note(),
158            );
159            copy_contents.AppendChild(&copy_child, can_gc).unwrap();
160        }
161    }
162}