Skip to content

Commit

Permalink
[removeElementsByAttr] fix removing elements when class is empty
Browse files Browse the repository at this point in the history
Ref #937

Regexp didn't not cover the case when class list is empty.
  • Loading branch information
TrySound committed Feb 20, 2021
1 parent 4490d62 commit d9f68d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
48 changes: 23 additions & 25 deletions plugins/removeElementsByAttr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
};

/**
Expand Down Expand Up @@ -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;
}
};
9 changes: 9 additions & 0 deletions test/plugins/removeElementsByAttr.07.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d9f68d3

Please sign in to comment.