Skip to content

The headless browser for AI agents and web scraping

fix(obscura-js): remove_attribute must update the id_index (#1013)

Andrey Mnatsakanov6aef52d37c19
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index ff374f8e..8b6ed147 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -1905,11 +1905,24 @@ fn op_dom_inner(shared: SharedState, cmd: String, arg1: String, arg2: String) ->
}
"remove_attribute" => {
let nid = arg1.parse::<u32>().unwrap_or(0);
- dom.with_node_mut(NodeId::new(nid), |n| {
+ let node_id = NodeId::new(nid);
+ // Removing `id` must clear the id_index too, matching set_attribute /
+ // remove_attribute_ns; otherwise getElementById keeps returning the
+ // still-attached element (#1013).
+ let old_id = if arg2 == "id" {
+ dom.with_node(node_id, |n| n.get_attribute("id").map(str::to_owned))
+ .flatten()
+ } else {
+ None
+ };
+ dom.with_node_mut(node_id, |n| {
if let NodeData::Element { attrs, .. } = &mut n.data {
attrs.retain(|a| !a.qualified_name_eq(&arg2));
}
});
+ if arg2 == "id" {
+ dom.update_id_index(node_id, old_id.as_deref(), None);
+ }
"true".into()
}
// Namespace-aware attribute ops. arg2 packs the pieces with a NUL:
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index d9d7f2b9..69184469 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -4219,6 +4219,25 @@ mod tests {
rt
}
+ // #1013: removeAttribute('id') must update the id_index so getElementById no
+ // longer returns the (still-attached) element.
+ #[test]
+ fn get_element_by_id_reflects_remove_attribute() {
+ let mut rt = setup_runtime("<html><body><div id='test'>x</div></body></html>");
+ assert_eq!(
+ rt.evaluate("document.getElementById('test') !== null").unwrap(),
+ serde_json::json!(true),
+ "the element should be found before its id is removed"
+ );
+ rt.evaluate("document.getElementById('test').removeAttribute('id')")
+ .unwrap();
+ assert_eq!(
+ rt.evaluate("document.getElementById('test') === null").unwrap(),
+ serde_json::json!(true),
+ "getElementById must not return an element whose id was removed"
+ );
+ }
+
#[cfg(feature = "render")]
#[test]
fn page_transport_keeps_render_resources_cache_only_across_document_resets() {