Skip to content

Commit

Permalink
Fix 'cleanupIDs' minify bug due to preserved IDs
Browse files Browse the repository at this point in the history
Prevent 'cleanupIDs' plugin from producing a preserved ID, including one
which matches a preserved prefix, when minifying.

Closes #1158
  • Loading branch information
thomsj authored and GreLI committed Oct 29, 2019
1 parent 71c7fe7 commit 0e6b0ad
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
11 changes: 8 additions & 3 deletions plugins/cleanupIDs.js
Expand Up @@ -121,13 +121,18 @@ exports.fn = function(data, params) {
return data;
}

const idPreserved = id => preserveIDs.has(id) || idMatchesPrefix(preserveIDPrefixes, id);

for (var ref of referencesIDs) {
var key = ref[0];

if (IDs.has(key)) {
// replace referenced IDs with the minified ones
if (params.minify && !preserveIDs.has(key) && !idMatchesPrefix(preserveIDPrefixes, key)) {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
if (params.minify && !idPreserved(key)) {
do {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
} while (idPreserved(currentIDstring));

IDs.get(key).attr('id').value = currentIDstring;

for (var attr of ref[1]) {
Expand All @@ -143,7 +148,7 @@ exports.fn = function(data, params) {
// remove non-referenced IDs attributes from elements
if (params.remove) {
for(var keyElem of IDs) {
if (!preserveIDs.has(keyElem[0]) && !idMatchesPrefix(preserveIDPrefixes, keyElem[0])) {
if (!idPreserved(keyElem[0])) {
keyElem[1].removeAttr('id');
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/plugins/cleanupIDs.16.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/cleanupIDs.17.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/cleanupIDs.18.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0e6b0ad

Please sign in to comment.