Skip to main content

script/dom/document/
documentfragment.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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// https://dom.spec.whatwg.org/#documentfragment
29#[dom_struct]
30pub(crate) struct DocumentFragment {
31    node: Node,
32    /// Caches for the getElement methods
33    id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
34
35    /// <https://dom.spec.whatwg.org/#concept-documentfragment-host>
36    host: MutNullableDom<Element>,
37}
38
39impl DocumentFragment {
40    /// Creates a new DocumentFragment.
41    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            // https://dom.spec.whatwg.org/#shadowroot
90            // > Shadow roots’s associated host is never null.
91            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    /// <https://dom.spec.whatwg.org/#dom-documentfragment-documentfragment>
101    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    /// <https://dom.spec.whatwg.org/#dom-parentnode-children>
112    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    /// <https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid>
118    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    /// <https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild>
127    fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
128        self.upcast::<Node>().child_elements().next()
129    }
130
131    /// <https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild>
132    fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
133        self.upcast::<Node>()
134            .rev_children()
135            .find_map(DomRoot::downcast::<Element>)
136    }
137
138    /// <https://dom.spec.whatwg.org/#dom-parentnode-childelementcount>
139    fn ChildElementCount(&self) -> u32 {
140        self.upcast::<Node>().child_elements().count() as u32
141    }
142
143    /// <https://dom.spec.whatwg.org/#dom-parentnode-prepend>
144    fn Prepend(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
145        self.upcast::<Node>().prepend(cx, nodes)
146    }
147
148    /// <https://dom.spec.whatwg.org/#dom-parentnode-append>
149    fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
150        self.upcast::<Node>().append(cx, nodes)
151    }
152
153    /// <https://dom.spec.whatwg.org/#dom-parentnode-replacechildren>
154    fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
155        self.upcast::<Node>().replace_children(cx, nodes)
156    }
157
158    /// <https://dom.spec.whatwg.org/#dom-parentnode-movebefore>
159    fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
160        self.upcast::<Node>().move_before(cx, node, child)
161    }
162
163    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselector>
164    fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
165        self.upcast::<Node>().query_selector(selectors)
166    }
167
168    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall>
169    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}