Skip to content

Commit 0c42934

Browse files
committed
Make sure path normalization happens on the first pass
1 parent efd2cc1 commit 0c42934

File tree

1 file changed

+15
-54
lines changed

1 file changed

+15
-54
lines changed

scripts/check_icons.js

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ function checkIcons() {
6868
const iconIds = {};
6969
const iconIdPartsObj = {};
7070

71-
globSync(`./icons/**/*.svg`).forEach(file => {
71+
globSync(`./icons/**/*.svg`).forEach(cleanSvgFile);
72+
73+
function cleanSvgFile(file) {
7274
const contents = readFileSync(file, 'utf8');
7375
let xml;
7476
try {
@@ -135,68 +137,33 @@ function checkIcons() {
135137

136138
// Checks for deeper levels
137139
} else {
138-
// convert ellipses to paths
139140
if (node.nodeName === 'ellipse' || node.nodeName === 'circle') {
140141
const attr = (name) => parseFloat(node.getAttribute(name));
141142
pathDataToAdd.add(ellipseAttrsToPathD(attr('rx') || attr('r'), attr('cx'), attr('ry') || attr('r'), attr('cy')));
142-
childrenToRemove.add(child);
143-
return;
144-
// convert rects to paths
145143
} else if (node.nodeName === 'rect') {
146144
const attr = (name) => node.getAttribute(name);
147145
pathDataToAdd.add(rectAttrsToPathD(attr));
148-
childrenToRemove.add(child);
149-
return;
150-
// convert polygons to paths
151146
} else if (node.nodeName === 'polygon') {
152147
pathDataToAdd.add('M ' + node.getAttribute('points') + 'z');
153-
childrenToRemove.add(child);
154-
return;
155-
// remove metadata nodes
156-
} else if (node.nodeName === 'title' || node.nodeName === 'desc') {
157-
childrenToRemove.add(child);
158-
return;
159-
} else if (node.nodeName === 'g') {
160-
// groups will be emptied so remove them
161-
childrenToRemove.add(child);
162-
return;
163-
}
164-
165-
// Remove unwanted attributes
166-
child.removeAtt(['fill', 'fill-rule', 'id', 'xmlns']);
167-
168-
if (level > 2) {
169-
let parent = child.up();
170-
if (parent.node.nodeName === 'g') {
171-
// move the node out of the group
172-
pathDataToAdd.add(child.toString());
173-
childrenToRemove.add(child);
174-
}
175-
}
176-
177-
// suspicious elements
178-
if (node.nodeName !== 'path') {
148+
} else if (node.nodeName === 'path') {
149+
pathDataToAdd.add(node.getAttribute('d'));
150+
} else if (node.nodeName !== 'title' && node.nodeName !== 'desc' && node.nodeName !== 'g') {
179151
warnings.push(chalk.yellow('Warning - Suspicious node: ' + node.nodeName));
180152
warnings.push(chalk.gray(' Each svg element should contain only one or more "path" elements.'));
181153
return;
182154
}
183155

156+
childrenToRemove.add(child);
157+
184158
// suspicious attributes
185-
let suspicious = node.attributes
159+
const suspiciousAttrs = node.attributes
186160
.map(attr => attr.name)
187161
.filter(name => name !== 'd');
188162

189-
if (suspicious.length) {
190-
warnings.push(chalk.yellow('Warning - Suspicious attributes on ' + node.nodeName + ': ' + suspicious));
163+
if (suspiciousAttrs.length) {
164+
warnings.push(chalk.yellow('Warning - Suspicious attributes on ' + node.nodeName + ': ' + suspiciousAttrs));
191165
warnings.push(chalk.gray(' Avoid identifiers, style, and presentation attributes.'));
192-
}
193-
194-
// normalize path data
195-
const d = node.getAttribute('d');
196-
let pathdata = d && svgPathParse.pathParse(d);
197-
if (pathdata) {
198-
pathdata = pathdata.normalize({round: 2});
199-
node.setAttribute('d', svgPathParse.serializePath(pathdata));
166+
return;
200167
}
201168
}
202169

@@ -207,26 +174,20 @@ function checkIcons() {
207174
child.remove();
208175
});
209176

210-
Array.from(pathDataToAdd).forEach((pathData) => {
211-
if (pathData[0] === '<') {
212-
xml.root().ele(pathData);
213-
} else {
177+
Array.from(pathDataToAdd).forEach(d => {
214178
xml.root().ele('path', {
215-
d: pathData
179+
d: svgPathParse.serializePath(svgPathParse.pathParse(d).normalize({round: 2}))
216180
});
217-
}
218181
});
219182

220-
221183
if (warnings.length) {
222184
warnings.forEach(w => console.warn(w));
223185
console.warn(' in ' + file);
224186
console.warn('');
225187
}
226188

227189
writeFileSync(file, xml.end({ prettyPrint: true }));
228-
229-
});
190+
}
230191

231192
// const iconIdParts = Object.keys(iconIdPartsObj).sort();
232193
// iconIdParts

0 commit comments

Comments
 (0)