script/dom/
documentfragment.rs1use dom_struct::dom_struct;
6use js::rust::HandleObject;
7use rustc_hash::FxBuildHasher;
8use stylo_atoms::Atom;
9
10use super::bindings::trace::HashMapTracedValues;
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::DocumentFragmentBinding::DocumentFragmentMethods;
13use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
14use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
15use crate::dom::bindings::error::{ErrorResult, Fallible};
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::document::Document;
20use crate::dom::element::Element;
21use crate::dom::html::htmlcollection::HTMLCollection;
22use crate::dom::node::{Node, NodeTraits};
23use crate::dom::nodelist::NodeList;
24use crate::dom::virtualmethods::VirtualMethods;
25use crate::dom::window::Window;
26use crate::script_runtime::CanGc;
27
28#[dom_struct]
30pub(crate) struct DocumentFragment {
31 node: Node,
32 id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
34
35 host: MutNullableDom<Element>,
37}
38
39impl DocumentFragment {
40 pub(crate) fn new_inherited(document: &Document, host: Option<&Element>) -> DocumentFragment {
42 DocumentFragment {
43 node: Node::new_inherited(document),
44 id_map: DomRefCell::new(HashMapTracedValues::new_fx()),
45 host: MutNullableDom::new(host),
46 }
47 }
48
49 pub(crate) fn new(document: &Document, can_gc: CanGc) -> DomRoot<DocumentFragment> {
50 Self::new_with_proto(document, None, can_gc)
51 }
52
53 fn new_with_proto(
54 document: &Document,
55 proto: Option<HandleObject>,
56 can_gc: CanGc,
57 ) -> DomRoot<DocumentFragment> {
58 Node::reflect_node_with_proto(
59 Box::new(DocumentFragment::new_inherited(document, None)),
60 document,
61 proto,
62 can_gc,
63 )
64 }
65
66 pub(crate) fn id_map(
67 &self,
68 ) -> &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>> {
69 &self.id_map
70 }
71
72 pub(crate) fn host(&self) -> Option<DomRoot<Element>> {
73 self.host.get()
74 }
75
76 pub(crate) fn set_host(&self, host: &Element) {
77 self.host.set(Some(host));
78 }
79}
80
81pub(crate) trait LayoutDocumentFragmentHelpers<'dom> {
82 fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element>;
83}
84
85impl<'dom> LayoutDocumentFragmentHelpers<'dom> for LayoutDom<'dom, DocumentFragment> {
86 #[inline]
87 fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element> {
88 #[expect(unsafe_code)]
89 unsafe {
90 self.unsafe_get()
93 .host
94 .get_inner_as_layout()
95 .expect("Shadow roots's associated host is never null")
96 }
97 }
98}
99
100impl DocumentFragmentMethods<crate::DomTypeHolder> for DocumentFragment {
101 fn Constructor(
103 window: &Window,
104 proto: Option<HandleObject>,
105 can_gc: CanGc,
106 ) -> Fallible<DomRoot<DocumentFragment>> {
107 let document = window.Document();
108
109 Ok(DocumentFragment::new_with_proto(&document, proto, can_gc))
110 }
111
112 fn Children(&self, can_gc: CanGc) -> DomRoot<HTMLCollection> {
114 let window = self.owner_window();
115 HTMLCollection::children(&window, self.upcast(), can_gc)
116 }
117
118 fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> {
120 let id = Atom::from(id);
121 self.id_map
122 .borrow()
123 .get(&id)
124 .map(|elements| DomRoot::from_ref(&*elements[0]))
125 }
126
127 fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
129 self.upcast::<Node>().child_elements().next()
130 }
131
132 fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
134 self.upcast::<Node>()
135 .rev_children()
136 .find_map(DomRoot::downcast::<Element>)
137 }
138
139 fn ChildElementCount(&self) -> u32 {
141 self.upcast::<Node>().child_elements().count() as u32
142 }
143
144 fn Prepend(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
146 self.upcast::<Node>().prepend(nodes, can_gc)
147 }
148
149 fn Append(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
151 self.upcast::<Node>().append(nodes, can_gc)
152 }
153
154 fn ReplaceChildren(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
156 self.upcast::<Node>().replace_children(nodes, can_gc)
157 }
158
159 fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
161 self.upcast::<Node>().query_selector(selectors)
162 }
163
164 fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> {
166 self.upcast::<Node>().query_selector_all(selectors)
167 }
168}
169
170impl VirtualMethods for DocumentFragment {
171 fn super_type(&self) -> Option<&dyn VirtualMethods> {
172 Some(self.upcast::<Node>() as &dyn VirtualMethods)
173 }
174}