fix(extra) : preserve rich text colors
This commit is contained in:
+17
-3
@@ -22,6 +22,18 @@ const HTML_COMMENT = /<!--[\s\S]*?-->/g;
|
||||
const TAG = /<\/?([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>/g;
|
||||
const ATTRIBUTE = /([a-zA-Z_:][a-zA-Z0-9_:.-]*)\s*=\s*("[^"]*"|'[^']*'|[^\s"'=<>`]+)/g;
|
||||
const HEX_COLOR = /^#[0-9a-fA-F]{6}$/;
|
||||
const RGB_COLOR = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i;
|
||||
|
||||
function normalizeRichTextColor(value: string): string | null {
|
||||
const color = value.trim();
|
||||
if (HEX_COLOR.test(color)) return color.toUpperCase();
|
||||
|
||||
const rgb = color.match(RGB_COLOR);
|
||||
if (!rgb) return null;
|
||||
const channels = rgb.slice(1).map(Number);
|
||||
if (channels.some((channel) => channel > 255)) return null;
|
||||
return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`.toUpperCase();
|
||||
}
|
||||
|
||||
export function sanitizeRichTextUrl(value: string): string {
|
||||
const url = value.trim();
|
||||
@@ -75,9 +87,11 @@ export function sanitizeRichTextHtml(value: string): string {
|
||||
|
||||
if (name === "span") {
|
||||
const style = attributeValue(rawAttributes, "style");
|
||||
const color = style.match(/(?:^|;)\s*color\s*:\s*(#[0-9a-fA-F]{6})\s*(?:;|$)/)?.[1];
|
||||
return color && HEX_COLOR.test(color)
|
||||
? `<span style="color: ${color.toUpperCase()}">`
|
||||
const color = normalizeRichTextColor(
|
||||
style.match(/(?:^|;)\s*color\s*:\s*([^;]+?)\s*(?:;|$)/i)?.[1] ?? "",
|
||||
);
|
||||
return color
|
||||
? `<span style="color: ${color}">`
|
||||
: "<span>";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user