-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'removeAttrsPlugin' of git://github.com/bennyschudel/svg…
…o into removeAttrsPlugin
- Loading branch information
Showing
7 changed files
with
172 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,5 @@ plugins: | |
- removeTitle | ||
- removeDesc | ||
- removeDimensions | ||
- removeAttrs | ||
- addClassesToSVGElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict'; | ||
|
||
var ELEM_SEP = ':'; | ||
|
||
exports.type = 'perItem'; | ||
|
||
exports.active = false; | ||
|
||
exports.params = { | ||
attrs: [] | ||
}; | ||
|
||
/** | ||
* Remove attributes | ||
* | ||
* @param attrs: | ||
* | ||
* format: [ element* : attribute* ] | ||
* | ||
* element : regexp (wrapped into ^...$), single * or omitted > all elements | ||
* attribute : regexp (wrapped into ^...$) | ||
* | ||
* examples: | ||
* | ||
* > remove fill attribute on path element | ||
* --- | ||
* attrs: 'path:fill' | ||
* | ||
* | ||
* > remove all fill and stroke attribute | ||
* --- | ||
* attrs: | ||
* - 'fill' | ||
* - 'stroke' | ||
* | ||
* [is same as] | ||
* | ||
* attrs: '(fill|stroke)' | ||
* | ||
* [is same as] | ||
* | ||
* attrs: '*:(fill|stroke)' | ||
* | ||
* [is same as] | ||
* | ||
* attrs: '.*:(fill|stroke)' | ||
* | ||
* | ||
* > remove all stroke related attributes | ||
* ---- | ||
* attrs: 'stroke.*' | ||
* | ||
* | ||
* @param {Object} item current iteration item | ||
* @param {Object} params plugin params | ||
* @return {Boolean} if false, item will be filtered out | ||
* | ||
* @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()) { | ||
|
||
// prepare patterns | ||
var patterns = params.attrs.map(function(pattern) { | ||
|
||
// apply to all elements if specifc element is omitted | ||
if (pattern.indexOf(ELEM_SEP) === -1) { | ||
pattern = ['.*', ELEM_SEP, pattern].join(''); | ||
} | ||
|
||
// create regexps for element and attribute name | ||
return pattern.split(ELEM_SEP) | ||
.map(function(value) { | ||
|
||
// adjust single * to match anything | ||
if (value === '*') { value = '.*'; } | ||
|
||
return new RegExp(['^', value, '$'].join(''), 'i'); | ||
}); | ||
|
||
}); | ||
|
||
// loop patterns | ||
patterns.forEach(function(pattern) { | ||
|
||
// matches element | ||
if (pattern[0].test(item.elem)) { | ||
|
||
// loop attributes | ||
item.eachAttr(function(attr) { | ||
var name = attr.name; | ||
|
||
// matches attribute name | ||
if (pattern[1].test(name)) { | ||
item.removeAttr(name); | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.