Skip to content

The headless browser for AI agents and web scraping

fix(js): reflect ordered-list and list-item attributes

SGavrl2f302d9b6e8a
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 09286194..6fccfe2c 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -5975,13 +5975,19 @@ function _convertNodes(nodes) {
}
// ---- Reflected IDL attributes (WHATWG) ---------------------------------------
-// Installed ONCE on Element.prototype as shared getter/setter pairs. This is
+class HTMLOListElement extends Element {
+ get [Symbol.toStringTag]() { return "HTMLOListElement"; }
+}
+class HTMLLIElement extends Element {
+ get [Symbol.toStringTag]() { return "HTMLLIElement"; }
+}
+// Installed ONCE on shared prototypes as getter/setter pairs. This is
// data-driven so there is no per-element defineProperty: element creation and
// the querySelector/mutation hot paths are unaffected (each access is a normal
// prototype getter that reads the backing attribute). Covers the global content
// attributes reflected on every element plus the ARIAMixin (aria-* + ariaXxx).
(function installElementReflectors() {
- const P = Element.prototype;
+ let P = Element.prototype;
const def = (name, get, set) => {
if (Object.prototype.hasOwnProperty.call(P, name)) return; // never clobber an existing member
Object.defineProperty(P, name, { get, set, enumerable: true, configurable: true });
@@ -5996,7 +6002,7 @@ function _convertNodes(nodes) {
};
// IDL `long` conversion (ToInt32): finite, truncated, wrapped to 32-bit signed.
const toLong = (v) => {
- let n = Number(v);
+ let n = +v;
if (!Number.isFinite(n)) n = 0;
n = Math.trunc(n) % 4294967296;
if (n >= 2147483648) n -= 4294967296;
@@ -6071,6 +6077,13 @@ function _convertNodes(nodes) {
ariaValueMin: "aria-valuemin", ariaValueNow: "aria-valuenow", ariaValueText: "aria-valuetext",
};
for (const prop in ARIA) reflectNullable(prop, ARIA[prop]);
+
+ P = HTMLOListElement.prototype;
+ reflectBool("reversed", "reversed");
+ reflectLong("start", "start", 1);
+ reflectStr("type", "type");
+ P = HTMLLIElement.prototype;
+ reflectLong("value", "value", 0);
})();
function _parseXPathPredicate(part) {
@@ -7574,6 +7587,12 @@ function _elementClassFor(nid) {
if (tag === "META" && globalThis.HTMLMetaElement
&& _domParse("namespace_uri", nid) === "http://www.w3.org/1999/xhtml"
&& _domParse("local_name", nid) === "meta") return globalThis.HTMLMetaElement;
+ if ((tag === "OL" || tag === "LI")
+ && _domParse("namespace_uri", nid) === "http://www.w3.org/1999/xhtml") {
+ const localName = _domParse("local_name", nid);
+ if (localName === "ol") return HTMLOListElement;
+ if (localName === "li") return HTMLLIElement;
+ }
// Only HTML slots take part in slot assignment; a foreign-namespace "SLOT"
// (createElementNS + cloneNode lands here) stays a plain Element.
if (tag === "SLOT" && globalThis.HTMLSlotElement
@@ -7606,6 +7625,8 @@ function _elementClassForKnownName(namespace, qualifiedName) {
if (localName === "input" && globalThis.HTMLInputElement) return globalThis.HTMLInputElement;
if (tag === "TEXTAREA" && globalThis.HTMLTextAreaElement) return globalThis.HTMLTextAreaElement;
if (localName === "meta" && globalThis.HTMLMetaElement) return globalThis.HTMLMetaElement;
+ if (localName === "ol") return HTMLOListElement;
+ if (localName === "li") return HTMLLIElement;
if (tag === "SLOT" && globalThis.HTMLSlotElement) return globalThis.HTMLSlotElement;
if (tag === "IFRAME" && globalThis.HTMLIFrameElement) return globalThis.HTMLIFrameElement;
if (tag === "IMG") return HTMLImageElement;
@@ -14046,8 +14067,8 @@ globalThis.HTMLHtmlElement = Element;
globalThis.HTMLBRElement = Element;
globalThis.HTMLHRElement = Element;
globalThis.HTMLUListElement = Element;
-globalThis.HTMLOListElement = Element;
-globalThis.HTMLLIElement = Element;
+globalThis.HTMLOListElement = HTMLOListElement;
+globalThis.HTMLLIElement = HTMLLIElement;
globalThis.HTMLPreElement = Element;
globalThis.HTMLHeadingElement = Element;
globalThis.HTMLTemplateElement = Element;
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index b89068d9..1ca6135c 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -9503,6 +9503,105 @@ mod tests {
]));
}
+ #[test]
+ fn list_element_brands_survive_parsing_creation_and_cloning() {
+ let mut rt = setup_runtime("<html><body><ol><li>item</li></ol></body></html>");
+ let result = rt.evaluate(r#"
+ return [['ol', HTMLOListElement], ['li', HTMLLIElement]].map(([tag, C]) => {
+ const parsed = document.querySelector(tag);
+ const created = document.createElement(tag);
+ const nodes = [parsed, created, parsed.cloneNode(true), created.cloneNode(),
+ document.importNode(parsed, true)];
+ return [nodes.map(node => node instanceof C && node instanceof HTMLElement
+ && node.constructor === C), Object.prototype.toString.call(created),
+ [document.createElement('div'), document.createElement('ul'),
+ document.createElementNS('urn:test', tag),
+ document.createElementNS('urn:test', tag.toUpperCase()).cloneNode(),
+ document.createElementNS('http://www.w3.org/2000/svg', tag)]
+ .map(node => node instanceof C)];
+ });
+ "#).unwrap();
+ assert_eq!(result, serde_json::json!([
+ [[true, true, true, true, true], "[object HTMLOListElement]",
+ [false, false, false, false, false]],
+ [[true, true, true, true, true], "[object HTMLLIElement]",
+ [false, false, false, false, false]]
+ ]));
+ }
+
+ #[test]
+ fn list_element_properties_reflect_boolean_and_string_attributes() {
+ let mut rt = setup_runtime(
+ "<html><body><ol reversed start='-2' type='A'><li value='-4'>item</li></ol></body></html>",
+ );
+ let result = rt.evaluate(r#"
+ const parsed = document.querySelector('ol');
+ const list = document.createElement('ol');
+ const initial = [parsed.reversed, parsed.start, parsed.type,
+ document.querySelector('li').value, list.reversed, list.start, list.type];
+ const states = [];
+ list.reversed = true;
+ states.push([list.reversed, list.getAttribute('reversed')]);
+ list.reversed = false;
+ states.push([list.reversed, list.getAttribute('reversed')]);
+ list.setAttribute('reversed', 'false');
+ states.push([list.reversed, list.getAttribute('reversed')]);
+ list.removeAttribute('reversed');
+ states.push([list.reversed, list.getAttribute('reversed')]);
+ list.type = 'i';
+ const written = [list.type, list.getAttribute('type')];
+ list.setAttribute('type', 'invalid');
+ const changed = list.type;
+ list.removeAttribute('type');
+ const removed = list.type;
+ list.type = null;
+ return [initial, states, written, changed, removed, list.getAttribute('type'),
+ 'reversed' in document.createElement('div'),
+ 'start' in document.createElement('li'),
+ ['reversed', 'start', 'type'].every(name =>
+ Object.getOwnPropertyDescriptor(HTMLOListElement.prototype, name)?.enumerable),
+ Object.getOwnPropertyDescriptor(HTMLLIElement.prototype, 'value')?.enumerable];
+ "#).unwrap();
+ assert_eq!(result, serde_json::json!([
+ [true, -2, "A", -4, false, 1, ""],
+ [[true, ""], [false, null], [true, "false"], [false, null]],
+ ["i", "i"], "invalid", "", "null", false, false,
+ true, true
+ ]));
+ }
+
+ #[test]
+ fn list_element_integer_reflection_defaults_parses_and_wraps() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt.evaluate(r#"
+ const list = document.createElement('ol');
+ const item = document.createElement('li');
+ const attributes = [null, '', 'invalid', ' +12tail', '-3.8',
+ '2147483648', '-2147483649', '99999999999999999999', '0x10'].map(value => {
+ if (value === null) { list.removeAttribute('start'); item.removeAttribute('value'); }
+ else { list.setAttribute('start', value); item.setAttribute('value', value); }
+ return [list.start, item.value];
+ });
+ const setters = [4294967297, 2147483648, -2147483649, NaN, Infinity,
+ -0, 3.8, null].map(value => {
+ list.start = value;
+ item.value = value;
+ return [list.start, list.getAttribute('start'), item.value, item.getAttribute('value')];
+ });
+ const bigints = [[list, 'start'], [item, 'value'], [list, 'tabIndex']].map(([node, key]) => {
+ try { node[key] = 1n; return 'accepted'; } catch (error) { return error.name; }
+ });
+ return [attributes, setters, bigints];
+ "#).unwrap();
+ assert_eq!(result, serde_json::json!([
+ [[1, 0], [1, 0], [1, 0], [12, 12], [-3, -3], [1, 0], [1, 0], [1, 0], [0, 0]],
+ [[1, "1", 1, "1"], [-2147483648i64, "-2147483648", -2147483648i64, "-2147483648"],
+ [2147483647, "2147483647", 2147483647, "2147483647"], [0, "0", 0, "0"],
+ [0, "0", 0, "0"], [0, "0", 0, "0"], [3, "3", 3, "3"], [0, "0", 0, "0"]],
+ ["TypeError", "TypeError", "TypeError"]
+ ]));
+ }
+
#[test]
fn meta_http_equiv_reflection_supports_snapshot_csp_filtering() {
let mut rt = setup_runtime(