From a2331a5dee731766a9ac4f4b5774e0ffa26872a7 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 24 Aug 2021 13:41:26 +0300 Subject: [PATCH] removeViewBox and convertShapeToPath types and visitor (#1537) - added types to convertShapeToPath - refactored removeViewBox with visitor plugin api and got rid from one closestByName usage --- plugins/convertShapeToPath.js | 26 ++++++++++++++- plugins/removeViewBox.js | 63 +++++++++++++++++------------------ tsconfig.json | 2 -- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/plugins/convertShapeToPath.js b/plugins/convertShapeToPath.js index 44c89336..4a1fb625 100644 --- a/plugins/convertShapeToPath.js +++ b/plugins/convertShapeToPath.js @@ -1,5 +1,9 @@ 'use strict'; +/** + * @typedef {import('../lib/types').PathDataItem} PathDataItem + */ + const { stringifyPathData } = require('../lib/path.js'); const { detachNodeFromParent } = require('../lib/xast.js'); @@ -18,9 +22,14 @@ const regNumber = /[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g; * @see https://www.w3.org/TR/SVG11/shapes.html * * @author Lev Solntsev + * + * @type {import('../lib/types').Plugin<{ + * convertArcs?: boolean, + * floatPrecision?: number + * }>} */ exports.fn = (root, params) => { - const { convertArcs = false, floatPrecision: precision = null } = params; + const { convertArcs = false, floatPrecision: precision } = params; return { element: { @@ -41,6 +50,9 @@ exports.fn = (root, params) => { // cleanupNumericValues when 'px' units has already been removed. // TODO: Calculate sizes from % and non-px units if possible. if (Number.isNaN(x - y + width - height)) return; + /** + * @type {Array} + */ const pathData = [ { command: 'M', args: [x, y] }, { command: 'H', args: [x + width] }, @@ -63,6 +75,9 @@ exports.fn = (root, params) => { const x2 = Number(node.attributes.x2 || '0'); const y2 = Number(node.attributes.y2 || '0'); if (Number.isNaN(x1 - y1 + x2 - y2)) return; + /** + * @type {Array} + */ const pathData = [ { command: 'M', args: [x1, y1] }, { command: 'L', args: [x2, y2] }, @@ -87,6 +102,9 @@ exports.fn = (root, params) => { detachNodeFromParent(node, parentNode); return; } + /** + * @type {Array} + */ const pathData = []; for (let i = 0; i < coords.length; i += 2) { pathData.push({ @@ -110,6 +128,9 @@ exports.fn = (root, params) => { if (Number.isNaN(cx - cy + r)) { return; } + /** + * @type {Array} + */ const pathData = [ { command: 'M', args: [cx, cy - r] }, { command: 'A', args: [r, r, 0, 1, 0, cx, cy + r] }, @@ -132,6 +153,9 @@ exports.fn = (root, params) => { if (Number.isNaN(ecx - ecy + rx - ry)) { return; } + /** + * @type {Array} + */ const pathData = [ { command: 'M', args: [ecx, ecy - ry] }, { command: 'A', args: [rx, ry, 0, 1, 0, ecx, ecy + ry] }, diff --git a/plugins/removeViewBox.js b/plugins/removeViewBox.js index 94e482f9..ef4660d2 100644 --- a/plugins/removeViewBox.js +++ b/plugins/removeViewBox.js @@ -1,13 +1,8 @@ 'use strict'; -const { closestByName } = require('../lib/xast.js'); - +exports.type = 'visitor'; exports.name = 'removeViewBox'; - -exports.type = 'perItem'; - exports.active = true; - exports.description = 'removes viewBox attribute when possible'; const viewBoxElems = ['svg', 'pattern', 'symbol']; @@ -22,33 +17,35 @@ const viewBoxElems = ['svg', 'pattern', 'symbol']; * ⬇ * * - * @param {Object} item current iteration item - * @return {Boolean} if false, item will be filtered out - * * @author Kir Belevich + * + * @type {import('../lib/types').Plugin} */ -exports.fn = function (item) { - if ( - item.type === 'element' && - viewBoxElems.includes(item.name) && - item.attributes.viewBox != null && - item.attributes.width != null && - item.attributes.height != null - ) { - // TODO remove width/height for such case instead - if (item.name === 'svg' && closestByName(item.parentNode, 'svg')) { - return; - } - - const nums = item.attributes.viewBox.split(/[ ,]+/g); - - if ( - nums[0] === '0' && - nums[1] === '0' && - item.attributes.width.replace(/px$/, '') === nums[2] && // could use parseFloat too - item.attributes.height.replace(/px$/, '') === nums[3] - ) { - delete item.attributes.viewBox; - } - } +exports.fn = () => { + return { + element: { + enter: (node, parentNode) => { + if ( + viewBoxElems.includes(node.name) && + node.attributes.viewBox != null && + node.attributes.width != null && + node.attributes.height != null + ) { + // TODO remove width/height for such case instead + if (node.name === 'svg' && parentNode.type !== 'root') { + return; + } + const nums = node.attributes.viewBox.split(/[ ,]+/g); + if ( + nums[0] === '0' && + nums[1] === '0' && + node.attributes.width.replace(/px$/, '') === nums[2] && // could use parseFloat too + node.attributes.height.replace(/px$/, '') === nums[3] + ) { + delete node.attributes.viewBox; + } + } + }, + }, + }; }; diff --git a/tsconfig.json b/tsconfig.json index e5bcdf9d..2e9b17c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,6 @@ "plugins/collapseGroups.js", "plugins/convertColors.js", "plugins/convertPathData.js", - "plugins/convertShapeToPath.js", "plugins/convertStyleToAttrs.js", "plugins/convertTransform.js", "plugins/mergeStyles.js", @@ -38,7 +37,6 @@ "plugins/removeUnusedNS.js", "plugins/removeUselessDefs.js", "plugins/removeUselessStrokeAndFill.js", - "plugins/removeViewBox.js", "plugins/removeXMLNS.js", "plugins/reusePaths.js", "plugins/sortDefsChildren.js",