Skip to content

Commit d021e42

Browse files
fix: handle script tags in SVG (#315)
1 parent d30c549 commit d021e42

File tree

6 files changed

+301
-57
lines changed

6 files changed

+301
-57
lines changed

src/utils.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,22 @@ function getAttributeValue(attributes, name) {
428428
return attributes[lowercasedAttributes[name.toLowerCase()]];
429429
}
430430

431+
function scriptFilter(tag, attribute, attributes) {
432+
if (attributes.type) {
433+
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();
434+
435+
if (
436+
type !== 'module' &&
437+
type !== 'text/javascript' &&
438+
type !== 'application/javascript'
439+
) {
440+
return false;
441+
}
442+
}
443+
444+
return true;
445+
}
446+
431447
const defaultAttributes = [
432448
{
433449
tag: 'audio',
@@ -483,21 +499,20 @@ const defaultAttributes = [
483499
tag: 'script',
484500
attribute: 'src',
485501
type: 'src',
486-
filter: (tag, attribute, attributes) => {
487-
if (attributes.type) {
488-
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();
489-
490-
if (
491-
type !== 'module' &&
492-
type !== 'text/javascript' &&
493-
type !== 'application/javascript'
494-
) {
495-
return false;
496-
}
497-
}
498-
499-
return true;
500-
},
502+
filter: scriptFilter,
503+
},
504+
// Using href with <script> is described here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script
505+
{
506+
tag: 'script',
507+
attribute: 'href',
508+
type: 'src',
509+
filter: scriptFilter,
510+
},
511+
{
512+
tag: 'script',
513+
attribute: 'xlink:href',
514+
type: 'src',
515+
filter: scriptFilter,
501516
},
502517
{
503518
tag: 'source',

test/__snapshots__/attributes-option.test.js.snap

Lines changed: 154 additions & 22 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)