@rgrove/parse-xml 3.0.0 → 4.0.1

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 (103) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +72 -97
  3. package/dist/browser.js +774 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/global.min.js +10 -0
  6. package/dist/global.min.js.map +7 -0
  7. package/dist/index.d.ts +24 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +50 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/lib/Parser.d.ts +218 -0
  12. package/dist/lib/Parser.d.ts.map +1 -0
  13. package/dist/lib/Parser.js +638 -0
  14. package/dist/lib/Parser.js.map +1 -0
  15. package/dist/lib/StringScanner.d.ts +97 -0
  16. package/dist/lib/StringScanner.d.ts.map +1 -0
  17. package/dist/lib/StringScanner.js +210 -0
  18. package/dist/lib/StringScanner.js.map +1 -0
  19. package/dist/lib/XmlCdata.d.ts +8 -0
  20. package/dist/lib/XmlCdata.d.ts.map +1 -0
  21. package/dist/lib/XmlCdata.js +15 -0
  22. package/dist/lib/XmlCdata.js.map +1 -0
  23. package/dist/lib/XmlComment.d.ts +16 -0
  24. package/dist/lib/XmlComment.d.ts.map +1 -0
  25. package/dist/lib/XmlComment.js +23 -0
  26. package/dist/lib/XmlComment.js.map +1 -0
  27. package/dist/lib/XmlDocument.d.ts +29 -0
  28. package/dist/lib/XmlDocument.d.ts.map +1 -0
  29. package/dist/lib/XmlDocument.js +47 -0
  30. package/dist/lib/XmlDocument.js.map +1 -0
  31. package/dist/lib/XmlElement.d.ts +40 -0
  32. package/dist/lib/XmlElement.d.ts.map +1 -0
  33. package/dist/lib/XmlElement.js +51 -0
  34. package/dist/lib/XmlElement.js.map +1 -0
  35. package/dist/lib/XmlNode.d.ts +74 -0
  36. package/dist/lib/XmlNode.d.ts.map +1 -0
  37. package/dist/lib/XmlNode.js +96 -0
  38. package/dist/lib/XmlNode.js.map +1 -0
  39. package/dist/lib/XmlProcessingInstruction.d.ts +22 -0
  40. package/dist/lib/XmlProcessingInstruction.d.ts.map +1 -0
  41. package/dist/lib/XmlProcessingInstruction.js +25 -0
  42. package/dist/lib/XmlProcessingInstruction.js.map +1 -0
  43. package/dist/lib/XmlText.d.ts +16 -0
  44. package/dist/lib/XmlText.d.ts.map +1 -0
  45. package/dist/lib/XmlText.js +23 -0
  46. package/dist/lib/XmlText.js.map +1 -0
  47. package/dist/lib/syntax.d.ts +69 -0
  48. package/dist/lib/syntax.d.ts.map +1 -0
  49. package/dist/lib/syntax.js +133 -0
  50. package/dist/lib/syntax.js.map +1 -0
  51. package/dist/lib/types.d.ts +5 -0
  52. package/dist/lib/types.d.ts.map +1 -0
  53. package/dist/lib/types.js +3 -0
  54. package/dist/lib/types.js.map +1 -0
  55. package/package.json +33 -26
  56. package/src/index.ts +30 -0
  57. package/src/lib/Parser.ts +819 -0
  58. package/src/lib/StringScanner.ts +254 -0
  59. package/src/lib/XmlCdata.ts +11 -0
  60. package/src/lib/XmlComment.ts +26 -0
  61. package/src/lib/XmlDocument.ts +57 -0
  62. package/src/lib/XmlElement.ts +81 -0
  63. package/src/lib/XmlNode.ts +107 -0
  64. package/src/lib/XmlProcessingInstruction.ts +35 -0
  65. package/src/lib/XmlText.ts +26 -0
  66. package/src/lib/syntax.ts +136 -0
  67. package/src/lib/types.ts +2 -0
  68. package/CHANGELOG.md +0 -162
  69. package/dist/types/index.d.ts +0 -68
  70. package/dist/types/index.d.ts.map +0 -1
  71. package/dist/types/lib/Parser.d.ts +0 -234
  72. package/dist/types/lib/Parser.d.ts.map +0 -1
  73. package/dist/types/lib/StringScanner.d.ts +0 -139
  74. package/dist/types/lib/StringScanner.d.ts.map +0 -1
  75. package/dist/types/lib/XmlCdata.d.ts +0 -11
  76. package/dist/types/lib/XmlCdata.d.ts.map +0 -1
  77. package/dist/types/lib/XmlComment.d.ts +0 -21
  78. package/dist/types/lib/XmlComment.d.ts.map +0 -1
  79. package/dist/types/lib/XmlDocument.d.ts +0 -42
  80. package/dist/types/lib/XmlDocument.d.ts.map +0 -1
  81. package/dist/types/lib/XmlElement.d.ts +0 -62
  82. package/dist/types/lib/XmlElement.d.ts.map +0 -1
  83. package/dist/types/lib/XmlNode.d.ts +0 -78
  84. package/dist/types/lib/XmlNode.d.ts.map +0 -1
  85. package/dist/types/lib/XmlProcessingInstruction.d.ts +0 -30
  86. package/dist/types/lib/XmlProcessingInstruction.d.ts.map +0 -1
  87. package/dist/types/lib/XmlText.d.ts +0 -21
  88. package/dist/types/lib/XmlText.d.ts.map +0 -1
  89. package/dist/types/lib/syntax.d.ts +0 -59
  90. package/dist/types/lib/syntax.d.ts.map +0 -1
  91. package/dist/umd/parse-xml.min.js +0 -2
  92. package/dist/umd/parse-xml.min.js.map +0 -1
  93. package/src/index.js +0 -67
  94. package/src/lib/Parser.js +0 -812
  95. package/src/lib/StringScanner.js +0 -312
  96. package/src/lib/XmlCdata.js +0 -17
  97. package/src/lib/XmlComment.js +0 -37
  98. package/src/lib/XmlDocument.js +0 -69
  99. package/src/lib/XmlElement.js +0 -101
  100. package/src/lib/XmlNode.js +0 -152
  101. package/src/lib/XmlProcessingInstruction.js +0 -48
  102. package/src/lib/XmlText.js +0 -37
  103. package/src/lib/syntax.js +0 -153
