The headless browser for AI agents and web scraping
fix(js): update attributes before custom element reactions
SGavrl8ba9e5e5ac59
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex b48f0360..e5719097 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -4488,13 +4488,13 @@ class Element extends Node {_dom("set_attribute_ns", this._nid, ns + "\0" + n + "\0" + value);if (detailsWasOpen !== null) _queueDetailsToggle(this, detailsWasOpen);if (_hasMutationObservers()) _queueAttributeMutationRecord(this, localName, ns || null, observedOldValue);- if (customOldValue !== undefined) _customElementCallback(this, 'attributeChangedCallback', [localName, customOldValue, value, ns || null]);// Namespace-aware writes can replace an attribute by namespace/local name// while changing its qualified name. Fall back to native reads afterwards// instead of maintaining a second, subtly different key space here.this._nullNamespaceAttrs = null;if (ns === '' && n === 'srcdoc' && this.localName === 'iframe') this._loadIframeSrcdoc(true);if (ns === "" && n === "style") _elementStyleGet(this)?._replaceFromAttribute(value);+ if (customOldValue !== undefined) _customElementCallback(this, 'attributeChangedCallback', [localName, customOldValue, value, ns || null]);}removeAttribute(n) {n = _htmlAttrName(this, n);@@ -4511,7 +4511,6 @@ class Element extends Node {_dom("remove_attribute", this._nid, n);if (detailsWasOpen !== null) _queueDetailsToggle(this, detailsWasOpen);if (observedOldValue !== null) _queueAttributeMutationRecord(this, n, null, observedOldValue);- if (customOldValue !== null) _customElementCallback(this, 'attributeChangedCallback', [n, customOldValue, null, null]);if (this._nullNamespaceAttrs instanceof Map) {this._nullNamespaceAttrs.delete(n);}@@ -4537,6 +4536,7 @@ class Element extends Node {image._imageSourceChanged();}}+ if (customOldValue !== null) _customElementCallback(this, 'attributeChangedCallback', [n, customOldValue, null, null]);}removeAttributeNS(ns, n) {ns = String(ns == null ? "" : ns);@@ -4550,10 +4550,10 @@ class Element extends Node {_dom("remove_attribute_ns", this._nid, ns + "\0" + n);if (detailsWasOpen !== null) _queueDetailsToggle(this, detailsWasOpen);if (observedOldValue !== null) _queueAttributeMutationRecord(this, n, ns || null, observedOldValue);- if (customOldValue !== null) _customElementCallback(this, 'attributeChangedCallback', [n, customOldValue, null, ns || null]);this._nullNamespaceAttrs = null;if (ns === '' && n === 'srcdoc' && this.localName === 'iframe') this._loadIframeSrc(this.getAttribute('src') || 'about:blank');if (ns === "" && n === "style") _elementStyleGet(this)?._replaceFromAttribute("");+ if (customOldValue !== null) _customElementCallback(this, 'attributeChangedCallback', [n, customOldValue, null, ns || null]);}hasAttribute(n) { return this.getAttribute(n) !== null; }hasAttributes() { return this.attributes.length > 0; }diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 198a0944..b470bf1a 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -9031,6 +9031,66 @@ mod tests {assert_eq!(result, serde_json::json!([1, 0, true]));}+ #[test]+ fn custom_element_attribute_reactions_observe_removal_and_can_restore_the_attribute() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt.evaluate(r#"+ const log = [];+ class RestoreEl extends HTMLElement {+ static get observedAttributes() { return ['mode']; }+ attributeChangedCallback(name, oldValue, newValue) {+ log.push([oldValue, newValue, this.getAttribute(name)]);+ if (newValue === null) this.setAttribute(name, 'restored');+ }+ }+ customElements.define('restore-el', RestoreEl);+ const results = [];+ for (const remove of [+ el => el.removeAttribute('mode'),+ el => el.toggleAttribute('mode', false),+ el => el.removeAttributeNS(null, 'mode'),+ ]) {+ const el = document.createElement('restore-el');+ el.setAttribute('mode', 'before');+ log.length = 0;+ remove(el);+ results.push([log.slice(), el.getAttribute('mode'), el.getAttributeNS(null, 'mode')]);+ }+ return results;+ "#).unwrap();+ let expected = serde_json::json!([+ [["before", null, null], [null, "restored", "restored"]], "restored", "restored"+ ]);+ assert_eq!(result, serde_json::json!([expected, expected, expected]));+ }++ #[test]+ fn custom_element_namespace_reactions_observe_updated_attributes_and_style() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt.evaluate(r#"+ const log = [];+ class StyledEl extends HTMLElement {+ static get observedAttributes() { return ['style']; }+ attributeChangedCallback(name, oldValue, newValue) {+ log.push([newValue, this.getAttribute(name), this.style.color]);+ if (newValue === null) this.setAttribute('style', 'color: red');+ }+ }+ customElements.define('styled-el', StyledEl);+ const el = document.createElement('styled-el');+ el.setAttribute('style', 'color: blue');+ log.length = 0;+ el.setAttributeNS(null, 'style', 'color: green');+ el.removeAttributeNS(null, 'style');+ return [log, el.getAttribute('style'), el.style.color];+ "#).unwrap();+ assert_eq!(result, serde_json::json!([+ [["color: green", "color: green", "green"], [null, null, ""],+ ["color: red", "color: red", "red"]],+ "color: red", "red"+ ]));+ }+#[test]fn custom_elements_connect_and_upgrade_on_insertion() {let mut rt = setup_runtime("<html><body></body></html>");