Skip to content

Commit

Permalink
Drop node.class usages (#1533)
Browse files Browse the repository at this point in the history
We are gonna remove everything outside of xast.
Here's dropped class prop.
  • Loading branch information
TrySound authored Aug 21, 2021
1 parent e4918cc commit 98c023b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
13 changes: 11 additions & 2 deletions plugins/addClassesToSVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ exports.fn = (root, params) => {
element: {
enter: (node, parentNode) => {
if (node.name === 'svg' && parentNode.type === 'root') {
// @ts-ignore class attribute will be just a string eventually
node.class.add(...classNames);
const classList = new Set(
node.attributes.class == null
? null
: node.attributes.class.split(' ')
);
for (const className of classNames) {
if (className != null) {
classList.add(className);
}
}
node.attributes.class = Array.from(classList).join(' ');
}
},
},
Expand Down
14 changes: 10 additions & 4 deletions plugins/inlineStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,19 @@ exports.fn = function (root, opts) {

for (selectedEl of selector.selectedEls) {
// class
var firstSubSelector = selector.item.data.children.first();
const classList = new Set(
selectedEl.attributes.class == null
? null
: selectedEl.attributes.class.split(' ')
);
const firstSubSelector = selector.item.data.children.first();
if (firstSubSelector.type === 'ClassSelector') {
selectedEl.class.remove(firstSubSelector.name);
classList.delete(firstSubSelector.name);
}
// clean up now empty class attributes
if (typeof selectedEl.class.item(0) === 'undefined') {
if (classList.size === 0) {
delete selectedEl.attributes.class;
} else {
selectedEl.attributes.class = Array.from(classList).join(' ');
}

// ID
Expand Down
9 changes: 0 additions & 9 deletions plugins/reusePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ exports.fn = function (root) {
);
root.children[0].spliceContent(0, 0, defsTag);
for (let def of defs) {
// Remove class and style before copying to avoid circular refs in
// JSON.stringify. This is fine because we don't actually want class or
// style information to be copied.
const style = def.style;
const defClass = def.class;
delete def.style;
delete def.class;
const defClone = def.clone();
def.style = style;
def.class = defClass;
delete defClone.attributes.transform;
defsTag.spliceContent(0, 0, defClone);
// Convert the original def to a use so the first usage isn't duplicated.
Expand Down
17 changes: 17 additions & 0 deletions test/plugins/addClassesToSVGElement.03.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 98c023b

Please sign in to comment.