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

add option to makeIdsUnique to preselect suffix #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 11 additions & 8 deletions src/svg-inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@
// We assume tha all IDs within the injected SVG are unique, therefore the same suffix can be used for all IDs of one
// injected SVG.
// If the onlyReferenced argument is set to true, only those IDs will be made unique that are referenced from within the SVG
function makeIdsUnique(svgElem, onlyReferenced) {
var idSuffix = ID_SUFFIX + uniqueIdCounter++;
function makeIdsUnique(svgElem, options, onlyReferenced) {
var idSuffix = "";
if (options.choosenName) idSuffix = options.choosenName;
else idSuffix = ID_SUFFIX + uniqueIdCounter++;
// 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;
Expand Down Expand Up @@ -257,7 +259,8 @@

// For cached SVGs the IDs are made unique by simply replacing the already inserted unique IDs with a
// higher ID counter. This is much more performant than a call to makeIdsUnique().
function makeIdsUniqueCached(svgString) {
function makeIdsUniqueCached(svgString, options) {
if (options.choosenName) return svgString.replace(ID_SUFFIX_REGEX, options.choosenName);
return svgString.replace(ID_SUFFIX_REGEX, ID_SUFFIX + uniqueIdCounter++);
}

Expand Down Expand Up @@ -520,7 +523,7 @@
var absUrl = getAbsoluteUrl(src);
var useCacheOption = options.useCache;
var makeIdsUniqueOption = options.makeIdsUnique;

var setSvgLoadCacheValue = function(val) {
if (useCacheOption) {
svgLoadCache[absUrl].forEach(function(svgLoad) {
Expand Down Expand Up @@ -549,13 +552,13 @@
// IDs for the SVG string have not been made unique before. This may happen if previous
// injection of a cached SVG have been run with the option makedIdsUnique set to false
svgElem = buildSvgElement(svgString, false);
hasUniqueIds = makeIdsUnique(svgElem, false);
hasUniqueIds = makeIdsUnique(svgElem, options, false);

loadValue[0] = hasUniqueIds;
loadValue[2] = hasUniqueIds && svgElemToSvgString(svgElem);
} else if (hasUniqueIds) {
// Make IDs unique for already cached SVGs with better performance
svgString = makeIdsUniqueCached(uniqueIdsSvgString);
svgString = makeIdsUniqueCached(uniqueIdsSvgString, options);
}
}

Expand Down Expand Up @@ -606,7 +609,7 @@
if (svgElem instanceof SVGElement) {
var hasUniqueIds = NULL;
if (makeIdsUniqueOption) {
hasUniqueIds = makeIdsUnique(svgElem, false);
hasUniqueIds = makeIdsUnique(svgElem, options, false);
}

if (useCacheOption) {
Expand Down Expand Up @@ -694,4 +697,4 @@
if (typeof module == 'object' && typeof module.exports == 'object') {
module.exports = SVGInjectInstance;
}
})(window, document);
})(window, document);