Skip to content

Commit

Permalink
fix: handle script tags in SVG (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 25, 2020
1 parent d30c549 commit d021e42
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 57 deletions.
45 changes: 30 additions & 15 deletions src/utils.js
Expand Up @@ -428,6 +428,22 @@ function getAttributeValue(attributes, name) {
return attributes[lowercasedAttributes[name.toLowerCase()]];
}

function scriptFilter(tag, attribute, attributes) {
if (attributes.type) {
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();

if (
type !== 'module' &&
type !== 'text/javascript' &&
type !== 'application/javascript'
) {
return false;
}
}

return true;
}

const defaultAttributes = [
{
tag: 'audio',
Expand Down Expand Up @@ -483,21 +499,20 @@ const defaultAttributes = [
tag: 'script',
attribute: 'src',
type: 'src',
filter: (tag, attribute, attributes) => {
if (attributes.type) {
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();

if (
type !== 'module' &&
type !== 'text/javascript' &&
type !== 'application/javascript'
) {
return false;
}
}

return true;
},
filter: scriptFilter,
},
// Using href with <script> is described here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script
{
tag: 'script',
attribute: 'href',
type: 'src',
filter: scriptFilter,
},
{
tag: 'script',
attribute: 'xlink:href',
type: 'src',
filter: scriptFilter,
},
{
tag: 'source',
Expand Down
176 changes: 154 additions & 22 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

0 comments on commit d021e42

Please sign in to comment.