From de517168a4530e282804e4a4170d1748e7d05da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Sat, 20 Feb 2021 14:29:57 +0100 Subject: [PATCH 1/2] Add 'preserve' parameter to 'removeEditorsNSData' plugin --- plugins/removeEditorsNSData.js | 17 ++++++++++++----- test/plugins/removeEditorsNSData.03.svg | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/plugins/removeEditorsNSData.03.svg diff --git a/plugins/removeEditorsNSData.js b/plugins/removeEditorsNSData.js index 9e5c8cd8f..f67b66664 100644 --- a/plugins/removeEditorsNSData.js +++ b/plugins/removeEditorsNSData.js @@ -7,10 +7,12 @@ exports.active = true; exports.description = 'removes editors namespaces, elements and attributes'; var editorNamespaces = require('./_collections').editorNamespaces, - prefixes = []; + prefixes = [], + preserve = []; exports.params = { - additionalNamespaces: [] + additionalNamespaces: [], + preserve: [], // preserves elements and attributes }; /** @@ -32,6 +34,9 @@ exports.fn = function(item, params) { if (Array.isArray(params.additionalNamespaces)) { editorNamespaces = editorNamespaces.concat(params.additionalNamespaces); } + if (Array.isArray(params.preserve)) { + preserve = preserve.concat(params.preserve); + } if (item.elem) { @@ -42,7 +47,9 @@ exports.fn = function(item, params) { prefixes.push(attr.local); // - item.removeAttr(attr.name); + if (preserve.indexOf(attr.name) === -1) { + item.removeAttr(attr.name); + } } }); @@ -50,13 +57,13 @@ exports.fn = function(item, params) { // <* sodipodi:*=""> item.eachAttr(function(attr) { - if (prefixes.indexOf(attr.prefix) > -1) { + if (prefixes.indexOf(attr.prefix) > -1 && preserve.indexOf(attr.name) === -1) { item.removeAttr(attr.name); } }); // - if (prefixes.indexOf(item.prefix) > -1) { + if (prefixes.indexOf(item.prefix) > -1 && preserve.indexOf(item.elem) === -1) { return false; } diff --git a/test/plugins/removeEditorsNSData.03.svg b/test/plugins/removeEditorsNSData.03.svg new file mode 100644 index 000000000..fc47631fe --- /dev/null +++ b/test/plugins/removeEditorsNSData.03.svg @@ -0,0 +1,25 @@ + + + + ... + + + + ... + + + + +@@@ + + + + + ... + + + + +@@@ + +{"preserve":["xmlns:sodipodi","sodipodi:namedview"]} From 7375a646d6091db62471f8ef85f4e807ca9e0f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Sat, 20 Feb 2021 14:35:14 +0100 Subject: [PATCH 2/2] Add nested element attribute preservation test case --- test/plugins/removeEditorsNSData.03.svg | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/plugins/removeEditorsNSData.03.svg b/test/plugins/removeEditorsNSData.03.svg index fc47631fe..ebad59c0c 100644 --- a/test/plugins/removeEditorsNSData.03.svg +++ b/test/plugins/removeEditorsNSData.03.svg @@ -8,6 +8,7 @@ ... + @@@ @@ -18,8 +19,9 @@ ... + @@@ -{"preserve":["xmlns:sodipodi","sodipodi:namedview"]} +{"preserve":["xmlns:sodipodi","sodipodi:fakeattr","sodipodi:namedview"]}