mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
[removeElementsByAttr] fix removing elements when class is empty
Ref https://github.com/svg/svgo/issues/937 Regexp didn't not cover the case when class list is empty.
This commit is contained in:
@ -7,8 +7,8 @@ exports.active = false;
|
||||
exports.description = 'removes arbitrary elements by ID or className (disabled by default)';
|
||||
|
||||
exports.params = {
|
||||
id: [],
|
||||
class: []
|
||||
id: [],
|
||||
class: []
|
||||
};
|
||||
|
||||
/**
|
||||
@ -51,30 +51,28 @@ exports.params = {
|
||||
* @author Eli Dupuis (@elidupuis)
|
||||
*/
|
||||
exports.fn = function(item, params) {
|
||||
var elemId, elemClass;
|
||||
|
||||
// wrap params in an array if not already
|
||||
['id', 'class'].forEach(function(key) {
|
||||
if (!Array.isArray(params[key])) {
|
||||
params[key] = [ params[key] ];
|
||||
}
|
||||
});
|
||||
|
||||
// abort if current item is no an element
|
||||
if (!item.isElem()) {
|
||||
return;
|
||||
// wrap params in an array if not already
|
||||
['id', 'class'].forEach(function(key) {
|
||||
if (!Array.isArray(params[key])) {
|
||||
params[key] = [ params[key] ];
|
||||
}
|
||||
});
|
||||
|
||||
// remove element if it's `id` matches configured `id` params
|
||||
elemId = item.attr('id');
|
||||
if (elemId) {
|
||||
return params.id.indexOf(elemId.value) === -1;
|
||||
}
|
||||
// abort if current item is no an element
|
||||
if (!item.isElem()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// remove element if it's `class` contains any of the configured `class` params
|
||||
elemClass = item.attr('class');
|
||||
if (elemClass) {
|
||||
var hasClassRegex = new RegExp(params.class.join('|'));
|
||||
return !hasClassRegex.test(elemClass.value);
|
||||
}
|
||||
// remove element if it's `id` matches configured `id` params
|
||||
const elemId = item.attr('id');
|
||||
if (elemId && params.id.length !== 0) {
|
||||
return params.id.includes(elemId.value) === false;
|
||||
}
|
||||
|
||||
// remove element if it's `class` contains any of the configured `class` params
|
||||
const elemClass = item.attr('class');
|
||||
if (elemClass && params.class.length !== 0) {
|
||||
const classList = elemClass.value.split(' ');
|
||||
return params.class.some(item => classList.includes(item)) === false;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user