@pipobscure/xml 0.1.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/LICENSE.md ADDED
@@ -0,0 +1,190 @@
1
+ EUROPEAN UNION PUBLIC LICENCE v. 1.2
2
+ EUPL © Philipp Dunkel 2026
3
+
4
+ This European Union Public Licence (the 'EUPL') applies to the Work (as defined below) which is provided under the
5
+ terms of this Licence. Any use of the Work, other than as authorised under this Licence is prohibited (to the extent such
6
+ use is covered by a right of the copyright holder of the Work).
7
+ The Work is provided under the terms of this Licence when the Licensor (as defined below) has placed the following
8
+ notice immediately following the copyright notice for the Work:
9
+ Licensed under the EUPL
10
+ or has expressed by any other means his willingness to license under the EUPL.
11
+
12
+ 1.Definitions
13
+ In this Licence, the following terms have the following meaning:
14
+ — 'The Licence':this Licence.
15
+ — 'The Original Work':the work or software distributed or communicated by the Licensor under this Licence, available
16
+ as Source Code and also as Executable Code as the case may be.
17
+ — 'Derivative Works':the works or software that could be created by the Licensee, based upon the Original Work or
18
+ modifications thereof. This Licence does not define the extent of modification or dependence on the Original Work
19
+ required in order to classify a work as a Derivative Work; this extent is determined by copyright law applicable in
20
+ the country mentioned in Article 15.
21
+ — 'The Work':the Original Work or its Derivative Works.
22
+ — 'The Source Code':the human-readable form of the Work which is the most convenient for people to study and
23
+ modify.
24
+ — 'The Executable Code':any code which has generally been compiled and which is meant to be interpreted by
25
+ a computer as a program.
26
+ — 'The Licensor':the natural or legal person that distributes or communicates the Work under the Licence.
27
+ — 'Contributor(s)':any natural or legal person who modifies the Work under the Licence, or otherwise contributes to
28
+ the creation of a Derivative Work.
29
+ — 'The Licensee' or 'You':any natural or legal person who makes any usage of the Work under the terms of the
30
+ Licence.
31
+ — 'Distribution' or 'Communication':any act of selling, giving, lending, renting, distributing, communicating,
32
+ transmitting, or otherwise making available, online or offline, copies of the Work or providing access to its essential
33
+ functionalities at the disposal of any other natural or legal person.
34
+
35
+ 2.Scope of the rights granted by the Licence
36
+ The Licensor hereby grants You a worldwide, royalty-free, non-exclusive, sublicensable licence to do the following, for
37
+ the duration of copyright vested in the Original Work:
38
+ — use the Work in any circumstance and for all usage,
39
+ — reproduce the Work,
40
+ — modify the Work, and make Derivative Works based upon the Work,
41
+ — communicate to the public, including the right to make available or display the Work or copies thereof to the public
42
+ and perform publicly, as the case may be, the Work,
43
+ — distribute the Work or copies thereof,
44
+ — lend and rent the Work or copies thereof,
45
+ — sublicense rights in the Work or copies thereof.
46
+ Those rights can be exercised on any media, supports and formats, whether now known or later invented, as far as the
47
+ applicable law permits so.
48
+ In the countries where moral rights apply, the Licensor waives his right to exercise his moral right to the extent allowed
49
+ by law in order to make effective the licence of the economic rights here above listed.
50
+ The Licensor grants to the Licensee royalty-free, non-exclusive usage rights to any patents held by the Licensor, to the
51
+ extent necessary to make use of the rights granted on the Work under this Licence.
52
+
53
+ 3.Communication of the Source Code
54
+ The Licensor may provide the Work either in its Source Code form, or as Executable Code. If the Work is provided as
55
+ Executable Code, the Licensor provides in addition a machine-readable copy of the Source Code of the Work along with
56
+ each copy of the Work that the Licensor distributes or indicates, in a notice following the copyright notice attached to
57
+ the Work, a repository where the Source Code is easily and freely accessible for as long as the Licensor continues to
58
+ distribute or communicate the Work.
59
+
60
+ 4.Limitations on copyright
61
+ Nothing in this Licence is intended to deprive the Licensee of the benefits from any exception or limitation to the
62
+ exclusive rights of the rights owners in the Work, of the exhaustion of those rights or of other applicable limitations
63
+ thereto.
64
+
65
+ 5.Obligations of the Licensee
66
+ The grant of the rights mentioned above is subject to some restrictions and obligations imposed on the Licensee. Those
67
+ obligations are the following:
68
+
69
+ Attribution right: The Licensee shall keep intact all copyright, patent or trademarks notices and all notices that refer to
70
+ the Licence and to the disclaimer of warranties. The Licensee must include a copy of such notices and a copy of the
71
+ Licence with every copy of the Work he/she distributes or communicates. The Licensee must cause any Derivative Work
72
+ to carry prominent notices stating that the Work has been modified and the date of modification.
73
+
74
+ Copyleft clause: If the Licensee distributes or communicates copies of the Original Works or Derivative Works, this
75
+ Distribution or Communication will be done under the terms of this Licence or of a later version of this Licence unless
76
+ the Original Work is expressly distributed only under this version of the Licence — for example by communicating
77
+ 'EUPL v. 1.2 only'. The Licensee (becoming Licensor) cannot offer or impose any additional terms or conditions on the
78
+ Work or Derivative Work that alter or restrict the terms of the Licence.
79
+
80
+ Compatibility clause: If the Licensee Distributes or Communicates Derivative Works or copies thereof based upon both
81
+ the Work and another work licensed under a Compatible Licence, this Distribution or Communication can be done
82
+ under the terms of this Compatible Licence. For the sake of this clause, 'Compatible Licence' refers to the licences listed
83
+ in the appendix attached to this Licence. Should the Licensee's obligations under the Compatible Licence conflict with
84
+ his/her obligations under this Licence, the obligations of the Compatible Licence shall prevail.
85
+
86
+ Provision of Source Code: When distributing or communicating copies of the Work, the Licensee will provide
87
+ a machine-readable copy of the Source Code or indicate a repository where this Source will be easily and freely available
88
+ for as long as the Licensee continues to distribute or communicate the Work.
89
+ Legal Protection: This Licence does not grant permission to use the trade names, trademarks, service marks, or names
90
+ of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and
91
+ reproducing the content of the copyright notice.
92
+
93
+ 6.Chain of Authorship
94
+ The original Licensor warrants that the copyright in the Original Work granted hereunder is owned by him/her or
95
+ licensed to him/her and that he/she has the power and authority to grant the Licence.
96
+ Each Contributor warrants that the copyright in the modifications he/she brings to the Work are owned by him/her or
97
+ licensed to him/her and that he/she has the power and authority to grant the Licence.
98
+ Each time You accept the Licence, the original Licensor and subsequent Contributors grant You a licence to their contributions
99
+ to the Work, under the terms of this Licence.
100
+
101
+ 7.Disclaimer of Warranty
102
+ The Work is a work in progress, which is continuously improved by numerous Contributors. It is not a finished work
103
+ and may therefore contain defects or 'bugs' inherent to this type of development.
104
+ For the above reason, the Work is provided under the Licence on an 'as is' basis and without warranties of any kind
105
+ concerning the Work, including without limitation merchantability, fitness for a particular purpose, absence of defects or
106
+ errors, accuracy, non-infringement of intellectual property rights other than copyright as stated in Article 6 of this
107
+ Licence.
108
+ This disclaimer of warranty is an essential part of the Licence and a condition for the grant of any rights to the Work.
109
+
110
+ 8.Disclaimer of Liability
111
+ Except in the cases of wilful misconduct or damages directly caused to natural persons, the Licensor will in no event be
112
+ liable for any direct or indirect, material or moral, damages of any kind, arising out of the Licence or of the use of the
113
+ Work, including without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, loss
114
+ of data or any commercial damage, even if the Licensor has been advised of the possibility of such damage. However,
115
+ the Licensor will be liable under statutory product liability laws as far such laws apply to the Work.
116
+
117
+ 9.Additional agreements
118
+ While distributing the Work, You may choose to conclude an additional agreement, defining obligations or services
119
+ consistent with this Licence. However, if accepting obligations, You may act only on your own behalf and on your sole
120
+ responsibility, not on behalf of the original Licensor or any other Contributor, and only if You agree to indemnify,
121
+ defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against such Contributor by
122
+ the fact You have accepted any warranty or additional liability.
123
+
124
+ 10.Acceptance of the Licence
125
+ The provisions of this Licence can be accepted by clicking on an icon 'I agree' placed under the bottom of a window
126
+ displaying the text of this Licence or by affirming consent in any other similar way, in accordance with the rules of
127
+ applicable law. Clicking on that icon indicates your clear and irrevocable acceptance of this Licence and all of its terms
128
+ and conditions.
129
+ Similarly, you irrevocably accept this Licence and all of its terms and conditions by exercising any rights granted to You
130
+ by Article 2 of this Licence, such as the use of the Work, the creation by You of a Derivative Work or the Distribution
131
+ or Communication by You of the Work or copies thereof.
132
+
133
+ 11.Information to the public
134
+ In case of any Distribution or Communication of the Work by means of electronic communication by You (for example,
135
+ by offering to download the Work from a remote location) the distribution channel or media (for example, a website)
136
+ must at least provide to the public the information requested by the applicable law regarding the Licensor, the Licence
137
+ and the way it may be accessible, concluded, stored and reproduced by the Licensee.
138
+
139
+ 12.Termination of the Licence
140
+ The Licence and the rights granted hereunder will terminate automatically upon any breach by the Licensee of the terms
141
+ of the Licence.
142
+ Such a termination will not terminate the licences of any person who has received the Work from the Licensee under
143
+ the Licence, provided such persons remain in full compliance with the Licence.
144
+
145
+ 13.Miscellaneous
146
+ Without prejudice of Article 9 above, the Licence represents the complete agreement between the Parties as to the
147
+ Work.
148
+ If any provision of the Licence is invalid or unenforceable under applicable law, this will not affect the validity or
149
+ enforceability of the Licence as a whole. Such provision will be construed or reformed so as necessary to make it valid
150
+ and enforceable.
151
+ The European Commission may publish other linguistic versions or new versions of this Licence or updated versions of
152
+ the Appendix, so far this is required and reasonable, without reducing the scope of the rights granted by the Licence.
153
+ New versions of the Licence will be published with a unique version number.
154
+ All linguistic versions of this Licence, approved by the European Commission, have identical value. Parties can take
155
+ advantage of the linguistic version of their choice.
156
+
157
+ 14.Jurisdiction
158
+ Without prejudice to specific agreement between parties,
159
+ — any litigation resulting from the interpretation of this License, arising between the European Union institutions,
160
+ bodies, offices or agencies, as a Licensor, and any Licensee, will be subject to the jurisdiction of the Court of Justice
161
+ of the European Union, as laid down in article 272 of the Treaty on the Functioning of the European Union,
162
+ — any litigation arising between other parties and resulting from the interpretation of this License, will be subject to
163
+ the exclusive jurisdiction of the competent court where the Licensor resides or conducts its primary business.
164
+
165
+ 15.Applicable Law
166
+ Without prejudice to specific agreement between parties,
167
+ — this Licence shall be governed by the law of the European Union Member State where the Licensor has his seat,
168
+ resides or has his registered office,
169
+ — this licence shall be governed by Belgian law if the Licensor has no seat, residence or registered office inside
170
+ a European Union Member State.
171
+
172
+
173
+ Appendix
174
+
175
+ 'Compatible Licences' according to Article 5 EUPL are:
176
+ — GNU General Public License (GPL) v. 2, v. 3
177
+ — GNU Affero General Public License (AGPL) v. 3
178
+ — Open Software License (OSL) v. 2.1, v. 3.0
179
+ — Eclipse Public License (EPL) v. 1.0
180
+ — CeCILL v. 2.0, v. 2.1
181
+ — Mozilla Public Licence (MPL) v. 2
182
+ — GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
183
+ — Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for works other than software
184
+ — European Union Public Licence (EUPL) v. 1.1, v. 1.2
185
+ — Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong Reciprocity (LiLiQ-R+).
186
+
187
+ The European Commission may update this Appendix to later versions of the above licences without producing
188
+ a new version of the EUPL, as long as they provide the rights granted in Article 2 of this Licence and protect the
189
+ covered Source Code from exclusive appropriation.
190
+ All other changes or additions to this Appendix require the production of a new EUPL version.
package/README.md ADDED
@@ -0,0 +1,396 @@
1
+ # @pipobscure/xml
2
+
3
+ A fully capable, forgiving XML parser for TypeScript and JavaScript. Produces plain JS objects that are JSON-serialisable, well-typed, and straightforward to traverse.
4
+
5
+ Designed for CalDAV, CardDAV, WebDAV, Atom, and similar document-oriented XML workloads where documents are small, real-world servers are quirky, and zero dependencies are preferred.
6
+
7
+ ## Features
8
+
9
+ - Full namespace support (prefix resolution, default namespace, `xmlns=""` undeclaration)
10
+ - All XML node types: elements, text, CDATA, comments, processing instructions, DOCTYPE, XML declaration
11
+ - Tolerant parsing — recovers gracefully from many real-world quirks instead of throwing
12
+ - Plain-object output — every node is a simple JS object, safe to `JSON.stringify` and `JSON.parse`
13
+ - Discriminated union type hierarchy — narrow any node to its concrete type with `instanceof`-free type guards
14
+ - Tree query helpers — a small functional API for locating elements and reading text
15
+ - Serializer — converts any node back to an XML string
16
+ - Zero dependencies, pure TypeScript, ESM
17
+
18
+ ## Installation
19
+
20
+ ```sh
21
+ npm install @pipobscure/xml
22
+ ```
23
+
24
+ ## Quick start
25
+
26
+ ```ts
27
+ import { parse, rootElement, child, attr, textContent, serialize } from '@pipobscure/xml';
28
+
29
+ const doc = parse(`<?xml version="1.0" encoding="utf-8"?>
30
+ <D:propfind xmlns:D="DAV:">
31
+ <D:prop>
32
+ <D:displayname/>
33
+ <D:getcontenttype/>
34
+ </D:prop>
35
+ </D:propfind>`);
36
+
37
+ const root = rootElement(doc); // Element — <D:propfind>
38
+ const prop = child(root, 'prop', 'DAV:'); // Element — <D:prop>
39
+ console.log(prop?.children.length); // 2
40
+
41
+ // Serialize back to XML
42
+ const xml = serialize(doc);
43
+ ```
44
+
45
+ ---
46
+
47
+ ## API reference
48
+
49
+ ### `parse(xml: string): Document`
50
+
51
+ Parses an XML string and returns a `Document` node. Never throws for malformed input — the parser is deliberately tolerant (see [Tolerance](#tolerance) below). It does throw `ParseError` for hard structural failures such as a completely empty input.
52
+
53
+ ```ts
54
+ import { parse } from '@pipobscure/xml';
55
+
56
+ const doc = parse('<root attr="hello">world</root>');
57
+ // doc.type === 'document'
58
+ // doc.children[0].type === 'element'
59
+ ```
60
+
61
+ ### `class ParseError extends Error`
62
+
63
+ Thrown by `parse()` only for unrecoverable failures. Carries three extra properties:
64
+
65
+ | Property | Type | Description |
66
+ |---|---|---|
67
+ | `position` | `number` | Byte offset in the source string |
68
+ | `line` | `number` | 1-based line number |
69
+ | `column` | `number` | 1-based column number |
70
+
71
+ ---
72
+
73
+ ## Node types
74
+
75
+ Every node has a `type` discriminant. All nodes are plain JS objects with readonly properties.
76
+
77
+ ### `Document`
78
+
79
+ The root container returned by `parse()`.
80
+
81
+ ```ts
82
+ interface Document {
83
+ readonly type: 'document';
84
+ readonly children: ReadonlyArray<DocumentChild>;
85
+ }
86
+ ```
87
+
88
+ `DocumentChild` is the union `XmlDeclaration | DocumentType | Element | Comment | ProcessingInstruction`.
89
+
90
+ ### `Element`
91
+
92
+ ```ts
93
+ interface Element {
94
+ readonly type: 'element';
95
+ readonly name: string; // local name
96
+ readonly prefix: string | null; // namespace prefix, or null
97
+ readonly namespace: string | null; // resolved namespace URI, or null
98
+ readonly attributes: ReadonlyArray<Attribute>;
99
+ readonly children: ReadonlyArray<ChildNode>;
100
+ }
101
+ ```
102
+
103
+ `ChildNode` is the union `Element | Text | CData | Comment | ProcessingInstruction`.
104
+
105
+ ### `Attribute`
106
+
107
+ ```ts
108
+ interface Attribute {
109
+ readonly name: string; // local name
110
+ readonly prefix: string | null; // namespace prefix, or null
111
+ readonly namespace: string | null; // resolved namespace URI, or null
112
+ readonly value: string; // decoded value
113
+ }
114
+ ```
115
+
116
+ Namespace-declaration attributes (`xmlns`, `xmlns:prefix`) are included in the `attributes` array with their namespace resolved to `http://www.w3.org/2000/xmlns/`. Unprefixed attributes carry `namespace: null` — they do not inherit the element's default namespace, per the XML Namespaces specification.
117
+
118
+ ### `Text`
119
+
120
+ ```ts
121
+ interface Text {
122
+ readonly type: 'text';
123
+ readonly value: string; // decoded character content
124
+ }
125
+ ```
126
+
127
+ ### `CData`
128
+
129
+ ```ts
130
+ interface CData {
131
+ readonly type: 'cdata';
132
+ readonly value: string; // raw CDATA content (between <![CDATA[ and ]]>)
133
+ }
134
+ ```
135
+
136
+ ### `Comment`
137
+
138
+ ```ts
139
+ interface Comment {
140
+ readonly type: 'comment';
141
+ readonly value: string; // text between <!-- and -->
142
+ }
143
+ ```
144
+
145
+ ### `ProcessingInstruction`
146
+
147
+ ```ts
148
+ interface ProcessingInstruction {
149
+ readonly type: 'processing-instruction';
150
+ readonly target: string; // PI target
151
+ readonly data: string; // everything after the target, leading whitespace stripped
152
+ }
153
+ ```
154
+
155
+ ### `XmlDeclaration`
156
+
157
+ ```ts
158
+ interface XmlDeclaration {
159
+ readonly type: 'xml-declaration';
160
+ readonly version: string;
161
+ readonly encoding: string | null;
162
+ readonly standalone: boolean | null;
163
+ }
164
+ ```
165
+
166
+ ### `DocumentType`
167
+
168
+ ```ts
169
+ interface DocumentType {
170
+ readonly type: 'doctype';
171
+ readonly name: string;
172
+ readonly publicId: string | null;
173
+ readonly systemId: string | null;
174
+ readonly internalSubset: string | null; // verbatim, unparsed
175
+ }
176
+ ```
177
+
178
+ ### Union aliases
179
+
180
+ | Alias | Members |
181
+ |---|---|
182
+ | `ChildNode` | `Element \| Text \| CData \| Comment \| ProcessingInstruction` |
183
+ | `DocumentChild` | `XmlDeclaration \| DocumentType \| Element \| Comment \| ProcessingInstruction` |
184
+ | `AnyNode` | All eight concrete types |
185
+
186
+ ---
187
+
188
+ ## Type guards
189
+
190
+ Each node type has a corresponding type guard that doubles as a discriminating predicate:
191
+
192
+ ```ts
193
+ import {
194
+ isDocument, isElement, isText, isCData,
195
+ isComment, isProcessingInstruction, isDocumentType, isXmlDeclaration,
196
+ } from '@pipobscure/xml';
197
+
198
+ for (const node of doc.children) {
199
+ if (isElement(node)) {
200
+ console.log(node.name, node.namespace);
201
+ }
202
+ }
203
+ ```
204
+
205
+ All guards have the signature `(node: Node) => node is T`.
206
+
207
+ ---
208
+
209
+ ## Tree query helpers
210
+
211
+ A small set of functions for navigating the document tree. All helpers are **tolerant**: they accept `null` and `undefined` and return the neutral value (`undefined`, `[]`, `""`, `0`) rather than throwing.
212
+
213
+ ### `rootElement(doc)`
214
+
215
+ ```ts
216
+ rootElement(doc: Document | null | undefined): Element | undefined
217
+ ```
218
+
219
+ Returns the first element child of a `Document`, or `undefined`.
220
+
221
+ ### `child(el, name, ns?)`
222
+
223
+ ```ts
224
+ child(el: Element | null | undefined, name: string, ns?: string): Element | undefined
225
+ ```
226
+
227
+ Returns the first direct child element with the given local name and (optionally) namespace URI.
228
+
229
+ ### `requireChild(el, name, ns?)`
230
+
231
+ ```ts
232
+ requireChild(el: Element | null | undefined, name: string, ns?: string): Element
233
+ ```
234
+
235
+ Like `child`, but throws a descriptive `Error` when the element is not found. Useful for strict processing where a missing child is a hard failure.
236
+
237
+ ### `children(el, name, ns?)`
238
+
239
+ ```ts
240
+ children(el: Element | null | undefined, name: string, ns?: string): Element[]
241
+ ```
242
+
243
+ Returns all direct child elements with the given local name and optional namespace URI.
244
+
245
+ ### `childElements(el)`
246
+
247
+ ```ts
248
+ childElements(el: Element | null | undefined): Element[]
249
+ ```
250
+
251
+ Returns all direct child elements regardless of name or namespace.
252
+
253
+ ### `childElementCount(el)`
254
+
255
+ ```ts
256
+ childElementCount(el: Element | null | undefined): number
257
+ ```
258
+
259
+ Returns the number of direct child elements.
260
+
261
+ ### `descendant(node, name, ns?)`
262
+
263
+ ```ts
264
+ descendant(
265
+ node: Document | Element | null | undefined,
266
+ name: string,
267
+ ns?: string,
268
+ ): Element | undefined
269
+ ```
270
+
271
+ Returns the first element anywhere in the subtree with the given local name and optional namespace URI. Depth-first, pre-order.
272
+
273
+ ### `descendants(node, name, ns?)`
274
+
275
+ ```ts
276
+ descendants(
277
+ node: Document | Element | null | undefined,
278
+ name: string,
279
+ ns?: string,
280
+ ): Element[]
281
+ ```
282
+
283
+ Returns all elements anywhere in the subtree with the given local name and optional namespace URI.
284
+
285
+ ### `textContent(node)`
286
+
287
+ ```ts
288
+ textContent(node: AnyNode | null | undefined): string
289
+ ```
290
+
291
+ Concatenates all `Text` and `CData` content in the subtree, equivalent to the DOM's `element.textContent`. Returns `""` for node types that carry no text (comments, PIs, etc.).
292
+
293
+ ### `attr(el, name, ns?)`
294
+
295
+ ```ts
296
+ attr(el: Element | null | undefined, name: string, ns?: string): string | undefined
297
+ ```
298
+
299
+ Returns the value of the attribute with the given local name and optional namespace URI, or `undefined` if not found.
300
+
301
+ ### Example — CalDAV response
302
+
303
+ ```ts
304
+ import { parse, rootElement, children, child, textContent, attr } from '@pipobscure/xml';
305
+
306
+ const doc = parse(calDavMultistatusXml);
307
+ const root = rootElement(doc);
308
+
309
+ for (const response of children(root, 'response', 'DAV:')) {
310
+ const href = textContent(child(response, 'href', 'DAV:'));
311
+ const status = textContent(child(response, 'status', 'DAV:'));
312
+ console.log(href, status);
313
+ }
314
+ ```
315
+
316
+ ---
317
+
318
+ ## Serializer
319
+
320
+ ### `serialize(node)`
321
+
322
+ ```ts
323
+ serialize(node: AnyNode | null | undefined): string
324
+ ```
325
+
326
+ Converts any node (or a complete `Document`) back to an XML string.
327
+
328
+ | Input type | Output |
329
+ |---|---|
330
+ | `Document` | All children concatenated |
331
+ | `Element` | `<tag attrs>…</tag>`, or `<tag attrs/>` when childless |
332
+ | `Text` | Character-escaped text (`&`, `<`, `>` → entities) |
333
+ | `CData` | `<![CDATA[…]]>`, splitting on embedded `]]>` |
334
+ | `Comment` | `<!--…-->` |
335
+ | `ProcessingInstruction` | `<?target data?>` |
336
+ | `XmlDeclaration` | `<?xml version="…" …?>` |
337
+ | `DocumentType` | `<!DOCTYPE …>` |
338
+ | `null` / `undefined` | `""` |
339
+
340
+ The serializer is tolerant of the same kind of incomplete objects as the query helpers — missing `children`, `attributes`, or `prefix` properties are treated as empty/absent rather than causing a throw.
341
+
342
+ **Round-trip guarantee.** For any valid XML document, `parse(serialize(parse(xml)))` produces a tree that is deeply equal to `parse(xml)`. Character content is re-escaped on serialize and re-decoded on re-parse, so the tree comparison holds even when the source used unescaped `>` or non-canonical entity forms.
343
+
344
+ **CDATA with `]]>`.** The sequence `]]>` inside a CDATA value is split across two adjacent CDATA sections so that the output remains well-formed. The text content is preserved exactly; only the tree structure changes (one `CData` node becomes two).
345
+
346
+ ```ts
347
+ import { parse, serialize } from '@pipobscure/xml';
348
+
349
+ const doc = parse('<root><![CDATA[<em>bold</em>]]></root>');
350
+ const xml = serialize(doc);
351
+ // '<root><![CDATA[<em>bold</em>]]></root>'
352
+
353
+ const doc2 = parse(xml);
354
+ // deepEqual(doc2, doc) === true
355
+ ```
356
+
357
+ ---
358
+
359
+ ## Tolerance
360
+
361
+ The parser is designed to handle the kind of non-conformant XML that real-world CalDAV and CardDAV servers emit. Specifically:
362
+
363
+ | Quirk | Behaviour |
364
+ |---|---|
365
+ | UTF-8 BOM | Silently skipped |
366
+ | No XML declaration | Parsed without error |
367
+ | Unknown named entities (`&nbsp;`) | Preserved verbatim (`&nbsp;`) |
368
+ | Bare `&` with no entity name | Emitted as `&` literally |
369
+ | Undeclared namespace prefix | Resolved to `null`; no throw |
370
+ | `--` inside a comment | Allowed |
371
+ | Attribute values with either quote style | Accepted |
372
+ | DOCTYPE internal subset | Captured verbatim, not validated |
373
+ | Mismatched closing tag | Tolerated |
374
+
375
+ The query helpers and serializer apply the same philosophy: missing or wrong-typed properties on nodes (e.g. a JSON-deserialised object missing its `children` array) are treated as absent or empty, never as fatal errors.
376
+
377
+ ---
378
+
379
+ ## JSON serialisation
380
+
381
+ All parse output is composed of plain objects and primitive values — no class instances, no `undefined` values, no circular references. A document tree can be safely round-tripped through `JSON.stringify` / `JSON.parse`:
382
+
383
+ ```ts
384
+ const doc = parse(xml);
385
+ const json = JSON.stringify(doc);
386
+ const doc2 = JSON.parse(json);
387
+
388
+ // Query helpers work on the revived object exactly as on the original:
389
+ const root = rootElement(doc2);
390
+ ```
391
+
392
+ ---
393
+
394
+ ## License
395
+
396
+ [EUPL-1.2](LICENSE) — European Union Public Licence v. 1.2
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @pipobscure/xml — XML character classification
3
+ *
4
+ * All functions operate on numeric Unicode code points (from `charCodeAt`)
5
+ * for maximum performance on the ASCII-heavy content typical of
6
+ * CalDAV / CardDAV payloads.
7
+ *
8
+ * The ranges follow XML 1.0 (fifth edition) §2.2 and the XML Namespaces 1.0
9
+ * specification. The parser itself is tolerant and will not reject documents
10
+ * solely on character-class grounds, but these helpers are used to drive the
11
+ * tokeniser for correct name recognition.
12
+ */
13
+ /** XML whitespace: space, tab, carriage-return, newline. */
14
+ export declare function isXmlWhitespace(code: number): boolean;
15
+ /**
16
+ * Valid XML NameStartChar (includes `:` so that QNames can be lexed as a
17
+ * single token and split on `:` afterwards).
18
+ *
19
+ * XML 1.0 §2.3 production [4]
20
+ */
21
+ export declare function isNameStartChar(code: number): boolean;
22
+ /**
23
+ * Valid XML NameChar (superset of NameStartChar).
24
+ *
25
+ * XML 1.0 §2.3 production [4a]
26
+ */
27
+ export declare function isNameChar(code: number): boolean;
28
+ /** ASCII hex digit [0-9A-Fa-f]. */
29
+ export declare function isHexDigit(code: number): boolean;
30
+ /** ASCII decimal digit [0-9]. */
31
+ export declare function isDecimalDigit(code: number): boolean;
32
+ //# sourceMappingURL=chars.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chars.d.ts","sourceRoot":"","sources":["../src/chars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,4DAA4D;AAC5D,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CA4BrD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAQhD;AAED,mCAAmC;AACnC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAMhD;AAED,iCAAiC;AACjC,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD"}