@amazon/vinyl-xml 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.
Files changed (85) hide show
  1. package/dist/index.cjs +976 -0
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.js +986 -0
  4. package/dist/index.js.map +7 -0
  5. package/dist/types/current/index.d.ts +14 -0
  6. package/dist/types/current/index.d.ts.map +1 -0
  7. package/dist/types/current/index.js +13 -0
  8. package/dist/types/current/tsconfig.tsbuildinfo +1 -0
  9. package/dist/types/current/xlink/actuateValidator.d.ts +3 -0
  10. package/dist/types/current/xlink/actuateValidator.d.ts.map +1 -0
  11. package/dist/types/current/xlink/actuateValidator.js +11 -0
  12. package/dist/types/current/xml/ParseXmlHandler.d.ts +56 -0
  13. package/dist/types/current/xml/ParseXmlHandler.d.ts.map +1 -0
  14. package/dist/types/current/xml/ParseXmlHandler.js +5 -0
  15. package/dist/types/current/xml/ParseXmlHandlerImpl.d.ts +49 -0
  16. package/dist/types/current/xml/ParseXmlHandlerImpl.d.ts.map +1 -0
  17. package/dist/types/current/xml/ParseXmlHandlerImpl.js +198 -0
  18. package/dist/types/current/xml/QName.d.ts +33 -0
  19. package/dist/types/current/xml/QName.d.ts.map +1 -0
  20. package/dist/types/current/xml/QName.js +19 -0
  21. package/dist/types/current/xml/XmlSchemaError.d.ts +15 -0
  22. package/dist/types/current/xml/XmlSchemaError.d.ts.map +1 -0
  23. package/dist/types/current/xml/XmlSchemaError.js +23 -0
  24. package/dist/types/current/xml/mappedXmlRules.d.ts +118 -0
  25. package/dist/types/current/xml/mappedXmlRules.d.ts.map +1 -0
  26. package/dist/types/current/xml/mappedXmlRules.js +122 -0
  27. package/dist/types/current/xml/mergeXmlRules.d.ts +62 -0
  28. package/dist/types/current/xml/mergeXmlRules.d.ts.map +1 -0
  29. package/dist/types/current/xml/mergeXmlRules.js +89 -0
  30. package/dist/types/current/xml/parseXml.d.ts +76 -0
  31. package/dist/types/current/xml/parseXml.d.ts.map +1 -0
  32. package/dist/types/current/xml/parseXml.js +305 -0
  33. package/dist/types/current/xml/stringifyXml.d.ts +67 -0
  34. package/dist/types/current/xml/stringifyXml.d.ts.map +1 -0
  35. package/dist/types/current/xml/stringifyXml.js +225 -0
  36. package/dist/types/current/xml/xmlRules.d.ts +207 -0
  37. package/dist/types/current/xml/xmlRules.d.ts.map +1 -0
  38. package/dist/types/current/xml/xmlRules.js +134 -0
  39. package/dist/types/current/xmlns/org/w3/1999/xlink.d.ts +7 -0
  40. package/dist/types/current/xmlns/org/w3/1999/xlink.d.ts.map +1 -0
  41. package/dist/types/current/xmlns/org/w3/1999/xlink.js +5 -0
  42. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  43. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.d.ts.map +1 -0
  44. package/dist/types/current/xmlns/org/w3/2001/XmlSchema.js +5 -0
  45. package/dist/types/ts3.7/index.d.ts +14 -0
  46. package/dist/types/ts3.7/xlink/actuateValidator.d.ts +3 -0
  47. package/dist/types/ts3.7/xml/ParseXmlHandler.d.ts +56 -0
  48. package/dist/types/ts3.7/xml/ParseXmlHandlerImpl.d.ts +49 -0
  49. package/dist/types/ts3.7/xml/QName.d.ts +33 -0
  50. package/dist/types/ts3.7/xml/XmlSchemaError.d.ts +15 -0
  51. package/dist/types/ts3.7/xml/mappedXmlRules.d.ts +118 -0
  52. package/dist/types/ts3.7/xml/mergeXmlRules.d.ts +118 -0
  53. package/dist/types/ts3.7/xml/parseXml.d.ts +76 -0
  54. package/dist/types/ts3.7/xml/stringifyXml.d.ts +67 -0
  55. package/dist/types/ts3.7/xml/xmlRules.d.ts +207 -0
  56. package/dist/types/ts3.7/xmlns/org/w3/1999/xlink.d.ts +7 -0
  57. package/dist/types/ts3.7/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  58. package/dist/types/ts4.1/index.d.ts +14 -0
  59. package/dist/types/ts4.1/xlink/actuateValidator.d.ts +3 -0
  60. package/dist/types/ts4.1/xml/ParseXmlHandler.d.ts +56 -0
  61. package/dist/types/ts4.1/xml/ParseXmlHandlerImpl.d.ts +49 -0
  62. package/dist/types/ts4.1/xml/QName.d.ts +33 -0
  63. package/dist/types/ts4.1/xml/XmlSchemaError.d.ts +15 -0
  64. package/dist/types/ts4.1/xml/mappedXmlRules.d.ts +118 -0
  65. package/dist/types/ts4.1/xml/mergeXmlRules.d.ts +118 -0
  66. package/dist/types/ts4.1/xml/parseXml.d.ts +77 -0
  67. package/dist/types/ts4.1/xml/stringifyXml.d.ts +67 -0
  68. package/dist/types/ts4.1/xml/xmlRules.d.ts +207 -0
  69. package/dist/types/ts4.1/xmlns/org/w3/1999/xlink.d.ts +7 -0
  70. package/dist/types/ts4.1/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  71. package/dist/types/ts4.7/index.d.ts +14 -0
  72. package/dist/types/ts4.7/xlink/actuateValidator.d.ts +3 -0
  73. package/dist/types/ts4.7/xml/ParseXmlHandler.d.ts +56 -0
  74. package/dist/types/ts4.7/xml/ParseXmlHandlerImpl.d.ts +49 -0
  75. package/dist/types/ts4.7/xml/QName.d.ts +33 -0
  76. package/dist/types/ts4.7/xml/XmlSchemaError.d.ts +15 -0
  77. package/dist/types/ts4.7/xml/mappedXmlRules.d.ts +118 -0
  78. package/dist/types/ts4.7/xml/mergeXmlRules.d.ts +118 -0
  79. package/dist/types/ts4.7/xml/parseXml.d.ts +76 -0
  80. package/dist/types/ts4.7/xml/stringifyXml.d.ts +67 -0
  81. package/dist/types/ts4.7/xml/xmlRules.d.ts +207 -0
  82. package/dist/types/ts4.7/xmlns/org/w3/1999/xlink.d.ts +7 -0
  83. package/dist/types/ts4.7/xmlns/org/w3/2001/XmlSchema.d.ts +17 -0
  84. package/docs/XML_PARSING.md +210 -0
  85. package/package.json +82 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,976 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
