2020-10-17 14:01:56 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2019-07-04 00:33:03 +03:00
|
|
|
const data = require("./_data/simple-icons.json");
|
|
|
|
const { htmlFriendlyToTitle } = require("./scripts/utils.js");
|
2020-12-13 22:29:01 +03:00
|
|
|
const svgPath = require("svgpath");
|
2020-11-23 13:36:10 +03:00
|
|
|
const { svgPathBbox } = require("svg-path-bbox");
|
2019-07-04 00:33:03 +03:00
|
|
|
|
|
|
|
const titleRegexp = /(.+) icon$/;
|
2020-07-05 16:57:00 +03:00
|
|
|
const svgRegexp = /^<svg( [^\s]*=".*"){3}><title>.*<\/title><path d=".*"\/><\/svg>\r?\n?$/;
|
2020-12-14 22:35:27 +03:00
|
|
|
const negativeZerosRegexp = /-0(?=[^\.]|[\s\d\w]|$)/g;
|
2020-06-22 19:24:56 +03:00
|
|
|
|
2020-06-10 12:59:42 +03:00
|
|
|
const iconSize = 24;
|
|
|
|
const iconFloatPrecision = 3;
|
2020-11-19 23:49:49 +03:00
|
|
|
const iconMaxFloatPrecision = 5;
|
2020-07-10 11:06:08 +03:00
|
|
|
const iconTolerance = 0.001;
|
2020-10-17 14:01:56 +03:00
|
|
|
|
|
|
|
// set env SI_UPDATE_IGNORE to recreate the ignore file
|
|
|
|
const updateIgnoreFile = process.env.SI_UPDATE_IGNORE === 'true'
|
|
|
|
const ignoreFile = "./.svglint-ignored.json";
|
|
|
|
const iconIgnored = !updateIgnoreFile ? require(ignoreFile) : {};
|
|
|
|
|
|
|
|
function sortObjectByKey(obj) {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.sort()
|
|
|
|
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortObjectByValue(obj) {
|
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.sort((a, b) => ('' + obj[a]).localeCompare(obj[b]))
|
|
|
|
.reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {});
|
|
|
|
}
|
|
|
|
|
2020-12-13 22:29:01 +03:00
|
|
|
function removeLeadingZeros(number) {
|
|
|
|
// convert 0.03 to '.03'
|
|
|
|
return number.toString().replace(/^(-?)(0)(\.?.+)/, '$1$3');
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:01:56 +03:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
process.on('exit', () => {
|
|
|
|
// ensure object output order is consistent due to async svglint processing
|
|
|
|
const sorted = sortObjectByKey(iconIgnored)
|
|
|
|
for (const linterName in sorted) {
|
|
|
|
sorted[linterName] = sortObjectByValue(sorted[linterName])
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
ignoreFile,
|
|
|
|
JSON.stringify(sorted, null, 2) + '\n',
|
|
|
|
{flag: 'w'}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function isIgnored(linterName, path) {
|
2020-12-13 22:29:01 +03:00
|
|
|
return iconIgnored[linterName] && iconIgnored[linterName].hasOwnProperty(path);
|
2020-10-17 14:01:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function ignoreIcon(linterName, path, $) {
|
|
|
|
if (!iconIgnored[linterName]) {
|
|
|
|
iconIgnored[linterName] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const title = $.find("title").text().replace(/(.*) icon/, '$1');
|
|
|
|
const iconName = htmlFriendlyToTitle(title);
|
|
|
|
|
|
|
|
iconIgnored[linterName][path] = iconName;
|
|
|
|
}
|
2019-07-04 00:33:03 +03:00
|
|
|
|
2018-08-16 12:33:32 +03:00
|
|
|
module.exports = {
|
|
|
|
rules: {
|
|
|
|
elm: {
|
|
|
|
"svg": 1,
|
|
|
|
"svg > title": 1,
|
2019-03-28 22:40:31 +03:00
|
|
|
"svg > path": 1,
|
|
|
|
"*": false,
|
2018-08-16 12:33:32 +03:00
|
|
|
},
|
|
|
|
attr: [
|
|
|
|
{ // ensure that the SVG elm has the appropriate attrs
|
|
|
|
"role": "img",
|
2020-06-10 12:59:42 +03:00
|
|
|
"viewBox": `0 0 ${iconSize} ${iconSize}`,
|
2018-08-16 12:33:32 +03:00
|
|
|
"xmlns": "http://www.w3.org/2000/svg",
|
|
|
|
"rule::selector": "svg",
|
|
|
|
"rule::whitelist": true,
|
|
|
|
},
|
|
|
|
{ // ensure that the title elm has the appropriate attr
|
|
|
|
"rule::selector": "svg > title",
|
|
|
|
"rule::whitelist": true,
|
2019-03-20 10:55:03 +03:00
|
|
|
},
|
|
|
|
{ // ensure that the path element only has the 'd' attr (no style, opacity, etc.)
|
|
|
|
"d": /^[,a-zA-Z0-9\. -]+$/,
|
|
|
|
"rule::selector": "svg > path",
|
|
|
|
"rule::whitelist": true,
|
2018-08-16 12:33:32 +03:00
|
|
|
}
|
2019-07-04 00:33:03 +03:00
|
|
|
],
|
|
|
|
custom: [
|
|
|
|
function(reporter, $, ast) {
|
2020-06-10 12:59:42 +03:00
|
|
|
reporter.name = "icon-title";
|
|
|
|
|
2019-07-04 00:33:03 +03:00
|
|
|
const iconTitleText = $.find("title").text();
|
|
|
|
if (!titleRegexp.test(iconTitleText)) {
|
|
|
|
reporter.error("<title> should follow the format \"[ICON_NAME] icon\"");
|
|
|
|
} else {
|
|
|
|
const titleMatch = iconTitleText.match(titleRegexp);
|
|
|
|
// titleMatch = [ "[ICON_NAME] icon", "[ICON_NAME]" ]
|
|
|
|
const rawIconName = titleMatch[1];
|
|
|
|
const iconName = htmlFriendlyToTitle(rawIconName);
|
|
|
|
const icon = data.icons.find(icon => icon.title === iconName);
|
|
|
|
if (icon === undefined) {
|
|
|
|
reporter.error(`No icon with title "${iconName}" found in simple-icons.json`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-10 12:59:42 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-size";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
2020-10-17 14:01:56 +03:00
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
2020-06-10 12:59:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:36:10 +03:00
|
|
|
const [minX, minY, maxX, maxY] = svgPathBbox(iconPath);
|
|
|
|
const width = +(maxX - minX).toFixed(iconFloatPrecision);
|
|
|
|
const height = +(maxY - minY).toFixed(iconFloatPrecision);
|
2020-06-10 12:59:42 +03:00
|
|
|
|
|
|
|
if (width === 0 && height === 0) {
|
|
|
|
reporter.error("Path bounds were reported as 0 x 0; check if the path is valid");
|
2020-10-17 14:01:56 +03:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-06-10 12:59:42 +03:00
|
|
|
} else if (width !== iconSize && height !== iconSize) {
|
|
|
|
reporter.error(`Size of <path> must be exactly ${iconSize} in one dimension; the size is currently ${width} x ${height}`);
|
2020-10-17 14:01:56 +03:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-06-10 12:59:42 +03:00
|
|
|
}
|
|
|
|
},
|
2020-11-19 23:49:49 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-precision";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-13 22:29:01 +03:00
|
|
|
|
2020-12-22 17:39:08 +03:00
|
|
|
const { segments } = svgPath(iconPath);
|
2020-11-19 23:49:49 +03:00
|
|
|
const segmentParts = segments.flat().filter((num) => (typeof num === 'number'));
|
|
|
|
|
|
|
|
const countDecimals = (num) => {
|
|
|
|
if (num && num % 1) {
|
|
|
|
let [base, op, trail] = num.toExponential().split(/e([+-])/);
|
|
|
|
let elen = parseInt(trail, 10);
|
|
|
|
let idx = base.indexOf('.');
|
|
|
|
return idx == -1 ? elen : base.length - idx - 1 + (op === '+' ? -elen : elen);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
const precisionArray = segmentParts.map(countDecimals);
|
|
|
|
const precisionMax = precisionArray && precisionArray.length > 0 ?
|
|
|
|
Math.max(...precisionArray) :
|
|
|
|
0;
|
|
|
|
|
|
|
|
if (precisionMax > iconMaxFloatPrecision) {
|
|
|
|
reporter.error(`Maximum precision should not be greater than ${iconMaxFloatPrecision}; it is currently ${precisionMax}`);
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-12-13 22:29:01 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "ineffective-segments";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:39:08 +03:00
|
|
|
const parsedPath = svgPath(iconPath);
|
|
|
|
const { segments } = parsedPath;
|
|
|
|
const { segments: absSegments } = parsedPath.abs().unshort();
|
2020-12-13 22:29:01 +03:00
|
|
|
|
|
|
|
const lowerMovementCommands = ['m', 'l'];
|
|
|
|
const lowerDirectionCommands = ['h', 'v'];
|
|
|
|
const lowerCurveCommand = 'c';
|
|
|
|
const lowerShorthandCurveCommand = 's';
|
|
|
|
const lowerCurveCommands = [lowerCurveCommand, lowerShorthandCurveCommand];
|
|
|
|
const upperMovementCommands = ['M', 'L'];
|
|
|
|
const upperHorDirectionCommand = 'H';
|
|
|
|
const upperVerDirectionCommand = 'V';
|
|
|
|
const upperDirectionCommands = [upperHorDirectionCommand, upperVerDirectionCommand];
|
|
|
|
const upperCurveCommand = 'C';
|
|
|
|
const upperShorthandCurveCommand = 'S';
|
|
|
|
const upperCurveCommands = [upperCurveCommand, upperShorthandCurveCommand];
|
|
|
|
const curveCommands = [...lowerCurveCommands, ...upperCurveCommands];
|
|
|
|
const commands = [...lowerMovementCommands, ...lowerDirectionCommands, ...upperMovementCommands, ...upperDirectionCommands, ...curveCommands];
|
|
|
|
const getInvalidSegments = ([command, x1Coord, y1Coord, ...rest], index) => {
|
|
|
|
if (commands.includes(command)) {
|
|
|
|
// Relative directions (h or v) having a length of 0
|
|
|
|
if (lowerDirectionCommands.includes(command) && x1Coord === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Relative movement (m or l) having a distance of 0
|
|
|
|
if (lowerMovementCommands.includes(command) && x1Coord === 0 && y1Coord === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (lowerCurveCommands.includes(command) && x1Coord === 0 && y1Coord === 0) {
|
|
|
|
const [x2Coord, y2Coord] = rest;
|
|
|
|
if (
|
|
|
|
// Relative shorthand curve (s) having a control point of 0
|
|
|
|
command === lowerShorthandCurveCommand ||
|
|
|
|
// Relative bézier curve (c) having control points of 0
|
|
|
|
(command === lowerCurveCommand && x2Coord === 0 && y2Coord === 0)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index > 0) {
|
|
|
|
let [yPrevCoord, xPrevCoord, ...prevRest] = [...absSegments[index - 1]].reverse();
|
|
|
|
// If the previous command was a direction one, we need to iterate back until we find the missing coordinates
|
|
|
|
if (upperDirectionCommands.includes(xPrevCoord)) {
|
|
|
|
xPrevCoord = undefined;
|
|
|
|
yPrevCoord = undefined;
|
|
|
|
let idx = index;
|
|
|
|
while (--idx > 0 && (xPrevCoord === undefined || yPrevCoord === undefined)) {
|
|
|
|
let [yPrevCoordDeep, xPrevCoordDeep, ...rest] = [...absSegments[idx]].reverse();
|
|
|
|
// If the previous command was a horizontal movement, we need to consider the single coordinate as x
|
|
|
|
if (upperHorDirectionCommand === xPrevCoordDeep) {
|
|
|
|
xPrevCoordDeep = yPrevCoordDeep;
|
|
|
|
yPrevCoordDeep = undefined;
|
|
|
|
}
|
|
|
|
// If the previous command was a vertical movement, we need to consider the single coordinate as y
|
|
|
|
if (upperVerDirectionCommand === xPrevCoordDeep) {
|
|
|
|
xPrevCoordDeep = undefined;
|
|
|
|
}
|
|
|
|
if (xPrevCoord === undefined && xPrevCoordDeep !== undefined) {
|
|
|
|
xPrevCoord = xPrevCoordDeep;
|
|
|
|
}
|
|
|
|
if (yPrevCoord === undefined && yPrevCoordDeep !== undefined) {
|
|
|
|
yPrevCoord = yPrevCoordDeep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upperCurveCommands.includes(command)) {
|
|
|
|
const [x2Coord, y2Coord, xCoord, yCoord] = rest;
|
|
|
|
// Absolute shorthand curve (S) having the same coordinate as the previous segment and a control point equal to the ending point
|
|
|
|
if (upperShorthandCurveCommand === command && x1Coord === xPrevCoord && y1Coord === yPrevCoord && x1Coord === x2Coord && y1Coord === y2Coord) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Absolute bézier curve (C) having the same coordinate as the previous segment and last control point equal to the ending point
|
|
|
|
if (upperCurveCommand === command && x1Coord === xPrevCoord && y1Coord === yPrevCoord && x2Coord === xCoord && y2Coord === yCoord) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
// Absolute horizontal direction (H) having the same x coordinate as the previous segment
|
|
|
|
(upperHorDirectionCommand === command && x1Coord === xPrevCoord) ||
|
|
|
|
// Absolute vertical direction (V) having the same y coordinate as the previous segment
|
|
|
|
(upperVerDirectionCommand === command && x1Coord === yPrevCoord) ||
|
|
|
|
// Absolute movement (M or L) having the same coordinate as the previous segment
|
|
|
|
(upperMovementCommands.includes(command) && x1Coord === xPrevCoord && y1Coord === yPrevCoord)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const invalidSegments = segments.filter(getInvalidSegments);
|
|
|
|
|
|
|
|
if (invalidSegments.length) {
|
|
|
|
invalidSegments.forEach(([command, x1Coord, y1Coord, ...rest]) => {
|
|
|
|
let readableSegment = `${command}${x1Coord}`,
|
|
|
|
resolutionTip = 'should be removed';
|
|
|
|
if (y1Coord !== undefined) {
|
|
|
|
readableSegment += ` ${y1Coord}`;
|
|
|
|
}
|
|
|
|
if (curveCommands.includes(command)) {
|
|
|
|
const [x2Coord, y2Coord, xCoord, yCoord] = rest;
|
|
|
|
readableSegment += `, ${x2Coord} ${y2Coord}`;
|
|
|
|
if (yCoord !== undefined) {
|
|
|
|
readableSegment += `, ${xCoord} ${yCoord}`;
|
|
|
|
}
|
|
|
|
if (command === lowerShorthandCurveCommand && (x2Coord !== 0 || y2Coord !== 0)) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(x2Coord)} ${removeLeadingZeros(y2Coord)}" or removed`;
|
|
|
|
}
|
|
|
|
if (command === upperShorthandCurveCommand) {
|
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(x2Coord)} ${removeLeadingZeros(y2Coord)}" or removed`;
|
|
|
|
}
|
|
|
|
if (command === lowerCurveCommand && (xCoord !== 0 || yCoord !== 0)) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(xCoord)} ${removeLeadingZeros(yCoord)}" or removed`;
|
|
|
|
}
|
|
|
|
if (command === upperCurveCommand) {
|
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(xCoord)} ${removeLeadingZeros(yCoord)}" or removed`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reporter.error(`Ineffective segment "${readableSegment}" in path (${resolutionTip}).`);
|
|
|
|
});
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-22 19:24:56 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "extraneous";
|
|
|
|
|
|
|
|
const rawSVG = $.html();
|
|
|
|
if (!svgRegexp.test(rawSVG)) {
|
2020-07-05 16:57:00 +03:00
|
|
|
reporter.error("Unexpected character(s), most likely extraneous whitespace, detected in SVG markup");
|
2020-06-22 19:24:56 +03:00
|
|
|
}
|
|
|
|
},
|
2020-12-14 22:35:27 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "negative-zeros";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find negative zeros inside path
|
|
|
|
const negativeZeroMatches = Array.from(iconPath.matchAll(negativeZerosRegexp));
|
|
|
|
if (negativeZeroMatches.length) {
|
|
|
|
// Calculate the index for each match in the file
|
|
|
|
const pathDStart = '<path d="';
|
|
|
|
const svgFileHtml = $.html();
|
|
|
|
const pathDIndex = svgFileHtml.indexOf(pathDStart) + pathDStart.length;
|
|
|
|
|
|
|
|
negativeZeroMatches.forEach((match) => {
|
|
|
|
const negativeZeroFileIndex = match.index + pathDIndex;
|
|
|
|
const previousChar = svgFileHtml[negativeZeroFileIndex - 1];
|
|
|
|
const replacement = "0123456789".includes(previousChar) ? " 0" : "0";
|
|
|
|
reporter.error(`Found "-0" at index ${negativeZeroFileIndex} (should be "${replacement}")`);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2020-07-10 11:06:08 +03:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-centered";
|
|
|
|
|
2020-10-17 14:01:56 +03:00
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
2020-07-28 13:33:40 +03:00
|
|
|
return;
|
2020-07-10 11:06:08 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 13:36:10 +03:00
|
|
|
const [minX, minY, maxX, maxY] = svgPathBbox(iconPath);
|
2020-07-10 11:06:08 +03:00
|
|
|
const targetCenter = iconSize / 2;
|
2020-11-23 13:36:10 +03:00
|
|
|
const centerX = +((minX + maxX) / 2).toFixed(iconFloatPrecision);
|
2020-07-10 11:06:08 +03:00
|
|
|
const devianceX = centerX - targetCenter;
|
2020-11-23 13:36:10 +03:00
|
|
|
const centerY = +((minY + maxY) / 2).toFixed(iconFloatPrecision);
|
2020-07-10 11:06:08 +03:00
|
|
|
const devianceY = centerY - targetCenter;
|
|
|
|
|
|
|
|
if (
|
|
|
|
Math.abs(devianceX) > iconTolerance ||
|
|
|
|
Math.abs(devianceY) > iconTolerance
|
|
|
|
) {
|
|
|
|
reporter.error(`<path> must be centered at (${targetCenter}, ${targetCenter}); the center is currently (${centerX}, ${centerY})`);
|
2020-10-17 14:01:56 +03:00
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
2020-07-10 11:06:08 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 12:33:32 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|