svg-toolbox 1.1.6 → 1.1.8
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/es/applyRemoveNanCoordinates.js +7 -3
- package/es/applyRemoveNanCoordinates.js.map +1 -1
- package/es/common.d.ts +4 -4
- package/es/common.js +28 -12
- package/es/common.js.map +1 -1
- package/es/validate.d.ts +20 -0
- package/es/validate.js +38 -0
- package/es/validate.js.map +1 -0
- package/lib/applyRemoveNanCoordinates.js +7 -3
- package/lib/applyRemoveNanCoordinates.js.map +1 -1
- package/lib/common.d.ts +4 -4
- package/lib/common.js +28 -12
- package/lib/common.js.map +1 -1
- package/lib/validate.d.ts +20 -0
- package/lib/validate.js +42 -0
- package/lib/validate.js.map +1 -0
- package/package.json +1 -1
- package/es/utils.d.ts +0 -103
- package/es/utils.js +0 -130
- package/es/utils.js.map +0 -1
- package/lib/utils.d.ts +0 -103
- package/lib/utils.js +0 -137
- package/lib/utils.js.map +0 -1
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* @author pipi
|
|
7
7
|
*/
|
|
8
8
|
import { JSDOM } from 'jsdom';
|
|
9
|
+
// Define the types of path commands that do not have parameters
|
|
10
|
+
const ignoreTypes = ['z', 'Z'];
|
|
9
11
|
/**
|
|
10
12
|
* Parses and normalizes the 'd' attribute of all path elements in an SVG content.
|
|
11
13
|
* @param {*} svgContent
|
|
@@ -41,11 +43,13 @@ export default function (svgContent) {
|
|
|
41
43
|
// Split each command into type and parameters
|
|
42
44
|
const type = command[0];
|
|
43
45
|
// If the command is not 'z' or 'Z', then it has parameters.
|
|
44
|
-
|
|
46
|
+
const ignoreType = ignoreTypes.includes(type);
|
|
47
|
+
if (!ignoreType) {
|
|
45
48
|
// Split parameters by spaces or commas, filter out empty values, and convert to numbers
|
|
46
49
|
const params = command.slice(1).split(/[\s,]+/);
|
|
47
50
|
// Filter out non-numeric values
|
|
48
|
-
|
|
51
|
+
const pickCommand = params.every(Number);
|
|
52
|
+
if (pickCommand) {
|
|
49
53
|
return { type, params };
|
|
50
54
|
}
|
|
51
55
|
}
|
|
@@ -57,7 +61,7 @@ export default function (svgContent) {
|
|
|
57
61
|
.filter((item) => item !== void 0)
|
|
58
62
|
// Filter out commands with no parameters, or commands with only 'z' or 'Z'. 'Zz' means close the path.
|
|
59
63
|
// -> https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/d#closepath
|
|
60
|
-
.filter((command) => (
|
|
64
|
+
.filter((command) => (ignoreTypes.includes(command.type) || command.params.length > 0));
|
|
61
65
|
// Reconstruct the 'd' attribute
|
|
62
66
|
const modifiedD = commands.map((command) => {
|
|
63
67
|
return command.type + command.params.join(' ');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyRemoveNanCoordinates.js","sourceRoot":"","sources":["../src/applyRemoveNanCoordinates.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,UAAkB;IACzC,oCAAoC;IACpC,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,UAAU,EAAE;QAChC,WAAW,EAAE,eAAe,CAAC,0BAA0B;KACxD,CAAC,CAAC;IACH,mCAAmC;IACnC,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC;IACrC,sBAAsB;IACtB,MAAM,UAAU,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAEjD,oCAAoC;IACpC,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACnE,CAAC;IAED,MAAM,KAAK,GAAG,UAAU,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAClD,iCAAiC;IACjC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE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,IAAI,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1F,gCAAgC;QAChC,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;YACzC,OAAO,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACZ,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,iCAAiC;IACtE,CAAC,CAAC,CAAC;IAEH,0BAA0B;IAC1B,MAAM,aAAa,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IAClD,OAAO,aAAa,CAAC;AACvB,CAAC"}
|
package/es/common.d.ts
CHANGED
|
@@ -64,12 +64,12 @@ export declare function cloneSVGElement(element: Element): Element;
|
|
|
64
64
|
*/
|
|
65
65
|
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
66
|
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
67
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
68
68
|
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
69
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
70
70
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
71
|
*
|
|
72
|
-
* @param {Element}
|
|
72
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
73
73
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
@@ -80,7 +80,7 @@ export declare function mergeSVGElements(elements: Element[]): Element;
|
|
|
80
80
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
81
81
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
82
82
|
*/
|
|
83
|
-
export declare function convertSVGToBase64(
|
|
83
|
+
export declare function convertSVGToBase64(svgContent: Element | string): string;
|
|
84
84
|
/**
|
|
85
85
|
* Converts a Base64-encoded string to an SVG string.
|
|
86
86
|
*
|
package/es/common.js
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
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
|
-
const
|
|
12
|
+
const fakeWindow = dom.window;
|
|
13
|
+
const { document } = fakeWindow;
|
|
12
14
|
/**
|
|
13
15
|
* Creates an SVG element from a given element.
|
|
14
16
|
*
|
|
@@ -30,7 +32,7 @@ const { document } = dom.window;
|
|
|
30
32
|
* @returns
|
|
31
33
|
*/
|
|
32
34
|
export function createSVGElement(svgContent) {
|
|
33
|
-
const svgElement = new
|
|
35
|
+
const svgElement = new fakeWindow.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
34
36
|
return svgElement;
|
|
35
37
|
}
|
|
36
38
|
/**
|
|
@@ -51,9 +53,9 @@ export function createSVGElement(svgContent) {
|
|
|
51
53
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
52
54
|
*/
|
|
53
55
|
export function cloneSVGElement(element) {
|
|
54
|
-
const serializer = new
|
|
56
|
+
const serializer = new fakeWindow.XMLSerializer();
|
|
55
57
|
const sourceCode = serializer.serializeToString(element);
|
|
56
|
-
const parser = new
|
|
58
|
+
const parser = new fakeWindow.DOMParser();
|
|
57
59
|
const doc = parser.parseFromString(sourceCode, 'image/svg+xml');
|
|
58
60
|
return doc.documentElement;
|
|
59
61
|
}
|
|
@@ -83,12 +85,12 @@ export function mergeSVGElements(elements) {
|
|
|
83
85
|
return mergedSVG;
|
|
84
86
|
}
|
|
85
87
|
/**
|
|
86
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
88
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
87
89
|
*
|
|
88
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
90
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
89
91
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
90
92
|
*
|
|
91
|
-
* @param {Element}
|
|
93
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
92
94
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
93
95
|
*
|
|
94
96
|
* @example
|
|
@@ -99,9 +101,18 @@ export function mergeSVGElements(elements) {
|
|
|
99
101
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
100
102
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
101
103
|
*/
|
|
102
|
-
export function convertSVGToBase64(
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
export function convertSVGToBase64(svgContent) {
|
|
105
|
+
let svgString;
|
|
106
|
+
if (isValidSvgString(svgContent)) {
|
|
107
|
+
svgString = svgContent;
|
|
108
|
+
}
|
|
109
|
+
else if (isValidSvgElement(svgContent)) {
|
|
110
|
+
const serializer = new fakeWindow.XMLSerializer();
|
|
111
|
+
svgString = serializer.serializeToString(svgContent);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
throw new Error('The provided content is not a valid SVG string or SVG element.');
|
|
115
|
+
}
|
|
105
116
|
return `data:image/svg+xml;base64,${Buffer.from(svgString).toString('base64')}`;
|
|
106
117
|
}
|
|
107
118
|
/**
|
|
@@ -122,7 +133,12 @@ export function convertSVGToBase64(svgElement) {
|
|
|
122
133
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
123
134
|
*/
|
|
124
135
|
export function convertBase64ToSVG(base64String) {
|
|
125
|
-
|
|
126
|
-
|
|
136
|
+
try {
|
|
137
|
+
const svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
138
|
+
return svgString;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
throw new Error('Invalid Base64 string.');
|
|
142
|
+
}
|
|
127
143
|
}
|
|
128
144
|
//# 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,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC;AAC9B,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAAkB;IACjD,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;IAC3G,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,eAAe,CAAC,OAAgB;IAC9C,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,aAAa,EAAE,CAAC;IAClD,MAAM,UAAU,GAAG,UAAU,CAAC,iBAAi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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,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file validate.ts
|
|
3
|
+
* @description This module provides functions to validate SVG content.
|
|
4
|
+
* @module validate
|
|
5
|
+
* @author pipi
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Validates whether the provided content is a valid SVG string.
|
|
9
|
+
*
|
|
10
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
11
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isValidSvgString(content: any): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Validates whether the provided content is a valid SVG element.
|
|
16
|
+
*
|
|
17
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
18
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isValidSvgElement(content: any): boolean;
|
package/es/validate.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file validate.ts
|
|
3
|
+
* @description This module provides functions to validate SVG content.
|
|
4
|
+
* @module validate
|
|
5
|
+
* @author pipi
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Validates whether the provided content is a valid SVG string.
|
|
9
|
+
*
|
|
10
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
11
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export function isValidSvgString(content) {
|
|
14
|
+
// Check if the content is of type string. If not, it cannot be a valid SVG string.
|
|
15
|
+
if (typeof content !== 'string') {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
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'));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Validates whether the provided content is a valid SVG element.
|
|
26
|
+
*
|
|
27
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
28
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
29
|
+
*/
|
|
30
|
+
export function isValidSvgElement(content) {
|
|
31
|
+
// Check if the content is an instance of Element and its tag name is 'svg'
|
|
32
|
+
return (content &&
|
|
33
|
+
typeof content === 'object' &&
|
|
34
|
+
content.tagName.toLowerCase() === 'svg' &&
|
|
35
|
+
content.namespaceURI === 'http://www.w3.org/2000/svg' // SVG namespace URI
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +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"}
|
|
@@ -9,6 +9,8 @@ exports.default = default_1;
|
|
|
9
9
|
* @author pipi
|
|
10
10
|
*/
|
|
11
11
|
var jsdom_1 = require("jsdom");
|
|
12
|
+
// Define the types of path commands that do not have parameters
|
|
13
|
+
var ignoreTypes = ['z', 'Z'];
|
|
12
14
|
/**
|
|
13
15
|
* Parses and normalizes the 'd' attribute of all path elements in an SVG content.
|
|
14
16
|
* @param {*} svgContent
|
|
@@ -44,11 +46,13 @@ function default_1(svgContent) {
|
|
|
44
46
|
// Split each command into type and parameters
|
|
45
47
|
var type = command[0];
|
|
46
48
|
// If the command is not 'z' or 'Z', then it has parameters.
|
|
47
|
-
|
|
49
|
+
var ignoreType = ignoreTypes.includes(type);
|
|
50
|
+
if (!ignoreType) {
|
|
48
51
|
// Split parameters by spaces or commas, filter out empty values, and convert to numbers
|
|
49
52
|
var params = command.slice(1).split(/[\s,]+/);
|
|
50
53
|
// Filter out non-numeric values
|
|
51
|
-
|
|
54
|
+
var pickCommand = params.every(Number);
|
|
55
|
+
if (pickCommand) {
|
|
52
56
|
return { type: type, params: params };
|
|
53
57
|
}
|
|
54
58
|
}
|
|
@@ -60,7 +64,7 @@ function default_1(svgContent) {
|
|
|
60
64
|
.filter(function (item) { return item !== void 0; })
|
|
61
65
|
// Filter out commands with no parameters, or commands with only 'z' or 'Z'. 'Zz' means close the path.
|
|
62
66
|
// -> https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/d#closepath
|
|
63
|
-
.filter(function (command) { return (
|
|
67
|
+
.filter(function (command) { return (ignoreTypes.includes(command.type) || command.params.length > 0); });
|
|
64
68
|
// Reconstruct the 'd' attribute
|
|
65
69
|
var modifiedD = commands.map(function (command) {
|
|
66
70
|
return command.type + command.params.join(' ');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyRemoveNanCoordinates.js","sourceRoot":"","sources":["../src/applyRemoveNanCoordinates.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"applyRemoveNanCoordinates.js","sourceRoot":"","sources":["../src/applyRemoveNanCoordinates.ts"],"names":[],"mappings":";;AAiBA,4BA4DC;AA7ED;;;;;;GAMG;AACH,+BAA8B;AAE9B,gEAAgE;AAChE,IAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAE/B;;;;GAIG;AACH,mBAAyB,UAAkB;IACzC,oCAAoC;IACpC,IAAM,GAAG,GAAG,IAAI,aAAK,CAAC,UAAU,EAAE;QAChC,WAAW,EAAE,eAAe,CAAC,0BAA0B;KACxD,CAAC,CAAC;IACH,mCAAmC;IACnC,IAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC;IACrC,sBAAsB;IACtB,IAAM,UAAU,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAEjD,oCAAoC;IACpC,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACnE,CAAC;IAED,IAAM,KAAK,GAAG,UAAU,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAClD,iCAAiC;IACjC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,UAAC,IAAI;QAC7B,IAAM,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,wBAAwB;QAC1D,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YACf,OAAO;QACT,CAA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G,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACZ,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,iCAAiC;IACtE,CAAC,CAAC,CAAC;IAEH,0BAA0B;IAC1B,IAAM,aAAa,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IAClD,OAAO,aAAa,CAAC;AACvB,CAAC"}
|
package/lib/common.d.ts
CHANGED
|
@@ -64,12 +64,12 @@ export declare function cloneSVGElement(element: Element): Element;
|
|
|
64
64
|
*/
|
|
65
65
|
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
66
|
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
67
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
68
68
|
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
69
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
70
70
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
71
|
*
|
|
72
|
-
* @param {Element}
|
|
72
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
73
73
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
@@ -80,7 +80,7 @@ export declare function mergeSVGElements(elements: Element[]): Element;
|
|
|
80
80
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
81
81
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
82
82
|
*/
|
|
83
|
-
export declare function convertSVGToBase64(
|
|
83
|
+
export declare function convertSVGToBase64(svgContent: Element | string): string;
|
|
84
84
|
/**
|
|
85
85
|
* Converts a Base64-encoded string to an SVG string.
|
|
86
86
|
*
|
package/lib/common.js
CHANGED
|
@@ -13,9 +13,11 @@ 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
|
-
var
|
|
19
|
+
var fakeWindow = dom.window;
|
|
20
|
+
var document = fakeWindow.document;
|
|
19
21
|
/**
|
|
20
22
|
* Creates an SVG element from a given element.
|
|
21
23
|
*
|
|
@@ -37,7 +39,7 @@ var document = dom.window.document;
|
|
|
37
39
|
* @returns
|
|
38
40
|
*/
|
|
39
41
|
function createSVGElement(svgContent) {
|
|
40
|
-
var svgElement = new
|
|
42
|
+
var svgElement = new fakeWindow.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
41
43
|
return svgElement;
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
@@ -58,9 +60,9 @@ function createSVGElement(svgContent) {
|
|
|
58
60
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
59
61
|
*/
|
|
60
62
|
function cloneSVGElement(element) {
|
|
61
|
-
var serializer = new
|
|
63
|
+
var serializer = new fakeWindow.XMLSerializer();
|
|
62
64
|
var sourceCode = serializer.serializeToString(element);
|
|
63
|
-
var parser = new
|
|
65
|
+
var parser = new fakeWindow.DOMParser();
|
|
64
66
|
var doc = parser.parseFromString(sourceCode, 'image/svg+xml');
|
|
65
67
|
return doc.documentElement;
|
|
66
68
|
}
|
|
@@ -90,12 +92,12 @@ function mergeSVGElements(elements) {
|
|
|
90
92
|
return mergedSVG;
|
|
91
93
|
}
|
|
92
94
|
/**
|
|
93
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
95
|
+
* Converts an SVG element or SVG string to a Base64-encoded string.
|
|
94
96
|
*
|
|
95
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
97
|
+
* This function serializes the SVG element or SVG string to a string and then encodes it to Base64.
|
|
96
98
|
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
97
99
|
*
|
|
98
|
-
* @param {Element}
|
|
100
|
+
* @param {Element | string} svgContent - The SVG element or SVG string to convert.
|
|
99
101
|
* @returns {string} - The Base64-encoded string representation of the SVG element.
|
|
100
102
|
*
|
|
101
103
|
* @example
|
|
@@ -106,9 +108,18 @@ function mergeSVGElements(elements) {
|
|
|
106
108
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
107
109
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
108
110
|
*/
|
|
109
|
-
function convertSVGToBase64(
|
|
110
|
-
var
|
|
111
|
-
|
|
111
|
+
function convertSVGToBase64(svgContent) {
|
|
112
|
+
var svgString;
|
|
113
|
+
if ((0, validate_1.isValidSvgString)(svgContent)) {
|
|
114
|
+
svgString = svgContent;
|
|
115
|
+
}
|
|
116
|
+
else if ((0, validate_1.isValidSvgElement)(svgContent)) {
|
|
117
|
+
var serializer = new fakeWindow.XMLSerializer();
|
|
118
|
+
svgString = serializer.serializeToString(svgContent);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new Error('The provided content is not a valid SVG string or SVG element.');
|
|
122
|
+
}
|
|
112
123
|
return "data:image/svg+xml;base64,".concat(Buffer.from(svgString).toString('base64'));
|
|
113
124
|
}
|
|
114
125
|
/**
|
|
@@ -129,7 +140,12 @@ function convertSVGToBase64(svgElement) {
|
|
|
129
140
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
130
141
|
*/
|
|
131
142
|
function convertBase64ToSVG(base64String) {
|
|
132
|
-
|
|
133
|
-
|
|
143
|
+
try {
|
|
144
|
+
var svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
145
|
+
return svgString;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
throw new Error('Invalid Base64 string.');
|
|
149
|
+
}
|
|
134
150
|
}
|
|
135
151
|
//# 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;;AA8BH,4CAGC;AAmBD,0CAMC;AAoBD,4CAMC;AAmBD,gDAWC;AAmBD,gDAOC;AA1ID,+BAA8B;AAC9B,uCAAiE;AAEjE,mCAAmC;AACnC,IAAM,GAAG,GAAG,IAAI,aAAK,CAAC,2CAA2C,CAAC,CAAC;AACnE,IAAM,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC;AACtB,IAAA,QAAQ,GAAK,UAAU,SAAf,CAAgB;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,gBAAgB,CAAC,UAAkB;IACjD,IAAM,UAAU,GAAG,IAAI,UAAU,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;IAC3G,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,eAAe,CAAC,OAAgB;IAC9C,IAAM,UAAU,GAAG,IAAI,UAAU,CAAC,aAAa,EAAE,CAAC;IAClD,IAAM,UAAU,GAAG,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACzD,IAAM,MAAM,GAAG,IAAI,UAAU,CAAC,SAAS,EAAE,CAAC;IAC1C,IAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC;IAChE,OAAO,GAAG,CAA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I,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file validate.ts
|
|
3
|
+
* @description This module provides functions to validate SVG content.
|
|
4
|
+
* @module validate
|
|
5
|
+
* @author pipi
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Validates whether the provided content is a valid SVG string.
|
|
9
|
+
*
|
|
10
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
11
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isValidSvgString(content: any): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Validates whether the provided content is a valid SVG element.
|
|
16
|
+
*
|
|
17
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
18
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isValidSvgElement(content: any): boolean;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file validate.ts
|
|
4
|
+
* @description This module provides functions to validate SVG content.
|
|
5
|
+
* @module validate
|
|
6
|
+
* @author pipi
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.isValidSvgString = isValidSvgString;
|
|
10
|
+
exports.isValidSvgElement = isValidSvgElement;
|
|
11
|
+
/**
|
|
12
|
+
* Validates whether the provided content is a valid SVG string.
|
|
13
|
+
*
|
|
14
|
+
* @param content - The content to be validated (could be a string or another type).
|
|
15
|
+
* @returns True if the content is a valid SVG string, false otherwise.
|
|
16
|
+
*/
|
|
17
|
+
function isValidSvgString(content) {
|
|
18
|
+
// Check if the content is of type string. If not, it cannot be a valid SVG string.
|
|
19
|
+
if (typeof content !== 'string') {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
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'));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Validates whether the provided content is a valid SVG element.
|
|
30
|
+
*
|
|
31
|
+
* @param content - The content to be validated (could be an Element or another type).
|
|
32
|
+
* @returns True if the content is a valid SVG element, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
function isValidSvgElement(content) {
|
|
35
|
+
// Check if the content is an instance of Element and its tag name is 'svg'
|
|
36
|
+
return (content &&
|
|
37
|
+
typeof content === 'object' &&
|
|
38
|
+
content.tagName.toLowerCase() === 'svg' &&
|
|
39
|
+
content.namespaceURI === 'http://www.w3.org/2000/svg' // SVG namespace URI
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +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,MAAM,CAAC,CAAC,CAAC;AACpE,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;QACvC,OAAO,CAAC,YAAY,KAAK,4BAA4B,CAAC,oBAAoB;KAC3E,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/es/utils.d.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file utils.ts
|
|
3
|
-
* @description This module provides utility functions for working with SVG elements.
|
|
4
|
-
* @module utils
|
|
5
|
-
* @requires jsdom - A JavaScript implementation of the WHATWG DOM and HTML standards.
|
|
6
|
-
* @author pipi
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Creates an SVG element from a given element.
|
|
10
|
-
*
|
|
11
|
-
* This function serializes the given element to a string, then parses it back to create an SVG element.
|
|
12
|
-
* This approach ensures that the element is correctly parsed as an SVG element.
|
|
13
|
-
*
|
|
14
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
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
|
|
27
|
-
*/
|
|
28
|
-
export declare function createSVGElement(svgContent: string): Element;
|
|
29
|
-
/**
|
|
30
|
-
* Clones an SVG element deeply.
|
|
31
|
-
*
|
|
32
|
-
* This function serializes the given SVG element to a string, then parses it back to create a deep clone.
|
|
33
|
-
* This approach ensures that all attributes and child nodes are duplicated.
|
|
34
|
-
*
|
|
35
|
-
* @param {Element} element - The SVG element to clone.
|
|
36
|
-
* @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
|
-
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
44
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
45
|
-
*/
|
|
46
|
-
export declare function cloneSVGElement(element: Element): Element;
|
|
47
|
-
/**
|
|
48
|
-
* Merges multiple SVG elements into a single SVG element.
|
|
49
|
-
*
|
|
50
|
-
* This function creates a new SVG element and appends clones of the provided elements to it.
|
|
51
|
-
* The SVG namespace is specified as 'http://www.w3.org/2000/svg'.
|
|
52
|
-
*
|
|
53
|
-
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
54
|
-
* @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
|
-
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
63
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
64
|
-
*/
|
|
65
|
-
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
|
-
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
68
|
-
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
70
|
-
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
|
-
*
|
|
72
|
-
* @param {Element} svgElement - The SVG element to convert.
|
|
73
|
-
* @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
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
81
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
82
|
-
*/
|
|
83
|
-
export declare function convertSVGToBase64(svgElement: Element): string;
|
|
84
|
-
/**
|
|
85
|
-
* Converts a Base64-encoded string to an SVG string.
|
|
86
|
-
*
|
|
87
|
-
* This function decodes the Base64-encoded string and then converts it to an SVG string.
|
|
88
|
-
* The resulting string can be used to create an SVG element using the `createSVGElement` function.
|
|
89
|
-
*
|
|
90
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
91
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
*
|
|
95
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
96
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
100
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
101
|
-
* console.log(svgString);
|
|
102
|
-
*/
|
|
103
|
-
export declare function convertBase64ToSVG(base64String: string): string;
|
package/es/utils.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file utils.ts
|
|
3
|
-
* @description This module provides utility functions for working with SVG elements.
|
|
4
|
-
* @module utils
|
|
5
|
-
* @requires jsdom - A JavaScript implementation of the WHATWG DOM and HTML standards.
|
|
6
|
-
* @author pipi
|
|
7
|
-
*/
|
|
8
|
-
import { JSDOM } from 'jsdom';
|
|
9
|
-
// Create a virtual DOM environment
|
|
10
|
-
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
|
|
11
|
-
const { document } = dom.window;
|
|
12
|
-
/**
|
|
13
|
-
* Creates an SVG element from a given element.
|
|
14
|
-
*
|
|
15
|
-
* This function serializes the given element to a string, then parses it back to create an SVG element.
|
|
16
|
-
* This approach ensures that the element is correctly parsed as an SVG element.
|
|
17
|
-
*
|
|
18
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
19
|
-
* @returns {Element} - The created SVG element.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
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
|
|
31
|
-
*/
|
|
32
|
-
export function createSVGElement(svgContent) {
|
|
33
|
-
const svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
34
|
-
return svgElement;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Clones an SVG element deeply.
|
|
38
|
-
*
|
|
39
|
-
* This function serializes the given SVG element to a string, then parses it back to create a deep clone.
|
|
40
|
-
* This approach ensures that all attributes and child nodes are duplicated.
|
|
41
|
-
*
|
|
42
|
-
* @param {Element} element - The SVG element to clone.
|
|
43
|
-
* @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
|
-
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
51
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
52
|
-
*/
|
|
53
|
-
export function cloneSVGElement(element) {
|
|
54
|
-
const serializer = new dom.window.XMLSerializer();
|
|
55
|
-
const sourceCode = serializer.serializeToString(element);
|
|
56
|
-
const parser = new dom.window.DOMParser();
|
|
57
|
-
const doc = parser.parseFromString(sourceCode, 'image/svg+xml');
|
|
58
|
-
return doc.documentElement;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Merges multiple SVG elements into a single SVG element.
|
|
62
|
-
*
|
|
63
|
-
* This function creates a new SVG element and appends clones of the provided elements to it.
|
|
64
|
-
* The SVG namespace is specified as 'http://www.w3.org/2000/svg'.
|
|
65
|
-
*
|
|
66
|
-
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
67
|
-
* @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
|
-
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
76
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
77
|
-
*/
|
|
78
|
-
export function mergeSVGElements(elements) {
|
|
79
|
-
const mergedSVG = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
80
|
-
elements.forEach((element) => {
|
|
81
|
-
mergedSVG.appendChild(cloneSVGElement(element));
|
|
82
|
-
});
|
|
83
|
-
return mergedSVG;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
87
|
-
*
|
|
88
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
89
|
-
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
90
|
-
*
|
|
91
|
-
* @param {Element} svgElement - The SVG element to convert.
|
|
92
|
-
* @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
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
100
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
101
|
-
*/
|
|
102
|
-
export function convertSVGToBase64(svgElement) {
|
|
103
|
-
const serializer = new dom.window.XMLSerializer();
|
|
104
|
-
const svgString = serializer.serializeToString(svgElement);
|
|
105
|
-
return `data:image/svg+xml;base64,${Buffer.from(svgString).toString('base64')}`;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Converts a Base64-encoded string to an SVG string.
|
|
109
|
-
*
|
|
110
|
-
* This function decodes the Base64-encoded string and then converts it to an SVG string.
|
|
111
|
-
* The resulting string can be used to create an SVG element using the `createSVGElement` function.
|
|
112
|
-
*
|
|
113
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
114
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
*
|
|
118
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
119
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
123
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
124
|
-
* console.log(svgString);
|
|
125
|
-
*/
|
|
126
|
-
export function convertBase64ToSVG(base64String) {
|
|
127
|
-
const svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
128
|
-
return svgString;
|
|
129
|
-
}
|
|
130
|
-
//# sourceMappingURL=utils.js.map
|
package/es/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,mCAAmC;AACnC,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACnE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;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;;;;;;;;;;;;;;;;GAgBG;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;;;;;;;;;;;;;;;;;GAiBG;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;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAAmB;IACpD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;IAClD,MAAM,SAAS,GAAG,UAAU,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC;IAC3D,OAAO,6BAA6B,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;AAClF,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,kBAAkB,CAAC,YAAoB;IACrD,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;IACtF,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/lib/utils.d.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file utils.ts
|
|
3
|
-
* @description This module provides utility functions for working with SVG elements.
|
|
4
|
-
* @module utils
|
|
5
|
-
* @requires jsdom - A JavaScript implementation of the WHATWG DOM and HTML standards.
|
|
6
|
-
* @author pipi
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Creates an SVG element from a given element.
|
|
10
|
-
*
|
|
11
|
-
* This function serializes the given element to a string, then parses it back to create an SVG element.
|
|
12
|
-
* This approach ensures that the element is correctly parsed as an SVG element.
|
|
13
|
-
*
|
|
14
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
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
|
|
27
|
-
*/
|
|
28
|
-
export declare function createSVGElement(svgContent: string): Element;
|
|
29
|
-
/**
|
|
30
|
-
* Clones an SVG element deeply.
|
|
31
|
-
*
|
|
32
|
-
* This function serializes the given SVG element to a string, then parses it back to create a deep clone.
|
|
33
|
-
* This approach ensures that all attributes and child nodes are duplicated.
|
|
34
|
-
*
|
|
35
|
-
* @param {Element} element - The SVG element to clone.
|
|
36
|
-
* @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
|
-
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
44
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
45
|
-
*/
|
|
46
|
-
export declare function cloneSVGElement(element: Element): Element;
|
|
47
|
-
/**
|
|
48
|
-
* Merges multiple SVG elements into a single SVG element.
|
|
49
|
-
*
|
|
50
|
-
* This function creates a new SVG element and appends clones of the provided elements to it.
|
|
51
|
-
* The SVG namespace is specified as 'http://www.w3.org/2000/svg'.
|
|
52
|
-
*
|
|
53
|
-
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
54
|
-
* @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
|
-
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
63
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
64
|
-
*/
|
|
65
|
-
export declare function mergeSVGElements(elements: Element[]): Element;
|
|
66
|
-
/**
|
|
67
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
68
|
-
*
|
|
69
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
70
|
-
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
71
|
-
*
|
|
72
|
-
* @param {Element} svgElement - The SVG element to convert.
|
|
73
|
-
* @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
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
81
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
82
|
-
*/
|
|
83
|
-
export declare function convertSVGToBase64(svgElement: Element): string;
|
|
84
|
-
/**
|
|
85
|
-
* Converts a Base64-encoded string to an SVG string.
|
|
86
|
-
*
|
|
87
|
-
* This function decodes the Base64-encoded string and then converts it to an SVG string.
|
|
88
|
-
* The resulting string can be used to create an SVG element using the `createSVGElement` function.
|
|
89
|
-
*
|
|
90
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
91
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
*
|
|
95
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
96
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
100
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
101
|
-
* console.log(svgString);
|
|
102
|
-
*/
|
|
103
|
-
export declare function convertBase64ToSVG(base64String: string): string;
|
package/lib/utils.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @file utils.ts
|
|
4
|
-
* @description This module provides utility functions for working with SVG elements.
|
|
5
|
-
* @module utils
|
|
6
|
-
* @requires jsdom - A JavaScript implementation of the WHATWG DOM and HTML standards.
|
|
7
|
-
* @author pipi
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.createSVGElement = createSVGElement;
|
|
11
|
-
exports.cloneSVGElement = cloneSVGElement;
|
|
12
|
-
exports.mergeSVGElements = mergeSVGElements;
|
|
13
|
-
exports.convertSVGToBase64 = convertSVGToBase64;
|
|
14
|
-
exports.convertBase64ToSVG = convertBase64ToSVG;
|
|
15
|
-
var jsdom_1 = require("jsdom");
|
|
16
|
-
// Create a virtual DOM environment
|
|
17
|
-
var dom = new jsdom_1.JSDOM("<!DOCTYPE html><html><body></body></html>");
|
|
18
|
-
var document = dom.window.document;
|
|
19
|
-
/**
|
|
20
|
-
* Creates an SVG element from a given element.
|
|
21
|
-
*
|
|
22
|
-
* This function serializes the given element to a string, then parses it back to create an SVG element.
|
|
23
|
-
* This approach ensures that the element is correctly parsed as an SVG element.
|
|
24
|
-
*
|
|
25
|
-
* @param {Element} element - The element to create an SVG element from.
|
|
26
|
-
* @returns {Element} - The created SVG element.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
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
|
|
38
|
-
*/
|
|
39
|
-
function createSVGElement(svgContent) {
|
|
40
|
-
var svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
|
|
41
|
-
return svgElement;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Clones an SVG element deeply.
|
|
45
|
-
*
|
|
46
|
-
* This function serializes the given SVG element to a string, then parses it back to create a deep clone.
|
|
47
|
-
* This approach ensures that all attributes and child nodes are duplicated.
|
|
48
|
-
*
|
|
49
|
-
* @param {Element} element - The SVG element to clone.
|
|
50
|
-
* @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
|
-
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
|
|
58
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
59
|
-
*/
|
|
60
|
-
function cloneSVGElement(element) {
|
|
61
|
-
var serializer = new dom.window.XMLSerializer();
|
|
62
|
-
var sourceCode = serializer.serializeToString(element);
|
|
63
|
-
var parser = new dom.window.DOMParser();
|
|
64
|
-
var doc = parser.parseFromString(sourceCode, 'image/svg+xml');
|
|
65
|
-
return doc.documentElement;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Merges multiple SVG elements into a single SVG element.
|
|
69
|
-
*
|
|
70
|
-
* This function creates a new SVG element and appends clones of the provided elements to it.
|
|
71
|
-
* The SVG namespace is specified as 'http://www.w3.org/2000/svg'.
|
|
72
|
-
*
|
|
73
|
-
* @param {Element[]} elements - An array of SVG elements to merge.
|
|
74
|
-
* @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
|
-
* @see https://www.w3.org/TR/SVG/ - SVG specification
|
|
83
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
|
|
84
|
-
*/
|
|
85
|
-
function mergeSVGElements(elements) {
|
|
86
|
-
var mergedSVG = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
87
|
-
elements.forEach(function (element) {
|
|
88
|
-
mergedSVG.appendChild(cloneSVGElement(element));
|
|
89
|
-
});
|
|
90
|
-
return mergedSVG;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Converts an SVG element to a Base64-encoded string.
|
|
94
|
-
*
|
|
95
|
-
* This function serializes the SVG element to a string and then encodes it to Base64.
|
|
96
|
-
* The resulting string can be used as a data URI for embedding SVG content in HTML or CSS.
|
|
97
|
-
*
|
|
98
|
-
* @param {Element} svgElement - The SVG element to convert.
|
|
99
|
-
* @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
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
|
|
107
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
|
|
108
|
-
*/
|
|
109
|
-
function convertSVGToBase64(svgElement) {
|
|
110
|
-
var serializer = new dom.window.XMLSerializer();
|
|
111
|
-
var svgString = serializer.serializeToString(svgElement);
|
|
112
|
-
return "data:image/svg+xml;base64,".concat(Buffer.from(svgString).toString('base64'));
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Converts a Base64-encoded string to an SVG string.
|
|
116
|
-
*
|
|
117
|
-
* This function decodes the Base64-encoded string and then converts it to an SVG string.
|
|
118
|
-
* The resulting string can be used to create an SVG element using the `createSVGElement` function.
|
|
119
|
-
*
|
|
120
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
121
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
*
|
|
125
|
-
* @param {string} base64String - The Base64-encoded string to convert.
|
|
126
|
-
* @returns {string} - The SVG string representation of the Base64-encoded string.
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
|
|
130
|
-
* const svgString = convertBase64ToSVG(base64String);
|
|
131
|
-
* console.log(svgString);
|
|
132
|
-
*/
|
|
133
|
-
function convertBase64ToSVG(base64String) {
|
|
134
|
-
var svgString = Buffer.from(base64String.split(',')[1], 'base64').toString('utf-8');
|
|
135
|
-
return svgString;
|
|
136
|
-
}
|
|
137
|
-
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AA4BH,4CAGC;AAmBD,0CAMC;AAoBD,4CAMC;AAmBD,gDAIC;AAqBD,gDAGC;AA/HD,+BAA8B;AAE9B,mCAAmC;AACnC,IAAM,GAAG,GAAG,IAAI,aAAK,CAAC,2CAA2C,CAAC,CAAC;AAC3D,IAAA,QAAQ,GAAK,GAAG,CAAC,MAAM,SAAf,CAAgB;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;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;;;;;;;;;;;;;;;;GAgBG;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;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,gBAAgB,CAAC,QAAmB;IAClD,IAAM,SAAS,GAAG,QAAQ,CAAC,e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