Skip to content

Commit

Permalink
removeAttrs: add optional value filter (svg#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Feb 24, 2019
1 parent 5d97108 commit d23355e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
29 changes: 23 additions & 6 deletions plugins/removeAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand All @@ -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
* ---
Expand All @@ -60,6 +65,10 @@ exports.params = {
*
* attrs: '.*:(fill|stroke)'
*
* [is same as]
*
* attrs: '.*:(fill|stroke):.*'
*
*
* > remove all stroke related attributes
* ----
Expand All @@ -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) {

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/plugins/removeAttrs.04.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions test/plugins/removeAttrs.05.svg
Loading
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 d23355e

Please sign in to comment.