xml-twig 1.9.0 → 1.9.1

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 +35 -67
  2. package/package.json +1 -1
  3. package/twig.js +21 -4
package/doc/twig.md CHANGED
@@ -127,6 +127,7 @@ You can specify a <code>function</code> or a <code>event</code> name</p>
127
127
  * [.children](#Twig+children) : [<code>Array.&lt;Twig&gt;</code>](#Twig) ℗
128
128
  * [.parent](#Twig+parent) : [<code>Twig</code>](#Twig) \| <code>undefined</code> ℗
129
129
  * [.pinned](#Twig+pinned) : <code>boolean</code> ℗
130
+ * [.trim](#Twig+trim) : <code>boolean</code> ℗
130
131
  * [.purge](#Twig+purge)
131
132
  * [.purgeUpTo](#Twig+purgeUpTo)
132
133
  * [.escapeEntity](#Twig+escapeEntity)
@@ -240,6 +241,13 @@ The parent object. Undefined on root element
240
241
  ### twig.pinned : <code>boolean</code> ℗
241
242
  Determines whether twig is needed in partial load
242
243
 
244
+ **Kind**: instance property of [<code>Twig</code>](#Twig)
245
+ **Access**: private
246
+ <a name="Twig+trim"></a>
247
+
248
+ ### twig.trim : <code>boolean</code> ℗
249
+ Determines whether text is trimmed
250
+
243
251
  **Kind**: instance property of [<code>Twig</code>](#Twig)
244
252
  **Access**: private
245
253
  <a name="Twig+purge"></a>
@@ -251,8 +259,7 @@ Purges the current, typically used after element has been processed.<br>The root
251
259
  <a name="Twig+purgeUpTo"></a>
252
260
 
253
261
  ### twig.purgeUpTo
254
- Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
255
- The `elt` object is not purged. If you like to purge including `elt`, use `.purgeUpTo(elt.previous())`
262
+ Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
256
263
 
257
264
  **Kind**: instance property of [<code>Twig</code>](#Twig)
258
265
 
@@ -274,8 +281,7 @@ Escapes special XML characters. According W3C specification these are only `&, <
274
281
  <a name="Twig+isEmpty"></a>
275
282
 
276
283
  ### twig.isEmpty ⇒ <code>boolean</code>
277
- Returns `true` if the element is empty, otherwise `false`.
278
- An empty element has no text nor any child elements, however empty elements can have attributes.
284
+ Returns `true` if the element is empty, otherwise `false`.
279
285
 
280
286
  **Kind**: instance property of [<code>Twig</code>](#Twig)
281
287
  **Returns**: <code>boolean</code> - true if empty element
@@ -310,8 +316,7 @@ The position in `#children` array. For root object 0
310
316
  <a name="Twig+path"></a>
311
317
 
312
318
  ### twig.path ⇒ <code>string</code>
313
- The X-Path position of the element
314
- NOTE: Applies only to currently loaded elements.
319
+ The X-Path position of the element
315
320
 
316
321
  **Kind**: instance property of [<code>Twig</code>](#Twig)
317
322
  **Returns**: <code>string</code> - X-Path
@@ -332,7 +337,7 @@ Returns the name of the element. Synonym for `twig.name`
332
337
  <a name="Twig+text"></a>
333
338
 
334
339
  ### twig.text ⇒ <code>string</code>
335
- The text of the element. No matter if given as text or CDATA entity
340
+ The text of the element. No matter if given as text or CDATA entity.
336
341
 
337
342
  **Kind**: instance property of [<code>Twig</code>](#Twig)
338
343
  **Returns**: <code>string</code> - Element text or empty string
@@ -411,8 +416,7 @@ Creates xml-writer from current element
411
416
  <a name="Twig+attr"></a>
412
417
 
413
418
  ### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
414
- Returns attribute value or `null` if not found.<br>
415
- If more than one matches the condition, then it returns object as [attribute()](#attribute)
419
+ Returns attribute value or `null` if not found.<br>
416
420
 
417
421
  **Kind**: instance property of [<code>Twig</code>](#Twig)
418
422
  **Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
@@ -455,10 +459,7 @@ Retrieve or update XML attribute. For update, the condition must be a string, i.
455
459
 
456
460
  **Example**
457
461
  ```js
458
- attribute((name, val) => { return name === 'age' && val > 50})
459
- attribute((name) => { return ['firstName', 'lastName'].includes(name) })
460
- attribute('firstName')
461
- attribute(/name/i)
462
+ attribute((name, val) => { return name === 'age' && val > 50})
462
463
  ```
463
464
  <a name="Twig+deleteAttribute"></a>
464
465
 
@@ -743,8 +744,7 @@ Deletes the current element from tree, same as `purge()`. The root object cannot
743
744
  <a name="Twig+setRoot"></a>
744
745
 
745
746
  ### twig.setRoot(name) ℗
746
- Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
747
- Used internally!
747
+ Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
748
748
 
749
749
  **Kind**: instance method of [<code>Twig</code>](#Twig)
750
750
  **Access**: private
@@ -793,6 +793,7 @@ Common function to filter Twig element
793
793
  * [.children](#Twig+children) : [<code>Array.&lt;Twig&gt;</code>](#Twig) ℗
794
794
  * [.parent](#Twig+parent) : [<code>Twig</code>](#Twig) \| <code>undefined</code> ℗
795
795
  * [.pinned](#Twig+pinned) : <code>boolean</code> ℗
796
+ * [.trim](#Twig+trim) : <code>boolean</code> ℗
796
797
  * [.purge](#Twig+purge)
797
798
  * [.purgeUpTo](#Twig+purgeUpTo)
798
799
  * [.escapeEntity](#Twig+escapeEntity)
@@ -906,6 +907,13 @@ The parent object. Undefined on root element
906
907
  ### twig.pinned : <code>boolean</code> ℗
907
908
  Determines whether twig is needed in partial load
908
909
 
910
+ **Kind**: instance property of [<code>Twig</code>](#Twig)
911
+ **Access**: private
912
+ <a name="Twig+trim"></a>
913
+
914
+ ### twig.trim : <code>boolean</code> ℗
915
+ Determines whether text is trimmed
916
+
909
917
  **Kind**: instance property of [<code>Twig</code>](#Twig)
910
918
  **Access**: private
911
919
  <a name="Twig+purge"></a>
@@ -917,8 +925,7 @@ Purges the current, typically used after element has been processed.<br>The root
917
925
  <a name="Twig+purgeUpTo"></a>
918
926
 
919
927
  ### twig.purgeUpTo
920
- Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
921
- The `elt` object is not purged. If you like to purge including `elt`, use `.purgeUpTo(elt.previous())`
928
+ Purges up to the elt element. This allows you to keep part of the tree in memory when you purge.<br>
922
929
 
923
930
  **Kind**: instance property of [<code>Twig</code>](#Twig)
924
931
 
@@ -940,8 +947,7 @@ Escapes special XML characters. According W3C specification these are only `&, <
940
947
  <a name="Twig+isEmpty"></a>
941
948
 
942
949
  ### twig.isEmpty ⇒ <code>boolean</code>
943
- Returns `true` if the element is empty, otherwise `false`.
944
- An empty element has no text nor any child elements, however empty elements can have attributes.
950
+ Returns `true` if the element is empty, otherwise `false`.
945
951
 
946
952
  **Kind**: instance property of [<code>Twig</code>](#Twig)
947
953
  **Returns**: <code>boolean</code> - true if empty element
@@ -976,8 +982,7 @@ The position in `#children` array. For root object 0
976
982
  <a name="Twig+path"></a>
977
983
 
978
984
  ### twig.path ⇒ <code>string</code>
979
- The X-Path position of the element
980
- NOTE: Applies only to currently loaded elements.
985
+ The X-Path position of the element
981
986
 
982
987
  **Kind**: instance property of [<code>Twig</code>](#Twig)
983
988
  **Returns**: <code>string</code> - X-Path
@@ -998,7 +1003,7 @@ Returns the name of the element. Synonym for `twig.name`
998
1003
  <a name="Twig+text"></a>
999
1004
 
1000
1005
  ### twig.text ⇒ <code>string</code>
1001
- The text of the element. No matter if given as text or CDATA entity
1006
+ The text of the element. No matter if given as text or CDATA entity.
1002
1007
 
1003
1008
  **Kind**: instance property of [<code>Twig</code>](#Twig)
1004
1009
  **Returns**: <code>string</code> - Element text or empty string
@@ -1077,8 +1082,7 @@ Creates xml-writer from current element
1077
1082
  <a name="Twig+attr"></a>
1078
1083
 
1079
1084
  ### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
1080
- Returns attribute value or `null` if not found.<br>
1081
- If more than one matches the condition, then it returns object as [attribute()](#attribute)
1085
+ Returns attribute value or `null` if not found.<br>
1082
1086
 
1083
1087
  **Kind**: instance property of [<code>Twig</code>](#Twig)
1084
1088
  **Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
@@ -1121,10 +1125,7 @@ Retrieve or update XML attribute. For update, the condition must be a string, i.
1121
1125
 
1122
1126
  **Example**
1123
1127
  ```js
1124
- attribute((name, val) => { return name === 'age' && val > 50})
1125
- attribute((name) => { return ['firstName', 'lastName'].includes(name) })
1126
- attribute('firstName')
1127
- attribute(/name/i)
1128
+ attribute((name, val) => { return name === 'age' && val > 50})
1128
1129
  ```
1129
1130
  <a name="Twig+deleteAttribute"></a>
1130
1131
 
@@ -1409,8 +1410,7 @@ Deletes the current element from tree, same as `purge()`. The root object cannot
1409
1410
  <a name="Twig+setRoot"></a>
1410
1411
 
1411
1412
  ### twig.setRoot(name) ℗
1412
- Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
1413
- Used internally!
1413
+ Sets the name of root element. In some cases the root is created before the XML-Root element is available<br>
1414
1414
 
1415
1415
  **Kind**: instance method of [<code>Twig</code>](#Twig)
1416
1416
  **Access**: private
@@ -1510,7 +1510,7 @@ Generic error for unsupported condition
1510
1510
 
1511
1511
  ## SAX
1512
1512
  **Kind**: global constant
1513
- **Version:**: 1.9.0
1513
+ **Version:**: 1.9.1
1514
1514
  **Author:**: Wernfried Domscheit
1515
1515
  **Copyright:**: Copyright (c) 2025 Wernfried Domscheit. All rights reserved.
1516
1516
  **Website:**: https://www.npmjs.com/package/xml-twig
@@ -1593,9 +1593,7 @@ Optional settings for the Twig parser
1593
1593
  <a name="TwigHandler"></a>
1594
1594
 
1595
1595
  ## TwigHandler
1596
- Reference to handler functions for Twig objects.<br>
1597
- Element can be specified as string, Regular Expression, custom function, `Twig.Root` or `Twig.Any`<br>
1598
- You can specify a `function` or a `event` name
1596
+ Reference to handler functions for Twig objects.<br>
1599
1597
 
1600
1598
  **Kind**: global typedef
1601
1599
  **Properties**
@@ -1609,13 +1607,7 @@ You can specify a `function` or a `event` name
1609
1607
  <a name="HandlerCondition"></a>
1610
1608
 
1611
1609
  ## 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)
1612
- Condition to specify when handler shall be called<br>
1613
- - If `string` then the element name must be equal to the string
1614
- - If `string[]` then the element name must be included in string array
1615
- - If `RegExp` then the element name must match the Regular Expression
1616
- - If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
1617
- - Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
1618
- - Use `Twig.Any` to call the handler on every element
1610
+ Condition to specify when handler shall be called<br>
1619
- If `string` then the element name must be equal to the string
1620
- If `string[]` then the element name must be included in string array
1621
- If `RegExp` then the element name must match the Regular Expression
1622
- If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
1623
- Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
1624
- Use `Twig.Any` to call the handler on every element
1625
1611
 
1626
1612
  **Kind**: global typedef
1627
1613
  <a name="HandlerFunction"></a>
@@ -1644,12 +1636,7 @@ Custom filter function to specify when handler shall be called
1644
1636
  <a name="ElementCondition"></a>
1645
1637
 
1646
1638
  ## ElementCondition : <code>string</code> \| <code>RegExp</code> \| [<code>ElementConditionFilter</code>](#ElementConditionFilter) \| [<code>Twig</code>](#Twig) \| <code>undefined</code>
1647
- Optional condition to get elements<br>
1648
- - If `undefined`, then all elements are returned.<br>
1649
- - If `string` then the element name must be equal to the string
1650
- - If `RegExp` then the element name must match the Regular Expression
1651
- - If [ElementConditionFilter](#ElementConditionFilter) then function must return `true`
1652
- - Use [Twig](#Twig) object to find a specific element
1639
+ Optional condition to get elements<br>
1653
- If `undefined`, then all elements are returned.<br>
1654
- If `string` then the element name must be equal to the string
1655
- If `RegExp` then the element name must match the Regular Expression
1656
- If [ElementConditionFilter](#ElementConditionFilter) then function must return `true`
1657
- Use [Twig](#Twig) object to find a specific element
1658
1640
 
1659
1641
  **Kind**: global typedef
1660
1642
  <a name="ElementConditionFilter"></a>
@@ -1683,11 +1670,7 @@ Custom filter function to select desired elements
1683
1670
  <a name="AttributeCondition"></a>
1684
1671
 
1685
1672
  ## AttributeCondition : <code>string</code> \| <code>RegExp</code> \| [<code>AttributeConditionFilter</code>](#AttributeConditionFilter)
1686
- Optional condition to get attributes<br>
1687
- - If `undefined`, then all attributes are returned.<br>
1688
- - If `string` then the attribute name must be equal to the string
1689
- - If `RegExp` then the attribute name must match the Regular Expression
1690
- - If [AttributeConditionFilter](#AttributeConditionFilter) then the attribute must filter function
1673
+ Optional condition to get attributes<br>
1691
- If `undefined`, then all attributes are returned.<br>
1692
- If `string` then the attribute name must be equal to the string
1693
- If `RegExp` then the attribute name must match the Regular Expression
1694
- If [AttributeConditionFilter](#AttributeConditionFilter) then the attribute must filter function
1695
1674
 
1696
1675
  **Kind**: global typedef
1697
1676
  <a name="AttributeConditionFilter"></a>
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  },
6
6
  "name": "xml-twig",
7
7
  "description": "Node module for processing huge XML documents in tree mode",
8
- "version": "1.9.0",
8
+ "version": "1.9.1",
9
9
  "main": "twig.js",
10
10
  "directories": {
11
11
  "doc": "doc"
package/twig.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @version: 1.9.0
2
+ * @version: 1.9.1
3
3
  * @author: Wernfried Domscheit
4
4
  * @copyright: Copyright (c) 2025 Wernfried Domscheit. All rights reserved.
5
5
  * @website: https://www.npmjs.com/package/xml-twig
@@ -278,6 +278,12 @@ function createParser(handler, options = {}) {
278
278
  enumerable: true
279
279
  });
280
280
 
281
+ Object.defineProperty(parser, 'trimText', {
282
+ value: options.trim,
283
+ writable: false,
284
+ enumerable: true
285
+ });
286
+
281
287
  if (options.file != null) {
282
288
  Object.defineProperty(parser, 'file', {
283
289
  value: options.file,
@@ -289,7 +295,7 @@ function createParser(handler, options = {}) {
289
295
  // Common events
290
296
  parser.on('text', function (str) {
291
297
  if (parser.twig.current === null) return;
292
- parser.twig.current.text = options.trim ? str.trim() : str;
298
+ parser.twig.current.text = str;
293
299
  });
294
300
 
295
301
  parser.on("comment", function (str) {
@@ -513,6 +519,12 @@ class Twig {
513
519
  */
514
520
  #pinned = false;
515
521
 
522
+ /**
523
+ * Determines whether text is trimmed
524
+ * @type {boolean}
525
+ */
526
+ #trim = true;
527
+
516
528
  /**
517
529
  * Create a new Twig object
518
530
  * @param {Parser} parser - The main parser object
@@ -525,6 +537,7 @@ class Twig {
525
537
  if (index === undefined)
526
538
  parser.twig.current = this;
527
539
 
540
+ this.#trim = parser.trimText;
528
541
  if (name === null) {
529
542
  // Root element not available yet
530
543
  parser.twig.tree = this;
@@ -698,11 +711,15 @@ class Twig {
698
711
  }
699
712
 
700
713
  /**
701
- * The text of the element. No matter if given as text or CDATA entity
714
+ * The text of the element. No matter if given as text or CDATA entity.
715
+ * If option `trim: true`, then whitespace from both ends of the string are removed
702
716
  * @returns {string} Element text or empty string
703
717
  */
704
718
  get text() {
705
- return this.#text ?? '';
719
+ if (this.#text === null)
720
+ return ''
721
+ else
722
+ return this.#trim ? this.#text.trim() : this.#text;
706
723
  }
707
724
 
708
725
  /**