@@ -0,0 +1,218 @@
1
+ import { XmlDocument } from './XmlDocument.js';
2
+ import type { XmlNode } from './XmlNode.js';
3
+ /**
4
+ * Parses an XML string into an `XmlDocument`.
5
+ *
6
+ * @private
7
+ */
8
+ export declare class Parser {
9
+ readonly document: XmlDocument;
10
+ private currentNode;
11
+ private readonly options;
12
+ private readonly scanner;
13
+ /**
14
+ * @param xml XML string to parse.
15
+ * @param options Parser options.
16
+ */
17
+ constructor(xml: string, options?: ParserOptions);
18
+ /**
19
+ * Adds the given `XmlNode` as a child of `this.currentNode`.
20
+ */
21
+ addNode(node: XmlNode): void;
22
+ /**
23
+ * Adds the given _text_ to the document, either by appending it to a
24
+ * preceding `XmlText` node (if possible) or by creating a new `XmlText` node.
25
+ */
26
+ addText(text: string): void;
27
+ /**
28
+ * Consumes element attributes.
29
+ *
30
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-starttags
31
+ */
32
+ consumeAttributes(): Record<string, string>;
33
+ /**
34
+ * Consumes an `AttValue` (attribute value) if possible.
35
+ *
36
+ * @returns
37
+ * Contents of the `AttValue` minus quotes, or `false` if nothing was
38
+ * consumed. An empty string indicates that an `AttValue` was consumed but
39
+ * was empty.
40
+ *
41
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-AttValue
42
+ */
43
+ consumeAttributeValue(): string | false;
44
+ /**
45
+ * Consumes a CDATA section if possible.
46
+ *
47
+ * @returns Whether a CDATA section was consumed.
48
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-cdata-sect
49
+ */
50
+ consumeCdataSection(): boolean;
51
+ /**
52
+ * Consumes character data if possible.
53
+ *
54
+ * @returns Whether character data was consumed.
55
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#dt-chardata
56
+ */
57
+ consumeCharData(): boolean;
58
+ /**
59
+ * Consumes a comment if possible.
60
+ *
61
+ * @returns Whether a comment was consumed.
62
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Comment
63
+ */
64
+ consumeComment(): boolean;
65
+ /**
66
+ * Consumes a reference in a content context if possible.
67
+ *
68
+ * This differs from `consumeReference()` in that a consumed reference will be
69
+ * added to the document as a text node instead of returned.
70
+ *
71
+ * @returns Whether a reference was consumed.
72
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#entproc
73
+ */
74
+ consumeContentReference(): boolean;
75
+ /**
76
+ * Consumes a doctype declaration if possible.
77
+ *
78
+ * This is a loose implementation since doctype declarations are currently
79
+ * discarded without further parsing.
80
+ *
81
+ * @returns Whether a doctype declaration was consumed.
82
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#dtd
83
+ */
84
+ consumeDoctypeDeclaration(): boolean;
85
+ /**
86
+ * Consumes an element if possible.
87
+ *
88
+ * @returns Whether an element was consumed.
89
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-element
90
+ */
91
+ consumeElement(): boolean;
92
+ /**
93
+ * Consumes an `Eq` production if possible.
94
+ *
95
+ * @returns Whether an `Eq` production was consumed.
96
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Eq
97
+ */
98
+ consumeEqual(): boolean;
99
+ /**
100
+ * Consumes `Misc` content if possible.
101
+ *
102
+ * @returns Whether anything was consumed.
103
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Misc
104
+ */
105
+ consumeMisc(): boolean;
106
+ /**
107
+ * Consumes one or more `Name` characters if possible.
108
+ *
109
+ * @returns `Name` characters, or an empty string if none were consumed.
110
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name
111
+ */
112
+ consumeName(): string;
113
+ /**
114
+ * Consumes a processing instruction if possible.
115
+ *
116
+ * @returns Whether a processing instruction was consumed.
117
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-pi
118
+ */
119
+ consumeProcessingInstruction(): boolean;
120
+ /**
121
+ * Consumes a prolog if possible.
122
+ *
123
+ * @returns Whether a prolog was consumed.
124
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-prolog-dtd
125
+ */
126
+ consumeProlog(): boolean;
127
+ /**
128
+ * Consumes a reference if possible.
129
+ *
130
+ * This differs from `consumeContentReference()` in that a consumed reference
131
+ * will be returned rather than added to the document.
132
+ *
133
+ * @returns
134
+ * Parsed reference value, or `false` if nothing was consumed (to
135
+ * distinguish from a reference that resolves to an empty string).
136
+ *
137
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Reference
138
+ */
139
+ consumeReference(): string | false;
140
+ /**
141
+ * Consumes a `SystemLiteral` if possible.
142
+ *
143
+ * A `SystemLiteral` is similar to an attribute value, but allows the
144
+ * characters `<` and `&` and doesn't replace references.
145
+ *
146
+ * @returns
147
+ * Value of the `SystemLiteral` minus quotes, or `false` if nothing was
148
+ * consumed. An empty string indicates that a `SystemLiteral` was consumed
149
+ * but was empty.
150
+ *
151
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-SystemLiteral
152
+ */
153
+ consumeSystemLiteral(): string | false;
154
+ /**
155
+ * Consumes one or more whitespace characters if possible.
156
+ *
157
+ * @returns Whether any whitespace characters were consumed.
158
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#white
159
+ */
160
+ consumeWhitespace(): boolean;
161
+ /**
162
+ * Consumes an XML declaration if possible.
163
+ *
164
+ * @returns Whether an XML declaration was consumed.
165
+ * @see https://www.w3.org/TR/2008/REC-xml-20081126/#NT-XMLDecl
166
+ */
167
+ consumeXmlDeclaration(): boolean;
168
+ /**
169
+ * Throws an error at the current scanner position.
170
+ */
171
+ error(message: string): Error;
172
+ /**
173
+ * Throws an invalid character error if any character in the given _string_
174
+ * isn't a valid XML character.
175
+ */
176
+ validateChars(string: string): void;
177
+ }
178
+ export declare type ParserOptions = {
179
+ /**
180
+ * When `true`, an undefined named entity (like "&bogus;") will be left in the
181
+ * output as is instead of causing a parse error.
182
+ *
183
+ * @default false
184
+ */
185
+ ignoreUndefinedEntities?: boolean;
186
+ /**
187
+ * When `true`, CDATA sections will be preserved in the document as `XmlCdata`
188
+ * nodes. Otherwise CDATA sections will be represented as `XmlText` nodes,
189
+ * which keeps the node tree simpler and easier to work with.
190
+ *
191
+ * @default false
192
+ */
193
+ preserveCdata?: boolean;
194
+ /**
195
+ * When `true`, comments will be preserved in the document as `XmlComment`
196
+ * nodes. Otherwise comments will not be included in the node tree.
197
+ *
198
+ * @default false
199
+ */
200
+ preserveComments?: boolean;
201
+ /**
202
+ * When an undefined named entity is encountered, this function will be called
203
+ * with the entity as its only argument. It should return a string value with
204
+ * which to replace the entity, or `null` or `undefined` to treat the entity
205
+ * as undefined (which may result in a parse error depending on the value of
206
+ * `ignoreUndefinedEntities`).
207
+ */
208
+ resolveUndefinedEntity?: (entity: string) => string | null | undefined;
209
+ /**
210
+ * When `true`, attributes in an element's `attributes` object will be sorted
211
+ * in alphanumeric order by name. Otherwise they'll retain their original
212
+ * order as found in the XML.
213
+ *
214
+ * @default false
215
+ */
216
+ sortAttributes?: boolean;
217
+ };
218
+ //# sourceMappingURL=Parser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Parser.d.ts","sourceRoot":"","sources":["../../src/lib/Parser.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAK/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C;;;;GAIG;AACH,qBAAa,MAAM;IACjB,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAE/B,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IAExC;;;OAGG;gBACS,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB;IAmBpD;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,OAAO;IASrB;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM;IAkBpB;;;;OAIG;IACH,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IA6C3C;;;;;;;;;OASG;IACH,qBAAqB,IAAI,MAAM,GAAG,KAAK;IAkDvC;;;;;OAKG;IACH,mBAAmB,IAAI,OAAO;IAuB9B;;;;;OAKG;IACH,eAAe,IAAI,OAAO;IAkB1B;;;;;OAKG;IACH,cAAc,IAAI,OAAO;IAyBzB;;;;;;;;OAQG;IACH,uBAAuB,IAAI,OAAO;IAWlC;;;;;;;;OAQG;IACH,yBAAyB,IAAI,OAAO;IAsBpC;;;;;OAKG;IACH,cAAc,IAAI,OAAO;IA8DzB;;;;;OAKG;IACH,YAAY,IAAI,OAAO;IAWvB;;;;;OAKG;IACH,WAAW,IAAI,OAAO;IAMtB;;;;;OAKG;IACH,WAAW,IAAI,MAAM;IAMrB;;;;;OAKG;IACH,4BAA4B,IAAI,OAAO;IAuCvC;;;;;OAKG;IACH,aAAa,IAAI,OAAO;IAexB;;;;;;;;;;;OAWG;IACH,gBAAgB,IAAI,MAAM,GAAG,KAAK;IAoElC;;;;;;;;;;;;OAYG;IACH,oBAAoB,IAAI,MAAM,GAAG,KAAK;IAkBtC;;;;;OAKG;IACH,iBAAiB,IAAI,OAAO;IAI5B;;;;;OAKG;IACH,qBAAqB,IAAI,OAAO;IAkDhC;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM;IAuDrB;;;OAGG;IACH,aAAa,CAAC,MAAM,EAAE,MAAM;CAgB7B;AAiBD,oBAAY,aAAa,GAAG;IAC1B;;;;;OAKG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAElC;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAEvE;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC"}