xml-twig 1.7.1 → 1.7.4

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/doc/twig.md CHANGED
@@ -22,6 +22,8 @@
22
22
  ## Constants
23
23
 
24
24
  <dl>
25
+ <dt><a href="#SAX">SAX</a></dt>
26
+ <dd></dd>
25
27
  <dt><a href="#Root">Root</a> : <code>RootHandler</code></dt>
26
28
  <dd></dd>
27
29
  <dt><a href="#Any">Any</a> : <code>AnyHandler</code></dt>
@@ -34,10 +36,10 @@
34
36
  <dt><a href="#createParser">createParser(handler, [options])</a> ⇒ <code><a href="#Parser">Parser</a></code></dt>
35
37
  <dd><p>Create a new Twig parser</p>
36
38
  </dd>
37
- <dt><a href="#onStart">onStart(binds, node, attrs)</a></dt>
39
+ <dt><a href="#onStart">onStart(parser, binds, node, attrs)</a></dt>
38
40
  <dd><p>Common Event hanlder for starting tag</p>
39
41
  </dd>
40
- <dt><a href="#onClose">onClose(handler, parser, options, name)</a></dt>
42
+ <dt><a href="#onClose">onClose(handler, parser, parser, options, name)</a></dt>
41
43
  <dd><p>Common Event hanlder for closing tag. On closed elements it either calls the Handler function or emits the specified event.</p>
42
44
  </dd>
43
45
  </dl>
@@ -118,7 +120,7 @@ You can specify a <code>function</code> or a <code>event</code> name</p>
118
120
 
