Skip to content

Commit

Permalink
Utilize DOMPurify hooks to preserve <use> elements in playground mode (
Browse files Browse the repository at this point in the history
  • Loading branch information
listopadiya committed Oct 6, 2020
1 parent 929a2ba commit efd36ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"test-typescript": "karma start ./test/deployment/typescript/karma.conf.js",
"test:ci": "cross-env SHOW_DIFF=true npm run test",
"createreferences": "node test/common/reference-server.js",
"prettier": "prettier --write {src,tests,typings}/**/*.{ts,js}",
"lint": "eslint {src,tests,typings}/**/*.{ts,js}"
"prettier": "prettier --write {playground,src,tests,typings}/**/*.{ts,js}",
"lint": "eslint {playground,src,tests,typings}/**/*.{ts,js}"
},
"repository": {
"type": "git",
Expand Down
11 changes: 10 additions & 1 deletion playground/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const { jsPDF } = window.jspdf
const DOMPurify = window.DOMPurify

DOMPurify.addHook('afterSanitizeAttributes', node => {
if (
(node.hasAttribute('xlink:href') && !node.getAttribute('xlink:href').match(/^#/)) ||
(node.hasAttribute('href') && !node.getAttribute('href').match(/^#/))
) {
node.remove()
}
})

const defaultSample = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 150">
<text x="20" y="20">Hello, world!</text>
</svg>`
Expand All @@ -25,7 +34,7 @@ window.addEventListener('load', () => {
editor.on(
'change',
debounce(() => {
const svgText = DOMPurify.sanitize(doc.getValue())
const svgText = DOMPurify.sanitize(doc.getValue(), { ADD_TAGS: ['use'] })
updateUrl(svgText)
updateIssueLinks()
updateSvg(svgText)
Expand Down

0 comments on commit efd36ea

Please sign in to comment.