Skip to content

Commit

Permalink
Merge pull request #113 from adroitwhiz/fix-xmlns-in-text
Browse files Browse the repository at this point in the history
Only fix XML namespace attributes if they're actually attributes
  • Loading branch information
fsih authored Jan 7, 2021
2 parents d8029b3 + df79388 commit 4c6e271
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/fixup-svg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ module.exports = function (svgString) {
// This will cause SVG parsing to fail, so replace these with a dummy namespace name.
// This namespace name is only valid for "xml", and if we bind "xmlns:xml" to the dummy namespace,
// parsing will fail yet again, so exclude "xmlns:xml" declarations.
if (svgString.match(/xmlns:(?!xml=)[^ ]+="http:\/\/www.w3.org\/XML\/1998\/namespace"/) !== null) {
const xmlnsRegex = /(<[^>]+?xmlns:(?!xml=)[^ ]+=)"http:\/\/www.w3.org\/XML\/1998\/namespace"/g;
if (svgString.match(xmlnsRegex) !== null) {
svgString = svgString.replace(
// capture the entire attribute
/(xmlns:(?!xml=)[^ ]+)="http:\/\/www.w3.org\/XML\/1998\/namespace"/g,
xmlnsRegex,
// use the captured attribute name; replace only the URL
($0, $1) => `${$1}="http://dummy.namespace"`
($0, $1) => `${$1}"http://dummy.namespace"`
);
}

Expand Down
7 changes: 7 additions & 0 deletions test/fixup-svg-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ test('fixupSvgString should correct namespace declarations bound to reserved nam
t.end();
});

test('fixupSvgString shouldn\'t correct non-attributes', t => {
const dontFix = fixupSvgString('<text>xmlns:test="http://www/w3.org/XML/1998/namespace" is not an xmlns attribute</text>');

t.notEqual(dontFix.indexOf('http://www/w3.org/XML/1998/namespace'), -1);
t.end();
});

test('fixupSvgString should empty script tags', t => {
const filePath = path.resolve(__dirname, './fixtures/script.svg');
const svgString = fs.readFileSync(filePath)
Expand Down

0 comments on commit 4c6e271

Please sign in to comment.