@cyberalien/svg-utils 0.0.9 → 0.0.11

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.
Files changed (2) hide show
  1. package/lib/svg/ids.js +38 -6
  2. package/package.json +1 -1
package/lib/svg/ids.js CHANGED
@@ -94,7 +94,32 @@ function changeSVGIDs(root, callback) {
94
94
  const node = idNodes.get(id);
95
95
  const newNode = cloneObject(node);
96
96
  delete newNode.attribs.id;
97
- for (const usageItem of usage) if (usageItem.node === node && usageItem.id === id) delete newNode.attribs[usageItem.attrib];
97
+ for (const usageItem of usage) if (usageItem.node === node && usageItem.id === id) {
98
+ const attr = usageItem.attrib;
99
+ const value = newNode.attribs[attr];
100
+ if (value === id) newNode.attribs[attr] = "";
101
+ else {
102
+ const parts = value.split(id);
103
+ const newValue = [];
104
+ const total = parts.length;
105
+ for (let i = 0; i < total; i++) {
106
+ if (i > 0) {
107
+ newValue.push(parts[i - 1]);
108
+ if (parts[i - i].slice(-1) !== ";") {
109
+ newValue.push(id);
110
+ continue;
111
+ }
112
+ }
113
+ if (i < total - 1) {
114
+ if (parts[i].slice(0, 1) !== ".") {
115
+ newValue.push(parts[i]);
116
+ continue;
117
+ }
118
+ }
119
+ }
120
+ newNode.attribs[attr] = newValue.join("");
121
+ }
122
+ }
98
123
  const content = stringifyXMLContent([newNode]);
99
124
  if (!content) throw new Error(`Failed to stringify node with ID: ${id}`);
100
125
  const newID = callback(id, content, node.tag);
@@ -114,14 +139,21 @@ function changeSVGIDs(root, callback) {
114
139
  * Remove duplicate IDs from SVG
115
140
  */
116
141
  function removeDuplicateIDs(root, data) {
117
- const remove = [];
142
+ const remove = /* @__PURE__ */ new Set();
118
143
  for (const id in data) {
119
144
  const nodes = data[id];
120
- if (nodes.length > 1) remove.push(...nodes.slice(1));
145
+ if (nodes.length > 1) remove.add(id);
146
+ }
147
+ if (remove.size) {
148
+ const removing = /* @__PURE__ */ new Set();
149
+ return iterateXMLContent(root, (node) => {
150
+ if (node.type !== "tag") return;
151
+ const id = node.attribs.id;
152
+ if (typeof id !== "string" || !remove.has(id)) return;
153
+ if (removing.has(id)) return "remove";
154
+ removing.add(id);
155
+ });
121
156
  }
122
- if (remove.length) return iterateXMLContent(root, (node) => {
123
- if (remove.includes(node)) return "remove";
124
- });
125
157
  return root;
126
158
  }
127
159
 
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.9",
6
+ "version": "0.0.11",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",