From 8bf508445f67ff8d2259552d6f3fc43f5934e640 Mon Sep 17 00:00:00 2001 From: David Beitey Date: Tue, 26 May 2020 16:32:34 +1000 Subject: [PATCH] Add linter to check svg path dimensions Float precision is set at 3 which is the default for svgo in .svgo.yml --- .svglintrc.js | 17 ++++++++++++++++- package-lock.json | 15 +++++++++++++++ package.json | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.svglintrc.js b/.svglintrc.js index 2f4aa93fd7e4..81fdb5cf4c8a 100644 --- a/.svglintrc.js +++ b/.svglintrc.js @@ -1,7 +1,10 @@ const data = require("./_data/simple-icons.json"); const { htmlFriendlyToTitle } = require("./scripts/utils.js"); +const getBounds = require('svg-path-bounding-box'); const titleRegexp = /(.+) icon$/; +const floatPrecision = 3; +const targetIconSize = 24; module.exports = { rules: { @@ -14,7 +17,7 @@ module.exports = { attr: [ { // ensure that the SVG elm has the appropriate attrs "role": "img", - "viewBox": "0 0 24 24", + "viewBox": `0 0 ${targetIconSize} ${targetIconSize}`, "xmlns": "http://www.w3.org/2000/svg", "rule::selector": "svg", @@ -46,6 +49,18 @@ module.exports = { } } }, + function(reporter, $, ast) { + const iconPath = $.find("path").attr("d"); + const bounds = getBounds(iconPath); + const width = +bounds.width.toFixed(floatPrecision); + const height = +bounds.height.toFixed(floatPrecision); + + if (width === 0 && height === 0) { + reporter.error(`Path bounds reported as 0 x 0; check the path is valid`); + } else if (width !== targetIconSize && height !== targetIconSize) { + reporter.error(`Size of must be exactly ${targetIconSize} in one dimension; currently ${width} x ${height}`); + } + }, ] } }; diff --git a/package-lock.json b/package-lock.json index a8cad88e60cb..255dfc0e5c20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6972,6 +6972,15 @@ } } }, + "svg-path-bounding-box": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/svg-path-bounding-box/-/svg-path-bounding-box-1.0.4.tgz", + "integrity": "sha1-7XPfODyLR4abZQjwWPV0j4gzwHA=", + "dev": true, + "requires": { + "svgpath": "^2.0.0" + } + }, "svglint": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/svglint/-/svglint-1.0.5.tgz", @@ -7085,6 +7094,12 @@ } } }, + "svgpath": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/svgpath/-/svgpath-2.2.3.tgz", + "integrity": "sha512-xA0glXYpJ9SYT4JeMp3c0psbqdZsG1c0ywGvdJUPY2FKEgwJV7NgkeYuuQiOxMp+XsK9nCqjm3KDw0LkM1YLXw==", + "dev": true + }, "symbol-tree": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz", diff --git a/package.json b/package.json index 450fcbdc8c30..3acc29316328 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "jest": "26.0.1", "jsonlint2": "1.7.1", "npm-run-all": "4.1.5", + "svg-path-bounding-box": "^1.0.4", "svglint": "1.0.5", "svgo": "1.3.2", "uglify-js": "3.9.3"