script/dom/html/
htmltemplateelement.rs1use 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 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 make_enumerated_getter!(
71 ShadowRootMode,
72 "shadowrootmode",
73 "open" | "closed",
74 missing => "",
75 invalid => ""
76 );
77
78 make_atomic_setter!(SetShadowRootMode, "shadowrootmode");
80
81 make_bool_getter!(ShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
83
84 make_bool_setter!(SetShadowRootDelegatesFocus, "shadowrootdelegatesfocus");
86
87 make_bool_getter!(ShadowRootClonable, "shadowrootclonable");
89
90 make_bool_setter!(SetShadowRootClonable, "shadowrootclonable");
92
93 make_bool_getter!(ShadowRootSerializable, "shadowrootserializable");
95
96 make_bool_setter!(SetShadowRootSerializable, "shadowrootserializable");
98
99 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 fn adopting_steps(&self, old_doc: &Document, can_gc: CanGc) {
116 self.super_type().unwrap().adopting_steps(old_doc, can_gc);
117 let doc = self
119 .owner_document()
120 .appropriate_template_contents_owner_document(CanGc::note());
121 Node::adopt(self.Content(CanGc::note()).upcast(), &doc, can_gc);
123 }
124
125 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 return;
139 }
140 let copy = copy.downcast::<HTMLTemplateElement>().unwrap();
141 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(©_contents_doc),
148 CloneChildrenFlag::CloneChildren,
149 None,
150 CanGc::note(),
151 );
152 copy_contents.AppendChild(©_child, can_gc).unwrap();
153 }
154 }
155}