The headless browser for AI agents and web scraping
fix(js): preserve constructed DocumentFragment wrapper identity
Juan Pablo Djeredjian790a77d3265d
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 9c102de3..c7d093a5 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -7297,7 +7297,13 @@ class DocumentFragment extends Node {constructor(nid) {const created = nid === undefined;super(created ? +_dom("create_document_fragment") : nid);- if (created) _seedDetachedTreeState(this);+ if (created) {+ _seedDetachedTreeState(this);+ // Register the wrapper like createDocumentFragment() does. The native+ // node reports node_type 9, so an unregistered fragment is re-wrapped+ // as a Document the first time a child's parentNode misses the cache.+ _cache.set(this._nid, this);+ }}get nodeType() { return 11; }get nodeName() { return "#document-fragment"; }diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex d7ca8d7f..d92a8446 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -9387,6 +9387,39 @@ mod tests {);}+ #[test]+ fn constructed_document_fragment_keeps_its_identity_as_parent() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r#"+ const check = (fragment) => {+ const child = document.createElement("div");+ fragment.appendChild(child);+ const scratch = document.createElement("span");+ document.body.appendChild(scratch);+ scratch.remove();+ const parent = child.parentNode;+ let removed;+ try { fragment.removeChild(child); removed = "ok"; } catch (error) { removed = error.name; }+ return [parent === fragment, parent && parent.nodeType, removed, child.parentNode];+ };+ return {+ constructed: check(new DocumentFragment()),+ created: check(document.createDocumentFragment())+ };+ "#,+ )+ .unwrap();+ assert_eq!(+ result,+ serde_json::json!({+ "constructed": [true, 11, "ok", null],+ "created": [true, 11, "ok", null]+ })+ );+ }+#[test]fn parent_node_insertion_of_several_nodes_is_one_mutation() {let mut rt = setup_runtime("<html><body><div id='p'><i id='first'></i></div><div id='q'><u></u></div></body></html>");