Skip to content

Commit

Permalink
fix: xml legal characters regex (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Mar 14, 2023
1 parent dfa9e24 commit e6f1e0b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) }`
}

Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/xml.ascii-null-character.html
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down

1 comment on commit e6f1e0b

@vercel
Copy link

@vercel vercel bot commented on e6f1e0b Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

modern-screenshot – ./

modern-screenshot-git-main-qq15725.vercel.app
modern-screenshot.vercel.app
modern-screenshot-qq15725.vercel.app

Please sign in to comment.