Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle script tags in SVG #315

Merged
merged 1 commit into from Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.