@office-open/xml 0.8.1 → 0.9.1

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 CHANGED
@@ -143,4 +143,4 @@ Performance comparison against original `xml` (1.0.1) and `xml-js` (1.6.11) pack
143
143
 
144
144
  ## License
145
145
 
146
- - [MIT](LICENSE) © [Demo Macro](https://imst.xyz/)
146
+ - [MIT](LICENSE) © [Demo Macro](https://www.demomacro.com/)
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 xml2js(xmlString: string, options?: Xml2JsOptions): Element;
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 js2xml(js: Element, options?: Js2XmlOptions): string;
20
- /** Alias for js2xml xml-js compatible export */
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
@@ -40,14 +45,43 @@ declare function escapeXml(str: string): string;
40
45
  * // => ' id="1" name="foo"'
41
46
  */
42
47
  declare function attrs(record: Record<string, string | number | boolean | undefined>): string;
48
+ /**
49
+ * Build an XML attribute string without escaping.
50
+ *
51
+ * Same as `attrs()` but skips `typeof` checks and `escapeXml` — use only when
52
+ * all values are known-safe (numbers, booleans, or strings free of `& " ' < >`).
53
+ * Avoids per-call array and `Object.keys()` allocation in hot loops.
54
+ *
55
+ * @example
56
+ * attrsRaw({ r: "A1", s: 5 })
57
+ * // => ' r="A1" s="5"'
58
+ */
59
+ declare function attrsRaw(record: Record<string, string | number | boolean | undefined>): string;
43
60
  /**
44
61
  * Build a self-closing XML element: `<tag attrStr/>`.
45
62
  * `attrStr` is a pre-serialized attribute string (from `attrs()`) or undefined.
46
63
  */
47
64
  declare function selfCloseElement(tag: string, attrStr?: string): string;
65
+ /**
66
+ * Build a complete XML element string from name, optional attributes, and string children.
67
+ *
68
+ * Replaces `new BuilderElement({...})` + `.toXml()` / `.serialize()` with a
69
+ * single function call returning a string — zero object allocation.
70
+ *
71
+ * @param name Element tag name (e.g. `"a:srgbClr"`)
72
+ * @param attrRecord Optional flat attribute map; `undefined` values are skipped
73
+ * @param children Optional pre-serialized child XML strings
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * element("a:solidFill", undefined, [element("a:srgbClr", { val: "FF0000" })])
78
+ * // => '<a:solidFill><a:srgbClr val="FF0000"/></a:solidFill>'
79
+ * ```
80
+ */
81
+ declare function element(name: string, attrRecord?: Readonly<Record<string, string | number | boolean | undefined>>, children?: readonly string[]): string;
48
82
  //#endregion
49
83
  //#region src/json.d.ts
50
84
  /** Convert XML string to JSON string — xml-js compatible export */
51
85
  declare function xml2json(xml: string, options?: Xml2JsOptions): string;
52
86
  //#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, xml2js, xml2json };
87
+ 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, attrsRaw, 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,11 +41,30 @@ function escapeXml(str) {
41
41
  * // => ' id="1" name="foo"'
42
42
  */
43
43
  function attrs(record) {
44
- let s = "";
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) s += ` ${keys[i]}="${typeof v === "string" ? escapeXml(v) : v}"`;
48
+ if (v !== void 0) parts.push(` ${keys[i]}="${typeof v === "string" ? escapeXml(v) : v}"`);
49
+ }
50
+ return parts.join("");
51
+ }
52
+ /**
53
+ * Build an XML attribute string without escaping.
54
+ *
55
+ * Same as `attrs()` but skips `typeof` checks and `escapeXml` — use only when
56
+ * all values are known-safe (numbers, booleans, or strings free of `& " ' < >`).
57
+ * Avoids per-call array and `Object.keys()` allocation in hot loops.
58
+ *
59
+ * @example
60
+ * attrsRaw({ r: "A1", s: 5 })
61
+ * // => ' r="A1" s="5"'
62
+ */
63
+ function attrsRaw(record) {
64
+ let s = "";
65
+ for (const key in record) {
66
+ const v = record[key];
67
+ if (v !== void 0) s += ` ${key}="${v}"`;
49
68
  }
50
69
  return s;
51
70
  }
@@ -56,9 +75,36 @@ function attrs(record) {
56
75
  function selfCloseElement(tag, attrStr) {
57
76
  return attrStr ? `<${tag}${attrStr}/>` : `<${tag}/>`;
58
77
  }
78
+ /**
79
+ * Build a complete XML element string from name, optional attributes, and string children.
80
+ *
81
+ * Replaces `new BuilderElement({...})` + `.toXml()` / `.serialize()` with a
82
+ * single function call returning a string — zero object allocation.
83
+ *
84
+ * @param name Element tag name (e.g. `"a:srgbClr"`)
85
+ * @param attrRecord Optional flat attribute map; `undefined` values are skipped
86
+ * @param children Optional pre-serialized child XML strings
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * element("a:solidFill", undefined, [element("a:srgbClr", { val: "FF0000" })])
91
+ * // => '<a:solidFill><a:srgbClr val="FF0000"/></a:solidFill>'
92
+ * ```
93
+ */
94
+ function element(name, attrRecord, children) {
95
+ const attrStr = attrRecord ? attrs(attrRecord) : void 0;
96
+ if (!children || children.length === 0) return selfCloseElement(name, attrStr);
97
+ const body = children.join("");
98
+ return body.length === 0 ? selfCloseElement(name, attrStr) : `<${name}${attrStr ?? ""}>${body}</${name}>`;
99
+ }
59
100
  //#endregion
60
101
  //#region src/serialize.ts
61
102
  const DEFAULT_INDENT = " ";
103
+ /**
104
+ * Serialize IXmlableObject to XML string.
105
+ * @deprecated Use `stringify` (Element → string) instead. This IXmlableObject path
106
+ * will be removed once the Descriptor migration is complete.
107
+ */
62
108
  function xml(input, options) {
63
109
  const opts = normalizeOptions$1(options);
64
110
  const parts = [];
@@ -66,10 +112,10 @@ function xml(input, options) {
66
112
  const declOpts = opts.declaration === true ? {} : opts.declaration;
67
113
  const enc = declOpts.encoding || "UTF-8";
68
114
  const sa = declOpts.standalone;
69
- let decl = "<?xml version=\"1.0\" encoding=\"" + enc + "\"";
70
- if (sa) decl += " standalone=\"" + sa + "\"";
71
- decl += "?>";
72
- parts.push(decl);
115
+ const declParts = [`<?xml version="1.0" encoding="${enc}"`];
116
+ if (sa) declParts.push(` standalone="${sa}"`);
117
+ declParts.push("?>");
118
+ parts.push(declParts.join(""));
73
119
  if (opts.indent) parts.push("\n");
74
120
  }
75
121
  const items = Array.isArray(input) ? input : [input];
@@ -171,7 +217,7 @@ function nativeTypeValue(value) {
171
217
  if (lower === "false") return false;
172
218
  return value;
173
219
  }
174
- function xml2js(xmlString, options) {
220
+ function parse(xmlString, options) {
175
221
  const captureSpaces = options?.captureSpacesBetweenElements ?? false;
176
222
  const trim = options?.trim ?? false;
177
223
  const ignoreDeclaration = options?.ignoreDeclaration ?? false;
@@ -355,16 +401,16 @@ function isWhitespace(ch) {
355
401
  }
356
402
  //#endregion
357
403
  //#region src/stringify.ts
358
- function js2xml(js, options) {
404
+ function stringify(js, options) {
359
405
  const opts = normalizeOptions(options);
360
406
  const parts = [];
361
407
  if (js.declaration && !opts.ignoreDeclaration) parts.push(writeDeclaration(js.declaration));
362
408
  if (js.elements?.length) parts.push(writeElements(js.elements, opts, 0, !parts.length));
363
409
  return parts.join("");
364
410
  }
365
- /** Alias for js2xml xml-js compatible export */
411
+ /** @deprecated Use `stringify` instead. xml-js compatible alias. */
366
412
  function json2xml(json, options) {
367
- return js2xml(json, options);
413
+ return stringify(json, options);
368
414
  }
369
415
  function normalizeOptions(options) {
370
416
  if (!options) return {
@@ -399,10 +445,10 @@ function writeIndentation(spaces, depth, firstLine) {
399
445
  function writeDeclaration(declaration) {
400
446
  const attrs = declaration.attributes;
401
447
  if (!attrs) return "<?xml version=\"1.0\"?>";
402
- let result = "<?xml version=\"1.0\"";
403
- if (attrs.encoding) result += ` encoding="${attrs.encoding}"`;
404
- if (attrs.standalone) result += ` standalone="${attrs.standalone}"`;
405
- return result + "?>";
448
+ const parts = [`<?xml version="1.0"`];
449
+ if (attrs.encoding) parts.push(` encoding="${attrs.encoding}"`);
450
+ if (attrs.standalone) parts.push(` standalone="${attrs.standalone}"`);
451
+ return parts.join("") + "?>";
406
452
  }
407
453
  function writeAttributes(attributes, elementName, element, attributeValueFn) {
408
454
  const parts = [];
@@ -550,7 +596,7 @@ function toElement(xmlObject) {
550
596
  //#region src/json.ts
551
597
  /** Convert XML string to JSON string — xml-js compatible export */
552
598
  function xml2json(xml, options) {
553
- return JSON.stringify(xml2js(xml, options));
599
+ return JSON.stringify(parse(xml, options));
554
600
  }
555
601
  //#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, xml2js, xml2json };
602
+ export { allChildren, attr, attrBool, attrNum, attrs, attrsRaw, 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.8.1",
3
+ "version": "0.9.1",
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://imst.xyz/"
21
+ "url": "https://www.demomacro.com/"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",