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            let doc = self.owner_document();
103            doc.appropriate_template_contents_owner_document(can_gc)
104                .CreateDocumentFragment(can_gc)
105        })
106    }
107}
108
109impl VirtualMethods for HTMLTemplateElement {
110    fn super_type(&self) -> Option<&dyn VirtualMethods> {
111        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
112    }
113
114    /// <https://html.spec.whatwg.org/multipage/#template-adopting-steps>
115    fn adopting_steps(&self, old_doc: &Document, can_gc: CanGc) {
116        self.super_type().unwrap().adopting_steps(old_doc, can_gc);
117        // Step 1.
118        let doc = self
119            .owner_document()
120            .appropriate_template_contents_owner_document(CanGc::note());
121        // Step 2.
122        Node::adopt(self.Content(CanGc::note()).upcast(), &doc, can_gc);
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#the-template-element:concept-node-clone-ext>
126    fn cloning_steps(
127        &self,
128        copy: &Node,
129        maybe_doc: Option<&Document>,
130        clone_children: CloneChildrenFlag,
131        can_gc: CanGc,
132    ) {
133        self.super_type()
134            .unwrap()
135            .cloning_steps(copy, maybe_doc, clone_children, can_gc);
136        if clone_children == CloneChildrenFlag::DoNotCloneChildren {
137            // Step 1.
138            return;
139        }
140        let copy = copy.downcast::<HTMLTemplateElement>().unwrap();
141        // Steps 2-3.
142        let copy_contents = DomRoot::upcast::<Node>(copy.Content(CanGc::note()));
143        let copy_contents_doc = copy_contents.owner_doc();
144        for child in self.Content(CanGc::note()).upcast::<Node>().children() {
145            let copy_child = Node::clone(
146                &child,
147                Some(&copy_contents_doc),
148                CloneChildrenFlag::CloneChildren,
149                None,
150                CanGc::note(),
151            );
152            copy_contents.AppendChild(&copy_child, can_gc).unwrap();
153        }
154    }
155}