fix(js): case-insensitive insertAdjacentHTML that keeps table/select fragments

Two defects: the position keyword was matched case-sensitively with no default
(so 'BeforeEnd' inserted nothing and an invalid position silently no-oped
instead of throwing SyntaxError), and fragments were parsed inside a throwaway
<div>, so context-sensitive children (<tr>, <td>, <option>, …) were dropped.

Lowercase the position and throw SyntaxError on an unknown value; parse via a
table/select-aware wrapMap (chosen from the fragment's leading tag, falling back
to the insertion context) so rows/cells/options survive.

Fixes #495
This commit is contained in:
Andrey Mnatsakanov committed 2026-07-26 13:30:07 +02:00
1 parent 8078cc3070
commit a663a15556
2 files changed
+92 -7

No files matched your search

+29
View File
@@ -1317,6 +1317,35 @@ mod tests {
rt
}
#[test]
fn insert_adjacent_html_parses_table_fragments() {
let mut rt = setup_runtime(
r#"<html><body><table id="t"><tbody id="tb"></tbody></table></body></html>"#,
);
let out = rt
.evaluate("(function(){var tb=document.getElementById('tb'); tb.insertAdjacentHTML('beforeend','<tr><td>1</td><td>2</td></tr>'); var tr=tb.firstElementChild; return tr ? (tr.tagName+':'+tr.children.length) : 'NULL';})()")
.unwrap();
assert_eq!(out, serde_json::json!("TR:2"));
}
#[test]
fn insert_adjacent_html_position_is_case_insensitive() {
let mut rt = setup_runtime(r#"<html><body><div id="host"><span>base</span></div></body></html>"#);
let out = rt
.evaluate("(function(){var h=document.getElementById('host'); h.insertAdjacentHTML('BeforeEnd','<b>x</b>'); return h.lastElementChild ? h.lastElementChild.tagName : 'NULL';})()")
.unwrap();
assert_eq!(out, serde_json::json!("B"));
}
#[test]
fn insert_adjacent_html_rejects_invalid_position() {
let mut rt = setup_runtime(r#"<html><body><div id="host"></div></body></html>"#);
let out = rt
.evaluate("(function(){var h=document.getElementById('host'); try { h.insertAdjacentHTML('nope','<b>x</b>'); return 'no-throw'; } catch(e){ return e.name; }})()")
.unwrap();
assert_eq!(out, serde_json::json!("SyntaxError"));
}
#[test]
fn test_document_title() {
let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>");