Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions lib/sanitize/filters/unique-svg-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,37 @@ module.exports = function configureUniquePrefixId (opts) {
}

return function createUniquePrefixId (value) {
// Find all the xlink:href props with local references and update
if (value.xlinkHref && value.xlinkHref.toString().startsWith("#")){
var newValue = "#" + opts.prefix + value.xlinkHref.toString().replace("#", "");
value.xlinkHref = newValue;
this.update(value);
var HASHBANG_PROPS = ['xlinkHref', 'href'];
var URL_PROPS = ['fill', 'mask', 'clipPath', 'filter'];
var propName, propValue, newValue;

for (var i = 0; i < HASHBANG_PROPS.length; i++) {
propName = HASHBANG_PROPS[i];
propValue = value[propName];

if (propValue && propValue.toString().startsWith("#")){
newValue = "#" + opts.prefix + propValue.toString().replace("#", "");
value[propName] = newValue;
this.update(value);
}
}

// Find all href props and update
if (value.href && value.href.toString().startsWith("#")){
var newValue = "#" + opts.prefix + value.href.toString().replace("#", "");
value.href = newValue;
this.update(value);
for (var j = 0; j < URL_PROPS.length; j++) {
propName = URL_PROPS[j];
propValue = value[propName];

if (propValue && propValue.toString().startsWith("url")){
newValue = "url(#" + _getMatches(propName, propValue) + ")";
value[propName] = newValue;
this.update(value);
}
}

// Find all IDs and update with filename prefix
if (value.id){
var newValue = opts.prefix + value.id;
newValue = opts.prefix + value.id;
value.id = newValue;
this.update(value);
}

// Find all fill props and update with filename prefix
if (value.fill && value.fill.toString().startsWith("url")){
var newValue = "url(#" + _getMatches('fill', value.fill) + ")";
value.fill = newValue;
this.update(value);
}

// Find all mask props and update with filename prefix
if (value.mask && value.mask.toString().startsWith("url")){
var newValue = _getMatches('mask', value.mask);
var newValue = "url(#" + _getMatches('fill', value.mask) + ")";
value.mask = newValue;
this.update(value);
}

// Find all clipPath props and update with filename prefix
if (value.clipPath && value.clipPath.toString().startsWith("url")){
var newValue = "url(#" + _getMatches('clipPath', value.clipPath) + ")";
value.clipPath = newValue;
this.update(value);
}
};
};