@office-open/core 0.4.5 → 0.5.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.
@@ -1,381 +0,0 @@
1
- import { hpsMeasureValue } from "../values.mjs";
2
- import { xml, xml2js } from "@office-open/xml";
3
- //#region src/xml-components/base.ts
4
- /**
5
- * Abstract base class for all XML components.
6
- */
7
- var BaseXmlComponent = class {
8
- /** The XML element name for this component (e.g., "w:p" for paragraph). */
9
- rootKey;
10
- constructor(rootKey) {
11
- this.rootKey = rootKey;
12
- }
13
- };
14
- //#endregion
15
- //#region src/xml-components/component.ts
16
- /**
17
- * Core XML Component classes for building OOXML element trees.
18
- *
19
- * @module
20
- */
21
- /**
22
- * Empty object singleton used for empty XML elements.
23
- *
24
- * @internal
25
- */
26
- const EMPTY_OBJECT = Object.seal({});
27
- /**
28
- * Base class for all XML components in OOXML documents.
29
- */
30
- var XmlComponent = class extends BaseXmlComponent {
31
- /**
32
- * Array of child components, text nodes, and attributes.
33
- */
34
- root;
35
- constructor(rootKey) {
36
- super(rootKey);
37
- this.root = new Array();
38
- }
39
- /**
40
- * Prepares this component and its children for XML serialization.
41
- */
42
- prepForXml(context) {
43
- context.stack.push(this);
44
- const children = [];
45
- for (const comp of this.root) if (comp instanceof BaseXmlComponent) {
46
- const prepared = comp.prepForXml(context);
47
- if (prepared !== void 0) children.push(prepared);
48
- } else children.push(comp);
49
- context.stack.pop();
50
- return { [this.rootKey]: children.length ? children.length === 1 && children[0] && typeof children[0] === "object" && "_attr" in children[0] ? children[0] : children : EMPTY_OBJECT };
51
- }
52
- /**
53
- * Direct XML serialization. Override in subclasses for zero-allocation output.
54
- * Default falls back to prepForXml() + xml().
55
- */
56
- toXml(context) {
57
- const obj = this.prepForXml(context);
58
- return obj ? xml(obj) : "";
59
- }
60
- /**
61
- * @deprecated Internal use only.
62
- */
63
- addChildElement(child) {
64
- this.root.push(child);
65
- return this;
66
- }
67
- };
68
- /**
69
- * XML component that is excluded from output if it has no meaningful content.
70
- */
71
- var IgnoreIfEmptyXmlComponent = class extends XmlComponent {
72
- includeIfEmpty;
73
- constructor(rootKey, includeIfEmpty) {
74
- super(rootKey);
75
- this.includeIfEmpty = includeIfEmpty;
76
- }
77
- prepForXml(context) {
78
- const result = super.prepForXml(context);
79
- if (this.includeIfEmpty) return result;
80
- if (result && (typeof result[this.rootKey] !== "object" || Object.keys(result[this.rootKey]).length)) return result;
81
- }
82
- };
83
- //#endregion
84
- //#region src/xml-components/attributes.ts
85
- /**
86
- * XML attribute components for OOXML document generation.
87
- *
88
- * @module
89
- */
90
- /**
91
- * Base class for creating XML attributes with automatic name mapping.
92
- */
93
- var XmlAttributeComponent = class extends BaseXmlComponent {
94
- /** Optional mapping from property names to XML attribute names. */
95
- xmlKeys;
96
- constructor(root) {
97
- super("_attr");
98
- this.root = root;
99
- }
100
- prepForXml(_) {
101
- const attrs = {};
102
- Object.entries(this.root).forEach(([key, value]) => {
103
- if (value !== void 0) {
104
- const newKey = this.xmlKeys && this.xmlKeys[key] || key;
105
- attrs[newKey] = value;
106
- }
107
- });
108
- return { _attr: attrs };
109
- }
110
- };
111
- /**
112
- * Next-generation attribute component with explicit key-value pairs.
113
- */
114
- var NextAttributeComponent = class extends BaseXmlComponent {
115
- constructor(root) {
116
- super("_attr");
117
- this.root = root;
118
- }
119
- prepForXml(_) {
120
- return { _attr: Object.values(this.root).filter(({ value }) => value !== void 0).reduce((acc, { key, value }) => ({
121
- ...acc,
122
- [key]: value
123
- }), {}) };
124
- }
125
- };
126
- //#endregion
127
- //#region src/xml-components/elements.ts
128
- /**
129
- * Simple XML element types for common OOXML patterns.
130
- *
131
- * @module
132
- */
133
- const ON_OFF_TRUE_CACHE = /* @__PURE__ */ new Map();
134
- /**
135
- * Build a CT_OnOff XML object without allocating any XmlComponent.
136
- * `val=true` returns a frozen singleton (cached per name).
137
- */
138
- function onOffObj(name, val = true) {
139
- if (val === true) {
140
- let cached = ON_OFF_TRUE_CACHE.get(name);
141
- if (!cached) {
142
- cached = Object.freeze({ [name]: Object.freeze({}) });
143
- ON_OFF_TRUE_CACHE.set(name, cached);
144
- }
145
- return cached;
146
- }
147
- const ns = name.split(":")[0];
148
- return { [name]: { _attr: { [`${ns}:val`]: val } } };
149
- }
150
- /**
151
- * Build a CT_HpsMeasure XML object (half-point size) without allocation.
152
- */
153
- function hpsMeasureObj(name, val) {
154
- const ns = name.split(":")[0];
155
- return { [name]: { _attr: { [`${ns}:val`]: hpsMeasureValue(val) } } };
156
- }
157
- /**
158
- * Build a CT_String XML object (string value attribute) without allocation.
159
- */
160
- function stringValObj(name, val) {
161
- const ns = name.split(":")[0];
162
- return { [name]: { _attr: { [`${ns}:val`]: val } } };
163
- }
164
- /**
165
- * Build a numeric value attribute XML object without allocation.
166
- */
167
- function numberValObj(name, val) {
168
- const ns = name.split(":")[0];
169
- return { [name]: { _attr: { [`${ns}:val`]: val } } };
170
- }
171
- /**
172
- * Build a string enum value attribute XML object without allocation.
173
- */
174
- function stringEnumValObj(name, val) {
175
- const ns = name.split(":")[0];
176
- return { [name]: { _attr: { [`${ns}:val`]: val } } };
177
- }
178
- /**
179
- * Build an element wrapping a text string without allocation.
180
- */
181
- function stringContainerObj(name, val) {
182
- return { [name]: [val] };
183
- }
184
- /**
185
- * XML element representing a boolean on/off value (CT_OnOff).
186
- * @deprecated Use `onOffObj()` for hot-path code.
187
- */
188
- var OnOffElement = class extends XmlComponent {
189
- constructor(name, val = true) {
190
- super(name);
191
- if (val !== true) this.root.push(new NextAttributeComponent({ val: {
192
- key: `${name.split(":")[0]}:val`,
193
- value: val
194
- } }));
195
- }
196
- };
197
- /**
198
- * XML element representing a half-point size measurement (CT_HpsMeasure).
199
- * @deprecated Use `hpsMeasureObj()` for hot-path code.
200
- */
201
- var HpsMeasureElement = class extends XmlComponent {
202
- constructor(name, val) {
203
- super(name);
204
- const ns = name.split(":")[0];
205
- this.root.push(new NextAttributeComponent({ val: {
206
- key: `${ns}:val`,
207
- value: hpsMeasureValue(val)
208
- } }));
209
- }
210
- };
211
- /**
212
- * XML element representing an empty element (CT_Empty).
213
- */
214
- var EmptyElement = class extends XmlComponent {};
215
- /**
216
- * XML element with a string value attribute (CT_String).
217
- * @deprecated Use `stringValObj()` for hot-path code.
218
- */
219
- var StringValueElement = class extends XmlComponent {
220
- constructor(name, val) {
221
- super(name);
222
- const ns = name.split(":")[0];
223
- this.root.push(new NextAttributeComponent({ val: {
224
- key: `${ns}:val`,
225
- value: val
226
- } }));
227
- }
228
- };
229
- /**
230
- * XML element with a numeric value attribute.
231
- * @deprecated Use `numberValObj()` for hot-path code.
232
- */
233
- var NumberValueElement = class extends XmlComponent {
234
- constructor(name, val) {
235
- super(name);
236
- const ns = name.split(":")[0];
237
- this.root.push(new NextAttributeComponent({ val: {
238
- key: `${ns}:val`,
239
- value: val
240
- } }));
241
- }
242
- };
243
- /**
244
- * XML element with a string enum value attribute.
245
- * @deprecated Use `stringEnumValObj()` for hot-path code.
246
- */
247
- var StringEnumValueElement = class extends XmlComponent {
248
- constructor(name, val) {
249
- super(name);
250
- const ns = name.split(":")[0];
251
- this.root.push(new NextAttributeComponent({ val: {
252
- key: `${ns}:val`,
253
- value: val
254
- } }));
255
- }
256
- };
257
- /**
258
- * XML element containing text content.
259
- * @deprecated Use `stringContainerObj()` for hot-path code.
260
- */
261
- var StringContainer = class extends XmlComponent {
262
- constructor(name, val) {
263
- super(name);
264
- this.root.push(val);
265
- }
266
- };
267
- /**
268
- * Flexible XML element builder with explicit attribute and child configuration.
269
- */
270
- var BuilderElement = class extends XmlComponent {
271
- constructor({ name, attributes, children }) {
272
- super(name);
273
- if (attributes) this.root.push(new NextAttributeComponent(attributes));
274
- if (children) this.root.push(...children);
275
- }
276
- };
277
- /**
278
- * Creates a NextAttributeComponent with explicit XML attribute keys.
279
- */
280
- const chartAttr = (attrs) => new NextAttributeComponent(Object.fromEntries(Object.entries(attrs).map(([key, value]) => [key, {
281
- key,
282
- value
283
- }])));
284
- /**
285
- * Wraps a component in a named XmlComponent element.
286
- */
287
- function wrapEl(elementName, child) {
288
- const el = new class extends XmlComponent {
289
- constructor(name) {
290
- super(name);
291
- }
292
- }(elementName);
293
- el["root"].push(child);
294
- return el;
295
- }
296
- //#endregion
297
- //#region src/xml-components/imported.ts
298
- /**
299
- * Imported XML Component module for handling external XML content.
300
- *
301
- * @module
302
- */
303
- /**
304
- * Converts an xml-js Element into an XmlComponent tree.
305
- */
306
- const convertToXmlComponent = (element) => {
307
- switch (element.type) {
308
- case void 0:
309
- case "element": {
310
- const xmlComponent = new ImportedXmlComponent(element.name, element.attributes);
311
- const childElements = element.elements || [];
312
- for (const childElm of childElements) {
313
- const child = convertToXmlComponent(childElm);
314
- if (child !== void 0) xmlComponent.push(child);
315
- }
316
- return xmlComponent;
317
- }
318
- case "text": return element.text;
319
- default: return;
320
- }
321
- };
322
- /**
323
- * Internal attribute component for imported XML elements.
324
- * @internal
325
- */
326
- var ImportedXmlComponentAttributes = class extends XmlAttributeComponent {};
327
- /**
328
- * XML component representing imported XML content.
329
- */
330
- var ImportedXmlComponent = class extends XmlComponent {
331
- _sourceXml;
332
- static fromXmlString(importedContent) {
333
- const xmlObj = xml2js(importedContent, { compact: false });
334
- const component = convertToXmlComponent(xmlObj.elements?.[0] ?? xmlObj);
335
- component._sourceXml = importedContent;
336
- return component;
337
- }
338
- get sourceXml() {
339
- return this._sourceXml;
340
- }
341
- toXml(_context) {
342
- return this._sourceXml ?? super.toXml(_context);
343
- }
344
- constructor(rootKey, _attr) {
345
- super(rootKey);
346
- if (_attr) this.root.push(new ImportedXmlComponentAttributes(_attr));
347
- }
348
- push(xmlComponent) {
349
- this.root.push(xmlComponent);
350
- }
351
- };
352
- /**
353
- * Represents attributes for imported root elements.
354
- */
355
- var ImportedRootElementAttributes = class extends XmlComponent {
356
- constructor(_attr) {
357
- super("");
358
- this._attr = _attr;
359
- }
360
- prepForXml(_) {
361
- return { _attr: this._attr };
362
- }
363
- };
364
- //#endregion
365
- //#region src/xml-components/initializable.ts
366
- /**
367
- * Initializable XML Component module.
368
- *
369
- * @module
370
- */
371
- /**
372
- * XML component that can be initialized from another component.
373
- */
374
- var InitializableXmlComponent = class extends XmlComponent {
375
- constructor(rootKey, initComponent) {
376
- super(rootKey);
377
- if (initComponent instanceof XmlComponent) this.root = initComponent.root;
378
- }
379
- };
380
- //#endregion
381
- export { EMPTY_OBJECT as C, BaseXmlComponent as E, XmlAttributeComponent as S, XmlComponent as T, stringContainerObj as _, BuilderElement as a, wrapEl as b, NumberValueElement as c, StringEnumValueElement as d, StringValueElement as f, onOffObj as g, numberValObj as h, convertToXmlComponent as i, OnOffElement as l, hpsMeasureObj as m, ImportedRootElementAttributes as n, EmptyElement as o, chartAttr as p, ImportedXmlComponent as r, HpsMeasureElement as s, InitializableXmlComponent as t, StringContainer as u, stringEnumValObj as v, IgnoreIfEmptyXmlComponent as w, NextAttributeComponent as x, stringValObj as y };