en16931-parser 0.0.0-stage → 1.0.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.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * xml-entities.ts — Pure XML text-entity encode/decode.
3
+ *
4
+ * Shared by any tool that hand-parses or hand-writes small XML text nodes
5
+ * (OOXML property parts, spreadsheet cell values…) without pulling in a full
6
+ * XML/DOM parser. Only the five predefined entities plus numeric character
7
+ * references are handled — these text nodes never carry markup.
8
+ */
9
+ /** Decodes `& < > " '` and numeric char refs in a single pass. */
10
+ export declare function decodeXmlEntities(s: string): string;
11
+ /** Encodes the five characters that are unsafe inside XML text/attributes. */
12
+ export declare function encodeXmlEntities(s: string): string;
13
+ /** Escapes `&`, `<`, and `>` for WebVTT cue text without mangling apostrophes/quotes. */
14
+ export declare function escapeVttCueText(s: string): string;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * xml-entities.ts — Pure XML text-entity encode/decode.
3
+ *
4
+ * Shared by any tool that hand-parses or hand-writes small XML text nodes
5
+ * (OOXML property parts, spreadsheet cell values…) without pulling in a full
6
+ * XML/DOM parser. Only the five predefined entities plus numeric character
7
+ * references are handled — these text nodes never carry markup.
8
+ */
9
+ const NAMED_XML_ENTITIES = {
10
+ lt: '<',
11
+ gt: '>',
12
+ quot: '"',
13
+ apos: "'",
14
+ amp: '&',
15
+ };
16
+ /**
17
+ * Keeps numeric character references that are zero, UTF-16 surrogates
18
+ * (U+D800..U+DFFF), or above U+10FFFF verbatim instead of throwing or
19
+ * emitting invalid XML / lone surrogate code units.
20
+ */
21
+ function codePointOrKeep(raw, code) {
22
+ if (code <= 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) {
23
+ return raw;
24
+ }
25
+ return String.fromCodePoint(code);
26
+ }
27
+ function resolveXmlEntityMatch(raw, hex, dec, named) {
28
+ if (hex !== undefined)
29
+ return codePointOrKeep(raw, parseInt(hex, 16));
30
+ if (dec !== undefined)
31
+ return codePointOrKeep(raw, parseInt(dec, 10));
32
+ return NAMED_XML_ENTITIES[named];
33
+ }
34
+ /** Decodes `&amp; &lt; &gt; &quot; &apos;` and numeric char refs in a single pass. */
35
+ export function decodeXmlEntities(s) {
36
+ return s.replace(/&(?:#x([0-9a-fA-F]+)|#(\d+)|(lt|gt|quot|apos|amp));/g, (raw, hex, dec, named) => resolveXmlEntityMatch(raw, hex, dec, named));
37
+ }
38
+ /** Encodes the five characters that are unsafe inside XML text/attributes. */
39
+ export function encodeXmlEntities(s) {
40
+ return s
41
+ .replace(/&/g, '&amp;')
42
+ .replace(/</g, '&lt;')
43
+ .replace(/>/g, '&gt;')
44
+ .replace(/"/g, '&quot;')
45
+ .replace(/'/g, '&apos;');
46
+ }
47
+ /** Escapes `&`, `<`, and `>` for WebVTT cue text without mangling apostrophes/quotes. */
48
+ export function escapeVttCueText(s) {
49
+ return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
50
+ }
@@ -0,0 +1,190 @@
1
+ /**
2
+ * xml-formatter-options.ts — Pure TypeScript XML 1.0 Lexer, Validator,
3
+ * Pretty-Printer, Minifier, Tree Builder, Summary Analyzer, and XPath Query
4
+ * Evaluator for `/xml-formatter` (`/fr/formateur-validateur-xml`).
5
+ *
6
+ * Reuses `src/core/xml-entities.ts` (strict DRY) and runs 100% in Node/Vitest
7
+ * with zero DOM or network dependencies.
8
+ */
9
+ export type XmlMode = 'format' | 'minify' | 'validate' | 'tree' | 'xpath';
10
+ export type XmlIndent = '2' | '4' | 'tab';
11
+ /** Maximum XML document size processed in browser memory (10 MB). */
12
+ export declare const XML_MAX_BYTES: number;
13
+ export interface XmlFormatOptions {
14
+ readonly indent?: XmlIndent;
15
+ readonly sortAttributes?: boolean;
16
+ readonly stripComments?: boolean;
17
+ readonly wrapAttributes?: boolean;
18
+ readonly preserveDeclaration?: boolean;
19
+ }
20
+ export interface XmlAttribute {
21
+ readonly name: string;
22
+ readonly value: string;
23
+ readonly quote: '"' | "'";
24
+ }
25
+ export type XmlErrorCode = 'EMPTY_DOCUMENT' | 'UNCLOSED_COMMENT' | 'UNCLOSED_CDATA' | 'UNCLOSED_PI' | 'UNCLOSED_DOCTYPE' | 'UNCLOSED_TAG' | 'INVALID_TAG_NAME' | 'DUPLICATE_ATTRIBUTE' | 'UNQUOTED_ATTRIBUTE' | 'UNCLOSED_ATTRIBUTE' | 'UNESCAPED_AMPERSAND' | 'MISMATCHED_CLOSING_TAG' | 'UNEXPECTED_CLOSING_TAG' | 'UNCLOSED_ELEMENT' | 'MULTIPLE_ROOT_ELEMENTS' | 'NO_ROOT_ELEMENT';
26
+ export interface XmlValidationError {
27
+ readonly code: XmlErrorCode;
28
+ readonly line: number;
29
+ readonly column: number;
30
+ readonly snippet: string;
31
+ readonly reason: string;
32
+ }
33
+ export type XmlToken = {
34
+ readonly type: 'declaration';
35
+ readonly raw: string;
36
+ readonly offset: number;
37
+ } | {
38
+ readonly type: 'pi';
39
+ readonly raw: string;
40
+ readonly offset: number;
41
+ } | {
42
+ readonly type: 'comment';
43
+ readonly raw: string;
44
+ readonly offset: number;
45
+ } | {
46
+ readonly type: 'cdata';
47
+ readonly raw: string;
48
+ readonly content: string;
49
+ readonly offset: number;
50
+ } | {
51
+ readonly type: 'doctype';
52
+ readonly raw: string;
53
+ readonly offset: number;
54
+ } | {
55
+ readonly type: 'openTag';
56
+ readonly name: string;
57
+ readonly attributes: readonly XmlAttribute[];
58
+ readonly selfClosing: boolean;
59
+ readonly offset: number;
60
+ } | {
61
+ readonly type: 'closeTag';
62
+ readonly name: string;
63
+ readonly offset: number;
64
+ } | {
65
+ readonly type: 'text';
66
+ readonly raw: string;
67
+ readonly offset: number;
68
+ };
69
+ export interface XmlNamespaceEntry {
70
+ readonly prefix: string;
71
+ readonly uri: string;
72
+ }
73
+ export interface XmlTreeSummary {
74
+ readonly maxDepth: number;
75
+ readonly elementCount: number;
76
+ readonly attributeCount: number;
77
+ readonly textNodeCount: number;
78
+ readonly commentCount: number;
79
+ readonly cdataCount: number;
80
+ readonly namespaces: readonly XmlNamespaceEntry[];
81
+ }
82
+ export interface XmlTreeNode {
83
+ readonly id: string;
84
+ readonly tag: string;
85
+ readonly xpath: string;
86
+ readonly depth: number;
87
+ readonly attributes: readonly XmlAttribute[];
88
+ readonly text: string;
89
+ readonly cdata: readonly string[];
90
+ readonly children: readonly XmlTreeNode[];
91
+ }
92
+ export type XmlValidationResult = {
93
+ readonly valid: true;
94
+ readonly rootTag: string;
95
+ readonly summary: XmlTreeSummary;
96
+ readonly root: XmlTreeNode;
97
+ } | {
98
+ readonly valid: false;
99
+ readonly error: XmlValidationError;
100
+ };
101
+ export type XmlFormatResult = {
102
+ readonly ok: true;
103
+ readonly output: string;
104
+ readonly summary: XmlTreeSummary;
105
+ } | {
106
+ readonly ok: false;
107
+ readonly error: XmlValidationError;
108
+ };
109
+ export interface XPathMatchItem {
110
+ readonly xpath: string;
111
+ readonly nodeType: 'element' | 'attribute' | 'scalar';
112
+ readonly label: string;
113
+ readonly value: string;
114
+ }
115
+ export declare const DEFAULT_SAMPLE_XPATH = "//item[@currency='EUR']/price";
116
+ export declare const SAMPLE_XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- B2B Purchase Order & Electronic Invoice Manifest -->\n<inv:orderCatalog xmlns:inv=\"urn:sunasty:invoice:1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.4\" region=\"EU-WEST\">\n <inv:header id=\"ORD-2026-0842\" priority=\"high\" status=\"confirmed\" channel=\"edi\">\n <inv:buyer vat=\"FR84501928374\">Atelier Numerique SAS</inv:buyer>\n <inv:issuedAt>2026-11-07T09:30:00Z</inv:issuedAt>\n <inv:notes><![CDATA[Deliver to Gate B <North Dock> & require signature on arrival.]]></inv:notes>\n </inv:header>\n <inv:items>\n <item id=\"SKU-101\" currency=\"EUR\" taxRate=\"0.20\" warehouse=\"PAR-01\">\n <name>Workstation NVMe Array 4TB</name>\n <quantity>2</quantity>\n <price>489.00</price>\n </item>\n <item id=\"SKU-204\" currency=\"EUR\" taxRate=\"0.20\" warehouse=\"PAR-01\">\n <name>Optical Fiber Patch 10GbE &amp; Transceiver</name>\n <quantity>6</quantity>\n <price>74.50</price>\n </item>\n <item id=\"SKU-309\" currency=\"USD\" taxRate=\"0.00\" warehouse=\"NYC-03\">\n <name>Hardware Security Key FIPS-140</name>\n <quantity>10</quantity>\n <price>55.00</price>\n </item>\n </inv:items>\n</inv:orderCatalog>";
117
+ /**
118
+ * Converts a 0-based character offset in `raw` into 1-based `{ line, column, snippet }`.
119
+ */
120
+ export declare function computeLineColumnSnippet(raw: string, offset: number): {
121
+ readonly line: number;
122
+ readonly column: number;
123
+ readonly snippet: string;
124
+ };
125
+ /**
126
+ * Finds the first unescaped `&` in an XML text node or attribute value.
127
+ * Returns the relative character offset or `-1` if all `&` belong to valid entities.
128
+ */
129
+ export declare function findUnescapedAmpersandOffset(text: string): number;
130
+ /**
131
+ * Tokenizes an XML 1.0 document into a flat stream of `XmlToken` items,
132
+ * returning the first lexical error with exact `line` and `column` if malformed.
133
+ */
134
+ export declare function tokenizeXml(raw: string): {
135
+ readonly tokens: XmlToken[];
136
+ readonly error?: XmlValidationError;
137
+ };
138
+ /**
139
+ * Validates XML 1.0 well-formedness, builds the `XmlTreeNode` hierarchy, and
140
+ * computes `XmlTreeSummary` statistics.
141
+ */
142
+ export declare function validateXml(raw: string): XmlValidationResult;
143
+ /**
144
+ * Builds the `XmlTreeNode` hierarchy and summary for a valid XML document.
145
+ */
146
+ export declare function buildXmlTree(raw: string): {
147
+ readonly ok: true;
148
+ readonly root: XmlTreeNode;
149
+ readonly summary: XmlTreeSummary;
150
+ } | {
151
+ readonly ok: false;
152
+ readonly error: XmlValidationError;
153
+ };
154
+ /**
155
+ * Computes structural summary metrics (`maxDepth`, `elementCount`, `attributeCount`,
156
+ * `textNodeCount`, `commentCount`, `cdataCount`, `namespaces`) for a valid XML document,
157
+ * or returns `null` if the XML is malformed.
158
+ */
159
+ export declare function computeXmlSummary(raw: string): XmlTreeSummary | null;
160
+ /**
161
+ * Pretty-prints an XML document with configurable indentation, attribute sorting,
162
+ * comment stripping, attribute line-wrapping, and declaration preservation.
163
+ */
164
+ export declare function formatXml(raw: string, options?: XmlFormatOptions): XmlFormatResult;
165
+ /**
166
+ * Minifies an XML document onto a single compact line by removing insignificant
167
+ * inter-tag whitespace (and optionally stripping comments).
168
+ */
169
+ export declare function minifyXml(raw: string, options?: Pick<XmlFormatOptions, 'stripComments' | 'preserveDeclaration' | 'sortAttributes'>): XmlFormatResult;
170
+ /**
171
+ * Evaluates a lightweight XPath 1.0 expression against an `XmlTreeNode` hierarchy
172
+ * (supporting `/a/b`, `//tag`, predicates `[@attr='val']`, `[1]`, `[price>100]`,
173
+ * attribute axes `/@attr` and `//@attr`, and `count(...)`).
174
+ */
175
+ export declare function evaluateSimpleXPath(root: XmlTreeNode, expr: string): readonly XPathMatchItem[];
176
+ /**
177
+ * Flattens and filters an `XmlTreeNode` hierarchy by a case-insensitive search query
178
+ * matching tag names, attribute names/values, text content, or CDATA sections.
179
+ */
180
+ export declare function filterXmlTreeNodes(root: XmlTreeNode, query: string): readonly XmlTreeNode[];
181
+ /**
182
+ * Parses URL query parameters (`?mode=...&indent=...&sortAttrs=...&stripComments=...&wrapAttrs=...`).
183
+ */
184
+ export declare function parseXmlUrlParams(search: string): {
185
+ readonly mode: XmlMode | null;
186
+ readonly indent: XmlIndent | null;
187
+ readonly sortAttrs: boolean | null;
188
+ readonly stripComments: boolean | null;
189
+ readonly wrapAttrs: boolean | null;
190
+ };