Skip to main content

script/dom/html/scripting/
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
67impl HTMLTemplateElementMethods<crate::DomTypeHolder> for HTMLTemplateElement {
68    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootmode
69    make_enumerated_getter!(
70        ShadowRootMode,
71        "shadowrootmode",
72        "open" | "closed",
73        missing => "",
74        invalid => ""
75    );
76
77    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootmode
78    make_atomic_setter!(SetShadowRootMode, "shadowrootmode");
79
80    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootdelegatesfocus
81    make_bool_getter!(ShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
82
83    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootdelegatesfocus
84    make_bool_setter!(SetShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
85
86    // https://html.spec.whatwg.org/multipage/#attr-template-shadowrootslotassignment
87    make_enumerated_getter!(
88        ShadowRootSlotAssignment,
89        "shadowrootslotassignment",
90        "named" | "manual",
91        missing => "named",
92        invalid => "named"
93    );
94
95    // https://html.spec.whatwg.org/multipage/#attr-template-shadowrootslotassignment
96    make_atomic_setter!(SetShadowRootSlotAssignment, "shadowrootslotassignment");
97
98    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable
99    make_bool_getter!(ShadowRootClonable, "shadowrootclonable");
100
101    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootclonable
102    make_bool_setter!(SetShadowRootClonable, "shadowrootclonable");
103
104    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable
105    make_bool_getter!(ShadowRootSerializable, "shadowrootserializable");
106
107    // https://html.spec.whatwg.org/multipage/#dom-template-shadowrootserializable
108    make_bool_setter!(SetShadowRootSerializable, "shadowrootserializable");
109
110    /// <https://html.spec.whatwg.org/multipage/#dom-template-content>
111    fn Content(&self, cx: &mut js::context::JSContext) -> DomRoot<DocumentFragment> {
112        self.contents.or_init(|| {
113            // https://html.spec.whatwg.org/multipage/#template-contents
114            // Step 1. Let document be the template element's node document's appropriate template contents owner document.
115            let doc = self.owner_document();
116            // Step 2. Create a DocumentFragment object whose node document is document and host is the template element.
117            let document_fragment = doc
118                .appropriate_template_contents_owner_document(cx)
119                .CreateDocumentFragment(cx);
120            document_fragment.set_host(self.upcast());
121            // Step 3. Set the template element's template contents to the newly created DocumentFragment object.
122            document_fragment
123        })
124    }
125}
126
127impl VirtualMethods for HTMLTemplateElement {
128    fn super_type(&self) -> Option<&dyn VirtualMethods> {
129        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
130    }
131
132    /// <https://html.spec.whatwg.org/multipage/#template-adopting-steps>
133    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
134        self.super_type().unwrap().adopting_steps(cx, old_doc);
135        // Step 1.
136        let doc = self
137            .owner_document()
138            .appropriate_template_contents_owner_document(cx);
139        // Step 2.
140        let content = self.Content(cx);
141        Node::adopt(cx, content.upcast(), &doc);
142    }
143
144    /// <https://html.spec.whatwg.org/multipage/#the-template-element:concept-node-clone-ext>
145    fn cloning_steps(
146        &self,
147        cx: &mut JSContext,
148        copy: &Node,
149        maybe_doc: Option<&Document>,
150        clone_children: CloneChildrenFlag,
151    ) {
152        self.super_type()
153            .unwrap()
154            .cloning_steps(cx, copy, maybe_doc, clone_children);
155        if clone_children == CloneChildrenFlag::DoNotCloneChildren {
156            // Step 1.
157            return;
158        }
159        let copy = copy.downcast::<HTMLTemplateElement>().unwrap();
160        // Steps 2-3.
161        let copy_contents = DomRoot::upcast::<Node>(copy.Content(cx));
162        let copy_contents_doc = copy_contents.owner_doc();
163        for child in self.Content(cx).upcast::<Node>().children() {
164            let copy_child = Node::clone(
165                cx,
166                &child,
167                Some(&copy_contents_doc),
168                CloneChildrenFlag::CloneChildren,
169                None,
170            );
171            copy_contents.AppendChild(cx, &copy_child).unwrap();
172        }
173    }
174}