@office-open/core 0.9.3 → 0.9.5

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,282 +0,0 @@
1
- import { children, escapeXml, findChild, textOf } from "@office-open/xml";
2
- //#region src/descriptor/builder.ts
3
- function element$1(tag) {
4
- return new DescriptorBuilder(tag);
5
- }
6
- var DescriptorBuilder = class {
7
- _tag;
8
- _attrs = [];
9
- _content = [];
10
- constructor(tag) {
11
- this._tag = tag;
12
- }
13
- /** Add an attribute mapping. */
14
- attr(key, xmlName, opts) {
15
- this._attrs.push({
16
- kind: "child",
17
- key,
18
- xmlName,
19
- ...opts
20
- });
21
- return this;
22
- }
23
- /** Add a single child element mapping. */
24
- child(key, tag, desc) {
25
- this._content.push({
26
- kind: "child",
27
- key,
28
- tag,
29
- desc
30
- });
31
- return this;
32
- }
33
- /** Add a repeating child element mapping. */
34
- children(key, tag, desc) {
35
- this._content.push({
36
- kind: "children",
37
- key,
38
- tag,
39
- desc
40
- });
41
- return this;
42
- }
43
- /** Add a union (one-of-several) child mapping. */
44
- union(key, variants) {
45
- this._content.push({
46
- kind: "union",
47
- key,
48
- variants
49
- });
50
- return this;
51
- }
52
- /** Add a text content mapping. */
53
- text(key) {
54
- this._content.push({
55
- kind: "text",
56
- key
57
- });
58
- return this;
59
- }
60
- /** Add a custom content handler. */
61
- custom(spec) {
62
- this._content.push(spec);
63
- return this;
64
- }
65
- /** Build the immutable ElementDescriptor. */
66
- build() {
67
- const result = {
68
- kind: "element",
69
- tag: this._tag
70
- };
71
- if (this._attrs.length) result.attrs = Object.freeze(this._attrs);
72
- if (this._content.length) result.content = Object.freeze(this._content);
73
- return Object.freeze(result);
74
- }
75
- };
76
- //#endregion
77
- //#region src/descriptor/runtime.ts
78
- /**
79
- * Descriptor runtime: stringify (write) and parse (parse path) functions.
80
- *
81
- * Write path: Options → stringify(desc, opts, ctx) → string
82
- * Parse path: Element → parse(desc, el, ctx) → Partial<Options>
83
- *
84
- * No intermediate representation — each path is a single step.
85
- *
86
- * @module
87
- */
88
- /**
89
- * Serialize an Options object to an XML string using its descriptor.
90
- * Returns `undefined` when an optional element should be omitted.
91
- */
92
- function stringify(desc, value, ctx) {
93
- if (desc.kind === "custom") return desc.stringify(value, ctx);
94
- return stringifyElement(desc, value, ctx);
95
- }
96
- function stringifyElement(desc, value, ctx) {
97
- const tag = desc.tag;
98
- const attrStr = stringifyAttrs(desc.attrs, value);
99
- let hasContent = false;
100
- const parts = [];
101
- if (desc.content) for (let i = 0; i < desc.content.length; i++) {
102
- const spec = desc.content[i];
103
- const s = stringifyContentSpec(spec, value, ctx);
104
- if (s !== void 0) {
105
- hasContent = true;
106
- parts.push(s);
107
- }
108
- }
109
- if (!hasContent) {
110
- if (!attrStr) return void 0;
111
- return `<${tag}${attrStr}/>`;
112
- }
113
- parts.unshift(`<${tag}${attrStr}>`);
114
- parts.push(`</${tag}>`);
115
- return parts.join("");
116
- }
117
- function stringifyAttrs(attrs, value) {
118
- if (!attrs) return "";
119
- const parts = [];
120
- for (let i = 0; i < attrs.length; i++) {
121
- const spec = attrs[i];
122
- const raw = value[spec.key];
123
- if (raw === void 0) continue;
124
- if (spec.default !== void 0 && raw === spec.default) continue;
125
- const encoded = spec.encode ? spec.encode(raw) : typeof raw === "string" || typeof raw === "number" || typeof raw === "boolean" ? String(raw) : String(raw);
126
- if (encoded === void 0) continue;
127
- parts.push(`${spec.xmlName}="${escapeXml(encoded)}"`);
128
- }
129
- return parts.length ? " " + parts.join(" ") : "";
130
- }
131
- function stringifyContentSpec(spec, value, ctx) {
132
- switch (spec.kind) {
133
- case "child": return stringifyChild(spec, value, ctx);
134
- case "children": return stringifyChildren(spec, value, ctx);
135
- case "union": return stringifyUnion(spec, value, ctx);
136
- case "text": return stringifyText(spec, value);
137
- case "custom": return stringifyCustom(spec, value, ctx);
138
- }
139
- }
140
- function stringifyChild(spec, value, ctx) {
141
- const childValue = value[spec.key];
142
- if (childValue === void 0 || childValue === null) return void 0;
143
- return stringify(spec.desc, childValue, ctx);
144
- }
145
- function stringifyChildren(spec, value, ctx) {
146
- const items = value[spec.key];
147
- if (!items || items.length === 0) return void 0;
148
- const parts = [];
149
- for (let i = 0; i < items.length; i++) {
150
- const s = stringify(spec.desc, items[i], ctx);
151
- if (s !== void 0) parts.push(s);
152
- }
153
- return parts.length ? parts.join("") : void 0;
154
- }
155
- function stringifyUnion(spec, value, ctx) {
156
- const childValue = value[spec.key];
157
- if (childValue === void 0 || childValue === null) return void 0;
158
- const variants = spec.variants;
159
- for (let i = 0; i < variants.length; i++) {
160
- const v = variants[i];
161
- if (v.match(childValue)) return stringify(v.desc, childValue, ctx);
162
- }
163
- }
164
- function stringifyText(spec, value) {
165
- const text = value[spec.key];
166
- if (text === void 0 || text === null) return void 0;
167
- return escapeXml(typeof text === "string" ? text : String(text));
168
- }
169
- function stringifyCustom(spec, value, ctx) {
170
- return spec.stringify(value, ctx);
171
- }
172
- /**
173
- * Parse an XML Element into an Options object using its descriptor.
174
- */
175
- function parse(desc, el, ctx) {
176
- if (desc.kind === "custom") return desc.parse(el, ctx);
177
- return parseElement(desc, el, ctx);
178
- }
179
- function parseElement(desc, el, ctx) {
180
- const result = {};
181
- if (desc.attrs && el.attributes) for (let i = 0; i < desc.attrs.length; i++) {
182
- const spec = desc.attrs[i];
183
- const raw = el.attributes[spec.xmlName];
184
- if (raw !== void 0) result[spec.key] = spec.decode ? spec.decode(String(raw)) : raw;
185
- }
186
- if (desc.content) for (let i = 0; i < desc.content.length; i++) {
187
- const spec = desc.content[i];
188
- parseContentSpec(spec, el, ctx, result);
189
- }
190
- return result;
191
- }
192
- function parseContentSpec(spec, el, ctx, result) {
193
- switch (spec.kind) {
194
- case "child": {
195
- const child = findChild(el, spec.tag);
196
- if (child) result[spec.key] = parse(spec.desc, child, ctx);
197
- break;
198
- }
199
- case "children": {
200
- const items = children(el, spec.tag);
201
- if (items.length) result[spec.key] = items.map((c) => parse(spec.desc, c, ctx));
202
- break;
203
- }
204
- case "union":
205
- for (let i = 0; i < spec.variants.length; i++) {
206
- const v = spec.variants[i];
207
- const child = findChild(el, v.tag);
208
- if (child) {
209
- result[spec.key] = parse(v.desc, child, ctx);
210
- break;
211
- }
212
- }
213
- break;
214
- case "text": {
215
- const text = textOf(el);
216
- if (text) result[spec.key] = text;
217
- break;
218
- }
219
- case "custom":
220
- Object.assign(result, spec.parse(el, ctx));
221
- break;
222
- }
223
- }
224
- //#endregion
225
- //#region src/descriptor/helpers.ts
226
- /**
227
- * OOXML-specific encode/decode helpers for descriptors.
228
- *
229
- * XML traversal helpers (findChild, etc.) are in @office-open/xml utils.
230
- * This file only contains OOXML value encoding/decoding.
231
- *
232
- * @module
233
- */
234
- /** Encode boolean for CT_OnOff: true → omit val, false → "0". */
235
- const boolEncode = (v) => {
236
- if (v === void 0) return void 0;
237
- return v ? void 0 : "0";
238
- };
239
- /** Decode CT_OnOff: absent or "true"/"1" → true, "0"/"false" → false. */
240
- const boolDecode = (raw) => raw !== "0" && raw !== "false";
241
- /** Create an enum encoder from a JS↔XML mapping. */
242
- const enumEncode = (map) => (v) => {
243
- if (v === void 0) return void 0;
244
- return map[v] ?? v;
245
- };
246
- /** Create an enum decoder from a JS↔XML mapping (inverted). */
247
- const enumDecode = (map) => {
248
- const inv = invertRecord(map);
249
- return (raw) => inv[raw] ?? raw;
250
- };
251
- function invertRecord(map) {
252
- const result = {};
253
- for (const key of Object.keys(map)) result[map[key]] = key;
254
- return result;
255
- }
256
- //#endregion
257
- //#region src/descriptor/registry.ts
258
- var DescriptorRegistry = class DescriptorRegistry {
259
- static _map = /* @__PURE__ */ new Map();
260
- /** Register a descriptor with its XML tag. */
261
- static register(tag, desc) {
262
- DescriptorRegistry._map.set(tag, desc);
263
- }
264
- /** Look up a descriptor by XML tag. */
265
- static get(tag) {
266
- return DescriptorRegistry._map.get(tag);
267
- }
268
- /** Get all registered tags. */
269
- static tags() {
270
- return new Set(DescriptorRegistry._map.keys());
271
- }
272
- /** Check if a tag is registered. */
273
- static has(tag) {
274
- return DescriptorRegistry._map.has(tag);
275
- }
276
- /** Get the number of registered descriptors. */
277
- static get size() {
278
- return DescriptorRegistry._map.size;
279
- }
280
- };
281
- //#endregion
282
- export { enumEncode as a, DescriptorBuilder as c, enumDecode as i, element$1 as l, boolDecode as n, parse as o, boolEncode as r, stringify as s, DescriptorRegistry as t };
@@ -1,153 +0,0 @@
1
- import { Element } from "@office-open/xml";
2
-
3
- //#region src/descriptor/context.d.ts
4
- /** Context passed during stringify (write path). */
5
- interface WriteContext {
6
- /** Register a relationship and return its rId. */
7
- addRelationship(type: string, target: string, mode?: string): string;
8
- /** Add a media file and return its reference. */
9
- addMedia(data: Uint8Array, type: string): string;
10
- }
11
- /** Context passed during parse (parse path). */
12
- interface ReadContext {
13
- /** Resolve a relationship rId to its target path. */
14
- resolveRelationship(rId: string): string | undefined;
15
- /** Get a parsed XML part by path. */
16
- getPart(path: string): Element | undefined;
17
- /** Get raw binary data (images, media, etc.) by path. */
18
- getRaw(path: string): Uint8Array | undefined;
19
- }
20
- //#endregion
21
- //#region src/descriptor/types.d.ts
22
- /** Element descriptor — declarative XML mapping. */
23
- interface ElementDescriptor<T> {
24
- readonly kind: "element";
25
- readonly tag: string;
26
- readonly attrs?: readonly AttrSpec<T>[];
27
- readonly content?: readonly ContentSpec<T>[];
28
- }
29
- /** Custom descriptor — for complex logic that doesn't fit the declarative model. */
30
- interface CustomDescriptor<T, Ctx = WriteContext> {
31
- readonly kind: "custom";
32
- stringify(value: T, ctx: Ctx): string | undefined;
33
- parse(el: Element, ctx: ReadContext): T;
34
- }
35
- /** Union type for all descriptors. */
36
- type Descriptor<T> = ElementDescriptor<T> | CustomDescriptor<T>;
37
- interface AttrSpec<T> {
38
- /** Property key in the Options object. */
39
- readonly key: keyof T & string;
40
- /** XML attribute name (e.g. "w:val"). */
41
- readonly xmlName: string;
42
- /** Default value — omitted during stringify when equal. */
43
- readonly default?: unknown;
44
- /** Encode a JS value to an XML attribute string. Return undefined to skip. */
45
- readonly encode?: (v: any) => string | undefined;
46
- /** Decode an XML attribute string to a JS value. */
47
- readonly decode?: (raw: string) => any;
48
- }
49
- /** Single child element mapped to a property. */
50
- interface ChildSpec<T> {
51
- readonly kind: "child";
52
- readonly key: keyof T & string;
53
- readonly tag: string;
54
- readonly desc: Descriptor<any>;
55
- }
56
- /** Multiple child elements of the same tag mapped to an array property. */
57
- interface ChildrenSpec<T> {
58
- readonly kind: "children";
59
- readonly key: keyof T & string;
60
- readonly tag: string;
61
- readonly desc: Descriptor<any>;
62
- }
63
- /** Union child — one of several possible child elements. */
64
- interface UnionSpec<T> {
65
- readonly kind: "union";
66
- readonly key: keyof T & string;
67
- readonly variants: readonly UnionVariant[];
68
- }
69
- /** Text content mapped to a property. */
70
- interface TextSpec<T> {
71
- readonly kind: "text";
72
- readonly key: keyof T & string;
73
- }
74
- interface UnionVariant {
75
- readonly tag: string;
76
- readonly match: (opts: any) => boolean;
77
- readonly desc: Descriptor<any>;
78
- }
79
- /** Discriminated union of all content spec types. */
80
- type ContentSpec<T> = ChildSpec<T> | ChildrenSpec<T> | UnionSpec<T> | TextSpec<T> | CustomDescriptor<T>;
81
- //#endregion
82
- //#region src/descriptor/builder.d.ts
83
- declare function element$1<T extends object>(tag: string): DescriptorBuilder<T>;
84
- declare class DescriptorBuilder<T extends object> {
85
- private readonly _tag;
86
- private readonly _attrs;
87
- private readonly _content;
88
- constructor(tag: string);
89
- /** Add an attribute mapping. */
90
- attr(key: keyof T & string, xmlName: string, opts?: {
91
- readonly default?: unknown;
92
- readonly encode?: (v: any) => string | undefined;
93
- readonly decode?: (raw: string) => any;
94
- }): this;
95
- /** Add a single child element mapping. */
96
- child(key: keyof T & string, tag: string, desc: ChildSpec<T>["desc"]): this;
97
- /** Add a repeating child element mapping. */
98
- children(key: keyof T & string, tag: string, desc: ChildrenSpec<T>["desc"]): this;
99
- /** Add a union (one-of-several) child mapping. */
100
- union(key: keyof T & string, variants: readonly UnionVariant[]): this;
101
- /** Add a text content mapping. */
102
- text(key: keyof T & string): this;
103
- /** Add a custom content handler. */
104
- custom(spec: CustomDescriptor<T>): this;
105
- /** Build the immutable ElementDescriptor. */
106
- build(): ElementDescriptor<T>;
107
- }
108
- //#endregion
109
- //#region src/descriptor/runtime.d.ts
110
- /**
111
- * Serialize an Options object to an XML string using its descriptor.
112
- * Returns `undefined` when an optional element should be omitted.
113
- */
114
- declare function stringify<T>(desc: Descriptor<T>, value: T, ctx: WriteContext): string | undefined;
115
- /**
116
- * Parse an XML Element into an Options object using its descriptor.
117
- */
118
- declare function parse<T>(desc: Descriptor<T>, el: Element, ctx: ReadContext): T;
119
- //#endregion
120
- //#region src/descriptor/helpers.d.ts
121
- /**
122
- * OOXML-specific encode/decode helpers for descriptors.
123
- *
124
- * XML traversal helpers (findChild, etc.) are in @office-open/xml utils.
125
- * This file only contains OOXML value encoding/decoding.
126
- *
127
- * @module
128
- */
129
- /** Encode boolean for CT_OnOff: true → omit val, false → "0". */
130
- declare const boolEncode: (v: boolean | undefined) => string | undefined;
131
- /** Decode CT_OnOff: absent or "true"/"1" → true, "0"/"false" → false. */
132
- declare const boolDecode: (raw: string) => boolean;
133
- /** Create an enum encoder from a JS↔XML mapping. */
134
- declare const enumEncode: (map: Record<string, string>) => (v: string | undefined) => string | undefined;
135
- /** Create an enum decoder from a JS↔XML mapping (inverted). */
136
- declare const enumDecode: (map: Record<string, string>) => (raw: string) => string;
137
- //#endregion
138
- //#region src/descriptor/registry.d.ts
139
- declare class DescriptorRegistry {
140
- private static readonly _map;
141
- /** Register a descriptor with its XML tag. */
142
- static register(tag: string, desc: Descriptor<any>): void;
143
- /** Look up a descriptor by XML tag. */
144
- static get(tag: string): Descriptor<any> | undefined;
145
- /** Get all registered tags. */
146
- static tags(): ReadonlySet<string>;
147
- /** Check if a tag is registered. */
148
- static has(tag: string): boolean;
149
- /** Get the number of registered descriptors. */
150
- static get size(): number;
151
- }
152
- //#endregion
153
- export { TextSpec as _, enumEncode as a, ReadContext as b, DescriptorBuilder as c, ChildSpec as d, ChildrenSpec as f, ElementDescriptor as g, Descriptor as h, enumDecode as i, element$1 as l, CustomDescriptor as m, boolDecode as n, parse as o, ContentSpec as p, boolEncode as r, stringify as s, DescriptorRegistry as t, AttrSpec as u, UnionSpec as v, WriteContext as x, UnionVariant as y };