@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.
- package/LICENSE +1 -1
- package/README.md +72 -97
- package/dist/browser.js +774 -0
- package/dist/browser.js.map +7 -0
- package/dist/global.min.js +10 -0
- package/dist/global.min.js.map +7 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/Parser.d.ts +218 -0
- package/dist/lib/Parser.d.ts.map +1 -0
- package/dist/lib/Parser.js +638 -0
- package/dist/lib/Parser.js.map +1 -0
- package/dist/lib/StringScanner.d.ts +97 -0
- package/dist/lib/StringScanner.d.ts.map +1 -0
- package/dist/lib/StringScanner.js +210 -0
- package/dist/lib/StringScanner.js.map +1 -0
- package/dist/lib/XmlCdata.d.ts +8 -0
- package/dist/lib/XmlCdata.d.ts.map +1 -0
- package/dist/lib/XmlCdata.js +15 -0
- package/dist/lib/XmlCdata.js.map +1 -0
- package/dist/lib/XmlComment.d.ts +16 -0
- package/dist/lib/XmlComment.d.ts.map +1 -0
- package/dist/lib/XmlComment.js +23 -0
- package/dist/lib/XmlComment.js.map +1 -0
- package/dist/lib/XmlDocument.d.ts +29 -0
- package/dist/lib/XmlDocument.d.ts.map +1 -0
- package/dist/lib/XmlDocument.js +47 -0
- package/dist/lib/XmlDocument.js.map +1 -0
- package/dist/lib/XmlElement.d.ts +40 -0
- package/dist/lib/XmlElement.d.ts.map +1 -0
- package/dist/lib/XmlElement.js +51 -0
- package/dist/lib/XmlElement.js.map +1 -0
- package/dist/lib/XmlNode.d.ts +74 -0
- package/dist/lib/XmlNode.d.ts.map +1 -0
- package/dist/lib/XmlNode.js +96 -0
- package/dist/lib/XmlNode.js.map +1 -0
- package/dist/lib/XmlProcessingInstruction.d.ts +22 -0
- package/dist/lib/XmlProcessingInstruction.d.ts.map +1 -0
- package/dist/lib/XmlProcessingInstruction.js +25 -0
- package/dist/lib/XmlProcessingInstruction.js.map +1 -0
- package/dist/lib/XmlText.d.ts +16 -0
- package/dist/lib/XmlText.d.ts.map +1 -0
- package/dist/lib/XmlText.js +23 -0
- package/dist/lib/XmlText.js.map +1 -0
- package/dist/lib/syntax.d.ts +69 -0
- package/dist/lib/syntax.d.ts.map +1 -0
- package/dist/lib/syntax.js +133 -0
- package/dist/lib/syntax.js.map +1 -0
- package/dist/lib/types.d.ts +5 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +3 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +33 -26
- package/src/index.ts +30 -0
- package/src/lib/Parser.ts +819 -0
- package/src/lib/StringScanner.ts +254 -0
- package/src/lib/XmlCdata.ts +11 -0
- package/src/lib/XmlComment.ts +26 -0
- package/src/lib/XmlDocument.ts +57 -0
- package/src/lib/XmlElement.ts +81 -0
- package/src/lib/XmlNode.ts +107 -0
- package/src/lib/XmlProcessingInstruction.ts +35 -0
- package/src/lib/XmlText.ts +26 -0
- package/src/lib/syntax.ts +136 -0
- package/src/lib/types.ts +2 -0
- package/CHANGELOG.md +0 -162
- package/dist/types/index.d.ts +0 -68
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/lib/Parser.d.ts +0 -234
- package/dist/types/lib/Parser.d.ts.map +0 -1
- package/dist/types/lib/StringScanner.d.ts +0 -139
- package/dist/types/lib/StringScanner.d.ts.map +0 -1
- package/dist/types/lib/XmlCdata.d.ts +0 -11
- package/dist/types/lib/XmlCdata.d.ts.map +0 -1
- package/dist/types/lib/XmlComment.d.ts +0 -21
- package/dist/types/lib/XmlComment.d.ts.map +0 -1
- package/dist/types/lib/XmlDocument.d.ts +0 -42
- package/dist/types/lib/XmlDocument.d.ts.map +0 -1
- package/dist/types/lib/XmlElement.d.ts +0 -62
- package/dist/types/lib/XmlElement.d.ts.map +0 -1
- package/dist/types/lib/XmlNode.d.ts +0 -78
- package/dist/types/lib/XmlNode.d.ts.map +0 -1
- package/dist/types/lib/XmlProcessingInstruction.d.ts +0 -30
- package/dist/types/lib/XmlProcessingInstruction.d.ts.map +0 -1
- package/dist/types/lib/XmlText.d.ts +0 -21
- package/dist/types/lib/XmlText.d.ts.map +0 -1
- package/dist/types/lib/syntax.d.ts +0 -59
- package/dist/types/lib/syntax.d.ts.map +0 -1
- package/dist/umd/parse-xml.min.js +0 -2
- package/dist/umd/parse-xml.min.js.map +0 -1
- package/src/index.js +0 -67
- package/src/lib/Parser.js +0 -812
- package/src/lib/StringScanner.js +0 -312
- package/src/lib/XmlCdata.js +0 -17
- package/src/lib/XmlComment.js +0 -37
- package/src/lib/XmlDocument.js +0 -69
- package/src/lib/XmlElement.js +0 -101
- package/src/lib/XmlNode.js +0 -152
- package/src/lib/XmlProcessingInstruction.js +0 -48
- package/src/lib/XmlText.js +0 -37
- package/src/lib/syntax.js +0 -153
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
ISC License
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
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
|
-
[](https://badge.fury.io/js/%40rgrove%2Fparse-xml)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
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
|
+
[](https://badge.fury.io/js/%40rgrove%2Fparse-xml) [](https://bundlephobia.com/result?p=@rgrove/parse-xml) [](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
|
|
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
|
|
23
|
+
- Returns a convenient [object tree](#basic-usage) representing an XML document.
|
|
37
24
|
|
|
38
|
-
- Works great in Node.js and
|
|
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
|
-
-
|
|
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
|
-
|
|
35
|
+
- Extremely [fast](#benchmark) and surprisingly [small](https://bundlephobia.com/result?p=@rgrove/parse-xml).
|
|
52
36
|
|
|
53
|
-
|
|
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
|
-
|
|
58
|
-
feasible (or useful) to suppport other character encodings in JavaScript.
|
|
39
|
+
## Not Features
|
|
59
40
|
|
|
60
|
-
|
|
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
|
-
|
|
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
|
-
|
|
70
|
-
|
|
52
|
+
import { parseXml } from '@rgrove/parse-xml';
|
|
53
|
+
parseXml('<kittens fuzzy="yes">I like fuzzy kittens.</kittens>');
|
|
71
54
|
```
|
|
72
55
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
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
|
-
[
|
|
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
|
|
182
|
-
|
|
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
|
|
188
|
-
|
|
163
|
+
@rgrove/parse-xml 4.0.0:
|
|
164
|
+
189 074 ops/s, ±0.10% | fastest
|
|
189
165
|
|
|
190
|
-
libxmljs2 0.
|
|
191
|
-
|
|
166
|
+
libxmljs2 0.30.1 (native):
|
|
167
|
+
74 006 ops/s, ±0.32% | 60.86% slower
|
|
192
168
|
|
|
193
|
-
xmldoc 1.
|
|
194
|
-
|
|
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
|
|
198
|
-
Slowest:
|
|
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
|
|
204
|
-
|
|
179
|
+
@rgrove/parse-xml 4.0.0:
|
|
180
|
+
1 066 ops/s, ±0.11% | 49.12% slower
|
|
205
181
|
|
|
206
|
-
libxmljs2 0.
|
|
207
|
-
|
|
182
|
+
libxmljs2 0.30.1 (native):
|
|
183
|
+
2 095 ops/s, ±2.68% | fastest
|
|
208
184
|
|
|
209
|
-
xmldoc 1.
|
|
210
|
-
|
|
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.
|
|
214
|
-
Slowest: xmldoc 1.
|
|
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
|
|
220
|
-
|
|
195
|
+
@rgrove/parse-xml 4.0.0:
|
|
196
|
+
91 ops/s, ±0.11% | 51.85% slower
|
|
221
197
|
|
|
222
|
-
libxmljs2 0.
|
|
223
|
-
|
|
198
|
+
libxmljs2 0.30.1 (native):
|
|
199
|
+
189 ops/s, ±0.99% | fastest
|
|
224
200
|
|
|
225
|
-
xmldoc 1.
|
|
226
|
-
|
|
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.
|
|
230
|
-
Slowest: xmldoc 1.
|
|
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
|
|