From e0b97a53c3b5707a1caa0839504bffac9af4066c Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 1 Oct 2021 17:34:11 +0300 Subject: [PATCH 1/2] Refactor removeEmptyAttrs - migrated to visitor plugin api - covered with tsdoc --- plugins/removeEmptyAttrs.js | 38 ++++++++++++++++++------------------- tsconfig.json | 1 - 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/plugins/removeEmptyAttrs.js b/plugins/removeEmptyAttrs.js index 55586832e..5d9870a29 100644 --- a/plugins/removeEmptyAttrs.js +++ b/plugins/removeEmptyAttrs.js @@ -2,32 +2,32 @@ const { attrsGroups } = require('./_collections.js'); +exports.type = 'visitor'; exports.name = 'removeEmptyAttrs'; - -exports.type = 'perItem'; - exports.active = true; - exports.description = 'removes empty attributes'; /** * Remove attributes with empty values. * - * @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') { - for (const [name, value] of Object.entries(item.attributes)) { - if ( - value === '' && - // empty conditional processing attributes prevents elements from rendering - attrsGroups.conditionalProcessing.includes(name) === false - ) { - delete item.attributes[name]; - } - } - } +exports.fn = () => { + return { + element: { + enter: (node) => { + for (const [name, value] of Object.entries(node.attributes)) { + if ( + value === '' && + // empty conditional processing attributes prevents elements from rendering + attrsGroups.conditionalProcessing.includes(name) === false + ) { + delete node.attributes[name]; + } + } + }, + }, + }; }; diff --git a/tsconfig.json b/tsconfig.json index 98b32eded..bd5643ec5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,7 +24,6 @@ "plugins/moveGroupAttrsToElems.js", "plugins/plugins.js", "plugins/removeDimensions.js", - "plugins/removeEmptyAttrs.js", "plugins/removeNonInheritableGroupAttrs.js", "plugins/removeXMLNS.js", "plugins/inlineStyles.js", From 327a623484d56fa05ca981e39eab2b1c4de3818b Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 1 Oct 2021 19:50:30 +0300 Subject: [PATCH 2/2] Comment cases --- test/plugins/removeEmptyAttrs.01.svg | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/plugins/removeEmptyAttrs.01.svg b/test/plugins/removeEmptyAttrs.01.svg index 39564efcc..75ef26f30 100644 --- a/test/plugins/removeEmptyAttrs.01.svg +++ b/test/plugins/removeEmptyAttrs.01.svg @@ -1,3 +1,7 @@ +Removes empty attributes + +=== +