@ai-sdk-tool/parser 3.2.0 → 3.3.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,2216 @@
1
+ import {
2
+ coerceBySchema,
3
+ getSchemaType,
4
+ unwrapJsonSchema
5
+ } from "./chunk-DZB6Y354.js";
6
+
7
+ // src/rxml/errors/types.ts
8
+ var RXMLParseError = class extends Error {
9
+ constructor(message, cause, line, column) {
10
+ super(message);
11
+ this.name = "RXMLParseError";
12
+ this.cause = cause;
13
+ this.line = line;
14
+ this.column = column;
15
+ }
16
+ };
17
+ var RXMLDuplicateStringTagError = class extends Error {
18
+ constructor(message) {
19
+ super(message);
20
+ this.name = "RXMLDuplicateStringTagError";
21
+ }
22
+ };
23
+ var RXMLCoercionError = class extends Error {
24
+ constructor(message, cause) {
25
+ super(message);
26
+ this.name = "RXMLCoercionError";
27
+ this.cause = cause;
28
+ }
29
+ };
30
+ var RXMLStringifyError = class extends Error {
31
+ constructor(message, cause) {
32
+ super(message);
33
+ this.name = "RXMLStringifyError";
34
+ this.cause = cause;
35
+ }
36
+ };
37
+
38
+ // src/rxml/core/types.ts
39
+ var CharCodes = {
40
+ OPEN_BRACKET: "<".charCodeAt(0),
41
+ CLOSE_BRACKET: ">".charCodeAt(0),
42
+ MINUS: "-".charCodeAt(0),
43
+ SLASH: "/".charCodeAt(0),
44
+ EXCLAMATION: "!".charCodeAt(0),
45
+ QUESTION: "?".charCodeAt(0),
46
+ SINGLE_QUOTE: "'".charCodeAt(0),
47
+ DOUBLE_QUOTE: '"'.charCodeAt(0),
48
+ OPEN_CORNER_BRACKET: "[".charCodeAt(0),
49
+ CLOSE_CORNER_BRACKET: "]".charCodeAt(0),
50
+ SPACE: " ".charCodeAt(0),
51
+ TAB: " ".charCodeAt(0),
52
+ NEWLINE: "\n".charCodeAt(0),
53
+ CARRIAGE_RETURN: "\r".charCodeAt(0)
54
+ };
55
+ var DEFAULT_NO_CHILD_NODES = [
56
+ "img",
57
+ "br",
58
+ "input",
59
+ "meta",
60
+ "link",
61
+ "hr",
62
+ "area",
63
+ "base",
64
+ "col",
65
+ "embed",
66
+ "param",
67
+ "source",
68
+ "track",
69
+ "wbr"
70
+ ];
71
+ var NAME_SPACER = "\r\n >/= ";
72
+
73
+ // src/rxml/utils/helpers.ts
74
+ var NAME_START_CHAR_REGEX = /[A-Za-z_:]/;
75
+ var NAME_CHAR_REGEX = /[A-Za-z0-9_.:-]/;
76
+ function isNameStartChar(ch) {
77
+ return NAME_START_CHAR_REGEX.test(ch);
78
+ }
79
+ function isNameChar(ch) {
80
+ return NAME_CHAR_REGEX.test(ch);
81
+ }
82
+ function skipQuoted(s, i) {
83
+ const quote = s[i];
84
+ let pos = i + 1;
85
+ while (pos < s.length) {
86
+ const ch = s[pos];
87
+ if (ch === "\\") {
88
+ pos += 2;
89
+ continue;
90
+ }
91
+ if (ch === quote) {
92
+ return pos + 1;
93
+ }
94
+ pos += 1;
95
+ }
96
+ return pos;
97
+ }
98
+ function parseName(s, pos) {
99
+ const start = pos;
100
+ let currentPos = pos;
101
+ while (NAME_SPACER.indexOf(s[currentPos]) === -1 && s[currentPos]) {
102
+ currentPos += 1;
103
+ }
104
+ return { name: s.slice(start, currentPos), newPos: currentPos };
105
+ }
106
+ function parseString(s, pos) {
107
+ const startChar = s[pos];
108
+ const startPos = pos + 1;
109
+ const endPos = s.indexOf(startChar, startPos);
110
+ if (endPos === -1) {
111
+ const tagEnd = s.indexOf(">", startPos);
112
+ if (tagEnd !== -1) {
113
+ return { value: s.slice(startPos, tagEnd), newPos: tagEnd };
114
+ }
115
+ return { value: s.slice(startPos), newPos: s.length };
116
+ }
117
+ return { value: s.slice(startPos, endPos), newPos: endPos + 1 };
118
+ }
119
+ function getLineColumn(s, pos) {
120
+ let line = 1;
121
+ let column = 1;
122
+ for (let i = 0; i < pos && i < s.length; i += 1) {
123
+ if (s[i] === "\n") {
124
+ line += 1;
125
+ column = 1;
126
+ } else {
127
+ column += 1;
128
+ }
129
+ }
130
+ return { line, column };
131
+ }
132
+ function escapeXml(text) {
133
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
134
+ }
135
+ function escapeXmlMinimalText(text) {
136
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/]]>/g, "]]&gt;");
137
+ }
138
+ function escapeXmlMinimalAttr(value, wrapper = '"') {
139
+ let escaped = value.replace(/&/g, "&amp;").replace(/</g, "&lt;");
140
+ if (wrapper === '"') {
141
+ escaped = escaped.replace(/"/g, "&quot;");
142
+ } else {
143
+ escaped = escaped.replace(/'/g, "&apos;");
144
+ }
145
+ return escaped;
146
+ }
147
+ function unescapeXml(text) {
148
+ return text.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&amp;/g, "&");
149
+ }
150
+
151
+ // src/rxml/builders/stringify.ts
152
+ function stringify(rootTag, obj, options = {}) {
153
+ var _a, _b, _c, _d, _e;
154
+ try {
155
+ const format = (_a = options.format) != null ? _a : true;
156
+ const declaration = (_b = options.declaration) != null ? _b : false;
157
+ const minimalEscaping = (_c = options.minimalEscaping) != null ? _c : false;
158
+ const suppressEmptyNode = (_d = options.suppressEmptyNode) != null ? _d : false;
159
+ const strictBooleanAttributes = (_e = options.strictBooleanAttributes) != null ? _e : false;
160
+ let result = "";
161
+ if (declaration) {
162
+ result += '<?xml version="1.0" encoding="UTF-8"?>\n';
163
+ }
164
+ result += stringifyValue(rootTag, obj, {
165
+ depth: 0,
166
+ format,
167
+ suppressEmptyNode,
168
+ minimalEscaping,
169
+ strictBooleanAttributes
170
+ });
171
+ if (result.endsWith("\n")) {
172
+ return result.slice(0, -1);
173
+ }
174
+ return result;
175
+ } catch (error) {
176
+ throw new RXMLStringifyError("Failed to stringify XML", error);
177
+ }
178
+ }
179
+ function escapeContent(content, minimalEscaping) {
180
+ return minimalEscaping ? escapeXmlMinimalText(content) : escapeXml(content);
181
+ }
182
+ function createSelfClosingTag(tagName, indent, newline) {
183
+ return `${indent}<${tagName}/>${newline}`;
184
+ }
185
+ function createTextElement(tagName, content, indent, newline) {
186
+ return `${indent}<${tagName}>${content}</${tagName}>${newline}`;
187
+ }
188
+ function isPrimitive(value) {
189
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
190
+ }
191
+ function stringifyPrimitive(tagName, value, context, format) {
192
+ const { minimalEscaping, suppressEmptyNode } = context;
193
+ const content = escapeContent(String(value), minimalEscaping);
194
+ if (content === "" && suppressEmptyNode) {
195
+ return "";
196
+ }
197
+ return createTextElement(tagName, content, format.indent, format.newline);
198
+ }
199
+ function stringifyArray(tagName, value, context) {
200
+ let result = "";
201
+ for (const item of value) {
202
+ result += stringifyValue(tagName, item, context);
203
+ }
204
+ return result;
205
+ }
206
+ function stringifyValue(tagName, value, context) {
207
+ const { format, suppressEmptyNode, minimalEscaping } = context;
208
+ const indent = format ? " ".repeat(context.depth) : "";
209
+ const newline = format ? "\n" : "";
210
+ if (value === null || value === void 0) {
211
+ if (suppressEmptyNode) {
212
+ return "";
213
+ }
214
+ return createSelfClosingTag(tagName, indent, newline);
215
+ }
216
+ if (isPrimitive(value)) {
217
+ return stringifyPrimitive(tagName, value, context, { indent, newline });
218
+ }
219
+ if (Array.isArray(value)) {
220
+ return stringifyArray(tagName, value, context);
221
+ }
222
+ if (typeof value === "object") {
223
+ return stringifyObject(tagName, value, context);
224
+ }
225
+ const content = escapeContent(String(value), minimalEscaping);
226
+ if (content === "" && suppressEmptyNode) {
227
+ return "";
228
+ }
229
+ return createTextElement(tagName, content, indent, newline);
230
+ }
231
+ function extractObjectParts(obj) {
232
+ const attributes = {};
233
+ const elements = {};
234
+ let textContent;
235
+ for (const [key, value] of Object.entries(obj)) {
236
+ if (key.startsWith("@")) {
237
+ attributes[key.substring(1)] = value;
238
+ } else if (key === "#text" || key === "_text") {
239
+ textContent = String(value);
240
+ } else if (key === "_attributes") {
241
+ if (typeof value === "object" && value !== null) {
242
+ Object.assign(attributes, value);
243
+ }
244
+ } else {
245
+ elements[key] = value;
246
+ }
247
+ }
248
+ return { attributes, elements, textContent };
249
+ }
250
+ function formatAttribute(attrName, attrValue, minimalEscaping, strictBooleanAttributes) {
251
+ if (attrValue === null) {
252
+ return strictBooleanAttributes ? ` ${attrName}="${attrName}"` : ` ${attrName}`;
253
+ }
254
+ const valueStr = String(attrValue);
255
+ if (valueStr.indexOf('"') === -1) {
256
+ const escaped2 = minimalEscaping ? escapeXmlMinimalAttr(valueStr, '"') : escapeXml(valueStr);
257
+ return ` ${attrName}="${escaped2}"`;
258
+ }
259
+ const escaped = minimalEscaping ? escapeXmlMinimalAttr(valueStr, "'") : escapeXml(valueStr);
260
+ return ` ${attrName}='${escaped}'`;
261
+ }
262
+ function buildOpeningTag(tagName, attributes, context) {
263
+ let openTag = `<${tagName}`;
264
+ const { minimalEscaping, strictBooleanAttributes } = context;
265
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
266
+ openTag += formatAttribute(
267
+ attrName,
268
+ attrValue,
269
+ minimalEscaping,
270
+ strictBooleanAttributes
271
+ );
272
+ }
273
+ return openTag;
274
+ }
275
+ function stringifyTextOnlyContent(options) {
276
+ const { tagName, textContent, openTag, format, minimalEscaping } = options;
277
+ const content = escapeContent(textContent, minimalEscaping);
278
+ return `${format.indent}${openTag}${content}</${tagName}>${format.newline}`;
279
+ }
280
+ function stringifyComplexContent(tagName, parts, context, options) {
281
+ const { format, minimalEscaping, depth } = context;
282
+ const { textContent, elements } = parts;
283
+ const hasElements = Object.keys(elements).length > 0;
284
+ let result = `${options.indent}${options.openTag}`;
285
+ if (textContent) {
286
+ const content = escapeContent(textContent, minimalEscaping);
287
+ result += format ? `${options.newline}${options.childIndent}${content}` : content;
288
+ }
289
+ if (hasElements) {
290
+ if (format) {
291
+ result += options.newline;
292
+ }
293
+ for (const [elementName, elementValue] of Object.entries(elements)) {
294
+ result += stringifyValue(elementName, elementValue, {
295
+ ...context,
296
+ depth: depth + 1
297
+ });
298
+ }
299
+ if (format) {
300
+ result += options.indent;
301
+ }
302
+ }
303
+ result += `</${tagName}>${options.newline}`;
304
+ return result;
305
+ }
306
+ function stringifyObject(tagName, obj, context) {
307
+ const { depth, format, suppressEmptyNode } = context;
308
+ const indent = format ? " ".repeat(depth) : "";
309
+ const newline = format ? "\n" : "";
310
+ const childIndent = format ? " ".repeat(depth + 1) : "";
311
+ const parts = extractObjectParts(obj);
312
+ const openTag = buildOpeningTag(tagName, parts.attributes, context);
313
+ const hasElements = Object.keys(parts.elements).length > 0;
314
+ const hasTextContent = parts.textContent !== void 0 && parts.textContent !== "";
315
+ if (!(hasElements || hasTextContent)) {
316
+ if (suppressEmptyNode) {
317
+ return "";
318
+ }
319
+ return `${indent}${openTag}/>${newline}`;
320
+ }
321
+ const fullOpenTag = `${openTag}>`;
322
+ if (!hasElements && hasTextContent && parts.textContent) {
323
+ return stringifyTextOnlyContent({
324
+ tagName,
325
+ textContent: parts.textContent,
326
+ openTag: fullOpenTag,
327
+ format: { indent, newline },
328
+ minimalEscaping: context.minimalEscaping
329
+ });
330
+ }
331
+ return stringifyComplexContent(tagName, parts, context, {
332
+ indent,
333
+ newline,
334
+ childIndent,
335
+ openTag: fullOpenTag
336
+ });
337
+ }
338
+
339
+ // src/rxml/schema/coercion.ts
340
+ function getPropertySchema(toolSchema, key) {
341
+ const unwrapped = unwrapJsonSchema(toolSchema);
342
+ if (!unwrapped || typeof unwrapped !== "object") {
343
+ return;
344
+ }
345
+ const u = unwrapped;
346
+ const props = u.properties;
347
+ if (props && Object.hasOwn(props, key)) {
348
+ return props[key];
349
+ }
350
+ return;
351
+ }
352
+ function getNodeValue(children, schema, tagName, textNodeName) {
353
+ if (children.length === 0) {
354
+ return "";
355
+ }
356
+ if (children.length === 1 && typeof children[0] === "string") {
357
+ return children[0];
358
+ }
359
+ return processComplexContent(
360
+ children,
361
+ getPropertySchema(schema, tagName),
362
+ textNodeName
363
+ );
364
+ }
365
+ function addAttributesToValue(value, attributes, textNodeName) {
366
+ if (Object.keys(attributes).length === 0) {
367
+ return value;
368
+ }
369
+ if (typeof value === "string") {
370
+ const valueResult = { [textNodeName]: value };
371
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
372
+ valueResult[`@_${attrName}`] = attrValue;
373
+ }
374
+ return valueResult;
375
+ }
376
+ if (value && typeof value === "object" && !Array.isArray(value)) {
377
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
378
+ value[`@_${attrName}`] = attrValue;
379
+ }
380
+ }
381
+ return value;
382
+ }
383
+ function addToResult(result, tagName, value) {
384
+ if (result[tagName]) {
385
+ if (!Array.isArray(result[tagName])) {
386
+ result[tagName] = [result[tagName]];
387
+ }
388
+ result[tagName].push(value);
389
+ } else {
390
+ result[tagName] = value;
391
+ }
392
+ }
393
+ function domToObject(nodes, schema, textNodeName = "#text") {
394
+ const result = {};
395
+ for (const node of nodes) {
396
+ if (typeof node === "string") {
397
+ continue;
398
+ }
399
+ const { tagName, children, attributes } = node;
400
+ let value = getNodeValue(children, schema, tagName, textNodeName);
401
+ value = addAttributesToValue(value, attributes, textNodeName);
402
+ addToResult(result, tagName, value);
403
+ }
404
+ return result;
405
+ }
406
+ function processChildElement(child, schema, textNodeName) {
407
+ let childValue;
408
+ if (child.children.length === 0) {
409
+ childValue = "";
410
+ } else if (child.children.length === 1 && typeof child.children[0] === "string") {
411
+ childValue = child.children[0];
412
+ } else {
413
+ childValue = processComplexContent(
414
+ child.children,
415
+ getPropertySchema(schema, child.tagName),
416
+ textNodeName
417
+ );
418
+ }
419
+ return addAttributesToValue(childValue, child.attributes, textNodeName);
420
+ }
421
+ function combineContent(textContent, elements, textNodeName) {
422
+ const hasText = textContent.length > 0;
423
+ const hasElements = Object.keys(elements).length > 0;
424
+ if (hasText && hasElements) {
425
+ return {
426
+ [textNodeName]: textContent.join("").trim(),
427
+ ...elements
428
+ };
429
+ }
430
+ if (hasText) {
431
+ return textContent.join("").trim();
432
+ }
433
+ if (hasElements) {
434
+ return elements;
435
+ }
436
+ return "";
437
+ }
438
+ function processComplexContent(children, schema, textNodeName) {
439
+ const textContent = [];
440
+ const elements = {};
441
+ for (const child of children) {
442
+ if (typeof child === "string") {
443
+ textContent.push(child);
444
+ } else {
445
+ const childValue = processChildElement(child, schema, textNodeName);
446
+ addToResult(elements, child.tagName, childValue);
447
+ }
448
+ }
449
+ return combineContent(textContent, elements, textNodeName);
450
+ }
451
+ function coerceDomBySchema(domObject, schema) {
452
+ try {
453
+ return coerceBySchema(domObject, schema);
454
+ } catch (error) {
455
+ throw new RXMLCoercionError("Failed to coerce DOM object by schema", error);
456
+ }
457
+ }
458
+ function visitObjectProperties(props, collected, visit) {
459
+ for (const [key, propSchema] of Object.entries(props)) {
460
+ const t = getSchemaType(propSchema);
461
+ if (t === "string") {
462
+ collected.add(key);
463
+ } else if (t === "object" || t === "array") {
464
+ visit(propSchema);
465
+ }
466
+ }
467
+ }
468
+ function visitArrayItems(u, visit) {
469
+ const items = u.items;
470
+ if (items) {
471
+ visit(items);
472
+ }
473
+ const prefix = u.prefixItems;
474
+ if (Array.isArray(prefix)) {
475
+ for (const item of prefix) {
476
+ visit(item);
477
+ }
478
+ }
479
+ }
480
+ function getStringTypedProperties(schema) {
481
+ const collected = /* @__PURE__ */ new Set();
482
+ const visit = (s) => {
483
+ const unwrapped = unwrapJsonSchema(s);
484
+ if (!unwrapped || typeof unwrapped !== "object") {
485
+ return;
486
+ }
487
+ const u = unwrapped;
488
+ const type = getSchemaType(unwrapped);
489
+ if (type === "object") {
490
+ const props = u.properties;
491
+ if (props && typeof props === "object") {
492
+ visitObjectProperties(props, collected, visit);
493
+ }
494
+ } else if (type === "array") {
495
+ visitArrayItems(u, visit);
496
+ }
497
+ };
498
+ visit(schema);
499
+ return collected;
500
+ }
501
+ function processArrayContent(value, schema, textNodeName) {
502
+ if (!Array.isArray(value)) {
503
+ return value;
504
+ }
505
+ const schemaType = getSchemaType(schema);
506
+ if (schemaType === "string") {
507
+ return value.map((item) => {
508
+ if (typeof item === "string") {
509
+ return item.trim();
510
+ }
511
+ if (item && typeof item === "object" && textNodeName in item) {
512
+ const textVal = item[textNodeName];
513
+ return typeof textVal === "string" ? textVal.trim() : String(textVal);
514
+ }
515
+ return String(item);
516
+ });
517
+ }
518
+ return value.map((item) => {
519
+ if (typeof item === "string") {
520
+ return item.trim();
521
+ }
522
+ if (item && typeof item === "object" && textNodeName in item) {
523
+ const textVal = item[textNodeName];
524
+ return typeof textVal === "string" ? textVal.trim() : textVal;
525
+ }
526
+ return item;
527
+ });
528
+ }
529
+ function processIndexedTuple(obj, textNodeName) {
530
+ const keys = Object.keys(obj);
531
+ const indices = keys.map((k) => Number.parseInt(k, 10)).sort((a, b) => a - b);
532
+ const isValidTuple = indices[0] === 0 && indices.every((val, idx) => val === idx);
533
+ if (!isValidTuple) {
534
+ return [obj];
535
+ }
536
+ const sortedKeys = keys.sort(
537
+ (a, b) => Number.parseInt(a, 10) - Number.parseInt(b, 10)
538
+ );
539
+ return sortedKeys.map((key) => {
540
+ const item = obj[key];
541
+ if (item && typeof item === "object" && textNodeName in item) {
542
+ const textVal = item[textNodeName];
543
+ return typeof textVal === "string" ? textVal.trim() : textVal;
544
+ }
545
+ return typeof item === "string" ? item.trim() : item;
546
+ });
547
+ }
548
+
549
+ // src/rxml/schema/extraction.ts
550
+ function skipDoctype(xmlContent, i, len) {
551
+ const gt = xmlContent.indexOf(">", i + 1);
552
+ return gt === -1 ? len : gt + 1;
553
+ }
554
+ function skipComment(xmlContent, i, len) {
555
+ const close = xmlContent.indexOf("-->", i + 4);
556
+ return close === -1 ? len : close + 3;
557
+ }
558
+ function skipCdata(xmlContent, i, len) {
559
+ const close = xmlContent.indexOf("]]>", i + 9);
560
+ return close === -1 ? len : close + 3;
561
+ }
562
+ function skipProcessingInstruction(xmlContent, i, len) {
563
+ const close = xmlContent.indexOf("?>", i + 1);
564
+ return close === -1 ? len : close + 2;
565
+ }
566
+ function skipSpecialConstruct(xmlContent, i, len) {
567
+ const ch = xmlContent[i];
568
+ if (ch === "!") {
569
+ if (xmlContent.startsWith("!DOCTYPE", i + 1)) {
570
+ return skipDoctype(xmlContent, i, len);
571
+ }
572
+ if (xmlContent.startsWith("!--", i + 1)) {
573
+ return skipComment(xmlContent, i, len);
574
+ }
575
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
576
+ return skipCdata(xmlContent, i, len);
577
+ }
578
+ const gt = xmlContent.indexOf(">", i + 1);
579
+ return gt === -1 ? len : gt + 1;
580
+ }
581
+ if (ch === "?") {
582
+ return skipProcessingInstruction(xmlContent, i, len);
583
+ }
584
+ return -1;
585
+ }
586
+ function parseTagName(xmlContent, i, len) {
587
+ let j = i;
588
+ if (j < len && isNameStartChar(xmlContent[j])) {
589
+ j += 1;
590
+ while (j < len && isNameChar(xmlContent[j])) {
591
+ j += 1;
592
+ }
593
+ }
594
+ return { name: xmlContent.slice(i, j), pos: j };
595
+ }
596
+ function skipToTagEnd(xmlContent, start, len) {
597
+ let k = start;
598
+ let isSelfClosing = false;
599
+ while (k < len) {
600
+ const c = xmlContent[k];
601
+ if (c === '"' || c === "'") {
602
+ k = skipQuoted(xmlContent, k);
603
+ continue;
604
+ }
605
+ if (c === ">") {
606
+ break;
607
+ }
608
+ if (c === "/" && xmlContent[k + 1] === ">") {
609
+ isSelfClosing = true;
610
+ k += 1;
611
+ break;
612
+ }
613
+ k += 1;
614
+ }
615
+ return { pos: k, isSelfClosing };
616
+ }
617
+ function processClosingTagMatch(options) {
618
+ const { xmlContent, nx, len, tagName, depth, nextLt } = options;
619
+ const tagInfo = parseTagName(xmlContent, nx + 1, len);
620
+ const gt = xmlContent.indexOf(">", tagInfo.pos);
621
+ if (tagInfo.name === tagName) {
622
+ const newDepth = depth - 1;
623
+ if (newDepth === 0) {
624
+ return { newPos: nextLt, newDepth, found: true };
625
+ }
626
+ return { newPos: gt === -1 ? len : gt + 1, newDepth, found: false };
627
+ }
628
+ return { newPos: gt === -1 ? len : gt + 1, newDepth: depth, found: false };
629
+ }
630
+ function processOpeningTagMatch(options) {
631
+ const { xmlContent, nx, len, tagName, depth } = options;
632
+ const tagInfo = parseTagName(xmlContent, nx, len);
633
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
634
+ const newDepth = tagInfo.name === tagName && !tagEndInfo.isSelfClosing ? depth + 1 : depth;
635
+ const newPos = xmlContent[tagEndInfo.pos] === ">" ? tagEndInfo.pos + 1 : tagEndInfo.pos + 1;
636
+ return { newPos, newDepth };
637
+ }
638
+ function findMatchingCloseTag(xmlContent, startPos, tagName, len) {
639
+ let pos = startPos;
640
+ let depth = 1;
641
+ while (pos < len) {
642
+ const nextLt = xmlContent.indexOf("<", pos);
643
+ if (nextLt === -1 || nextLt + 1 >= len) {
644
+ break;
645
+ }
646
+ const nx = nextLt + 1;
647
+ const h = xmlContent[nx];
648
+ const specialPos = skipSpecialConstruct(xmlContent, nx, len);
649
+ if (specialPos !== -1) {
650
+ pos = specialPos;
651
+ continue;
652
+ }
653
+ if (h === "/") {
654
+ const result = processClosingTagMatch({
655
+ xmlContent,
656
+ nx,
657
+ len,
658
+ tagName,
659
+ depth,
660
+ nextLt
661
+ });
662
+ if (result.found) {
663
+ return result.newPos;
664
+ }
665
+ pos = result.newPos;
666
+ depth = result.newDepth;
667
+ } else {
668
+ const result = processOpeningTagMatch({
669
+ xmlContent,
670
+ nx,
671
+ len,
672
+ tagName,
673
+ depth
674
+ });
675
+ pos = result.newPos;
676
+ depth = result.newDepth;
677
+ }
678
+ }
679
+ return -1;
680
+ }
681
+ function updateBestMatch(depth, bestDepth, contentStart, contentEnd) {
682
+ if (depth < bestDepth) {
683
+ return { start: contentStart, end: contentEnd, depth };
684
+ }
685
+ return null;
686
+ }
687
+ function processTargetTag(options) {
688
+ const { xmlContent, tagEnd, isSelfClosing, target, len, depth, bestDepth } = options;
689
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
690
+ if (isSelfClosing) {
691
+ return updateBestMatch(depth, bestDepth, contentStart, contentStart);
692
+ }
693
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
694
+ if (closePos !== -1) {
695
+ return updateBestMatch(depth, bestDepth, contentStart, closePos);
696
+ }
697
+ return null;
698
+ }
699
+ function handleClosingTagInExtract(xmlContent, i, len, depth) {
700
+ const gt = xmlContent.indexOf(">", i + 1);
701
+ return {
702
+ newPos: gt === -1 ? len : gt + 1,
703
+ newDepth: Math.max(0, depth - 1)
704
+ };
705
+ }
706
+ function processOpeningTagInExtract(options) {
707
+ const { xmlContent, i, len, target, depth, bestDepth } = options;
708
+ const tagInfo = parseTagName(xmlContent, i, len);
709
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
710
+ const tagEnd = tagEndInfo.pos;
711
+ const isSelfClosing = tagEndInfo.isSelfClosing;
712
+ let bestMatch = null;
713
+ if (tagInfo.name === target) {
714
+ bestMatch = processTargetTag({
715
+ xmlContent,
716
+ tagEnd,
717
+ isSelfClosing,
718
+ target,
719
+ len,
720
+ depth,
721
+ bestDepth
722
+ });
723
+ }
724
+ return {
725
+ newPos: xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1,
726
+ newDepth: depth + (isSelfClosing ? 0 : 1),
727
+ bestMatch
728
+ };
729
+ }
730
+ function extractRawInner(xmlContent, tagName) {
731
+ const len = xmlContent.length;
732
+ const target = tagName;
733
+ let bestStart = -1;
734
+ let bestEnd = -1;
735
+ let bestDepth = Number.POSITIVE_INFINITY;
736
+ let i = 0;
737
+ let depth = 0;
738
+ while (i < len) {
739
+ const lt = xmlContent.indexOf("<", i);
740
+ if (lt === -1 || lt + 1 >= len) {
741
+ return;
742
+ }
743
+ i = lt + 1;
744
+ const ch = xmlContent[i];
745
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
746
+ if (specialPos !== -1) {
747
+ i = specialPos;
748
+ continue;
749
+ }
750
+ if (ch === "/") {
751
+ const result2 = handleClosingTagInExtract(xmlContent, i, len, depth);
752
+ i = result2.newPos;
753
+ depth = result2.newDepth;
754
+ continue;
755
+ }
756
+ const result = processOpeningTagInExtract({
757
+ xmlContent,
758
+ i,
759
+ len,
760
+ target,
761
+ depth,
762
+ bestDepth
763
+ });
764
+ if (result.bestMatch) {
765
+ bestStart = result.bestMatch.start;
766
+ bestEnd = result.bestMatch.end;
767
+ bestDepth = result.bestMatch.depth;
768
+ }
769
+ i = result.newPos;
770
+ depth = result.newDepth;
771
+ }
772
+ if (bestStart !== -1) {
773
+ return xmlContent.slice(bestStart, bestEnd);
774
+ }
775
+ return;
776
+ }
777
+ function processOpeningTag(options) {
778
+ const { xmlContent, tagEnd, isSelfClosing, target, len, ranges } = options;
779
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
780
+ if (isSelfClosing) {
781
+ ranges.push({ start: contentStart, end: contentStart });
782
+ return contentStart;
783
+ }
784
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
785
+ if (closePos !== -1) {
786
+ ranges.push({ start: contentStart, end: closePos });
787
+ const gt = xmlContent.indexOf(">", closePos);
788
+ return gt === -1 ? len : gt + 1;
789
+ }
790
+ return -1;
791
+ }
792
+ function handleClosingTagInFindAll(xmlContent, i, len) {
793
+ const gt = xmlContent.indexOf(">", i + 1);
794
+ return gt === -1 ? len : gt + 1;
795
+ }
796
+ function findAllInnerRanges(xmlContent, tagName) {
797
+ const len = xmlContent.length;
798
+ const target = tagName;
799
+ const ranges = [];
800
+ let i = 0;
801
+ while (i < len) {
802
+ const lt = xmlContent.indexOf("<", i);
803
+ if (lt === -1 || lt + 1 >= len) {
804
+ break;
805
+ }
806
+ i = lt + 1;
807
+ const ch = xmlContent[i];
808
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
809
+ if (specialPos !== -1) {
810
+ i = specialPos;
811
+ continue;
812
+ }
813
+ if (ch === "/") {
814
+ i = handleClosingTagInFindAll(xmlContent, i, len);
815
+ continue;
816
+ }
817
+ const tagInfo = parseTagName(xmlContent, i, len);
818
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
819
+ const tagEnd = tagEndInfo.pos;
820
+ const isSelfClosing = tagEndInfo.isSelfClosing;
821
+ if (tagInfo.name !== target) {
822
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
823
+ continue;
824
+ }
825
+ const nextPos = processOpeningTag({
826
+ xmlContent,
827
+ tagEnd,
828
+ isSelfClosing,
829
+ target,
830
+ len,
831
+ ranges
832
+ });
833
+ if (nextPos === -1) {
834
+ break;
835
+ }
836
+ i = nextPos;
837
+ }
838
+ return ranges;
839
+ }
840
+ function findTopLevelTargetRange(options) {
841
+ const { xmlContent, tagEnd, isSelfClosing, target, len } = options;
842
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
843
+ if (isSelfClosing) {
844
+ return { start: contentStart, end: contentStart };
845
+ }
846
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
847
+ if (closePos !== -1) {
848
+ return { start: contentStart, end: closePos };
849
+ }
850
+ return;
851
+ }
852
+ function handleClosingTagInFindFirst(xmlContent, i, len, depth) {
853
+ const gt = xmlContent.indexOf(">", i + 1);
854
+ return {
855
+ newPos: gt === -1 ? len : gt + 1,
856
+ newDepth: Math.max(0, depth - 1)
857
+ };
858
+ }
859
+ function findFirstTopLevelRange(xmlContent, tagName) {
860
+ const len = xmlContent.length;
861
+ const target = tagName;
862
+ let i = 0;
863
+ let depth = 0;
864
+ while (i < len) {
865
+ const lt = xmlContent.indexOf("<", i);
866
+ if (lt === -1 || lt + 1 >= len) {
867
+ return;
868
+ }
869
+ i = lt + 1;
870
+ const ch = xmlContent[i];
871
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
872
+ if (specialPos !== -1) {
873
+ i = specialPos;
874
+ continue;
875
+ }
876
+ if (ch === "/") {
877
+ const result = handleClosingTagInFindFirst(xmlContent, i, len, depth);
878
+ i = result.newPos;
879
+ depth = result.newDepth;
880
+ continue;
881
+ }
882
+ const tagInfo = parseTagName(xmlContent, i, len);
883
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
884
+ const tagEnd = tagEndInfo.pos;
885
+ const isSelfClosing = tagEndInfo.isSelfClosing;
886
+ if (depth === 0 && tagInfo.name === target) {
887
+ return findTopLevelTargetRange({
888
+ xmlContent,
889
+ tagEnd,
890
+ isSelfClosing,
891
+ target,
892
+ len
893
+ });
894
+ }
895
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
896
+ depth += isSelfClosing ? 0 : 1;
897
+ }
898
+ return;
899
+ }
900
+ function isPositionExcluded(pos, excludeRanges) {
901
+ if (!excludeRanges || excludeRanges.length === 0) {
902
+ return false;
903
+ }
904
+ for (const r of excludeRanges) {
905
+ if (pos >= r.start && pos < r.end) {
906
+ return true;
907
+ }
908
+ }
909
+ return false;
910
+ }
911
+ function skipCommentInCounting(xmlContent, i, len) {
912
+ const close = xmlContent.indexOf("-->", i + 4);
913
+ return close === -1 ? len : close + 3;
914
+ }
915
+ function skipCdataInCounting(xmlContent, i, len) {
916
+ const close = xmlContent.indexOf("]]>", i + 9);
917
+ return close === -1 ? len : close + 3;
918
+ }
919
+ function skipSpecialInCounting(xmlContent, ch, i, len) {
920
+ if (ch === "!") {
921
+ if (xmlContent.startsWith("!--", i + 1)) {
922
+ return skipCommentInCounting(xmlContent, i, len);
923
+ }
924
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
925
+ return skipCdataInCounting(xmlContent, i, len);
926
+ }
927
+ const gt = xmlContent.indexOf(">", i + 1);
928
+ return gt === -1 ? len : gt + 1;
929
+ }
930
+ if (ch === "?") {
931
+ const close = xmlContent.indexOf("?>", i + 1);
932
+ return close === -1 ? len : close + 2;
933
+ }
934
+ if (ch === "/") {
935
+ const gt = xmlContent.indexOf(">", i + 1);
936
+ return gt === -1 ? len : gt + 1;
937
+ }
938
+ return -1;
939
+ }
940
+ function parseAndCountTag(options) {
941
+ const { xmlContent, i, len, target, lt, excludeRanges } = options;
942
+ let j = i;
943
+ if (j < len && isNameStartChar(xmlContent[j])) {
944
+ j += 1;
945
+ while (j < len && isNameChar(xmlContent[j])) {
946
+ j += 1;
947
+ }
948
+ }
949
+ const name = xmlContent.slice(i, j);
950
+ let k = j;
951
+ while (k < len) {
952
+ const c = xmlContent[k];
953
+ if (c === '"' || c === "'") {
954
+ k = skipQuoted(xmlContent, k);
955
+ continue;
956
+ }
957
+ if (c === ">") {
958
+ break;
959
+ }
960
+ if (c === "/" && xmlContent[k + 1] === ">") {
961
+ k += 1;
962
+ break;
963
+ }
964
+ k += 1;
965
+ }
966
+ const shouldCount = name === target && !isPositionExcluded(lt, excludeRanges);
967
+ return { nextPos: k + 1, shouldCount };
968
+ }
969
+ function countTagOccurrences(xmlContent, tagName, excludeRanges, shouldSkipFirst = true) {
970
+ const len = xmlContent.length;
971
+ const target = tagName;
972
+ let i = 0;
973
+ let count = 0;
974
+ let skipFirstLocal = shouldSkipFirst;
975
+ while (i < len) {
976
+ const lt = xmlContent.indexOf("<", i);
977
+ if (lt === -1) {
978
+ break;
979
+ }
980
+ i = lt + 1;
981
+ if (i >= len) {
982
+ break;
983
+ }
984
+ const ch = xmlContent[i];
985
+ const skipPos = skipSpecialInCounting(xmlContent, ch, i, len);
986
+ if (skipPos !== -1) {
987
+ i = skipPos;
988
+ continue;
989
+ }
990
+ const result = parseAndCountTag({
991
+ xmlContent,
992
+ i,
993
+ len,
994
+ target,
995
+ lt,
996
+ excludeRanges
997
+ });
998
+ if (result.shouldCount) {
999
+ if (skipFirstLocal) {
1000
+ skipFirstLocal = false;
1001
+ } else {
1002
+ count += 1;
1003
+ }
1004
+ }
1005
+ i = result.nextPos;
1006
+ }
1007
+ return count;
1008
+ }
1009
+
1010
+ // src/rxml/core/tokenizer.ts
1011
+ var XMLTokenizer = class {
1012
+ constructor(xmlString, options = {}) {
1013
+ this.pos = 0;
1014
+ this.xmlString = xmlString;
1015
+ this.options = {
1016
+ keepComments: false,
1017
+ keepWhitespace: false,
1018
+ noChildNodes: DEFAULT_NO_CHILD_NODES.slice(),
1019
+ textNodeName: "#text",
1020
+ throwOnDuplicateStringTags: true,
1021
+ ...options
1022
+ };
1023
+ this.pos = options.pos || 0;
1024
+ }
1025
+ /**
1026
+ * Handle closing tag parsing
1027
+ */
1028
+ handleClosingTag(tagName, children) {
1029
+ const closeStart = this.pos + 2;
1030
+ this.pos = this.xmlString.indexOf(">", this.pos);
1031
+ const closeTag = this.xmlString.substring(closeStart, this.pos);
1032
+ if (tagName && closeTag.trim() !== tagName) {
1033
+ const { line, column } = getLineColumn(this.xmlString, this.pos);
1034
+ throw new RXMLParseError(
1035
+ `Unexpected close tag at line ${line}, column ${column}. Expected </${tagName}>, found </${closeTag}>`,
1036
+ void 0,
1037
+ line,
1038
+ column
1039
+ );
1040
+ }
1041
+ if (this.pos !== -1) {
1042
+ this.pos += 1;
1043
+ }
1044
+ return children;
1045
+ }
1046
+ /**
1047
+ * Check if we're at end of string and should throw unclosed tag error
1048
+ */
1049
+ checkUnclosedTag(tagName, consumedToEnd) {
1050
+ if (tagName && this.pos >= this.xmlString.length && !consumedToEnd) {
1051
+ const { line, column } = getLineColumn(this.xmlString, this.pos - 1);
1052
+ throw new RXMLParseError(
1053
+ `Unclosed tag at line ${line}, column ${column}. Expected closing tag </${tagName}>`,
1054
+ void 0,
1055
+ line,
1056
+ column
1057
+ );
1058
+ }
1059
+ }
1060
+ /**
1061
+ * Process special content (comments, CDATA, DOCTYPE) and track if we consumed to end
1062
+ */
1063
+ processSpecialContent(children) {
1064
+ const prevPos = this.pos;
1065
+ this.handleSpecialContent(children);
1066
+ return this.pos >= this.xmlString.length && prevPos < this.xmlString.length;
1067
+ }
1068
+ /**
1069
+ * Handle text content parsing
1070
+ */
1071
+ handleTextContent(children) {
1072
+ const text = this.parseText();
1073
+ if (this.options.keepWhitespace) {
1074
+ if (text.length > 0) {
1075
+ children.push(text);
1076
+ }
1077
+ } else {
1078
+ const trimmed = text.trim();
1079
+ if (trimmed.length > 0) {
1080
+ children.push(trimmed);
1081
+ }
1082
+ }
1083
+ this.pos += 1;
1084
+ }
1085
+ /**
1086
+ * Handle regular element parsing
1087
+ */
1088
+ handleRegularElement(children) {
1089
+ const node = this.parseNode();
1090
+ children.push(node);
1091
+ if (node.tagName[0] === "?") {
1092
+ children.push(...node.children);
1093
+ node.children = [];
1094
+ }
1095
+ }
1096
+ /**
1097
+ * Process a single child element based on the current character
1098
+ */
1099
+ processSingleChild(children, tagName) {
1100
+ if (this.xmlString.charCodeAt(this.pos) !== CharCodes.OPEN_BRACKET) {
1101
+ this.handleTextContent(children);
1102
+ return { shouldReturn: false, consumedToEnd: false };
1103
+ }
1104
+ const nextChar = this.xmlString.charCodeAt(this.pos + 1);
1105
+ if (nextChar === CharCodes.SLASH) {
1106
+ const result = this.handleClosingTag(tagName, children);
1107
+ if (result !== null) {
1108
+ return { shouldReturn: true, consumedToEnd: false };
1109
+ }
1110
+ return { shouldReturn: false, consumedToEnd: false };
1111
+ }
1112
+ if (nextChar === CharCodes.EXCLAMATION) {
1113
+ const wasConsumedToEnd = this.processSpecialContent(children);
1114
+ return { shouldReturn: false, consumedToEnd: wasConsumedToEnd };
1115
+ }
1116
+ this.handleRegularElement(children);
1117
+ return { shouldReturn: false, consumedToEnd: false };
1118
+ }
1119
+ /**
1120
+ * Parse XML children recursively
1121
+ */
1122
+ parseChildren(tagName) {
1123
+ const children = [];
1124
+ let consumedToEnd = false;
1125
+ while (this.xmlString[this.pos]) {
1126
+ const result = this.processSingleChild(children, tagName);
1127
+ if (result.shouldReturn) {
1128
+ return children;
1129
+ }
1130
+ if (result.consumedToEnd) {
1131
+ consumedToEnd = true;
1132
+ }
1133
+ }
1134
+ this.checkUnclosedTag(tagName, consumedToEnd);
1135
+ return children;
1136
+ }
1137
+ /**
1138
+ * Check if character is whitespace
1139
+ */
1140
+ isWhitespace(code) {
1141
+ return code === CharCodes.SPACE || code === CharCodes.TAB || code === CharCodes.NEWLINE || code === CharCodes.CARRIAGE_RETURN;
1142
+ }
1143
+ /**
1144
+ * Skip whitespace characters
1145
+ */
1146
+ skipWhitespace() {
1147
+ while (this.pos < this.xmlString.length && this.isWhitespace(this.xmlString.charCodeAt(this.pos))) {
1148
+ this.pos += 1;
1149
+ }
1150
+ }
1151
+ /**
1152
+ * Parse attribute value
1153
+ */
1154
+ parseAttributeValue() {
1155
+ if (this.pos >= this.xmlString.length || this.xmlString[this.pos] !== "=") {
1156
+ return null;
1157
+ }
1158
+ this.pos += 1;
1159
+ this.skipWhitespace();
1160
+ const code = this.xmlString.charCodeAt(this.pos);
1161
+ if (code === CharCodes.SINGLE_QUOTE || code === CharCodes.DOUBLE_QUOTE) {
1162
+ const { value: parsedValue, newPos: valueEnd } = parseString(
1163
+ this.xmlString,
1164
+ this.pos
1165
+ );
1166
+ this.pos = valueEnd;
1167
+ return parsedValue;
1168
+ }
1169
+ return null;
1170
+ }
1171
+ /**
1172
+ * Parse single attribute
1173
+ */
1174
+ parseAttribute(attributes) {
1175
+ const { name: attrName, newPos: nameEnd } = parseName(
1176
+ this.xmlString,
1177
+ this.pos
1178
+ );
1179
+ this.pos = nameEnd;
1180
+ this.skipWhitespace();
1181
+ const value = this.parseAttributeValue();
1182
+ attributes[attrName] = value;
1183
+ }
1184
+ /**
1185
+ * Parse all attributes
1186
+ */
1187
+ parseAttributes() {
1188
+ const attributes = {};
1189
+ while (this.xmlString.charCodeAt(this.pos) !== CharCodes.CLOSE_BRACKET && this.xmlString[this.pos]) {
1190
+ const c = this.xmlString.charCodeAt(this.pos);
1191
+ if (this.isWhitespace(c)) {
1192
+ this.pos += 1;
1193
+ continue;
1194
+ }
1195
+ if (c > 64 && c < 91 || c > 96 && c < 123) {
1196
+ this.parseAttribute(attributes);
1197
+ } else {
1198
+ this.pos += 1;
1199
+ }
1200
+ }
1201
+ return attributes;
1202
+ }
1203
+ /**
1204
+ * Parse special tag content (script, style)
1205
+ */
1206
+ parseSpecialTagContent(_tagName, closingTag) {
1207
+ const start = this.pos + 1;
1208
+ this.pos = this.xmlString.indexOf(closingTag, this.pos);
1209
+ if (this.pos === -1) {
1210
+ const children2 = [this.xmlString.slice(start)];
1211
+ this.pos = this.xmlString.length;
1212
+ return children2;
1213
+ }
1214
+ const children = [this.xmlString.slice(start, this.pos)];
1215
+ this.pos += closingTag.length;
1216
+ return children;
1217
+ }
1218
+ /**
1219
+ * Parse node children based on tag type
1220
+ */
1221
+ parseNodeChildren(tagName, isSelfClosing) {
1222
+ var _a;
1223
+ if (isSelfClosing) {
1224
+ this.pos += 1;
1225
+ return [];
1226
+ }
1227
+ if (tagName === "script") {
1228
+ return this.parseSpecialTagContent(tagName, "</script>");
1229
+ }
1230
+ if (tagName === "style") {
1231
+ return this.parseSpecialTagContent(tagName, "</style>");
1232
+ }
1233
+ if (((_a = this.options.noChildNodes) == null ? void 0 : _a.indexOf(tagName)) === -1) {
1234
+ this.pos += 1;
1235
+ return this.parseChildren(tagName);
1236
+ }
1237
+ this.pos += 1;
1238
+ if (DEFAULT_NO_CHILD_NODES.includes(tagName)) {
1239
+ return [];
1240
+ }
1241
+ const closingTag = `</${tagName}>`;
1242
+ const closingPos = this.xmlString.indexOf(closingTag, this.pos);
1243
+ if (closingPos !== -1) {
1244
+ this.pos = closingPos + closingTag.length;
1245
+ }
1246
+ return [];
1247
+ }
1248
+ /**
1249
+ * Parse a single XML node
1250
+ */
1251
+ parseNode() {
1252
+ this.pos += 1;
1253
+ const { name: tagName, newPos } = parseName(this.xmlString, this.pos);
1254
+ this.pos = newPos;
1255
+ const attributes = this.parseAttributes();
1256
+ const isSelfClosing = this.xmlString.charCodeAt(this.pos - 1) === CharCodes.SLASH || tagName[0] === "?" && this.xmlString.charCodeAt(this.pos - 1) === CharCodes.QUESTION;
1257
+ const children = this.parseNodeChildren(tagName, isSelfClosing);
1258
+ return { tagName, attributes, children };
1259
+ }
1260
+ /**
1261
+ * Parse text content until next tag
1262
+ */
1263
+ parseText() {
1264
+ const start = this.pos;
1265
+ this.pos = this.xmlString.indexOf("<", this.pos) - 1;
1266
+ if (this.pos === -2) {
1267
+ this.pos = this.xmlString.length;
1268
+ }
1269
+ return this.xmlString.slice(start, this.pos + 1);
1270
+ }
1271
+ /**
1272
+ * Handle comments, CDATA, and DOCTYPE declarations
1273
+ */
1274
+ handleSpecialContent(children) {
1275
+ if (this.xmlString.charCodeAt(this.pos + 2) === CharCodes.MINUS) {
1276
+ this.handleComment(children);
1277
+ } else if (this.xmlString.charCodeAt(this.pos + 2) === CharCodes.OPEN_CORNER_BRACKET && this.xmlString.charCodeAt(this.pos + 8) === CharCodes.OPEN_CORNER_BRACKET && this.xmlString.substr(this.pos + 3, 5).toLowerCase() === "cdata") {
1278
+ this.handleCData(children);
1279
+ } else {
1280
+ this.handleDoctype(children);
1281
+ }
1282
+ }
1283
+ /**
1284
+ * Handle XML comments
1285
+ */
1286
+ handleComment(children) {
1287
+ const startCommentPos = this.pos;
1288
+ while (this.pos !== -1 && !(this.xmlString.charCodeAt(this.pos) === CharCodes.CLOSE_BRACKET && this.xmlString.charCodeAt(this.pos - 1) === CharCodes.MINUS && this.xmlString.charCodeAt(this.pos - 2) === CharCodes.MINUS)) {
1289
+ this.pos = this.xmlString.indexOf(">", this.pos + 1);
1290
+ }
1291
+ if (this.pos === -1) {
1292
+ this.pos = this.xmlString.length;
1293
+ }
1294
+ if (this.options.keepComments) {
1295
+ children.push(this.xmlString.substring(startCommentPos, this.pos + 1));
1296
+ }
1297
+ this.pos += 1;
1298
+ }
1299
+ /**
1300
+ * Handle CDATA sections
1301
+ */
1302
+ handleCData(children) {
1303
+ const cdataEndIndex = this.xmlString.indexOf("]]>", this.pos);
1304
+ if (cdataEndIndex === -1) {
1305
+ children.push(this.xmlString.substr(this.pos + 9));
1306
+ this.pos = this.xmlString.length;
1307
+ } else {
1308
+ children.push(this.xmlString.substring(this.pos + 9, cdataEndIndex));
1309
+ this.pos = cdataEndIndex + 3;
1310
+ }
1311
+ }
1312
+ /**
1313
+ * Handle DOCTYPE declarations
1314
+ */
1315
+ handleDoctype(children) {
1316
+ const startDoctype = this.pos + 1;
1317
+ this.pos += 2;
1318
+ let encapsulated = false;
1319
+ while ((this.xmlString.charCodeAt(this.pos) !== CharCodes.CLOSE_BRACKET || encapsulated) && this.xmlString[this.pos]) {
1320
+ if (this.xmlString.charCodeAt(this.pos) === CharCodes.OPEN_CORNER_BRACKET) {
1321
+ encapsulated = true;
1322
+ } else if (encapsulated && this.xmlString.charCodeAt(this.pos) === CharCodes.CLOSE_CORNER_BRACKET) {
1323
+ encapsulated = false;
1324
+ }
1325
+ this.pos += 1;
1326
+ }
1327
+ children.push(this.xmlString.substring(startDoctype, this.pos));
1328
+ this.pos += 1;
1329
+ }
1330
+ /**
1331
+ * Get current position
1332
+ */
1333
+ getPosition() {
1334
+ return this.pos;
1335
+ }
1336
+ /**
1337
+ * Set position
1338
+ */
1339
+ setPosition(pos) {
1340
+ this.pos = pos;
1341
+ }
1342
+ };
1343
+
1344
+ // src/rxml/core/parser.ts
1345
+ var WHITESPACE_REGEX = /\s/;
1346
+ var NUMERIC_STRING_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
1347
+ var DIGIT_KEY_REGEX = /^\d+$/;
1348
+ function getTopLevelStringProps(s) {
1349
+ const set = /* @__PURE__ */ new Set();
1350
+ const unwrapped = unwrapJsonSchema(s);
1351
+ if (unwrapped && typeof unwrapped === "object") {
1352
+ const props = unwrapped.properties;
1353
+ if (props && typeof props === "object") {
1354
+ for (const [k, v] of Object.entries(props)) {
1355
+ if (getSchemaType(v) === "string") {
1356
+ set.add(k);
1357
+ }
1358
+ }
1359
+ }
1360
+ }
1361
+ return set;
1362
+ }
1363
+ function restorePlaceholderString(val, placeholderMap) {
1364
+ if (val.startsWith("__RXML_PLACEHOLDER_")) {
1365
+ const orig = placeholderMap.get(val);
1366
+ return orig !== void 0 ? orig : val;
1367
+ }
1368
+ return val;
1369
+ }
1370
+ function restorePlaceholdersInObject(obj, _placeholderMap, textNodeName, restorer) {
1371
+ const out = {};
1372
+ for (const [k, v] of Object.entries(obj)) {
1373
+ const restored = restorer(v);
1374
+ if (k === textNodeName && typeof restored === "string") {
1375
+ out[k] = restored.trim();
1376
+ } else {
1377
+ out[k] = restored;
1378
+ }
1379
+ }
1380
+ return out;
1381
+ }
1382
+ function createPlaceholderRestorer(placeholderMap, textNodeName) {
1383
+ const restorer = (val) => {
1384
+ if (val == null) {
1385
+ return val;
1386
+ }
1387
+ if (typeof val === "string") {
1388
+ return restorePlaceholderString(val, placeholderMap);
1389
+ }
1390
+ if (Array.isArray(val)) {
1391
+ return val.map(restorer);
1392
+ }
1393
+ if (typeof val === "object") {
1394
+ return restorePlaceholdersInObject(
1395
+ val,
1396
+ placeholderMap,
1397
+ textNodeName,
1398
+ restorer
1399
+ );
1400
+ }
1401
+ return val;
1402
+ };
1403
+ return restorer;
1404
+ }
1405
+ function tryConvertToNumber(val) {
1406
+ if (typeof val !== "string") {
1407
+ return val;
1408
+ }
1409
+ const trimmed = val.trim();
1410
+ if (NUMERIC_STRING_REGEX.test(trimmed)) {
1411
+ const num = Number(trimmed);
1412
+ if (Number.isFinite(num)) {
1413
+ return num;
1414
+ }
1415
+ }
1416
+ return trimmed;
1417
+ }
1418
+ function processItemValue(item, textNodeName) {
1419
+ let currentVal = item;
1420
+ if (item && typeof item === "object" && Object.hasOwn(item, textNodeName)) {
1421
+ currentVal = item[textNodeName];
1422
+ }
1423
+ const trimmed = typeof currentVal === "string" ? currentVal.trim() : currentVal;
1424
+ return tryConvertToNumber(trimmed);
1425
+ }
1426
+ function processItemWrapper(itemValue, textNodeName) {
1427
+ if (Array.isArray(itemValue)) {
1428
+ return itemValue.map((item) => processItemValue(item, textNodeName));
1429
+ }
1430
+ const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
1431
+ return tryConvertToNumber(trimmed);
1432
+ }
1433
+ function deepDecodeStringsBySchema(input, schema) {
1434
+ var _a;
1435
+ if (input == null || schema == null) {
1436
+ return input;
1437
+ }
1438
+ const type = getSchemaType(schema);
1439
+ if (type === "string" && typeof input === "string") {
1440
+ return unescapeXml(input);
1441
+ }
1442
+ if (type === "array" && Array.isArray(input)) {
1443
+ const unwrapped = unwrapJsonSchema(schema);
1444
+ const itemSchema = (_a = unwrapped == null ? void 0 : unwrapped.items) != null ? _a : {};
1445
+ return input.map((item) => deepDecodeStringsBySchema(item, itemSchema));
1446
+ }
1447
+ if (type === "object" && input && typeof input === "object") {
1448
+ const obj = input;
1449
+ const out = {};
1450
+ for (const key of Object.keys(obj)) {
1451
+ const childSchema = getPropertySchema(schema, key);
1452
+ out[key] = deepDecodeStringsBySchema(obj[key], childSchema);
1453
+ }
1454
+ return out;
1455
+ }
1456
+ if (typeof input === "string") {
1457
+ return unescapeXml(input);
1458
+ }
1459
+ return input;
1460
+ }
1461
+ function parse(xmlInner, schema, options = {}) {
1462
+ var _a, _b, _c;
1463
+ const textNodeName = (_a = options.textNodeName) != null ? _a : "#text";
1464
+ const throwDup = (_b = options.throwOnDuplicateStringTags) != null ? _b : true;
1465
+ let actualXmlInner = xmlInner.trim();
1466
+ if (actualXmlInner.startsWith("<") && actualXmlInner.endsWith(">")) {
1467
+ const s = actualXmlInner;
1468
+ let i = 0;
1469
+ let rootStart = -1;
1470
+ let rootName = "";
1471
+ while (i < s.length) {
1472
+ const lt = s.indexOf("<", i);
1473
+ if (lt === -1) {
1474
+ break;
1475
+ }
1476
+ const next = s[lt + 1];
1477
+ if (next === "?") {
1478
+ const end = s.indexOf("?>", lt + 2);
1479
+ i = end === -1 ? s.length : end + 2;
1480
+ continue;
1481
+ }
1482
+ if (next === "!") {
1483
+ if (s.startsWith("!--", lt + 2)) {
1484
+ const end2 = s.indexOf("-->", lt + 5);
1485
+ i = end2 === -1 ? s.length : end2 + 3;
1486
+ continue;
1487
+ }
1488
+ if (s.startsWith("![CDATA[", lt + 2)) {
1489
+ const end2 = s.indexOf("]]>", lt + 9);
1490
+ i = end2 === -1 ? s.length : end2 + 3;
1491
+ continue;
1492
+ }
1493
+ const end = s.indexOf(">", lt + 2);
1494
+ i = end === -1 ? s.length : end + 1;
1495
+ continue;
1496
+ }
1497
+ if (next === "/") {
1498
+ break;
1499
+ }
1500
+ let j = lt + 1;
1501
+ while (j < s.length && s[j] !== " " && s[j] !== "\n" && s[j] !== "\r" && s[j] !== " " && s[j] !== "/" && s[j] !== ">") {
1502
+ j += 1;
1503
+ }
1504
+ rootStart = lt;
1505
+ rootName = s.slice(lt + 1, j);
1506
+ break;
1507
+ }
1508
+ if (rootStart === 0 && rootName) {
1509
+ const range = findFirstTopLevelRange(s, rootName);
1510
+ if (range) {
1511
+ let fullEnd = range.end + `</${rootName}>`.length;
1512
+ const closeHead = s.indexOf(`</${rootName}`, range.end);
1513
+ if (closeHead === range.end) {
1514
+ let p = closeHead + 2 + rootName.length;
1515
+ while (p < s.length && WHITESPACE_REGEX.test(s[p])) {
1516
+ p += 1;
1517
+ }
1518
+ if (s[p] === ">") {
1519
+ fullEnd = p + 1;
1520
+ }
1521
+ }
1522
+ if (fullEnd === s.length) {
1523
+ const unwrapped = unwrapJsonSchema(schema);
1524
+ const schemaProps = unwrapped && typeof unwrapped === "object" ? unwrapped.properties : void 0;
1525
+ if (schemaProps && !Object.hasOwn(schemaProps, rootName)) {
1526
+ actualXmlInner = s.slice(range.start, range.end);
1527
+ }
1528
+ }
1529
+ }
1530
+ }
1531
+ }
1532
+ const topLevelStringProps = getTopLevelStringProps(schema);
1533
+ const deepStringTypedProps = getStringTypedProperties(schema);
1534
+ const duplicateKeys = /* @__PURE__ */ new Set();
1535
+ for (const key of topLevelStringProps) {
1536
+ const excludeRanges = [];
1537
+ for (const other of topLevelStringProps) {
1538
+ if (other === key) {
1539
+ continue;
1540
+ }
1541
+ const range = findFirstTopLevelRange(actualXmlInner, other);
1542
+ if (range) {
1543
+ excludeRanges.push(range);
1544
+ }
1545
+ }
1546
+ const occurrences = countTagOccurrences(
1547
+ actualXmlInner,
1548
+ key,
1549
+ excludeRanges,
1550
+ true
1551
+ );
1552
+ if (occurrences > 0 && throwDup) {
1553
+ throw new RXMLDuplicateStringTagError(
1554
+ `Duplicate string tags for <${key}> detected`
1555
+ );
1556
+ }
1557
+ if (occurrences > 0 && !throwDup) {
1558
+ duplicateKeys.add(key);
1559
+ if (options.onError) {
1560
+ options.onError(
1561
+ `RXML: Duplicate string tags for <${key}> detected; using first occurrence.`,
1562
+ { tag: key, occurrences }
1563
+ );
1564
+ }
1565
+ }
1566
+ }
1567
+ let xmlInnerForParsing = actualXmlInner;
1568
+ const originalContentMap = /* @__PURE__ */ new Map();
1569
+ try {
1570
+ const ranges = [];
1571
+ for (const key of deepStringTypedProps) {
1572
+ const innerRanges = findAllInnerRanges(actualXmlInner, key);
1573
+ for (const r of innerRanges) {
1574
+ if (r.end > r.start) {
1575
+ ranges.push({ ...r, key });
1576
+ }
1577
+ }
1578
+ }
1579
+ if (ranges.length > 0) {
1580
+ const sorted = [...ranges].sort((a, b) => a.start - b.start);
1581
+ let rebuilt = "";
1582
+ let cursor = 0;
1583
+ for (const r of sorted) {
1584
+ if (r.start < cursor) {
1585
+ continue;
1586
+ }
1587
+ if (cursor < r.start) {
1588
+ rebuilt += actualXmlInner.slice(cursor, r.start);
1589
+ }
1590
+ const placeholder = `__RXML_PLACEHOLDER_${r.key}_${r.start}_${r.end}__`;
1591
+ const originalContent = actualXmlInner.slice(r.start, r.end);
1592
+ originalContentMap.set(placeholder, originalContent);
1593
+ rebuilt += placeholder;
1594
+ cursor = r.end;
1595
+ }
1596
+ if (cursor < actualXmlInner.length) {
1597
+ rebuilt += actualXmlInner.slice(cursor);
1598
+ }
1599
+ xmlInnerForParsing = rebuilt;
1600
+ }
1601
+ } catch (error) {
1602
+ if (options.onError) {
1603
+ options.onError(
1604
+ "RXML: Failed to replace string placeholders, falling back to original XML.",
1605
+ { error }
1606
+ );
1607
+ }
1608
+ xmlInnerForParsing = actualXmlInner;
1609
+ }
1610
+ let parsedNodes;
1611
+ try {
1612
+ const wrappedXml = `<root>${xmlInnerForParsing}</root>`;
1613
+ const tokenizer = new XMLTokenizer(wrappedXml, {
1614
+ ...options,
1615
+ textNodeName
1616
+ });
1617
+ const rootNode = tokenizer.parseNode();
1618
+ parsedNodes = rootNode.children;
1619
+ } catch (cause) {
1620
+ throw new RXMLParseError("Failed to parse XML", cause);
1621
+ }
1622
+ const parsedArgs = domToObject(parsedNodes, schema, textNodeName);
1623
+ const restorePlaceholdersDeep = createPlaceholderRestorer(
1624
+ originalContentMap,
1625
+ textNodeName
1626
+ );
1627
+ const parsedArgsRestored = restorePlaceholdersDeep(parsedArgs);
1628
+ const args = {};
1629
+ for (const k of Object.keys(parsedArgsRestored || {})) {
1630
+ const v = parsedArgsRestored[k];
1631
+ let val = v;
1632
+ const propSchema = getPropertySchema(schema, k);
1633
+ const propType = getSchemaType(propSchema);
1634
+ if (propType === "string" && duplicateKeys.has(k) && Array.isArray(v)) {
1635
+ const firstValue = v[0];
1636
+ if (typeof firstValue === "string" && firstValue.startsWith("__RXML_PLACEHOLDER_")) {
1637
+ const originalContent = originalContentMap.get(firstValue);
1638
+ if (originalContent !== void 0) {
1639
+ args[k] = originalContent;
1640
+ continue;
1641
+ }
1642
+ } else {
1643
+ args[k] = firstValue;
1644
+ continue;
1645
+ }
1646
+ }
1647
+ if (propType === "string" && !Array.isArray(v)) {
1648
+ const placeholderUsed = typeof v === "string" && v.startsWith("__RXML_PLACEHOLDER_") || v && typeof v === "object" && Object.hasOwn(v, textNodeName) && typeof v[textNodeName] === "string" && v[textNodeName].startsWith(
1649
+ "__RXML_PLACEHOLDER_"
1650
+ );
1651
+ if (placeholderUsed) {
1652
+ let placeholderKey;
1653
+ if (typeof v === "string") {
1654
+ placeholderKey = v;
1655
+ } else {
1656
+ placeholderKey = v[textNodeName];
1657
+ }
1658
+ const originalContent = originalContentMap.get(placeholderKey);
1659
+ if (originalContent !== void 0) {
1660
+ args[k] = originalContent;
1661
+ continue;
1662
+ }
1663
+ }
1664
+ const raw = extractRawInner(actualXmlInner, k);
1665
+ if (typeof raw === "string") {
1666
+ args[k] = raw;
1667
+ continue;
1668
+ }
1669
+ }
1670
+ if (v && typeof v === "object" && Object.hasOwn(v, textNodeName)) {
1671
+ val = v[textNodeName];
1672
+ }
1673
+ if (Array.isArray(v)) {
1674
+ if (propType === "string") {
1675
+ const mapped = v.map((item) => {
1676
+ if (item && typeof item === "object" && Object.hasOwn(item, textNodeName)) {
1677
+ const textVal = item[textNodeName];
1678
+ return typeof textVal === "string" ? textVal : String(textVal);
1679
+ }
1680
+ return typeof item === "string" ? item : String(item);
1681
+ });
1682
+ if (mapped.length > 1 && throwDup) {
1683
+ throw new RXMLDuplicateStringTagError(
1684
+ `Duplicate string tags for <${k}> detected`
1685
+ );
1686
+ }
1687
+ if (mapped.length > 1 && !throwDup && options.onError) {
1688
+ options.onError(
1689
+ `RXML: Duplicate string tags for <${k}> detected; using first occurrence.`,
1690
+ { tag: k, occurrences: mapped.length }
1691
+ );
1692
+ }
1693
+ args[k] = (_c = mapped[0]) != null ? _c : "";
1694
+ continue;
1695
+ }
1696
+ val = processArrayContent(v, propSchema, textNodeName);
1697
+ } else if (v && typeof v === "object" && !Object.hasOwn(v, textNodeName)) {
1698
+ const obj = v;
1699
+ const keys2 = Object.keys(obj);
1700
+ if (keys2.length === 1 && keys2[0] === "item") {
1701
+ val = processItemWrapper(obj.item, textNodeName);
1702
+ } else {
1703
+ let isIndexedTuple = false;
1704
+ if (keys2.length > 0 && keys2.every((key) => DIGIT_KEY_REGEX.test(key))) {
1705
+ const indices = keys2.map((keyStr) => Number.parseInt(keyStr, 10)).sort((a, b) => a - b);
1706
+ isIndexedTuple = indices[0] === 0 && indices.every((indexVal, idx) => indexVal === idx);
1707
+ }
1708
+ if (isIndexedTuple) {
1709
+ val = processIndexedTuple(obj, textNodeName);
1710
+ } else {
1711
+ val = v;
1712
+ }
1713
+ }
1714
+ }
1715
+ args[k] = typeof val === "string" ? val.trim() : val;
1716
+ }
1717
+ for (const key of topLevelStringProps) {
1718
+ if (!Object.hasOwn(args, key)) {
1719
+ const raw = extractRawInner(actualXmlInner, key);
1720
+ if (typeof raw === "string") {
1721
+ args[key] = raw;
1722
+ }
1723
+ }
1724
+ }
1725
+ let dataToCoerce = args;
1726
+ const keys = Object.keys(args);
1727
+ if (keys.length === 1) {
1728
+ const rootKey = keys[0];
1729
+ const rootValue = args[rootKey];
1730
+ const unwrapped = unwrapJsonSchema(schema);
1731
+ if (unwrapped && typeof unwrapped === "object") {
1732
+ const schemaProps = unwrapped.properties;
1733
+ if (schemaProps && !Object.hasOwn(schemaProps, rootKey)) {
1734
+ dataToCoerce = rootValue;
1735
+ }
1736
+ }
1737
+ }
1738
+ try {
1739
+ const coerced = coerceDomBySchema(dataToCoerce, schema);
1740
+ const decoded = deepDecodeStringsBySchema(coerced, schema);
1741
+ return decoded;
1742
+ } catch (error) {
1743
+ throw new RXMLCoercionError("Failed to coerce by schema", error);
1744
+ }
1745
+ }
1746
+
1747
+ // src/rxml/heuristics/engine.ts
1748
+ function applyRawSegmentUpdate(current, result) {
1749
+ if (result.rawSegment !== void 0) {
1750
+ return { ...current, rawSegment: result.rawSegment };
1751
+ }
1752
+ return current;
1753
+ }
1754
+ function applyParsedUpdate(current, result) {
1755
+ if (result.parsed !== void 0) {
1756
+ return { ...current, parsed: result.parsed };
1757
+ }
1758
+ return current;
1759
+ }
1760
+ function applyWarningsUpdate(current, result) {
1761
+ var _a, _b;
1762
+ if (result.warnings && result.warnings.length > 0) {
1763
+ const meta = (_a = current.meta) != null ? _a : {};
1764
+ const existingWarnings = (_b = meta.warnings) != null ? _b : [];
1765
+ return {
1766
+ ...current,
1767
+ meta: { ...meta, warnings: [...existingWarnings, ...result.warnings] }
1768
+ };
1769
+ }
1770
+ return current;
1771
+ }
1772
+ function attemptReparse(current, result, reparseCount, maxReparses, parse3) {
1773
+ if (!result.reparse || result.rawSegment === void 0 || reparseCount >= maxReparses) {
1774
+ return { state: current, newCount: reparseCount };
1775
+ }
1776
+ try {
1777
+ const reparsed = parse3(result.rawSegment, current.schema);
1778
+ return {
1779
+ state: { ...current, parsed: reparsed, errors: [] },
1780
+ newCount: reparseCount + 1
1781
+ };
1782
+ } catch (error) {
1783
+ return {
1784
+ state: { ...current, errors: [...current.errors, error] },
1785
+ newCount: reparseCount + 1
1786
+ };
1787
+ }
1788
+ }
1789
+ function executePhase(ctx, heuristics, options) {
1790
+ var _a;
1791
+ let current = ctx;
1792
+ let reparseCount = 0;
1793
+ const maxReparses = (_a = options.maxReparses) != null ? _a : 2;
1794
+ for (const heuristic of heuristics) {
1795
+ if (!heuristic.applies(current)) {
1796
+ continue;
1797
+ }
1798
+ const result = heuristic.run(current);
1799
+ current = applyRawSegmentUpdate(current, result);
1800
+ current = applyParsedUpdate(current, result);
1801
+ current = applyWarningsUpdate(current, result);
1802
+ const reparseResult = attemptReparse(
1803
+ current,
1804
+ result,
1805
+ reparseCount,
1806
+ maxReparses,
1807
+ options.parse
1808
+ );
1809
+ current = reparseResult.state;
1810
+ reparseCount = reparseResult.newCount;
1811
+ if (result.stop) {
1812
+ break;
1813
+ }
1814
+ }
1815
+ return current;
1816
+ }
1817
+ function applyHeuristicPipeline(ctx, config, options) {
1818
+ let current = ctx;
1819
+ if (config.preParse && config.preParse.length > 0) {
1820
+ current = executePhase(current, config.preParse, options);
1821
+ }
1822
+ if (current.parsed === null && current.errors.length === 0) {
1823
+ try {
1824
+ const parsed = options.parse(current.rawSegment, current.schema);
1825
+ current = { ...current, parsed, errors: [] };
1826
+ } catch (error) {
1827
+ current = { ...current, errors: [error] };
1828
+ }
1829
+ }
1830
+ if (current.errors.length > 0 && config.fallbackReparse && config.fallbackReparse.length > 0) {
1831
+ current = executePhase(current, config.fallbackReparse, options);
1832
+ }
1833
+ if (current.parsed !== null && config.postParse && config.postParse.length > 0) {
1834
+ current = executePhase(current, config.postParse, options);
1835
+ }
1836
+ return current;
1837
+ }
1838
+ function createIntermediateCall(toolName, rawSegment, schema) {
1839
+ return {
1840
+ toolName,
1841
+ schema,
1842
+ rawSegment,
1843
+ parsed: null,
1844
+ errors: [],
1845
+ meta: { originalContent: rawSegment }
1846
+ };
1847
+ }
1848
+
1849
+ // src/rxml/heuristics/xml-defaults.ts
1850
+ var MALFORMED_CLOSE_RE_G = /<\/\s+([A-Za-z0-9_:-]+)\s*>/g;
1851
+ var MALFORMED_CLOSE_RE = /<\/\s+([A-Za-z0-9_:-]+)\s*>/;
1852
+ var STATUS_TO_STEP_BOUNDARY_RE = /<\/status>\s*<step>/g;
1853
+ var WHITESPACE_REGEX2 = /\s/;
1854
+ var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
1855
+ var NAME_START_CHAR_RE = /[A-Za-z_:]/;
1856
+ var STEP_TAG_RE = /<step>([\s\S]*?)<\/step>/i;
1857
+ var STATUS_TAG_RE = /<status>([\s\S]*?)<\/status>/i;
1858
+ var normalizeCloseTagsHeuristic = {
1859
+ id: "normalize-close-tags",
1860
+ phase: "pre-parse",
1861
+ applies: () => true,
1862
+ run: (ctx) => {
1863
+ const normalized = ctx.rawSegment.replace(MALFORMED_CLOSE_RE_G, "</$1>");
1864
+ if (normalized !== ctx.rawSegment) {
1865
+ return { rawSegment: normalized };
1866
+ }
1867
+ return {};
1868
+ }
1869
+ };
1870
+ var escapeInvalidLtHeuristic = {
1871
+ id: "escape-invalid-lt",
1872
+ phase: "pre-parse",
1873
+ applies: () => true,
1874
+ run: (ctx) => {
1875
+ const escaped = escapeInvalidLt(ctx.rawSegment);
1876
+ if (escaped !== ctx.rawSegment) {
1877
+ return { rawSegment: escaped };
1878
+ }
1879
+ return {};
1880
+ }
1881
+ };
1882
+ var balanceTagsHeuristic = {
1883
+ id: "balance-tags",
1884
+ phase: "fallback-reparse",
1885
+ applies: (ctx) => {
1886
+ var _a;
1887
+ const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
1888
+ const normalized = original.replace(MALFORMED_CLOSE_RE_G, "</$1>");
1889
+ const balanced = balanceTags(original);
1890
+ const hasMalformedClose = MALFORMED_CLOSE_RE.test(original);
1891
+ if (!hasMalformedClose && balanced.length > normalized.length && ctx.errors.length === 0) {
1892
+ return false;
1893
+ }
1894
+ return balanced !== normalized;
1895
+ },
1896
+ run: (ctx) => {
1897
+ var _a;
1898
+ const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
1899
+ const balanced = balanceTags(original);
1900
+ const escaped = escapeInvalidLt(balanced);
1901
+ return { rawSegment: escaped, reparse: true };
1902
+ }
1903
+ };
1904
+ var dedupeShellStringTagsHeuristic = {
1905
+ id: "dedupe-shell-string-tags",
1906
+ phase: "fallback-reparse",
1907
+ applies: (ctx) => shouldDeduplicateStringTags(ctx.schema),
1908
+ run: (ctx) => {
1909
+ const names = getStringPropertyNames(ctx.schema);
1910
+ let deduped = ctx.rawSegment;
1911
+ for (const key of names) {
1912
+ deduped = dedupeSingleTag(deduped, key);
1913
+ }
1914
+ if (deduped !== ctx.rawSegment) {
1915
+ return { rawSegment: deduped, reparse: true };
1916
+ }
1917
+ return {};
1918
+ }
1919
+ };
1920
+ var repairAgainstSchemaHeuristic = {
1921
+ id: "repair-against-schema",
1922
+ phase: "post-parse",
1923
+ applies: (ctx) => ctx.parsed !== null && typeof ctx.parsed === "object",
1924
+ run: (ctx) => {
1925
+ const repaired = repairParsedAgainstSchema(ctx.parsed, ctx.schema);
1926
+ if (repaired !== ctx.parsed) {
1927
+ return { parsed: repaired };
1928
+ }
1929
+ return {};
1930
+ }
1931
+ };
1932
+ var defaultPipelineConfig = {
1933
+ preParse: [normalizeCloseTagsHeuristic, escapeInvalidLtHeuristic],
1934
+ fallbackReparse: [balanceTagsHeuristic, dedupeShellStringTagsHeuristic],
1935
+ postParse: [repairAgainstSchemaHeuristic]
1936
+ };
1937
+ var INDEX_TAG_RE = /^<(\d+)(?:>|\/?>)/;
1938
+ function isIndexTagAt(xml, pos) {
1939
+ const remaining = xml.slice(pos);
1940
+ return INDEX_TAG_RE.test(remaining);
1941
+ }
1942
+ function escapeInvalidLt(xml) {
1943
+ const len = xml.length;
1944
+ let out = "";
1945
+ for (let i = 0; i < len; i += 1) {
1946
+ const ch = xml[i];
1947
+ if (ch === "<") {
1948
+ const next = i + 1 < len ? xml[i + 1] : "";
1949
+ const isValidStart = NAME_START_CHAR_RE.test(next) || next === "/" || next === "!" || next === "?";
1950
+ const isIndexTag = !isValidStart && isIndexTagAt(xml, i);
1951
+ if (!(isValidStart || isIndexTag)) {
1952
+ out += "&lt;";
1953
+ continue;
1954
+ }
1955
+ }
1956
+ out += ch;
1957
+ }
1958
+ return out;
1959
+ }
1960
+ function balanceTags(xml) {
1961
+ const src = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>").replace(STATUS_TO_STEP_BOUNDARY_RE, "</status></step><step>");
1962
+ let i = 0;
1963
+ const len = src.length;
1964
+ const out = [];
1965
+ const stack = [];
1966
+ while (i < len) {
1967
+ const lt = src.indexOf("<", i);
1968
+ if (lt === -1) {
1969
+ out.push(src.slice(i));
1970
+ break;
1971
+ }
1972
+ out.push(src.slice(i, lt));
1973
+ if (lt + 1 >= len) {
1974
+ break;
1975
+ }
1976
+ const next = src[lt + 1];
1977
+ if (next === "!" || next === "?") {
1978
+ i = handleSpecialTagSegment(src, lt, out);
1979
+ continue;
1980
+ }
1981
+ if (next === "/") {
1982
+ i = handleClosingTagSegment(src, lt, out, stack);
1983
+ continue;
1984
+ }
1985
+ i = handleOpeningTagSegment(src, lt, out, stack);
1986
+ }
1987
+ for (let k = stack.length - 1; k >= 0; k -= 1) {
1988
+ out.push(`</${stack[k]}>`);
1989
+ }
1990
+ return out.join("");
1991
+ }
1992
+ function skipWs(s, p, len) {
1993
+ let idx = p;
1994
+ while (idx < len && WHITESPACE_REGEX2.test(s[idx])) {
1995
+ idx += 1;
1996
+ }
1997
+ return idx;
1998
+ }
1999
+ function parseTagNameAt(s, p, len) {
2000
+ let idx = p;
2001
+ const start = idx;
2002
+ while (idx < len && NAME_CHAR_RE.test(s[idx])) {
2003
+ idx += 1;
2004
+ }
2005
+ return { name: s.slice(start, idx), pos: idx };
2006
+ }
2007
+ function handleSpecialTagSegment(src, lt, out) {
2008
+ const gt = src.indexOf(">", lt + 1);
2009
+ if (gt === -1) {
2010
+ out.push(src.slice(lt));
2011
+ return src.length;
2012
+ }
2013
+ out.push(src.slice(lt, gt + 1));
2014
+ return gt + 1;
2015
+ }
2016
+ function handleClosingTagSegment(src, lt, out, stack) {
2017
+ const len = src.length;
2018
+ let p = skipWs(src, lt + 2, len);
2019
+ const { name, pos } = parseTagNameAt(src, p, len);
2020
+ p = pos;
2021
+ const gt = src.indexOf(">", p);
2022
+ const closingText = gt === -1 ? src.slice(lt) : src.slice(lt, gt + 1);
2023
+ const idx = stack.lastIndexOf(name);
2024
+ if (idx !== -1) {
2025
+ for (let k = stack.length - 1; k > idx; k -= 1) {
2026
+ out.push(`</${stack[k]}>`);
2027
+ stack.pop();
2028
+ }
2029
+ out.push(closingText);
2030
+ stack.pop();
2031
+ }
2032
+ return gt === -1 ? len : gt + 1;
2033
+ }
2034
+ function handleOpeningTagSegment(src, lt, out, stack) {
2035
+ const len = src.length;
2036
+ let p = skipWs(src, lt + 1, len);
2037
+ const nameStart = p;
2038
+ const parsed = parseTagNameAt(src, p, len);
2039
+ p = parsed.pos;
2040
+ const name = src.slice(nameStart, p);
2041
+ const q = src.indexOf(">", p);
2042
+ if (q === -1) {
2043
+ out.push(src.slice(lt));
2044
+ return len;
2045
+ }
2046
+ let r = q - 1;
2047
+ while (r >= nameStart && WHITESPACE_REGEX2.test(src[r])) {
2048
+ r -= 1;
2049
+ }
2050
+ const selfClosing = src[r] === "/";
2051
+ out.push(src.slice(lt, q + 1));
2052
+ if (!selfClosing && name) {
2053
+ stack.push(name);
2054
+ }
2055
+ return q + 1;
2056
+ }
2057
+ function extractSchemaProperties(schema) {
2058
+ const unwrapped = unwrapJsonSchema(schema);
2059
+ if (!unwrapped || typeof unwrapped !== "object") {
2060
+ return void 0;
2061
+ }
2062
+ return unwrapped.properties;
2063
+ }
2064
+ function shouldDeduplicateStringTags(schema) {
2065
+ const props = extractSchemaProperties(schema);
2066
+ if (!props) {
2067
+ return false;
2068
+ }
2069
+ const commandRaw = props.command;
2070
+ if (!commandRaw) {
2071
+ return false;
2072
+ }
2073
+ const command = unwrapJsonSchema(commandRaw);
2074
+ return (command == null ? void 0 : command.type) === "array";
2075
+ }
2076
+ function getStringPropertyNames(schema) {
2077
+ const props = extractSchemaProperties(schema);
2078
+ if (!props) {
2079
+ return [];
2080
+ }
2081
+ const names = [];
2082
+ for (const key of Object.keys(props)) {
2083
+ const prop = unwrapJsonSchema(props[key]);
2084
+ if ((prop == null ? void 0 : prop.type) === "string") {
2085
+ names.push(key);
2086
+ }
2087
+ }
2088
+ return names;
2089
+ }
2090
+ function escapeRegExp(s) {
2091
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2092
+ }
2093
+ function dedupeSingleTag(xml, key) {
2094
+ var _a, _b;
2095
+ const escaped = escapeRegExp(key);
2096
+ const re = new RegExp(`<${escaped}>([\\s\\S]*?)<\\/${escaped}>`, "g");
2097
+ const matches = Array.from(xml.matchAll(re));
2098
+ if (matches.length <= 1) {
2099
+ return xml;
2100
+ }
2101
+ const last = matches.at(-1);
2102
+ let result = "";
2103
+ let cursor = 0;
2104
+ for (const m of matches) {
2105
+ const idx = (_a = m.index) != null ? _a : 0;
2106
+ result += xml.slice(cursor, idx);
2107
+ if (last && idx === ((_b = last.index) != null ? _b : -1)) {
2108
+ result += m[0];
2109
+ }
2110
+ cursor = idx + m[0].length;
2111
+ }
2112
+ result += xml.slice(cursor);
2113
+ return result;
2114
+ }
2115
+ function repairParsedAgainstSchema(input, schema) {
2116
+ if (!input || typeof input !== "object") {
2117
+ return input;
2118
+ }
2119
+ const properties = extractSchemaProperties(schema);
2120
+ if (!properties) {
2121
+ return input;
2122
+ }
2123
+ applySchemaProps(input, properties);
2124
+ return input;
2125
+ }
2126
+ function applySchemaProps(obj, properties) {
2127
+ for (const key of Object.keys(obj)) {
2128
+ const propSchema = properties[key];
2129
+ if (!propSchema) {
2130
+ continue;
2131
+ }
2132
+ const prop = unwrapJsonSchema(propSchema);
2133
+ if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
2134
+ const itemSchema = unwrapJsonSchema(prop.items);
2135
+ obj[key] = coerceArrayItems(obj[key], itemSchema);
2136
+ continue;
2137
+ }
2138
+ if ((prop == null ? void 0 : prop.type) === "object") {
2139
+ const val = obj[key];
2140
+ if (val && typeof val === "object") {
2141
+ obj[key] = repairParsedAgainstSchema(val, prop);
2142
+ }
2143
+ }
2144
+ }
2145
+ }
2146
+ function coerceArrayItems(val, itemSchema) {
2147
+ if (!Array.isArray(val)) {
2148
+ return val;
2149
+ }
2150
+ return val.map((v) => coerceArrayItem(v, itemSchema));
2151
+ }
2152
+ function coerceArrayItem(v, itemSchema) {
2153
+ const itemType = itemSchema == null ? void 0 : itemSchema.type;
2154
+ if (typeof v === "string" && itemType === "object") {
2155
+ const parsed = tryParseStringToSchemaObject(v, itemSchema);
2156
+ if (parsed !== null) {
2157
+ return parsed;
2158
+ }
2159
+ const fallback = extractStepStatusFromString(
2160
+ v.replace(MALFORMED_CLOSE_RE_G, "</$1>")
2161
+ );
2162
+ if (fallback) {
2163
+ return fallback;
2164
+ }
2165
+ return v;
2166
+ }
2167
+ if (v && typeof v === "object" && itemType === "object") {
2168
+ return repairParsedAgainstSchema(v, itemSchema);
2169
+ }
2170
+ return v;
2171
+ }
2172
+ function tryParseStringToSchemaObject(xml, itemSchema) {
2173
+ try {
2174
+ const normalized = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>");
2175
+ const fixed = parse(normalized, itemSchema, { noChildNodes: [] });
2176
+ return typeof fixed === "string" ? null : fixed;
2177
+ } catch (e) {
2178
+ return null;
2179
+ }
2180
+ }
2181
+ function extractStepStatusFromString(normXml) {
2182
+ const stepMatch = normXml.match(STEP_TAG_RE);
2183
+ const statusMatch = normXml.match(STATUS_TAG_RE);
2184
+ if (stepMatch && statusMatch) {
2185
+ return { step: stepMatch[1], status: statusMatch[1] };
2186
+ }
2187
+ return null;
2188
+ }
2189
+
2190
+ // src/rxml/parse.ts
2191
+ function parse2(xml, schema, options = {}) {
2192
+ if (!options.repair) {
2193
+ return parse(xml, schema, options);
2194
+ }
2195
+ const baseOptions = {
2196
+ ...options,
2197
+ repair: false
2198
+ };
2199
+ const ctx = createIntermediateCall("", xml, schema);
2200
+ const result = applyHeuristicPipeline(ctx, defaultPipelineConfig, {
2201
+ parse: (raw, s) => parse(raw, s, baseOptions),
2202
+ onError: options.onError,
2203
+ maxReparses: options.maxReparses
2204
+ });
2205
+ if (result.parsed !== null) {
2206
+ return result.parsed;
2207
+ }
2208
+ const error = result.errors[0];
2209
+ throw new RXMLParseError("Failed to parse XML with repair heuristics", error);
2210
+ }
2211
+
2212
+ export {
2213
+ stringify,
2214
+ parse2 as parse
2215
+ };
2216
+ //# sourceMappingURL=chunk-TR2ARLIF.js.map