Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removeAttrs: Added optional value filter #977

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions plugins/removeAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,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 @@ -37,6 +38,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 @@ -56,6 +61,10 @@ exports.params = {
*
* attrs: '.*:(fill|stroke)'
*
* [is same as]
*
* attrs: '.*:(fill|stroke):.*'
*
*
* > remove all stroke related attributes
* ----
Expand All @@ -81,12 +90,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*
Herman-Freund marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -110,7 +123,11 @@ exports.fn = function(item, params) {

// 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.03.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.04.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.