Skip to content

Commit d021e42

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

6 files changed

+301
-57
lines changed
 

‎src/utils.js

+30-15
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

+154-22
Large diffs are not rendered by default.

‎test/__snapshots__/esModule-option.test.js.snap

+42-6
Large diffs are not rendered by default.

‎test/__snapshots__/loader.test.js.snap

+14-2
Large diffs are not rendered by default.

‎test/__snapshots__/minimize-option.test.js.snap

+48-12
Large diffs are not rendered by default.

‎test/fixtures/simple.html

+13
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,16 @@ <h2>An Ordered HTML List</h2>
343343
5ErkJggg==" alt="Red dot" />
344344

345345
<img data-dz-thumbnail="" width="95" height="95" title="<%= name %>" alt="<%= name %>" src="<%= imgsrc %>" />
346+
347+
<svg>
348+
<script href="./script.file.js"></script>
349+
</svg>
350+
<svg>
351+
<script type="application/json" href="./script.file.js"></script>
352+
</svg>
353+
<svg>
354+
<script xlink:href="./script.file.js"></script>
355+
</svg>
356+
<svg>
357+
<script type="application/json" xlink:href="./script.file.js"></script>
358+
</svg>

0 commit comments

Comments
 (0)
Please sign in to comment.