xml-twig 1.2.90 → 1.2.91
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/README.md +3 -3
- package/doc/twig.md +3 -4
- package/package.json +1 -1
- package/twig.js +9 -10
package/README.md
CHANGED
|
@@ -217,7 +217,7 @@ The [Twig](./doc/twig.md#Twig) Class models a "some-kind" Element tree. I try t
|
|
|
217
217
|
#### XML-Namespaces
|
|
218
218
|
|
|
219
219
|
When the XML-Files uses [Namespaces](https://www.w3schools.com/xml/xml_namespaces.asp) then you can address the elements as they appear in the file, for example `cd:data`.
|
|
220
|
-
With option `{
|
|
220
|
+
With option `{ xmlns : true }` you will get access to the `.namespace` property.
|
|
221
221
|
|
|
222
222
|
### Access elements and attributes
|
|
223
223
|
|
|
@@ -232,7 +232,7 @@ With option `{ namespaces : true }` you will get access to the `.namespace` prop
|
|
|
232
232
|
Specify attribute name or regular expression or custom condition. For details see [AttributeCondition](./doc/twig.md#AttributeCondition).<br>
|
|
233
233
|
Let's assume an XML element like this:
|
|
234
234
|
```js
|
|
235
|
-
<person firstName="Jean-Luc"
|
|
235
|
+
<person firstName="Jean-Luc" lastName="Picard" age="59" />
|
|
236
236
|
```
|
|
237
237
|
Here are some examples the get attribute and values:
|
|
238
238
|
```js
|
|
@@ -392,7 +392,7 @@ This `xml-twig` module focus on reading a XML files. In principle it would be po
|
|
|
392
392
|
|
|
393
393
|
Accessing Twig-Elements by [XML-Path](https://www.w3.org/TR/xpath/) language is not supported. One reason it, the `Twig` class models more an [Element](https://www.w3schools.com/xml/xml_elements.asp) rather than a [Node](https://www.w3schools.com/xml/dom_nodes.asp) which would be more generic.
|
|
394
394
|
|
|
395
|
-
|
|
395
|
+
Despite [W3C Recommendations](https://www.w3.org/TR/xml/#charencoding) ("All XML processors MUST be able to read entities in both the UTF-8 and UTF-16 encodings"), the `sax` parser does not support UTF-16 encodings. When you have a XML-File encoded in UF-16, then you must use the `expat` parser.
|
|
396
396
|
|
|
397
397
|
|
|
398
398
|
|
package/doc/twig.md
CHANGED
|
@@ -1415,21 +1415,20 @@ Create a new Twig parser
|
|
|
1415
1415
|
Optional settings for the Twig parser
|
|
1416
1416
|
|
|
1417
1417
|
**Kind**: global typedef
|
|
1418
|
-
**Default**: <code>{ method: 'sax',
|
|
1418
|
+
**Default**: <code>{ method: 'sax', xmlns: false, trim: true, resumeAfterError: false, partial: false }</code>
|
|
1419
1419
|
**Properties**
|
|
1420
1420
|
|
|
1421
1421
|
| Name | Type | Description |
|
|
1422
1422
|
| --- | --- | --- |
|
|
1423
1423
|
| [method] | <code>'sax'</code> \| <code>'expat'</code> | The underlying parser. Either `'sax'` or `'expat'`. |
|
|
1424
|
-
| [encoding] | <code>string</code> | Encoding of the XML File. Applies only to `expat` parser. |
|
|
1425
1424
|
| [xmlns] | <code>boolean</code> | If `true`, then namespaces are accessible by `namespace` property. |
|
|
1426
1425
|
| [trim] | <code>boolean</code> | If `true`, then turn any whitespace into a single space. Text and comments are trimmed. |
|
|
1427
1426
|
| [resumeAfterError] | <code>boolean</code> | If `true` then parser continues reading after an error. Otherwise it throws exception. |
|
|
1428
|
-
| [partial] | <code>boolean</code> |
|
|
1427
|
+
| [partial] | <code>boolean</code> | If `true` then unhandled elements are purged. |
|
|
1429
1428
|
|
|
1430
1429
|
**Example**
|
|
1431
1430
|
```js
|
|
1432
|
-
{
|
|
1431
|
+
{ method: 'expat', xmlns: true }
|
|
1433
1432
|
```
|
|
1434
1433
|
<a name="TwigHandler"></a>
|
|
1435
1434
|
|
package/package.json
CHANGED
package/twig.js
CHANGED
|
@@ -27,13 +27,12 @@ const Any = new AnyHandler();
|
|
|
27
27
|
* Optional settings for the Twig parser
|
|
28
28
|
* @typedef ParserOptions
|
|
29
29
|
* @property {'sax' | 'expat'} [method] - The underlying parser. Either `'sax'` or `'expat'`.
|
|
30
|
-
* @property {string} [encoding] - Encoding of the XML File. Applies only to `expat` parser.
|
|
31
30
|
* @property {boolean} [xmlns] - If `true`, then namespaces are accessible by `namespace` property.
|
|
32
31
|
* @property {boolean} [trim] - If `true`, then turn any whitespace into a single space. Text and comments are trimmed.
|
|
33
32
|
* @property {boolean} [resumeAfterError] - If `true` then parser continues reading after an error. Otherwise it throws exception.
|
|
34
|
-
* @property {boolean} [partial] -
|
|
35
|
-
* @example {
|
|
36
|
-
* @default { method: 'sax',
|
|
33
|
+
* @property {boolean} [partial] - If `true` then unhandled elements are purged.
|
|
34
|
+
* @example { method: 'expat', xmlns: true }
|
|
35
|
+
* @default { method: 'sax', xmlns: false, trim: true, resumeAfterError: false, partial: false }
|
|
37
36
|
*/
|
|
38
37
|
|
|
39
38
|
/**
|
|
@@ -96,8 +95,8 @@ const Any = new AnyHandler();
|
|
|
96
95
|
* @param {ParserOptions} [options] - Object of optional options
|
|
97
96
|
* @throws {UnsupportedParser} - For an unsupported parser. Currently `expat` and `sax` (default) are supported.
|
|
98
97
|
*/
|
|
99
|
-
function createParser(handler, options) {
|
|
100
|
-
options = Object.assign({ method: SAX,
|
|
98
|
+
function createParser(handler, options = {}) {
|
|
99
|
+
options = Object.assign({ method: SAX, xmlns: false, trim: true, resumeAfterError: false, partial: false }, options);
|
|
101
100
|
let parser;
|
|
102
101
|
let namespaces = {};
|
|
103
102
|
let closeEvent;
|
|
@@ -187,7 +186,7 @@ function createParser(handler, options) {
|
|
|
187
186
|
|
|
188
187
|
parser.on("attribute", function (attr) {
|
|
189
188
|
current.attribute(attr.name, attr.value);
|
|
190
|
-
if (attr.uri !==
|
|
189
|
+
if ((attr.uri ?? '') !== '' && attr.local !== undefined) {
|
|
191
190
|
namespaces[attr.local] = attr.uri;
|
|
192
191
|
Object.defineProperty(current, 'namespace', {
|
|
193
192
|
value: { local: attr.local, uri: attr.uri },
|
|
@@ -200,7 +199,7 @@ function createParser(handler, options) {
|
|
|
200
199
|
current.text = current.text ?? '' + str;
|
|
201
200
|
});
|
|
202
201
|
|
|
203
|
-
|
|
202
|
+
const hndl = Array.isArray(handler) ? handler : [handler];
|
|
204
203
|
let rootHandler = hndl.find(x => x.tag instanceof RootHandler);
|
|
205
204
|
parser.on("end", function () {
|
|
206
205
|
if (typeof rootHandler?.function === 'function') rootHandler.function(tree);
|
|
@@ -209,8 +208,6 @@ function createParser(handler, options) {
|
|
|
209
208
|
|
|
210
209
|
} else if (options.method === EXPAT) {
|
|
211
210
|
parser = require("node-expat").createParser();
|
|
212
|
-
parser.encoding = options.encoding;
|
|
213
|
-
|
|
214
211
|
Object.defineProperty(parser, 'currentLine', {
|
|
215
212
|
enumerable: true,
|
|
216
213
|
get() { return parser.parser.getCurrentLineNumber(); }
|
|
@@ -245,6 +242,8 @@ function createParser(handler, options) {
|
|
|
245
242
|
}
|
|
246
243
|
}
|
|
247
244
|
}
|
|
245
|
+
for (let attr in attrs)
|
|
246
|
+
current.attribute(attr, attrs[attr]);
|
|
248
247
|
if (options.xmlns) {
|
|
249
248
|
for (let key of Object.keys(attrs).filter(x => x.startsWith('xmlns:')))
|
|
250
249
|
namespaces[key.split(':')[1]] = attrs[key];
|