2024-03-24 19:38:18 +02:00
|
|
|
/* eslint complexity: off, max-depth: off */
|
2023-08-08 06:38:52 +02:00
|
|
|
import fs from 'node:fs/promises';
|
2021-12-25 16:22:56 +02:00
|
|
|
import path from 'node:path';
|
2023-08-08 06:38:52 +02:00
|
|
|
import process from 'node:process';
|
2024-03-24 19:38:18 +02:00
|
|
|
import svgPathBbox from 'svg-path-bbox';
|
|
|
|
import parsePath from 'svg-path-segments';
|
|
|
|
import svgpath from 'svgpath';
|
2021-12-25 16:22:56 +02:00
|
|
|
import {
|
2023-08-08 06:38:52 +02:00
|
|
|
SVG_PATH_REGEX,
|
2024-03-24 19:38:18 +02:00
|
|
|
collator,
|
2021-12-25 16:22:56 +02:00
|
|
|
getDirnameFromImportMeta,
|
|
|
|
htmlFriendlyToTitle,
|
2023-04-19 15:23:13 +02:00
|
|
|
} from './sdk.mjs';
|
2021-12-25 16:22:56 +02:00
|
|
|
|
|
|
|
const __dirname = getDirnameFromImportMeta(import.meta.url);
|
|
|
|
const dataFile = path.join(__dirname, '_data', 'simple-icons.json');
|
|
|
|
const htmlNamedEntitiesFile = path.join(
|
|
|
|
__dirname,
|
|
|
|
'node_modules',
|
|
|
|
'named-html-entities-json',
|
|
|
|
'index.json',
|
|
|
|
);
|
|
|
|
const svglintIgnoredFile = path.join(__dirname, '.svglint-ignored.json');
|
|
|
|
|
2023-08-08 06:38:52 +02:00
|
|
|
const data = JSON.parse(await fs.readFile(dataFile, 'utf8'));
|
2021-12-25 16:22:56 +02:00
|
|
|
const htmlNamedEntities = JSON.parse(
|
2023-08-08 06:38:52 +02:00
|
|
|
await fs.readFile(htmlNamedEntitiesFile, 'utf8'),
|
|
|
|
);
|
|
|
|
const svglintIgnores = JSON.parse(
|
|
|
|
await fs.readFile(svglintIgnoredFile, 'utf8'),
|
2021-12-25 16:22:56 +02:00
|
|
|
);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
const svgRegexp =
|
2024-03-24 19:38:18 +02:00
|
|
|
/^<svg( \S*=".*"){3}><title>.*<\/title><path d=".*"\/><\/svg>$/;
|
|
|
|
const negativeZerosRegexp = /-0(?=[^.]|[\s\d\w]|$)/g;
|
2020-06-22 18:24:56 +02:00
|
|
|
|
2020-06-10 11:59:42 +02:00
|
|
|
const iconSize = 24;
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconTargetCenter = iconSize / 2;
|
2020-06-10 11:59:42 +02:00
|
|
|
const iconFloatPrecision = 3;
|
2020-11-19 22:49:49 +02:00
|
|
|
const iconMaxFloatPrecision = 5;
|
2020-07-10 10:06:08 +02:00
|
|
|
const iconTolerance = 0.001;
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
// Set env SI_UPDATE_IGNORE to recreate the ignore file
|
2021-01-15 22:47:00 +02:00
|
|
|
const updateIgnoreFile = process.env.SI_UPDATE_IGNORE === 'true';
|
2021-10-25 21:13:10 +02:00
|
|
|
const ignoreFile = './.svglint-ignored.json';
|
2024-03-24 19:38:18 +02:00
|
|
|
const iconIgnored = updateIgnoreFile ? {} : svglintIgnores;
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const sortObjectByKey = (object) => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.keys(object)
|
|
|
|
.sort()
|
|
|
|
.map((k) => [k, object[k]]),
|
|
|
|
);
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const sortObjectByValue = (object) => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.keys(object)
|
|
|
|
.sort((a, b) => collator.compare(object[a], object[b]))
|
|
|
|
.map((k) => [k, object[k]]),
|
|
|
|
);
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const removeLeadingZeros = (number) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Convert 0.03 to '.03'
|
2020-12-13 21:29:01 +02:00
|
|
|
return number.toString().replace(/^(-?)(0)(\.?.+)/, '$1$3');
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2020-12-13 21:29:01 +02:00
|
|
|
|
2021-01-03 19:08:06 +02:00
|
|
|
/**
|
|
|
|
* Given three points, returns if the middle one (x2, y2) is collinear
|
|
|
|
* to the line formed by the two limit points.
|
|
|
|
**/
|
2024-03-24 19:38:18 +02:00
|
|
|
// eslint-disable-next-line max-params
|
2021-10-25 21:13:10 +02:00
|
|
|
const collinear = (x1, y1, x2, y2, x3, y3) => {
|
|
|
|
return x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) === 0;
|
|
|
|
};
|
2021-01-03 19:08:06 +02:00
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of digits after the decimal point.
|
|
|
|
* @param num The number of interest.
|
|
|
|
*/
|
2024-03-24 19:38:18 +02:00
|
|
|
const countDecimals = (number_) => {
|
|
|
|
if (number_ && number_ % 1) {
|
|
|
|
const [base, op, trail] = number_.toExponential().split(/e([+-])/);
|
|
|
|
const elen = Number.parseInt(trail, 10);
|
|
|
|
const index = base.indexOf('.');
|
|
|
|
return index === -1
|
2021-10-25 21:13:10 +02:00
|
|
|
? elen
|
2024-03-24 19:38:18 +02:00
|
|
|
: base.length - index - 1 + (op === '+' ? -elen : elen);
|
2021-03-02 20:00:18 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-03-02 20:00:18 +02:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index at which the first path value of an SVG starts.
|
|
|
|
* @param svgFileContent The raw SVG as text.
|
|
|
|
*/
|
2021-10-25 21:13:10 +02:00
|
|
|
const getPathDIndex = (svgFileContent) => {
|
2021-03-02 20:00:18 +02:00
|
|
|
const pathDStart = '<path d="';
|
|
|
|
return svgFileContent.indexOf(pathDStart) + pathDStart.length;
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2021-03-02 20:00:18 +02:00
|
|
|
|
2021-10-02 16:46:07 +02:00
|
|
|
/**
|
|
|
|
* Get the index at which the text of the first `<title></title>` tag starts.
|
|
|
|
* @param svgFileContent The raw SVG as text.
|
|
|
|
**/
|
2021-10-25 21:13:10 +02:00
|
|
|
const getTitleTextIndex = (svgFileContent) => {
|
2021-10-02 16:46:07 +02:00
|
|
|
const titleStart = '<title>';
|
|
|
|
return svgFileContent.indexOf(titleStart) + titleStart.length;
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2021-10-02 16:46:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a hexadecimal number passed as string to decimal number as integer.
|
|
|
|
* @param hex The hexadecimal number representation to convert.
|
|
|
|
**/
|
2021-10-25 21:13:10 +02:00
|
|
|
const hexadecimalToDecimal = (hex) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
let result = 0;
|
|
|
|
let digitValue;
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const digit of hex.toLowerCase()) {
|
|
|
|
digitValue = '0123456789abcdefgh'.indexOf(digit);
|
2021-10-02 16:46:07 +02:00
|
|
|
result = result * 16 + digitValue;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-02 16:46:07 +02:00
|
|
|
return result;
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const maybeShortenedWithEllipsis = (string_) => {
|
|
|
|
return string_.length > 20 ? `${string_.slice(0, 20)}...` : string_;
|
2023-08-04 03:27:20 +02:00
|
|
|
};
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
/**
|
|
|
|
* Memoize a function which accepts a single argument.
|
|
|
|
* A second argument can be passed to be used as key.
|
|
|
|
*/
|
2024-03-24 19:38:18 +02:00
|
|
|
const memoize = (function_) => {
|
2023-08-08 06:35:36 +02:00
|
|
|
const results = {};
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
return (argument, defaultKey = null) => {
|
|
|
|
const key = defaultKey || argument;
|
|
|
|
|
|
|
|
results[key] ||= function_(argument);
|
2023-08-08 06:35:36 +02:00
|
|
|
|
|
|
|
return results[key];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const getIconPath = memoize(($icon, _filepath) => $icon.find('path').attr('d'));
|
2023-08-08 06:35:36 +02:00
|
|
|
const getIconPathSegments = memoize((iconPath) => parsePath(iconPath));
|
|
|
|
const getIconPathBbox = memoize((iconPath) => svgPathBbox(iconPath));
|
|
|
|
|
2020-10-17 13:01:56 +02:00
|
|
|
if (updateIgnoreFile) {
|
2023-08-08 06:38:52 +02:00
|
|
|
process.on('exit', async () => {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Ensure object output order is consistent due to async svglint processing
|
2021-10-25 21:13:10 +02:00
|
|
|
const sorted = sortObjectByKey(iconIgnored);
|
2020-10-17 13:01:56 +02:00
|
|
|
for (const linterName in sorted) {
|
2024-03-24 19:38:18 +02:00
|
|
|
if (linterName) {
|
|
|
|
sorted[linterName] = sortObjectByValue(sorted[linterName]);
|
|
|
|
}
|
2020-10-17 13:01:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 06:38:52 +02:00
|
|
|
await fs.writeFile(ignoreFile, JSON.stringify(sorted, null, 2) + '\n', {
|
2021-10-25 21:13:10 +02:00
|
|
|
flag: 'w',
|
|
|
|
});
|
2020-10-17 13:01:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const isIgnored = (linterName, path) => {
|
|
|
|
return (
|
2024-03-24 19:38:18 +02:00
|
|
|
iconIgnored[linterName] && Object.hasOwn(iconIgnored[linterName], path)
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
};
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const ignoreIcon = (linterName, path, $) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
iconIgnored[linterName] ||= {};
|
2020-10-17 13:01:56 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const title = $.find('title').text();
|
2020-10-17 13:01:56 +02:00
|
|
|
const iconName = htmlFriendlyToTitle(title);
|
|
|
|
|
|
|
|
iconIgnored[linterName][path] = iconName;
|
2021-10-25 21:13:10 +02:00
|
|
|
};
|
2019-07-03 23:33:03 +02:00
|
|
|
|
2024-03-29 18:06:38 +02:00
|
|
|
const config = {
|
2021-10-25 21:13:10 +02:00
|
|
|
rules: {
|
|
|
|
elm: {
|
|
|
|
svg: 1,
|
|
|
|
'svg > title': 1,
|
|
|
|
'svg > path': 1,
|
|
|
|
'*': false,
|
|
|
|
},
|
|
|
|
attr: [
|
|
|
|
{
|
2024-03-24 19:38:18 +02:00
|
|
|
// Ensure that the SVG element has the appropriate attributes
|
2023-08-04 03:27:20 +02:00
|
|
|
// alphabetically ordered
|
2021-10-25 21:13:10 +02:00
|
|
|
role: 'img',
|
|
|
|
viewBox: `0 0 ${iconSize} ${iconSize}`,
|
|
|
|
xmlns: 'http://www.w3.org/2000/svg',
|
|
|
|
'rule::selector': 'svg',
|
|
|
|
'rule::whitelist': true,
|
2021-12-02 01:03:52 +02:00
|
|
|
'rule::order': true,
|
2021-10-25 21:13:10 +02:00
|
|
|
},
|
|
|
|
{
|
2024-03-24 19:38:18 +02:00
|
|
|
// Ensure that the title element has the appropriate attribute
|
2021-10-25 21:13:10 +02:00
|
|
|
'rule::selector': 'svg > title',
|
|
|
|
'rule::whitelist': true,
|
|
|
|
},
|
|
|
|
{
|
2024-03-24 19:38:18 +02:00
|
|
|
// Ensure that the path element only has the 'd' attribute
|
2023-08-04 03:27:20 +02:00
|
|
|
// (no style, opacity, etc.)
|
2023-08-08 06:38:52 +02:00
|
|
|
d: SVG_PATH_REGEX,
|
2021-10-25 21:13:10 +02:00
|
|
|
'rule::selector': 'svg > path',
|
|
|
|
'rule::whitelist': true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
custom: [
|
|
|
|
(reporter, $, ast) => {
|
|
|
|
reporter.name = 'icon-title';
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const iconTitleText = $.find('title').text();
|
|
|
|
const xmlNamedEntitiesCodepoints = [38, 60, 62];
|
|
|
|
const xmlNamedEntities = ['amp', 'lt', 'gt'];
|
2021-10-25 21:13:10 +02:00
|
|
|
let _validCodepointsRepr = true;
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
// Avoid character codepoints as hexadecimal representation
|
|
|
|
const hexadecimalCodepoints = [
|
|
|
|
...iconTitleText.matchAll(/&#x([A-Fa-f\d]+);/g),
|
|
|
|
];
|
2021-10-25 21:13:10 +02:00
|
|
|
if (hexadecimalCodepoints.length > 0) {
|
|
|
|
_validCodepointsRepr = false;
|
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const match of hexadecimalCodepoints) {
|
2021-10-25 21:13:10 +02:00
|
|
|
const charHexReprIndex =
|
|
|
|
getTitleTextIndex(ast.source) + match.index + 1;
|
|
|
|
const charDec = hexadecimalToDecimal(match[1]);
|
|
|
|
|
|
|
|
let charRepr;
|
|
|
|
if (xmlNamedEntitiesCodepoints.includes(charDec)) {
|
|
|
|
charRepr = `&${
|
|
|
|
xmlNamedEntities[xmlNamedEntitiesCodepoints.indexOf(charDec)]
|
|
|
|
};`;
|
|
|
|
} else if (charDec < 128) {
|
|
|
|
charRepr = String.fromCodePoint(charDec);
|
|
|
|
} else {
|
|
|
|
charRepr = `&#${charDec};`;
|
2018-08-16 11:33:32 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
'Hexadecimal representation of encoded character' +
|
|
|
|
` "${match[0]}" found at index ${charHexReprIndex}:` +
|
2021-10-25 21:13:10 +02:00
|
|
|
` replace it with "${charRepr}".`,
|
2021-10-02 16:46:07 +02:00
|
|
|
);
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
// Avoid character codepoints as named entities
|
|
|
|
const namedEntitiesCodepoints = [
|
|
|
|
...iconTitleText.matchAll(/&([A-Za-z\d]+);/g),
|
|
|
|
];
|
2021-10-25 21:13:10 +02:00
|
|
|
if (namedEntitiesCodepoints.length > 0) {
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const match of namedEntitiesCodepoints) {
|
2021-10-25 21:13:10 +02:00
|
|
|
const namedEntiyReprIndex =
|
|
|
|
getTitleTextIndex(ast.source) + match.index + 1;
|
|
|
|
|
|
|
|
if (!xmlNamedEntities.includes(match[1].toLowerCase())) {
|
2021-10-02 16:46:07 +02:00
|
|
|
_validCodepointsRepr = false;
|
2021-10-25 21:13:10 +02:00
|
|
|
const namedEntityJsRepr = htmlNamedEntities[match[1]];
|
|
|
|
let replacement;
|
|
|
|
|
|
|
|
if (
|
|
|
|
namedEntityJsRepr === undefined ||
|
2024-03-24 19:38:18 +02:00
|
|
|
namedEntityJsRepr.length !== 1
|
2021-10-25 21:13:10 +02:00
|
|
|
) {
|
|
|
|
replacement = 'its decimal or literal representation';
|
|
|
|
} else {
|
|
|
|
const namedEntityDec = namedEntityJsRepr.codePointAt(0);
|
2024-03-24 19:38:18 +02:00
|
|
|
replacement =
|
|
|
|
namedEntityDec < 128
|
|
|
|
? `"${namedEntityJsRepr}"`
|
|
|
|
: `"&#${namedEntityDec};"`;
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
'Named entity representation of encoded character' +
|
|
|
|
` "${match[0]}" found at index ${namedEntiyReprIndex}.` +
|
2021-10-25 21:13:10 +02:00
|
|
|
` Replace it with ${replacement}.`,
|
|
|
|
);
|
2021-10-02 16:46:07 +02:00
|
|
|
}
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_validCodepointsRepr) {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Compare encoded title with original title and report error if not equal
|
|
|
|
const encodingMatches = [
|
|
|
|
...iconTitleText.matchAll(/&(#(\d+)|(amp|quot|lt|gt));/g),
|
|
|
|
];
|
|
|
|
const encodedBuf = [];
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
const indexesToIgnore = [];
|
|
|
|
for (const match of encodingMatches) {
|
|
|
|
for (let r = match.index; r < match.index + match[0].length; r++) {
|
|
|
|
indexesToIgnore.push(r);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
for (let i = iconTitleText.length - 1; i >= 0; i--) {
|
2023-08-04 03:27:20 +02:00
|
|
|
if (indexesToIgnore.includes(i)) {
|
2021-10-25 21:13:10 +02:00
|
|
|
encodedBuf.unshift(iconTitleText[i]);
|
|
|
|
} else {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Encode all non ascii characters plus "'&<> (XML named entities)
|
|
|
|
const charDecimalCode = iconTitleText.codePointAt(i);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
if (charDecimalCode > 127) {
|
|
|
|
encodedBuf.unshift(`&#${charDecimalCode};`);
|
|
|
|
} else if (xmlNamedEntitiesCodepoints.includes(charDecimalCode)) {
|
|
|
|
encodedBuf.unshift(
|
|
|
|
`&${
|
|
|
|
xmlNamedEntities[
|
|
|
|
xmlNamedEntitiesCodepoints.indexOf(charDecimalCode)
|
|
|
|
]
|
|
|
|
};`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
encodedBuf.unshift(iconTitleText[i]);
|
|
|
|
}
|
2021-10-02 16:46:07 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const encodedIconTitleText = encodedBuf.join('');
|
|
|
|
if (encodedIconTitleText !== iconTitleText) {
|
|
|
|
_validCodepointsRepr = false;
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.error(
|
|
|
|
`Unencoded unicode characters found in title "${iconTitleText}":` +
|
|
|
|
` rewrite it as "${encodedIconTitleText}".`,
|
|
|
|
);
|
|
|
|
}
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
// Check if there are some other encoded characters in decimal notation
|
2021-10-25 21:13:10 +02:00
|
|
|
// which shouldn't be encoded
|
2024-03-24 19:38:18 +02:00
|
|
|
// eslint-disable-next-line unicorn/prefer-number-properties
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const match of encodingMatches.filter((m) => !isNaN(m[2]))) {
|
2024-03-24 19:38:18 +02:00
|
|
|
const decimalNumber = Number.parseInt(match[2], 10);
|
2023-08-04 03:27:20 +02:00
|
|
|
if (decimalNumber > 127) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
_validCodepointsRepr = false;
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
const decimalCodepointCharIndex =
|
|
|
|
getTitleTextIndex(ast.source) + match.index + 1;
|
2024-03-24 19:38:18 +02:00
|
|
|
let replacement;
|
2023-08-04 03:27:20 +02:00
|
|
|
if (xmlNamedEntitiesCodepoints.includes(decimalNumber)) {
|
|
|
|
replacement = `"&${
|
|
|
|
xmlNamedEntities[
|
|
|
|
xmlNamedEntitiesCodepoints.indexOf(decimalNumber)
|
|
|
|
]
|
|
|
|
};"`;
|
|
|
|
} else {
|
|
|
|
replacement = String.fromCodePoint(decimalNumber);
|
2024-03-24 19:38:18 +02:00
|
|
|
replacement = replacement === '"' ? `'"'` : `"${replacement}"`;
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reporter.error(
|
|
|
|
`Unnecessary encoded character "${match[0]}" found` +
|
|
|
|
` at index ${decimalCodepointCharIndex}:` +
|
|
|
|
` replace it with ${replacement}.`,
|
|
|
|
);
|
|
|
|
}
|
2021-10-02 16:46:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (_validCodepointsRepr) {
|
|
|
|
const iconName = htmlFriendlyToTitle(iconTitleText);
|
|
|
|
const iconExists = data.icons.some(
|
|
|
|
(icon) => icon.title === iconName,
|
|
|
|
);
|
|
|
|
if (!iconExists) {
|
|
|
|
reporter.error(
|
|
|
|
`No icon with title "${iconName}" found in simple-icons.json`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'icon-size';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
2021-10-25 21:13:10 +02:00
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const [minX, minY, maxX, maxY] = getIconPathBbox(iconPath);
|
2024-03-24 19:38:18 +02:00
|
|
|
const width = Number((maxX - minX).toFixed(iconFloatPrecision));
|
|
|
|
const height = Number((maxY - minY).toFixed(iconFloatPrecision));
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
if (width === 0 && height === 0) {
|
|
|
|
reporter.error(
|
|
|
|
'Path bounds were reported as 0 x 0; check if the path is valid',
|
|
|
|
);
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
} else if (width !== iconSize && height !== iconSize) {
|
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
`Size of <path> must be exactly ${iconSize} in one dimension;` +
|
|
|
|
` the size is currently ${width} x ${height}`,
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'icon-precision';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
|
|
|
const segments = getIconPathSegments(iconPath);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const segment of segments) {
|
2021-10-25 21:13:10 +02:00
|
|
|
const precisionMax = Math.max(
|
2024-03-24 19:38:18 +02:00
|
|
|
// eslint-disable-next-line unicorn/no-array-callback-reference
|
2021-10-25 21:13:10 +02:00
|
|
|
...segment.params.slice(1).map(countDecimals),
|
|
|
|
);
|
|
|
|
if (precisionMax > iconMaxFloatPrecision) {
|
2024-03-24 19:38:18 +02:00
|
|
|
let errorMessage =
|
2023-08-04 03:27:20 +02:00
|
|
|
`found ${precisionMax} decimals in segment` +
|
2024-03-24 19:38:18 +02:00
|
|
|
` "${iconPath.slice(segment.start, segment.end)}"`;
|
2024-05-03 00:15:20 +02:00
|
|
|
if (segment.chain !== undefined) {
|
2023-08-04 03:27:20 +02:00
|
|
|
const readableChain = maybeShortenedWithEllipsis(
|
2024-05-03 00:15:20 +02:00
|
|
|
iconPath.slice(segment.chain.start, segment.chain.end),
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2024-03-24 19:38:18 +02:00
|
|
|
errorMessage += ` of chain "${readableChain}"`;
|
2019-07-03 23:33:03 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
|
|
|
errorMessage += ` at index ${
|
2023-08-04 03:27:20 +02:00
|
|
|
segment.start + getPathDIndex(ast.source)
|
2021-10-25 21:13:10 +02:00
|
|
|
}`;
|
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
'Maximum precision should not be greater than' +
|
2024-03-24 19:38:18 +02:00
|
|
|
` ${iconMaxFloatPrecision}; ${errorMessage}`,
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
}
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'ineffective-segments';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
|
|
|
const segments = getIconPathSegments(iconPath);
|
2021-10-25 21:13:10 +02:00
|
|
|
const absSegments = svgpath(iconPath).abs().unshort().segments;
|
|
|
|
|
|
|
|
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';
|
2024-03-24 19:38:18 +02:00
|
|
|
const upperVersionDirectionCommand = 'V';
|
2021-10-25 21:13:10 +02:00
|
|
|
const upperDirectionCommands = [
|
|
|
|
upperHorDirectionCommand,
|
2024-03-24 19:38:18 +02:00
|
|
|
upperVersionDirectionCommand,
|
2021-10-25 21:13:10 +02:00
|
|
|
];
|
|
|
|
const upperCurveCommand = 'C';
|
|
|
|
const upperShorthandCurveCommand = 'S';
|
|
|
|
const upperCurveCommands = [
|
|
|
|
upperCurveCommand,
|
|
|
|
upperShorthandCurveCommand,
|
|
|
|
];
|
|
|
|
const curveCommands = [...lowerCurveCommands, ...upperCurveCommands];
|
2024-03-24 19:38:18 +02:00
|
|
|
const commands = new Set([
|
2021-10-25 21:13:10 +02:00
|
|
|
...lowerMovementCommands,
|
|
|
|
...lowerDirectionCommands,
|
|
|
|
...upperMovementCommands,
|
|
|
|
...upperDirectionCommands,
|
|
|
|
...curveCommands,
|
2024-03-24 19:38:18 +02:00
|
|
|
]);
|
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
const isInvalidSegment = (
|
|
|
|
[command, x1Coord, y1Coord, ...rest],
|
|
|
|
index,
|
2023-08-04 03:27:20 +02:00
|
|
|
previousSegmentIsZ,
|
2021-10-25 21:13:10 +02:00
|
|
|
) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
if (commands.has(command)) {
|
2021-10-25 21:13:10 +02:00
|
|
|
// Relative directions (h or v) having a length of 0
|
|
|
|
if (lowerDirectionCommands.includes(command) && x1Coord === 0) {
|
|
|
|
return true;
|
2020-06-10 11:59:42 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
// Relative movement (m or l) having a distance of 0
|
|
|
|
if (
|
|
|
|
index > 0 &&
|
|
|
|
lowerMovementCommands.includes(command) &&
|
|
|
|
x1Coord === 0 &&
|
|
|
|
y1Coord === 0
|
|
|
|
) {
|
2023-08-04 03:27:20 +02:00
|
|
|
// When the path is closed (z), the new segment can start with
|
|
|
|
// a relative placement (m) as if it were absolute (M)
|
|
|
|
return command.toLowerCase() === 'm' ? !previousSegmentIsZ : true;
|
2020-11-19 22:49:49 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
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;
|
2020-11-19 22:49:49 +02:00
|
|
|
}
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (index > 0) {
|
2024-03-24 19:38:18 +02:00
|
|
|
let [yPreviousCoord, xPreviousCoord] = [
|
2021-10-25 21:13:10 +02:00
|
|
|
...absSegments[index - 1],
|
|
|
|
].reverse();
|
2023-08-04 03:27:20 +02:00
|
|
|
// If the previous command was a direction one,
|
|
|
|
// we need to iterate back until we find the missing coordinates
|
2024-03-24 19:38:18 +02:00
|
|
|
if (upperDirectionCommands.includes(xPreviousCoord)) {
|
|
|
|
xPreviousCoord = undefined;
|
|
|
|
yPreviousCoord = undefined;
|
|
|
|
let index_ = index;
|
2021-10-25 21:13:10 +02:00
|
|
|
while (
|
2024-03-24 19:38:18 +02:00
|
|
|
--index_ > 0 &&
|
|
|
|
(xPreviousCoord === undefined || yPreviousCoord === undefined)
|
2021-10-25 21:13:10 +02:00
|
|
|
) {
|
2024-03-24 19:38:18 +02:00
|
|
|
let [yPreviousCoordDeep, xPreviousCoordDeep] = [
|
|
|
|
...absSegments[index_],
|
2021-10-25 21:13:10 +02:00
|
|
|
].reverse();
|
2023-08-04 03:27:20 +02:00
|
|
|
// If the previous command was a horizontal movement,
|
|
|
|
// we need to consider the single coordinate as x
|
2024-03-24 19:38:18 +02:00
|
|
|
if (upperHorDirectionCommand === xPreviousCoordDeep) {
|
|
|
|
xPreviousCoordDeep = yPreviousCoordDeep;
|
|
|
|
yPreviousCoordDeep = undefined;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
// If the previous command was a vertical movement,
|
|
|
|
// we need to consider the single coordinate as y
|
2024-03-24 19:38:18 +02:00
|
|
|
if (upperVersionDirectionCommand === xPreviousCoordDeep) {
|
|
|
|
xPreviousCoordDeep = undefined;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (
|
2024-03-24 19:38:18 +02:00
|
|
|
xPreviousCoord === undefined &&
|
|
|
|
xPreviousCoordDeep !== undefined
|
2021-10-25 21:13:10 +02:00
|
|
|
) {
|
2024-03-24 19:38:18 +02:00
|
|
|
xPreviousCoord = xPreviousCoordDeep;
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (
|
2024-03-24 19:38:18 +02:00
|
|
|
yPreviousCoord === undefined &&
|
|
|
|
yPreviousCoordDeep !== undefined
|
2021-10-25 21:13:10 +02:00
|
|
|
) {
|
2024-03-24 19:38:18 +02:00
|
|
|
yPreviousCoord = yPreviousCoordDeep;
|
2021-03-02 20:00:18 +02:00
|
|
|
}
|
2020-12-13 21:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-15 22:47:00 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (upperCurveCommands.includes(command)) {
|
|
|
|
const [x2Coord, y2Coord, xCoord, yCoord] = rest;
|
2023-08-04 03:27:20 +02:00
|
|
|
// Absolute shorthand curve (S) having
|
|
|
|
// the same coordinate as the previous segment
|
|
|
|
// and a control point equal to the ending point
|
2021-10-25 21:13:10 +02:00
|
|
|
if (
|
|
|
|
upperShorthandCurveCommand === command &&
|
2024-03-24 19:38:18 +02:00
|
|
|
x1Coord === xPreviousCoord &&
|
|
|
|
y1Coord === yPreviousCoord &&
|
2021-10-25 21:13:10 +02:00
|
|
|
x1Coord === x2Coord &&
|
|
|
|
y1Coord === y2Coord
|
|
|
|
) {
|
|
|
|
return true;
|
2021-01-15 12:16:50 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
// Absolute bézier curve (C) having
|
|
|
|
// the same coordinate as the previous segment
|
|
|
|
// and last control point equal to the ending point
|
2021-10-25 21:13:10 +02:00
|
|
|
if (
|
|
|
|
upperCurveCommand === command &&
|
2024-03-24 19:38:18 +02:00
|
|
|
x1Coord === xPreviousCoord &&
|
|
|
|
y1Coord === yPreviousCoord &&
|
2021-10-25 21:13:10 +02:00
|
|
|
x2Coord === xCoord &&
|
|
|
|
y2Coord === yCoord
|
|
|
|
) {
|
|
|
|
return true;
|
2021-01-03 19:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-15 22:47:00 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
return (
|
2023-08-04 03:27:20 +02:00
|
|
|
// Absolute horizontal direction (H) having
|
|
|
|
// the same x coordinate as the previous segment
|
2021-10-25 21:13:10 +02:00
|
|
|
(upperHorDirectionCommand === command &&
|
2024-03-24 19:38:18 +02:00
|
|
|
x1Coord === xPreviousCoord) ||
|
2023-08-04 03:27:20 +02:00
|
|
|
// Absolute vertical direction (V) having
|
|
|
|
// the same y coordinate as the previous segment
|
2024-03-24 19:38:18 +02:00
|
|
|
(upperVersionDirectionCommand === command &&
|
|
|
|
x1Coord === yPreviousCoord) ||
|
2023-08-04 03:27:20 +02:00
|
|
|
// Absolute movement (M or L) having the same
|
|
|
|
// coordinate as the previous segment
|
2021-10-25 21:13:10 +02:00
|
|
|
(upperMovementCommands.includes(command) &&
|
2024-03-24 19:38:18 +02:00
|
|
|
x1Coord === xPreviousCoord &&
|
|
|
|
y1Coord === yPreviousCoord)
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2021-01-03 19:08:06 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
for (let index = 0; index < segments.length; index++) {
|
|
|
|
const segment = segments[index];
|
|
|
|
const previousSegmentIsZ =
|
|
|
|
index > 0 && segments[index - 1].params[0].toLowerCase() === 'z';
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
if (isInvalidSegment(segment.params, index, previousSegmentIsZ)) {
|
2024-03-24 19:38:18 +02:00
|
|
|
const [command, _x1, _y1, ...rest] = segment.params;
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
let errorMessage = `Ineffective segment "${iconPath.slice(
|
|
|
|
segment.start,
|
|
|
|
segment.end,
|
|
|
|
)}" found`;
|
|
|
|
let resolutionTip = 'should be removed';
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
if (curveCommands.includes(command)) {
|
|
|
|
const [x2, y2, x, y] = rest;
|
|
|
|
|
|
|
|
if (
|
|
|
|
command === lowerShorthandCurveCommand &&
|
|
|
|
(x2 !== 0 || y2 !== 0)
|
|
|
|
) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(
|
|
|
|
x2,
|
|
|
|
)} ${removeLeadingZeros(y2)}" or removed`;
|
2021-01-03 19:08:06 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (command === upperShorthandCurveCommand) {
|
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(
|
|
|
|
x2,
|
|
|
|
)} ${removeLeadingZeros(y2)}" or removed`;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (command === lowerCurveCommand && (x !== 0 || y !== 0)) {
|
|
|
|
resolutionTip = `should be "l${removeLeadingZeros(
|
|
|
|
x,
|
|
|
|
)} ${removeLeadingZeros(y)}" or removed`;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (command === upperCurveCommand) {
|
|
|
|
resolutionTip = `should be "L${removeLeadingZeros(
|
|
|
|
x,
|
|
|
|
)} ${removeLeadingZeros(y)}" or removed`;
|
2021-03-02 20:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 18:24:56 +02:00
|
|
|
|
2024-05-03 00:15:20 +02:00
|
|
|
if (segment.chain !== undefined) {
|
2023-08-04 03:27:20 +02:00
|
|
|
const readableChain = maybeShortenedWithEllipsis(
|
2024-05-03 00:15:20 +02:00
|
|
|
iconPath.slice(segment.chain.start, segment.chain.end),
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2024-03-24 19:38:18 +02:00
|
|
|
errorMessage += ` in chain "${readableChain}"`;
|
2020-06-22 18:24:56 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
|
|
|
errorMessage += ` at index ${
|
2023-08-04 03:27:20 +02:00
|
|
|
segment.start + getPathDIndex(ast.source)
|
2021-10-25 21:13:10 +02:00
|
|
|
}`;
|
2020-12-14 21:35:27 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
reporter.error(`${errorMessage} (${resolutionTip})`);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'collinear-segments';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts collinear coordinates from SVG path straight lines
|
|
|
|
* (does not extracts collinear coordinates from curves).
|
|
|
|
**/
|
|
|
|
const getCollinearSegments = (iconPath) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
const segments = getIconPathSegments(iconPath);
|
|
|
|
const collinearSegments = [];
|
|
|
|
const straightLineCommands = 'HhVvLlMm';
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
let currentLine = [];
|
|
|
|
let currentAbsCoord = [undefined, undefined];
|
|
|
|
let startPoint;
|
|
|
|
let _inStraightLine = false;
|
|
|
|
let _nextInStraightLine = false;
|
|
|
|
let _resetStartPoint = false;
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
for (let s = 0; s < segments.length; s++) {
|
2024-03-24 19:38:18 +02:00
|
|
|
const seg = segments[s];
|
|
|
|
const parms = seg.params;
|
|
|
|
const cmd = parms[0];
|
|
|
|
const nextCmd = s + 1 < segments.length ? segments[s + 1][0] : null;
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
switch (cmd) {
|
|
|
|
// Next switch cases have been ordered by frequency
|
|
|
|
// of occurrence in the SVG paths of the icons
|
2024-03-24 19:38:18 +02:00
|
|
|
case 'M': {
|
|
|
|
currentAbsCoord[0] = parms[1];
|
|
|
|
currentAbsCoord[1] = parms[2];
|
2024-03-09 00:36:38 +02:00
|
|
|
// SVG 1.1:
|
|
|
|
// If a moveto is followed by multiple pairs of coordinates,
|
|
|
|
// the subsequent pairs are treated as implicit lineto commands.
|
2024-05-03 00:15:20 +02:00
|
|
|
if (seg.chain === undefined || seg.chain.start === seg.start) {
|
2024-03-09 00:36:38 +02:00
|
|
|
startPoint = undefined;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'm': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[1];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[2];
|
2024-05-03 00:15:20 +02:00
|
|
|
if (seg.chain === undefined || seg.chain.start === seg.start) {
|
2024-03-09 00:36:38 +02:00
|
|
|
startPoint = undefined;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'H': {
|
|
|
|
currentAbsCoord[0] = parms[1];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'h': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[1];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'V': {
|
|
|
|
currentAbsCoord[1] = parms[1];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'v': {
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[1];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'L': {
|
|
|
|
currentAbsCoord[0] = parms[1];
|
|
|
|
currentAbsCoord[1] = parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'l': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[1];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
case 'Z':
|
2024-03-24 19:38:18 +02:00
|
|
|
case 'z': {
|
2024-03-09 00:36:38 +02:00
|
|
|
// TODO: Overlapping in Z should be handled in another rule
|
2024-03-24 19:38:18 +02:00
|
|
|
currentAbsCoord = [startPoint[0], startPoint[1]];
|
2023-08-04 03:27:20 +02:00
|
|
|
_resetStartPoint = true;
|
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'C': {
|
|
|
|
currentAbsCoord[0] = parms[5];
|
|
|
|
currentAbsCoord[1] = parms[6];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'c': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[5];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[6];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'A': {
|
|
|
|
currentAbsCoord[0] = parms[6];
|
|
|
|
currentAbsCoord[1] = parms[7];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'a': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[6];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[7];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 's': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[1];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'S': {
|
|
|
|
currentAbsCoord[0] = parms[1];
|
|
|
|
currentAbsCoord[1] = parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 't': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[1];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'T': {
|
|
|
|
currentAbsCoord[0] = parms[1];
|
|
|
|
currentAbsCoord[1] = parms[2];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'Q': {
|
|
|
|
currentAbsCoord[0] = parms[3];
|
|
|
|
currentAbsCoord[1] = parms[4];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'q': {
|
|
|
|
currentAbsCoord[0] = (currentAbsCoord[0] || 0) + parms[3];
|
|
|
|
currentAbsCoord[1] = (currentAbsCoord[1] || 0) + parms[4];
|
2023-08-04 03:27:20 +02:00
|
|
|
break;
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
2023-08-04 03:27:20 +02:00
|
|
|
throw new Error(`"${cmd}" command not handled`);
|
2024-03-24 19:38:18 +02:00
|
|
|
}
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (startPoint === undefined) {
|
2024-03-24 19:38:18 +02:00
|
|
|
startPoint = [currentAbsCoord[0], currentAbsCoord[1]];
|
2021-10-25 21:13:10 +02:00
|
|
|
} else if (_resetStartPoint) {
|
|
|
|
startPoint = undefined;
|
|
|
|
_resetStartPoint = false;
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
2021-02-22 18:20:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
_nextInStraightLine = straightLineCommands.includes(nextCmd);
|
2024-03-24 19:38:18 +02:00
|
|
|
const _exitingStraightLine =
|
|
|
|
_inStraightLine && !_nextInStraightLine;
|
2021-10-25 21:13:10 +02:00
|
|
|
_inStraightLine = straightLineCommands.includes(cmd);
|
2021-02-22 18:20:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
if (_inStraightLine) {
|
2024-03-24 19:38:18 +02:00
|
|
|
currentLine.push([currentAbsCoord[0], currentAbsCoord[1]]);
|
2021-10-25 21:13:10 +02:00
|
|
|
} else {
|
|
|
|
if (_exitingStraightLine) {
|
|
|
|
if (straightLineCommands.includes(cmd)) {
|
2024-03-24 19:38:18 +02:00
|
|
|
currentLine.push([currentAbsCoord[0], currentAbsCoord[1]]);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
// Get collinear coordinates
|
2024-03-24 19:38:18 +02:00
|
|
|
for (let p = 1; p < currentLine.length - 1; p++) {
|
|
|
|
const _collinearCoord = collinear(
|
|
|
|
currentLine[p - 1][0],
|
|
|
|
currentLine[p - 1][1],
|
|
|
|
currentLine[p][0],
|
|
|
|
currentLine[p][1],
|
|
|
|
currentLine[p + 1][0],
|
|
|
|
currentLine[p + 1][1],
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
if (_collinearCoord) {
|
|
|
|
collinearSegments.push(
|
2024-03-24 19:38:18 +02:00
|
|
|
segments[s - currentLine.length + p + 1],
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
}
|
2021-02-22 18:20:07 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
|
|
|
currentLine = [];
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-22 18:20:07 +02:00
|
|
|
|
2021-10-25 21:13:10 +02:00
|
|
|
return collinearSegments;
|
|
|
|
};
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
|
|
|
const collinearSegments = getCollinearSegments(iconPath);
|
2023-08-08 06:35:36 +02:00
|
|
|
if (collinearSegments.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const pathDIndex = getPathDIndex(ast.source);
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const segment of collinearSegments) {
|
2024-03-24 19:38:18 +02:00
|
|
|
let errorMessage = `Collinear segment "${iconPath.slice(
|
2021-10-25 21:13:10 +02:00
|
|
|
segment.start,
|
|
|
|
segment.end,
|
|
|
|
)}" found`;
|
2024-05-03 00:15:20 +02:00
|
|
|
if (segment.chain !== undefined) {
|
2024-03-24 19:38:18 +02:00
|
|
|
const readableChain = maybeShortenedWithEllipsis(
|
2024-05-03 00:15:20 +02:00
|
|
|
iconPath.slice(segment.chain.start, segment.chain.end),
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2024-03-24 19:38:18 +02:00
|
|
|
errorMessage += ` in chain "${readableChain}"`;
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
|
|
|
errorMessage += ` at index ${
|
2021-10-25 21:13:10 +02:00
|
|
|
segment.start + pathDIndex
|
|
|
|
} (should be removed)`;
|
2024-03-24 19:38:18 +02:00
|
|
|
reporter.error(errorMessage);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(reporter, $, ast) => {
|
|
|
|
reporter.name = 'extraneous';
|
|
|
|
|
|
|
|
if (!svgRegexp.test(ast.source)) {
|
2023-02-09 21:47:14 +02:00
|
|
|
if (ast.source.includes('\n') || ast.source.includes('\r')) {
|
|
|
|
reporter.error(
|
|
|
|
'Unexpected newline character(s) detected in SVG markup',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
'Unexpected character(s), most likely extraneous' +
|
|
|
|
' whitespace, detected in SVG markup',
|
2023-02-09 21:47:14 +02:00
|
|
|
);
|
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'negative-zeros';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
// Find negative zeros inside path
|
2024-03-24 19:38:18 +02:00
|
|
|
const negativeZeroMatches = [...iconPath.matchAll(negativeZerosRegexp)];
|
|
|
|
if (negativeZeroMatches.length > 0) {
|
2021-10-25 21:13:10 +02:00
|
|
|
// Calculate the index for each match in the file
|
2023-08-04 03:27:20 +02:00
|
|
|
const pathDIndex = getPathDIndex(ast.source);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-04 03:27:20 +02:00
|
|
|
for (const match of negativeZeroMatches) {
|
2021-10-25 21:13:10 +02:00
|
|
|
const negativeZeroFileIndex = match.index + pathDIndex;
|
2023-08-04 03:27:20 +02:00
|
|
|
const previousChar = ast.source[negativeZeroFileIndex - 1];
|
2021-10-25 21:13:10 +02:00
|
|
|
const replacement = '0123456789'.includes(previousChar)
|
|
|
|
? ' 0'
|
|
|
|
: '0';
|
|
|
|
reporter.error(
|
2023-08-04 03:27:20 +02:00
|
|
|
`Found "-0" at index ${negativeZeroFileIndex} (should` +
|
|
|
|
` be "${replacement}")`,
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2023-08-04 03:27:20 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'icon-centered';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
2021-10-25 21:13:10 +02:00
|
|
|
if (!updateIgnoreFile && isIgnored(reporter.name, iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const [minX, minY, maxX, maxY] = getIconPathBbox(iconPath);
|
2024-03-24 19:38:18 +02:00
|
|
|
const centerX = Number(((minX + maxX) / 2).toFixed(iconFloatPrecision));
|
2023-08-08 06:35:36 +02:00
|
|
|
const devianceX = centerX - iconTargetCenter;
|
2024-03-24 19:38:18 +02:00
|
|
|
const centerY = Number(((minY + maxY) / 2).toFixed(iconFloatPrecision));
|
2023-08-08 06:35:36 +02:00
|
|
|
const devianceY = centerY - iconTargetCenter;
|
2021-10-25 21:13:10 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
Math.abs(devianceX) > iconTolerance ||
|
|
|
|
Math.abs(devianceY) > iconTolerance
|
|
|
|
) {
|
|
|
|
reporter.error(
|
2023-08-08 06:35:36 +02:00
|
|
|
`<path> must be centered at (${iconTargetCenter}, ${iconTargetCenter});` +
|
2023-08-04 03:27:20 +02:00
|
|
|
` the center is currently (${centerX}, ${centerY})`,
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
|
|
|
if (updateIgnoreFile) {
|
|
|
|
ignoreIcon(reporter.name, iconPath, $);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-03-24 19:38:18 +02:00
|
|
|
(reporter, $, ast, {filepath}) => {
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.name = 'path-format';
|
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
const iconPath = getIconPath($, filepath);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-08 06:38:52 +02:00
|
|
|
if (!SVG_PATH_REGEX.test(iconPath)) {
|
2024-03-24 19:38:18 +02:00
|
|
|
const errorMessage = 'Invalid path format';
|
|
|
|
let reason;
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2023-08-08 06:35:36 +02:00
|
|
|
if (!iconPath.startsWith('M') && !iconPath.startsWith('m')) {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Doesn't start with moveto
|
2023-08-04 03:27:20 +02:00
|
|
|
reason =
|
|
|
|
'should start with "moveto" command ("M" or "m"),' +
|
2024-03-24 19:38:18 +02:00
|
|
|
` but starts with "${iconPath[0]}"`;
|
|
|
|
reporter.error(`${errorMessage}: ${reason}`);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const validPathCharacters = SVG_PATH_REGEX.source.replaceAll(
|
|
|
|
/[[\]+^$]/g,
|
|
|
|
'',
|
|
|
|
);
|
|
|
|
const invalidCharactersMsgs = [];
|
|
|
|
const pathDIndex = getPathDIndex(ast.source);
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
for (const [i, char] of Object.entries(iconPath)) {
|
|
|
|
if (!validPathCharacters.includes(char)) {
|
2021-10-25 21:13:10 +02:00
|
|
|
invalidCharactersMsgs.push(
|
2024-03-24 19:38:18 +02:00
|
|
|
`"${char}" at index ${pathDIndex + Number.parseInt(i, 10)}`,
|
2021-10-25 21:13:10 +02:00
|
|
|
);
|
2021-09-14 22:40:48 +02:00
|
|
|
}
|
2020-07-10 10:06:08 +02:00
|
|
|
}
|
2021-10-25 21:13:10 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
// Contains invalid characters
|
2021-10-25 21:13:10 +02:00
|
|
|
if (invalidCharactersMsgs.length > 0) {
|
|
|
|
reason = `unexpected character${
|
|
|
|
invalidCharactersMsgs.length > 1 ? 's' : ''
|
2023-08-08 06:35:36 +02:00
|
|
|
} found (${invalidCharactersMsgs.join(', ')})`;
|
2024-03-24 19:38:18 +02:00
|
|
|
reporter.error(`${errorMessage}: ${reason}`);
|
2021-10-25 21:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(reporter, $, ast) => {
|
|
|
|
reporter.name = 'svg-format';
|
|
|
|
|
|
|
|
// Don't allow explicit '</path>' closing tag
|
|
|
|
if (ast.source.includes('</path>')) {
|
|
|
|
const reason =
|
|
|
|
`found a closing "path" tag at index ${ast.source.indexOf(
|
|
|
|
'</path>',
|
2023-08-04 03:27:20 +02:00
|
|
|
)}. The path should be self-closing,` +
|
|
|
|
' use "/>" instead of "></path>".';
|
2021-10-25 21:13:10 +02:00
|
|
|
reporter.error(`Invalid SVG content format: ${reason}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-08-16 11:33:32 +02:00
|
|
|
};
|
2024-03-29 18:06:38 +02:00
|
|
|
|
|
|
|
export default config;
|