mirror of
https://github.com/svg/svgo.git
synced 2025-08-01 18:46:52 +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: {
|
||||
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(' ');
|
||||
}
|
||||
},
|
||||
},
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
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