Skip to content

Commit

Permalink
Add the preserveCurrentColor option to the removeAttrs plugin. (svg#1000
Browse files Browse the repository at this point in the history
)

Add the preserveCurrentColor option to the removeAttrs plugin.
  • Loading branch information
roblevintennis authored and GreLI committed Feb 24, 2019
1 parent 32558b8 commit 0fe5368
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plugins/removeAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.description = 'removes specified attributes';

exports.params = {
elemSeparator: DEFAULT_SEPARATOR,
preserveCurrentColor: false,
attrs: []
};

Expand All @@ -19,6 +20,9 @@ exports.params = {
* @param elemSeparator
* format: string
*
* @param preserveCurrentColor
* format: boolean
*
* @param attrs:
*
* format: [ element* : attribute* ]
Expand Down Expand Up @@ -69,14 +73,14 @@ exports.params = {
* @author Benny Schudel
*/
exports.fn = function(item, params) {

// wrap into an array if params is not
if (!Array.isArray(params.attrs)) {
params.attrs = [params.attrs];
}

if (item.isElem()) {
var elemSeparator = typeof params.elemSeparator == 'string' ? params.elemSeparator : DEFAULT_SEPARATOR;
var preserveCurrentColor = typeof params.preserveCurrentColor == 'boolean' ? params.preserveCurrentColor : false;

// prepare patterns
var patterns = params.attrs.map(function(pattern) {
Expand Down Expand Up @@ -107,10 +111,15 @@ exports.fn = function(item, params) {
// loop attributes
item.eachAttr(function(attr) {
var name = attr.name;
var value = attr.value;
var isFillCurrentColor = preserveCurrentColor && name == 'fill' && value == 'currentColor';
var isStrokeCurrentColor = preserveCurrentColor && name == 'stroke' && value == 'currentColor';

if (!(isFillCurrentColor || isStrokeCurrentColor)) {
// matches attribute name
if (pattern[1].test(name)) {
item.removeAttr(name);
if (pattern[1].test(name)) {
item.removeAttr(name);
}
}

});
Expand Down
19 changes: 19 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.

0 comments on commit 0fe5368

Please sign in to comment.