Don't serialize all cssRules if multiple text nodes exists (#866)

This commit is contained in:
Justin Halsall
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 67554c33cc
commit ffc9185fa8
2 changed files with 60 additions and 2 deletions

View File

@@ -632,7 +632,12 @@ function serializeNode(
if (isStyle && textContent) {
try {
// try to read style sheet
if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
if (n.nextSibling || n.previousSibling) {
// This is not the only child of the stylesheet.
// We can't read all of the sheet's .cssRules and expect them
// to _only_ include the current rule(s) added by the text node.
// So we'll be conservative and keep textContent as-is.
} else if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
textContent = stringifyStyleSheet(
(n.parentNode as HTMLStyleElement).sheet!,
);