Skip to content

The headless browser for AI agents and web scraping

fix(dom): implement CharacterData range semantics

SGavrldc5e60e42218
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 814a0566..1fb9767d 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -2138,6 +2138,12 @@ class Node {
}
appendChild(c) {
if (!c) return c;
+ if (this instanceof CharacterData) {
+ throw new DOMException(
+ "Failed to execute 'appendChild' on 'Node': This node type cannot have children.",
+ "HierarchyRequestError",
+ );
+ }
if (c instanceof DocumentFragment) {
const children = Array.from(c.childNodes);
for (const child of children) this.appendChild(child);
@@ -2401,27 +2407,46 @@ class CharacterData extends Node {
}
set data(v) {
const oldValue = _domParse("text_content", this._nid) ?? "";
- _dom("set_text_content", this._nid, String(v ?? ""));
+ _dom("set_text_content", this._nid, v === null ? "" : String(v));
if (globalThis.__mutationObservers?.length) {
globalThis.__notifyMutation('characterData', this._nid, [], [], null, oldValue);
}
}
get length() { return this.data.length; }
substringData(offset, count) {
- return this.data.substring(offset, offset + count);
+ if (arguments.length < 2) throw new TypeError("CharacterData.substringData requires 2 arguments");
+ const d = this.data;
+ offset = offset >>> 0;
+ count = count >>> 0;
+ if (offset > d.length) throw new DOMException("Offset is outside the data", "IndexSizeError");
+ return d.slice(offset, offset + count);
+ }
+ appendData(s) {
+ if (arguments.length < 1) throw new TypeError("CharacterData.appendData requires 1 argument");
+ this.data = this.data + String(s);
}
- appendData(s) { this.data += s; }
insertData(offset, s) {
+ if (arguments.length < 2) throw new TypeError("CharacterData.insertData requires 2 arguments");
const d = this.data;
- this.data = d.slice(0, offset) + s + d.slice(offset);
+ offset = offset >>> 0;
+ if (offset > d.length) throw new DOMException("Offset is outside the data", "IndexSizeError");
+ this.data = d.slice(0, offset) + String(s) + d.slice(offset);
}
deleteData(offset, count) {
+ if (arguments.length < 2) throw new TypeError("CharacterData.deleteData requires 2 arguments");
const d = this.data;
+ offset = offset >>> 0;
+ count = count >>> 0;
+ if (offset > d.length) throw new DOMException("Offset is outside the data", "IndexSizeError");
this.data = d.slice(0, offset) + d.slice(offset + count);
}
replaceData(offset, count, s) {
+ if (arguments.length < 3) throw new TypeError("CharacterData.replaceData requires 3 arguments");
const d = this.data;
- this.data = d.slice(0, offset) + s + d.slice(offset + count);
+ offset = offset >>> 0;
+ count = count >>> 0;
+ if (offset > d.length) throw new DOMException("Offset is outside the data", "IndexSizeError");
+ this.data = d.slice(0, offset) + String(s) + d.slice(offset + count);
}
}
@@ -2431,8 +2456,10 @@ class Text extends CharacterData {
get wholeText() { return this.data; }
splitText(offset) {
const d = this.data;
- const tail = d.substring(offset);
- this.data = d.substring(0, offset);
+ offset = offset >>> 0;
+ if (offset > d.length) throw new DOMException("Offset is outside the data", "IndexSizeError");
+ const tail = d.slice(offset);
+ this.data = d.slice(0, offset);
const newNid = +_dom("create_text_node", tail);
const parent = this.parentNode;
if (parent) {
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 66233933..a7fd971f 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -6584,6 +6584,53 @@ mod tests {
assert_eq!(normalized, serde_json::json!("live DOM title"));
}
+ #[test]
+ fn character_data_uses_webidl_ranges_and_rejects_children() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate(
+ r#"
+ (function() {
+ const errorName = callback => {
+ try { callback(); return null; } catch (error) { return error.name; }
+ };
+ const data = document.createTextNode("test");
+ data.data = undefined;
+ const undefinedData = data.data;
+ data.data = null;
+
+ const appended = document.createTextNode("a");
+ appended.appendData(null);
+ const inserted = document.createTextNode("test");
+ inserted.insertData(-0x100000000 + 2, "X");
+ const deleted = document.createTextNode("test");
+ deleted.deleteData(2, -1);
+ const replaced = document.createTextNode("test");
+ replaced.replaceData(2, -1, "yo");
+ const substring = document.createTextNode("test");
+
+ return [
+ undefinedData, data.data, appended.data, inserted.data,
+ deleted.data, replaced.data,
+ substring.substringData(-0x100000000 + 2, 1),
+ errorName(() => substring.substringData(5, 0)),
+ errorName(() => substring.substringData()),
+ errorName(() => substring.splitText(5)),
+ errorName(() => substring.appendChild(document.createComment("child")))
+ ];
+ })()
+ "#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([
+ "undefined", "", "anull", "teXst", "te", "teyo", "s",
+ "IndexSizeError", "TypeError", "IndexSizeError", "HierarchyRequestError"
+ ])
+ );
+ }
+
#[test]
fn document_title_setter_creates_missing_title_element() {
let mut rt = setup_runtime("<html><body><main>content</main></body></html>");