mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
fix(core): a rich-text style value is written from its own tokens, not copied (#8734)
This commit is contained in:
@@ -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 <b", "color: red"],
|
||||
[
|
||||
"a value holding an at-rule",
|
||||
"font-style: italic; font-family: Inter @media",
|
||||
"font-style: italic",
|
||||
],
|
||||
["a value holding a selector colon", "font-family: a:hover; color: red", "color: red"],
|
||||
])("writes a kept value from its own tokens, not %s", (_case, style, kept) => {
|
||||
const host = parse("<span>x</span>");
|
||||
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(`<span style='font-family: "Image(Display)"; color: red'>x</span>`, parse);
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user