From 729bdbe347c7c250ef4e6d97b7dca718d07a3152 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 2 Sep 2021 02:17:56 +0300 Subject: [PATCH] Refactor cleanupIDs I still did get how id generation works but data flow is clear now at least. - covered with types - migrated to visitor plugin api - got rid of traverse api --- plugins/cleanupIDs.js | 503 ++++++++++++++++++++++-------------------- tsconfig.json | 1 - 2 files changed, 261 insertions(+), 243 deletions(-) diff --git a/plugins/cleanupIDs.js b/plugins/cleanupIDs.js index b47e5a79e..45bfe2812 100644 --- a/plugins/cleanupIDs.js +++ b/plugins/cleanupIDs.js @@ -1,259 +1,103 @@ 'use strict'; -const { traverse, traverseBreak } = require('../lib/xast.js'); -const { parseName } = require('../lib/svgo/tools.js'); - -exports.name = 'cleanupIDs'; +/** + * @typedef {import('../lib/types').XastElement} XastElement + */ -exports.type = 'full'; +const { visitSkip } = require('../lib/xast.js'); +const { referencesProps } = require('./_collections.js'); +exports.type = 'visitor'; +exports.name = 'cleanupIDs'; exports.active = true; - exports.description = 'removes unused IDs and minifies used'; -exports.params = { - remove: true, - minify: true, - prefix: '', - preserve: [], - preservePrefixes: [], - force: false, -}; - -var referencesProps = new Set(require('./_collections').referencesProps), - regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, - regReferencesHref = /^#(.+?)$/, - regReferencesBegin = /(\w+)\./, - styleOrScript = ['style', 'script'], - generateIDchars = [ - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i', - 'j', - 'k', - 'l', - 'm', - 'n', - 'o', - 'p', - 'q', - 'r', - 's', - 't', - 'u', - 'v', - 'w', - 'x', - 'y', - 'z', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z', - ], - maxIDindex = generateIDchars.length - 1; +const regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/; +const regReferencesHref = /^#(.+?)$/; +const regReferencesBegin = /(\w+)\./; +const generateIDchars = [ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', +]; +const maxIDindex = generateIDchars.length - 1; /** - * Remove unused and minify used IDs - * (only if there are no any