xml-twig 1.2.92 → 1.2.94

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 (3) hide show
  1. package/doc/twig.md +28 -22
  2. package/package.json +38 -38
  3. package/twig.js +36 -13
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><p>`</p>
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>RootHandler</code> | <code>AnyHandler</code> | <code>undefined</code></dt>
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.&lt;Twig&gt;</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.&lt;Twig&gt;</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>RootHandler</code> \| <code>AnyHandler</code> \| <code>undefined</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.92",
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.94",
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|RootHandler|AnyHandler|undefined} HandlerCondition
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);
@@ -185,14 +205,15 @@ function createParser(handler, options = {}) {
185
205
  });
186
206
 
187
207
  parser.on("attribute", function (attr) {
188
- current.attribute(attr.name, attr.value);
189
- if ((attr.uri ?? '') !== '' && attr.local !== undefined) {
208
+ if (options.xmlns && (attr.uri ?? '') !== '' && attr.local !== undefined) {
190
209
  namespaces[attr.local] = attr.uri;
191
210
  Object.defineProperty(current, 'namespace', {
192
211
  value: { local: attr.local, uri: attr.uri },
193
212
  writable: false,
194
213
  enumerable: true
195
214
  });
215
+ } else {
216
+ current.attribute(attr.name, attr.value);
196
217
  }
197
218
  });
198
219
  parser.on("cdata", function (str) {
@@ -219,13 +240,18 @@ function createParser(handler, options = {}) {
219
240
  closeEvent = "endElement";
220
241
 
221
242
  parser.on("startElement", function (name, attrs) {
243
+ let attr = {};
244
+ if (options.xmlns) {
245
+ for (let key of Object.keys(attrs).filter(x => !x.startsWith('xmlns:')))
246
+ attr[key] = attrs[key];
247
+ }
222
248
  if (tree === undefined) {
223
- tree = new Twig(name, current, attrs);
249
+ tree = new Twig(name, current, options.xmlns ? attr : attrs);
224
250
  } else {
225
251
  if (current.isRoot && current.name === undefined) {
226
252
  current.setRoot(name);
227
253
  } else {
228
- let elt = new Twig(name, current, attrs);
254
+ let elt = new Twig(name, current, options.xmlns ? attr : attrs);
229
255
  if (options.partial) {
230
256
  for (let hndl of Array.isArray(handler) ? handler : [handler]) {
231
257
  if (typeof hndl.tag === 'string' && name === hndl.tag) {
@@ -242,8 +268,6 @@ function createParser(handler, options = {}) {
242
268
  }
243
269
  }
244
270
  }
245
- for (let attr in attrs)
246
- current.attribute(attr, attrs[attr]);
247
271
  if (options.xmlns) {
248
272
  for (let key of Object.keys(attrs).filter(x => x.startsWith('xmlns:')))
249
273
  namespaces[key.split(':')[1]] = attrs[key];
@@ -625,7 +649,7 @@ class Twig {
625
649
 
626
650
  /**
627
651
  * Internal recursive function used by `writer()`
628
- * @param {XMLWriter} xw - The writer object
652
+ * @param {external:XMLWriter} xw - The writer object
629
653
  * @param {Twig[]} childArray - Array of child elements
630
654
  */
631
655
  #addChild = function (xw, childArray) {
@@ -640,11 +664,10 @@ class Twig {
640
664
  xw.endElement();
641
665
  };
642
666
 
643
-
644
667
  /**
645
668
  * Creates xml-writer from current element
646
- * @param {?boolean|string|XMLWriter} par - `true` or intention character or an already created XMLWriter
647
- * @returns {XMLWriter}
669
+ * @param {?boolean|string|external:XMLWriter} par - `true` or intention character or an already created XMLWriter
670
+ * @returns {external:XMLWriter}
648
671
  */
649
672
  writer = function (par) {
650
673
  const XMLWriter = require('xml-writer');