@cyberalien/svg-utils 0.0.3 → 0.0.5
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/lib/xml/stringify.d.ts +2 -0
- package/lib/xml/stringify.js +18 -10
- package/package.json +1 -1
package/lib/xml/stringify.d.ts
CHANGED
package/lib/xml/stringify.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
useSelfClosing: true,
|
|
3
|
-
numberTemplate: ` {key}="{value}"
|
|
3
|
+
numberTemplate: ` {key}="{value}"`,
|
|
4
|
+
prettyPrint: false,
|
|
5
|
+
sortAttributes: false
|
|
4
6
|
};
|
|
5
7
|
function assertNever(v) {}
|
|
6
8
|
/**
|
|
@@ -11,10 +13,16 @@ function stringifyXMLContent(root, options) {
|
|
|
11
13
|
...defaultOptions,
|
|
12
14
|
...options
|
|
13
15
|
};
|
|
16
|
+
const { prettyPrint } = fullOptions;
|
|
14
17
|
let output = "";
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
const tab = typeof prettyPrint === "string" ? prettyPrint : prettyPrint ? " " : "";
|
|
19
|
+
const tabs = (length) => tab.repeat(length);
|
|
20
|
+
const nl = prettyPrint === false ? "" : "\n";
|
|
21
|
+
const add = (node, depth) => {
|
|
22
|
+
output += tabs(depth) + "<" + node.tag;
|
|
23
|
+
const keys = Object.keys(node.attribs);
|
|
24
|
+
if (fullOptions.sortAttributes) keys.sort();
|
|
25
|
+
for (const key of keys) {
|
|
18
26
|
const value = node.attribs[key];
|
|
19
27
|
switch (typeof value) {
|
|
20
28
|
case "string":
|
|
@@ -26,16 +34,16 @@ function stringifyXMLContent(root, options) {
|
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
if (!node.children.length) {
|
|
29
|
-
if (fullOptions.useSelfClosing) output += " />";
|
|
30
|
-
else output += "></" + node.tag + ">";
|
|
37
|
+
if (fullOptions.useSelfClosing) output += " />" + nl;
|
|
38
|
+
else output += "></" + node.tag + ">" + nl;
|
|
31
39
|
return true;
|
|
32
40
|
}
|
|
33
|
-
output += ">";
|
|
41
|
+
output += ">" + nl;
|
|
34
42
|
for (let i = 0; i < node.children.length; i++) {
|
|
35
43
|
const childNode = node.children[i];
|
|
36
44
|
switch (childNode.type) {
|
|
37
45
|
case "tag":
|
|
38
|
-
if (!add(childNode)) return false;
|
|
46
|
+
if (!add(childNode, depth + 1)) return false;
|
|
39
47
|
break;
|
|
40
48
|
case "text":
|
|
41
49
|
output += childNode.content;
|
|
@@ -43,10 +51,10 @@ function stringifyXMLContent(root, options) {
|
|
|
43
51
|
default: assertNever(childNode);
|
|
44
52
|
}
|
|
45
53
|
}
|
|
46
|
-
output += "</" + node.tag + ">";
|
|
54
|
+
output += tabs(depth) + "</" + node.tag + ">" + nl;
|
|
47
55
|
return true;
|
|
48
56
|
};
|
|
49
|
-
for (const node of root) if (!add(node)) return null;
|
|
57
|
+
for (const node of root) if (!add(node, 0)) return null;
|
|
50
58
|
return output;
|
|
51
59
|
}
|
|
52
60
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "Common functions for working with SVG used by various packages.",
|
|
5
5
|
"author": "Vjacheslav Trushkin",
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.5",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"bugs": "https://github.com/cyberalien/svg-utils/issues",
|
|
9
9
|
"homepage": "https://cyberalien.dev/",
|