Skip to content

Commit

Permalink
fix(inlineStyles): remove all classes in multiclass selector (#1801)
Browse files Browse the repository at this point in the history
When running into a multi-class selector with `inlineStyles`, it would
only remove the first class of the selector instead of all of them.

This iterates the classes in the selector instead of only taking the
first. Also performs some minor refactors.
  • Loading branch information
SethFalco authored Sep 27, 2023
1 parent b15da27 commit 2539b9f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
34 changes: 12 additions & 22 deletions plugins/inlineStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ const compareSpecificity = (a, b) => {
return 0;
};

/**
* @type {(value: any) => any}
*/
const toAny = (value) => value;

/**
* Moves + merges styles from style elements to element styles
*
Expand Down Expand Up @@ -323,31 +318,27 @@ exports.fn = (root, params) => {
? null
: selectedEl.attributes.class.split(' ')
);
/**
* csstree v2 changed this type
* @type {csstree.CssNode}
*/
const firstSubSelector = toAny(selector.node.children.first);
if (
firstSubSelector != null &&
firstSubSelector.type === 'ClassSelector'
) {
classList.delete(firstSubSelector.name);

for (const child of selector.node.children) {
if (child.type === 'ClassSelector') {
classList.delete(child.name);
}
}

if (classList.size === 0) {
delete selectedEl.attributes.class;
} else {
selectedEl.attributes.class = Array.from(classList).join(' ');
}

// ID
const firstSubSelector = selector.node.children.first;
if (
firstSubSelector != null &&
firstSubSelector.type === 'IdSelector'
firstSubSelector.type === 'IdSelector' &&
selectedEl.attributes.id === firstSubSelector.name
) {
if (selectedEl.attributes.id === firstSubSelector.name) {
delete selectedEl.attributes.id;
}
delete selectedEl.attributes.id;
}
}
}
Expand All @@ -360,16 +351,15 @@ exports.fn = (root, params) => {
if (
node.type === 'Rule' &&
node.prelude.type === 'SelectorList' &&
// csstree v2 changed this type
toAny(node.prelude.children.isEmpty)
node.prelude.children.isEmpty
) {
list.remove(item);
}
},
});

// csstree v2 changed this type
if (toAny(style.cssAst.children.isEmpty)) {
if (style.cssAst.children.isEmpty) {
// remove emtpy style element
detachNodeFromParent(style.node, style.parentNode);
} else {
Expand Down
24 changes: 24 additions & 0 deletions test/plugins/inlineStyles.21.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/plugins/inlineStyles.22.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 2539b9f

Please sign in to comment.