fix(js): preserve inline styles when cloning

This commit is contained in:
SGavrl committed 2026-08-01 15:41:45 +00:00
1 parent c327354911
commit 5d7a02041a
2 files changed
+26

No files matched your search

+4
View File
@@ -654,6 +654,10 @@ function _shallowCloneNode(node) {
const v = node.getAttribute(name); const v = node.getAttribute(name);
if (v !== null) el.setAttribute(name, v); if (v !== null) el.setAttribute(name, v);
} }
// CSS declarations currently live on the JS wrapper independently of the
// DOM attribute. Copy that state as well so styles assigned through
// `node.style` survive cloning even before attribute reflection runs.
if (node.style && node.style.cssText) el.style.cssText = node.style.cssText;
return el; return el;
} }
+22
View File
@@ -1369,6 +1369,28 @@ mod tests {
assert_eq!(out, serde_json::json!("7|0")); assert_eq!(out, serde_json::json!("7|0"));
} }
#[test]
fn clone_node_copies_js_assigned_inline_styles() {
let mut rt = setup_runtime("<html><body><div id='d'></div></body></html>");
let out = rt
.evaluate(
"(function(){var d=document.getElementById('d');d.style.color='red';d.style.fontSize='12px';var c=d.cloneNode(false);return c.style.color+'|'+c.style.fontSize+'|'+c.style.cssText;})()",
)
.unwrap();
assert_eq!(out, serde_json::json!("red|12px|color: red; font-size: 12px;"));
}
#[test]
fn clone_node_deep_copies_template_content() {
let mut rt = setup_runtime("<html><body></body></html>");
let out = rt
.evaluate(
"(function(){var t=document.createElement('template');t.content.appendChild(document.createElement('option')).textContent='choice';var c=t.cloneNode(true);return c.content.childNodes.length+'|'+c.content.firstChild.tagName+'|'+c.content.firstChild.textContent;})()",
)
.unwrap();
assert_eq!(out, serde_json::json!("1|OPTION|choice"));
}
#[test] #[test]
fn test_document_title() { fn test_document_title() {
let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>"); let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>");