2019-07-03 23:33:03 +02:00
|
|
|
const data = require("./_data/simple-icons.json");
|
|
|
|
const { htmlFriendlyToTitle } = require("./scripts/utils.js");
|
2020-06-10 11:59:42 +02:00
|
|
|
const getBounds = require("svg-path-bounding-box");
|
2019-07-03 23:33:03 +02:00
|
|
|
|
|
|
|
const titleRegexp = /(.+) icon$/;
|
2020-07-05 15:57:00 +02:00
|
|
|
const svgRegexp = /^<svg( [^\s]*=".*"){3}><title>.*<\/title><path d=".*"\/><\/svg>\r?\n?$/;
|
2020-06-22 18:24:56 +02:00
|
|
|
|
2020-06-10 11:59:42 +02:00
|
|
|
const iconSize = 24;
|
|
|
|
const iconFloatPrecision = 3;
|
|
|
|
const iconIgnored = require("./.svglint-ignored.json");
|
2019-07-03 23:33:03 +02:00
|
|
|
|
2018-08-16 11:33:32 +02:00
|
|
|
module.exports = {
|
|
|
|
rules: {
|
|
|
|
elm: {
|
|
|
|
"svg": 1,
|
|
|
|
"svg > title": 1,
|
2019-03-28 21:40:31 +02:00
|
|
|
"svg > path": 1,
|
|
|
|
"*": false,
|
2018-08-16 11:33:32 +02:00
|
|
|
},
|
|
|
|
attr: [
|
|
|
|
{ // ensure that the SVG elm has the appropriate attrs
|
|
|
|
"role": "img",
|
2020-06-10 11:59:42 +02:00
|
|
|
"viewBox": `0 0 ${iconSize} ${iconSize}`,
|
2018-08-16 11:33:32 +02:00
|
|
|
"xmlns": "http://www.w3.org/2000/svg",
|
2018-11-06 18:10:17 +02:00
|
|
|
|
2018-08-16 11:33:32 +02:00
|
|
|
"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 09:55:03 +02: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 11:33:32 +02:00
|
|
|
}
|
2019-07-03 23:33:03 +02:00
|
|
|
],
|
|
|
|
custom: [
|
|
|
|
function(reporter, $, ast) {
|
2020-06-10 11:59:42 +02:00
|
|
|
reporter.name = "icon-title";
|
|
|
|
|
2019-07-03 23:33:03 +02: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 11:59:42 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "icon-size";
|
|
|
|
|
|
|
|
const iconPath = $.find("path").attr("d");
|
|
|
|
if (iconIgnored.hasOwnProperty(iconPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bounds = getBounds(iconPath);
|
|
|
|
const width = +bounds.width.toFixed(iconFloatPrecision);
|
|
|
|
const height = +bounds.height.toFixed(iconFloatPrecision);
|
|
|
|
|
|
|
|
if (width === 0 && height === 0) {
|
|
|
|
reporter.error("Path bounds were reported as 0 x 0; check if the path is valid");
|
|
|
|
} 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-06-22 18:24:56 +02:00
|
|
|
function(reporter, $, ast) {
|
|
|
|
reporter.name = "extraneous";
|
|
|
|
|
|
|
|
const rawSVG = $.html();
|
|
|
|
if (!svgRegexp.test(rawSVG)) {
|
2020-07-05 15:57:00 +02:00
|
|
|
reporter.error("Unexpected character(s), most likely extraneous whitespace, detected in SVG markup");
|
2020-06-22 18:24:56 +02:00
|
|
|
}
|
|
|
|
},
|
2018-08-16 11:33:32 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|