script/dom/document/
documentfragment.rs1use dom_struct::dom_struct;
6use js::context::{JSContext, NoGC};
7use js::rust::HandleObject;
8use script_bindings::dom::UnrootedDom;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::DocumentFragmentBinding::DocumentFragmentMethods;
12use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
13use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
14use crate::dom::bindings::error::{ErrorResult, Fallible};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom, ToLayoutOptional};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::document::Document;
19use crate::dom::document::tree_ordered_index_map::TreeOrderedIndexMap;
20use crate::dom::element::Element;
21use crate::dom::html::htmlcollection::HTMLCollection;
22use crate::dom::node::virtualmethods::VirtualMethods;
23use crate::dom::node::{Node, NodeTraits};
24use crate::dom::nodelist::NodeList;
25use crate::dom::window::Window;
26
27#[dom_struct]
29pub(crate) struct DocumentFragment {
30 node: Node,
32 id_map: TreeOrderedIndexMap,
34 host: MutNullableDom<Element>,
36}
37
38impl DocumentFragment {
39 pub(crate) fn new_inherited(document: &Document, host: Option<&Element>) -> DocumentFragment {
41 DocumentFragment {
42 node: Node::new_inherited(document),
43 id_map: TreeOrderedIndexMap::id(),
44 host: MutNullableDom::new(host),
45 }
46 }
47
48 pub(crate) fn new(
49 cx: &mut js::context::JSContext,
50 document: &Document,
51 ) -> DomRoot<DocumentFragment> {
52 Self::new_with_proto(cx, document, None)
53 }
54
55 fn new_with_proto(
56 cx: &mut js::context::JSContext,
57 document: &Document,
58 proto: Option<HandleObject>,
59 ) -> DomRoot<DocumentFragment> {
60 Node::reflect_node_with_proto(
61 cx,
62 Box::new(DocumentFragment::new_inherited(document, None)),
63 document,
64 proto,
65 )
66 }
67
68 pub(crate) fn id_map(&self) -> &TreeOrderedIndexMap {
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 host_unrooted<'a>(&self, no_gc: &'a NoGC) -> Option<UnrootedDom<'a, Element>> {
77 self.host.get_unrooted(no_gc)
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 .to_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, cx: &mut js::context::JSContext) -> DomRoot<HTMLCollection> {
114 let window = self.owner_window();
115 HTMLCollection::children(cx, &window, self.upcast())
116 }
117
118 fn GetElementById(&self, cx: &JSContext, id: DOMString) -> Option<DomRoot<Element>> {
120 self.id_map.get(cx, self.upcast(), &Atom::from(id))
121 }
122
123 fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
125 self.upcast::<Node>().child_elements().next()
126 }
127
128 fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
130 self.upcast::<Node>()
131 .rev_children()
132 .find_map(DomRoot::downcast::<Element>)
133 }
134
135 fn ChildElementCount(&self, no_gc: &NoGC) -> u32 {
137 self.upcast::<Node>().child_elements_unrooted(no_gc).count() as u32
138 }
139
140 fn Prepend(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
142 self.upcast::<Node>().prepend(cx, nodes)
143 }
144
145 fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
147 self.upcast::<Node>().append(cx, nodes)
148 }
149
150 fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
152 self.upcast::<Node>().replace_children(cx, nodes)
153 }
154
155 fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
157 self.upcast::<Node>().move_before(cx, node, child)
158 }
159
160 fn QuerySelector(
162 &self,
163 cx: &mut JSContext,
164 selectors: DOMString,
165 ) -> Fallible<Option<DomRoot<Element>>> {
166 self.upcast::<Node>().query_selector(cx.no_gc(), selectors)
167 }
168
169 fn QuerySelectorAll(
171 &self,
172 cx: &mut JSContext,
173 selectors: DOMString,
174 ) -> Fallible<DomRoot<NodeList>> {
175 self.upcast::<Node>().query_selector_all(cx, selectors)
176 }
177}
178
179impl VirtualMethods for DocumentFragment {
180 fn super_type(&self) -> Option<&dyn VirtualMethods> {
181 Some(self.upcast::<Node>() as &dyn VirtualMethods)
182 }
183}