From 4821e02f2b5c3627002f79d9cb1639997a545a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 17:39:24 +0000 Subject: [PATCH] fix(core): a rich-text style value is written from its own tokens, not copied (#8734) --- .../core/src/utils/richTextSanitize.test.ts | 20 +++++++++++++++++++ packages/core/src/utils/richTextSanitize.ts | 20 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/core/src/utils/richTextSanitize.test.ts b/packages/core/src/utils/richTextSanitize.test.ts index 81e72a4805..0c53cfd193 100644 --- a/packages/core/src/utils/richTextSanitize.test.ts +++ b/packages/core/src/utils/richTextSanitize.test.ts @@ -203,6 +203,26 @@ describe.each(PARSERS)("sanitizeRichTextChildren (%s)", (_name, parse) => { expect(kept).not.toMatch(/position|\\|url\(|image-set\(/i); }); + it.each([ + [ + "spacing a value was sent with", + "font-family:Inter,\n\tsans-serif;color: red", + "font-family: Inter, sans-serif; color: red", + ], + ["a value holding markup", "color: red; font-family: Inter { + const host = parse("x"); + host.firstElementChild?.setAttribute("style", style); + sanitizeRichTextChildren(host); + expect(host.firstElementChild?.getAttribute("style")).toBe(kept); + }); + // fails without the fix: the resource-function check read inside quoted strings and dropped the family. it("keeps a quoted font family whose name reads like a function", () => { const out = clean(`x`, parse); diff --git a/packages/core/src/utils/richTextSanitize.ts b/packages/core/src/utils/richTextSanitize.ts index dc21b69dde..a77f462337 100644 --- a/packages/core/src/utils/richTextSanitize.ts +++ b/packages/core/src/utils/richTextSanitize.ts @@ -197,14 +197,30 @@ function filterStyle(style: string): string { const colon = declaration.indexOf(":"); if (colon === -1) return null; const property = declaration.slice(0, colon).trim().toLowerCase(); - const value = declaration.slice(colon + 1).trim(); - if (!isRichTextFormattingStyle(property, value)) return null; + const value = serializedValue(declaration.slice(colon + 1)); + if (value === null || !isRichTextFormattingStyle(property, value)) return null; return `${property}: ${value}`; }) .filter((declaration): declaration is string => declaration !== null) .join("; "); } +// Whitespace, a plain string, a number with its unit, a hash, a word or a function's name, a separator, !important. +const VALUE_TOKEN = + /\s+|"[^"\\\n\r\f]*"|'[^'\\\n\r\f]*'|[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?(?:%|[a-z]+)?|#[\w-]+|-?-?[a-z_\u00a0-\uffff][\w\u00a0-\uffff-]*\(?|[(),/+*-]|!important\b/giy; + +/** The value written out again from its tokens, or null when some of it is no token a formatting value uses. */ +function serializedValue(value: string): string | null { + const tokens: string[] = []; + VALUE_TOKEN.lastIndex = 0; + while (VALUE_TOKEN.lastIndex < value.length) { + const token = VALUE_TOKEN.exec(value)?.[0]; + if (token === undefined) return null; + tokens.push(/^\s/.test(token) ? " " : token); + } + return tokens.join("").trim(); +} + const PLAIN_STRING = /"[^"\\\n\r\f]*"|'[^'\\\n\r\f]*'/g; const withoutPlainStrings = (text: string) => text.replace(PLAIN_STRING, (string) => "_".repeat(string.length));