Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ID replacements not being performed in style elements #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.5.0
6 changes: 4 additions & 2 deletions dist/svg-inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
// Regular expression for functional notations of an IRI references. This will find occurences in the form
// url(#anyId) or url("#anyId") (for Internet Explorer) and capture the referenced ID
var funcIriRegex = /url\("?#([a-zA-Z][\w:.-]*)"?\)/g;
// Regex for style #id notation
var idRegex = /#(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)/g;
// Get all elements with an ID. The SVG spec recommends to put referenced elements inside <defs> elements, but
// this is not a requirement, therefore we have to search for IDs in the whole SVG.
var idElements = svgElem.querySelectorAll('[id]');
Expand Down Expand Up @@ -200,11 +202,11 @@
if (element.localName == _STYLE_) {
// If element is a style element, replace IDs in all occurences of "url(#anyId)" in text content
value = element.textContent;
newValue = value && value.replace(funcIriRegex, function(match, id) {
newValue = value && value.replace(idRegex, function(match, id) {
if (referencedIds) {
referencedIds[id] = 1;
}
return 'url(#' + id + idSuffix + ')';
return "#" + id + idSuffix;
});
if (newValue !== value) {
element.textContent = newValue;
Expand Down
20 changes: 10 additions & 10 deletions dist/svg-inject.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/svg-inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
// Regular expression for functional notations of an IRI references. This will find occurences in the form
// url(#anyId) or url("#anyId") (for Internet Explorer) and capture the referenced ID
var funcIriRegex = /url\("?#([a-zA-Z][\w:.-]*)"?\)/g;
// Regex for style #id notation
var idRegex = /#(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)/g;
// Get all elements with an ID. The SVG spec recommends to put referenced elements inside <defs> elements, but
// this is not a requirement, therefore we have to search for IDs in the whole SVG.
var idElements = svgElem.querySelectorAll('[id]');
Expand Down Expand Up @@ -200,11 +202,11 @@
if (element.localName == _STYLE_) {
// If element is a style element, replace IDs in all occurences of "url(#anyId)" in text content
value = element.textContent;
newValue = value && value.replace(funcIriRegex, function(match, id) {
newValue = value && value.replace(idRegex, function(match, id) {
if (referencedIds) {
referencedIds[id] = 1;
}
return 'url(#' + id + idSuffix + ')';
return "#" + id + idSuffix;
});
if (newValue !== value) {
element.textContent = newValue;
Expand Down
Loading