37
+
38
+ // src/index.ts
39
+ var index_exports = {};
40
+ __export(index_exports, {
41
+ EmptyRules: () => EmptyRules,
42
+ ParseXmlHandlerImpl: () => ParseXmlHandlerImpl,
43
+ XMLNS: () => XMLNS,
44
+ XmlElementImpl: () => XmlElementImpl,
45
+ XmlRuleType: () => XmlRuleType,
46
+ XmlSchemaError: () => XmlSchemaError,
47
+ XmlWriter: () => XmlWriter,
48
+ assertActuateEnum: () => assertActuateEnum,
49
+ attr: () => attr,
50
+ attrBoolean: () => attrBoolean,
51
+ attrDateTime: () => attrDateTime,
52
+ attrFloat: () => attrFloat,
53
+ attrInt: () => attrInt,
54
+ attrString: () => attrString,
55
+ characters: () => characters,
56
+ charactersString: () => charactersString,
57
+ element: () => element,
58
+ elements: () => elements,
59
+ isNameChar: () => isNameChar,
60
+ mapXmlRules: () => mapXmlRules,
61
+ mergeXmlRules: () => mergeXmlRules,
62
+ parseXml: () => parseXml,
63
+ parseXmlHandler: () => parseXmlHandler,
64
+ qName: () => qName,
65
+ readAttributeValue: () => readAttributeValue,
66
+ readQName: () => readQName,
67
+ stringifyXml: () => stringifyXml,
68
+ untilTagClose: () => untilTagClose,
69
+ xmlAttributes: () => xmlAttributes,
70
+ xmlRuleKey: () => xmlRuleKey,
71
+ xmlSchemaErrorLocale: () => xmlSchemaErrorLocale
72
+ });
73
+ module.exports = __toCommonJS(index_exports);
74
+
75
+ // src/xml/xmlRules.ts
76
+ var import_vinyl_util = require("@amazon/vinyl-util");
77
+ var XmlRuleType = /* @__PURE__ */ ((XmlRuleType2) => {
78
+ XmlRuleType2["CHARACTERS"] = "characters";
79
+ XmlRuleType2["ATTRIBUTE"] = "attribute";
80
+ XmlRuleType2["ELEMENT"] = "element";
81
+ XmlRuleType2["ELEMENTS"] = "elements";
82
+ return XmlRuleType2;
83
+ })(XmlRuleType || {});
84
+ function element(rules, options) {
85
+ return __spreadProps(__spreadValues({
86
+ type: "element" /* ELEMENT */,
87
+ localPart: void 0,
88
+ namespaceUri: void 0,
89
+ required: false
90
+ }, options), {
91
+ rules
92
+ });
93
+ }
94
+ function elements(rules, options) {
95
+ return __spreadProps(__spreadValues({
96
+ type: "elements" /* ELEMENTS */,
97
+ localPart: void 0,
98
+ namespaceUri: void 0,
99
+ minOccurs: 0,
100
+ maxOccurs: null,
101
+ useEmptyArrays: false
102
+ }, options), {
103
+ rules
104
+ });
105
+ }
106
+ function characters(parse, stringify2) {
107
+ return { type: "characters" /* CHARACTERS */, parse, stringify: stringify2 };
108
+ }
109
+ var charactersString = /* @__PURE__ */ characters((i) => i, import_vinyl_util.stringify);
110
+ function attr(parse, stringify2, options) {
111
+ return __spreadProps(__spreadValues({
112
+ type: "attribute" /* ATTRIBUTE */,
113
+ localPart: void 0,
114
+ namespaceUri: null,
115
+ required: false,
116
+ default: void 0
117
+ }, options), {
118
+ parse,
119
+ stringify: stringify2
120
+ });
121
+ }
122
+ function attrString(options) {
123
+ return attr((i) => i, import_vinyl_util.stringify, options);
124
+ }
125
+ function attrInt(options) {
126
+ return attr(parseInt, import_vinyl_util.stringify, options);
127
+ }
128
+ function attrFloat(options) {
129
+ return attr(parseFloat, import_vinyl_util.stringify, options);
130
+ }
131
+ function attrBoolean(options) {
132
+ return attr(import_vinyl_util.parseBoolean, import_vinyl_util.stringify, options);
133
+ }
134
+ function attrDateTime(options) {
135
+ return attr(import_vinyl_util.parseDate, import_vinyl_util.stringifyDate, options);
136
+ }
137
+
138
+ // src/xml/stringifyXml.ts
139
+ var import_vinyl_util3 = require("@amazon/vinyl-util");
140
+
141
+ // src/xml/XmlSchemaError.ts
142
+ var import_vinyl_util2 = require("@amazon/vinyl-util");
143
+ var xmlSchemaErrorLocale = {
144
+ atLeastElements: "Expected at least {expected} occurrences of {property} but was {actual}",
145
+ atMostElements: "Expected at most {expected} occurrences of {property} but was {actual}",
146
+ cyclicReferences: "Cyclic reference detected",
147
+ requiredAttribute: `Attribute '{property}' is required`
148
+ };
149
+ var XmlSchemaError = class _XmlSchemaError extends import_vinyl_util2.ReportableError {
150
+ get [Symbol.toStringTag]() {
151
+ return "XmlSchemaError";
152
+ }
153
+ constructor(message) {
154
+ super(message, import_vinyl_util2.ErrorOrigin.PARSING);
155
+ Object.setPrototypeOf(this, _XmlSchemaError.prototype);
156
+ }
157
+ };
158
+
159
+ // src/xml/stringifyXml.ts
160
+ var locale = {
161
+ /**
162
+ * When a namespace binding is generated, if the prefix was not provided in the stringifier
163
+ * options, it will be generated from this template.
164
+ */
165
+ prefixTemplate: "ns_{value}"
166
+ };
167
+ var defaultStringifyOptions = {
168
+ indent: " ",
169
+ newline: "\n",
170
+ prefixMap: /* @__PURE__ */ new Map(),
171
+ includeXmlDeclaration: true
172
+ };
173
+ var XmlWriter = class {
174
+ constructor(rules, options) {
175
+ this.rules = rules;
176
+ __publicField(this, "options");
177
+ __publicField(this, "usedPrefixes", /* @__PURE__ */ new Map());
178
+ __publicField(this, "traversed", /* @__PURE__ */ new Set());
179
+ this.options = __spreadValues(__spreadValues({}, defaultStringifyOptions), options);
180
+ }
181
+ stringify(object) {
182
+ let str = this.options.includeXmlDeclaration ? '<?xml version="1.0"?>' + this.options.newline : "";
183
+ const state = new StringifyState();
184
+ str += this.stringifyChildren(object, this.rules.elements, state);
185
+ this.usedPrefixes.clear();
186
+ this.traversed.clear();
187
+ return str;
188
+ }
189
+ qName(localPart, namespaceUri) {
190
+ if (!namespaceUri) return localPart;
191
+ const prefixMap = this.options.prefixMap;
192
+ const prefix = (0, import_vinyl_util3.getOrSet)(this.usedPrefixes, namespaceUri, () => {
193
+ if (prefixMap.has(namespaceUri)) {
194
+ return prefixMap.get(namespaceUri);
195
+ }
196
+ return (0, import_vinyl_util3.substitute)(locale.prefixTemplate, {
197
+ value: (this.usedPrefixes.size + 1).toString(36)
198
+ });
199
+ });
200
+ return `${prefix}:${localPart}`;
201
+ }
202
+ /**
203
+ * Stringifies an element.
204
+ *
205
+ * @param object
206
+ * @param rule
207
+ * @param state
208
+ */
209
+ stringifyElement(object, rule, state) {
210
+ var _a2;
211
+ if (this.traversed.has(object))
212
+ throw new XmlSchemaError(xmlSchemaErrorLocale.cyclicReferences);
213
+ this.traversed.add(object);
214
+ const nl = this.options.newline;
215
+ const isDocument = state.depth === 0;
216
+ let namespaces = "";
217
+ let xmlns = state.xmlns;
218
+ if (xmlns !== rule.namespaceUri && (xmlns == null || !rule.namespaceUri)) {
219
+ xmlns = rule.namespaceUri;
220
+ namespaces += ` xmlns="${(_a2 = rule.namespaceUri) != null ? _a2 : ""}"`;
221
+ }
222
+ const childState = state.createChild(xmlns, this.options.indent);
223
+ const rules = rule.rules();
224
+ const attributes = this.stringifyAttributes(object, rules.attributes);
225
+ const children = this.stringifyChildren(
226
+ object,
227
+ rules.elements,
228
+ childState
229
+ );
230
+ const text = this.stringifyCharacters(object, rules.characters);
231
+ if (isDocument) {
232
+ this.usedPrefixes.forEach((prefix, uri) => {
233
+ namespaces += ` xmlns:${prefix}="${uri}"`;
234
+ });
235
+ }
236
+ const indent = state.indent;
237
+ const childIndent = childState.indent;
238
+ const qName2 = rule.namespaceUri === xmlns ? rule.localPart : this.qName(rule.localPart, rule.namespaceUri);
239
+ let str = `${indent}<${qName2}${namespaces}${attributes}`;
240
+ if (children) {
241
+ str += `>`;
242
+ if (text) str += `${nl}${childIndent}${text}`;
243
+ str += nl + children;
244
+ str += `${nl}${indent}</${qName2}>`;
245
+ } else if (text) {
246
+ str += `>${text}</${qName2}>`;
247
+ } else {
248
+ str += "/>";
249
+ }
250
+ return str;
251
+ }
252
+ /**
253
+ * Stringifies an element's attributes.
254
+ *
255
+ * @param object
256
+ * @param attributes
257
+ * @return Returns the attributes string. This will include a leading space if there are any
258
+ * found attributes.
259
+ */
260
+ stringifyAttributes(object, attributes) {
261
+ let str = "";
262
+ attributes.forEach((attributeRule) => {
263
+ const propertyValue = object[attributeRule.property];
264
+ if (propertyValue !== void 0) {
265
+ const attributeValue = attributeRule.stringify(propertyValue);
266
+ if (attributeRule.default === void 0 || !(0, import_vinyl_util3.equalDeep)(propertyValue, attributeRule.default)) {
267
+ str += ` ${this.qName(
268
+ attributeRule.localPart,
269
+ attributeRule.namespaceUri
270
+ )}="${(0, import_vinyl_util3.encodeEntities)(attributeValue)}"`;
271
+ }
272
+ } else {
273
+ if (attributeRule.required) {
274
+ throw new XmlSchemaError(
275
+ (0, import_vinyl_util3.substitute)(xmlSchemaErrorLocale.requiredAttribute, {
276
+ property: attributeRule.property
277
+ })
278
+ );
279
+ }
280
+ }
281
+ });
282
+ return str;
283
+ }
284
+ /**
285
+ * Stringifies a property as a CDATA node.
286
+ *
287
+ * @param object
288
+ * @param charactersRule
289
+ */
290
+ stringifyCharacters(object, charactersRule) {
291
+ if (!charactersRule) return null;
292
+ const str = charactersRule.stringify(object[charactersRule.property]);
293
+ if (!str) return "";
294
+ return `<![CDATA[${str}]]>`;
295
+ }
296
+ /**
297
+ * Stringifies an element's child elements.
298
+ *
299
+ * @param object
300
+ * @param elements
301
+ * @param state
302
+ */
303
+ stringifyChildren(object, elements2, state) {
304
+ let str = "";
305
+ elements2.forEach((elementsRule) => {
306
+ const child = object[elementsRule.property];
307
+ if (child) {
308
+ if (elementsRule.array) {
309
+ const arr = child;
310
+ if (elementsRule.minOccurs > arr.length) {
311
+ throw new XmlSchemaError(
312
+ (0, import_vinyl_util3.substitute)(xmlSchemaErrorLocale.atLeastElements, {
313
+ property: elementsRule.property,
314
+ expected: elementsRule.minOccurs,
315
+ actual: arr.length
316
+ })
317
+ );
318
+ }
319
+ if (elementsRule.maxOccurs != null && elementsRule.maxOccurs < arr.length) {
320
+ throw new XmlSchemaError(
321
+ (0, import_vinyl_util3.substitute)(xmlSchemaErrorLocale.atMostElements, {
322
+ property: elementsRule.property,
323
+ expected: elementsRule.maxOccurs,
324
+ actual: arr.length
325
+ })
326
+ );
327
+ }
328
+ for (const element2 of arr) {
329
+ if (str) str += this.options.newline;
330
+ str += this.stringifyElement(
331
+ element2,
332
+ elementsRule,
333
+ state
334
+ );
335
+ }
336
+ } else {
337
+ if (str) str += this.options.newline;
338
+ str += this.stringifyElement(child, elementsRule, state);
339
+ }
340
+ } else {
341
+ if (elementsRule.minOccurs > 0) {
342
+ throw new XmlSchemaError(
343
+ (0, import_vinyl_util3.substitute)(xmlSchemaErrorLocale.atLeastElements, {
344
+ property: elementsRule.property,
345
+ expected: elementsRule.minOccurs,
346
+ actual: 0
347
+ })
348
+ );
349
+ }
350
+ }
351
+ });
352
+ return str;
353
+ }
354
+ };
355
+ var StringifyState = class _StringifyState {
356
+ constructor(depth = 0, xmlns = null, indent = "") {
357
+ this.depth = depth;
358
+ this.xmlns = xmlns;
359
+ this.indent = indent;
360
+ }
361
+ createChild(xmlns, indent) {
362
+ return new _StringifyState(this.depth + 1, xmlns, this.indent + indent);
363
+ }
364
+ };
365
+ function stringifyXml(object, rules, options) {
366
+ const writer = new XmlWriter(rules, options);
367
+ return writer.stringify(object);
368
+ }
369
+
370
+ // src/xml/parseXml.ts
371
+ var import_vinyl_util4 = require("@amazon/vinyl-util");
372
+ var XMLNS = "http://www.w3.org/2000/xmlns/";
373
+ var LT = /* @__PURE__ */ (0, import_vinyl_util4.char)("<");
374
+ var GT = /* @__PURE__ */ (0, import_vinyl_util4.char)(">");
375
+ var EQ = /* @__PURE__ */ (0, import_vinyl_util4.char)("=");
376
+ var SLASH = /* @__PURE__ */ (0, import_vinyl_util4.char)("/");
377
+ var COLON = /* @__PURE__ */ (0, import_vinyl_util4.char)(":");
378
+ var QMARK = /* @__PURE__ */ (0, import_vinyl_util4.char)("?");
379
+ var EXCL = /* @__PURE__ */ (0, import_vinyl_util4.char)("!");
380
+ var APOS = /* @__PURE__ */ (0, import_vinyl_util4.char)("'");
381
+ var QUOT = /* @__PURE__ */ (0, import_vinyl_util4.char)('"');
382
+ var QUOTES = /* @__PURE__ */ new Set([APOS, QUOT]);
383
+ function parseXml(str, handler) {
384
+ const c = new import_vinyl_util4.StringReader(str);
385
+ let current = {
386
+ qName: "#document",
387
+ namespaceMap: /* @__PURE__ */ new Map(),
388
+ defaultNamespaceUri: null
389
+ };
390
+ const stack = [current];
391
+ const namespaceLookup = (prefix) => {
392
+ if (!prefix) return current.defaultNamespaceUri;
393
+ for (let i = stack.length - 1; i >= 0; i--) {
394
+ const map = stack[i].namespaceMap;
395
+ if (map.has(prefix)) return stack[i].namespaceMap.get(prefix);
396
+ }
397
+ return null;
398
+ };
399
+ try {
400
+ handler.startDocument();
401
+ } catch (e) {
402
+ c.err(e.message);
403
+ }
404
+ while (c.hasNext()) {
405
+ const charsStart = c.untilChar(LT);
406
+ if (charsStart < c.position) {
407
+ const chars = str.substring(charsStart, c.position);
408
+ try {
409
+ handler.textNode(chars);
410
+ } catch (e) {
411
+ c.err(e.message);
412
+ }
413
+ }
414
+ if (!c.hasNext()) break;
415
+ c.next();
416
+ if (c.charIf(SLASH)) {
417
+ c.white();
418
+ const name = readQName(c);
419
+ if (name.qName !== current.qName)
420
+ c.err(`Expected </${current.qName}> but was </${name.qName}>`);
421
+ stack.pop();
422
+ current = (0, import_vinyl_util4.last)(stack);
423
+ c.white();
424
+ c.readChar(GT);
425
+ try {
426
+ handler.endElement();
427
+ } catch (e) {
428
+ c.err(e.message);
429
+ }
430
+ } else if (c.charIf(QMARK)) {
431
+ untilTagClose(c);
432
+ } else if (c.charIf(EXCL)) {
433
+ if (c.charIf(
434
+ 45
435
+ /* - */
436
+ )) {
437
+ c.readChar(45);
438
+ c.untilString("-->");
439
+ c.read("-->");
440
+ } else if (c.stringIf("[CDATA[")) {
441
+ const charsStart2 = c.untilString("]]>");
442
+ try {
443
+ handler.cDataNode(str.substring(charsStart2, c.position));
444
+ } catch (e) {
445
+ c.err(e.message);
446
+ }
447
+ c.read("]]>");
448
+ } else {
449
+ untilTagClose(c);
450
+ }
451
+ } else {
452
+ const name = readQName(c);
453
+ current = {
454
+ qName: name.qName,
455
+ namespaceMap: /* @__PURE__ */ new Map(),
456
+ defaultNamespaceUri: current.defaultNamespaceUri
457
+ };
458
+ stack.push(current);
459
+ const attributes = xmlAttributes(c, current);
460
+ name.namespaceUri = namespaceLookup(name.prefix);
461
+ for (const attributesKey in attributes) {
462
+ const value = attributes[attributesKey];
463
+ const name2 = value.name;
464
+ if (name2.prefix && !name2.namespaceUri) {
465
+ name2.namespaceUri = namespaceLookup(name2.prefix);
466
+ }
467
+ }
468
+ try {
469
+ handler.startElement(name, attributes);
470
+ } catch (e) {
471
+ c.err(e.message);
472
+ }
473
+ if (c.charIf(SLASH)) {
474
+ c.readChar(GT);
475
+ stack.pop();
476
+ current = (0, import_vinyl_util4.last)(stack);
477
+ try {
478
+ handler.endElement();
479
+ } catch (e) {
480
+ c.err(e.message);
481
+ }
482
+ } else {
483
+ c.readChar(GT);
484
+ }
485
+ }
486
+ }
487
+ try {
488
+ return handler.endDocument();
489
+ } catch (e) {
490
+ return c.err(e.message);
491
+ }
492
+ }
493
+ function readAttributeValue(c) {
494
+ const quote = c.next();
495
+ if (!QUOTES.has(quote)) c.err(`Expected ' or "`);
496
+ const value = c.substringUntil((charCode) => charCode === quote);
497
+ c.readChar(quote);
498
+ return (0, import_vinyl_util4.decodeEntities)(value);
499
+ }
500
+ function untilTagClose(c) {
501
+ while (true) {
502
+ c.until((c2) => c2 === APOS || c2 === QUOT || c2 === GT);
503
+ const char2 = c.next();
504
+ if (QUOTES.has(char2)) {
505
+ c.untilChar(char2);
506
+ c.next();
507
+ } else {
508
+ break;
509
+ }
510
+ }
511
+ }
512
+ function isNameChar(charCode) {
513
+ if (charCode >= 97) {
514
+ if (charCode >= 183) {
515
+ if (charCode >= 768) {
516
+ if (charCode >= 8255) {
517
+ return charCode <= 8256;
518
+ } else {
519
+ return charCode <= 879;
520
+ }
521
+ } else {
522
+ return charCode === 183;
523
+ }
524
+ } else {
525
+ return charCode <= 122;
526
+ }
527
+ } else {
528
+ if (charCode >= 95) {
529
+ return charCode === 95;
530
+ } else {
531
+ if (charCode >= 65) {
532
+ return charCode <= 90;
533
+ } else {
534
+ if (charCode >= 45) {
535
+ if (charCode >= 48) {
536
+ return charCode <= 57;
537
+ } else {
538
+ return charCode <= 46;
539
+ }
540
+ } else {
541
+ return false;
542
+ }
543
+ }
544
+ }
545
+ }
546
+ }
547
+ function readQName(reader) {
548
+ const prefixOrLocalName = reader.substringWhile(isNameChar);
549
+ let prefix, localPart, name;
550
+ if (reader.charIf(COLON)) {
551
+ prefix = prefixOrLocalName;
552
+ localPart = reader.substringWhile(isNameChar);
553
+ name = `${prefix}:${localPart}`;
554
+ } else {
555
+ prefix = null;
556
+ localPart = prefixOrLocalName;
557
+ name = localPart;
558
+ }
559
+ return { prefix, localPart, qName: name, namespaceUri: null };
560
+ }
561
+ function xmlAttributes(reader, stackElement) {
562
+ const attributes = {};
563
+ while (true) {
564
+ reader.white();
565
+ if (!isNameChar(reader.peek)) break;
566
+ const name = readQName(reader);
567
+ reader.white();
568
+ let value = "";
569
+ if (reader.charIf(EQ)) {
570
+ reader.white();
571
+ value = readAttributeValue(reader);
572
+ } else {
573
+ }
574
+ if (name.prefix === "xmlns") {
575
+ name.namespaceUri = XMLNS;
576
+ stackElement.namespaceMap.set(
577
+ name.localPart,
578
+ value === "" ? null : value
579
+ );
580
+ } else if (!name.prefix && name.localPart === "xmlns") {
581
+ name.namespaceUri = XMLNS;
582
+ stackElement.defaultNamespaceUri = !value ? null : value;
583
+ }
584
+ if (attributes[name.qName] != null)
585
+ reader.err(`Attribute '${name.qName}' is already specified.`);
586
+ attributes[name.qName] = { name, value };
587
+ }
588
+ return attributes;
589
+ }
590
+
591
+ // src/xml/mergeXmlRules.ts
592
+ var import_vinyl_util5 = require("@amazon/vinyl-util");
593
+ var locale2 = {
594
+ illegalRuleOverrideTypeError: "Cannot override the rule {property} of type {sourceType} with a rule of type {overrideType}",
595
+ illegalQNameOverrideError: "Cannot override the rule {property} with a different localPart or namespace"
596
+ };
597
+ function mergeXmlRules(...rules) {
598
+ let finalRule = rules[0];
599
+ for (let i = 1; i < rules.length; i++) {
600
+ finalRule = import_vinyl_util5.plainObjectMergeRule.merge(
601
+ finalRule,
602
+ rules[i],
603
+ mergeXmlRule
604
+ );
605
+ }
606
+ return finalRule;
607
+ }
608
+ function mergeXmlRule(sourceA, sourceB, prop) {
609
+ var _a2, _b, _c, _d;
610
+ if (sourceA == null) return sourceB;
611
+ const sourceAIsLazy = typeof sourceA === "function";
612
+ const sourceBIsLazy = typeof sourceB === "function";
613
+ if (sourceAIsLazy || sourceBIsLazy) {
614
+ return () => {
615
+ const a = sourceAIsLazy ? sourceA() : sourceA;
616
+ const b = sourceBIsLazy ? sourceB() : sourceB;
617
+ return mergeXmlRule(a, b, prop);
618
+ };
619
+ }
620
+ if (sourceA.type !== sourceB.type) {
621
+ throw new import_vinyl_util5.ValidationError(
622
+ (0, import_vinyl_util5.substitute)(locale2.illegalRuleOverrideTypeError, {
623
+ property: prop,
624
+ sourceType: sourceA.type,
625
+ overrideType: sourceB.type
626
+ })
627
+ );
628
+ }
629
+ switch (sourceA.type) {
630
+ case "attribute" /* ATTRIBUTE */:
631
+ case "element" /* ELEMENT */:
632
+ case "elements" /* ELEMENTS */: {
633
+ const b = sourceB;
634
+ if (sourceA.namespaceUri !== b.namespaceUri || ((_a2 = sourceA.localPart) != null ? _a2 : prop) !== ((_b = b.localPart) != null ? _b : prop)) {
635
+ throw new import_vinyl_util5.ValidationError(
636
+ (0, import_vinyl_util5.substitute)(locale2.illegalQNameOverrideError, {
637
+ property: prop
638
+ })
639
+ );
640
+ }
641
+ break;
642
+ }
643
+ }
644
+ switch (sourceA.type) {
645
+ case "attribute" /* ATTRIBUTE */: {
646
+ const b = sourceB;
647
+ return __spreadProps(__spreadValues({}, b), {
648
+ default: (_c = b.default) != null ? _c : sourceA.default
649
+ });
650
+ }
651
+ case "characters" /* CHARACTERS */:
652
+ return sourceB;
653
+ case "element" /* ELEMENT */: {
654
+ const b = sourceB;
655
+ return __spreadProps(__spreadValues({}, b), {
656
+ required: sourceA.required || b.required,
657
+ rules: mergeXmlRules(sourceA.rules, b.rules)
658
+ });
659
+ }
660
+ case "elements" /* ELEMENTS */: {
661
+ const b = sourceB;
662
+ const maxOccurs = sourceA.maxOccurs != null && b.maxOccurs != null ? Math.min(sourceA.maxOccurs, b.maxOccurs) : (_d = sourceA.maxOccurs) != null ? _d : b.maxOccurs;
663
+ return __spreadProps(__spreadValues({}, b), {
664
+ minOccurs: Math.max(sourceA.minOccurs, b.minOccurs),
665
+ maxOccurs,
666
+ useEmptyArrays: sourceA.useEmptyArrays || b.useEmptyArrays,
667
+ rules: mergeXmlRules(sourceA.rules, b.rules)
668
+ });
669
+ }
670
+ }
671
+ }
672
+
673
+ // src/xml/mappedXmlRules.ts
674
+ var import_vinyl_util6 = require("@amazon/vinyl-util");
675
+ var EmptyRules = {
676
+ __type: /* @__PURE__ */ (0, import_vinyl_util6.invariant)(),
677
+ attributes: /* @__PURE__ */ new Map(),
678
+ elements: /* @__PURE__ */ new Map(),
679
+ characters: void 0
680
+ };
681
+ function mapXmlRules(rules, parentNamespaceUri = null) {
682
+ var _a2, _b, _c;
683
+ const attributeRules = /* @__PURE__ */ new Map();
684
+ const elementRules = /* @__PURE__ */ new Map();
685
+ let charactersRule = void 0;
686
+ for (const property in rules) {
687
+ const handlerOrFunction = rules[property];
688
+ let handler;
689
+ if (typeof handlerOrFunction === "function") {
690
+ handler = handlerOrFunction();
691
+ } else {
692
+ handler = handlerOrFunction;
693
+ }
694
+ let key;
695
+ switch (handler.type) {
696
+ case "characters" /* CHARACTERS */:
697
+ if (charactersRule !== void 0)
698
+ throw new import_vinyl_util6.IllegalArgumentError(
699
+ "characters handler already defined"
700
+ );
701
+ charactersRule = {
702
+ property,
703
+ parse: handler.parse,
704
+ stringify: handler.stringify
705
+ };
706
+ break;
707
+ case "element" /* ELEMENT */:
708
+ case "elements" /* ELEMENTS */: {
709
+ const localPart = (_a2 = handler.localPart) != null ? _a2 : property;
710
+ const namespaceUri = handler.namespaceUri === void 0 ? parentNamespaceUri : handler.namespaceUri;
711
+ key = xmlRuleKey(localPart, namespaceUri);
712
+ if (elementRules.has(key)) {
713
+ throw new import_vinyl_util6.IllegalArgumentError(
714
+ `Multiple element rules for ${key}`
715
+ );
716
+ }
717
+ if (handler.type === "element" /* ELEMENT */) {
718
+ const rules2 = handler.rules;
719
+ elementRules.set(key, {
720
+ array: false,
721
+ property,
722
+ minOccurs: handler.required ? 1 : 0,
723
+ maxOccurs: 1,
724
+ rules: (0, import_vinyl_util6.memoize)(() => mapXmlRules(rules2, namespaceUri)),
725
+ useEmptyArrays: false,
726
+ localPart,
727
+ namespaceUri
728
+ });
729
+ } else {
730
+ const rules2 = handler.rules;
731
+ elementRules.set(key, {
732
+ array: true,
733
+ property,
734
+ maxOccurs: handler.maxOccurs,
735
+ minOccurs: handler.minOccurs,
736
+ rules: (0, import_vinyl_util6.memoize)(() => mapXmlRules(rules2, namespaceUri)),
737
+ useEmptyArrays: handler.useEmptyArrays,
738
+ localPart,
739
+ namespaceUri
740
+ });
741
+ }
742
+ break;
743
+ }
744
+ case "attribute" /* ATTRIBUTE */: {
745
+ const namespaceUri = handler.namespaceUri;
746
+ const localPart = (_b = handler.localPart) != null ? _b : property;
747
+ key = xmlRuleKey(localPart, namespaceUri);
748
+ if (attributeRules.has(key)) {
749
+ throw new import_vinyl_util6.IllegalArgumentError(
750
+ `Multiple attribute rules for ${key}`
751
+ );
752
+ }
753
+ attributeRules.set(key, {
754
+ property,
755
+ default: handler.default,
756
+ parse: handler.parse,
757
+ stringify: handler.stringify,
758
+ required: handler.required,
759
+ localPart,
760
+ namespaceUri: (_c = handler.namespaceUri) != null ? _c : null
761
+ });
762
+ break;
763
+ }
764
+ }
765
+ }
766
+ return {
767
+ __type: (0, import_vinyl_util6.invariant)(),
768
+ attributes: attributeRules,
769
+ elements: elementRules,
770
+ characters: charactersRule
771
+ };
772
+ }
773
+ function xmlRuleKey(localPart, namespaceUri = null) {
774
+ return `${namespaceUri != null ? namespaceUri : ""}:${localPart}`;
775
+ }
776
+
777
+ // src/xml/QName.ts
778
+ function qName(localPart, prefix = null, namespaceUri = null) {
779
+ return {
780
+ localPart,
781
+ prefix,
782
+ namespaceUri,
783
+ qName: prefix ? `${prefix}:${localPart}` : localPart
784
+ };
785
+ }
786
+
787
+ // src/xml/ParseXmlHandlerImpl.ts
788
+ var import_vinyl_util7 = require("@amazon/vinyl-util");
789
+ function getRule(name, rules) {
790
+ return rules.get(xmlRuleKey(name.localPart, name.namespaceUri));
791
+ }
792
+ var ParseXmlHandlerImpl = class {
793
+ constructor(rules, options) {
794
+ this.rules = rules;
795
+ __publicField(this, "stack", []);
796
+ __publicField(this, "options");
797
+ __publicField(this, "busy", false);
798
+ this.options = __spreadValues({ validateRules: true }, options);
799
+ }
800
+ startDocument() {
801
+ if (this.busy)
802
+ throw new import_vinyl_util7.IllegalStateError("XML content handler is busy");
803
+ this.busy = true;
804
+ const rules = this.rules;
805
+ this.stack.push({
806
+ element: new XmlElementImpl(),
807
+ rules,
808
+ characters: ""
809
+ });
810
+ }
811
+ startElement(name, attributes) {
812
+ var _a2;
813
+ const parent = (0, import_vinyl_util7.last)(this.stack);
814
+ let element2 = null;
815
+ const elementRule = getRule(name, parent.rules.elements);
816
+ const rules = (_a2 = elementRule == null ? void 0 : elementRule.rules()) != null ? _a2 : EmptyRules;
817
+ if (elementRule) {
818
+ const prop = elementRule.property;
819
+ element2 = new XmlElementImpl(parent.element);
820
+ if (elementRule.array) {
821
+ const arr = parent.element[prop];
822
+ if (Array.isArray(arr)) {
823
+ arr.push(element2);
824
+ } else {
825
+ parent.element[prop] = [element2];
826
+ }
827
+ } else {
828
+ if (parent.element[prop]) {
829
+ throw new XmlSchemaError(
830
+ `Expected single element with name ${name.qName}`
831
+ );
832
+ } else {
833
+ parent.element[prop] = element2;
834
+ }
835
+ }
836
+ const attributeRules = rules.attributes;
837
+ for (const k in attributes) {
838
+ const attribute = attributes[k];
839
+ const name2 = attribute.name;
840
+ const attributeRule = getRule(name2, attributeRules);
841
+ if (attributeRule) {
842
+ element2[attributeRule.property] = attributeRule.parse(
843
+ attribute.value
844
+ );
845
+ }
846
+ }
847
+ attributeRules.forEach((_value, key) => {
848
+ const rule = attributeRules.get(key);
849
+ if (!(rule.property in element2)) {
850
+ if (rule.default != null) {
851
+ element2[rule.property] = rule.default;
852
+ } else if (this.options.validateRules && rule.required) {
853
+ throw new XmlSchemaError(
854
+ (0, import_vinyl_util7.substitute)(xmlSchemaErrorLocale.requiredAttribute, {
855
+ property: rule.property
856
+ })
857
+ );
858
+ }
859
+ }
860
+ });
861
+ }
862
+ this.stack.push({
863
+ element: element2,
864
+ rules,
865
+ characters: ""
866
+ });
867
+ }
868
+ textNode(str) {
869
+ const current = (0, import_vinyl_util7.last)(this.stack);
870
+ if (!current.rules.characters) return;
871
+ current.characters += (0, import_vinyl_util7.decodeEntities)(str.trim());
872
+ }
873
+ cDataNode(str) {
874
+ const current = (0, import_vinyl_util7.last)(this.stack);
875
+ if (!current.rules.characters) return;
876
+ current.characters += str;
877
+ }
878
+ endElement() {
879
+ const lastEl = this.stack.pop();
880
+ const charactersRule = lastEl.rules.characters;
881
+ if (charactersRule)
882
+ lastEl.element[charactersRule.property] = charactersRule.parse(
883
+ lastEl.characters
884
+ );
885
+ lastEl.rules.elements.forEach((rule) => {
886
+ if (rule.array && !(rule.property in lastEl.element)) {
887
+ if (rule.useEmptyArrays) {
888
+ lastEl.element[rule.property] = [];
889
+ }
890
+ }
891
+ });
892
+ this.validate(lastEl);
893
+ }
894
+ endDocument() {
895
+ this.busy = false;
896
+ const last3 = this.stack.pop();
897
+ this.validate(last3);
898
+ return last3.element;
899
+ }
900
+ validate(current) {
901
+ if (!this.options.validateRules) return;
902
+ current.rules.elements.forEach((rule) => {
903
+ var _a2, _b;
904
+ const length = rule.array ? (_b = (_a2 = current.element[rule.property]) == null ? void 0 : _a2.length) != null ? _b : 0 : current.element[rule.property] ? 1 : 0;
905
+ if (length < rule.minOccurs) {
906
+ throw new XmlSchemaError(
907
+ (0, import_vinyl_util7.substitute)(xmlSchemaErrorLocale.atLeastElements, {
908
+ property: rule.property,
909
+ expected: rule.minOccurs,
910
+ actual: length
911
+ })
912
+ );
913
+ }
914
+ if (rule.maxOccurs != null && length > rule.maxOccurs) {
915
+ throw new XmlSchemaError(
916
+ (0, import_vinyl_util7.substitute)(xmlSchemaErrorLocale.atMostElements, {
917
+ property: rule.property,
918
+ expected: rule.maxOccurs,
919
+ actual: length
920
+ })
921
+ );
922
+ }
923
+ });
924
+ }
925
+ };
926
+ function parseXmlHandler(rules, options) {
927
+ return new ParseXmlHandlerImpl(mapXmlRules(rules), options);
928
+ }
929
+ var parentSymbol = /* @__PURE__ */ Symbol("parent");
930
+ var _a;
931
+ _a = parentSymbol;
932
+ var _XmlElementImpl = class _XmlElementImpl {
933
+ constructor(parent = null) {
934
+ __publicField(this, _a);
935
+ this[parentSymbol] = parent;
936
+ }
937
+ get parent() {
938
+ return this[parentSymbol];
939
+ }
940
+ set parent(value) {
941
+ this[parentSymbol] = value;
942
+ }
943
+ clone() {
944
+ const out = new _XmlElementImpl(this.parent);
945
+ for (const key in this) {
946
+ const child = (0, import_vinyl_util7.clone)(this[key]);
947
+ if (Array.isArray(child)) {
948
+ for (const e of child) {
949
+ if (typeof e === "object" && "parent" in e) e.parent = out;
950
+ }
951
+ } else if (typeof child === "object" && "parent" in child) {
952
+ child.parent = out;
953
+ }
954
+ out[key] = child;
955
+ }
956
+ return out;
957
+ }
958
+ toJSON() {
959
+ const out = {};
960
+ for (const key in this) {
961
+ out[key] = (0, import_vinyl_util7.toJson)(this[key]);
962
+ }
963
+ return out;
964
+ }
965
+ };
966
+ var XmlElementImpl = _XmlElementImpl;
967
+
968
+ // src/xlink/actuateValidator.ts
969
+ var import_vinyl_validation = require("@amazon/vinyl-validation");
970
+ var import_vinyl_util8 = require("@amazon/vinyl-util");
971
+ var actuateValidator = /* @__PURE__ */ (0, import_vinyl_validation.isOneOf)("onLoad", "onRequest");
972
+ function assertActuateEnum(str) {
973
+ actuateValidator.assert(str, import_vinyl_util8.ErrorOrigin.PARSING);
974
+ return str;
975
+ }
976
+ //# sourceMappingURL=index.cjs.map