@rgrove/parse-xml 3.0.0 → 4.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 (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 +30 -22
  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
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  ISC License
2
2
 
3
- Copyright (c) 2021 Ryan Grove <ryan@wonko.com>
3
+ Copyright Ryan Grove <ryan@wonko.com>
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any purpose
6
6
  with or without fee is hereby granted, provided that the above copyright notice
package/README.md CHANGED
@@ -2,22 +2,13 @@
2
2
 
3
3
  A fast, safe, compliant XML parser for Node.js and browsers.
4
4
 
5
- [![npm version](https://badge.fury.io/js/%40rgrove%2Fparse-xml.svg)](https://badge.fury.io/js/%40rgrove%2Fparse-xml)
6
- [![Bundle size](https://badgen.net/bundlephobia/minzip/@rgrove/parse-xml)](https://bundlephobia.com/result?p=@rgrove/parse-xml)
7
- [![Test & Lint](https://github.com/rgrove/parse-xml/workflows/Test%20&%20Lint/badge.svg)](https://github.com/rgrove/parse-xml/actions?query=workflow%3A%22Test+%26+Lint%22)
8
-
9
- ## Contents
10
-
11
- - [Installation](#installation)
12
- - [Features](#features)
13
- - [Not Features](#not-features)
14
- - [API](#api)
15
- - [Examples](#examples)
16
- - [Basic Usage](#basic-usage)
17
- - [Friendly Errors](#friendly-errors)
18
- - [Why another XML parser?](#why-another-xml-parser)
19
- - [Benchmark](#benchmark)
20
- - [License](#license)
5
+ [![npm version](https://badge.fury.io/js/%40rgrove%2Fparse-xml.svg)](https://badge.fury.io/js/%40rgrove%2Fparse-xml) [![Bundle size](https://badgen.net/bundlephobia/minzip/@rgrove/parse-xml)](https://bundlephobia.com/result?p=@rgrove/parse-xml) [![CI](https://github.com/rgrove/parse-xml/actions/workflows/ci.yml/badge.svg)](https://github.com/rgrove/parse-xml/actions/workflows/ci.yml)
6
+
7
+ ## Links
8
+
9
+ - [API Docs](https://rgrove.github.io/parse-xml/)
10
+ - [GitHub](https://github.com/rgrove/parse-xml)
11
+ - [npm](https://www.npmjs.com/package/@rgrove/parse-xml)
21
12
 
22
13
  ## Installation
23
14
 
@@ -25,54 +16,51 @@ A fast, safe, compliant XML parser for Node.js and browsers.
25
16
  npm install @rgrove/parse-xml
26
17
  ```
27
18
 
28
- Or, if you like living dangerously, you can load [the minified UMD bundle][umd]
29
- in a browser via [Unpkg] and use the `parseXml` global.
30
-
31
- [umd]:https://unpkg.com/@rgrove/parse-xml/dist/umd/parse-xml.min.js
32
- [Unpkg]:https://unpkg.com/
19
+ Or, if you like living dangerously, you can load [the minified bundle](https://unpkg.com/@rgrove/parse-xml/dist/global.min.js) in a browser via [Unpkg](https://unpkg.com/) and use the `parseXml` global.
33
20
 
34
21
  ## Features
35
22
 
36
- - Returns an [object tree](#basic-usage) representing an XML document.
23
+ - Returns a convenient [object tree](#basic-usage) representing an XML document.
37
24
 
38
- - Works great in Node.js and in modern browsers.
25
+ - Works great in Node.js and browsers.
39
26
 
40
- - Provides [helpful, detailed error messages](#friendly-errors) with context
41
- when a document is not well-formed.
27
+ - Provides [helpful, detailed error messages](#friendly-errors) with context when a document is not well-formed.
42
28
 
43
- - Mostly conforms to [XML 1.0 (Fifth Edition)](https://www.w3.org/TR/2008/REC-xml-20081126/)
44
- as a non-validating parser (see [below](#not-features) for details).
29
+ - Mostly conforms to [XML 1.0 (Fifth Edition)](https://www.w3.org/TR/2008/REC-xml-20081126/) as a non-validating parser (see [below](#not-features) for details).
45
30
 
46
31
  - Passes all relevant tests in the [XML Conformance Test Suite](https://www.w3.org/XML/Test/).
47
32
 
48
- - It's [fast](#benchmark), [small](https://bundlephobia.com/result?p=@rgrove/parse-xml),
49
- and has no dependencies.
33
+ - Written in TypeScript and compiled to ES2020 JavaScript for Node.js and ES2017 JavaScript for browsers. The browser build is also optimized for minification.
50
34
 
51
- ## Not Features
35
+ - Extremely [fast](#benchmark) and surprisingly [small](https://bundlephobia.com/result?p=@rgrove/parse-xml).
52
36
 
53
- This parser currently discards document type declarations (`<!DOCTYPE ... >`)
54
- and all their contents, because they're rarely useful and some of their features
55
- aren't safe when the XML being parsed comes from an untrusted source.
37
+ - Zero dependencies.
56
38
 
57
- In addition, the only supported character encoding is UTF-8 because it's not
58
- feasible (or useful) to suppport other character encodings in JavaScript.
39
+ ## Not Features
59
40
 
60
- ## API
41
+ This parser currently discards document type declarations (`<!DOCTYPE ... >`) and all their contents, because they're rarely useful and some of their features aren't safe when the XML being parsed comes from an untrusted source.
61
42
 
62
- See [API.md](API.md) for complete API docs.
43
+ In addition, the only supported character encoding is UTF-8 because it's not feasible (or useful) to support other character encodings in JavaScript.
63
44
 
64
45
  ## Examples
65
46
 
66
47
  ### Basic Usage
67
48
 
49
+ **ESM**
50
+
68
51
  ```js
69
- const parseXml = require('@rgrove/parse-xml');
70
- let doc = parseXml('<kittens fuzzy="yes">I like fuzzy kittens.</kittens>');
52
+ import { parseXml } from '@rgrove/parse-xml';
53
+ parseXml('<kittens fuzzy="yes">I like fuzzy kittens.</kittens>');
71
54
  ```
72
55
 
73
- The result is an [`XmlDocument`] instance containing the parsed document, with a
74
- structure that looks like this (some properties and methods are excluded for
75
- clarity; see the [API docs](API.md) for details):
56
+ **CommonJS**
57
+
58
+ ```js
59
+ const { parseXml } = require('@rgrove/parse-xml');
60
+ parseXml('<kittens fuzzy="yes">I like fuzzy kittens.</kittens>');
61
+ ```
62
+
63
+ The result is an [`XmlDocument`](https://rgrove.github.io/parse-xml/classes/XmlDocument.html) instance containing the parsed document, with a structure that looks like this (some properties and methods are excluded for clarity; see the [API docs](https://rgrove.github.io/parse-xml/) for details):
76
64
 
77
65
  ```js
78
66
  {
@@ -97,12 +85,15 @@ clarity; see the [API docs](API.md) for details):
97
85
  }
98
86
  ```
99
87
 
100
- [`XmlDocument`]:API.md#xmldocument
88
+ All parse-xml objects have `toJSON()` methods that return JSON-serializable objects, so you can easily convert an XML document to JSON:
89
+
90
+ ```js
91
+ let json = JSON.stringify(parseXml(xml));
92
+ ```
101
93
 
102
94
  ### Friendly Errors
103
95
 
104
- When something goes wrong, parse-xml throws an error that tells you exactly what
105
- happened and shows you where the problem is so you can fix it.
96
+ When something goes wrong, parse-xml throws an error that tells you exactly what happened and shows you where the problem is so you can fix it.
106
97
 
107
98
  ```js
108
99
  parseXml('<foo><bar>baz</foo>');
@@ -132,106 +123,90 @@ In addition to a helpful message, error objects have the following properties:
132
123
 
133
124
  - **pos** _Number_
134
125
 
135
- Character position where the error occurred relative to the beginning of the
136
- input (0-based).
126
+ Character position where the error occurred relative to the beginning of the input (0-based).
137
127
 
138
128
  ## Why another XML parser?
139
129
 
140
- There are many XML parsers for Node, and some of them are good. However, most of
141
- them suffer from one or more of the following shortcomings:
130
+ There are many XML parsers for Node, and some of them are good. However, most of them suffer from one or more of the following shortcomings:
142
131
 
143
132
  - Native dependencies.
144
133
 
145
- - Loose, non-standard parsing behavior that can lead to unexpected or even
146
- unsafe results when given input the author didn't anticipate.
134
+ - Loose, non-standard parsing behavior that can lead to unexpected or even unsafe results when given input the author didn't anticipate.
147
135
 
148
- - Kitchen sink APIs that tightly couple a parser with DOM manipulation
149
- functions, a stringifier, or other tooling that isn't directly related to
150
- parsing and consuming XML.
136
+ - Kitchen sink APIs that tightly couple a parser with DOM manipulation functions, a stringifier, or other tooling that isn't directly related to parsing and consuming XML.
151
137
 
152
- - Stream-based parsing. This is great in the rare case that you need to parse
153
- truly enormous documents, but can be a pain to work with when all you want
154
- is a node tree.
138
+ - Stream-based parsing. This is great in the rare case that you need to parse truly enormous documents, but can be a pain to work with when all you want is a node tree.
155
139
 
156
140
  - Poor error handling.
157
141
 
158
142
  - Too big or too Node-specific to work well in browsers.
159
143
 
160
- parse-xml's goal is to be a small, fast, safe, compliant, non-streaming,
161
- non-validating, browser-friendly parser, because I think this is an under-served
162
- niche.
144
+ parse-xml's goal is to be a small, fast, safe, compliant, non-streaming, non-validating, browser-friendly parser, because I think this is an under-served niche.
163
145
 
164
- I think parse-xml demonstrates that it's not necessary to jettison the spec
165
- entirely or to write complex code in order to implement a small, fast XML
166
- parser.
146
+ I think parse-xml demonstrates that it's not necessary to jettison the spec entirely or to write complex code in order to implement a small, fast XML parser.
167
147
 
168
148
  Also, it was fun.
169
149
 
170
150
  ## Benchmark
171
151
 
172
- Here's how parse-xml stacks up against two comparable libraries, [libxmljs2]
173
- (which is based on the native libxml library) and [xmldoc] (which is based on
174
- [sax-js]).
152
+ Here's how parse-xml stacks up against two comparable libraries, [libxmljs2](https://github.com/marudor/libxmljs2) (which is based on the native libxml library) and [xmldoc](https://github.com/nfarina/xmldoc) (which is based on [sax-js](https://github.com/isaacs/sax-js)).
175
153
 
176
- [libxmljs2]:https://github.com/marudor/libxmljs2
177
- [sax-js]:https://github.com/isaacs/sax-js
178
- [xmldoc]:https://github.com/nfarina/xmldoc
154
+ While libxmljs2 is faster at parsing medium and large documents, its performance comes at the expense of a large C dependency, no browser support, and a [history of security vulnerabilities](https://www.cvedetails.com/vulnerability-list/vendor_id-1962/product_id-3311/Xmlsoft-Libxml2.html) in the underlying libxml2 library.
179
155
 
180
156
  ```
181
- Node.js v14.15.4 / Darwin x64
182
- Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz
157
+ Node.js v18.9.1 / Darwin arm64
158
+ Apple M1 Max
183
159
 
184
160
  Running "Small document (291 bytes)" suite...
185
161
  Progress: 100%
186
162
 
187
- @rgrove/parse-xml 3.0.0:
188
- 77 109 ops/s, ±0.46% | fastest
163
+ @rgrove/parse-xml 4.0.0:
164
+ 189 074 ops/s, ±0.10% | fastest
189
165
 
190
- libxmljs2 0.26.6 (native):
191
- 29 480 ops/s, ±4.62% | slowest, 61.77% slower
166
+ libxmljs2 0.30.1 (native):
167
+ 74 006 ops/s, ±0.32% | 60.86% slower
192
168
 
193
- xmldoc 1.1.2 (sax-js):
194
- 36 035 ops/s, ±0.62% | 53.27% slower
169
+ xmldoc 1.2.0 (sax-js):
170
+ 68 045 ops/s, ±0.08% | slowest, 64.01% slower
195
171
 
196
172
  Finished 3 cases!
197
- Fastest: @rgrove/parse-xml 3.0.0
198
- Slowest: libxmljs2 0.26.6 (native)
173
+ Fastest: @rgrove/parse-xml 4.0.0
174
+ Slowest: xmldoc 1.2.0 (sax-js)
199
175
 
200
176
  Running "Medium document (72081 bytes)" suite...
201
177
  Progress: 100%
202
178
 
203
- @rgrove/parse-xml 3.0.0:
204
- 321 ops/s, ±0.99% | 54.34% slower
179
+ @rgrove/parse-xml 4.0.0:
180
+ 1 066 ops/s, ±0.11% | 49.12% slower
205
181
 
206
- libxmljs2 0.26.6 (native):
207
- 703 ops/s, ±10.64% | fastest
182
+ libxmljs2 0.30.1 (native):
183
+ 2 095 ops/s, ±2.68% | fastest
208
184
 
209
- xmldoc 1.1.2 (sax-js):
210
- 235 ops/s, ±0.50% | slowest, 66.57% slower
185
+ xmldoc 1.2.0 (sax-js):
186
+ 459 ops/s, ±0.10% | slowest, 78.09% slower
211
187
 
212
188
  Finished 3 cases!
213
- Fastest: libxmljs2 0.26.6 (native)
214
- Slowest: xmldoc 1.1.2 (sax-js)
189
+ Fastest: libxmljs2 0.30.1 (native)
190
+ Slowest: xmldoc 1.2.0 (sax-js)
215
191
 
216
192
  Running "Large document (1162464 bytes)" suite...
217
193
  Progress: 100%
218
194
 
219
- @rgrove/parse-xml 3.0.0:
220
- 20 ops/s, ±0.48% | 72.97% slower
195
+ @rgrove/parse-xml 4.0.0:
196
+ 91 ops/s, ±0.11% | 51.85% slower
221
197
 
222
- libxmljs2 0.26.6 (native):
223
- 74 ops/s, ±12.02% | fastest
198
+ libxmljs2 0.30.1 (native):
199
+ 189 ops/s, ±0.99% | fastest
224
200
 
225
- xmldoc 1.1.2 (sax-js):
226
- 19 ops/s, ±1.68% | slowest, 74.32% slower
201
+ xmldoc 1.2.0 (sax-js):
202
+ 39 ops/s, ±0.08% | slowest, 79.37% slower
227
203
 
228
204
  Finished 3 cases!
229
- Fastest: libxmljs2 0.26.6 (native)
230
- Slowest: xmldoc 1.1.2 (sax-js)
205
+ Fastest: libxmljs2 0.30.1 (native)
206
+ Slowest: xmldoc 1.2.0 (sax-js)
231
207
  ```
232
208
 
233
- See the [parse-xml-benchmark](https://github.com/rgrove/parse-xml-benchmark)
234
- repo for instructions on running this benchmark yourself.
209
+ See the [parse-xml-benchmark](https://github.com/rgrove/parse-xml-benchmark) repo for instructions on running this benchmark yourself.
235
210
 
236
211
  ## License
237
212