@office-open/xml 0.8.0 → 0.9.0
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 +1 -1
- package/dist/index.d.mts +26 -4
- package/dist/index.mjs +44 -17
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { C as XmlAttrs, D as XmlOption, E as XmlObject, S as XmlAtom, T as XmlDescArray, _ as ElementCompact, a as childCount, b as Js2XmlOptions, c as collectText, d as findDeep, f as hasChild, g as Element, h as DeclarationAttributes, i as attrNum, l as colorAttr, m as Attributes, n as attr, o as childText, p as textOf, r as attrBool, s as children, t as allChildren, u as findChild, v as ElementObject, w as XmlDesc, x as Xml2JsOptions, y as IgnoreOptions } from "./utils-DJSm61Ws.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/serialize.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Serialize IXmlableObject to XML string.
|
|
6
|
+
* @deprecated Use `stringify` (Element → string) instead. This IXmlableObject path
|
|
7
|
+
* will be removed once the Descriptor migration is complete.
|
|
8
|
+
*/
|
|
4
9
|
declare function xml(input: Record<string, unknown> | Record<string, unknown>[], options?: boolean | string | {
|
|
5
10
|
indent?: boolean | string;
|
|
6
11
|
declaration?: boolean | {
|
|
@@ -12,12 +17,12 @@ declare function xml(input: Record<string, unknown> | Record<string, unknown>[],
|
|
|
12
17
|
//#region src/parse.d.ts
|
|
13
18
|
declare function unescapeXml(str: string): string;
|
|
14
19
|
declare function nativeTypeValue(value: string): string | number | boolean;
|
|
15
|
-
declare function
|
|
20
|
+
declare function parse(xmlString: string, options?: Xml2JsOptions): Element;
|
|
16
21
|
declare function parseAttributes(str: string): Record<string, string>;
|
|
17
22
|
//#endregion
|
|
18
23
|
//#region src/stringify.d.ts
|
|
19
|
-
declare function
|
|
20
|
-
/**
|
|
24
|
+
declare function stringify(js: Element, options?: Js2XmlOptions): string;
|
|
25
|
+
/** @deprecated Use `stringify` instead. xml-js compatible alias. */
|
|
21
26
|
declare function json2xml(json: Element, options?: Js2XmlOptions): string;
|
|
22
27
|
//#endregion
|
|
23
28
|
//#region src/convert.d.ts
|
|
@@ -45,9 +50,26 @@ declare function attrs(record: Record<string, string | number | boolean | undefi
|
|
|
45
50
|
* `attrStr` is a pre-serialized attribute string (from `attrs()`) or undefined.
|
|
46
51
|
*/
|
|
47
52
|
declare function selfCloseElement(tag: string, attrStr?: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Build a complete XML element string from name, optional attributes, and string children.
|
|
55
|
+
*
|
|
56
|
+
* Replaces `new BuilderElement({...})` + `.toXml()` / `.serialize()` with a
|
|
57
|
+
* single function call returning a string — zero object allocation.
|
|
58
|
+
*
|
|
59
|
+
* @param name Element tag name (e.g. `"a:srgbClr"`)
|
|
60
|
+
* @param attrRecord Optional flat attribute map; `undefined` values are skipped
|
|
61
|
+
* @param children Optional pre-serialized child XML strings
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* element("a:solidFill", undefined, [element("a:srgbClr", { val: "FF0000" })])
|
|
66
|
+
* // => '<a:solidFill><a:srgbClr val="FF0000"/></a:solidFill>'
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function element(name: string, attrRecord?: Readonly<Record<string, string | number | boolean | undefined>>, children?: readonly string[]): string;
|
|
48
70
|
//#endregion
|
|
49
71
|
//#region src/json.d.ts
|
|
50
72
|
/** Convert XML string to JSON string — xml-js compatible export */
|
|
51
73
|
declare function xml2json(xml: string, options?: Xml2JsOptions): string;
|
|
52
74
|
//#endregion
|
|
53
|
-
export { type Attributes, type DeclarationAttributes, type Element, type ElementCompact, type ElementObject, type IgnoreOptions, type Js2XmlOptions, type Xml2JsOptions, type XmlAtom, type XmlAttrs, type XmlDesc, type XmlDescArray, type XmlObject, type XmlOption, allChildren, attr, attrBool, attrNum, attrs, childCount, childText, children, collectText, colorAttr, escapeXml, findChild, findDeep, hasChild, js2xml, json2xml, nativeTypeValue, parseAttributes, selfCloseElement, textOf, toElement, unescapeXml, xml,
|
|
75
|
+
export { type Attributes, type DeclarationAttributes, type Element, type ElementCompact, type ElementObject, type IgnoreOptions, type Js2XmlOptions, type Xml2JsOptions, type XmlAtom, type XmlAttrs, type XmlDesc, type XmlDescArray, type XmlObject, type XmlOption, allChildren, attr, attrBool, attrNum, attrs, childCount, childText, children, collectText, colorAttr, element, escapeXml, findChild, findDeep, hasChild, stringify as js2xml, stringify, json2xml, nativeTypeValue, parse, parse as xml2js, parseAttributes, selfCloseElement, textOf, toElement, unescapeXml, xml, xml2json };
|
package/dist/index.mjs
CHANGED
|
@@ -41,13 +41,13 @@ function escapeXml(str) {
|
|
|
41
41
|
* // => ' id="1" name="foo"'
|
|
42
42
|
*/
|
|
43
43
|
function attrs(record) {
|
|
44
|
-
|
|
44
|
+
const parts = [];
|
|
45
45
|
const keys = Object.keys(record);
|
|
46
46
|
for (let i = 0; i < keys.length; i++) {
|
|
47
47
|
const v = record[keys[i]];
|
|
48
|
-
if (v !== void 0)
|
|
48
|
+
if (v !== void 0) parts.push(` ${keys[i]}="${typeof v === "string" ? escapeXml(v) : v}"`);
|
|
49
49
|
}
|
|
50
|
-
return
|
|
50
|
+
return parts.join("");
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* Build a self-closing XML element: `<tag attrStr/>`.
|
|
@@ -56,9 +56,36 @@ function attrs(record) {
|
|
|
56
56
|
function selfCloseElement(tag, attrStr) {
|
|
57
57
|
return attrStr ? `<${tag}${attrStr}/>` : `<${tag}/>`;
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Build a complete XML element string from name, optional attributes, and string children.
|
|
61
|
+
*
|
|
62
|
+
* Replaces `new BuilderElement({...})` + `.toXml()` / `.serialize()` with a
|
|
63
|
+
* single function call returning a string — zero object allocation.
|
|
64
|
+
*
|
|
65
|
+
* @param name Element tag name (e.g. `"a:srgbClr"`)
|
|
66
|
+
* @param attrRecord Optional flat attribute map; `undefined` values are skipped
|
|
67
|
+
* @param children Optional pre-serialized child XML strings
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* element("a:solidFill", undefined, [element("a:srgbClr", { val: "FF0000" })])
|
|
72
|
+
* // => '<a:solidFill><a:srgbClr val="FF0000"/></a:solidFill>'
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function element(name, attrRecord, children) {
|
|
76
|
+
const attrStr = attrRecord ? attrs(attrRecord) : void 0;
|
|
77
|
+
if (!children || children.length === 0) return selfCloseElement(name, attrStr);
|
|
78
|
+
const body = children.join("");
|
|
79
|
+
return body.length === 0 ? selfCloseElement(name, attrStr) : `<${name}${attrStr ?? ""}>${body}</${name}>`;
|
|
80
|
+
}
|
|
59
81
|
//#endregion
|
|
60
82
|
//#region src/serialize.ts
|
|
61
83
|
const DEFAULT_INDENT = " ";
|
|
84
|
+
/**
|
|
85
|
+
* Serialize IXmlableObject to XML string.
|
|
86
|
+
* @deprecated Use `stringify` (Element → string) instead. This IXmlableObject path
|
|
87
|
+
* will be removed once the Descriptor migration is complete.
|
|
88
|
+
*/
|
|
62
89
|
function xml(input, options) {
|
|
63
90
|
const opts = normalizeOptions$1(options);
|
|
64
91
|
const parts = [];
|
|
@@ -66,10 +93,10 @@ function xml(input, options) {
|
|
|
66
93
|
const declOpts = opts.declaration === true ? {} : opts.declaration;
|
|
67
94
|
const enc = declOpts.encoding || "UTF-8";
|
|
68
95
|
const sa = declOpts.standalone;
|
|
69
|
-
|
|
70
|
-
if (sa)
|
|
71
|
-
|
|
72
|
-
parts.push(
|
|
96
|
+
const declParts = [`<?xml version="1.0" encoding="${enc}"`];
|
|
97
|
+
if (sa) declParts.push(` standalone="${sa}"`);
|
|
98
|
+
declParts.push("?>");
|
|
99
|
+
parts.push(declParts.join(""));
|
|
73
100
|
if (opts.indent) parts.push("\n");
|
|
74
101
|
}
|
|
75
102
|
const items = Array.isArray(input) ? input : [input];
|
|
@@ -171,7 +198,7 @@ function nativeTypeValue(value) {
|
|
|
171
198
|
if (lower === "false") return false;
|
|
172
199
|
return value;
|
|
173
200
|
}
|
|
174
|
-
function
|
|
201
|
+
function parse(xmlString, options) {
|
|
175
202
|
const captureSpaces = options?.captureSpacesBetweenElements ?? false;
|
|
176
203
|
const trim = options?.trim ?? false;
|
|
177
204
|
const ignoreDeclaration = options?.ignoreDeclaration ?? false;
|
|
@@ -355,16 +382,16 @@ function isWhitespace(ch) {
|
|
|
355
382
|
}
|
|
356
383
|
//#endregion
|
|
357
384
|
//#region src/stringify.ts
|
|
358
|
-
function
|
|
385
|
+
function stringify(js, options) {
|
|
359
386
|
const opts = normalizeOptions(options);
|
|
360
387
|
const parts = [];
|
|
361
388
|
if (js.declaration && !opts.ignoreDeclaration) parts.push(writeDeclaration(js.declaration));
|
|
362
389
|
if (js.elements?.length) parts.push(writeElements(js.elements, opts, 0, !parts.length));
|
|
363
390
|
return parts.join("");
|
|
364
391
|
}
|
|
365
|
-
/**
|
|
392
|
+
/** @deprecated Use `stringify` instead. xml-js compatible alias. */
|
|
366
393
|
function json2xml(json, options) {
|
|
367
|
-
return
|
|
394
|
+
return stringify(json, options);
|
|
368
395
|
}
|
|
369
396
|
function normalizeOptions(options) {
|
|
370
397
|
if (!options) return {
|
|
@@ -399,10 +426,10 @@ function writeIndentation(spaces, depth, firstLine) {
|
|
|
399
426
|
function writeDeclaration(declaration) {
|
|
400
427
|
const attrs = declaration.attributes;
|
|
401
428
|
if (!attrs) return "<?xml version=\"1.0\"?>";
|
|
402
|
-
|
|
403
|
-
if (attrs.encoding)
|
|
404
|
-
if (attrs.standalone)
|
|
405
|
-
return
|
|
429
|
+
const parts = [`<?xml version="1.0"`];
|
|
430
|
+
if (attrs.encoding) parts.push(` encoding="${attrs.encoding}"`);
|
|
431
|
+
if (attrs.standalone) parts.push(` standalone="${attrs.standalone}"`);
|
|
432
|
+
return parts.join("") + "?>";
|
|
406
433
|
}
|
|
407
434
|
function writeAttributes(attributes, elementName, element, attributeValueFn) {
|
|
408
435
|
const parts = [];
|
|
@@ -550,7 +577,7 @@ function toElement(xmlObject) {
|
|
|
550
577
|
//#region src/json.ts
|
|
551
578
|
/** Convert XML string to JSON string — xml-js compatible export */
|
|
552
579
|
function xml2json(xml, options) {
|
|
553
|
-
return JSON.stringify(
|
|
580
|
+
return JSON.stringify(parse(xml, options));
|
|
554
581
|
}
|
|
555
582
|
//#endregion
|
|
556
|
-
export { allChildren, attr, attrBool, attrNum, attrs, childCount, childText, children, collectText, colorAttr, escapeXml, findChild, findDeep, hasChild, js2xml, json2xml, nativeTypeValue, parseAttributes, selfCloseElement, textOf, toElement, unescapeXml, xml,
|
|
583
|
+
export { allChildren, attr, attrBool, attrNum, attrs, childCount, childText, children, collectText, colorAttr, element, escapeXml, findChild, findDeep, hasChild, stringify as js2xml, stringify, json2xml, nativeTypeValue, parse, parse as xml2js, parseAttributes, selfCloseElement, textOf, toElement, unescapeXml, xml, xml2json };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@office-open/xml",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "XML parsing and serialization for Office Open XML. Zero dependencies, drop-in replacement for xml + xml-js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"office-open",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"author": {
|
|
19
19
|
"name": "Demo Macro",
|
|
20
20
|
"email": "abc@imst.xyz",
|
|
21
|
-
"url": "https://
|
|
21
|
+
"url": "https://www.demomacro.com/"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|