@ooxml-tools/xml 0.5.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var xmlJs = require('xml-js');
|
|
4
|
+
|
|
5
|
+
function asXmlElement(xml) {
|
|
6
|
+
if (typeof xml === "string") {
|
|
7
|
+
return xmlJs.xml2js(xml, {
|
|
8
|
+
compact: false,
|
|
9
|
+
captureSpacesBetweenElements: false,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return xml;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param templateStrings template strings
|
|
19
|
+
* @param templateValues tamplate values
|
|
20
|
+
* @returns xml string
|
|
21
|
+
*/
|
|
22
|
+
function safeXml(templateStrings, ...templateValues) {
|
|
23
|
+
let xml = "";
|
|
24
|
+
for (let i = 0; i < templateStrings.length; i++) {
|
|
25
|
+
if (templateValues[i] !== undefined && templateValues[i] !== null) {
|
|
26
|
+
const value = templateValues[i];
|
|
27
|
+
const xmlPart = Array.isArray(value) ? value.join("") : value;
|
|
28
|
+
xml += `${templateStrings[i]}${xmlPart}`;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
xml += `${templateStrings[i]}`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (process.env.NODE_ENV === "development") {
|
|
35
|
+
// This will "throw" if the XML is invalid
|
|
36
|
+
xmlJs.xml2js(xml, {
|
|
37
|
+
compact: false,
|
|
38
|
+
captureSpacesBetweenElements: false,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return xml;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function cdata(input) {
|
|
45
|
+
if (input === null || input === undefined) {
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
return `<![CDATA[${input}]]>`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function compact(xml) {
|
|
52
|
+
const parsed = xmlJs.xml2js(xml, {
|
|
53
|
+
compact: false,
|
|
54
|
+
trim: true,
|
|
55
|
+
});
|
|
56
|
+
return xmlJs.js2xml(parsed);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function _collapseFragments(orig) {
|
|
60
|
+
var _a;
|
|
61
|
+
let node = orig;
|
|
62
|
+
const cloneIfRequired = () => {
|
|
63
|
+
if (node === orig) {
|
|
64
|
+
return Object.assign(Object.assign({}, node), { elements: node.elements ? [...node.elements] : undefined });
|
|
65
|
+
}
|
|
66
|
+
return node;
|
|
67
|
+
};
|
|
68
|
+
if (node.elements) {
|
|
69
|
+
for (let i = node.elements.length - 1; i >= 0; i--) {
|
|
70
|
+
if (node.elements) {
|
|
71
|
+
const child = node.elements[i];
|
|
72
|
+
const out = _collapseFragments(child);
|
|
73
|
+
if (Array.isArray(out)) {
|
|
74
|
+
node = cloneIfRequired();
|
|
75
|
+
node.elements.splice(i, 1, ...out);
|
|
76
|
+
}
|
|
77
|
+
else if (out !== child) {
|
|
78
|
+
node = cloneIfRequired();
|
|
79
|
+
if (node.elements) {
|
|
80
|
+
node.elements[i] = out;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (node.name === "XML_FRAGMENT") {
|
|
87
|
+
return (_a = node.elements) !== null && _a !== void 0 ? _a : [];
|
|
88
|
+
}
|
|
89
|
+
return node;
|
|
90
|
+
}
|
|
91
|
+
function collapseFragments(root) {
|
|
92
|
+
const out = _collapseFragments(asXmlElement(root));
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function format(input, { spaces = 2 } = {}) {
|
|
97
|
+
const elementJson = xmlJs.xml2js(input, {
|
|
98
|
+
compact: false,
|
|
99
|
+
captureSpacesBetweenElements: false,
|
|
100
|
+
});
|
|
101
|
+
return xmlJs.js2xml(elementJson, {
|
|
102
|
+
spaces: spaces,
|
|
103
|
+
compact: false,
|
|
104
|
+
indentText: true,
|
|
105
|
+
fullTagEmptyElement: true,
|
|
106
|
+
indentAttributes: true,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function createFragment(elements) {
|
|
111
|
+
return {
|
|
112
|
+
type: "element",
|
|
113
|
+
name: "XML_FRAGMENT",
|
|
114
|
+
elements: elements,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const TEST_ATTR_NAME = "_testid";
|
|
119
|
+
|
|
120
|
+
function getAllByTestIdRecursive(root, id, collection) {
|
|
121
|
+
var _a;
|
|
122
|
+
const testAttrValue = (_a = root.attributes) === null || _a === void 0 ? void 0 : _a[TEST_ATTR_NAME];
|
|
123
|
+
if (testAttrValue === id) {
|
|
124
|
+
collection.push(root);
|
|
125
|
+
}
|
|
126
|
+
if (root.elements) {
|
|
127
|
+
for (const element of root.elements) {
|
|
128
|
+
getAllByTestIdRecursive(element, id, collection);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return collection;
|
|
132
|
+
}
|
|
133
|
+
function getAllByTestId(root, id) {
|
|
134
|
+
return getAllByTestIdRecursive(root, id, []);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getByTestId(root, id) {
|
|
138
|
+
const elements = getAllByTestId(root, id);
|
|
139
|
+
if (elements.length > 1) {
|
|
140
|
+
throw new Error(`Only expected 1 element with ${TEST_ATTR_NAME}='${id}'`);
|
|
141
|
+
}
|
|
142
|
+
return elements[0];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function getSingleTextNode(element) {
|
|
146
|
+
if (element) {
|
|
147
|
+
const elements = element.elements;
|
|
148
|
+
if (elements && elements.length === 1 && elements[0].type === "text") {
|
|
149
|
+
return elements[0];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
throw new Error("Not a single text node");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function removeTestIdsRecursive(root) {
|
|
156
|
+
var _a;
|
|
157
|
+
const attributes = root.attributes ? Object.assign({}, root.attributes) : undefined;
|
|
158
|
+
if (attributes) {
|
|
159
|
+
delete attributes[TEST_ATTR_NAME];
|
|
160
|
+
}
|
|
161
|
+
return Object.assign(Object.assign({}, root), { attributes, elements: (_a = root.elements) === null || _a === void 0 ? void 0 : _a.map((element) => {
|
|
162
|
+
return removeTestIdsRecursive(element);
|
|
163
|
+
}) });
|
|
164
|
+
}
|
|
165
|
+
function removeTestIds(root) {
|
|
166
|
+
return removeTestIdsRecursive(root);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Remove all "special" content from the XML
|
|
170
|
+
function clean(element) {
|
|
171
|
+
return collapseFragments(removeTestIds(element));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
exports.asXmlElement = asXmlElement;
|
|
175
|
+
exports.cdata = cdata;
|
|
176
|
+
exports.clean = clean;
|
|
177
|
+
exports.collapseFragments = collapseFragments;
|
|
178
|
+
exports.compact = compact;
|
|
179
|
+
exports.createFragment = createFragment;
|
|
180
|
+
exports.format = format;
|
|
181
|
+
exports.getAllByTestId = getAllByTestId;
|
|
182
|
+
exports.getByTestId = getByTestId;
|
|
183
|
+
exports.getSingleTextNode = getSingleTextNode;
|
|
184
|
+
exports.removeTestIds = removeTestIds;
|
|
185
|
+
exports.safeXml = safeXml;
|
|
@@ -7,9 +7,9 @@ declare function asXmlElement(xml: string | Element): Element;
|
|
|
7
7
|
* @param templateValues tamplate values
|
|
8
8
|
* @returns xml string
|
|
9
9
|
*/
|
|
10
|
-
declare function safeXml(templateStrings: TemplateStringsArray, ...templateValues: (undefined | null | string | number | (string | number)[])[]): string;
|
|
10
|
+
declare function safeXml(templateStrings: TemplateStringsArray, ...templateValues: (undefined | null | string | number | (undefined | null | string | number)[])[]): string;
|
|
11
11
|
|
|
12
|
-
declare function cdata(input: string | number): string;
|
|
12
|
+
declare function cdata(input: string | number | null | undefined): string;
|
|
13
13
|
|
|
14
14
|
declare function compact(xml: string): string;
|
|
15
15
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Element, ElementCompact } from 'xml-js';
|
|
2
|
+
|
|
3
|
+
declare function asXmlElement(xml: string | Element): Element;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param templateStrings template strings
|
|
7
|
+
* @param templateValues tamplate values
|
|
8
|
+
* @returns xml string
|
|
9
|
+
*/
|
|
10
|
+
declare function safeXml(templateStrings: TemplateStringsArray, ...templateValues: (undefined | null | string | number | (undefined | null | string | number)[])[]): string;
|
|
11
|
+
|
|
12
|
+
declare function cdata(input: string | number | null | undefined): string;
|
|
13
|
+
|
|
14
|
+
declare function compact(xml: string): string;
|
|
15
|
+
|
|
16
|
+
declare function collapseFragments(root: string | Element): Element | Element[];
|
|
17
|
+
|
|
18
|
+
type FormatOptions = {
|
|
19
|
+
spaces?: number;
|
|
20
|
+
};
|
|
21
|
+
declare function format(input: string, { spaces }?: FormatOptions): string;
|
|
22
|
+
|
|
23
|
+
declare function createFragment(elements: Element[]): Element;
|
|
24
|
+
|
|
25
|
+
declare function getAllByTestId(root: Element | ElementCompact, id: string): (Element | ElementCompact)[];
|
|
26
|
+
|
|
27
|
+
declare function getByTestId(root: Element | ElementCompact, id: string): Element | ElementCompact | undefined;
|
|
28
|
+
|
|
29
|
+
declare function getSingleTextNode(element: Element | ElementCompact | undefined): Element;
|
|
30
|
+
|
|
31
|
+
declare function removeTestIds(root: Element | ElementCompact): Element;
|
|
32
|
+
|
|
33
|
+
declare function clean(element: Element | ElementCompact): Element | Element[];
|
|
34
|
+
|
|
35
|
+
export { asXmlElement, cdata, clean, collapseFragments, compact, createFragment, format, getAllByTestId, getByTestId, getSingleTextNode, removeTestIds, safeXml };
|
|
36
|
+
export type { FormatOptions };
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooxml-tools/xml",
|
|
3
3
|
"description": "Some XML helpers to help with OOXML development",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.2",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"main": "./dist/npm/index.js",
|
|
7
|
-
"types": "./dist/npm/types.d.ts",
|
|
8
6
|
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ooxml-tools/xml"
|
|
10
|
+
},
|
|
9
11
|
"scripts": {
|
|
10
12
|
"lint": "npx prettier . --check",
|
|
11
13
|
"test": "vitest run --coverage",
|
|
@@ -13,6 +15,18 @@
|
|
|
13
15
|
"lint:format": "npx prettier . --write",
|
|
14
16
|
"build": "rollup -c rollup.config.ts --configPlugin typescript"
|
|
15
17
|
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/npm/es/index.js",
|
|
22
|
+
"default": "./dist/npm/es/types.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/npm/cjs/index.js",
|
|
26
|
+
"default": "./dist/npm/cjs/types.d.ts"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
16
30
|
"files": [
|
|
17
31
|
"./dist/npm",
|
|
18
32
|
"./package.json",
|