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