@iconify/tools 2.0.8 → 2.0.12

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 (49) hide show
  1. package/lib/colors/parse.d.ts +19 -3
  2. package/lib/colors/parse.js +109 -40
  3. package/lib/colors/parse.mjs +86 -39
  4. package/lib/colors/validate.js +5 -2
  5. package/lib/colors/validate.mjs +3 -1
  6. package/lib/icon-set/index.js +4 -21
  7. package/lib/icon-set/index.mjs +6 -21
  8. package/lib/icon-set/merge.js +0 -1
  9. package/lib/icon-set/merge.mjs +1 -2
  10. package/lib/icon-set/types.d.ts +3 -1
  11. package/lib/index.d.ts +2 -0
  12. package/lib/index.js +5 -1
  13. package/lib/index.mjs +4 -0
  14. package/lib/optimise/flags.js +9 -0
  15. package/lib/optimise/flags.mjs +8 -0
  16. package/lib/optimise/global-style.d.ts +5 -0
  17. package/lib/optimise/global-style.js +158 -0
  18. package/lib/optimise/global-style.mjs +129 -0
  19. package/lib/optimise/svgo.js +1 -1
  20. package/lib/optimise/svgo.mjs +0 -1
  21. package/lib/svg/analyse/error.d.ts +5 -0
  22. package/lib/svg/analyse/error.js +22 -0
  23. package/lib/svg/analyse/error.mjs +16 -0
  24. package/lib/svg/analyse/types.d.ts +89 -0
  25. package/lib/svg/analyse/types.js +2 -0
  26. package/lib/svg/analyse/types.mjs +0 -0
  27. package/lib/svg/analyse.d.ts +8 -0
  28. package/lib/svg/analyse.js +352 -0
  29. package/lib/svg/analyse.mjs +302 -0
  30. package/lib/svg/cleanup/attribs.d.ts +1 -1
  31. package/lib/svg/cleanup/attribs.js +8 -0
  32. package/lib/svg/cleanup/attribs.mjs +8 -1
  33. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  34. package/lib/svg/cleanup/bad-tags.js +0 -2
  35. package/lib/svg/cleanup/bad-tags.mjs +0 -3
  36. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  37. package/lib/svg/cleanup/root-svg.d.ts +1 -1
  38. package/lib/svg/cleanup/root-svg.js +4 -2
  39. package/lib/svg/cleanup/root-svg.mjs +3 -3
  40. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  41. package/lib/svg/data/attributes.js +1 -1
  42. package/lib/svg/data/attributes.mjs +1 -1
  43. package/lib/svg/data/tags.d.ts +15 -7
  44. package/lib/svg/data/tags.js +20 -9
  45. package/lib/svg/data/tags.mjs +11 -6
  46. package/lib/svg/parse-style.d.ts +7 -7
  47. package/lib/svg/parse-style.js +27 -7
  48. package/lib/svg/parse-style.mjs +26 -7
  49. package/package.json +18 -2
