xml-twig 1.2.91 → 1.2.93
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 +1 -1
- package/doc/twig.md +28 -22
- package/package.json +38 -38
- package/twig.js +26 -7
package/README.md
CHANGED
|
@@ -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
|
-
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
|
|
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 UTF-16, then you must use the `expat` parser.
|
|
396
396
|
|
|
397
397
|
|
|
398
398
|
|
package/doc/twig.md
CHANGED
|
@@ -23,8 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
<dl>
|
|
25
25
|
<dt><a href="#Root">Root</a> : <code>RootHandler</code></dt>
|
|
26
|
-
<dd
|
|
27
|
-
</dd>
|
|
26
|
+
<dd></dd>
|
|
28
27
|
<dt><a href="#Any">Any</a> : <code>AnyHandler</code></dt>
|
|
29
28
|
<dd></dd>
|
|
30
29
|
</dl>
|
|
@@ -32,7 +31,7 @@
|
|
|
32
31
|
## Functions
|
|
33
32
|
|
|
34
33
|
<dl>
|
|
35
|
-
<dt><a href="#createParser">createParser(handler, [options])</a></dt>
|
|
34
|
+
<dt><a href="#createParser">createParser(handler, [options])</a> ⇒ <code><a href="#Parser">Parser</a></code></dt>
|
|
36
35
|
<dd><p>Create a new Twig parser</p>
|
|
37
36
|
</dd>
|
|
38
37
|
</dl>
|
|
@@ -48,7 +47,7 @@
|
|
|
48
47
|
Element can be specified as string, Regular Expression, custom function, <code>Twig.Root</code> or <code>Twig.Any</code><br>
|
|
49
48
|
You can specify a <code>function</code> or a <code>event</code> name</p>
|
|
50
49
|
</dd>
|
|
51
|
-
<dt><a href="#HandlerCondition">HandlerCondition</a> : <code>string</code> | <code>RegExp</code> | <code><a href="#HandlerConditionFilter">HandlerConditionFilter</a></code> | <code>
|
|
50
|
+
<dt><a href="#HandlerCondition">HandlerCondition</a> : <code>string</code> | <code>RegExp</code> | <code><a href="#HandlerConditionFilter">HandlerConditionFilter</a></code> | <code><a href="#Root">Root</a></code> | <code><a href="#Any">Any</a></code> | <code>undefined</code></dt>
|
|
52
51
|
<dd><p>Condition to specify when handler shall be called<br> </p>
|
|
53
52
|
<ul>
|
|
54
53
|
<li>If <code>undefined</code>, then all elements are returned.<br> </li>
|
|
@@ -78,6 +77,8 @@ You can specify a <code>function</code> or a <code>event</code> name</p>
|
|
|
78
77
|
<dt><a href="#ElementConditionFilter">ElementConditionFilter</a> ⇒ <code>boolean</code></dt>
|
|
79
78
|
<dd><p>Custom filter function to select desired elements</p>
|
|
80
79
|
</dd>
|
|
80
|
+
<dt><a href="#Parser">Parser</a> ⇒ <code><a href="https://www.npmjs.com/package/sax">sax</a></code> | <code><a href="https://www.npmjs.com/package/node-expat">node-expat</a></code></dt>
|
|
81
|
+
<dd></dd>
|
|
81
82
|
<dt><a href="#AttributeCondition">AttributeCondition</a> : <code>string</code> | <code>RegExp</code> | <code><a href="#AttributeConditionFilter">AttributeConditionFilter</a></code></dt>
|
|
82
83
|
<dd><p>Optional condition to get attributes<br> </p>
|
|
83
84
|
<ul>
|
|
@@ -134,7 +135,7 @@ You can specify a <code>function</code> or a <code>event</code> name</p>
|
|
|
134
135
|
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
135
136
|
* [.close](#Twig+close)
|
|
136
137
|
* [.addChild](#Twig+addChild) ℗
|
|
137
|
-
* [.writer](#Twig+writer) ⇒ <code>XMLWriter</code>
|
|
138
|
+
* [.writer](#Twig+writer) ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
138
139
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
139
140
|
* [.attributes](#Twig+attributes) ⇒ <code>object</code>
|
|
140
141
|
* [.hasAttribute](#Twig+hasAttribute) ⇒ <code>boolean</code>
|
|
@@ -355,19 +356,19 @@ Internal recursive function used by `writer()`
|
|
|
355
356
|
|
|
356
357
|
| Param | Type | Description |
|
|
357
358
|
| --- | --- | --- |
|
|
358
|
-
| xw | <code>XMLWriter</code> | The writer object |
|
|
359
|
+
| xw | [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer) | The writer object |
|
|
359
360
|
| childArray | [<code>Array.<Twig></code>](#Twig) | Array of child elements |
|
|
360
361
|
|
|
361
362
|
<a name="Twig+writer"></a>
|
|
362
363
|
|
|
363
|
-
### twig.writer ⇒ <code>XMLWriter</code>
|
|
364
|
+
### twig.writer ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
364
365
|
Creates xml-writer from current element
|
|
365
366
|
|
|
366
367
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
367
368
|
|
|
368
369
|
| Param | Type | Description |
|
|
369
370
|
| --- | --- | --- |
|
|
370
|
-
| par | <code>boolean</code> \| <code>string</code> \| <code>XMLWriter</code> | `true` or intention character or an already created XMLWriter |
|
|
371
|
+
| par | <code>boolean</code> \| <code>string</code> \| [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer) | `true` or intention character or an already created XMLWriter |
|
|
371
372
|
|
|
372
373
|
<a name="Twig+attr"></a>
|
|
373
374
|
|
|
@@ -743,7 +744,7 @@ Common function to filter Twig element
|
|
|
743
744
|
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
744
745
|
* [.close](#Twig+close)
|
|
745
746
|
* [.addChild](#Twig+addChild) ℗
|
|
746
|
-
* [.writer](#Twig+writer) ⇒ <code>XMLWriter</code>
|
|
747
|
+
* [.writer](#Twig+writer) ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
747
748
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
748
749
|
* [.attributes](#Twig+attributes) ⇒ <code>object</code>
|
|
749
750
|
* [.hasAttribute](#Twig+hasAttribute) ⇒ <code>boolean</code>
|
|
@@ -964,19 +965,19 @@ Internal recursive function used by `writer()`
|
|
|
964
965
|
|
|
965
966
|
| Param | Type | Description |
|
|
966
967
|
| --- | --- | --- |
|
|
967
|
-
| xw | <code>XMLWriter</code> | The writer object |
|
|
968
|
+
| xw | [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer) | The writer object |
|
|
968
969
|
| childArray | [<code>Array.<Twig></code>](#Twig) | Array of child elements |
|
|
969
970
|
|
|
970
971
|
<a name="Twig+writer"></a>
|
|
971
972
|
|
|
972
|
-
### twig.writer ⇒ <code>XMLWriter</code>
|
|
973
|
+
### twig.writer ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
973
974
|
Creates xml-writer from current element
|
|
974
975
|
|
|
975
976
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
976
977
|
|
|
977
978
|
| Param | Type | Description |
|
|
978
979
|
| --- | --- | --- |
|
|
979
|
-
| par | <code>boolean</code> \| <code>string</code> \| <code>XMLWriter</code> | `true` or intention character or an already created XMLWriter |
|
|
980
|
+
| par | <code>boolean</code> \| <code>string</code> \| [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer) | `true` or intention character or an already created XMLWriter |
|
|
980
981
|
|
|
981
982
|
<a name="Twig+attr"></a>
|
|
982
983
|
|
|
@@ -1386,8 +1387,6 @@ Generic error for unsupported data types
|
|
|
1386
1387
|
<a name="Root"></a>
|
|
1387
1388
|
|
|
1388
1389
|
## Root : <code>RootHandler</code>
|
|
1389
|
-
`
|
|
1390
|
-
|
|
1391
1390
|
**Kind**: global constant
|
|
1392
1391
|
<a name="Any"></a>
|
|
1393
1392
|
|
|
@@ -1395,10 +1394,11 @@ Generic error for unsupported data types
|
|
|
1395
1394
|
**Kind**: global constant
|
|
1396
1395
|
<a name="createParser"></a>
|
|
1397
1396
|
|
|
1398
|
-
## createParser(handler, [options])
|
|
1397
|
+
## createParser(handler, [options]) ⇒ [<code>Parser</code>](#Parser)
|
|
1399
1398
|
Create a new Twig parser
|
|
1400
1399
|
|
|
1401
1400
|
**Kind**: global function
|
|
1401
|
+
**Returns**: [<code>Parser</code>](#Parser) - The parser Object
|
|
1402
1402
|
**Throws**:
|
|
1403
1403
|
|
|
1404
1404
|
- [<code>UnsupportedParser</code>](#UnsupportedParser) - For an unsupported parser. Currently `expat` and `sax` (default) are supported.
|
|
@@ -1446,7 +1446,7 @@ Reference to handler functions for Twig objects.<br>
|
|
|
1446
1446
|
|
|
1447
1447
|
<a name="HandlerCondition"></a>
|
|
1448
1448
|
|
|
1449
|
-
## HandlerCondition : <code>string</code> \| <code>RegExp</code> \| [<code>HandlerConditionFilter</code>](#HandlerConditionFilter) \| <code>
|
|
1449
|
+
## HandlerCondition : <code>string</code> \| <code>RegExp</code> \| [<code>HandlerConditionFilter</code>](#HandlerConditionFilter) \| [<code>Root</code>](#Root) \| [<code>Any</code>](#Any) \| <code>undefined</code>
|
|
1450
1450
|
Condition to specify when handler shall be called<br>
|
|
1451
|
-
If `undefined`, then all elements are returned.<br>
|
|
1452
|
-
If `string` then the element name must be equal to the string
|
|
1453
|
-
If `RegExp` then the element name must match the Regular Expression
|
|
1454
|
-
If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
|
|
1455
|
-
Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
|
|
1456
|
-
Use `Twig.Any` to call the handler on every element
|
|
1457
1451
|
|
|
1458
1452
|
**Kind**: global typedef
|
|
@@ -1492,6 +1492,18 @@ Custom filter function to select desired elements
|
|
|
1492
1492
|
| name | <code>string</code> | Name of the element |
|
|
1493
1493
|
| elt | [<code>Twig</code>](#Twig) | The Twig object |
|
|
1494
1494
|
|
|
1495
|
+
<a name="Parser"></a>
|
|
1496
|
+
|
|
1497
|
+
## Parser ⇒ [<code>sax</code>](https://www.npmjs.com/package/sax) \| [<code>node-expat</code>](https://www.npmjs.com/package/node-expat)
|
|
1498
|
+
**Kind**: global typedef
|
|
1499
|
+
**Returns**: [<code>sax</code>](https://www.npmjs.com/package/sax) \| [<code>node-expat</code>](https://www.npmjs.com/package/node-expat) - The parser Object
|
|
1500
|
+
**Properties**
|
|
1501
|
+
|
|
1502
|
+
| Name | Type | Description |
|
|
1503
|
+
| --- | --- | --- |
|
|
1504
|
+
| [currentLine] | <code>number</code> | The currently processed line in the XML-File |
|
|
1505
|
+
| [currentColumn] | <code>number</code> | The currently processed column in the XML-File |
|
|
1506
|
+
|
|
1495
1507
|
<a name="AttributeCondition"></a>
|
|
1496
1508
|
|
|
1497
1509
|
## AttributeCondition : <code>string</code> \| <code>RegExp</code> \| [<code>AttributeConditionFilter</code>](#AttributeConditionFilter)
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"dependencies": {
|
|
3
|
-
"sax": "^1.3.0",
|
|
4
|
-
"xml-writer": "^1.7.0"
|
|
5
|
-
},
|
|
6
|
-
"name": "xml-twig",
|
|
7
|
-
"description": "Node module for processing huge XML documents in tree mode",
|
|
8
|
-
"version": "1.2.
|
|
9
|
-
"main": "twig.js",
|
|
10
|
-
"directories": {
|
|
11
|
-
"doc": "doc"
|
|
12
|
-
},
|
|
13
|
-
"devDependencies": {
|
|
14
|
-
"jsdoc-to-markdown": "^8.0.0",
|
|
15
|
-
"luxon": "^2.1.1",
|
|
16
|
-
"node-expat": "^2.4.0"
|
|
17
|
-
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "node demo.js"
|
|
20
|
-
},
|
|
21
|
-
"repository": {
|
|
22
|
-
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/Wernfried/xml-twig.git"
|
|
24
|
-
},
|
|
25
|
-
"keywords": [
|
|
26
|
-
"xml",
|
|
27
|
-
"sax",
|
|
28
|
-
"expat",
|
|
29
|
-
"parser",
|
|
30
|
-
"xml-reader"
|
|
31
|
-
],
|
|
32
|
-
"author": "Wernfried Domscheit",
|
|
33
|
-
"license": "ISC",
|
|
34
|
-
"bugs": {
|
|
35
|
-
"url": "https://github.com/Wernfried/xml-twig/issues"
|
|
36
|
-
},
|
|
37
|
-
"homepage": "https://github.com/Wernfried/xml-twig#readme"
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"sax": "^1.3.0",
|
|
4
|
+
"xml-writer": "^1.7.0"
|
|
5
|
+
},
|
|
6
|
+
"name": "xml-twig",
|
|
7
|
+
"description": "Node module for processing huge XML documents in tree mode",
|
|
8
|
+
"version": "1.2.93",
|
|
9
|
+
"main": "twig.js",
|
|
10
|
+
"directories": {
|
|
11
|
+
"doc": "doc"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"jsdoc-to-markdown": "^8.0.0",
|
|
15
|
+
"luxon": "^2.1.1",
|
|
16
|
+
"node-expat": "^2.4.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node demo.js"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Wernfried/xml-twig.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"xml",
|
|
27
|
+
"sax",
|
|
28
|
+
"expat",
|
|
29
|
+
"parser",
|
|
30
|
+
"xml-reader"
|
|
31
|
+
],
|
|
32
|
+
"author": "Wernfried Domscheit",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/Wernfried/xml-twig/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/Wernfried/xml-twig#readme"
|
|
38
|
+
}
|
package/twig.js
CHANGED
|
@@ -4,6 +4,20 @@ const EXPAT = 'expat';
|
|
|
4
4
|
let tree;
|
|
5
5
|
let current;
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @external XMLWriter
|
|
9
|
+
* @see {@link https://www.npmjs.com/package/xml-writer|xml-writer}
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @external sax
|
|
14
|
+
* @see {@link https://www.npmjs.com/package/sax|sax}
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @external node-expat
|
|
19
|
+
* @see {@link https://www.npmjs.com/package/node-expat|node-expat}
|
|
20
|
+
*/
|
|
7
21
|
|
|
8
22
|
|
|
9
23
|
class RootHandler { }
|
|
@@ -13,7 +27,7 @@ class AnyHandler { }
|
|
|
13
27
|
/**
|
|
14
28
|
* @constant {RootHandler} Root
|
|
15
29
|
* @type {RootHandler}
|
|
16
|
-
|
|
30
|
+
*/
|
|
17
31
|
const Root = new RootHandler();
|
|
18
32
|
|
|
19
33
|
/**
|
|
@@ -53,7 +67,7 @@ const Any = new AnyHandler();
|
|
|
53
67
|
* - If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
|
|
54
68
|
* - Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
|
|
55
69
|
* - Use `Twig.Any` to call the handler on every element
|
|
56
|
-
* @typedef {string|RegExp|HandlerConditionFilter|
|
|
70
|
+
* @typedef {string|RegExp|HandlerConditionFilter|Root|Any|undefined} HandlerCondition
|
|
57
71
|
*/
|
|
58
72
|
|
|
59
73
|
/**
|
|
@@ -87,13 +101,19 @@ const Any = new AnyHandler();
|
|
|
87
101
|
* @returns {boolean} If the function returns `true`, then it is included in the filter
|
|
88
102
|
*/
|
|
89
103
|
|
|
90
|
-
|
|
104
|
+
/**
|
|
105
|
+
* @typedef Parser
|
|
106
|
+
* @property {number} [currentLine] - The currently processed line in the XML-File
|
|
107
|
+
* @property {number} [currentColumn] - The currently processed column in the XML-File
|
|
108
|
+
* @returns {external:sax|external:node-expat} The parser Object
|
|
109
|
+
*/
|
|
91
110
|
|
|
92
111
|
/**
|
|
93
112
|
* Create a new Twig parser
|
|
94
113
|
* @param {TwigHandler|TwigHandler[]} handler - Object or array of element specification and function to handle elements
|
|
95
114
|
* @param {ParserOptions} [options] - Object of optional options
|
|
96
115
|
* @throws {UnsupportedParser} - For an unsupported parser. Currently `expat` and `sax` (default) are supported.
|
|
116
|
+
* @returns {Parser} The parser Object
|
|
97
117
|
*/
|
|
98
118
|
function createParser(handler, options = {}) {
|
|
99
119
|
options = Object.assign({ method: SAX, xmlns: false, trim: true, resumeAfterError: false, partial: false }, options);
|
|
@@ -625,7 +645,7 @@ class Twig {
|
|
|
625
645
|
|
|
626
646
|
/**
|
|
627
647
|
* Internal recursive function used by `writer()`
|
|
628
|
-
* @param {XMLWriter} xw - The writer object
|
|
648
|
+
* @param {external:XMLWriter} xw - The writer object
|
|
629
649
|
* @param {Twig[]} childArray - Array of child elements
|
|
630
650
|
*/
|
|
631
651
|
#addChild = function (xw, childArray) {
|
|
@@ -640,11 +660,10 @@ class Twig {
|
|
|
640
660
|
xw.endElement();
|
|
641
661
|
};
|
|
642
662
|
|
|
643
|
-
|
|
644
663
|
/**
|
|
645
664
|
* Creates xml-writer from current element
|
|
646
|
-
* @param {?boolean|string|XMLWriter} par - `true` or intention character or an already created XMLWriter
|
|
647
|
-
* @returns {XMLWriter}
|
|
665
|
+
* @param {?boolean|string|external:XMLWriter} par - `true` or intention character or an already created XMLWriter
|
|
666
|
+
* @returns {external:XMLWriter}
|
|
648
667
|
*/
|
|
649
668
|
writer = function (par) {
|
|
650
669
|
const XMLWriter = require('xml-writer');
|