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:
@ -69,8 +69,17 @@ exports.fn = (root, params) => {
|
|||||||
element: {
|
element: {
|
||||||
enter: (node, parentNode) => {
|
enter: (node, parentNode) => {
|
||||||
if (node.name === 'svg' && parentNode.type === 'root') {
|
if (node.name === 'svg' && parentNode.type === 'root') {
|
||||||
// @ts-ignore class attribute will be just a string eventually
|
const classList = new Set(
|
||||||
node.class.add(...classNames);
|
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(' ');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -207,13 +207,19 @@ exports.fn = function (root, opts) {
|
|||||||
|
|
||||||
for (selectedEl of selector.selectedEls) {
|
for (selectedEl of selector.selectedEls) {
|
||||||
// class
|
// 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') {
|
if (firstSubSelector.type === 'ClassSelector') {
|
||||||
selectedEl.class.remove(firstSubSelector.name);
|
classList.delete(firstSubSelector.name);
|
||||||
}
|
}
|
||||||
// clean up now empty class attributes
|
if (classList.size === 0) {
|
||||||
if (typeof selectedEl.class.item(0) === 'undefined') {
|
|
||||||
delete selectedEl.attributes.class;
|
delete selectedEl.attributes.class;
|
||||||
|
} else {
|
||||||
|
selectedEl.attributes.class = Array.from(classList).join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ID
|
// ID
|
||||||
|
@ -62,16 +62,7 @@ exports.fn = function (root) {
|
|||||||
);
|
);
|
||||||
root.children[0].spliceContent(0, 0, defsTag);
|
root.children[0].spliceContent(0, 0, defsTag);
|
||||||
for (let def of defs) {
|
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();
|
const defClone = def.clone();
|
||||||
def.style = style;
|
|
||||||
def.class = defClass;
|
|
||||||
delete defClone.attributes.transform;
|
delete defClone.attributes.transform;
|
||||||
defsTag.spliceContent(0, 0, defClone);
|
defsTag.spliceContent(0, 0, defClone);
|
||||||
// Convert the original def to a use so the first usage isn't duplicated.
|
// Convert the original def to a use so the first usage isn't duplicated.
|
||||||
|
17
test/plugins/addClassesToSVGElement.03.svg
Normal file
17
test/plugins/addClassesToSVGElement.03.svg
Normal 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"]}
|
Reference in New Issue
Block a user