The headless browser for AI agents and web scraping
fix(js): honor mutation observer attribute filters
SGavrla5f4dc9655d5
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 674a7182..0fb5a73c 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -9621,7 +9621,7 @@ globalThis.MutationObserver = class MutationObserver {}observe(target, options) {this._targets.push({ target, options: options || {} });- globalThis.__mutationObservers.push(this);+ if (!globalThis.__mutationObservers.includes(this)) globalThis.__mutationObservers.push(this);}disconnect() {this._targets = [];@@ -9684,6 +9684,8 @@ globalThis.__notifyMutation = function(type, target_nid, addedNodes, removedNode(type === 'characterData' && t.options.characterData) ||(type === 'childList' && t.options.childList);if (!wantsType) continue;+ if (type === 'attributes' && t.options.attributeFilter !== undefined+ && !t.options.attributeFilter.includes(attributeName)) continue;if (root._nid === target_nid) { matched = true; break; }if (t.options.subtree) {// Walk parents until we hit the observed root or run off the tree.diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 5dfc209f..a87a82b0 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -21757,6 +21757,56 @@ mod tests {assert_eq!(patched["taken"], 1);}+ #[test]+ fn mutation_observer_attribute_filter_is_per_target_and_preserves_child_lists() {+ let mut rt = setup_runtime("<html><body><div id='root'><span id='child'></span></div></body></html>");+ let result = rt.evaluate(r#"(() => {+ const root = document.getElementById('root');+ const child = document.getElementById('child');+ const filtered = new MutationObserver(() => {});+ filtered.observe(root, {attributes: true, attributeFilter: ['data-state'], subtree: true, childList: true});+ filtered.observe(child, {attributes: true, attributeFilter: ['title']});+ const empty = new MutationObserver(() => {});+ empty.observe(root, {attributes: true, attributeFilter: [], subtree: true, childList: true});+ const all = new MutationObserver(() => {});+ all.observe(root, {attributes: true, subtree: true});+ root.style.setProperty('--indicator', '1px');+ root.setAttribute('data-state', 'active');+ child.setAttribute('data-state', 'active');+ child.setAttribute('title', 'selected');+ child.setAttribute('class', 'selected');+ root.appendChild(document.createElement('i'));+ const names = observer => observer.takeRecords().map(record => record.attributeName || record.type);+ const result = {filtered: names(filtered), empty: names(empty), all: names(all)};+ for (const observer of [filtered, empty, all]) observer.disconnect();+ return result;+ })()"#).unwrap();+ assert_eq!(result, serde_json::json!({+ "filtered": ["data-state", "data-state", "title", "childList"],+ "empty": ["childList"],+ "all": ["style", "data-state", "data-state", "title", "class"]+ }));+ }++ #[tokio::test(flavor = "current_thread")]+ async fn mutation_observer_attribute_filter_does_not_feed_back_style_writes() {+ let mut rt = setup_runtime("<html><body><div id='tabs'><span id='tab'></span></div></body></html>");+ rt.evaluate(r#"(() => {+ const tabs = document.getElementById('tabs');+ globalThis.indicatorUpdates = 0;+ const observer = new MutationObserver(() => {+ indicatorUpdates++;+ // Bound the broken implementation so the regression cannot hang V8.+ if (indicatorUpdates === 4) observer.disconnect();+ tabs.style.setProperty('--indicator', '1px');+ });+ observer.observe(tabs, {attributes: true, attributeFilter: ['data-state'], subtree: true});+ document.getElementById('tab').setAttribute('data-state', 'active');+ })()"#).unwrap();+ rt.run_event_loop().await.unwrap();+ assert_eq!(rt.evaluate("indicatorUpdates").unwrap().as_f64(), Some(1.0));+ }+// Writing goes through the same insertion steps as any other insertion.#[test]fn document_write_reports_to_mutation_observers() {