use dom_struct::dom_struct;
use js::context::JSContext;
use js::rust::HandleObject;
use rustc_hash::FxBuildHasher;
use stylo_atoms::Atom;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DocumentFragmentBinding::DocumentFragmentMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::HashMapTracedValues;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::html::htmlcollection::HTMLCollection;
use crate::dom::node::{Node, NodeTraits};
use crate::dom::nodelist::NodeList;
use crate::dom::virtualmethods::VirtualMethods;
use crate::dom::window::Window;
#[dom_struct]
pub(crate) struct DocumentFragment {
node: Node,
id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
host: MutNullableDom<Element>,
}
impl DocumentFragment {
pub(crate) fn new_inherited(document: &Document, host: Option<&Element>) -> DocumentFragment {
DocumentFragment {
node: Node::new_inherited(document),
id_map: DomRefCell::new(HashMapTracedValues::new_fx()),
host: MutNullableDom::new(host),
}
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
document: &Document,
) -> DomRoot<DocumentFragment> {
Self::new_with_proto(cx, document, None)
}
fn new_with_proto(
cx: &mut js::context::JSContext,
document: &Document,
proto: Option<HandleObject>,
) -> DomRoot<DocumentFragment> {
Node::reflect_node_with_proto(
cx,
Box::new(DocumentFragment::new_inherited(document, None)),
document,
proto,
)
}
pub(crate) fn id_map(
&self,
) -> &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>> {
&self.id_map
}
pub(crate) fn host(&self) -> Option<DomRoot<Element>> {
self.host.get()
}
pub(crate) fn set_host(&self, host: &Element) {
self.host.set(Some(host));
}
}
impl<'dom> LayoutDom<'dom, DocumentFragment> {
#[inline]
pub(crate) fn shadowroot_host_for_layout(self) -> LayoutDom<'dom, Element> {
#[expect(unsafe_code)]
unsafe {
self.unsafe_get()
.host
.get_inner_as_layout()
.expect("Shadow roots's associated host is never null")
}
}
}
impl DocumentFragmentMethods<crate::DomTypeHolder> for DocumentFragment {
fn Constructor(
cx: &mut js::context::JSContext,
window: &Window,
proto: Option<HandleObject>,
) -> Fallible<DomRoot<DocumentFragment>> {
let document = window.Document();
Ok(DocumentFragment::new_with_proto(cx, &document, proto))
}
fn Children(&self, cx: &mut js::context::JSContext) -> DomRoot<HTMLCollection> {
let window = self.owner_window();
HTMLCollection::children(cx, &window, self.upcast())
}
fn GetElementById(&self, id: DOMString) -> Option<DomRoot<Element>> {
let id = Atom::from(id);
self.id_map
.borrow()
.get(&id)
.map(|elements| DomRoot::from_ref(&*elements[0]))
}
fn GetFirstElementChild(&self) -> Option<DomRoot<Element>> {
self.upcast::<Node>().child_elements().next()
}
fn GetLastElementChild(&self) -> Option<DomRoot<Element>> {
self.upcast::<Node>()
.rev_children()
.find_map(DomRoot::downcast::<Element>)
}
fn ChildElementCount(&self) -> u32 {
self.upcast::<Node>().child_elements().count() as u32
}
fn Prepend(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().prepend(cx, nodes)
}
fn Append(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().append(cx, nodes)
}
fn ReplaceChildren(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
self.upcast::<Node>().replace_children(cx, nodes)
}
fn MoveBefore(&self, cx: &mut JSContext, node: &Node, child: Option<&Node>) -> ErrorResult {
self.upcast::<Node>().move_before(cx, node, child)
}
fn QuerySelector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
self.upcast::<Node>().query_selector(selectors)
}
fn QuerySelectorAll(&self, selectors: DOMString) -> Fallible<DomRoot<NodeList>> {
self.upcast::<Node>().query_selector_all(selectors)
}
}
impl VirtualMethods for DocumentFragment {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<Node>() as &dyn VirtualMethods)
}
}