From 0e6b0ad8e5ee16a1f4efe93530db57097f7c8b81 Mon Sep 17 00:00:00 2001 From: James Thomson Date: Tue, 1 Oct 2019 20:59:32 +0100 Subject: [PATCH] Fix 'cleanupIDs' minify bug due to preserved IDs Prevent 'cleanupIDs' plugin from producing a preserved ID, including one which matches a preserved prefix, when minifying. Closes #1158 --- plugins/cleanupIDs.js | 11 ++++++++--- test/plugins/cleanupIDs.16.svg | 23 +++++++++++++++++++++++ test/plugins/cleanupIDs.17.svg | 23 +++++++++++++++++++++++ test/plugins/cleanupIDs.18.svg | 23 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 test/plugins/cleanupIDs.16.svg create mode 100644 test/plugins/cleanupIDs.17.svg create mode 100644 test/plugins/cleanupIDs.18.svg diff --git a/plugins/cleanupIDs.js b/plugins/cleanupIDs.js index f1fdc9f3..918474a0 100644 --- a/plugins/cleanupIDs.js +++ b/plugins/cleanupIDs.js @@ -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]) { @@ -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'); } } diff --git a/test/plugins/cleanupIDs.16.svg b/test/plugins/cleanupIDs.16.svg new file mode 100644 index 00000000..dc20966f --- /dev/null +++ b/test/plugins/cleanupIDs.16.svg @@ -0,0 +1,23 @@ + + + + + + + + + +@@@ + + + + + + + + + + +@@@ + +{"preserve": "a"} diff --git a/test/plugins/cleanupIDs.17.svg b/test/plugins/cleanupIDs.17.svg new file mode 100644 index 00000000..93ad9bc4 --- /dev/null +++ b/test/plugins/cleanupIDs.17.svg @@ -0,0 +1,23 @@ + + + + + + + + + +@@@ + + + + + + + + + + +@@@ + +{"preservePrefixes": "a"} diff --git a/test/plugins/cleanupIDs.18.svg b/test/plugins/cleanupIDs.18.svg new file mode 100644 index 00000000..bba201ff --- /dev/null +++ b/test/plugins/cleanupIDs.18.svg @@ -0,0 +1,23 @@ + + + + + + + + + +@@@ + + + + + + + + + + +@@@ + +{"preservePrefixes": "a"}