Merge pull request #559 from mnaza/fix/documentelement-innerhtml-head-556

fix(dom): keep <head> content when setting documentElement.innerHTML
This commit is contained in:
SGavrl authored and GitHub committed 2026-08-07 16:47:55 +00:00
commit 750d54ce56
3 files changed
+45 -9

No files matched your search

+1 -1
View File
@@ -431,7 +431,7 @@ fn op_dom_inner(state: &OpState, cmd: String, arg1: String, arg2: String) -> Str
Some(name) => obscura_dom::parse_fragment_with_context(&arg2, name),
None => obscura_dom::parse_fragment(&arg2),
};
let import_root = fragment.find_body_or_root();
let import_root = fragment.fragment_root();
dom.import_children_from(target, &fragment, import_root);
}
"true".into()
+32
View File
@@ -2142,6 +2142,38 @@ mod tests {
);
}
/// Setting innerHTML on the <html> element parses in the "before head"
/// insertion mode, which synthesizes head and body. The importer must keep
/// both; it previously returned the synthesized body and dropped the head
/// (so a <title>/<meta> assigned this way vanished).
#[test]
fn documentelement_inner_html_keeps_head_and_body() {
let mut rt = setup_runtime("<html><head></head><body></body></html>");
let v = rt
.evaluate(
"(function(){ document.documentElement.innerHTML = '<head><title>T</title></head><body><p>hi</p></body>'; \
var t = document.querySelector('title'); var p = document.querySelector('p'); \
return (t ? t.textContent : 'no-title') + '|' + (p ? p.textContent : 'no-p'); })()",
)
.unwrap();
assert_eq!(v, serde_json::json!("T|hi"));
}
/// Regression guard: innerHTML on an ordinary element still imports the
/// parsed nodes directly (no head/body is synthesized for a div context),
/// so the fix above must not change the common case.
#[test]
fn ordinary_element_inner_html_imports_content_directly() {
let mut rt = setup_runtime("<html><body><div id=\"d\"></div></body></html>");
let v = rt
.evaluate(
"(function(){ var d=document.getElementById('d'); d.innerHTML='<span>a</span><span>b</span>'; \
return d.children.length + '|' + d.textContent; })()",
)
.unwrap();
assert_eq!(v, serde_json::json!("2|ab"));
}
/// Issue #463: the same must hold for a template that arrives via innerHTML
/// rather than the initial document parse — that is how most frameworks
/// inject templates.