Skip to content

Commit e18a92d

Browse files
committed
Added optional value filter
1 parent 102a721 commit e18a92d

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

plugins/removeAttrs.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ exports.params = {
2121
*
2222
* @param attrs:
2323
*
24-
* format: [ element* : attribute* ]
24+
* format: [ element* : attribute* : value* ]
2525
*
26-
* element : regexp (wrapped into ^...$), single * or omitted > all elements
26+
* element : regexp (wrapped into ^...$), single * or omitted > all elements (must be present when value is used)
2727
* attribute : regexp (wrapped into ^...$)
28+
* value : regexp (wrapped into ^...$), single * or omitted > all values
2829
*
2930
* examples:
3031
*
@@ -37,6 +38,10 @@ exports.params = {
3738
* ---
3839
* attrs: 'path:fill'
3940
*
41+
* > remove fill attribute on path element where value is none
42+
* ---
43+
* attrs: 'path:fill:none'
44+
*
4045
*
4146
* > remove all fill and stroke attribute
4247
* ---
@@ -56,6 +61,10 @@ exports.params = {
5661
*
5762
* attrs: '.*:(fill|stroke)'
5863
*
64+
* [is same as]
65+
*
66+
* attrs: '.*:(fill|stroke):.*'
67+
*
5968
*
6069
* > remove all stroke related attributes
6170
* ----
@@ -81,12 +90,16 @@ exports.fn = function(item, params) {
8190
// prepare patterns
8291
var patterns = params.attrs.map(function(pattern) {
8392

84-
// apply to all elements if specifc element is omitted
93+
// if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
8594
if (pattern.indexOf(elemSeparator) === -1) {
86-
pattern = ['.*', elemSeparator, pattern].join('');
95+
pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join('');
96+
97+
// if only 1 separator, assume it's element and attribute name, and apply regardless of attribute value
98+
} else if (pattern.split(elemSeparator).length < 3) {
99+
pattern = [pattern, elemSeparator, '.*'].join('');
87100
}
88101

89-
// create regexps for element and attribute name
102+
// create regexps for element, attribute name, and attribute value
90103
return pattern.split(elemSeparator)
91104
.map(function(value) {
92105

@@ -110,7 +123,11 @@ exports.fn = function(item, params) {
110123

111124
// matches attribute name
112125
if (pattern[1].test(name)) {
113-
item.removeAttr(name);
126+
127+
// matches attribute value
128+
if (pattern[2].test(attr.value)) {
129+
item.removeAttr(name);
130+
}
114131
}
115132

116133
});

test/plugins/removeAttrs.03.svg

Lines changed: 21 additions & 0 deletions
Loading

test/plugins/removeAttrs.04.svg

Lines changed: 21 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)