1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

Drop node.class usages (#1533)

We are gonna remove everything outside of xast.
Here's dropped class prop.
This commit is contained in:
Bogdan Chadkin
2021-08-21 19:42:32 +03:00
committed by GitHub
parent e4918ccdd1
commit 98c023bdb0
4 changed files with 38 additions and 15 deletions

View File

@ -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(' ');
}
},
},

View File

@ -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

View File

@ -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.

View File

@ -0,0 +1,17 @@
Should avoid adding existing classes
===
<svg xmlns="http://www.w3.org/2000/svg" class="mySvg">
test
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg" class="mySvg size-big">
test
</svg>
@@@
{"classNames":["mySvg","size-big"]}