From d23355e91a20d39bfe13bfbadf81155cdc18c541 Mon Sep 17 00:00:00 2001 From: GreLI Date: Sun, 24 Feb 2019 22:00:55 +0300 Subject: [PATCH] removeAttrs: add optional value filter (#977) --- plugins/removeAttrs.js | 29 +++++++++++++++++++++++------ test/plugins/removeAttrs.04.svg | 21 +++++++++++++++++++++ test/plugins/removeAttrs.05.svg | 21 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 test/plugins/removeAttrs.04.svg create mode 100644 test/plugins/removeAttrs.05.svg diff --git a/plugins/removeAttrs.js b/plugins/removeAttrs.js index 0ee35f26..491e0dc4 100644 --- a/plugins/removeAttrs.js +++ b/plugins/removeAttrs.js @@ -25,10 +25,11 @@ exports.params = { * * @param attrs: * - * format: [ element* : attribute* ] + * format: [ element* : attribute* : value* ] * - * element : regexp (wrapped into ^...$), single * or omitted > all elements + * element : regexp (wrapped into ^...$), single * or omitted > all elements (must be present when value is used) * attribute : regexp (wrapped into ^...$) + * value : regexp (wrapped into ^...$), single * or omitted > all values * * examples: * @@ -41,6 +42,10 @@ exports.params = { * --- * attrs: 'path:fill' * + * > remove fill attribute on path element where value is none + * --- + * attrs: 'path:fill:none' + * * * > remove all fill and stroke attribute * --- @@ -60,6 +65,10 @@ exports.params = { * * attrs: '.*:(fill|stroke)' * + * [is same as] + * + * attrs: '.*:(fill|stroke):.*' + * * * > remove all stroke related attributes * ---- @@ -85,12 +94,16 @@ exports.fn = function(item, params) { // prepare patterns var patterns = params.attrs.map(function(pattern) { - // apply to all elements if specifc element is omitted + // if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value* if (pattern.indexOf(elemSeparator) === -1) { - pattern = ['.*', elemSeparator, pattern].join(''); + pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join(''); + + // if only 1 separator, assume it's element and attribute name, and apply regardless of attribute value + } else if (pattern.split(elemSeparator).length < 3) { + pattern = [pattern, elemSeparator, '.*'].join(''); } - // create regexps for element and attribute name + // create regexps for element, attribute name, and attribute value return pattern.split(elemSeparator) .map(function(value) { @@ -118,7 +131,11 @@ exports.fn = function(item, params) { if (!(isFillCurrentColor || isStrokeCurrentColor)) { // matches attribute name if (pattern[1].test(name)) { - item.removeAttr(name); + + // matches attribute value + if (pattern[2].test(attr.value)) { + item.removeAttr(name); + } } } diff --git a/test/plugins/removeAttrs.04.svg b/test/plugins/removeAttrs.04.svg new file mode 100644 index 00000000..86e924d4 --- /dev/null +++ b/test/plugins/removeAttrs.04.svg @@ -0,0 +1,21 @@ + + + + + + + + +@@@ + + + + + + + + + +@@@ + +{"attrs":"*:(stroke|fill):red"} diff --git a/test/plugins/removeAttrs.05.svg b/test/plugins/removeAttrs.05.svg new file mode 100644 index 00000000..edbb5dc7 --- /dev/null +++ b/test/plugins/removeAttrs.05.svg @@ -0,0 +1,21 @@ + + + + + + + + +@@@ + + + + + + + + + +@@@ + +{"attrs":"*:(stroke|fill):((?!^#FFF$).)*"}