119
121
  * [Twig](#Twig)
120
122
  * [new Twig()](#new_Twig_new)
121
- * [new Twig(name, [parent], [attributes], [index])](#new_Twig_new)
123
+ * [new Twig(parser, name, [parent], [attributes], [index])](#new_Twig_new)
122
124
  * [.attributes](#Twig+attributes) : <code>object</code> ℗
123
125
  * [.text](#Twig+text) : <code>string</code> \| <code>number</code> ℗
124
126
  * [.name](#Twig+name) : <code>string</code> ℗
@@ -184,12 +186,13 @@ Generic class modeling a XML Node
184
186
 
185
187
  <a name="new_Twig_new"></a>
186
188
 
187
- ### new Twig(name, [parent], [attributes], [index])
189
+ ### new Twig(parser, name, [parent], [attributes], [index])
188
190
  Create a new Twig object
189
191
 
190
192
 
191
193
  | Param | Type | Description |
192
194
  | --- | --- | --- |
195
+ | parser | [<code>Parser</code>](#Parser) | The main parser object |
193
196
  | name | <code>string</code> | The name of the XML element |
194
197
  | [parent] | [<code>Twig</code>](#Twig) | The parent object |
195
198
  | [attributes] | <code>object</code> | Attribute object |
@@ -246,7 +249,8 @@ Purges the current, typically used after element has been processed.<br>The root
246
249
  <a name="Twig+purgeUpTo"></a>
247
250
 
248
251
  ### twig.purgeUpTo
249
- Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
252
+ Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
253
+ The `elt` object is not purged. If you like to purge including `elt`, use `.purgeUpTo(elt.previous())`
250
254
 
251
255
  **Kind**: instance property of [<code>Twig</code>](#Twig)
252
256
 
@@ -268,7 +272,8 @@ Escapes special XML characters. According W3C specification these are only `&, <
268
272
  <a name="Twig+isEmpty"></a>
269
273
 
270
274
  ### twig.isEmpty ⇒ <code>boolean</code>
271
- Returns `true` if the element is empty, otherwise `false`.
275
+ Returns `true` if the element is empty, otherwise `false`.
276
+ An empty element has no text nor any child elements, however empty elements can have attributes.
272
277
 
273
278
  **Kind**: instance property of [<code>Twig</code>](#Twig)
274
279
  **Returns**: <code>boolean</code> - true if empty element
@@ -303,7 +308,8 @@ The position in `#children` array. For root object 0
303
308
  <a name="Twig+path"></a>
304
309
 
305
310
  ### twig.path ⇒ <code>string</code>
306
- The X-Path position of the element
311
+ The X-Path position of the element
312
+ NOTE: Applies only to currently loaded elements.
307
313
 
308
314
  **Kind**: instance property of [<code>Twig</code>](#Twig)
309
315
  **Returns**: <code>string</code> - X-Path
@@ -403,7 +409,8 @@ Creates xml-writer from current element
403
409
  <a name="Twig+attr"></a>
404
410
 
405
411
  ### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
406
- Returns attribute value or `null` if not found.<br>
412
+ Returns attribute value or `null` if not found.<br>
413
+ If more than one matches the condition, then it returns object as [attribute()](#attribute)
407
414
 
408
415
  **Kind**: instance property of [<code>Twig</code>](#Twig)
409
416
  **Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
@@ -446,7 +453,10 @@ Retrieve or update XML attribute. For update, the condition must be a string, i.
446
453
 
447
454
  **Example**
448
455
  ```js
449
- attribute((name, val) => { return name === 'age' && val > 50})
456
+ attribute((name, val) => { return name === 'age' && val > 50})
457
+ attribute((name) => { return ['firstName', 'lastName'].includes(name) })
458
+ attribute('firstName')
459
+ attribute(/name/i)
450
460
  ```
451
461
  <a name="Twig+deleteAttribute"></a>
452
462
 
@@ -709,7 +719,8 @@ Deletes the current element from tree, same as `purge()`. The root object cannot
709
719
  <a name="Twig+setRoot"></a>
710
720
 
711
721
  ### twig.setRoot(name) ℗
712
- Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
722
+ Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
723
+ Used internally!
713
724
 
714
725
  **Kind**: instance method of [<code>Twig</code>](#Twig)
715
726
  **Access**: private
@@ -751,7 +762,7 @@ Common function to filter Twig element
751
762
 
752
763
  * [Twig](#Twig)
753
764
  * [new Twig()](#new_Twig_new)
754
- * [new Twig(name, [parent], [attributes], [index])](#new_Twig_new)
765
+ * [new Twig(parser, name, [parent], [attributes], [index])](#new_Twig_new)
755
766
  * [.attributes](#Twig+attributes) : <code>object</code> ℗
756
767
  * [.text](#Twig+text) : <code>string</code> \| <code>number</code> ℗
757
768
  * [.name](#Twig+name) : <code>string</code> ℗
@@ -817,12 +828,13 @@ Generic class modeling a XML Node
817
828
 
818
829
  <a name="new_Twig_new"></a>
819
830
 
820
- ### new Twig(name, [parent], [attributes], [index])
831
+ ### new Twig(parser, name, [parent], [attributes], [index])
821
832
  Create a new Twig object
822
833
 
823
834
 
824
835
  | Param | Type | Description |
825
836
  | --- | --- | --- |
837
+ | parser | [<code>Parser</code>](#Parser) | The main parser object |
826
838
  | name | <code>string</code> | The name of the XML element |
827
839
  | [parent] | [<code>Twig</code>](#Twig) | The parent object |
828
840
  | [attributes] | <code>object</code> | Attribute object |
@@ -879,7 +891,8 @@ Purges the current, typically used after element has been processed.<br>The root
879
891
  <a name="Twig+purgeUpTo"></a>
880
892
 
881
893
  ### twig.purgeUpTo
882
- Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
894
+ Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
895
+ The `elt` object is not purged. If you like to purge including `elt`, use `.purgeUpTo(elt.previous())`
883
896
 
884
897
  **Kind**: instance property of [<code>Twig</code>](#Twig)
885
898
 
@@ -901,7 +914,8 @@ Escapes special XML characters. According W3C specification these are only `&, <
901
914
  <a name="Twig+isEmpty"></a>
902
915
 
903
916
  ### twig.isEmpty ⇒ <code>boolean</code>
904
- Returns `true` if the element is empty, otherwise `false`.
917
+ Returns `true` if the element is empty, otherwise `false`.
918
+ An empty element has no text nor any child elements, however empty elements can have attributes.
905
919
 
906
920
  **Kind**: instance property of [<code>Twig</code>](#Twig)
907
921
  **Returns**: <code>boolean</code> - true if empty element
@@ -936,7 +950,8 @@ The position in `#children` array. For root object 0
936
950
  <a name="Twig+path"></a>
937
951
 
938
952
  ### twig.path ⇒ <code>string</code>
939
- The X-Path position of the element
953
+ The X-Path position of the element
954
+ NOTE: Applies only to currently loaded elements.
940
955
 
941
956
  **Kind**: instance property of [<code>Twig</code>](#Twig)
942
957
  **Returns**: <code>string</code> - X-Path
@@ -1036,7 +1051,8 @@ Creates xml-writer from current element
1036
1051
  <a name="Twig+attr"></a>
1037
1052
 
1038
1053
  ### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
1039
- Returns attribute value or `null` if not found.<br>
1054
+ Returns attribute value or `null` if not found.<br>
1055
+ If more than one matches the condition, then it returns object as [attribute()](#attribute)
1040
1056
 
1041
1057
  **Kind**: instance property of [<code>Twig</code>](#Twig)
1042
1058
  **Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
@@ -1079,7 +1095,10 @@ Retrieve or update XML attribute. For update, the condition must be a string, i.
1079
1095
 
1080
1096
  **Example**
1081
1097
  ```js
1082
- attribute((name, val) => { return name === 'age' && val > 50})
1098
+ attribute((name, val) => { return name === 'age' && val > 50})
1099
+ attribute((name) => { return ['firstName', 'lastName'].includes(name) })
1100
+ attribute('firstName')
1101
+ attribute(/name/i)
1083
1102
  ```
1084
1103
  <a name="Twig+deleteAttribute"></a>
1085
1104
 
@@ -1342,7 +1361,8 @@ Deletes the current element from tree, same as `purge()`. The root object cannot
1342
1361
  <a name="Twig+setRoot"></a>
1343
1362
 
1344
1363
  ### twig.setRoot(name) ℗
1345
- Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
1364
+ Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
1365
+ Used internally!
1346
1366
 
1347
1367
  **Kind**: instance method of [<code>Twig</code>](#Twig)
1348
1368
  **Access**: private
@@ -1438,6 +1458,14 @@ Generic error for unsupported condition
1438
1458
  | condition | <code>\*</code> | The condition value |
1439
1459
  | t | <code>Array.&lt;string&gt;</code> | List of supported data types |
1440
1460
 
1461
+ <a name="SAX"></a>
1462
+
1463
+ ## SAX
1464
+ **Kind**: global constant
1465
+ **Version:**: 1.7.4
1466
+ **Author:**: Wernfried Domscheit
1467
+ **Copyright:**: Copyright (c) 2025 Wernfried Domscheit. All rights reserved.
1468
+ **Website:**: https://www.npmjs.com/package/xml-twig
1441
1469
  <a name="Root"></a>
1442
1470
 
1443
1471
  ## Root : <code>RootHandler</code>
@@ -1465,20 +1493,21 @@ Create a new Twig parser
1465
1493
 
1466
1494
  <a name="onStart"></a>
1467
1495
 
1468
- ## onStart(binds, node, attrs)
1496
+ ## onStart(parser, binds, node, attrs)
1469
1497
  Common Event hanlder for starting tag
1470
1498
 
1471
1499
  **Kind**: global function
1472
1500
 
1473
1501
  | Param | Type | Description |
1474
1502
  | --- | --- | --- |
1503
+ | parser | [<code>Parser</code>](#Parser) | The main parser object |
1475
1504
  | binds | <code>object</code> | Additional parameter object |
1476
1505
  | node | <code>object</code> \| <code>string</code> | Node or Node name |
1477
1506
  | attrs | <code>object</code> | Node Attributes |
1478
1507
 
1479
1508
  <a name="onClose"></a>
1480
1509
 
1481
- ## onClose(handler, parser, options, name)
1510
+ ## onClose(handler, parser, parser, options, name)
1482
1511
  Common Event hanlder for closing tag. On closed elements it either calls the Handler function or emits the specified event.
1483
1512
 
1484
1513
  **Kind**: global function
@@ -1486,6 +1515,7 @@ Common Event hanlder for closing tag. On closed elements it either calls the Han
1486
1515
  | Param | Type | Description |
1487
1516
  | --- | --- | --- |
1488
1517
  | handler | [<code>TwigHandler</code>](#TwigHandler) \| [<code>Array.&lt;TwigHandler&gt;</code>](#TwigHandler) | Object or array of element specification and function to handle elements |
1518
+ | parser | [<code>Parser</code>](#Parser) | The main parser object |
1489
1519
  | 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 |
1490
1520
  | options | [<code>ParserOptions</code>](#ParserOptions) | Object of optional options |
1491
1521
  | name | <code>string</code> | Event handler parameter |
@@ -1506,7 +1536,7 @@ Optional settings for the Twig parser
1506
1536
  | [trim] | <code>boolean</code> | If `true`, then turn any whitespace into a single space. Text and comments are trimmed. |
1507
1537
  | [resumeAfterError] | <code>boolean</code> | If `true` then parser continues reading after an error. Otherwise it throws exception. |
1508
1538
  | [partial] | <code>boolean</code> | If `true` then unhandled elements are purged. |
1509
- | [file] | <code>string</code> | The filename to be parsed. Just used for information and logging purpose. |
1539
+ | [file] | <code>string</code> | Optional. The name of file to be parsed. Just used for information and logging purpose. |
1510
1540
 
1511
1541
  **Example**
1512
1542
  ```js
@@ -1515,7 +1545,9 @@ Optional settings for the Twig parser
1515
1545
  <a name="TwigHandler"></a>
1516
1546
 
1517
1547
  ## TwigHandler
1518
- Reference to handler functions for Twig objects.<br>
1548
+ Reference to handler functions for Twig objects.<br>
1549
+ Element can be specified as string, Regular Expression, custom function, `Twig.Root` or `Twig.Any`<br>
1550
+ You can specify a `function` or a `event` name
1519
1551
 
1520
1552
  **Kind**: global typedef
1521
1553
  **Properties**
@@ -1529,7 +1561,13 @@ Reference to handler functions for Twig objects.<br>
1529
1561
  <a name="HandlerCondition"></a>
1530
1562
 
1531
1563
  ## HandlerCondition : <code>string</code> \| <code>Array.&lt;string&gt;</code> \| <code>RegExp</code> \| [<code>HandlerConditionFilter</code>](#HandlerConditionFilter) \| [<code>Root</code>](#Root) \| [<code>Any</code>](#Any)
1532
- Condition to specify when handler shall be called<br>
1533
- If `string` then the element name must be equal to the string
1534
- If `string[]` then the element name must be included in string array
1535
- If `RegExp` then the element name must match the Regular Expression
1536
- If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
1537
- Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
1538
- Use `Twig.Any` to call the handler on every element
1564
+ Condition to specify when handler shall be called<br>
1565
+ - If `string` then the element name must be equal to the string
1566
+ - If `string[]` then the element name must be included in string array
1567
+ - If `RegExp` then the element name must match the Regular Expression
1568
+ - If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
1569
+ - Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
1570
+ - Use `Twig.Any` to call the handler on every element
1539
1571
 
1540
1572
  **Kind**: global typedef
1541
1573
  <a name="HandlerFunction"></a>
@@ -1558,7 +1596,12 @@ Custom filter function to specify when handler shall be called
1558
1596
  <a name="ElementCondition"></a>
1559
1597
 
1560
1598
  ## ElementCondition : <code>string</code> \| <code>RegExp</code> \| [<code>ElementConditionFilter</code>](#ElementConditionFilter) \| [<code>Twig</code>](#Twig) \| <code>undefined</code>
1561
- Optional condition to get elements<br>
1562
- If `undefined`, then all elements are returned.<br>
1563
- If `string` then the element name must be equal to the string
1564
- If `RegExp` then the element name must match the Regular Expression
1565
- If [ElementConditionFilter](#ElementConditionFilter) then function must return `true`
1566
- Use [Twig](#Twig) object to find a specific element
1599
+ Optional condition to get elements<br>
1600
+ - If `undefined`, then all elements are returned.<br>
1601
+ - If `string` then the element name must be equal to the string
1602
+ - If `RegExp` then the element name must match the Regular Expression
1603
+ - If [ElementConditionFilter](#ElementConditionFilter) then function must return `true`
1604
+ - Use [Twig](#Twig) object to find a specific element
1567
1605
 
1568
1606
  **Kind**: global typedef
1569
1607
  <a name="ElementConditionFilter"></a>
@@ -1585,11 +1628,18 @@ Custom filter function to select desired elements
1585
1628
  | --- | --- | --- |
1586
1629
  | [currentLine] | <code>number</code> | The currently processed line in the XML-File. |
1587
1630
  | [currentColumn] | <code>number</code> | The currently processed column in the XML-File. |
1631
+ | [file] | <code>string</code> | The name of file to be parsed. Just used for information and logging purpose. |
1632
+ | [twig] | <code>object</code> | Object with XML tree and current XML element |
1633
+ | [method] | <code>string</code> | The underlying parser. Either `'sax'`, `'expat'`. |
1588
1634
 
1589
1635
  <a name="AttributeCondition"></a>
1590
1636
 
1591
1637
  ## AttributeCondition : <code>string</code> \| <code>RegExp</code> \| [<code>AttributeConditionFilter</code>](#AttributeConditionFilter)
1592
- Optional condition to get attributes<br>
1593
- If `undefined`, then all attributes are returned.<br>
1594
- If `string` then the attribute name must be equal to the string
1595
- If `RegExp` then the attribute name must match the Regular Expression
1596
- If [AttributeConditionFilter](#AttributeConditionFilter) then the attribute must filter function
1638
+ Optional condition to get attributes<br>
1639
+ - If `undefined`, then all attributes are returned.<br>
1640
+ - If `string` then the attribute name must be equal to the string
1641
+ - If `RegExp` then the attribute name must match the Regular Expression
1642
+ - If [AttributeConditionFilter](#AttributeConditionFilter) then the attribute must filter function
1597
1643
 
1598
1644
  **Kind**: global typedef
1599
1645
  <a name="AttributeConditionFilter"></a>
package/package.json CHANGED
@@ -5,18 +5,24 @@
5
5
  },
6
6
  "name": "xml-twig",
7
7
  "description": "Node module for processing huge XML documents in tree mode",
8
- "version": "1.7.1",
8
+ "version": "1.7.4",
9
9
  "main": "twig.js",
10
10
  "directories": {
11
11
  "doc": "doc"
12
12
  },
13
+ "files": [
14
+ "twig.js",
15
+ "doc/*.md"
16
+ ],
13
17
  "devDependencies": {
14
18
  "jsdoc-to-markdown": "^9.0.0",
15
19
  "luxon": "^3.5.0",
16
20
  "node-expat": "^2.4.1"
17
21
  },
18
22
  "scripts": {
19
- "test": "node demo.js"
23
+ "test": "node demo.js",
24
+ "postversion": "sed -i -e \"s/@version: .*/@version: %npm_package_version%/\" twig.js",
25
+ "prepare": "jsdoc2md --private twig.js > doc/twig.md"
20
26
  },
21
27
  "repository": {
22
28
  "type": "git",