Skip to content

Commit efd36ea

Browse files
authored
Utilize DOMPurify hooks to preserve <use> elements in playground mode (#156)
1 parent 929a2ba commit efd36ea

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"test-typescript": "karma start ./test/deployment/typescript/karma.conf.js",
2626
"test:ci": "cross-env SHOW_DIFF=true npm run test",
2727
"createreferences": "node test/common/reference-server.js",
28-
"prettier": "prettier --write {src,tests,typings}/**/*.{ts,js}",
29-
"lint": "eslint {src,tests,typings}/**/*.{ts,js}"
28+
"prettier": "prettier --write {playground,src,tests,typings}/**/*.{ts,js}",
29+
"lint": "eslint {playground,src,tests,typings}/**/*.{ts,js}"
3030
},
3131
"repository": {
3232
"type": "git",

playground/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const { jsPDF } = window.jspdf
22
const DOMPurify = window.DOMPurify
33

4+
DOMPurify.addHook('afterSanitizeAttributes', node => {
5+
if (
6+
(node.hasAttribute('xlink:href') && !node.getAttribute('xlink:href').match(/^#/)) ||
7+
(node.hasAttribute('href') && !node.getAttribute('href').match(/^#/))
8+
) {
9+
node.remove()
10+
}
11+
})
12+
413
const defaultSample = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 150">
514
<text x="20" y="20">Hello, world!</text>
615
</svg>`
@@ -25,7 +34,7 @@ window.addEventListener('load', () => {
2534
editor.on(
2635
'change',
2736
debounce(() => {
28-
const svgText = DOMPurify.sanitize(doc.getValue())
37+
const svgText = DOMPurify.sanitize(doc.getValue(), { ADD_TAGS: ['use'] })
2938
updateUrl(svgText)
3039
updateIssueLinks()
3140
updateSvg(svgText)

0 commit comments

Comments
 (0)