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:
@@ -51,8 +51,6 @@ 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])) {
|
||||
@@ -66,15 +64,15 @@ exports.fn = function(item, params) {
|
||||
}
|
||||
|
||||
// remove element if it's `id` matches configured `id` params
|
||||
elemId = item.attr('id');
|
||||
if (elemId) {
|
||||
return params.id.indexOf(elemId.value) === -1;
|
||||
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
|
||||
elemClass = item.attr('class');
|
||||
if (elemClass) {
|
||||
var hasClassRegex = new RegExp(params.class.join('|'));
|
||||
return !hasClassRegex.test(elemClass.value);
|
||||
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