Skip to content

Commit 2eacfd8

Browse files
committed
feat(emSize): add support for missing width/height
1 parent 492e79d commit 2eacfd8

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/cli/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`cli --icon 1`] = `
44
"import React from \\"react\\";
55
66
const One = props => (
7-
<svg width=\\"1em\\" height=\\"1em\\" viewBox=\\"0 0 48 1\\" {...props}>
7+
<svg viewBox=\\"0 0 48 1\\" width=\\"1em\\" height=\\"1em\\" {...props}>
88
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
99
</svg>
1010
);

src/h2x/emSize.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
import { JSXAttribute } from 'h2x-plugin-jsx'
2+
3+
const makeSizeAttr = name => {
4+
const attr = new JSXAttribute()
5+
attr.name = name
6+
attr.value = '1em'
7+
attr.litteral = false
8+
return attr
9+
}
10+
111
const emSize = () => ({
212
visitor: {
3-
JSXAttribute: {
13+
JSXElement: {
414
enter(path) {
5-
if (
6-
path.parent.name === 'svg' &&
7-
(path.node.name === 'width' || path.node.name === 'height')
8-
) {
9-
path.node.value = '1em'
10-
path.node.litteral = false
15+
if (path.node.name === 'svg' && !path.node.attributes.some(attr => attr && attr.name === 'width' && attr.value === '1em') && !path.node.attributes.some(attr => attr && attr.name === 'height' && attr.value === '1em')) {
16+
const nextAttrs = path.node.attributes.filter(attr => attr.name !== 'width' && attr.name !== 'height');
17+
nextAttrs.push(makeSizeAttr('width'));
18+
nextAttrs.push(makeSizeAttr('height'));
19+
path.node.attributes = nextAttrs;
20+
path.replace(path.node);
1121
}
1222
},
1323
},

0 commit comments

Comments
 (0)