Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Fix: idPrefix regex to support style attr
Browse files Browse the repository at this point in the history
Currently createIdPrefix for tag attributes only works
if the url is the first value.

e.g: <path mask="url(#foo")>

This commit adds support also for the following.

<g style="mask: url(#foo)">
  • Loading branch information
koox00 committed May 13, 2019
1 parent d2c7218 commit a693841
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
15 changes: 11 additions & 4 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function createClassPrefix(classPrefix) {
}

function createIdPrefix(idPrefix) {
var url_pattern = /^url\(#.+\)$/i;
var url_pattern = /url\(#.+?\)/i;
return function prefixIds(tag) {
var idIdx = getAttributeIndex(tag, 'id');
if (idIdx !== -1) {
Expand All @@ -150,10 +150,17 @@ function createIdPrefix(idPrefix) {
if (tag.attributes && tag.attributes.length > 0) {
// replace instances of url(#foo) in attributes
tag.attributes.forEach(function (attr) {
if (attr[1].match(url_pattern)) {
var attrValue = attr[1].match(url_pattern);
if (attrValue) {
attr[1] = attr[1].replace(url_pattern, function (match) {
var id = match.substring(5, match.length -1);
return "url(#" + idPrefix + id + ")";
var index = attr[1].indexOf(attrValue[0]);
var id = match.substring(5, match.length - 1)
return (
attr[1].slice(index, index + 5) +
idPrefix +
id +
attr[1].slice(index + 5 + id.length)
)
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/with-ids.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions tests/svg-inline-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ describe('getExtractedSVG()', function(){
var svgWithStyle = require('raw!./fixtures/with-ids.svg');
var processedStyleInsertedSVG = SVGInlineLoader.getExtractedSVG(svgWithStyle, { idPrefix: 'test.prefix-' });


assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 3 );
assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 4 );
// // replaces xlink:href=
assert.isTrue( processedStyleInsertedSVG.match(/xlink:href=/g).length === 1 );
// // replaces url(#foo)
assert.isTrue( processedStyleInsertedSVG.match(/url\(#test\.prefix-foo\)/g).length === 1 );
assert.isTrue( processedStyleInsertedSVG.match(/url\(#test\.prefix-foo\)/g).length === 2 );
});

it('should be able to specify tags to be removed by `removingTags` option', function () {
Expand Down

0 comments on commit a693841

Please sign in to comment.