From e6f1e0bb15cc42fa592353baf95e47fa9847e88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E6=89=8B=E6=8D=A7=E9=B2=9C=E8=8A=B1?= <157215725@qq.com> Date: Tue, 14 Mar 2023 16:16:53 +0800 Subject: [PATCH] fix: xml legal characters regex (#12) --- src/utils.ts | 3 ++- test/fixtures/xml.ascii-null-character.html | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 310f042..80fcfd9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -96,8 +96,9 @@ export function createSvg(width: number, height: number, ownerDocument?: Documen export function svgToDataUrl(svg: SVGElement) { const xhtml = new XMLSerializer() .serializeToString(svg) + // https://www.w3.org/TR/xml/#charsets // eslint-disable-next-line no-control-regex - .replace(/[\u0000-\u0008\u000B-\u000C\u000E-\u001F]/g, '') + .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/ug, '') return `data:image/svg+xml;charset=utf-8,${ encodeURIComponent(xhtml) }` } diff --git a/test/fixtures/xml.ascii-null-character.html b/test/fixtures/xml.ascii-null-character.html index dcb86a3..2eba5f9 100644 --- a/test/fixtures/xml.ascii-null-character.html +++ b/test/fixtures/xml.ascii-null-character.html @@ -7,11 +7,15 @@ ;[ // \u0000-\u0008 - '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\u0008', - // \u000B - '\u000B', + ...Array.from({ length: 0x0008 }, (_, i) => String.fromCharCode(i)), + // \u000B-\u000C + '\u000B', '\u000C', // \u000E-\u001F - '\u000E', '\u000F', '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', '\u001E', + ...Array.from({ length: 0x001F - 0x000E }, (_, i) => String.fromCharCode(0x000E + i)), + // \uD800-\uDFFF + ...Array.from({ length: 0xDFFF - 0xD800 }, (_, i) => String.fromCharCode(0xD800 + i)), + // \uFFFE-\uFFFF + '\uFFFE', '\uFFFF', ].forEach(unicode => { root.appendChild(document.createTextNode(unicode)) })