Skip to content

Commit

Permalink
Add tests and fix for onlyMatchedOnce option cleaning classes/IDs tha…
Browse files Browse the repository at this point in the history
…t matched more than once.

Improve code.
  • Loading branch information
strarsis authored and GreLI committed Nov 3, 2017
1 parent 00ca73b commit e25cf30
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 21 deletions.
50 changes: 29 additions & 21 deletions plugins/inlineStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,42 @@ exports.fn = function(document, opts) {
}


if (opts.removeMatchedSelectors) {
// clean up matched class + ID attribute values
for (selector of sortedSelectors) {
if(!selector.selectedEls) {
continue;
}
if (!opts.removeMatchedSelectors) {
return document; // no further processing required
}

for (selectedEl of selector.selectedEls) {
// class
var firstSubSelector = selector.item.data.children.first();
if(firstSubSelector.type === 'ClassSelector') {
selectedEl.class.remove(firstSubSelector.name);
}
// clean up now empty class attributes
if(typeof selectedEl.class.item(0) === 'undefined') {
selectedEl.removeAttr('class');
}

// ID
if(firstSubSelector.type === 'IdSelector') {
selectedEl.removeAttr('id', firstSubSelector.name);
}
// clean up matched class + ID attribute values
for (selector of sortedSelectors) {
if(!selector.selectedEls) {
continue;
}

if (opts.onlyMatchedOnce && selector.selectedEls !== null && selector.selectedEls.length > 1) {
// skip selectors that match more than once if option onlyMatchedOnce is enabled
continue;
}

for (selectedEl of selector.selectedEls) {
// class
var firstSubSelector = selector.item.data.children.first();
if(firstSubSelector.type === 'ClassSelector') {
selectedEl.class.remove(firstSubSelector.name);
}
// clean up now empty class attributes
if(typeof selectedEl.class.item(0) === 'undefined') {
selectedEl.removeAttr('class');
}

// ID
if(firstSubSelector.type === 'IdSelector') {
selectedEl.removeAttr('id', firstSubSelector.name);
}
}
}


// clean up elements
// clean up now empty elements
for (var style of styles) {
csstree.walkRules(style.cssAst, function(node, item, list) {
// clean up <style/> atrules without any rulesets left
Expand Down
38 changes: 38 additions & 0 deletions test/plugins/inlineStyles.18.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions test/plugins/inlineStyles.19.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 e25cf30

Please sign in to comment.