script/dom/document/
documentfragment.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use rustc_hash::FxBuildHasher;
9use stylo_atoms::Atom;
10
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::bindings::trace::HashMapTracedValues;
20use crate::dom::document::Document;
21use crate::dom::element::Element;
22use crate::dom::html::htmlcollection::HTMLCollection;
23use crate::dom::node::{Node, NodeTraits};
24use crate::dom::nodelist::NodeList;
25use crate::dom::virtualmethods::VirtualMethods;
26use crate::dom::window::Window;
27use crate::script_runtime::CanGc;
28
29#[dom_struct]
31pub(crate) struct DocumentFragment {
32 node: Node,
33 id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
35
36 host: MutNullableDom<Element>,
38}
39
40impl DocumentFragment {
41 pub(crate) fn new_inherited(document: &Document, host: Option<&Element>) -> DocumentFragment {
43 DocumentFragment {
44 node: Node::new_inherited(document),
45 id_map: DomRefCell::new(HashMapTracedValues::new_fx()),
46 host: MutNullableDom::new(host),
47 }
48 }
49
50 pub(crate) fn new(
51 cx: &mut js::context::JSContext,
52 document: &Document,
53 ) -> DomRoot<DocumentFragment> {
54 Self::new_with_proto(cx, document, None)
55 }
56
57 fn new_with_proto(
58 cx: &mut js::context::JSContext,
59 document: &Document,
60 proto: Option<HandleObject>,
61 ) -> DomRoot<DocumentFragment> {
62 Node::reflect_node_with_proto(
63 cx,
64 Box::new(DocumentFragment::new_inherited(document, None)),
65 document,
66 proto,
67 )
68 }
69
70 pub(crate) fn id_map(
71 &self,
72 ) -> &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>> {
73 &self.id_map
74 }
75
76 pub(crate) fn host(&self) -> Option<DomRoot<Element>> {
77 self.host.get()
78 }
79
80 pub(crate) fn set_host(&self, host: &Element) {
81 self.host.set(Some(host));
82 }
83}
84
85impl<'dom> LayoutDom<'dom, DocumentFragment> {
86 #[inline]
87 pub(crate) 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 cx: &mut js::context::JSContext,
104 window: &Window,
105 proto: Option<HandleObject>,
106 ) -> Fallible<DomRoot<DocumentFragment>> {
107 let document = window.Document();
108
109 Ok(DocumentFragment::new_with_proto(cx, &document, proto))
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, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
146 self.upcast::<Node>().prepend(cx, nodes)
147 }
148
149 fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
151 self.upcast::<Node>().append(cx, nodes)
152 }
153
154 fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
156 self.upcast::<Node>().replace_children(cx, nodes)
157 }
158
159 fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
161 self.upcast::<Node>().move_before(cx, node, child)
162 }
163
164 fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
166 self.upcast::<Node>().query_selector(selectors)
167 }
168
169 fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> {
171 self.upcast::<Node>().query_selector_all(selectors)
172 }
173}
174
175impl VirtualMethods for DocumentFragment {
176 fn super_type(&self) -> Option<&dyn VirtualMethods> {
177 Some(self.upcast::<Node>() as &dyn VirtualMethods)
178 }
179}