@cyberalien/svg-utils 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/svg/ids.d.ts +7 -2
- package/lib/svg/ids.js +25 -4
- package/lib/xml/iterate.d.ts +1 -1
- package/package.json +1 -1
package/lib/svg/ids.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { ParsedXMLTagElement } from "../xml/types.js";
|
|
2
2
|
type HashCallback = (id: string, content: string, tagName: string) => string;
|
|
3
|
+
type Result = Record<string, ParsedXMLTagElement[]>;
|
|
3
4
|
/**
|
|
4
5
|
* Change IDs in SVG using a callback function
|
|
5
6
|
*/
|
|
6
|
-
declare function changeSVGIDs(root: ParsedXMLTagElement[], callback: HashCallback):
|
|
7
|
-
|
|
7
|
+
declare function changeSVGIDs(root: ParsedXMLTagElement[], callback: HashCallback): Result;
|
|
8
|
+
/**
|
|
9
|
+
* Remove duplicate IDs from SVG
|
|
10
|
+
*/
|
|
11
|
+
declare function removeDuplicateIDs(root: ParsedXMLTagElement[], data: Result): ParsedXMLTagElement[];
|
|
12
|
+
export { changeSVGIDs, removeDuplicateIDs };
|
package/lib/svg/ids.js
CHANGED
|
@@ -9,6 +9,7 @@ function changeSVGIDs(root, callback) {
|
|
|
9
9
|
const idMap = /* @__PURE__ */ new Map();
|
|
10
10
|
const idNodes = /* @__PURE__ */ new Map();
|
|
11
11
|
const nestedIDs = /* @__PURE__ */ new Map();
|
|
12
|
+
const results = Object.create(null);
|
|
12
13
|
const usage = [];
|
|
13
14
|
const parse = (replacement) => {
|
|
14
15
|
iterateXMLContent(root, (node, stack) => {
|
|
@@ -20,7 +21,12 @@ function changeSVGIDs(root, callback) {
|
|
|
20
21
|
if (idNodes.has(nodeID)) throw new Error(`Duplicate ID found: ${nodeID}`);
|
|
21
22
|
idNodes.set(nodeID, node);
|
|
22
23
|
idMap.set(node, nodeID);
|
|
23
|
-
} else if (nodeID === replacement[0])
|
|
24
|
+
} else if (nodeID === replacement[0]) {
|
|
25
|
+
const newID = replacement[1];
|
|
26
|
+
attribs.id = newID;
|
|
27
|
+
if (!results[newID]) results[newID] = [node];
|
|
28
|
+
else results[newID].push(node);
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
31
|
for (const attrib in attribs) {
|
|
26
32
|
const value = attribs[attrib];
|
|
@@ -78,7 +84,7 @@ function changeSVGIDs(root, callback) {
|
|
|
78
84
|
});
|
|
79
85
|
};
|
|
80
86
|
parse();
|
|
81
|
-
if (!idMap.size) return;
|
|
87
|
+
if (!idMap.size) return results;
|
|
82
88
|
const allIDs = new Set(idMap.values());
|
|
83
89
|
const parseIDs = (parseAll = false) => {
|
|
84
90
|
const oldSize = allIDs.size;
|
|
@@ -100,8 +106,23 @@ function changeSVGIDs(root, callback) {
|
|
|
100
106
|
};
|
|
101
107
|
while (allIDs.size) if (!parseIDs()) {
|
|
102
108
|
parseIDs(true);
|
|
103
|
-
return;
|
|
109
|
+
return results;
|
|
110
|
+
}
|
|
111
|
+
return results;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Remove duplicate IDs from SVG
|
|
115
|
+
*/
|
|
116
|
+
function removeDuplicateIDs(root, data) {
|
|
117
|
+
const remove = [];
|
|
118
|
+
for (const id in data) {
|
|
119
|
+
const nodes = data[id];
|
|
120
|
+
if (nodes.length > 1) remove.push(...nodes.slice(1));
|
|
104
121
|
}
|
|
122
|
+
if (remove.length) return iterateXMLContent(root, (node) => {
|
|
123
|
+
if (remove.includes(node)) return "remove";
|
|
124
|
+
});
|
|
125
|
+
return root;
|
|
105
126
|
}
|
|
106
127
|
|
|
107
|
-
export { changeSVGIDs };
|
|
128
|
+
export { changeSVGIDs, removeDuplicateIDs };
|
package/lib/xml/iterate.d.ts
CHANGED
|
@@ -8,5 +8,5 @@ import { ParsedXMLNode, ParsedXMLTagElement } from "./types.js";
|
|
|
8
8
|
* - 'abort': stop iteration
|
|
9
9
|
*/
|
|
10
10
|
type CallbackResult = void | 'remove' | 'skip' | 'abort';
|
|
11
|
-
declare function iterateXMLContent(root: ParsedXMLTagElement[], callback: (node: ParsedXMLNode, stack: ParsedXMLTagElement[]) => CallbackResult):
|
|
11
|
+
declare function iterateXMLContent(root: ParsedXMLTagElement[], callback: (node: ParsedXMLNode, stack: ParsedXMLTagElement[]) => CallbackResult): ParsedXMLTagElement[];
|
|
12
12
|
export { iterateXMLContent };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "Common functions for working with SVG used by various packages.",
|
|
5
5
|
"author": "Vjacheslav Trushkin",
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.9",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"bugs": "https://github.com/cyberalien/svg-utils/issues",
|
|
9
9
|
"homepage": "https://cyberalien.dev/",
|