The headless browser for AI agents and web scraping
fix(dom): align node identity and id lookup semantics
SGavrl61ec5b32512f
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 1fb9767d..4a0aac14 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -2281,7 +2281,10 @@ class Node {}return n;}- contains(o) { return o ? _dom("contains", this._nid, o._nid) === "true" : false; }+ contains(o) {+ if (o === this) return true;+ return o ? _dom("contains", this._nid, o._nid) === "true" : false;+ }hasChildNodes() { return _dom("has_child_nodes", this._nid) === "true"; }cloneNode(deep) {const t = this.nodeType;@@ -2390,7 +2393,7 @@ class Node {}return true;}- isSameNode(other) { return other && this._nid === other._nid; }+ isSameNode(other) { return !!other && this._nid === other._nid; }addEventListener(type, callback, options) {_eventTargetAdd(this, type, callback, options);}@@ -5377,7 +5380,10 @@ class Document extends Node {}get hidden() { return false; }get visibilityState() { return "visible"; }- getElementById(id) { return _wrapEl(+_dom("get_element_by_id", id)); }+ getElementById(id) {+ const needle = String(id);+ return needle === "" ? null : _wrapEl(+_dom("get_element_by_id", needle));+ }querySelector(s) { return _wrapEl(+_dom("query_selector", s)); }querySelectorAll(s) {const ids = _domParse("query_selector_all", s) || [];diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex a7fd971f..6eba8c36 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -6631,6 +6631,33 @@ mod tests {);}+ #[test]+ fn node_identity_and_element_id_follow_dom_conversion_rules() {+ let mut rt = setup_runtime(+ r#"<html><body><div id=""></div><div id="null"></div><div id="undefined"></div></body></html>"#,+ );+ let result = rt+ .evaluate(+ r#"+ (function() {+ const node = document.body.firstChild;+ return [+ node.contains(node),+ node.isSameNode(null),+ document.getElementById("") === null,+ document.getElementById(null)?.id,+ document.getElementById(undefined)?.id+ ];+ })()+ "#,+ )+ .unwrap();+ assert_eq!(+ result,+ serde_json::json!([true, false, true, "null", "undefined"])+ );+ }+#[test]fn document_title_setter_creates_missing_title_element() {let mut rt = setup_runtime("<html><body><main>content</main></body></html>");