Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inlineStyles): remove all classes in multiclass selector #1801

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading