script/dom/document/
documentfragment.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use rustc_hash::FxBuildHasher;
9use script_bindings::cell::DomRefCell;
10use stylo_atoms::Atom;
11
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;
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(
50 cx: &mut js::context::JSContext,
51 document: &Document,
52 ) -> DomRoot<DocumentFragment> {
53 Self::new_with_proto(cx, document, None)
54 }
55
56 fn new_with_proto(
57 cx: &mut js::context::JSContext,
58 document: &Document,
59 proto: Option<HandleObject>,
60 ) -> DomRoot<DocumentFragment> {
61 Node::reflect_node_with_proto(
62 cx,
63 Box::new(DocumentFragment::new_inherited(document, None)),
64 document,
65 proto,
66 )
67 }
68
69 pub(crate) fn id_map(
70 &self,
71 ) -> &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>> {
72 &self.id_map
73 }
74
75 pub(crate) fn host(&self) -> Option<DomRoot<Element>> {
76 self.host.get()
77 }
78
79 pub(crate) fn set_host(&self, host: &Element) {
80 self.host.set(Some(host));
81 }
82}
83
84impl<'dom> LayoutDom<'dom, DocumentFragment> {
85 #[inline]
86 pub(crate) fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element> {
87 #[expect(unsafe_code)]
88 unsafe {
89 self.unsafe_get()
92 .host
93 .get_inner_as_layout()
94 .expect("Shadow roots's associated host is never null")
95 }
96 }
97}
98
99impl DocumentFragmentMethods<crate::DomTypeHolder> for DocumentFragment {
100 fn Constructor(
102 cx: &mut js::context::JSContext,
103 window: &Window,
104 proto: Option<HandleObject>,
105 ) -> Fallible<DomRoot<DocumentFragment>> {
106 let document = window.Document();
107
108 Ok(DocumentFragment::new_with_proto(cx, &document, proto))
109 }
110
111 fn Children(&self, cx: &mut js::context::JSContext) -> DomRoot<HTMLCollection> {
113 let window = self.owner_window();
114 HTMLCollection::children(cx, &window, self.upcast())
115 }
116
117 fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> {
119 let id = Atom::from(id);
120 self.id_map
121 .borrow()
122 .get(&id)
123 .map(|elements| DomRoot::from_ref(&*elements[0]))
124 }
125
126 fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
128 self.upcast::<Node>().child_elements().next()
129 }
130
131 fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
133 self.upcast::<Node>()
134 .rev_children()
135 .find_map(DomRoot::downcast::<Element>)
136 }
137
138 fn ChildElementCount(&self) -> u32 {
140 self.upcast::<Node>().child_elements().count() as u32
141 }
142
143 fn Prepend(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
145 self.upcast::<Node>().prepend(cx, nodes)
146 }
147
148 fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
150 self.upcast::<Node>().append(cx, nodes)
151 }
152
153 fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
155 self.upcast::<Node>().replace_children(cx, nodes)
156 }
157
158 fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
160 self.upcast::<Node>().move_before(cx, node, child)
161 }
162
163 fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
165 self.upcast::<Node>().query_selector(selectors)
166 }
167
168 fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> {
170 self.upcast::<Node>().query_selector_all(selectors)
171 }
172}
173
174impl VirtualMethods for DocumentFragment {
175 fn super_type(&self) -> Option<&dyn VirtualMethods> {
176 Some(self.upcast::<Node>() as &dyn VirtualMethods)
177 }
178}