summaryrefslogtreecommitdiff
path: root/node_modules/svgo/plugins/cleanupIds.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2023-12-21 22:46:53 +0800
committerlittleblack111 <littleblack11111@gmail.com>2023-12-21 22:46:53 +0800
commitf06956efd2dc4bf77244a9fcce2b883a00b925af (patch)
tree0123cb73ce67f1f4b17e1d6cbabff076ee44e31b /node_modules/svgo/plugins/cleanupIds.js
parent8d980c13970c15b6d2312b6fc4ac51698dd0d220 (diff)
updated node modules
Diffstat (limited to 'node_modules/svgo/plugins/cleanupIds.js')
-rw-r--r--node_modules/svgo/plugins/cleanupIds.js70
1 files changed, 24 insertions, 46 deletions
diff --git a/node_modules/svgo/plugins/cleanupIds.js b/node_modules/svgo/plugins/cleanupIds.js
index 1d2532b2..65b38d16 100644
--- a/node_modules/svgo/plugins/cleanupIds.js
+++ b/node_modules/svgo/plugins/cleanupIds.js
@@ -5,14 +5,11 @@
*/
const { visitSkip } = require('../lib/xast.js');
-const { referencesProps } = require('./_collections.js');
+const { hasScripts, findReferences } = require('../lib/svgo/tools');
exports.name = 'cleanupIds';
exports.description = 'removes unused IDs and minifies used';
-const regReferencesUrl = /\burl\((["'])?#(.+?)\1\)/;
-const regReferencesHref = /^#(.+?)$/;
-const regReferencesBegin = /(\D+)\./;
const generateIdChars = [
'a',
'b',
@@ -86,7 +83,8 @@ const hasStringPrefix = (string, prefixes) => {
/**
* Generate unique minimal ID.
*
- * @type {(currentId: null | Array<number>) => Array<number>}
+ * @param {?number[]} currentId
+ * @returns {number[]}
*/
const generateId = (currentId) => {
if (currentId == null) {
@@ -146,7 +144,7 @@ exports.fn = (_root, params) => {
*/
const nodeById = new Map();
/**
- * @type {Map<string, Array<{element: XastElement, name: string, value: string }>>}
+ * @type {Map<string, Array<{element: XastElement, name: string }>>}
*/
const referencesById = new Map();
let deoptimized = false;
@@ -154,11 +152,11 @@ exports.fn = (_root, params) => {
return {
element: {
enter: (node) => {
- if (force == false) {
- // deoptimize if style or script elements are present
+ if (!force) {
+ // deoptimize if style or scripts are present
if (
- (node.name === 'style' || node.name === 'script') &&
- node.children.length !== 0
+ (node.name === 'style' && node.children.length !== 0) ||
+ hasScripts(node)
) {
deoptimized = true;
return;
@@ -189,36 +187,14 @@ exports.fn = (_root, params) => {
nodeById.set(id, node);
}
} else {
- // collect all references
- /**
- * @type {null | string}
- */
- let id = null;
- if (referencesProps.includes(name)) {
- const match = value.match(regReferencesUrl);
- if (match != null) {
- id = match[2]; // url() reference
- }
- }
- if (name === 'href' || name.endsWith(':href')) {
- const match = value.match(regReferencesHref);
- if (match != null) {
- id = match[1]; // href reference
- }
- }
- if (name === 'begin') {
- const match = value.match(regReferencesBegin);
- if (match != null) {
- id = match[1]; // href reference
- }
- }
- if (id != null) {
+ const ids = findReferences(name, value);
+ for (const id of ids) {
let refs = referencesById.get(id);
if (refs == null) {
refs = [];
referencesById.set(id, refs);
}
- refs.push({ element: node, name, value });
+ refs.push({ element: node, name });
}
}
}
@@ -231,33 +207,35 @@ exports.fn = (_root, params) => {
return;
}
/**
- * @type {(id: string) => boolean}
- **/
+ * @param {string} id
+ * @returns {boolean}
+ */
const isIdPreserved = (id) =>
preserveIds.has(id) || hasStringPrefix(id, preserveIdPrefixes);
- /**
- * @type {null | Array<number>}
- */
+ /** @type {?number[]} */
let currentId = null;
for (const [id, refs] of referencesById) {
const node = nodeById.get(id);
if (node != null) {
// replace referenced IDs with the minified ones
if (minify && isIdPreserved(id) === false) {
- /**
- * @type {null | string}
- */
+ /** @type {?string} */
let currentIdString = null;
do {
currentId = generateId(currentId);
currentIdString = getIdString(currentId);
- } while (isIdPreserved(currentIdString));
+ } while (
+ isIdPreserved(currentIdString) ||
+ (referencesById.has(currentIdString) &&
+ nodeById.get(currentIdString) == null)
+ );
node.attributes.id = currentIdString;
- for (const { element, name, value } of refs) {
+ for (const { element, name } of refs) {
+ const value = element.attributes[name];
if (value.includes('#')) {
// replace id in href and url()
element.attributes[name] = value.replace(
- `#${id}`,
+ `#${encodeURI(id)}`,
`#${currentIdString}`
);
} else {