svg-toolbox 1.1.7 → 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 +19 -20
- package/es/common.d.ts +7 -40
- package/es/common.js +26 -44
- package/es/common.js.map +1 -1
- package/es/validate.d.ts +14 -0
- package/es/validate.js +44 -0
- package/es/validate.js.map +1 -0
- package/lib/common.d.ts +7 -40
- package/lib/common.js +26 -44
- package/lib/common.js.map +1 -1
- package/lib/validate.d.ts +14 -0
- package/lib/validate.js +48 -0
- package/lib/validate.js.map +1 -0
- package/package.json +5 -2
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
|
[](https://www.npmjs.com/package/svg-toolbox)
|
|
5
12
|
[](https://www.npmjs.com/package/svg-toolbox)
|
|
6
|
-
[](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
|
-
|
|
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 =
|
|
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
|
-
* @
|
|
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,35 +38,22 @@ 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
|
*/
|
|
65
44
|
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
45
|
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
46
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
68
47
|
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
48
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
70
49
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
50
|
*
|
|
72
|
-
* @param {Element}
|
|
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
|
*/
|
|
83
|
-
export declare function convertSVGToBase64(
|
|
56
|
+
export declare function convertSVGToBase64(svgContent: Element | string): string;
|
|
84
57
|
/**
|
|
85
58
|
* Converts a Base64-encoded string to an SVG string.
|
|
86
59
|
*
|
|
@@ -89,12 +62,6 @@ export declare function convertSVGToBase64(svgElement: Element): 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
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @author pipi
|
|
7
7
|
*/
|
|
8
8
|
import { JSDOM } from 'jsdom';
|
|
9
|
+
import { isValidSvgElement, isValidSvgString } from './validate';
|
|
9
10
|
// Create a virtual DOM environment
|
|
10
11
|
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
|
|
11
12
|
const { document } = dom.window;
|
|
@@ -17,17 +18,9 @@ const { document } = dom.window;
|
|
|
17
18
|
*
|
|
18
19
|
* @param {Element} element - The element to create an SVG element from.
|
|
19
20
|
* @returns {Element} - The created SVG element.
|
|
20
|
-
*
|
|
21
|
-
* @
|
|
22
|
-
*
|
|
23
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
24
|
-
* @returns {Element} - The created SVG element.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
28
|
-
* const svgElement2 = createSVGElement(svgElement);
|
|
29
|
-
* @param svgContent
|
|
30
|
-
* @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
|
|
31
24
|
*/
|
|
32
25
|
export function createSVGElement(svgContent) {
|
|
33
26
|
const svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
@@ -41,12 +34,6 @@ export function createSVGElement(svgContent) {
|
|
|
41
34
|
*
|
|
42
35
|
* @param {Element} element - The SVG element to clone.
|
|
43
36
|
* @returns {Element} - The cloned SVG element.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
47
|
-
* const clonedElement = cloneSVGElement(svgElement);
|
|
48
|
-
* console.log(clonedElement);
|
|
49
|
-
*
|
|
50
37
|
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
51
38
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
52
39
|
*/
|
|
@@ -65,13 +52,6 @@ export function cloneSVGElement(element) {
|
|
|
65
52
|
*
|
|
66
53
|
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
67
54
|
* @returns {Element} - The merged SVG element.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
71
|
-
* const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
|
|
72
|
-
* const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
|
|
73
|
-
* console.log(mergedElement);
|
|
74
|
-
*
|
|
75
55
|
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
76
56
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
77
57
|
*/
|
|
@@ -83,25 +63,28 @@ export function mergeSVGElements(elements) {
|
|
|
83
63
|
return mergedSVG;
|
|
84
64
|
}
|
|
85
65
|
/**
|
|
86
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
66
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
87
67
|
*
|
|
88
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
68
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
89
69
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
90
70
|
*
|
|
91
|
-
* @param {Element}
|
|
71
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
92
72
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
96
|
-
* const base64String = convertSVGToBase64(svgElement);
|
|
97
|
-
* console.log(base64String);
|
|
98
|
-
*
|
|
99
73
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
100
74
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
101
75
|
*/
|
|
102
|
-
export function convertSVGToBase64(
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
export function convertSVGToBase64(svgContent) {
|
|
77
|
+
let svgString;
|
|
78
|
+
if (isValidSvgString(svgContent)) {
|
|
79
|
+
svgString = svgContent;
|
|
80
|
+
}
|
|
81
|
+
else if (isValidSvgElement(svgContent)) {
|
|
82
|
+
const serializer = new dom.window.XMLSerializer();
|
|
83
|
+
svgString = serializer.serializeToString(svgContent);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
throw new Error('The provided content is not a valid SVG string or SVG element.');
|
|
87
|
+
}
|
|
105
88
|
return `data:image/svg+xml;base64,${Buffer.from(svgString).toString('base64')}`;
|
|
106
89
|
}
|
|
107
90
|
/**
|
|
@@ -112,17 +95,16 @@ export function convertSVGToBase64(svgElement) {
|
|
|
112
95
|
*
|
|
113
96
|
* @param {string} base64String - The Base64-encoded string to convert.
|
|
114
97
|
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
118
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
119
|
-
* console.log(svgString);
|
|
120
|
-
*
|
|
121
98
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
|
|
122
99
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
123
100
|
*/
|
|
124
101
|
export function convertBase64ToSVG(base64String) {
|
|
125
|
-
|
|
126
|
-
|
|
102
|
+
try {
|
|
103
|
+
const svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
104
|
+
return svgString;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
throw new Error('Invalid Base64 string.');
|
|
108
|
+
}
|
|
127
109
|
}
|
|
128
110
|
//# sourceMappingURL=common.js.map
|
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;
|
|
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,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;AAEhC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAAkB;IACjD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;IAC3G,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAAC,OAAgB;IAC9C,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;IAClD,MAAM,UAAU,GAAG,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IAC1C,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC,eAAgB,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAmB;IAClD,MAAM,SAAS,GAAG,QAAQ,CAAC,eAAe,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;IAChF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,SAAS,CAAC,WAAW,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IACH,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAA4B;IAC7D,IAAI,SAAiB,CAAC;IACtB,IAAI,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;QACjC,SAAS,GAAG,UAAoB,CAAC;IACnC,CAAC;SAAM,IAAI,iBAAiB,CAAC,UAAU,CAAC,EAAE,CAAC;QACzC,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;QAClD,SAAS,GAAG,UAAU,CAAC,iBAAiB,CAAC,UAAqB,CAAC,CAAC;IAClE,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,6BAA6B,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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates whether the provided content is a valid SVG string.
|
|
3
|
+
*
|
|
4
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
5
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isValidSvgString(content: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Validates whether the provided content is a valid SVG element.
|
|
10
|
+
*
|
|
11
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
12
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isValidSvgElement(content: any): boolean;
|
package/es/validate.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file validate.ts
|
|
3
|
+
* @description This module provides functions to validate SVG content.
|
|
4
|
+
* @module validate
|
|
5
|
+
* @author pipi
|
|
6
|
+
*/
|
|
7
|
+
import { JSDOM } from 'jsdom';
|
|
8
|
+
/**
|
|
9
|
+
* Validates whether the provided content is a valid SVG string.
|
|
10
|
+
*
|
|
11
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
12
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
13
|
+
*/
|
|
14
|
+
export function isValidSvgString(content) {
|
|
15
|
+
// Check if the content is of type string. If not, it cannot be a valid SVG string.
|
|
16
|
+
if (typeof content !== 'string') {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
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
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Validates whether the provided content is a valid SVG element.
|
|
34
|
+
*
|
|
35
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
36
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export function isValidSvgElement(content) {
|
|
39
|
+
// Check if the content is an instance of Element and its tag name is 'svg'
|
|
40
|
+
return (content &&
|
|
41
|
+
typeof content === 'object' &&
|
|
42
|
+
content.tagName.toLowerCase() === 'svg');
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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
|
-
* @
|
|
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,35 +38,22 @@ 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
|
*/
|
|
65
44
|
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
45
|
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
46
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
68
47
|
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
48
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
70
49
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
50
|
*
|
|
72
|
-
* @param {Element}
|
|
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
|
*/
|
|
83
|
-
export declare function convertSVGToBase64(
|
|
56
|
+
export declare function convertSVGToBase64(svgContent: Element | string): string;
|
|
84
57
|
/**
|
|
85
58
|
* Converts a Base64-encoded string to an SVG string.
|
|
86
59
|
*
|
|
@@ -89,12 +62,6 @@ export declare function convertSVGToBase64(svgElement: Element): 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
|
@@ -13,6 +13,7 @@ exports.mergeSVGElements = mergeSVGElements;
|
|
|
13
13
|
exports.convertSVGToBase64 = convertSVGToBase64;
|
|
14
14
|
exports.convertBase64ToSVG = convertBase64ToSVG;
|
|
15
15
|
var jsdom_1 = require("jsdom");
|
|
16
|
+
var validate_1 = require("./validate");
|
|
16
17
|
// Create a virtual DOM environment
|
|
17
18
|
var dom = new jsdom_1.JSDOM("<!DOCTYPE html><html><body></body></html>");
|
|
18
19
|
var document = dom.window.document;
|
|
@@ -24,17 +25,9 @@ var document = dom.window.document;
|
|
|
24
25
|
*
|
|
25
26
|
* @param {Element} element - The element to create an SVG element from.
|
|
26
27
|
* @returns {Element} - The created SVG element.
|
|
27
|
-
*
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
31
|
-
* @returns {Element} - The created SVG element.
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
35
|
-
* const svgElement2 = createSVGElement(svgElement);
|
|
36
|
-
* @param svgContent
|
|
37
|
-
* @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
|
|
38
31
|
*/
|
|
39
32
|
function createSVGElement(svgContent) {
|
|
40
33
|
var svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
@@ -48,12 +41,6 @@ function createSVGElement(svgContent) {
|
|
|
48
41
|
*
|
|
49
42
|
* @param {Element} element - The SVG element to clone.
|
|
50
43
|
* @returns {Element} - The cloned SVG element.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
54
|
-
* const clonedElement = cloneSVGElement(svgElement);
|
|
55
|
-
* console.log(clonedElement);
|
|
56
|
-
*
|
|
57
44
|
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
58
45
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
59
46
|
*/
|
|
@@ -72,13 +59,6 @@ function cloneSVGElement(element) {
|
|
|
72
59
|
*
|
|
73
60
|
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
74
61
|
* @returns {Element} - The merged SVG element.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
78
|
-
* const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
|
|
79
|
-
* const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
|
|
80
|
-
* console.log(mergedElement);
|
|
81
|
-
*
|
|
82
62
|
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
83
63
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
84
64
|
*/
|
|
@@ -90,25 +70,28 @@ function mergeSVGElements(elements) {
|
|
|
90
70
|
return mergedSVG;
|
|
91
71
|
}
|
|
92
72
|
/**
|
|
93
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
73
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
94
74
|
*
|
|
95
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
75
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
96
76
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
97
77
|
*
|
|
98
|
-
* @param {Element}
|
|
78
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
99
79
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
103
|
-
* const base64String = convertSVGToBase64(svgElement);
|
|
104
|
-
* console.log(base64String);
|
|
105
|
-
*
|
|
106
80
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
107
81
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
108
82
|
*/
|
|
109
|
-
function convertSVGToBase64(
|
|
110
|
-
var
|
|
111
|
-
|
|
83
|
+
function convertSVGToBase64(svgContent) {
|
|
84
|
+
var svgString;
|
|
85
|
+
if ((0, validate_1.isValidSvgString)(svgContent)) {
|
|
86
|
+
svgString = svgContent;
|
|
87
|
+
}
|
|
88
|
+
else if ((0, validate_1.isValidSvgElement)(svgContent)) {
|
|
89
|
+
var serializer = new dom.window.XMLSerializer();
|
|
90
|
+
svgString = serializer.serializeToString(svgContent);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error('The provided content is not a valid SVG string or SVG element.');
|
|
94
|
+
}
|
|
112
95
|
return "data:image/svg+xml;base64,".concat(Buffer.from(svgString).toString('base64'));
|
|
113
96
|
}
|
|
114
97
|
/**
|
|
@@ -119,17 +102,16 @@ function convertSVGToBase64(svgElement) {
|
|
|
119
102
|
*
|
|
120
103
|
* @param {string} base64String - The Base64-encoded string to convert.
|
|
121
104
|
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
125
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
126
|
-
* console.log(svgString);
|
|
127
|
-
*
|
|
128
105
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
|
|
129
106
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
130
107
|
*/
|
|
131
108
|
function convertBase64ToSVG(base64String) {
|
|
132
|
-
|
|
133
|
-
|
|
109
|
+
try {
|
|
110
|
+
var svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
111
|
+
return svgString;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw new Error('Invalid Base64 string.');
|
|
115
|
+
}
|
|
134
116
|
}
|
|
135
117
|
//# sourceMappingURL=common.js.map
|
package/lib/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAqBH,4CAGC;AAaD,0CAMC;AAaD,4CAMC;AAaD,gDAWC;AAaD,gDAOC;AAxGD,+BAA8B;AAC9B,uCAAiE;AAEjE,mCAAmC;AACnC,IAAM,GAAG,GAAG,IAAI,aAAK,CAAC,2CAA2C,CAAC,CAAC;AAC3D,IAAA,QAAQ,GAAK,GAAG,CAAC,MAAM,SAAf,CAAgB;AAEhC;;;;;;;;;;;GAWG;AACH,SAAgB,gBAAgB,CAAC,UAAkB;IACjD,IAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;IAC3G,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,eAAe,CAAC,OAAgB;IAC9C,IAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;IAClD,IAAM,UAAU,GAAG,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACzD,IAAM,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IAC1C,IAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC,eAAgB,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAAC,QAAmB;IAClD,IAAM,SAAS,GAAG,QAAQ,CAAC,eAAe,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC;IAChF,QAAQ,CAAC,OAAO,CAAC,UAAC,OAAO;QACvB,SAAS,CAAC,WAAW,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IACH,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,kBAAkB,CAAC,UAA4B;IAC7D,IAAI,SAAiB,CAAC;IACtB,IAAI,IAAA,2BAAgB,EAAC,UAAU,CAAC,EAAE,CAAC;QACjC,SAAS,GAAG,UAAoB,CAAC;IACnC,CAAC;SAAM,IAAI,IAAA,4BAAiB,EAAC,UAAU,CAAC,EAAE,CAAC;QACzC,IAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;QAClD,SAAS,GAAG,UAAU,CAAC,iBAAiB,CAAC,UAAqB,CAAC,CAAC;IAClE,CAAC;SAAM,CAAC;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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates whether the provided content is a valid SVG string.
|
|
3
|
+
*
|
|
4
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
5
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isValidSvgString(content: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Validates whether the provided content is a valid SVG element.
|
|
10
|
+
*
|
|
11
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
12
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isValidSvgElement(content: any): boolean;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidSvgString = isValidSvgString;
|
|
4
|
+
exports.isValidSvgElement = isValidSvgElement;
|
|
5
|
+
/**
|
|
6
|
+
* @file validate.ts
|
|
7
|
+
* @description This module provides functions to validate SVG content.
|
|
8
|
+
* @module validate
|
|
9
|
+
* @author pipi
|
|
10
|
+
*/
|
|
11
|
+
var jsdom_1 = require("jsdom");
|
|
12
|
+
/**
|
|
13
|
+
* Validates whether the provided content is a valid SVG string.
|
|
14
|
+
*
|
|
15
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
16
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
17
|
+
*/
|
|
18
|
+
function isValidSvgString(content) {
|
|
19
|
+
// Check if the content is of type string. If not, it cannot be a valid SVG string.
|
|
20
|
+
if (typeof content !== 'string') {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
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
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Validates whether the provided content is a valid SVG element.
|
|
38
|
+
*
|
|
39
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
40
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
41
|
+
*/
|
|
42
|
+
function isValidSvgElement(content) {
|
|
43
|
+
// Check if the content is an instance of Element and its tag name is 'svg'
|
|
44
|
+
return (content &&
|
|
45
|
+
typeof content === 'object' &&
|
|
46
|
+
content.tagName.toLowerCase() === 'svg');
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":";;AAcA,4CAiBC;AAQD,8CAOC;AA9CD;;;;;GAKG;AACH,+BAA8B;AAE9B;;;;;GAKG;AACH,SAAgB,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,IAAM,GAAG,GAAG,IAAI,aAAK,CAAC,OAAO,CAAC,CAAC;QAC/B,yEAAyE;QACzE,IAAM,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,SAAgB,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svg-toolbox",
|
|
3
|
-
"version": "1.1.
|
|
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":
|
|
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",
|