Skip to content

The headless browser for AI agents and web scraping

fix(js): run custom element reactions

Kevsosmoothcfce0627554f
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 6cc50bee..5094d4f6 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -2328,6 +2328,7 @@ function _runPostConnectionSteps(nodes) {
__prepareInsertedSubtree(n);
if (n instanceof Element && n.tagName === 'LINK') _loadLinkedStylesheet(n);
}
+ for (const n of nodes) _connectCustomElements(n, true);
} finally {
_postConnection = outer;
}
@@ -2344,6 +2345,75 @@ function _iframeConnectionPending(iframe) {
return false;
}
+// Custom element reactions for connection changes. Only defined names are
+// queried, so a page without definitions pays one Map size check per
+// insertion or removal. The reactions run once the DOM operation is done.
+let _customElementSelector = '';
+let _customElementSelectorSize = 0;
+function _definedCustomElementsIn(root, inclusive) {
+ const registry = globalThis.customElements?._registry;
+ if (!registry || registry.size === 0) return null;
+ const type = root.nodeType;
+ if (type !== 1 && type !== 11) return null;
+ if (_customElementSelectorSize !== registry.size) {
+ _customElementSelector = Array.from(registry.keys(), (name) => name.replace(/\./g, '\\.')).join(',');
+ _customElementSelectorSize = registry.size;
+ }
+ const found = [];
+ if (inclusive && type === 1 && registry.has(root.localName)) _pushCustomElementTree(root, found);
+ const ids = _domParse("query_selector_all_scoped", root._nid, _customElementSelector) || [];
+ for (const nid of ids) {
+ const el = _wrapEl(+nid);
+ if (el) _pushCustomElementTree(el, found);
+ }
+ return found;
+}
+// Shadow-including tree order: a custom element, then the defined elements of
+// its shadow tree, then its light descendants. Only custom element hosts are
+// visited, which is where components keep nested components.
+function _pushCustomElementTree(el, found) {
+ found.push(el);
+ const shadow = _shadowRootForHost(el, true);
+ if (shadow) {
+ const nested = _definedCustomElementsIn(shadow, false);
+ if (nested) for (const inner of nested) found.push(inner);
+ }
+}
+// Upgrades or connects the defined elements of a subtree that just became
+// connected, in tree order.
+function _connectCustomElements(root, inclusive) {
+ const elements = _definedCustomElementsIn(root, inclusive);
+ if (!elements || elements.length === 0 || !root.isConnected) return;
+ const registry = globalThis.customElements;
+ for (const el of elements) {
+ if (!el.__customUpgraded) registry._upgradeElement(el, registry._registry.get(el.localName));
+ else _customElementCallback(el, 'connectedCallback');
+ }
+}
+// Collected before a connected subtree leaves the tree, called after.
+function _connectedCustomElementsIn(root, inclusive) {
+ const elements = _definedCustomElementsIn(root, inclusive);
+ if (!elements || elements.length === 0 || !root.isConnected) return null;
+ return elements;
+}
+function _disconnectCustomElements(elements) {
+ if (elements) for (const el of elements) _customElementCallback(el, 'disconnectedCallback');
+}
+function _customElementCallback(el, name, args) {
+ if (!el.__customUpgraded || el.__customUpgradeFailed) return;
+ const callback = el[name];
+ if (typeof callback !== 'function') return;
+ try { callback.apply(el, args || []); }
+ catch (e) { globalThis.reportError?.(e); }
+}
+// attributeChangedCallback: the old value is only read for an upgraded
+// element whose definition observes the attribute.
+function _customElementObserves(el, localName) {
+ if (!el.__customUpgraded || el.__customUpgradeFailed) return false;
+ const observed = globalThis.customElements?._observedAttributes?.get(el.localName);
+ return !!observed && observed.has(localName);
+}
+
function __prepareInsertedSubtree(root) {
// HTML's script preparation algorithm leaves a disconnected script
// unstarted. When an ancestor is later connected, insertion steps visit
@@ -2664,6 +2734,7 @@ function _removeNode(parent, child, suppress, method = "removeChild") {
const [previousSibling, nextSibling] = observed ? _recordSiblings(child) : [null, null];
const removedWindowNames = _windowNamedNamesInTree(child);
if (child instanceof Element) _releaseLinkedStylesheetsIn(child);
+ const leavingCustomElements = _connectedCustomElementsIn(child, true);
const parentConnected = parent.isConnected;
const removed = _dom("remove_child", child._nid) === "true";
if (!removed) {
@@ -2675,12 +2746,14 @@ function _removeNode(parent, child, suppress, method = "removeChild") {
_detachStyleSheetsInSubtree(child);
_reconcileWindowNamedProperties(removedWindowNames);
if (observed) _queueTreeMutationRecord(parent, [], [child], previousSibling, nextSibling);
+ _disconnectCustomElements(leavingCustomElements);
}
// Move one node under `parent` before `child` (append when null). A node
// that still has a parent is removed from it first, which old-parent
// observers see as a removal (adopt runs "remove" before inserting).
function _insertOneNode(parent, node, child, method, queueOldParentRecord) {
+ let leavingCustomElements = null;
if (node._shadowParent) {
node._shadowParent.removeChild(node);
} else {
@@ -2691,6 +2764,7 @@ function _insertOneNode(parent, node, child, method, queueOldParentRecord) {
_queueTreeMutationRecord(oldParent, [], [node], previousSibling, nextSibling);
}
_detachStyleSheetsInSubtree(node);
+ leavingCustomElements = _connectedCustomElementsIn(node, true);
}
}
const parentConnected = parent.isConnected;
@@ -2705,6 +2779,7 @@ function _insertOneNode(parent, node, child, method, queueOldParentRecord) {
_seedInsertedTreeState(node, parent, parentConnected);
if (node._nodeDocument !== undefined) node._nodeDocument = undefined;
_registerWindowNamedTree(node);
+ _disconnectCustomElements(leavingCustomElements);
}
// "Insert": returns the inserted nodes. `suppress` skips parent's record.
@@ -4178,6 +4253,7 @@ class Element extends Node {
// retained CSSStyleSheet wrappers cannot keep stale owner/source nodes.
for (const style of this.querySelectorAll("style")) _detachStyleSheet(style);
_releaseLinkedStylesheetsIn(this);
+ const leavingCustomElements = _connectedCustomElementsIn(this, false);
let oldChildren = [];
let newChildren = [];
if (globalThis.__mutationObservers?.length) {
@@ -4195,6 +4271,8 @@ class Element extends Node {
newChildren = _domParse("child_nodes", this._nid) || [];
globalThis.__notifyMutation('childList', this._nid, newChildren, oldChildren);
}
+ _disconnectCustomElements(leavingCustomElements);
+ _connectCustomElements(this, false);
}
get outerHTML() { return _domParse("outer_html", this._nid) ?? ""; }
get innerText() {
@@ -4314,6 +4392,7 @@ class Element extends Node {
? this.getAttribute(n)
: null;
const observedOldValue = _attributeOldValuesWanted() ? this.getAttribute(n) : null;
+ const customOldValue = _customElementObserves(this, n) ? this.getAttribute(n) : undefined;
const value = String(v);
_dom("set_attribute", this._nid, n + "\0" + value);
if (detailsWasOpen !== null) _queueDetailsToggle(this, detailsWasOpen);
@@ -4341,6 +4420,7 @@ class Element extends Node {
}
if (popoverPrev !== undefined) this._popoverTypeMaybeChanged(popoverPrev);
if (_hasMutationObservers()) _queueAttributeMutationRecord(this, n, null, observedOldValue);
+ if (customOldValue !== undefined) _customElementCallback(this, 'attributeChangedCallback', [n, customOldValue, value, null]);
if (this.localName === "source"
&& (n === "srcset" || n === "sizes" || n === "media" || n === "type")) {
const picture = this.parentElement;
@@ -4362,9 +4442,11 @@ class Element extends Node {
? this.hasAttribute('open')
: null;
const observedOldValue = _attributeOldValuesWanted() ? this.getAttributeNS(ns, localName) : null;
+ const customOldValue = _customElementObserves(this, localName) ? this.getAttributeNS(ns, localName) : undefined;
_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.
@@ -4383,9 +4465,11 @@ class Element extends Node {
? this.getAttribute(n)
: null;
const observedOldValue = _hasMutationObservers() ? this.getAttribute(n) : null;
+ const customOldValue = _customElementObserves(this, n) ? this.getAttribute(n) : null;
_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);
}
@@ -4420,9 +4504,11 @@ class Element extends Node {
: null;
if (ns === '' && n === 'srcdoc' && this.localName === 'iframe' && this.getAttributeNS(ns, n) === null) return;
const observedOldValue = _hasMutationObservers() ? this.getAttributeNS(ns, n) : null;
+ const customOldValue = _customElementObserves(this, n) ? this.getAttributeNS(ns, n) : null;
_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("");
@@ -7091,12 +7177,15 @@ class DocumentFragment extends Node {
get innerHTML() { return _domParse("inner_html", this._nid) ?? ""; }
set innerHTML(v) {
const html = String(v ?? "");
+ const leavingCustomElements = _connectedCustomElementsIn(this, false);
if (this._fragmentContext) {
_dom("set_inner_html_context", this._nid, _fragmentContextPayload(this._fragmentContext, html));
} else {
_dom("set_inner_html", this._nid, html);
}
_connectSrcdocFrames(this, html);
+ _disconnectCustomElements(leavingCustomElements);
+ _connectCustomElements(this, false);
}
querySelector(s) { return _wrapEl(+_dom("query_selector_scoped", this._nid, s)); }
querySelectorAll(s) {
@@ -11070,8 +11159,18 @@ function _isValidCustomElementName(name) {
// PotentialCustomElementName (approx): lowercase start, a hyphen, no uppercase.
return /^[a-z][a-z0-9._\u00b7\u00c0-\uffff-]*-[a-z0-9._\u00b7\u00c0-\uffff-]*$/.test(name);
}
+// Read once at define time, as the spec does, and only when the class has an
+// attributeChangedCallback.
+function _observedAttributesOf(cls) {
+ if (typeof cls.prototype?.attributeChangedCallback !== 'function') return null;
+ const list = cls.observedAttributes;
+ if (list == null) return null;
+ const names = new Set();
+ for (const name of list) names.add(String(name));
+ return names.size ? names : null;
+}
class CustomElementRegistry {
- constructor() { this._registry = new Map(); this._byCtor = new Map(); this._whenDefinedResolvers = new Map(); this._defining = false; }
+ constructor() { this._registry = new Map(); this._byCtor = new Map(); this._observedAttributes = new Map(); this._whenDefinedResolvers = new Map(); this._defining = false; }
define(name, cls, opts) {
if (!_isConstructorCE(cls)) throw new TypeError("Failed to execute 'define' on 'CustomElementRegistry': parameter 2 is not a constructor.");
if (!_isValidCustomElementName(name)) throw new DOMException("Failed to execute 'define' on 'CustomElementRegistry': \"" + name + "\" is not a valid custom element name", "SyntaxError");
@@ -11079,7 +11178,12 @@ class CustomElementRegistry {
if (this._registry.has(name)) throw new DOMException("Failed to execute 'define' on 'CustomElementRegistry': the name \"" + name + "\" has already been used with this registry", "NotSupportedError");
if (this._byCtor.has(cls)) throw new DOMException("Failed to execute 'define' on 'CustomElementRegistry': the constructor has already been used with this registry", "NotSupportedError");
this._defining = true;
- try { this._byCtor.set(cls, name); this._defineInner(name, cls, opts); } finally { this._defining = false; }
+ try {
+ const observed = _observedAttributesOf(cls);
+ this._byCtor.set(cls, name);
+ if (observed) this._observedAttributes.set(name, observed);
+ this._defineInner(name, cls, opts);
+ } finally { this._defining = false; }
}
_defineInner(name, cls, opts) {
this._registry.set(name, cls);
@@ -11118,9 +11222,15 @@ class CustomElementRegistry {
if (constructed !== el) {
throw new TypeError("Custom element constructor did not produce the element being upgraded");
}
- if (typeof el.connectedCallback === 'function' && globalThis.document?.contains?.(el)) {
- try { el.connectedCallback(); } catch (e) {}
+ const observed = this._observedAttributes.get(el.localName);
+ if (observed) {
+ for (const attr of Array.from(el.attributes)) {
+ if (observed.has(attr.localName)) {
+ _customElementCallback(el, 'attributeChangedCallback', [attr.localName, null, attr.value, attr.namespaceURI]);
+ }
+ }
}
+ if (el.isConnected) _customElementCallback(el, 'connectedCallback');
} catch (e) {
el.__customUpgradeFailed = true;
}
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index c0c1c715..e65e88d8 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -8978,6 +8978,137 @@ mod tests {
assert_eq!(result, serde_json::json!([1, 0, true]));
}
+ #[test]
+ fn custom_elements_connect_and_upgrade_on_insertion() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate(
+ r#"
+ const log = [];
+ class XEl extends HTMLElement {
+ constructor() { super(); log.push('ctor'); }
+ connectedCallback() {
+ log.push('connected');
+ if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
+ }
+ disconnectedCallback() { log.push('disconnected'); }
+ }
+ customElements.define('x-el', XEl);
+ const created = document.createElement('x-el');
+ log.push('created');
+ document.body.appendChild(created);
+ log.push('appended:' + (created.shadowRoot !== null));
+ const holder = document.createElement('div');
+ holder.innerHTML = '<x-el></x-el>';
+ log.push('parsed:' + (holder.firstChild instanceof XEl));
+ document.body.appendChild(holder);
+ log.push('inner:' + (holder.firstChild.shadowRoot !== null));
+ document.body.innerHTML = '<x-el></x-el>';
+ log.push('set:' + (document.body.firstChild instanceof XEl));
+ document.body.firstChild.remove();
+ return log;
+ "#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([
+ "ctor", "created", "connected", "appended:true",
+ "parsed:false", "ctor", "connected", "inner:true",
+ "disconnected", "disconnected", "ctor", "connected", "set:true",
+ "disconnected"
+ ])
+ );
+ }
+
+ #[test]
+ fn custom_elements_get_reactions_for_moves_and_observed_attributes() {
+ let mut rt = setup_runtime(r#"<html><body><a-el id="a" mode="x" other="y"></a-el><p id="p"></p></body></html>"#);
+ let result = rt
+ .evaluate(
+ r#"
+ const log = [];
+ class AEl extends HTMLElement {
+ static get observedAttributes() { return ['mode']; }
+ attributeChangedCallback(name, oldValue, newValue) {
+ log.push(name + ':' + oldValue + '>' + newValue);
+ }
+ connectedCallback() { log.push('connected'); }
+ disconnectedCallback() { log.push('disconnected'); }
+ }
+ customElements.define('a-el', AEl);
+ const a = document.getElementById('a');
+ a.setAttribute('mode', 'y');
+ a.setAttribute('other', 'z');
+ a.removeAttribute('mode');
+ a.removeAttribute('mode');
+ document.getElementById('p').appendChild(a);
+ const detached = document.createElement('div');
+ detached.appendChild(a);
+ detached.removeChild(a);
+ return log;
+ "#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([
+ "mode:null>x", "connected", "mode:x>y", "mode:y>null",
+ "disconnected", "connected", "disconnected"
+ ])
+ );
+ }
+
+ #[test]
+ fn custom_elements_in_a_connected_shadow_tree_connect() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate(
+ r#"
+ const log = [];
+ class InEl extends HTMLElement {
+ connectedCallback() { log.push('inner'); }
+ }
+ class OutEl extends HTMLElement {
+ connectedCallback() {
+ const root = this.attachShadow({ mode: 'open' });
+ root.innerHTML = '<in-el></in-el>';
+ log.push('outer');
+ }
+ }
+ customElements.define('in-el', InEl);
+ customElements.define('out-el', OutEl);
+ const host = document.createElement('out-el');
+ document.body.appendChild(host);
+ host.shadowRoot.appendChild(document.createElement('in-el'));
+ return [log, host.shadowRoot.firstChild instanceof InEl];
+ "#,
+ )
+ .unwrap();
+ assert_eq!(result, serde_json::json!([["inner", "outer", "inner"], true]));
+ }
+
+ #[test]
+ fn throwing_connected_callback_is_reported_not_thrown() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate(
+ r#"
+ const reported = [];
+ globalThis.reportError = (e) => reported.push(e.message);
+ class BadEl extends HTMLElement {
+ connectedCallback() { throw new Error('connect failed'); }
+ }
+ customElements.define('bad-el', BadEl);
+ const el = document.createElement('bad-el');
+ document.body.appendChild(el);
+ return [el.isConnected, reported];
+ "#,
+ )
+ .unwrap();
+ assert_eq!(result, serde_json::json!([true, ["connect failed"]]));
+ }
+
#[test]
fn editing_command_detection_reports_unsupported_commands_without_mutation() {
let mut rt = setup_runtime("<body><p>unchanged</p><iframe></iframe></body>");