Skip to main content

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::context::JSContext;
8use js::rust::HandleObject;
9
10use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
11use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
12use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::{DomRoot, MutNullableDom};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::documentfragment::DocumentFragment;
18use crate::dom::html::htmlelement::HTMLElement;
19use crate::dom::node::virtualmethods::VirtualMethods;
20use crate::dom::node::{CloneChildrenFlag, Node, NodeTraits};
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        cx: &mut js::context::JSContext,
44        local_name: LocalName,
45        prefix: Option<Prefix>,
46        document: &Document,
47        proto: Option<HandleObject>,
48    ) -> DomRoot<HTMLTemplateElement> {
49        let n = Node::reflect_node_with_proto(
50            cx,
51            Box::new(HTMLTemplateElement::new_inherited(
52                local_name, prefix, document,
53            )),
54            document,
55            proto,
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/#attr-template-shadowrootslotassignment>
88    make_enumerated_getter!(
89        ShadowRootSlotAssignment,
90        "shadowrootslotassignment",
91        "named" | "manual",
92        missing => "named",
93        invalid => "named"
94    );
95
96    /// <https://html.spec.whatwg.org/multipage/#attr-template-shadowrootslotassignment>
97    make_atomic_setter!(SetShadowRootSlotAssignment, "shadowrootslotassignment");
98
99    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable>
100    make_bool_getter!(ShadowRootClonable, "shadowrootclonable");
101
102    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable>
103    make_bool_setter!(SetShadowRootClonable, "shadowrootclonable");
104
105    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable>
106    make_bool_getter!(ShadowRootSerializable, "shadowrootserializable");
107
108    /// <https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable>
109    make_bool_setter!(SetShadowRootSerializable, "shadowrootserializable");
110
111    /// <https://html.spec.whatwg.org/multipage/#dom-template-content>
112    fn Content(&self, cx: &mut js::context::JSContext) -> DomRoot<DocumentFragment> {
113        self.contents.or_init(|| {
114            // https://html.spec.whatwg.org/multipage/#template-contents
115            // Step 1. Let document be the template element's node document's appropriate template contents owner document.
116            let doc = self.owner_document();
117            // Step 2. Create a DocumentFragment object whose node document is document and host is the template element.
118            let document_fragment = doc
119                .appropriate_template_contents_owner_document(cx)
120                .CreateDocumentFragment(cx);
121            document_fragment.set_host(self.upcast());
122            // Step 3. Set the template element's template contents to the newly created DocumentFragment object.
123            document_fragment
124        })
125    }
126}
127
128impl VirtualMethods for HTMLTemplateElement {
129    fn super_type(&self) -> Option<&dyn VirtualMethods> {
130        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
131    }
132
133    /// <https://html.spec.whatwg.org/multipage/#template-adopting-steps>
134    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
135        self.super_type().unwrap().adopting_steps(cx, old_doc);
136        // Step 1.
137        let doc = self
138            .owner_document()
139            .appropriate_template_contents_owner_document(cx);
140        // Step 2.
141        let content = self.Content(cx);
142        Node::adopt(cx, content.upcast(), &doc);
143    }
144
145    /// <https://html.spec.whatwg.org/multipage/#the-template-element:concept-node-clone-ext>
146    fn cloning_steps(
147        &self,
148        cx: &mut JSContext,
149        copy: &Node,
150        maybe_doc: Option<&Document>,
151        clone_children: CloneChildrenFlag,
152    ) {
153        self.super_type()
154            .unwrap()
155            .cloning_steps(cx, copy, maybe_doc, clone_children);
156        if clone_children == CloneChildrenFlag::DoNotCloneChildren {
157            // Step 1.
158            return;
159        }
160        let copy = copy.downcast::<HTMLTemplateElement>().unwrap();
161        // Steps 2-3.
162        let copy_contents = DomRoot::upcast::<Node>(copy.Content(cx));
163        let copy_contents_doc = copy_contents.owner_doc();
164        for child in self.Content(cx).upcast::<Node>().children() {
165            let copy_child = Node::clone(
166                cx,
167                &child,
168                Some(&copy_contents_doc),
169                CloneChildrenFlag::CloneChildren,
170                None,
171            );
172            copy_contents.AppendChild(cx, &copy_child).unwrap();
173        }
174    }
175}