script/dom/
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 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// https://dom.spec.whatwg.org/#documentfragment
30#[dom_struct]
31pub(crate) struct DocumentFragment {
32    node: Node,
33    /// Caches for the getElement methods
34    id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
35
36    /// <https://dom.spec.whatwg.org/#concept-documentfragment-host>
37    host: MutNullableDom<Element>,
38}
39
40impl DocumentFragment {
41    /// Creates a new DocumentFragment.
42    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            // https://dom.spec.whatwg.org/#shadowroot
92            // > Shadow roots’s associated host is never null.
93            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    /// <https://dom.spec.whatwg.org/#dom-documentfragment-documentfragment>
103    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    /// <https://dom.spec.whatwg.org/#dom-parentnode-children>
114    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    /// <https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid>
120    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    /// <https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild>
129    fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
130        self.upcast::<Node>().child_elements().next()
131    }
132
133    /// <https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild>
134    fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
135        self.upcast::<Node>()
136            .rev_children()
137            .find_map(DomRoot::downcast::<Element>)
138    }
139
140    /// <https://dom.spec.whatwg.org/#dom-parentnode-childelementcount>
141    fn ChildElementCount(&self) -> u32 {
142        self.upcast::<Node>().child_elements().count() as u32
143    }
144
145    /// <https://dom.spec.whatwg.org/#dom-parentnode-prepend>
146    fn Prepend(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
147        self.upcast::<Node>().prepend(cx, nodes)
148    }
149
150    /// <https://dom.spec.whatwg.org/#dom-parentnode-append>
151    fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
152        self.upcast::<Node>().append(cx, nodes)
153    }
154
155    /// <https://dom.spec.whatwg.org/#dom-parentnode-replacechildren>
156    fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
157        self.upcast::<Node>().replace_children(cx, nodes)
158    }
159
160    /// <https://dom.spec.whatwg.org/#dom-parentnode-movebefore>
161    fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
162        self.upcast::<Node>().move_before(cx, node, child)
163    }
164
165    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselector>
166    fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
167        self.upcast::<Node>().query_selector(selectors)
168    }
169
170    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall>
171    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}