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::rust::HandleObject;
7use rustc_hash::FxBuildHasher;
8use stylo_atoms::Atom;
9
10use super::bindings::trace::HashMapTracedValues;
11use crate::dom::bindings::cell::DomRefCell;
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::document::Document;
20use crate::dom::element::Element;
21use crate::dom::html::htmlcollection::HTMLCollection;
22use crate::dom::node::{Node, NodeTraits};
23use crate::dom::nodelist::NodeList;
24use crate::dom::virtualmethods::VirtualMethods;
25use crate::dom::window::Window;
26use crate::script_runtime::CanGc;
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(document: &Document, can_gc: CanGc) -> DomRoot<DocumentFragment> {
50        Self::new_with_proto(document, None, can_gc)
51    }
52
53    fn new_with_proto(
54        document: &Document,
55        proto: Option<HandleObject>,
56        can_gc: CanGc,
57    ) -> DomRoot<DocumentFragment> {
58        Node::reflect_node_with_proto(
59            Box::new(DocumentFragment::new_inherited(document, None)),
60            document,
61            proto,
62            can_gc,
63        )
64    }
65
66    pub(crate) fn id_map(
67        &self,
68    ) -> &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>> {
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 set_host(&self, host: &Element) {
77        self.host.set(Some(host));
78    }
79}
80
81pub(crate) trait LayoutDocumentFragmentHelpers<'dom> {
82    fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element>;
83}
84
85impl<'dom> LayoutDocumentFragmentHelpers<'dom> for LayoutDom<'dom, DocumentFragment> {
86    #[inline]
87    fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element> {
88        #[expect(unsafe_code)]
89        unsafe {
90            // https://dom.spec.whatwg.org/#shadowroot
91            // > Shadow roots’s associated host is never null.
92            self.unsafe_get()
93                .host
94                .get_inner_as_layout()
95                .expect("Shadow roots's associated host is never null")
96        }
97    }
98}
99
100impl DocumentFragmentMethods<crate::DomTypeHolder> for DocumentFragment {
101    /// <https://dom.spec.whatwg.org/#dom-documentfragment-documentfragment>
102    fn Constructor(
103        window: &Window,
104        proto: Option<HandleObject>,
105        can_gc: CanGc,
106    ) -> Fallible<DomRoot<DocumentFragment>> {
107        let document = window.Document();
108
109        Ok(DocumentFragment::new_with_proto(&document, proto, can_gc))
110    }
111
112    /// <https://dom.spec.whatwg.org/#dom-parentnode-children>
113    fn Children(&self, can_gc: CanGc) -> DomRoot<HTMLCollection> {
114        let window = self.owner_window();
115        HTMLCollection::children(&window, self.upcast(), can_gc)
116    }
117
118    /// <https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid>
119    fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> {
120        let id = Atom::from(id);
121        self.id_map
122            .borrow()
123            .get(&id)
124            .map(|elements| DomRoot::from_ref(&*elements[0]))
125    }
126
127    /// <https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild>
128    fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
129        self.upcast::<Node>().child_elements().next()
130    }
131
132    /// <https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild>
133    fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
134        self.upcast::<Node>()
135            .rev_children()
136            .find_map(DomRoot::downcast::<Element>)
137    }
138
139    /// <https://dom.spec.whatwg.org/#dom-parentnode-childelementcount>
140    fn ChildElementCount(&self) -> u32 {
141        self.upcast::<Node>().child_elements().count() as u32
142    }
143
144    /// <https://dom.spec.whatwg.org/#dom-parentnode-prepend>
145    fn Prepend(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
146        self.upcast::<Node>().prepend(nodes, can_gc)
147    }
148
149    /// <https://dom.spec.whatwg.org/#dom-parentnode-append>
150    fn Append(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
151        self.upcast::<Node>().append(nodes, can_gc)
152    }
153
154    /// <https://dom.spec.whatwg.org/#dom-parentnode-replacechildren>
155    fn ReplaceChildren(&self, nodes: Vec<NodeOrString>, can_gc: CanGc) -> ErrorResult {
156        self.upcast::<Node>().replace_children(nodes, can_gc)
157    }
158
159    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselector>
160    fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
161        self.upcast::<Node>().query_selector(selectors)
162    }
163
164    /// <https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall>
165    fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> {
166        self.upcast::<Node>().query_selector_all(selectors)
167    }
168}
169
170impl VirtualMethods for DocumentFragment {
171    fn super_type(&self) -> Option<&dyn VirtualMethods> {
172        Some(self.upcast::<Node>() as &dyn VirtualMethods)
173    }
174}