xml-twig 1.4.0 → 1.5.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.
- package/README.md +9 -8
- package/doc/twig.md +11 -10
- package/package.json +1 -1
- package/twig.js +14 -13
package/README.md
CHANGED
|
@@ -48,8 +48,8 @@ API Documentation: see [Twig](./doc/twig.md)
|
|
|
48
48
|
const fs = require('fs')
|
|
49
49
|
const twig = require('xml-twig')
|
|
50
50
|
|
|
51
|
-
function rootHandler(elt) {
|
|
52
|
-
console.log(`<${elt.name}> finished after ${
|
|
51
|
+
function rootHandler(elt, parserObj) {
|
|
52
|
+
console.log(`<${elt.name}> finished after ${parserObj.currentLine} lines`);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
const parser = twig.createParser({ tag: twig.Root, function: rootHandler }, { method: 'sax' })
|
|
@@ -78,8 +78,8 @@ API Documentation: see [Twig](./doc/twig.md)
|
|
|
78
78
|
|
|
79
79
|
|
|
80
80
|
```js
|
|
81
|
-
function bookHandler(elt) {
|
|
82
|
-
console.log(`${elt.attr("category")} ${elt.name} at line ${
|
|
81
|
+
function bookHandler(elt, parserObj) {
|
|
82
|
+
console.log(`${elt.attr("category")} ${elt.name} at line ${parserObj.currentLine}`)
|
|
83
83
|
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
|
|
84
84
|
}
|
|
85
85
|
|
|
@@ -119,8 +119,8 @@ API Documentation: see [Twig](./doc/twig.md)
|
|
|
119
119
|
- **Read every element from XML Document**
|
|
120
120
|
|
|
121
121
|
```js
|
|
122
|
-
function anyHandler(elt) {
|
|
123
|
-
console.log(`${' '.repeat(elt.level)}${elt.name} => "${elt.text ?? ''}" at line ${
|
|
122
|
+
function anyHandler(elt, parserObj) {
|
|
123
|
+
console.log(`${' '.repeat(elt.level)}${elt.name} => "${elt.text ?? ''}" at line ${parserObj.currentLine}`)
|
|
124
124
|
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
|
|
125
125
|
}
|
|
126
126
|
|
|
@@ -164,8 +164,8 @@ Be aware if you run methods like `elt.followingSibling()`, `elt.descendant()`, `
|
|
|
164
164
|
const parser = twig.createParser(handle_ebook, { partial: true })
|
|
165
165
|
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser);
|
|
166
166
|
|
|
167
|
-
function ebookHandler(elt) {
|
|
168
|
-
console.log(`${elt.name} at line ${
|
|
167
|
+
function ebookHandler(elt, parserObj) {
|
|
168
|
+
console.log(`${elt.name} at line ${parserObj.currentLine}`)
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
function rootHandler(elt) {
|
|
@@ -355,6 +355,7 @@ For methods which return a single **Twig** element (e.g. `elt.next("book")`) the
|
|
|
355
355
|
`.namespace` - **object**: Namespace of the element or `null`. Only available if parsed with option `xmlns: true`.<br>
|
|
356
356
|
Example `{ local: 'h', uri: 'http://www.w3.org/TR/html4/' }`
|
|
357
357
|
|
|
358
|
+
`.path` - **string**: The [XPath](https://www.w3.org/TR/xpath/) location of the Element. **Note:**<br> Like all other methods the returned path refers to currently loaded XML Chunk, not the input XML-File. Unlike JavaScript, indexes in XPath are starting at 1 instead of 0.
|
|
358
359
|
|
|
359
360
|
#### Update XML Elements
|
|
360
361
|
|
package/doc/twig.md
CHANGED
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
<dd><p>Generic error for non implemented feature</p>
|
|
10
10
|
</dd>
|
|
11
11
|
<dt><a href="#UnsupportedParser">UnsupportedParser</a></dt>
|
|
12
|
-
<dd><p>Error for unsupported
|
|
12
|
+
<dd><p>Error for unsupported parser</p>
|
|
13
13
|
</dd>
|
|
14
14
|
<dt><a href="#UnsupportedType">UnsupportedType</a></dt>
|
|
15
|
-
<dd><p>Generic error for unsupported data
|
|
15
|
+
<dd><p>Generic error for unsupported data type</p>
|
|
16
16
|
</dd>
|
|
17
17
|
<dt><a href="#UnsupportedCondition">UnsupportedCondition</a></dt>
|
|
18
|
-
<dd><p>Generic error for unsupported
|
|
18
|
+
<dd><p>Generic error for unsupported condition</p>
|
|
19
19
|
</dd>
|
|
20
20
|
</dl>
|
|
21
21
|
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
<dt><a href="#onStart">onStart(binds, node, attrs)</a></dt>
|
|
38
38
|
<dd><p>Common Event hanlder for starting tag</p>
|
|
39
39
|
</dd>
|
|
40
|
-
<dt><a href="#onClose">onClose(handler, options, name)</a></dt>
|
|
41
|
-
<dd><p>Common Event hanlder for closing tag
|
|
40
|
+
<dt><a href="#onClose">onClose(handler, parser, options, name)</a></dt>
|
|
41
|
+
<dd><p>Common Event hanlder for closing tag. On closed elements it either calls the Handler function or emits the specified event.</p>
|
|
42
42
|
</dd>
|
|
43
43
|
</dl>
|
|
44
44
|
|
|
@@ -1386,7 +1386,7 @@ Generic error for non implemented feature
|
|
|
1386
1386
|
<a name="UnsupportedParser"></a>
|
|
1387
1387
|
|
|
1388
1388
|
## UnsupportedParser
|
|
1389
|
-
Error for unsupported
|
|
1389
|
+
Error for unsupported parser
|
|
1390
1390
|
|
|
1391
1391
|
**Kind**: global class
|
|
1392
1392
|
<a name="new_UnsupportedParser_new"></a>
|
|
@@ -1404,7 +1404,7 @@ Error for unsupported data types
|
|
|
1404
1404
|
<a name="UnsupportedType"></a>
|
|
1405
1405
|
|
|
1406
1406
|
## UnsupportedType
|
|
1407
|
-
Generic error for unsupported data
|
|
1407
|
+
Generic error for unsupported data type
|
|
1408
1408
|
|
|
1409
1409
|
**Kind**: global class
|
|
1410
1410
|
<a name="new_UnsupportedType_new"></a>
|
|
@@ -1422,7 +1422,7 @@ Generic error for unsupported data types
|
|
|
1422
1422
|
<a name="UnsupportedCondition"></a>
|
|
1423
1423
|
|
|
1424
1424
|
## UnsupportedCondition
|
|
1425
|
-
Generic error for unsupported
|
|
1425
|
+
Generic error for unsupported condition
|
|
1426
1426
|
|
|
1427
1427
|
**Kind**: global class
|
|
1428
1428
|
<a name="new_UnsupportedCondition_new"></a>
|
|
@@ -1478,14 +1478,15 @@ Common Event hanlder for starting tag
|
|
|
1478
1478
|
|
|
1479
1479
|
<a name="onClose"></a>
|
|
1480
1480
|
|
|
1481
|
-
## onClose(handler, options, name)
|
|
1482
|
-
Common Event hanlder for closing tag
|
|
1481
|
+
## onClose(handler, parser, options, name)
|
|
1482
|
+
Common Event hanlder for closing tag. On closed elements it either calls the Handler function or emits the specified event.
|
|
1483
1483
|
|
|
1484
1484
|
**Kind**: global function
|
|
1485
1485
|
|
|
1486
1486
|
| Param | Type | Description |
|
|
1487
1487
|
| --- | --- | --- |
|
|
1488
1488
|
| handler | [<code>TwigHandler</code>](#TwigHandler) \| [<code>Array.<TwigHandler></code>](#TwigHandler) | Object or array of element specification and function to handle elements |
|
|
1489
|
+
| parser | [<code>sax</code>](https://www.npmjs.com/package/sax) \| [<code>node-expat</code>](https://www.npmjs.com/package/node-expat) | SAXStream or node-expat Stream object |
|
|
1489
1490
|
| options | [<code>ParserOptions</code>](#ParserOptions) | Object of optional options |
|
|
1490
1491
|
| name | <code>string</code> | Event handler parameter |
|
|
1491
1492
|
|
package/package.json
CHANGED
package/twig.js
CHANGED
|
@@ -158,7 +158,7 @@ function createParser(handler, options = {}) {
|
|
|
158
158
|
get() { return parser._parser.column + 1; }
|
|
159
159
|
});
|
|
160
160
|
|
|
161
|
-
parser.on("closetag", onClose.bind(null, handler, options));
|
|
161
|
+
parser.on("closetag", onClose.bind(null, handler, parser, options));
|
|
162
162
|
parser.on("opentagstart", onStart.bind(null, {
|
|
163
163
|
handler: Array.isArray(handler) ? handler : [handler],
|
|
164
164
|
options: options,
|
|
@@ -226,7 +226,7 @@ function createParser(handler, options = {}) {
|
|
|
226
226
|
get() { return parser.parser.getCurrentColumnNumber(); }
|
|
227
227
|
});
|
|
228
228
|
|
|
229
|
-
parser.on("endElement", onClose.bind(null, handler, options));
|
|
229
|
+
parser.on("endElement", onClose.bind(null, handler, parser, options));
|
|
230
230
|
parser.on("startElement", onStart.bind(null, {
|
|
231
231
|
handler: Array.isArray(handler) ? handler : [handler],
|
|
232
232
|
options: options,
|
|
@@ -370,38 +370,39 @@ function onStart(binds, node, attrs) {
|
|
|
370
370
|
}
|
|
371
371
|
|
|
372
372
|
/**
|
|
373
|
-
* Common Event hanlder for closing tag
|
|
373
|
+
* Common Event hanlder for closing tag. On closed elements it either calls the Handler function or emits the specified event.
|
|
374
374
|
* @param {TwigHandler|TwigHandler[]} handler - Object or array of element specification and function to handle elements
|
|
375
|
+
* @param {external:sax|external:node-expat} parser - SAXStream or node-expat Stream object
|
|
375
376
|
* @param {ParserOptions} options - Object of optional options
|
|
376
377
|
* @param {string} name - Event handler parameter
|
|
377
378
|
*/
|
|
378
|
-
function onClose(handler, options, name) {
|
|
379
|
+
function onClose(handler, parser, options, name) {
|
|
379
380
|
current.close();
|
|
380
381
|
let purge = true;
|
|
381
382
|
|
|
382
383
|
for (let hndl of Array.isArray(handler) ? handler : [handler]) {
|
|
383
384
|
if (hndl.tag instanceof AnyHandler) {
|
|
384
|
-
if (typeof hndl.function === 'function') hndl.function(current ?? tree);
|
|
385
|
+
if (typeof hndl.function === 'function') hndl.function(current ?? tree, parser);
|
|
385
386
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, current ?? tree);
|
|
386
387
|
purge = false;
|
|
387
388
|
} else if (hndl.tag instanceof RootHandler && current.isRoot) {
|
|
388
|
-
if (typeof hndl.function === 'function') hndl.function(tree);
|
|
389
|
+
if (typeof hndl.function === 'function') hndl.function(tree, parser);
|
|
389
390
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, tree);
|
|
390
391
|
purge = false;
|
|
391
392
|
} else if (Array.isArray(hndl.tag) && hndl.tag.includes(name)) {
|
|
392
|
-
if (typeof hndl.function === 'function') hndl.function(current ?? tree);
|
|
393
|
+
if (typeof hndl.function === 'function') hndl.function(current ?? tree, parser);
|
|
393
394
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, current ?? tree);
|
|
394
395
|
purge = false;
|
|
395
396
|
} else if (typeof hndl.tag === 'string' && name === hndl.tag) {
|
|
396
|
-
if (typeof hndl.function === 'function') hndl.function(current ?? tree);
|
|
397
|
+
if (typeof hndl.function === 'function') hndl.function(current ?? tree, parser);
|
|
397
398
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, current ?? tree);
|
|
398
399
|
purge = false;
|
|
399
400
|
} else if (hndl.tag instanceof RegExp && hndl.tag.test(name)) {
|
|
400
|
-
if (typeof hndl.function === 'function') hndl.function(current ?? tree);
|
|
401
|
+
if (typeof hndl.function === 'function') hndl.function(current ?? tree, parser);
|
|
401
402
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, current ?? tree);
|
|
402
403
|
purge = false;
|
|
403
404
|
} else if (typeof hndl.tag === 'function' && hndl.tag(name, current ?? tree)) {
|
|
404
|
-
if (typeof hndl.function === 'function') hndl.function(current ?? tree);
|
|
405
|
+
if (typeof hndl.function === 'function') hndl.function(current ?? tree, parser);
|
|
405
406
|
if (typeof hndl.event === 'string') parser.emit(hndl.event, current ?? tree);
|
|
406
407
|
purge = false;
|
|
407
408
|
}
|
|
@@ -1236,7 +1237,7 @@ class NotImplementedYet extends TypeError {
|
|
|
1236
1237
|
|
|
1237
1238
|
|
|
1238
1239
|
/**
|
|
1239
|
-
* Error for unsupported
|
|
1240
|
+
* Error for unsupported parser
|
|
1240
1241
|
* @exception UnsupportedParser
|
|
1241
1242
|
*/
|
|
1242
1243
|
class UnsupportedParser extends TypeError {
|
|
@@ -1249,7 +1250,7 @@ class UnsupportedParser extends TypeError {
|
|
|
1249
1250
|
}
|
|
1250
1251
|
|
|
1251
1252
|
/**
|
|
1252
|
-
* Generic error for unsupported data
|
|
1253
|
+
* Generic error for unsupported data type
|
|
1253
1254
|
* @exception UnsupportedType
|
|
1254
1255
|
*/
|
|
1255
1256
|
class UnsupportedType extends TypeError {
|
|
@@ -1262,7 +1263,7 @@ class UnsupportedType extends TypeError {
|
|
|
1262
1263
|
}
|
|
1263
1264
|
|
|
1264
1265
|
/**
|
|
1265
|
-
* Generic error for unsupported
|
|
1266
|
+
* Generic error for unsupported condition
|
|
1266
1267
|
* @exception UnsupportedCondition
|
|
1267
1268
|
*/
|
|
1268
1269
|
class UnsupportedCondition extends TypeError {
|