fix(js): coerce non-string primitives in before/after/replaceWith

before(), after() and replaceWith() only converted string arguments to Text
nodes; other primitives (numbers, booleans, null) were passed straight to
insert_before with no _nid, corrupting the call. Route them through the shared
_convertNodes helper (the same path append/prepend use), so every non-node
argument becomes document.createTextNode(String(n)).

Fixes #498
This commit is contained in:
Andrey Mnatsakanov committed 2026-07-26 13:30:07 +02:00
1 parent 8078cc3070
commit 499a5ef983
2 files changed
+26 -12

No files matched your search

+20
View File
@@ -1317,6 +1317,26 @@ mod tests {
rt
}
#[test]
fn childnode_helpers_coerce_non_string_primitives_to_text() {
let mut rt = setup_runtime(r#"<html><body><div id="p"><span id="t">x</span></div></body></html>"#);
let before = rt
.evaluate("(function(){var t=document.getElementById('t'); t.before(5); return t.previousSibling ? t.previousSibling.textContent : 'NULL';})()")
.unwrap();
assert_eq!(before, serde_json::json!("5"));
let after = rt
.evaluate("(function(){var t=document.getElementById('t'); t.after(true); return t.nextSibling ? t.nextSibling.textContent : 'NULL';})()")
.unwrap();
assert_eq!(after, serde_json::json!("true"));
let replaced = rt
.evaluate("(function(){var t=document.getElementById('t'); t.replaceWith(42); return document.getElementById('p').textContent;})()")
.unwrap();
assert!(
replaced.as_str().unwrap().contains("42"),
"replaceWith(42) should leave text '42': {replaced}"
);
}
#[test]
fn test_document_title() {
let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>");