From 72f4fe7e09f8eb5833aafe9a7fb64fb2953d39dd Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Vecchio Date: Sun, 8 Oct 2017 18:42:22 -0500 Subject: [PATCH] Improvement: Update defs id's inside style attribute when needed --- svg-injector.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/svg-injector.js b/svg-injector.js index 08de011..3c9ab91 100644 --- a/svg-injector.js +++ b/svg-injector.js @@ -304,13 +304,21 @@ newId = currentId + '-' + injectCount; // All of the properties that can reference this element type - var referencingElements; + var referencingElements,currentStyle, newStyle; forEach.call(properties, function (property) { // :NOTE: using a substring match attr selector here to deal with IE "adding extra quotes in url() attrs" referencingElements = svg.querySelectorAll('[' + property + '*="' + currentId + '"]'); for (var j = 0, referencingElementLen = referencingElements.length; j < referencingElementLen; j++) { referencingElements[j].setAttribute(property, 'url(#' + newId + ')'); } + + // An svg can also have some of the mentioned properties declared inside the style attribute + referencingElements = svg.querySelectorAll('[style*="' + property + '"]'); + for (var j = 0, referencingElementLen = referencingElements.length; j < referencingElementLen; j++) { + currentStyle = referencingElements[j].getAttribute('style'); + newStyle = currentStyle.replace('url(#' + currentId + ')','url(#' + newId + ')'); + referencingElements[j].setAttribute('style', newStyle); + } }); elementDefs[i].id = newId;