@@ -0,0 +1,8 @@
1
+ import type { SVG } from './index';
2
+ import type { AnalyseSVGStructureResult, AnalyseSVGStructureOptions } from './analyse/types';
3
+ /**
4
+ * Find all IDs, links, which elements use palette, which items aren't used
5
+ *
6
+ * Before running this function run cleanup functions to change inline style to attributes and fix attributes
7
+ */
8
+ export declare function analyseSVGStructure(svg: SVG, options?: AnalyseSVGStructureOptions): Promise<AnalyseSVGStructureResult>;
@@ -0,0 +1,352 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyseSVGStructure = void 0;
4
+ const parse_1 = require("./parse");
5
+ const attributes_1 = require("./data/attributes");
6
+ const tags_1 = require("./data/tags");
7
+ const error_1 = require("./analyse/error");
8
+ /**
9
+ * Find all IDs, links, which elements use palette, which items aren't used
10
+ *
11
+ * Before running this function run cleanup functions to change inline style to attributes and fix attributes
12
+ */
13
+ async function analyseSVGStructure(svg, options = {}) {
14
+ // Options
15
+ const fixErrors = options.fixErrors;
16
+ // Root element
17
+ let root = svg.$svg(':root').get(0);
18
+ if (root._parsed) {
19
+ // Reload to reset custom properties
20
+ svg.load(svg.toString());
21
+ root = svg.$svg(':root').get(0);
22
+ }
23
+ root._parsed = true;
24
+ const cheerio = svg.$svg;
25
+ // List of all elements
26
+ const elements = new Map();
27
+ // List of IDs
28
+ const ids = Object.create(null);
29
+ // Links
30
+ let links = [];
31
+ /**
32
+ * Found element with id
33
+ */
34
+ function addID(element, id) {
35
+ if (ids[id]) {
36
+ throw new Error(`Duplicate id "${id}"`);
37
+ }
38
+ element._id = id;
39
+ ids[id] = element._index;
40
+ return true;
41
+ }
42
+ /**
43
+ * Add _belongsTo
44
+ */
45
+ function gotElementWithID(element, id, isMask) {
46
+ addID(element, id);
47
+ if (!element._belongsTo) {
48
+ element._belongsTo = [];
49
+ }
50
+ element._belongsTo.push({
51
+ id,
52
+ isMask,
53
+ indexes: new Set([element._index]),
54
+ });
55
+ return;
56
+ }
57
+ /**
58
+ * Mark element as reusable, set properties
59
+ */
60
+ function gotReusableElement(item, isMask) {
61
+ const element = item.element;
62
+ const attribs = element.attribs;
63
+ const index = element._index;
64
+ const id = attribs['id'];
65
+ if (typeof id !== 'string') {
66
+ const message = `Definition element ${(0, error_1.analyseTagError)(element)} does not have id`;
67
+ if (fixErrors) {
68
+ item.removeNode = true;
69
+ item.testChildren = false;
70
+ console.warn(message);
71
+ return false;
72
+ }
73
+ throw new Error(message);
74
+ }
75
+ if (ids[id] && fixErrors) {
76
+ console.warn(`Duplicate id "${id}"`);
77
+ item.removeNode = true;
78
+ item.testChildren = false;
79
+ return false;
80
+ }
81
+ element._reusableElement = {
82
+ id,
83
+ isMask,
84
+ index,
85
+ };
86
+ gotElementWithID(element, id, isMask);
87
+ return true;
88
+ }
89
+ /**
90
+ * Found element that uses another element
91
+ */
92
+ function gotElementReference(item, id, usedAsMask) {
93
+ const element = item.element;
94
+ const usedByIndex = element._index;
95
+ const link = {
96
+ id,
97
+ usedByIndex,
98
+ usedAsMask,
99
+ };
100
+ // Add to global list
101
+ links.push(link);
102
+ // Add to element and parent elements
103
+ if (!element._linksTo) {
104
+ element._linksTo = [];
105
+ }
106
+ element._linksTo.push(link);
107
+ }
108
+ // Find all reusable elements and all usages
109
+ let index = 0;
110
+ await (0, parse_1.parseSVG)(svg, (item) => {
111
+ var _a;
112
+ const { tagName, parents } = item;
113
+ if (tags_1.styleTag.has(tagName)) {
114
+ item.testChildren = false;
115
+ return;
116
+ }
117
+ const element = item.element;
118
+ const attribs = element.attribs;
119
+ // Set index
120
+ index++;
121
+ element._index = index;
122
+ elements.set(index, element);
123
+ if (!parents.length) {
124
+ // root <svg>
125
+ element._usedAsMask = false;
126
+ element._usedAsPaint = true;
127
+ return;
128
+ }
129
+ element._usedAsMask = false;
130
+ element._usedAsPaint = false;
131
+ // Get parent element
132
+ const parentItem = parents[0];
133
+ const parentElement = parentItem.element;
134
+ // Check for mask or clip path
135
+ if (tags_1.maskTags.has(tagName)) {
136
+ // Element can only be used as mask or clip path
137
+ if (!gotReusableElement(item, true)) {
138
+ return;
139
+ }
140
+ }
141
+ else if (tags_1.reusableElementsWithPalette.has(tagName)) {
142
+ // Reusable element that uses palette
143
+ if (!gotReusableElement(item, false)) {
144
+ return;
145
+ }
146
+ }
147
+ else if (tags_1.defsTag.has(parentItem.tagName)) {
148
+ // Symbol without <symbol> tag
149
+ if (!gotReusableElement(item, false)) {
150
+ return;
151
+ }
152
+ }
153
+ else if (!tags_1.defsTag.has(tagName)) {
154
+ // Not reusable element, not <defs>. Copy parent stuff
155
+ element._usedAsMask = parentElement._usedAsMask;
156
+ element._usedAsPaint = parentElement._usedAsPaint;
157
+ element._parentElement = parentElement._index;
158
+ if (!parentElement._childElements) {
159
+ parentElement._childElements = [];
160
+ }
161
+ parentElement._childElements.push(index);
162
+ // Copy id of reusable element from parent
163
+ const parentReusableElement = parentElement._reusableElement;
164
+ if (parentReusableElement) {
165
+ if (element._reusableElement) {
166
+ // Reusable element inside reusable element: should not happen!
167
+ throw new Error(`Reusable element ${(0, error_1.analyseTagError)(element)} is inside another reusable element id="${parentReusableElement.id}"`);
168
+ }
169
+ element._reusableElement = parentReusableElement;
170
+ }
171
+ // Copy all parent ids
172
+ const parentBelongsTo = parentElement._belongsTo;
173
+ if (parentBelongsTo) {
174
+ const list = element._belongsTo || (element._belongsTo = []);
175
+ parentBelongsTo.forEach((item) => {
176
+ item.indexes.add(index);
177
+ list.push(item);
178
+ });
179
+ }
180
+ // Check if element has its own id
181
+ if (element._id === void 0) {
182
+ const id = attribs['id'];
183
+ if (typeof id === 'string') {
184
+ if (ids[id] && fixErrors) {
185
+ console.warn(`Duplicate id "${id}"`);
186
+ cheerio(element).removeAttr('id');
187
+ }
188
+ else {
189
+ gotElementWithID(element, id, false);
190
+ }
191
+ }
192
+ }
193
+ }
194
+ // Check if element uses any ID
195
+ if ((_a = attributes_1.tagSpecificNonPresentationalAttributes[tagName]) === null || _a === void 0 ? void 0 : _a.has('href')) {
196
+ const href = attribs['href'] || attribs['xlink:href'];
197
+ if (typeof href === 'string') {
198
+ if (href.slice(0, 1) !== '#') {
199
+ throw new Error(`Invalid link in ${(0, error_1.analyseTagError)(element)}`);
200
+ }
201
+ const id = href.slice(1);
202
+ gotElementReference(item, id, false);
203
+ }
204
+ }
205
+ // Check colors
206
+ Object.keys(attribs).forEach((attr) => {
207
+ // Get id
208
+ let value = attribs[attr];
209
+ if (value.slice(0, 5).toLowerCase() !== 'url(#') {
210
+ return;
211
+ }
212
+ value = value.slice(5);
213
+ if (value.slice(-1) !== ')') {
214
+ return;
215
+ }
216
+ const id = value.slice(0, value.length - 1).trim();
217
+ if (attributes_1.urlPresentationalAttributes.has(attr)) {
218
+ // Used as mask, clip path or filter
219
+ gotElementReference(item, id, attr !== 'filter');
220
+ return;
221
+ }
222
+ if (attributes_1.commonColorPresentationalAttributes.has(attr) ||
223
+ attributes_1.markerAttributes.has(attr)) {
224
+ // Used as paint
225
+ gotElementReference(item, id, false);
226
+ return;
227
+ }
228
+ });
229
+ });
230
+ // Make sure all required IDs exist
231
+ links = links.filter((item) => {
232
+ const id = item.id;
233
+ if (ids[id]) {
234
+ return true;
235
+ }
236
+ // Attempt to fix error
237
+ function fix() {
238
+ const index = item.usedByIndex;
239
+ const element = elements.get(index);
240
+ const tagName = element.tagName;
241
+ function remove() {
242
+ var _a;
243
+ const $element = cheerio(element);
244
+ const parent = element.parent;
245
+ if (parent) {
246
+ if (parent._childElements) {
247
+ parent._childElements = parent._childElements.filter((num) => num !== index);
248
+ }
249
+ (_a = parent._belongsTo) === null || _a === void 0 ? void 0 : _a.forEach((list) => {
250
+ list.indexes.delete(index);
251
+ });
252
+ }
253
+ $element.remove();
254
+ }
255
+ // Remove links
256
+ if (element._linksTo) {
257
+ element._linksTo = element._linksTo.filter((item) => item.id !== id);
258
+ }
259
+ // Remove <use /> without children
260
+ if (!element.children.length) {
261
+ if (tags_1.useTag.has(tagName)) {
262
+ remove();
263
+ return;
264
+ }
265
+ }
266
+ // Remove attributes that use id
267
+ const matches = new Set(['#' + id, 'url(#' + id + ')']);
268
+ const attribs = element.attribs;
269
+ for (const attr in attribs) {
270
+ if (matches.has(attribs[attr])) {
271
+ cheerio(element).removeAttr(attr);
272
+ }
273
+ }
274
+ }
275
+ const message = `Missing element with id="${id}"`;
276
+ if (fixErrors) {
277
+ fix();
278
+ console.warn(message);
279
+ }
280
+ else {
281
+ throw new Error(message);
282
+ }
283
+ return false;
284
+ });
285
+ // Check if tree item already has child item
286
+ function hasChildItem(tree, child, canThrow) {
287
+ const item = tree.children.find((item) => item.index === child.index &&
288
+ item.usedAsMask === child.usedAsMask);
289
+ if (item && canThrow) {
290
+ throw new Error('Recursion');
291
+ }
292
+ return !!item;
293
+ }
294
+ // Generate tree
295
+ const tree = {
296
+ index: 1,
297
+ usedAsMask: false,
298
+ children: [],
299
+ };
300
+ function parseTreeItem(tree, usedItems, inMask) {
301
+ var _a, _b;
302
+ const element = elements.get(tree.index);
303
+ // Add usage
304
+ if (tree.usedAsMask || inMask) {
305
+ element._usedAsMask = true;
306
+ inMask = true;
307
+ }
308
+ else {
309
+ element._usedAsPaint = true;
310
+ }
311
+ usedItems = usedItems.slice(0);
312
+ usedItems.push(element._index);
313
+ // Add all child elements
314
+ (_a = element._childElements) === null || _a === void 0 ? void 0 : _a.forEach((childIndex) => {
315
+ if (usedItems.indexOf(childIndex) !== -1) {
316
+ throw new Error('Recursion');
317
+ }
318
+ const childItem = {
319
+ index: childIndex,
320
+ usedAsMask: false,
321
+ children: [],
322
+ parent: tree,
323
+ };
324
+ tree.children.push(childItem);
325
+ parseTreeItem(childItem, usedItems, inMask);
326
+ });
327
+ // Add all links
328
+ (_b = element._linksTo) === null || _b === void 0 ? void 0 : _b.forEach((link) => {
329
+ const linkIndex = ids[link.id];
330
+ const usedAsMask = link.usedAsMask;
331
+ const childItem = {
332
+ index: linkIndex,
333
+ usedAsMask,
334
+ children: [],
335
+ parent: tree,
336
+ };
337
+ if (hasChildItem(tree, childItem, false)) {
338
+ return;
339
+ }
340
+ tree.children.push(childItem);
341
+ parseTreeItem(childItem, usedItems, inMask || usedAsMask);
342
+ });
343
+ }
344
+ parseTreeItem(tree, [0], false);
345
+ return {
346
+ elements,
347
+ ids,
348
+ links,
349
+ tree,
350
+ };
351
+ }
352
+ exports.analyseSVGStructure = analyseSVGStructure;
@@ -0,0 +1,302 @@
1
+ // src/svg/analyse.ts
2
+ import { parseSVG } from "./parse.mjs";
3
+ import {
4
+ commonColorPresentationalAttributes,
5
+ markerAttributes,
6
+ tagSpecificNonPresentationalAttributes,
7
+ urlPresentationalAttributes
8
+ } from "./data/attributes.mjs";
9
+ import {
10
+ defsTag,
11
+ maskTags,
12
+ reusableElementsWithPalette,
13
+ styleTag,
14
+ useTag
15
+ } from "./data/tags.mjs";
16
+ import { analyseTagError } from "./analyse/error.mjs";
17
+ async function analyseSVGStructure(svg, options = {}) {
18
+ const fixErrors = options.fixErrors;
19
+ let root = svg.$svg(":root").get(0);
20
+ if (root._parsed) {
21
+ svg.load(svg.toString());
22
+ root = svg.$svg(":root").get(0);
23
+ }
24
+ root._parsed = true;
25
+ const cheerio = svg.$svg;
26
+ const elements = new Map();
27
+ const ids = Object.create(null);
28
+ let links = [];
29
+ function addID(element, id) {
30
+ if (ids[id]) {
31
+ throw new Error(`Duplicate id "${id}"`);
32
+ }
33
+ element._id = id;
34
+ ids[id] = element._index;
35
+ return true;
36
+ }
37
+ function gotElementWithID(element, id, isMask) {
38
+ addID(element, id);
39
+ if (!element._belongsTo) {
40
+ element._belongsTo = [];
41
+ }
42
+ element._belongsTo.push({
43
+ id,
44
+ isMask,
45
+ indexes: new Set([element._index])
46
+ });
47
+ return;
48
+ }
49
+ function gotReusableElement(item, isMask) {
50
+ const element = item.element;
51
+ const attribs = element.attribs;
52
+ const index2 = element._index;
53
+ const id = attribs["id"];
54
+ if (typeof id !== "string") {
55
+ const message = `Definition element ${analyseTagError(element)} does not have id`;
56
+ if (fixErrors) {
57
+ item.removeNode = true;
58
+ item.testChildren = false;
59
+ console.warn(message);
60
+ return false;
61
+ }
62
+ throw new Error(message);
63
+ }
64
+ if (ids[id] && fixErrors) {
65
+ console.warn(`Duplicate id "${id}"`);
66
+ item.removeNode = true;
67
+ item.testChildren = false;
68
+ return false;
69
+ }
70
+ element._reusableElement = {
71
+ id,
72
+ isMask,
73
+ index: index2
74
+ };
75
+ gotElementWithID(element, id, isMask);
76
+ return true;
77
+ }
78
+ function gotElementReference(item, id, usedAsMask) {
79
+ const element = item.element;
80
+ const usedByIndex = element._index;
81
+ const link = {
82
+ id,
83
+ usedByIndex,
84
+ usedAsMask
85
+ };
86
+ links.push(link);
87
+ if (!element._linksTo) {
88
+ element._linksTo = [];
89
+ }
90
+ element._linksTo.push(link);
91
+ }
92
+ let index = 0;
93
+ await parseSVG(svg, (item) => {
94
+ var _a;
95
+ const { tagName, parents } = item;
96
+ if (styleTag.has(tagName)) {
97
+ item.testChildren = false;
98
+ return;
99
+ }
100
+ const element = item.element;
101
+ const attribs = element.attribs;
102
+ index++;
103
+ element._index = index;
104
+ elements.set(index, element);
105
+ if (!parents.length) {
106
+ element._usedAsMask = false;
107
+ element._usedAsPaint = true;
108
+ return;
109
+ }
110
+ element._usedAsMask = false;
111
+ element._usedAsPaint = false;
112
+ const parentItem = parents[0];
113
+ const parentElement = parentItem.element;
114
+ if (maskTags.has(tagName)) {
115
+ if (!gotReusableElement(item, true)) {
116
+ return;
117
+ }
118
+ } else if (reusableElementsWithPalette.has(tagName)) {
119
+ if (!gotReusableElement(item, false)) {
120
+ return;
121
+ }
122
+ } else if (defsTag.has(parentItem.tagName)) {
123
+ if (!gotReusableElement(item, false)) {
124
+ return;
125
+ }
126
+ } else if (!defsTag.has(tagName)) {
127
+ element._usedAsMask = parentElement._usedAsMask;
128
+ element._usedAsPaint = parentElement._usedAsPaint;
129
+ element._parentElement = parentElement._index;
130
+ if (!parentElement._childElements) {
131
+ parentElement._childElements = [];
132
+ }
133
+ parentElement._childElements.push(index);
134
+ const parentReusableElement = parentElement._reusableElement;
135
+ if (parentReusableElement) {
136
+ if (element._reusableElement) {
137
+ throw new Error(`Reusable element ${analyseTagError(element)} is inside another reusable element id="${parentReusableElement.id}"`);
138
+ }
139
+ element._reusableElement = parentReusableElement;
140
+ }
141
+ const parentBelongsTo = parentElement._belongsTo;
142
+ if (parentBelongsTo) {
143
+ const list = element._belongsTo || (element._belongsTo = []);
144
+ parentBelongsTo.forEach((item2) => {
145
+ item2.indexes.add(index);
146
+ list.push(item2);
147
+ });
148
+ }
149
+ if (element._id === void 0) {
150
+ const id = attribs["id"];
151
+ if (typeof id === "string") {
152
+ if (ids[id] && fixErrors) {
153
+ console.warn(`Duplicate id "${id}"`);
154
+ cheerio(element).removeAttr("id");
155
+ } else {
156
+ gotElementWithID(element, id, false);
157
+ }
158
+ }
159
+ }
160
+ }
161
+ if ((_a = tagSpecificNonPresentationalAttributes[tagName]) == null ? void 0 : _a.has("href")) {
162
+ const href = attribs["href"] || attribs["xlink:href"];
163
+ if (typeof href === "string") {
164
+ if (href.slice(0, 1) !== "#") {
165
+ throw new Error(`Invalid link in ${analyseTagError(element)}`);
166
+ }
167
+ const id = href.slice(1);
168
+ gotElementReference(item, id, false);
169
+ }
170
+ }
171
+ Object.keys(attribs).forEach((attr) => {
172
+ let value = attribs[attr];
173
+ if (value.slice(0, 5).toLowerCase() !== "url(#") {
174
+ return;
175
+ }
176
+ value = value.slice(5);
177
+ if (value.slice(-1) !== ")") {
178
+ return;
179
+ }
180
+ const id = value.slice(0, value.length - 1).trim();
181
+ if (urlPresentationalAttributes.has(attr)) {
182
+ gotElementReference(item, id, attr !== "filter");
183
+ return;
184
+ }
185
+ if (commonColorPresentationalAttributes.has(attr) || markerAttributes.has(attr)) {
186
+ gotElementReference(item, id, false);
187
+ return;
188
+ }
189
+ });
190
+ });
191
+ links = links.filter((item) => {
192
+ const id = item.id;
193
+ if (ids[id]) {
194
+ return true;
195
+ }
196
+ function fix() {
197
+ const index2 = item.usedByIndex;
198
+ const element = elements.get(index2);
199
+ const tagName = element.tagName;
200
+ function remove() {
201
+ var _a;
202
+ const $element = cheerio(element);
203
+ const parent = element.parent;
204
+ if (parent) {
205
+ if (parent._childElements) {
206
+ parent._childElements = parent._childElements.filter((num) => num !== index2);
207
+ }
208
+ (_a = parent._belongsTo) == null ? void 0 : _a.forEach((list) => {
209
+ list.indexes.delete(index2);
210
+ });
211
+ }
212
+ $element.remove();
213
+ }
214
+ if (element._linksTo) {
215
+ element._linksTo = element._linksTo.filter((item2) => item2.id !== id);
216
+ }
217
+ if (!element.children.length) {
218
+ if (useTag.has(tagName)) {
219
+ remove();
220
+ return;
221
+ }
222
+ }
223
+ const matches = new Set(["#" + id, "url(#" + id + ")"]);
224
+ const attribs = element.attribs;
225
+ for (const attr in attribs) {
226
+ if (matches.has(attribs[attr])) {
227
+ cheerio(element).removeAttr(attr);
228
+ }
229
+ }
230
+ }
231
+ const message = `Missing element with id="${id}"`;
232
+ if (fixErrors) {
233
+ fix();
234
+ console.warn(message);
235
+ } else {
236
+ throw new Error(message);
237
+ }
238
+ return false;
239
+ });
240
+ function hasChildItem(tree2, child, canThrow) {
241
+ const item = tree2.children.find((item2) => item2.index === child.index && item2.usedAsMask === child.usedAsMask);
242
+ if (item && canThrow) {
243
+ throw new Error("Recursion");
244
+ }
245
+ return !!item;
246
+ }
247
+ const tree = {
248
+ index: 1,
249
+ usedAsMask: false,
250
+ children: []
251
+ };
252
+ function parseTreeItem(tree2, usedItems, inMask) {
253
+ var _a, _b;
254
+ const element = elements.get(tree2.index);
255
+ if (tree2.usedAsMask || inMask) {
256
+ element._usedAsMask = true;
257
+ inMask = true;
258
+ } else {
259
+ element._usedAsPaint = true;
260
+ }
261
+ usedItems = usedItems.slice(0);
262
+ usedItems.push(element._index);
263
+ (_a = element._childElements) == null ? void 0 : _a.forEach((childIndex) => {
264
+ if (usedItems.indexOf(childIndex) !== -1) {
265
+ throw new Error("Recursion");
266
+ }
267
+ const childItem = {
268
+ index: childIndex,
269
+ usedAsMask: false,
270
+ children: [],
271
+ parent: tree2
272
+ };
273
+ tree2.children.push(childItem);
274
+ parseTreeItem(childItem, usedItems, inMask);
275
+ });
276
+ (_b = element._linksTo) == null ? void 0 : _b.forEach((link) => {
277
+ const linkIndex = ids[link.id];
278
+ const usedAsMask = link.usedAsMask;
279
+ const childItem = {
280
+ index: linkIndex,
281
+ usedAsMask,
282
+ children: [],
283
+ parent: tree2
284
+ };
285
+ if (hasChildItem(tree2, childItem, false)) {
286
+ return;
287
+ }
288
+ tree2.children.push(childItem);
289
+ parseTreeItem(childItem, usedItems, inMask || usedAsMask);
290
+ });
291
+ }
292
+ parseTreeItem(tree, [0], false);
293
+ return {
294
+ elements,
295
+ ids,
296
+ links,
297
+ tree
298
+ };
299
+ }
300
+ export {
301
+ analyseSVGStructure
302
+ };
@@ -1,4 +1,4 @@
1
- import type { SVG } from '..';
1
+ import type { SVG } from '../../svg';
2
2
  /**
3
3
  * Remove useless attributes
4
4
  */
@@ -2,12 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.removeBadAttributes = void 0;
4
4
  const attributes_1 = require("../data/attributes");
5
+ const tags_1 = require("../data/tags");
5
6
  const parse_1 = require("../parse");
6
7
  /**
7
8
  * Remove useless attributes
8
9
  */
9
10
  async function removeBadAttributes(svg) {
10
11
  await (0, parse_1.parseSVG)(svg, (item) => {
12
+ const tagName = item.tagName;
11
13
  const attribs = item.element.attribs;
12
14
  const $element = item.$element;
13
15
  // Common tags
@@ -20,6 +22,12 @@ async function removeBadAttributes(svg) {
20
22
  $element.removeAttr(attr);
21
23
  return;
22
24
  }
25
+ // Attributes on <defs> aren't passed to child nodes, so remove everything)
26
+ if (tags_1.defsTag.has(tagName) &&
27
+ !attributes_1.tagSpecificPresentationalAttributes[tagName].has(attr)) {
28
+ $element.removeAttr(attr);
29
+ return;
30
+ }
23
31
  // Check for namespace
24
32
  const nsParts = attr.split(':');
25
33
  if (nsParts.length > 1) {