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

Commit 1d6b498

Browse files
committed
Fix: idPrefix regex to support style attr
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)">
1 parent d2c7218 commit 1d6b498

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

lib/transformer.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function createClassPrefix(classPrefix) {
131131
}
132132

133133
function createIdPrefix(idPrefix) {
134-
var url_pattern = /^url\(#.+\)$/i;
134+
var url_pattern = /url\(#.+?\)/i;
135135
return function prefixIds(tag) {
136136
var idIdx = getAttributeIndex(tag, 'id');
137137
if (idIdx !== -1) {
@@ -150,10 +150,17 @@ function createIdPrefix(idPrefix) {
150150
if (tag.attributes && tag.attributes.length > 0) {
151151
// replace instances of url(#foo) in attributes
152152
tag.attributes.forEach(function (attr) {
153-
if (attr[1].match(url_pattern)) {
153+
var attrValue = attr[1].match(url_pattern);
154+
if (attrValue) {
154155
attr[1] = attr[1].replace(url_pattern, function (match) {
155-
var id = match.substring(5, match.length -1);
156-
return "url(#" + idPrefix + id + ")";
156+
var index = attr[1].indexOf(attrValue[0]);
157+
var id = match.substring(5, match.length - 1)
158+
return (
159+
attr[1].slice(index, index + 5) +
160+
idPrefix +
161+
id +
162+
attr[1].slice(index + 5 + id.length)
163+
)
157164
});
158165
}
159166

tests/fixtures/with-ids.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/svg-inline-loader.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ describe('getExtractedSVG()', function(){
5656
var svgWithStyle = require('raw!./fixtures/with-ids.svg');
5757
var processedStyleInsertedSVG = SVGInlineLoader.getExtractedSVG(svgWithStyle, { idPrefix: 'test.prefix-' });
5858

59+
console.log(processedStyleInsertedSVG)
5960

60-
assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 3 );
61+
assert.isTrue( processedStyleInsertedSVG.match(/test\.prefix-foo/g).length === 4 );
6162
// // replaces xlink:href=
6263
assert.isTrue( processedStyleInsertedSVG.match(/xlink:href=/g).length === 1 );
6364
// // replaces url(#foo)
64-
assert.isTrue( processedStyleInsertedSVG.match(/url\(#test\.prefix-foo\)/g).length === 1 );
65+
assert.isTrue( processedStyleInsertedSVG.match(/url\(#test\.prefix-foo\)/g).length === 2 );
6566
});
6667

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

0 commit comments

Comments
 (0)