n8n-nodes-svgr 0.1.1 → 0.2.0

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.
@@ -0,0 +1,670 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.optimizeSvg = optimizeSvg;
4
+ exports.optimizeSvgBasic = optimizeSvgBasic;
5
+ const defaultOptions = {
6
+ removeDoctype: true,
7
+ removeXMLProcInst: true,
8
+ removeComments: true,
9
+ removeMetadata: true,
10
+ removeEditorsNSData: true,
11
+ cleanupAttrs: true,
12
+ cleanupIds: false,
13
+ removeEmptyAttrs: true,
14
+ removeUnusedNS: true,
15
+ prefixIds: false,
16
+ removeTitle: false,
17
+ removeDesc: false,
18
+ removeHiddenElems: true,
19
+ removeEmptyContainers: true,
20
+ removeEmptyText: true,
21
+ removeUselessDefs: false,
22
+ convertColors: true,
23
+ minifyStyles: true,
24
+ inlineStyles: false,
25
+ mergeStyles: false,
26
+ cleanupNumericValues: true,
27
+ collapseGroups: true,
28
+ moveElemsAttrsToGroup: false,
29
+ moveGroupAttrsToElems: false,
30
+ convertPathData: false,
31
+ convertTransform: false,
32
+ convertShapeToPath: false,
33
+ mergePaths: false,
34
+ removeXmlns: true,
35
+ removeStyleAttr: true,
36
+ removeShapeRendering: true,
37
+ removeDimensions: false,
38
+ removeViewBox: false,
39
+ };
40
+ function removeDoctype(svg) {
41
+ return svg.replace(/<!DOCTYPE[^>]*>/gi, '');
42
+ }
43
+ function removeXMLProcInst(svg) {
44
+ return svg.replace(/<\?xml[^?]*\?>\s*/gi, '');
45
+ }
46
+ function removeComments(svg) {
47
+ return svg.replace(/<!--[\s\S]*?-->/g, '');
48
+ }
49
+ function removeMetadata(svg) {
50
+ return svg.replace(/<metadata[\s\S]*?<\/metadata>/gi, '');
51
+ }
52
+ function removeEditorsNSData(svg) {
53
+ let result = svg;
54
+ result = result.replace(/\s+xmlns:inkscape="[^"]*"/gi, '');
55
+ result = result.replace(/\s+inkscape:[a-z-]+="[^"]*"/gi, '');
56
+ result = result.replace(/\s+xmlns:sodipodi="[^"]*"/gi, '');
57
+ result = result.replace(/\s+sodipodi:[a-z-]+="[^"]*"/gi, '');
58
+ result = result.replace(/\s+xmlns:sketch="[^"]*"/gi, '');
59
+ result = result.replace(/\s+sketch:[a-z-]+="[^"]*"/gi, '');
60
+ result = result.replace(/\s+xmlns:i="[^"]*"/gi, '');
61
+ result = result.replace(/\s+i:[a-z-]+="[^"]*"/gi, '');
62
+ result = result.replace(/\s+xmlns:x="[^"]*"/gi, '');
63
+ result = result.replace(/\s+data-figma[a-z-]*="[^"]*"/gi, '');
64
+ result = result.replace(/\s+data-name="[^"]*"/gi, '');
65
+ return result;
66
+ }
67
+ function cleanupAttrs(svg) {
68
+ return svg.replace(/(\w+)="([^"]*)"/g, (match, attr, value) => {
69
+ const cleaned = value.replace(/\s+/g, ' ').trim();
70
+ return `${attr}="${cleaned}"`;
71
+ });
72
+ }
73
+ function removeEmptyAttrs(svg) {
74
+ return svg.replace(/\s+[\w-]+=""/g, '');
75
+ }
76
+ function removeUnusedNS(svg) {
77
+ let result = svg;
78
+ result = result.replace(/\s+xmlns="[^"]*"/g, '');
79
+ if (!result.includes('xlink:') && !result.includes('xlinkHref')) {
80
+ result = result.replace(/\s+xmlns:xlink="[^"]*"/g, '');
81
+ }
82
+ return result;
83
+ }
84
+ function prefixIds(svg, prefix) {
85
+ if (!prefix)
86
+ return svg;
87
+ let result = svg;
88
+ result = result.replace(/\bid="([^"]*)"/g, `id="${prefix}$1"`);
89
+ result = result.replace(/url\(#([^)]*)\)/g, `url(#${prefix}$1)`);
90
+ result = result.replace(/href="#([^"]*)"/g, `href="#${prefix}$1"`);
91
+ result = result.replace(/xlink:href="#([^"]*)"/g, `xlink:href="#${prefix}$1"`);
92
+ result = result.replace(/\bclass="([^"]*)"/g, (match, classes) => {
93
+ const prefixedClasses = classes
94
+ .split(/\s+/)
95
+ .map((cls) => (cls ? `${prefix}${cls}` : ''))
96
+ .join(' ');
97
+ return `class="${prefixedClasses}"`;
98
+ });
99
+ return result;
100
+ }
101
+ function cleanupIds(svg) {
102
+ let result = svg;
103
+ const idMatches = result.matchAll(/\bid="([^"]*)"/g);
104
+ const definedIds = new Set();
105
+ for (const match of idMatches) {
106
+ definedIds.add(match[1]);
107
+ }
108
+ const referencedIds = new Set();
109
+ const urlRefs = result.matchAll(/url\(#([^)]*)\)/g);
110
+ for (const match of urlRefs) {
111
+ referencedIds.add(match[1]);
112
+ }
113
+ const hrefRefs = result.matchAll(/href="#([^"]*)"/g);
114
+ for (const match of hrefRefs) {
115
+ referencedIds.add(match[1]);
116
+ }
117
+ const xlinkRefs = result.matchAll(/xlink:href="#([^"]*)"/g);
118
+ for (const match of xlinkRefs) {
119
+ referencedIds.add(match[1]);
120
+ }
121
+ for (const id of definedIds) {
122
+ if (!referencedIds.has(id)) {
123
+ result = result.replace(new RegExp(`\\s+id="${id}"`, 'g'), '');
124
+ }
125
+ }
126
+ return result;
127
+ }
128
+ function removeTitle(svg) {
129
+ return svg.replace(/<title[\s\S]*?<\/title>/gi, '');
130
+ }
131
+ function removeDesc(svg) {
132
+ return svg.replace(/<desc[\s\S]*?<\/desc>/gi, '');
133
+ }
134
+ function removeHiddenElems(svg) {
135
+ let result = svg;
136
+ result = result.replace(/<[^>]+display\s*:\s*none[^>]*>[\s\S]*?<\/[^>]+>/gi, '');
137
+ result = result.replace(/<[^>]+display\s*=\s*["']none["'][^>]*\/?>/gi, '');
138
+ result = result.replace(/<[^>]+visibility\s*:\s*hidden[^>]*>[\s\S]*?<\/[^>]+>/gi, '');
139
+ result = result.replace(/<[^>]+visibility\s*=\s*["']hidden["'][^>]*\/?>/gi, '');
140
+ result = result.replace(/<[^>]+\s+opacity\s*=\s*["']0["'][^>]*>[\s\S]*?<\/[^>]+>/gi, '');
141
+ result = result.replace(/<[^>]+\s+opacity\s*=\s*["']0["'][^>]*\/>/gi, '');
142
+ return result;
143
+ }
144
+ function removeEmptyContainers(svg) {
145
+ let result = svg;
146
+ let prevResult = '';
147
+ while (result !== prevResult) {
148
+ prevResult = result;
149
+ result = result.replace(/<g[^>]*>\s*<\/g>/gi, '');
150
+ result = result.replace(/<defs[^>]*>\s*<\/defs>/gi, '');
151
+ result = result.replace(/<pattern[^>]*>\s*<\/pattern>/gi, '');
152
+ result = result.replace(/<clipPath[^>]*>\s*<\/clipPath>/gi, '');
153
+ result = result.replace(/<mask[^>]*>\s*<\/mask>/gi, '');
154
+ result = result.replace(/<symbol[^>]*>\s*<\/symbol>/gi, '');
155
+ }
156
+ return result;
157
+ }
158
+ function removeEmptyText(svg) {
159
+ return svg.replace(/<text[^>]*>\s*<\/text>/gi, '');
160
+ }
161
+ function removeUselessDefs(svg) {
162
+ let result = svg;
163
+ const defsMatch = result.match(/<defs[^>]*>([\s\S]*?)<\/defs>/gi);
164
+ if (!defsMatch)
165
+ return result;
166
+ const referencedIds = new Set();
167
+ const urlRefs = result.matchAll(/url\(#([^)]*)\)/g);
168
+ for (const match of urlRefs) {
169
+ referencedIds.add(match[1]);
170
+ }
171
+ const hrefRefs = result.matchAll(/href="#([^"]*)"/g);
172
+ for (const match of hrefRefs) {
173
+ referencedIds.add(match[1]);
174
+ }
175
+ const xlinkRefs = result.matchAll(/xlink:href="#([^"]*)"/g);
176
+ for (const match of xlinkRefs) {
177
+ referencedIds.add(match[1]);
178
+ }
179
+ for (const defsContent of defsMatch) {
180
+ let newDefsContent = defsContent;
181
+ const elementsWithId = defsContent.matchAll(/<(\w+)[^>]*\s+id="([^"]*)"[^>]*(?:\/>|>[\s\S]*?<\/\1>)/gi);
182
+ for (const elemMatch of elementsWithId) {
183
+ const fullElement = elemMatch[0];
184
+ const elementId = elemMatch[2];
185
+ if (!referencedIds.has(elementId)) {
186
+ newDefsContent = newDefsContent.replace(fullElement, '');
187
+ }
188
+ }
189
+ result = result.replace(defsContent, newDefsContent);
190
+ }
191
+ return result;
192
+ }
193
+ function convertColors(svg) {
194
+ let result = svg;
195
+ result = result.replace(/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/gi, (_, r, g, b) => {
196
+ const toHex = (n) => parseInt(n, 10).toString(16).padStart(2, '0');
197
+ return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
198
+ });
199
+ const colorMap = {
200
+ '#000000': '#000',
201
+ '#ffffff': '#fff',
202
+ '#ff0000': 'red',
203
+ '#00ff00': 'lime',
204
+ '#0000ff': 'blue',
205
+ '#ffff00': 'yellow',
206
+ '#00ffff': 'cyan',
207
+ '#ff00ff': 'magenta',
208
+ };
209
+ for (const [hex, name] of Object.entries(colorMap)) {
210
+ const regex = new RegExp(hex, 'gi');
211
+ result = result.replace(regex, name);
212
+ }
213
+ result = result.replace(/#([0-9a-fA-F])\1([0-9a-fA-F])\2([0-9a-fA-F])\3/gi, '#$1$2$3');
214
+ return result;
215
+ }
216
+ function minifyStyles(svg) {
217
+ return svg.replace(/<style[^>]*>([\s\S]*?)<\/style>/gi, (match, content) => {
218
+ let minified = content;
219
+ minified = minified.replace(/\/\*[\s\S]*?\*\//g, '');
220
+ minified = minified.replace(/\s+/g, ' ');
221
+ minified = minified.replace(/;\s+/g, ';');
222
+ minified = minified.replace(/:\s+/g, ':');
223
+ minified = minified.replace(/\s*{\s*/g, '{');
224
+ minified = minified.replace(/\s*}\s*/g, '}');
225
+ minified = minified.trim();
226
+ return `<style>${minified}</style>`;
227
+ });
228
+ }
229
+ function inlineStyles(svg) {
230
+ let result = svg;
231
+ const styleMatch = result.match(/<style[^>]*>([\s\S]*?)<\/style>/i);
232
+ if (!styleMatch)
233
+ return result;
234
+ const styleContent = styleMatch[1];
235
+ const rules = {};
236
+ const ruleMatches = styleContent.matchAll(/([.#]?[\w-]+)\s*\{\s*([^}]*?)\s*\}/g);
237
+ for (const match of ruleMatches) {
238
+ const selector = match[1].trim();
239
+ let declarations = match[2].trim();
240
+ if (declarations.endsWith(';')) {
241
+ declarations = declarations.slice(0, -1);
242
+ }
243
+ rules[selector] = declarations;
244
+ }
245
+ for (const [selector, declarations] of Object.entries(rules)) {
246
+ if (selector.startsWith('.')) {
247
+ const className = selector.slice(1);
248
+ result = result.replace(new RegExp(`(<\\w+\\s+[^>]*?class="[^"]*${className}[^"]*"[^>]*?)(\\s*\\/?>)`, 'gi'), (match, before, closing) => {
249
+ if (before.includes('style="')) {
250
+ return before.replace(/style="([^"]*)"/, `style="$1;${declarations}"`) + closing;
251
+ }
252
+ return `${before} style="${declarations}"${closing}`;
253
+ });
254
+ }
255
+ }
256
+ for (const [selector, declarations] of Object.entries(rules)) {
257
+ if (selector.startsWith('#')) {
258
+ const idName = selector.slice(1);
259
+ result = result.replace(new RegExp(`(<[^>]*\\bid="${idName}"[^>]*?)(\\s*\\/?>)`, 'gi'), (match, before, closing) => {
260
+ if (before.includes('style="')) {
261
+ return before.replace(/style="([^"]*)"/, `style="$1;${declarations}"`) + closing;
262
+ }
263
+ return `${before} style="${declarations}"${closing}`;
264
+ });
265
+ }
266
+ }
267
+ for (const [selector, declarations] of Object.entries(rules)) {
268
+ if (!selector.startsWith('.') && !selector.startsWith('#')) {
269
+ result = result.replace(new RegExp(`(<${selector}\\b[^>]*?)(\\s*\\/?>)`, 'gi'), (match, before, closing) => {
270
+ if (before.includes('style="')) {
271
+ return before.replace(/style="([^"]*)"/, `style="$1;${declarations}"`) + closing;
272
+ }
273
+ return `${before} style="${declarations}"${closing}`;
274
+ });
275
+ }
276
+ }
277
+ result = result.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '');
278
+ return result;
279
+ }
280
+ function mergeStyles(svg) {
281
+ const styleMatches = svg.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/gi);
282
+ const allStyles = [];
283
+ for (const match of styleMatches) {
284
+ allStyles.push(match[1].trim());
285
+ }
286
+ if (allStyles.length <= 1)
287
+ return svg;
288
+ let result = svg.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '');
289
+ const mergedStyle = `<style>${allStyles.join('')}</style>`;
290
+ result = result.replace(/(<svg[^>]*>)/, `$1${mergedStyle}`);
291
+ return result;
292
+ }
293
+ function cleanupNumericValues(svg) {
294
+ let result = svg;
295
+ result = result.replace(/="([^"]*)"/g, (match, value) => {
296
+ let cleaned = value;
297
+ cleaned = cleaned.replace(/\b0+\.(\d)/g, '.$1');
298
+ cleaned = cleaned.replace(/(\d)\.0+\b/g, '$1');
299
+ cleaned = cleaned.replace(/\b0(px|em|ex|pt|pc|cm|mm|in|%)\b/gi, '0');
300
+ cleaned = cleaned.replace(/\s+/g, ' ');
301
+ return `="${cleaned}"`;
302
+ });
303
+ return result;
304
+ }
305
+ function collapseGroups(svg) {
306
+ let result = svg;
307
+ let prevResult = '';
308
+ while (result !== prevResult) {
309
+ prevResult = result;
310
+ result = result.replace(/<g>\s*([\s\S]*?)\s*<\/g>/gi, '$1');
311
+ }
312
+ return result;
313
+ }
314
+ function moveElemsAttrsToGroup(svg) {
315
+ let result = svg;
316
+ const presentationAttrs = [
317
+ 'fill',
318
+ 'stroke',
319
+ 'stroke-width',
320
+ 'stroke-linecap',
321
+ 'stroke-linejoin',
322
+ 'stroke-dasharray',
323
+ 'stroke-dashoffset',
324
+ 'stroke-miterlimit',
325
+ 'stroke-opacity',
326
+ 'fill-opacity',
327
+ 'fill-rule',
328
+ 'opacity',
329
+ 'font-family',
330
+ 'font-size',
331
+ 'font-weight',
332
+ 'text-anchor',
333
+ 'dominant-baseline',
334
+ ];
335
+ result = result.replace(/<g([^>]*)>([\s\S]*?)<\/g>/gi, (match, groupAttrs, content) => {
336
+ var _a;
337
+ const childElements = content.match(/<\w+[^>]*(?:\/>|>[^<]*<\/\w+>)/g);
338
+ if (!childElements || childElements.length < 2)
339
+ return match;
340
+ const childAttrsArray = [];
341
+ for (const child of childElements) {
342
+ const attrs = {};
343
+ const attrMatches = child.matchAll(/\s([\w-]+)="([^"]*)"/g);
344
+ for (const attrMatch of attrMatches) {
345
+ if (presentationAttrs.includes(attrMatch[1])) {
346
+ attrs[attrMatch[1]] = attrMatch[2];
347
+ }
348
+ }
349
+ childAttrsArray.push(attrs);
350
+ }
351
+ const commonAttrs = {};
352
+ for (const attr of presentationAttrs) {
353
+ const firstValue = (_a = childAttrsArray[0]) === null || _a === void 0 ? void 0 : _a[attr];
354
+ if (firstValue && childAttrsArray.every((attrs) => attrs[attr] === firstValue)) {
355
+ commonAttrs[attr] = firstValue;
356
+ }
357
+ }
358
+ if (Object.keys(commonAttrs).length === 0)
359
+ return match;
360
+ let newContent = content;
361
+ for (const [attr, value] of Object.entries(commonAttrs)) {
362
+ newContent = newContent.replace(new RegExp(`\\s${attr}="${value}"`, 'g'), '');
363
+ }
364
+ const commonAttrsStr = Object.entries(commonAttrs)
365
+ .map(([attr, value]) => ` ${attr}="${value}"`)
366
+ .join('');
367
+ return `<g${groupAttrs}${commonAttrsStr}>${newContent}</g>`;
368
+ });
369
+ return result;
370
+ }
371
+ function moveGroupAttrsToElems(svg) {
372
+ let result = svg;
373
+ const presentationAttrs = [
374
+ 'fill',
375
+ 'stroke',
376
+ 'stroke-width',
377
+ 'stroke-linecap',
378
+ 'stroke-linejoin',
379
+ 'stroke-dasharray',
380
+ 'stroke-dashoffset',
381
+ 'stroke-miterlimit',
382
+ 'stroke-opacity',
383
+ 'fill-opacity',
384
+ 'fill-rule',
385
+ 'opacity',
386
+ 'font-family',
387
+ 'font-size',
388
+ 'font-weight',
389
+ 'text-anchor',
390
+ 'dominant-baseline',
391
+ ];
392
+ result = result.replace(/<g([^>]*)>([\s\S]*?)<\/g>/gi, (match, groupAttrs, content) => {
393
+ const groupPresentationAttrs = {};
394
+ let remainingGroupAttrs = groupAttrs;
395
+ for (const attr of presentationAttrs) {
396
+ const attrMatch = groupAttrs.match(new RegExp(`\\s${attr}="([^"]*)"`));
397
+ if (attrMatch) {
398
+ groupPresentationAttrs[attr] = attrMatch[1];
399
+ remainingGroupAttrs = remainingGroupAttrs.replace(attrMatch[0], '');
400
+ }
401
+ }
402
+ if (Object.keys(groupPresentationAttrs).length === 0)
403
+ return match;
404
+ const newContent = content.replace(/<(\w+)((?:\s+[\w-]+="[^"]*")*)\s*(\/?)>/g, (_elemMatch, tag, attrs, selfClose) => {
405
+ let newAttrs = attrs;
406
+ for (const [attr, value] of Object.entries(groupPresentationAttrs)) {
407
+ if (!attrs.includes(`${attr}="`)) {
408
+ newAttrs += ` ${attr}="${value}"`;
409
+ }
410
+ }
411
+ return `<${tag}${newAttrs}${selfClose ? ' />' : '>'}`;
412
+ });
413
+ return `<g${remainingGroupAttrs}>${newContent}</g>`;
414
+ });
415
+ return result;
416
+ }
417
+ function convertPathData(svg) {
418
+ return svg.replace(/\sd="([^"]*)"/g, (match, d) => {
419
+ let optimized = d;
420
+ optimized = optimized.replace(/\s+/g, ' ');
421
+ optimized = optimized.replace(/([MLHVCSQTAZ])\s+/gi, '$1');
422
+ optimized = optimized.replace(/\s+([MLHVCSQTAZ])/gi, '$1');
423
+ optimized = optimized.replace(/(\d)\s+(-)/g, '$1$2');
424
+ optimized = optimized.replace(/\s*,\s*/g, ',');
425
+ optimized = optimized.replace(/,/g, ' ');
426
+ optimized = optimized.replace(/\s+/g, ' ').trim();
427
+ optimized = optimized.replace(/\b0+\.(\d)/g, '.$1');
428
+ optimized = optimized.replace(/(\.\d*?)0+(\s|$|[A-Za-z])/g, '$1$2');
429
+ optimized = optimized.replace(/\.(\s|$|[A-Za-z])/g, '$1');
430
+ return ` d="${optimized}"`;
431
+ });
432
+ }
433
+ function convertTransform(svg) {
434
+ return svg.replace(/\stransform="([^"]*)"/g, (match, transform) => {
435
+ let optimized = transform;
436
+ optimized = optimized.replace(/translate\(\s*0\s*(,\s*0\s*)?\)/gi, '');
437
+ optimized = optimized.replace(/rotate\(\s*0\s*(,\s*[\d.]+\s*,\s*[\d.]+\s*)?\)/gi, '');
438
+ optimized = optimized.replace(/scale\(\s*1\s*(,\s*1\s*)?\)/gi, '');
439
+ optimized = optimized.replace(/skew[XY]\(\s*0\s*\)/gi, '');
440
+ optimized = optimized.replace(/\s+/g, ' ').trim();
441
+ if (!optimized)
442
+ return '';
443
+ return ` transform="${optimized}"`;
444
+ });
445
+ }
446
+ function convertShapeToPath(svg) {
447
+ let result = svg;
448
+ result = result.replace(/<rect([^>]*)\/>/gi, (match, attrs) => {
449
+ var _a, _b, _c, _d, _e, _f;
450
+ const x = parseFloat(((_a = attrs.match(/\sx="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1]) || '0');
451
+ const y = parseFloat(((_b = attrs.match(/\sy="([^"]*)"/)) === null || _b === void 0 ? void 0 : _b[1]) || '0');
452
+ const width = parseFloat(((_c = attrs.match(/\swidth="([^"]*)"/)) === null || _c === void 0 ? void 0 : _c[1]) || '0');
453
+ const height = parseFloat(((_d = attrs.match(/\sheight="([^"]*)"/)) === null || _d === void 0 ? void 0 : _d[1]) || '0');
454
+ const rx = parseFloat(((_e = attrs.match(/\srx="([^"]*)"/)) === null || _e === void 0 ? void 0 : _e[1]) || '0');
455
+ const ry = parseFloat(((_f = attrs.match(/\sry="([^"]*)"/)) === null || _f === void 0 ? void 0 : _f[1]) || rx.toString());
456
+ if (width === 0 || height === 0)
457
+ return match;
458
+ const otherAttrs = attrs
459
+ .replace(/\s[xy]="[^"]*"/g, '')
460
+ .replace(/\swidth="[^"]*"/g, '')
461
+ .replace(/\sheight="[^"]*"/g, '')
462
+ .replace(/\sr[xy]="[^"]*"/g, '');
463
+ let d;
464
+ if (rx > 0 || ry > 0) {
465
+ const r = Math.min(rx, width / 2, height / 2);
466
+ d = `M${x + r},${y}h${width - 2 * r}a${r},${r} 0 0 1 ${r},${r}v${height - 2 * r}a${r},${r} 0 0 1 -${r},${r}h-${width - 2 * r}a${r},${r} 0 0 1 -${r},-${r}v-${height - 2 * r}a${r},${r} 0 0 1 ${r},-${r}z`;
467
+ }
468
+ else {
469
+ d = `M${x},${y}h${width}v${height}h-${width}z`;
470
+ }
471
+ return `<path${otherAttrs} d="${d}"/>`;
472
+ });
473
+ result = result.replace(/<circle([^>]*)\/>/gi, (match, attrs) => {
474
+ var _a, _b, _c;
475
+ const cx = parseFloat(((_a = attrs.match(/\scx="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1]) || '0');
476
+ const cy = parseFloat(((_b = attrs.match(/\scy="([^"]*)"/)) === null || _b === void 0 ? void 0 : _b[1]) || '0');
477
+ const r = parseFloat(((_c = attrs.match(/\sr="([^"]*)"/)) === null || _c === void 0 ? void 0 : _c[1]) || '0');
478
+ if (r === 0)
479
+ return match;
480
+ const otherAttrs = attrs.replace(/\sc[xy]="[^"]*"/g, '').replace(/\sr="[^"]*"/g, '');
481
+ const d = `M${cx - r},${cy}a${r},${r} 0 1 0 ${2 * r},0a${r},${r} 0 1 0 -${2 * r},0`;
482
+ return `<path${otherAttrs} d="${d}"/>`;
483
+ });
484
+ result = result.replace(/<ellipse([^>]*)\/>/gi, (match, attrs) => {
485
+ var _a, _b, _c, _d;
486
+ const cx = parseFloat(((_a = attrs.match(/\scx="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1]) || '0');
487
+ const cy = parseFloat(((_b = attrs.match(/\scy="([^"]*)"/)) === null || _b === void 0 ? void 0 : _b[1]) || '0');
488
+ const rx = parseFloat(((_c = attrs.match(/\srx="([^"]*)"/)) === null || _c === void 0 ? void 0 : _c[1]) || '0');
489
+ const ry = parseFloat(((_d = attrs.match(/\sry="([^"]*)"/)) === null || _d === void 0 ? void 0 : _d[1]) || '0');
490
+ if (rx === 0 || ry === 0)
491
+ return match;
492
+ const otherAttrs = attrs.replace(/\sc[xy]="[^"]*"/g, '').replace(/\sr[xy]="[^"]*"/g, '');
493
+ const d = `M${cx - rx},${cy}a${rx},${ry} 0 1 0 ${2 * rx},0a${rx},${ry} 0 1 0 -${2 * rx},0`;
494
+ return `<path${otherAttrs} d="${d}"/>`;
495
+ });
496
+ result = result.replace(/<line([^>]*)\/>/gi, (match, attrs) => {
497
+ var _a, _b, _c, _d;
498
+ const x1 = parseFloat(((_a = attrs.match(/\sx1="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1]) || '0');
499
+ const y1 = parseFloat(((_b = attrs.match(/\sy1="([^"]*)"/)) === null || _b === void 0 ? void 0 : _b[1]) || '0');
500
+ const x2 = parseFloat(((_c = attrs.match(/\sx2="([^"]*)"/)) === null || _c === void 0 ? void 0 : _c[1]) || '0');
501
+ const y2 = parseFloat(((_d = attrs.match(/\sy2="([^"]*)"/)) === null || _d === void 0 ? void 0 : _d[1]) || '0');
502
+ const otherAttrs = attrs.replace(/\s[xy][12]="[^"]*"/g, '');
503
+ const d = `M${x1},${y1}L${x2},${y2}`;
504
+ return `<path${otherAttrs} d="${d}"/>`;
505
+ });
506
+ result = result.replace(/<polygon([^>]*)\/>/gi, (match, attrs) => {
507
+ var _a;
508
+ const points = (_a = attrs.match(/\spoints="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1];
509
+ if (!points)
510
+ return match;
511
+ const otherAttrs = attrs.replace(/\spoints="[^"]*"/g, '');
512
+ const pointArray = points.trim().split(/[\s,]+/);
513
+ if (pointArray.length < 4)
514
+ return match;
515
+ let d = `M${pointArray[0]},${pointArray[1]}`;
516
+ for (let i = 2; i < pointArray.length; i += 2) {
517
+ d += `L${pointArray[i]},${pointArray[i + 1]}`;
518
+ }
519
+ d += 'z';
520
+ return `<path${otherAttrs} d="${d}"/>`;
521
+ });
522
+ result = result.replace(/<polyline([^>]*)\/>/gi, (match, attrs) => {
523
+ var _a;
524
+ const points = (_a = attrs.match(/\spoints="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1];
525
+ if (!points)
526
+ return match;
527
+ const otherAttrs = attrs.replace(/\spoints="[^"]*"/g, '');
528
+ const pointArray = points.trim().split(/[\s,]+/);
529
+ if (pointArray.length < 4)
530
+ return match;
531
+ let d = `M${pointArray[0]},${pointArray[1]}`;
532
+ for (let i = 2; i < pointArray.length; i += 2) {
533
+ d += `L${pointArray[i]},${pointArray[i + 1]}`;
534
+ }
535
+ return `<path${otherAttrs} d="${d}"/>`;
536
+ });
537
+ return result;
538
+ }
539
+ function mergePaths(svg) {
540
+ let result = svg;
541
+ result = result.replace(/(<path\s+)([^>]*d="[^"]*")([^>]*)(\/?>)\s*(<path\s+)([^>]*d="[^"]*")([^>]*)(\/?>)/gi, (match, p1Start, p1D, p1Rest, p1End, p2Start, p2D, p2Rest) => {
542
+ var _a, _b;
543
+ const attrs1 = (p1Rest || '').trim();
544
+ const attrs2 = (p2Rest || '').trim();
545
+ if (attrs1 !== attrs2)
546
+ return match;
547
+ const d1 = ((_a = p1D.match(/d="([^"]*)"/)) === null || _a === void 0 ? void 0 : _a[1]) || '';
548
+ const d2 = ((_b = p2D.match(/d="([^"]*)"/)) === null || _b === void 0 ? void 0 : _b[1]) || '';
549
+ const mergedD = `${d1} ${d2}`;
550
+ return `${p1Start}d="${mergedD}"${p1Rest}${p1End}`;
551
+ });
552
+ return result;
553
+ }
554
+ function removeStyleAttr(svg) {
555
+ return svg.replace(/\s+style="[^"]*"/g, '');
556
+ }
557
+ function removeShapeRendering(svg) {
558
+ let result = svg;
559
+ result = result.replace(/\s+shape-rendering="[^"]*"/g, '');
560
+ result = result.replace(/\s+shapeRendering="[^"]*"/g, '');
561
+ return result;
562
+ }
563
+ function removeDimensions(svg) {
564
+ let result = svg;
565
+ result = result.replace(/\s+width="[^"]*"/g, '');
566
+ result = result.replace(/\s+height="[^"]*"/g, '');
567
+ return result;
568
+ }
569
+ function removeViewBox(svg) {
570
+ return svg.replace(/\s+viewBox="[^"]*"/g, '');
571
+ }
572
+ function optimizeSvg(svg, options = {}) {
573
+ const opts = { ...defaultOptions, ...options };
574
+ let result = svg.trim();
575
+ const shouldRemoveNS = options.removeUnusedNS !== false && (opts.removeUnusedNS || opts.removeXmlns);
576
+ if (opts.removeDoctype)
577
+ result = removeDoctype(result);
578
+ if (opts.removeXMLProcInst)
579
+ result = removeXMLProcInst(result);
580
+ if (opts.removeComments)
581
+ result = removeComments(result);
582
+ if (opts.removeMetadata)
583
+ result = removeMetadata(result);
584
+ if (opts.removeEditorsNSData)
585
+ result = removeEditorsNSData(result);
586
+ if (opts.cleanupAttrs)
587
+ result = cleanupAttrs(result);
588
+ if (opts.cleanupIds)
589
+ result = cleanupIds(result);
590
+ if (opts.removeEmptyAttrs)
591
+ result = removeEmptyAttrs(result);
592
+ if (shouldRemoveNS)
593
+ result = removeUnusedNS(result);
594
+ if (opts.prefixIds) {
595
+ const prefix = typeof opts.prefixIds === 'string' ? opts.prefixIds : 'svgr_';
596
+ result = prefixIds(result, prefix);
597
+ }
598
+ if (opts.removeTitle)
599
+ result = removeTitle(result);
600
+ if (opts.removeDesc)
601
+ result = removeDesc(result);
602
+ if (opts.removeHiddenElems)
603
+ result = removeHiddenElems(result);
604
+ if (opts.removeEmptyText)
605
+ result = removeEmptyText(result);
606
+ if (opts.removeUselessDefs)
607
+ result = removeUselessDefs(result);
608
+ if (opts.convertColors)
609
+ result = convertColors(result);
610
+ if (opts.mergeStyles)
611
+ result = mergeStyles(result);
612
+ if (opts.minifyStyles)
613
+ result = minifyStyles(result);
614
+ if (opts.inlineStyles)
615
+ result = inlineStyles(result);
616
+ if (opts.cleanupNumericValues)
617
+ result = cleanupNumericValues(result);
618
+ if (opts.moveElemsAttrsToGroup)
619
+ result = moveElemsAttrsToGroup(result);
620
+ if (opts.moveGroupAttrsToElems)
621
+ result = moveGroupAttrsToElems(result);
622
+ if (opts.collapseGroups)
623
+ result = collapseGroups(result);
624
+ if (opts.removeEmptyContainers)
625
+ result = removeEmptyContainers(result);
626
+ if (opts.convertShapeToPath)
627
+ result = convertShapeToPath(result);
628
+ if (opts.convertPathData)
629
+ result = convertPathData(result);
630
+ if (opts.convertTransform)
631
+ result = convertTransform(result);
632
+ if (opts.mergePaths)
633
+ result = mergePaths(result);
634
+ if (opts.removeStyleAttr && !opts.inlineStyles)
635
+ result = removeStyleAttr(result);
636
+ if (opts.removeShapeRendering)
637
+ result = removeShapeRendering(result);
638
+ if (opts.removeDimensions)
639
+ result = removeDimensions(result);
640
+ if (opts.removeViewBox)
641
+ result = removeViewBox(result);
642
+ return result;
643
+ }
644
+ function optimizeSvgBasic(svg, options = {}) {
645
+ return optimizeSvg(svg, {
646
+ removeDoctype: true,
647
+ removeXMLProcInst: true,
648
+ removeComments: true,
649
+ removeMetadata: true,
650
+ removeEditorsNSData: true,
651
+ removeUnusedNS: true,
652
+ removeStyleAttr: true,
653
+ removeShapeRendering: true,
654
+ removeDimensions: options.icon || !options.dimensions,
655
+ removeViewBox: options.removeViewBox,
656
+ cleanupAttrs: false,
657
+ removeEmptyAttrs: false,
658
+ prefixIds: false,
659
+ removeTitle: false,
660
+ removeDesc: false,
661
+ removeHiddenElems: false,
662
+ removeEmptyContainers: false,
663
+ removeEmptyText: false,
664
+ convertColors: false,
665
+ minifyStyles: false,
666
+ cleanupNumericValues: false,
667
+ collapseGroups: false,
668
+ });
669
+ }
670
+ //# sourceMappingURL=svgo.js.map