use super::*;
pub(crate) fn cached_document() -> Option<Document> {
DOCUMENT_CACHE.with(|cell: &UnsafeCell<Option<Document>>| {
let cached_ptr: *mut Option<Document> = cell.get();
unsafe {
if let Some(doc) = &*cached_ptr {
return Some(doc.clone());
}
}
let window_value: Window = window()?;
let document: Document = window_value.document()?;
DOCUMENT_CACHE.with(|cell: &UnsafeCell<Option<Document>>| unsafe {
*cell.get() = Some(document.clone());
});
Some(document)
})
}
pub(crate) fn append_nodes(parent: &Element, nodes: impl IntoIterator<Item = Node>) {
let mut iter = nodes.into_iter();
let Some(first) = iter.next() else {
return;
};
let Some(second) = iter.next() else {
let _: Result<Node, JsValue> = parent.append_child(&first);
return;
};
let document: Document = match cached_document() {
Some(doc) => doc,
None => {
let _: Result<Node, JsValue> = parent.append_child(&first);
let _: Result<Node, JsValue> = parent.append_child(&second);
for node in iter {
let _: Result<Node, JsValue> = parent.append_child(&node);
}
return;
}
};
let fragment: DocumentFragment = document.create_document_fragment();
let _: Result<Node, JsValue> = fragment.append_child(&first);
let _: Result<Node, JsValue> = fragment.append_child(&second);
for node in iter {
let _: Result<Node, JsValue> = fragment.append_child(&node);
}
let fragment_node: Node = fragment.into();
let _: Result<Node, JsValue> = parent.append_child(&fragment_node);
}