diff --git a/lib/sanitize/filters/unique-svg-ids.js b/lib/sanitize/filters/unique-svg-ids.js index 964eb53..ffca9ae 100644 --- a/lib/sanitize/filters/unique-svg-ids.js +++ b/lib/sanitize/filters/unique-svg-ids.js @@ -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); - } }; };