mirror of
https://github.com/svg/svgo.git
synced 2025-08-07 15:22:54 +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.description = 'removes arbitrary elements by ID or className (disabled by default)';
|
||||||
|
|
||||||
exports.params = {
|
exports.params = {
|
||||||
id: [],
|
id: [],
|
||||||
class: []
|
class: []
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,30 +51,28 @@ exports.params = {
|
|||||||
* @author Eli Dupuis (@elidupuis)
|
* @author Eli Dupuis (@elidupuis)
|
||||||
*/
|
*/
|
||||||
exports.fn = function(item, params) {
|
exports.fn = function(item, params) {
|
||||||
var elemId, elemClass;
|
// wrap params in an array if not already
|
||||||
|
['id', 'class'].forEach(function(key) {
|
||||||
// wrap params in an array if not already
|
if (!Array.isArray(params[key])) {
|
||||||
['id', 'class'].forEach(function(key) {
|
params[key] = [ params[key] ];
|
||||||
if (!Array.isArray(params[key])) {
|
|
||||||
params[key] = [ params[key] ];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// abort if current item is no an element
|
|
||||||
if (!item.isElem()) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// remove element if it's `id` matches configured `id` params
|
// abort if current item is no an element
|
||||||
elemId = item.attr('id');
|
if (!item.isElem()) {
|
||||||
if (elemId) {
|
return;
|
||||||
return params.id.indexOf(elemId.value) === -1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// remove element if it's `class` contains any of the configured `class` params
|
// remove element if it's `id` matches configured `id` params
|
||||||
elemClass = item.attr('class');
|
const elemId = item.attr('id');
|
||||||
if (elemClass) {
|
if (elemId && params.id.length !== 0) {
|
||||||
var hasClassRegex = new RegExp(params.class.join('|'));
|
return params.id.includes(elemId.value) === false;
|
||||||
return !hasClassRegex.test(elemClass.value);
|
}
|
||||||
}
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
9
test/plugins/removeElementsByAttr.07.svg
Normal file
9
test/plugins/removeElementsByAttr.07.svg
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19">
|
||||||
|
<rect class="some-class" width="19" height="19"/>
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19">
|
||||||
|
<rect class="some-class" width="19" height="19"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 296 B |
Reference in New Issue
Block a user