xml-model 2.0.0-beta.3 → 2.0.0-beta.4
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/dist/xml/xml-js.d.ts +7 -22
- package/dist/xml/xml-js.js +14 -6
- package/package.json +1 -1
package/dist/xml/xml-js.d.ts
CHANGED
|
@@ -120,21 +120,6 @@ export type ParseOptions = IgnoreOptions & ChangingKeyNames & {
|
|
|
120
120
|
* @default false
|
|
121
121
|
*/
|
|
122
122
|
addParent?: boolean;
|
|
123
|
-
/**
|
|
124
|
-
* Whether to always wrap sub-elements in an array, even when there is only one.
|
|
125
|
-
* Pass an array of element names to restrict this behaviour to those names only.
|
|
126
|
-
* Only applicable in compact mode.
|
|
127
|
-
* @default false
|
|
128
|
-
*/
|
|
129
|
-
alwaysArray?: boolean | Array<string>;
|
|
130
|
-
/**
|
|
131
|
-
* Whether to always emit an `elements` array even on empty elements.
|
|
132
|
-
* `<a></a>` becomes `{ elements: [{ type: "element", name: "a", elements: [] }] }`
|
|
133
|
-
* instead of `{ elements: [{ type: "element", name: "a" }] }`.
|
|
134
|
-
* Only applicable in non-compact mode.
|
|
135
|
-
* @default false
|
|
136
|
-
*/
|
|
137
|
-
alwaysChildren?: boolean;
|
|
138
123
|
/**
|
|
139
124
|
* Whether to parse the contents of processing instructions as attributes.
|
|
140
125
|
* `<?go to="there"?>` becomes `{ go: { attributes: { to: "there" } } }`
|
|
@@ -221,14 +206,8 @@ declare function isEmpty(xml: object): xml is XMLVoid;
|
|
|
221
206
|
*/
|
|
222
207
|
declare function getContent(xml: XMLElement): string;
|
|
223
208
|
/**
|
|
224
|
-
* Creates a
|
|
225
|
-
* When no tag name is provided, returns a fragment with a text child.
|
|
226
|
-
* When a tag name is provided, returns a full element with the given name and optional attributes.
|
|
227
|
-
*
|
|
209
|
+
* Creates a text-only fragment wrapping the given content.
|
|
228
210
|
* @param content - The text content to wrap (defaults to empty string).
|
|
229
|
-
* @param tag - Optional element tag name.
|
|
230
|
-
* @param attributes - Optional attributes; only valid when `tag` is provided.
|
|
231
|
-
* @throws {TypeError} When `attributes` are provided without a `tag`.
|
|
232
211
|
*/
|
|
233
212
|
declare function fromContent(content: string): {
|
|
234
213
|
elements: [{
|
|
@@ -236,6 +215,12 @@ declare function fromContent(content: string): {
|
|
|
236
215
|
text: string;
|
|
237
216
|
}] | [];
|
|
238
217
|
};
|
|
218
|
+
/**
|
|
219
|
+
* Creates a full element wrapping the given text content.
|
|
220
|
+
* @param content - The text content to wrap (defaults to empty string).
|
|
221
|
+
* @param tag - Element tag name.
|
|
222
|
+
* @param attributes - Optional attributes.
|
|
223
|
+
*/
|
|
239
224
|
declare function fromContent(content: string, tag: string, attributes?: XMLElement["attributes"]): {
|
|
240
225
|
type: "element";
|
|
241
226
|
name: string;
|
package/dist/xml/xml-js.js
CHANGED
|
@@ -25,14 +25,22 @@ var ZXMLNode = z.discriminatedUnion("type", [
|
|
|
25
25
|
]);
|
|
26
26
|
var ZXMLRoot = z.object({ elements: z.array(ZXMLNode) });
|
|
27
27
|
function parse(xml, options = {}) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const res = (0, import_lib.xml2js)(xml,
|
|
28
|
+
if ("compact" in options) throw new Error("xml-model always uses non-compact mode (compact is forced to false)");
|
|
29
|
+
if ("alwaysChildren" in options) throw new Error("xml-model always parses with alwaysChildren: true");
|
|
30
|
+
const res = (0, import_lib.xml2js)(xml, {
|
|
31
|
+
...options,
|
|
32
|
+
compact: false,
|
|
33
|
+
alwaysChildren: true
|
|
34
|
+
});
|
|
31
35
|
if ("elements" in res) return res;
|
|
32
36
|
throw new Error("Got empty XML");
|
|
33
37
|
}
|
|
34
38
|
function stringify(xml, options = {}) {
|
|
35
|
-
|
|
39
|
+
if ("compact" in options) throw new Error("xml-model always uses non-compact mode (compact is forced to false)");
|
|
40
|
+
return (0, import_lib.js2xml)(xml, {
|
|
41
|
+
...options,
|
|
42
|
+
compact: false
|
|
43
|
+
});
|
|
36
44
|
}
|
|
37
45
|
function isRoot(xml) {
|
|
38
46
|
return Object.keys(xml).length === 1 && "elements" in xml && Array.isArray(xml.elements);
|
|
@@ -51,11 +59,11 @@ function isEmpty(xml) {
|
|
|
51
59
|
* @throws {TypeError} When the element has multiple or non-text children.
|
|
52
60
|
*/
|
|
53
61
|
function getContent(xml) {
|
|
54
|
-
if (xml.elements
|
|
62
|
+
if (!xml.elements.length) return "";
|
|
63
|
+
if (xml.elements.length === 1) {
|
|
55
64
|
const content = xml.elements[0];
|
|
56
65
|
if (content.type === "text") return content.text;
|
|
57
66
|
}
|
|
58
|
-
if (!xml.elements) return "";
|
|
59
67
|
throw new TypeError(`can't get text from XMLElement: ${JSON.stringify(xml)}`);
|
|
60
68
|
}
|
|
61
69
|
function fromContent(content = "", tag, attributes) {
|