schnee effeckt und fehler Korektur
This commit is contained in:
162
node_modules/svgo/plugins/removeAttrs.js
generated
vendored
162
node_modules/svgo/plugins/removeAttrs.js
generated
vendored
@@ -1,26 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var ELEM_SEP = ':';
|
||||
|
||||
exports.type = 'perItem';
|
||||
|
||||
exports.name = 'removeAttrs';
|
||||
exports.type = 'visitor';
|
||||
exports.active = false;
|
||||
|
||||
exports.description = 'removes specified attributes';
|
||||
|
||||
exports.params = {
|
||||
attrs: []
|
||||
};
|
||||
const DEFAULT_SEPARATOR = ':';
|
||||
const ENOATTRS = `Warning: The plugin "removeAttrs" requires the "attrs" parameter.
|
||||
It should have a pattern to remove, otherwise the plugin is a noop.
|
||||
Config example:
|
||||
|
||||
plugins: [
|
||||
{
|
||||
name: "removeAttrs",
|
||||
params: {
|
||||
attrs: "(fill|stroke)"
|
||||
}
|
||||
}
|
||||
]
|
||||
`;
|
||||
|
||||
/**
|
||||
* Remove attributes
|
||||
*
|
||||
* @param attrs:
|
||||
* @example elemSeparator
|
||||
* format: string
|
||||
*
|
||||
* format: [ element* : attribute* ]
|
||||
* @example preserveCurrentColor
|
||||
* format: boolean
|
||||
*
|
||||
* element : regexp (wrapped into ^...$), single * or omitted > all elements
|
||||
* @example attrs:
|
||||
*
|
||||
* format: [ element* : attribute* : value* ]
|
||||
*
|
||||
* 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:
|
||||
*
|
||||
@@ -33,6 +48,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
|
||||
* ---
|
||||
@@ -52,68 +71,89 @@ exports.params = {
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* @type {import('../lib/types').Plugin<{
|
||||
* elemSeparator?: string,
|
||||
* preserveCurrentColor?: boolean,
|
||||
* attrs: string | Array<string>
|
||||
* }>}
|
||||
*/
|
||||
exports.fn = function(item, params) {
|
||||
exports.fn = (root, params) => {
|
||||
if (typeof params.attrs == 'undefined') {
|
||||
console.warn(ENOATTRS);
|
||||
return null;
|
||||
}
|
||||
|
||||
// wrap into an array if params is not
|
||||
if (!Array.isArray(params.attrs)) {
|
||||
params.attrs = [params.attrs];
|
||||
}
|
||||
const elemSeparator =
|
||||
typeof params.elemSeparator == 'string'
|
||||
? params.elemSeparator
|
||||
: DEFAULT_SEPARATOR;
|
||||
const preserveCurrentColor =
|
||||
typeof params.preserveCurrentColor == 'boolean'
|
||||
? params.preserveCurrentColor
|
||||
: false;
|
||||
const attrs = Array.isArray(params.attrs) ? params.attrs : [params.attrs];
|
||||
|
||||
if (item.isElem()) {
|
||||
return {
|
||||
element: {
|
||||
enter: (node) => {
|
||||
for (let pattern of attrs) {
|
||||
// if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
|
||||
if (pattern.includes(elemSeparator) === false) {
|
||||
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('');
|
||||
}
|
||||
|
||||
// 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, attribute name, and attribute value
|
||||
const list = pattern.split(elemSeparator).map((value) => {
|
||||
// adjust single * to match anything
|
||||
if (value === '*') {
|
||||
value = '.*';
|
||||
}
|
||||
return new RegExp(['^', value, '$'].join(''), 'i');
|
||||
});
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// matches element
|
||||
if (list[0].test(node.name)) {
|
||||
// loop attributes
|
||||
for (const [name, value] of Object.entries(node.attributes)) {
|
||||
const isFillCurrentColor =
|
||||
preserveCurrentColor &&
|
||||
name == 'fill' &&
|
||||
value == 'currentColor';
|
||||
const isStrokeCurrentColor =
|
||||
preserveCurrentColor &&
|
||||
name == 'stroke' &&
|
||||
value == 'currentColor';
|
||||
if (
|
||||
!isFillCurrentColor &&
|
||||
!isStrokeCurrentColor &&
|
||||
// matches attribute name
|
||||
list[1].test(name) &&
|
||||
// matches attribute value
|
||||
list[2].test(value)
|
||||
) {
|
||||
delete node.attributes[name];
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user