svg-toolbox 1.1.8 → 1.1.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/README.md CHANGED
@@ -1,9 +1,15 @@
1
+ <h1 align="center">
2
+ <br/>
3
+ <img width="200" alt="image" src="https://github.com/user-attachments/assets/5fb4fbd0-10b4-4abf-93fd-d98b17845f34" />
4
+ <br/>
5
+ </h1>
6
+
7
+
1
8
  # SVG Utility Functions
2
9
  This module provides utility functions for working with SVG elements and files, including creating, cloning, merging, converting SVG to Base64, comparing SVG images, normalizing path data, and converting SVG to PNG format.
3
10
 
4
11
  [![npm version](https://img.shields.io/npm/v/svg-toolbox.svg?style=flat-square)](https://www.npmjs.com/package/svg-toolbox)
5
12
  [![npm downloads](https://img.shields.io/npm/dt/svg-toolbox.svg?style=flat-square)](https://www.npmjs.com/package/svg-toolbox)
6
- [![deps](https://img.shields.io/github/license/SteamedBread2333/svg-toolbox.svg)](https://www.npmjs.com/package/svg-toolbox)
7
13
 
8
14
  ## Installation
9
15
  ```bash
@@ -33,10 +39,7 @@ npm install svg-toolbox
33
39
  Creates an SVG element from a given SVG content string.
34
40
 
35
41
  ```typescript
36
- import { createSVGElement } from 'svg-toolbox';
37
-
38
- const svgContent = `<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />`;
39
- const svgElement = createSVGElement(svgContent);
42
+ const svgElement = createSVGElement(`<svg><path d="M10 20L30 40Z" /></svg>`);
40
43
  console.log(svgElement);
41
44
  ```
42
45
  ### cloneSVGElement
@@ -92,25 +95,21 @@ console.log(mergedElement);
92
95
  ```
93
96
 
94
97
  ### convertSVGToBase64
95
- Converts an SVG element to a Base64-encoded string.
98
+ Converts an SVG element or SVG string to a Base64-encoded string.
96
99
 
97
100
  ```typescript
98
- import { convertSVGToBase64 } from 'svg-toolbox';
99
- import { JSDOM } from 'jsdom';
100
-
101
- const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
102
- const { document } = dom.window;
101
+ import { createSVGElement, convertSVGToBase64, convertBase64ToSVG } from 'svg-toolbox';
103
102
 
104
- const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
105
- svgElement.setAttribute('cx', '50');
106
- svgElement.setAttribute('cy', '50');
107
- svgElement.setAttribute('r', '40');
108
- svgElement.setAttribute('stroke', 'black');
109
- svgElement.setAttribute('stroke-width', '3');
110
- svgElement.setAttribute('fill', 'red');
103
+ const svgElement = createSVGElement(`<svg><path d="M10 20L30 40Z" /></svg>`);
111
104
 
112
105
  const base64String = convertSVGToBase64(svgElement);
113
- console.log(base64String);
106
+ console.log('convertSVGToBase64 param element', base64String);
107
+
108
+ const svgString = convertBase64ToSVG(base64String);
109
+ console.log('convertBase64ToSVG', svgString);
110
+
111
+ const svgBase64 = convertSVGToBase64(svgString);
112
+ console.log('convertSVGToBase64 param string', svgBase64);
114
113
  ```
115
114
 
116
115
  ### convertBase64ToSVG
@@ -169,4 +168,4 @@ svg2Png(svgPath, pngPath, scale);
169
168
  ```
170
169
 
171
170
  ## License
172
- MIT License
171
+ MIT License
package/es/common.d.ts CHANGED
@@ -13,17 +13,9 @@
13
13
  *
14
14
  * @param {Element} element - The element to create an SVG element from.
15
15
  * @returns {Element} - The created SVG element.
16
- *
17
- * @example
18
- *
19
- * @param {Element} element - The element to create an SVG element from.
20
- * @returns {Element} - The created SVG element.
21
- *
22
- * @example
23
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
24
- * const svgElement2 = createSVGElement(svgElement);
25
- * @param svgContent
26
- * @returns
16
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
17
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
18
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
27
19
  */
28
20
  export declare function createSVGElement(svgContent: string): Element;
29
21
  /**
@@ -34,12 +26,6 @@ export declare function createSVGElement(svgContent: string): Element;
34
26
  *
35
27
  * @param {Element} element - The SVG element to clone.
36
28
  * @returns {Element} - The cloned SVG element.
37
- *
38
- * @example
39
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
40
- * const clonedElement = cloneSVGElement(svgElement);
41
- * console.log(clonedElement);
42
- *
43
29
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
44
30
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
45
31
  */
@@ -52,13 +38,6 @@ export declare function cloneSVGElement(element: Element): Element;
52
38
  *
53
39
  * @param {Element[]} elements - An array of SVG elements to merge.
54
40
  * @returns {Element} - The merged SVG element.
55
- *
56
- * @example
57
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
58
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
59
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
60
- * console.log(mergedElement);
61
- *
62
41
  * @see https://www.w3.org/TR/SVG/ - SVG specification
63
42
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
64
43
  */
@@ -71,12 +50,6 @@ export declare function mergeSVGElements(elements: Element[]): Element;
71
50
  *
72
51
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
73
52
  * @returns {string} - The Base64-encoded string representation of the SVG element.
74
- *
75
- * @example
76
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
77
- * const base64String = convertSVGToBase64(svgElement);
78
- * console.log(base64String);
79
- *
80
53
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
81
54
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
82
55
  */
@@ -89,12 +62,6 @@ export declare function convertSVGToBase64(svgContent: Element | string): string
89
62
  *
90
63
  * @param {string} base64String - The Base64-encoded string to convert.
91
64
  * @returns {string} - The SVG string representation of the Base64-encoded string.
92
- *
93
- * @example
94
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
95
- * const svgString = convertBase64ToSVG(base64String);
96
- * console.log(svgString);
97
- *
98
65
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
99
66
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
100
67
  */
package/es/common.js CHANGED
@@ -9,8 +9,7 @@ import { JSDOM } from 'jsdom';
9
9
  import { isValidSvgElement, isValidSvgString } from './validate';
10
10
  // Create a virtual DOM environment
11
11
  const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
12
- const fakeWindow = dom.window;
13
- const { document } = fakeWindow;
12
+ const { document } = dom.window;
14
13
  /**
15
14
  * Creates an SVG element from a given element.
16
15
  *
@@ -19,20 +18,12 @@ const { document } = fakeWindow;
19
18
  *
20
19
  * @param {Element} element - The element to create an SVG element from.
21
20
  * @returns {Element} - The created SVG element.
22
- *
23
- * @example
24
- *
25
- * @param {Element} element - The element to create an SVG element from.
26
- * @returns {Element} - The created SVG element.
27
- *
28
- * @example
29
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
30
- * const svgElement2 = createSVGElement(svgElement);
31
- * @param svgContent
32
- * @returns
21
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
22
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
23
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
33
24
  */
34
25
  export function createSVGElement(svgContent) {
35
- const svgElement = new fakeWindow.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
26
+ const svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
36
27
  return svgElement;
37
28
  }
38
29
  /**
@@ -43,19 +34,13 @@ export function createSVGElement(svgContent) {
43
34
  *
44
35
  * @param {Element} element - The SVG element to clone.
45
36
  * @returns {Element} - The cloned SVG element.
46
- *
47
- * @example
48
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
49
- * const clonedElement = cloneSVGElement(svgElement);
50
- * console.log(clonedElement);
51
- *
52
37
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
53
38
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
54
39
  */
55
40
  export function cloneSVGElement(element) {
56
- const serializer = new fakeWindow.XMLSerializer();
41
+ const serializer = new dom.window.XMLSerializer();
57
42
  const sourceCode = serializer.serializeToString(element);
58
- const parser = new fakeWindow.DOMParser();
43
+ const parser = new dom.window.DOMParser();
59
44
  const doc = parser.parseFromString(sourceCode, 'image/svg+xml');
60
45
  return doc.documentElement;
61
46
  }
@@ -67,13 +52,6 @@ export function cloneSVGElement(element) {
67
52
  *
68
53
  * @param {Element[]} elements - An array of SVG elements to merge.
69
54
  * @returns {Element} - The merged SVG element.
70
- *
71
- * @example
72
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
73
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
74
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
75
- * console.log(mergedElement);
76
- *
77
55
  * @see https://www.w3.org/TR/SVG/ - SVG specification
78
56
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
79
57
  */
@@ -92,12 +70,6 @@ export function mergeSVGElements(elements) {
92
70
  *
93
71
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
94
72
  * @returns {string} - The Base64-encoded string representation of the SVG element.
95
- *
96
- * @example
97
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
98
- * const base64String = convertSVGToBase64(svgElement);
99
- * console.log(base64String);
100
- *
101
73
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
102
74
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
103
75
  */
@@ -107,7 +79,7 @@ export function convertSVGToBase64(svgContent) {
107
79
  svgString = svgContent;
108
80
  }
109
81
  else if (isValidSvgElement(svgContent)) {
110
- const serializer = new fakeWindow.XMLSerializer();
82
+ const serializer = new dom.window.XMLSerializer();
111
83
  svgString = serializer.serializeToString(svgContent);
112
84
  }
113
85
  else {
@@ -123,12 +95,6 @@ export function convertSVGToBase64(svgContent) {
123
95
  *
124
96
  * @param {string} base64String - The Base64-encoded string to convert.
125
97
  * @returns {string} - The SVG string representation of the Base64-encoded string.
126
- *
127
- * @example
128
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
129
- * const svgString = convertBase64ToSVG(base64String);
130
- * console.log(svgString);
131
- *
132
98
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
133
99
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
134
100
  */
package/es/common.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEjE,mCAAmC;AACnC,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACnE,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC;AA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B,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;AAClF,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAoB;IACrD,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACtF,OAAO,SAAS,CAAC;IACnB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC"}
package/es/validate.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * @file validate.ts
3
- * @description This module provides functions to validate SVG content.
4
- * @module validate
5
- * @author pipi
6
- */
7
1
  /**
8
2
  * Validates whether the provided content is a valid SVG string.
9
3
  *
package/es/validate.js CHANGED
@@ -4,6 +4,7 @@
4
4
  * @module validate
5
5
  * @author pipi
6
6
  */
7
+ import { JSDOM } from 'jsdom';
7
8
  /**
8
9
  * Validates whether the provided content is a valid SVG string.
9
10
  *
@@ -15,11 +16,18 @@ export function isValidSvgString(content) {
15
16
  if (typeof content !== 'string') {
16
17
  return false;
17
18
  }
18
- // Regular expression to find the <svg> opening tag in the string.
19
- const svgRegExp = /<svg(\s+[^>]*)?>/i;
20
- const startTag = content.match(svgRegExp);
21
- // Check if a matching <svg> tag was found and it's in lowercase (case-insensitive check).
22
- return !!(startTag && startTag[0].toLowerCase().includes('<svg'));
19
+ try {
20
+ // Create a virtual DOM environment
21
+ const dom = new JSDOM(content);
22
+ // Parse the content as SVG and check if the root node is an SVG element.
23
+ const rootNode = new dom.window.DOMParser().parseFromString(content, 'image/svg+xml').documentElement;
24
+ // Return true if the root node is an SVG element, false otherwise.
25
+ return rootNode.nodeName.toLowerCase() === 'svg';
26
+ }
27
+ catch (error) {
28
+ // If there's an error during parsing, it's not a valid SVG string.
29
+ return false;
30
+ }
23
31
  }
24
32
  /**
25
33
  * Validates whether the provided content is a valid SVG element.
@@ -31,8 +39,6 @@ export function isValidSvgElement(content) {
31
39
  // Check if the content is an instance of Element and its tag name is 'svg'
32
40
  return (content &&
33
41
  typeof content === 'object' &&
34
- content.tagName.toLowerCase() === 'svg' &&
35
- content.namespaceURI === 'http://www.w3.org/2000/svg' // SVG namespace URI
36
- );
42
+ content.tagName.toLowerCase() === 'svg');
37
43
  }
38
44
  //# sourceMappingURL=validate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAY;IAC3C,mFAAmF;IACnF,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kEAAkE;IAClE,MAAM,SAAS,GAAG,mBAAmB,CAAC;IACtC,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAE1C,0FAA0F;IAC1F,OAAO,CAAC,CAAC,CAAC,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;AACpE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAY;IAC5C,2EAA2E;IAC3E,OAAO,CACL,OAAO;QACP,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,KAAK;QACvC,OAAO,CAAC,YAAY,KAAK,4BAA4B,CAAC,oBAAoB;KAC3E,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAY;IAC3C,mFAAmF;IACnF,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,mCAAmC;QACnC,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;QAC/B,yEAAyE;QACzE,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;QACtG,mEAAmE;QACnE,OAAO,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC;IACnD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mEAAmE;QACnE,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAY;IAC5C,2EAA2E;IAC3E,OAAO,CACL,OAAO;QACP,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,KAAK,CACxC,CAAC;AACJ,CAAC"}
package/lib/common.d.ts CHANGED
@@ -13,17 +13,9 @@
13
13
  *
14
14
  * @param {Element} element - The element to create an SVG element from.
15
15
  * @returns {Element} - The created SVG element.
16
- *
17
- * @example
18
- *
19
- * @param {Element} element - The element to create an SVG element from.
20
- * @returns {Element} - The created SVG element.
21
- *
22
- * @example
23
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
24
- * const svgElement2 = createSVGElement(svgElement);
25
- * @param svgContent
26
- * @returns
16
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
17
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
18
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
27
19
  */
28
20
  export declare function createSVGElement(svgContent: string): Element;
29
21
  /**
@@ -34,12 +26,6 @@ export declare function createSVGElement(svgContent: string): Element;
34
26
  *
35
27
  * @param {Element} element - The SVG element to clone.
36
28
  * @returns {Element} - The cloned SVG element.
37
- *
38
- * @example
39
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
40
- * const clonedElement = cloneSVGElement(svgElement);
41
- * console.log(clonedElement);
42
- *
43
29
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
44
30
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
45
31
  */
@@ -52,13 +38,6 @@ export declare function cloneSVGElement(element: Element): Element;
52
38
  *
53
39
  * @param {Element[]} elements - An array of SVG elements to merge.
54
40
  * @returns {Element} - The merged SVG element.
55
- *
56
- * @example
57
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
58
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
59
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
60
- * console.log(mergedElement);
61
- *
62
41
  * @see https://www.w3.org/TR/SVG/ - SVG specification
63
42
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
64
43
  */
@@ -71,12 +50,6 @@ export declare function mergeSVGElements(elements: Element[]): Element;
71
50
  *
72
51
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
73
52
  * @returns {string} - The Base64-encoded string representation of the SVG element.
74
- *
75
- * @example
76
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
77
- * const base64String = convertSVGToBase64(svgElement);
78
- * console.log(base64String);
79
- *
80
53
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
81
54
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
82
55
  */
@@ -89,12 +62,6 @@ export declare function convertSVGToBase64(svgContent: Element | string): string
89
62
  *
90
63
  * @param {string} base64String - The Base64-encoded string to convert.
91
64
  * @returns {string} - The SVG string representation of the Base64-encoded string.
92
- *
93
- * @example
94
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
95
- * const svgString = convertBase64ToSVG(base64String);
96
- * console.log(svgString);
97
- *
98
65
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
99
66
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
100
67
  */
package/lib/common.js CHANGED
@@ -16,8 +16,7 @@ var jsdom_1 = require("jsdom");
16
16
  var validate_1 = require("./validate");
17
17
  // Create a virtual DOM environment
18
18
  var dom = new jsdom_1.JSDOM("<!DOCTYPE html><html><body></body></html>");
19
- var fakeWindow = dom.window;
20
- var document = fakeWindow.document;
19
+ var document = dom.window.document;
21
20
  /**
22
21
  * Creates an SVG element from a given element.
23
22
  *
@@ -26,20 +25,12 @@ var document = fakeWindow.document;
26
25
  *
27
26
  * @param {Element} element - The element to create an SVG element from.
28
27
  * @returns {Element} - The created SVG element.
29
- *
30
- * @example
31
- *
32
- * @param {Element} element - The element to create an SVG element from.
33
- * @returns {Element} - The created SVG element.
34
- *
35
- * @example
36
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
37
- * const svgElement2 = createSVGElement(svgElement);
38
- * @param svgContent
39
- * @returns
28
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
29
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
30
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
40
31
  */
41
32
  function createSVGElement(svgContent) {
42
- var svgElement = new fakeWindow.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
33
+ var svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
43
34
  return svgElement;
44
35
  }
45
36
  /**
@@ -50,19 +41,13 @@ function createSVGElement(svgContent) {
50
41
  *
51
42
  * @param {Element} element - The SVG element to clone.
52
43
  * @returns {Element} - The cloned SVG element.
53
- *
54
- * @example
55
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
56
- * const clonedElement = cloneSVGElement(svgElement);
57
- * console.log(clonedElement);
58
- *
59
44
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
60
45
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
61
46
  */
62
47
  function cloneSVGElement(element) {
63
- var serializer = new fakeWindow.XMLSerializer();
48
+ var serializer = new dom.window.XMLSerializer();
64
49
  var sourceCode = serializer.serializeToString(element);
65
- var parser = new fakeWindow.DOMParser();
50
+ var parser = new dom.window.DOMParser();
66
51
  var doc = parser.parseFromString(sourceCode, 'image/svg+xml');
67
52
  return doc.documentElement;
68
53
  }
@@ -74,13 +59,6 @@ function cloneSVGElement(element) {
74
59
  *
75
60
  * @param {Element[]} elements - An array of SVG elements to merge.
76
61
  * @returns {Element} - The merged SVG element.
77
- *
78
- * @example
79
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
80
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
81
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
82
- * console.log(mergedElement);
83
- *
84
62
  * @see https://www.w3.org/TR/SVG/ - SVG specification
85
63
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
86
64
  */
@@ -99,12 +77,6 @@ function mergeSVGElements(elements) {
99
77
  *
100
78
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
101
79
  * @returns {string} - The Base64-encoded string representation of the SVG element.
102
- *
103
- * @example
104
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
105
- * const base64String = convertSVGToBase64(svgElement);
106
- * console.log(base64String);
107
- *
108
80
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
109
81
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
110
82
  */
@@ -114,7 +86,7 @@ function convertSVGToBase64(svgContent) {
114
86
  svgString = svgContent;
115
87
  }
116
88
  else if ((0, validate_1.isValidSvgElement)(svgContent)) {
117
- var serializer = new fakeWindow.XMLSerializer();
89
+ var serializer = new dom.window.XMLSerializer();
118
90
  svgString = serializer.serializeToString(svgContent);
119
91
  }
120
92
  else {
@@ -130,12 +102,6 @@ function convertSVGToBase64(svgContent) {
130
102
  *
131
103
  * @param {string} base64String - The Base64-encoded string to convert.
132
104
  * @returns {string} - The SVG string representation of the Base64-encoded string.
133
- *
134
- * @example
135
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
136
- * const svgString = convertBase64ToSVG(base64String);
137
- * console.log(svgString);
138
- *
139
105
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
140
106
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
141
107
  */
package/lib/common.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AA8BH,4CAGC;AAmBD,0CAMC;AAoBD,4CAMC;AAmBD,gDAWC;AAmBD,gDAOC;AA1ID,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AAC;QACN,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,oCAA6B,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAE,CAAC;AAClF,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,kBAAkB,CAAC,YAAoB;IACrD,IAAI,CAAC;QACH,IAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACtF,OAAO,SAAS,CAAC;IACnB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC"}
package/lib/validate.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * @file validate.ts
3
- * @description This module provides functions to validate SVG content.
4
- * @module validate
5
- * @author pipi
6
- */
7
1
  /**
8
2
  * Validates whether the provided content is a valid SVG string.
9
3
  *
package/lib/validate.js CHANGED
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidSvgString = isValidSvgString;
4
+ exports.isValidSvgElement = isValidSvgElement;
2
5
  /**
3
6
  * @file validate.ts
4
7
  * @description This module provides functions to validate SVG content.
5
8
  * @module validate
6
9
  * @author pipi
7
10
  */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.isValidSvgString = isValidSvgString;
10
- exports.isValidSvgElement = isValidSvgElement;
11
+ var jsdom_1 = require("jsdom");
11
12
  /**
12
13
  * Validates whether the provided content is a valid SVG string.
13
14
  *
@@ -19,11 +20,18 @@ function isValidSvgString(content) {
19
20
  if (typeof content !== 'string') {
20
21
  return false;
21
22
  }
22
- // Regular expression to find the <svg> opening tag in the string.
23
- var svgRegExp = /<svg(\s+[^>]*)?>/i;
24
- var startTag = content.match(svgRegExp);
25
- // Check if a matching <svg> tag was found and it's in lowercase (case-insensitive check).
26
- return !!(startTag && startTag[0].toLowerCase().includes('<svg'));
23
+ try {
24
+ // Create a virtual DOM environment
25
+ var dom = new jsdom_1.JSDOM(content);
26
+ // Parse the content as SVG and check if the root node is an SVG element.
27
+ var rootNode = new dom.window.DOMParser().parseFromString(content, 'image/svg+xml').documentElement;
28
+ // Return true if the root node is an SVG element, false otherwise.
29
+ return rootNode.nodeName.toLowerCase() === 'svg';
30
+ }
31
+ catch (error) {
32
+ // If there's an error during parsing, it's not a valid SVG string.
33
+ return false;
34
+ }
27
35
  }
28
36
  /**
29
37
  * Validates whether the provided content is a valid SVG element.
@@ -35,8 +43,6 @@ function isValidSvgElement(content) {
35
43
  // Check if the content is an instance of Element and its tag name is 'svg'
36
44
  return (content &&
37
45
  typeof content === 'object' &&
38
- content.tagName.toLowerCase() === 'svg' &&
39
- content.namespaceURI === 'http://www.w3.org/2000/svg' // SVG namespace URI
40
- );
46
+ content.tagName.toLowerCase() === 'svg');
41
47
  }
42
48
  //# sourceMappingURL=validate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAQH,4CAYC;AAQD,8CAQC;AAlCD;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,OAAY;IAC3C,mFAAmF;IACnF,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kEAAkE;IAClE,IAAM,SAAS,GAAG,mBAAmB,CAAC;IACtC,IAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAE1C,0FAA0F;IAC1F,OAAO,CAAC,CAAC,CAAC,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC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": "svg-toolbox",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "This library provides some SVG-related tools",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -11,7 +11,10 @@
11
11
  "LICENSE",
12
12
  "README.md"
13
13
  ],
14
- "repository": "https://github.com/SteamedBread2333/svg-toolbox",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/SteamedBread2333/svg-toolbox.git"
17
+ },
15
18
  "scripts": {
16
19
  "build:es": "tsc -p tsconfig.json",
17
20
  "build:cjs": "tsc -p tsconfig.cjs.json",