xml-twig 1.0.5 → 1.1.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 +81 -84
- package/doc/twig.md +187 -144
- package/package.json +3 -2
- package/samples/sample.js +88 -0
- package/twig.js +164 -187
- package/demo/demo.js +0 -18
- /package/{demo → samples}/memory-test.js +0 -0
- /package/{demo → samples}/speed-test.js +0 -0
package/doc/twig.md
CHANGED
|
@@ -14,9 +14,16 @@
|
|
|
14
14
|
<dt><a href="#UnsupportedCondition">UnsupportedCondition</a></dt>
|
|
15
15
|
<dd><p>Generic error for unsupported data types</p>
|
|
16
16
|
</dd>
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
</dl>
|
|
18
|
+
|
|
19
|
+
## Constants
|
|
20
|
+
|
|
21
|
+
<dl>
|
|
22
|
+
<dt><a href="#Root">Root</a> : <code>RootHandler</code></dt>
|
|
23
|
+
<dd><p>`</p>
|
|
19
24
|
</dd>
|
|
25
|
+
<dt><a href="#Any">Any</a> : <code>AnyHandler</code></dt>
|
|
26
|
+
<dd></dd>
|
|
20
27
|
</dl>
|
|
21
28
|
|
|
22
29
|
## Functions
|
|
@@ -34,25 +41,39 @@
|
|
|
34
41
|
<dd><p>Optional settings for the Twig parser</p>
|
|
35
42
|
</dd>
|
|
36
43
|
<dt><a href="#TwigHandler">TwigHandler</a></dt>
|
|
37
|
-
<dd><p>Reference to handler functions for Twig objects.<br>
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
<dd><p>Reference to handler functions for Twig objects.<br>
|
|
45
|
+
Element can be specified as string, Regular Expression, custom function, <code>Twig.Root</code> or <code>Twig.Any</code><br>
|
|
46
|
+
You can specify a <code>function</code> or a <code>event</code> name</p>
|
|
47
|
+
</dd>
|
|
48
|
+
<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></dt>
|
|
49
|
+
<dd><p>Condition to specify when handler shall be called<br> </p>
|
|
50
|
+
<ul>
|
|
51
|
+
<li>If <code>undefined</code>, then all elements are returned.<br> </li>
|
|
52
|
+
<li>If <code>string</code> then the element name must be equal to the string</li>
|
|
53
|
+
<li>If <code>RegExp</code> then the element name must match the Regular Expression</li>
|
|
54
|
+
<li>If <a href="#HandlerConditionFilter">HandlerConditionFilter</a> then function must return <code>true</code></li>
|
|
55
|
+
<li>Use <code>Twig.Root</code> to call the handler on root element, i.e. when the end of document is reached</li>
|
|
56
|
+
<li>Use <code>Twig.Any</code> to call the handler on every element</li>
|
|
57
|
+
</ul>
|
|
40
58
|
</dd>
|
|
41
59
|
<dt><a href="#HandlerFunction">HandlerFunction</a></dt>
|
|
42
60
|
<dd><p>Handler function for Twig objects, i.e. the way you like to use the XML element.</p>
|
|
43
61
|
</dd>
|
|
62
|
+
<dt><a href="#HandlerConditionFilter">HandlerConditionFilter</a> : <code>function</code></dt>
|
|
63
|
+
<dd><p>Custom filter function to specify when handler shall be called</p>
|
|
64
|
+
</dd>
|
|
44
65
|
<dt><a href="#ElementCondition">ElementCondition</a> : <code>string</code> | <code>RegExp</code> | <code><a href="#ElementConditionFilter">ElementConditionFilter</a></code> | <code><a href="#Twig">Twig</a></code></dt>
|
|
45
66
|
<dd><p>Optional condition to get elements<br> </p>
|
|
46
67
|
<ul>
|
|
47
68
|
<li>If <code>undefined</code>, then all elements are returned.<br> </li>
|
|
48
69
|
<li>If <code>string</code> then the element name must be equal to the string</li>
|
|
49
70
|
<li>If <code>RegExp</code> then the element name must match the Regular Expression</li>
|
|
50
|
-
<li>If <a href="#ElementConditionFilter">ElementConditionFilter</a> then
|
|
51
|
-
<li>Use <a href="#Twig">Twig</a> object to find a specific element
|
|
71
|
+
<li>If <a href="#ElementConditionFilter">ElementConditionFilter</a> then function must return <code>true</code></li>
|
|
72
|
+
<li>Use <a href="#Twig">Twig</a> object to find a specific element</li>
|
|
52
73
|
</ul>
|
|
53
74
|
</dd>
|
|
54
75
|
<dt><a href="#ElementConditionFilter">ElementConditionFilter</a> : <code>function</code></dt>
|
|
55
|
-
<dd><p>Custom filter function to
|
|
76
|
+
<dd><p>Custom filter function to select desired elements</p>
|
|
56
77
|
</dd>
|
|
57
78
|
<dt><a href="#AttributeCondition">AttributeCondition</a> : <code>string</code> | <code>RegExp</code> | <code><a href="#AttributeConditionFilter">AttributeConditionFilter</a></code></dt>
|
|
58
79
|
<dd><p>Optional condition to get attributes<br> </p>
|
|
@@ -93,23 +114,24 @@ Otherwise the element name must be equal to the string or Regular Expression. Yo
|
|
|
93
114
|
* [.name](#Twig+name) ℗
|
|
94
115
|
* [.children](#Twig+children) ℗
|
|
95
116
|
* [.parent](#Twig+parent) ℗
|
|
96
|
-
* [.
|
|
97
|
-
* [.level](#Twig+level) ℗
|
|
117
|
+
* [.pinned](#Twig+pinned) ℗
|
|
98
118
|
* [.isEmpty](#Twig+isEmpty) ⇒ <code>boolean</code>
|
|
99
119
|
* [.level](#Twig+level) ⇒ <code>number</code>
|
|
100
120
|
* [.isRoot](#Twig+isRoot) ⇒ <code>boolean</code>
|
|
101
121
|
* [.hasChildren](#Twig+hasChildren) ⇒ <code>boolean</code>
|
|
102
|
-
* [.line](#Twig+line) ⇒ <code>number</code>
|
|
103
|
-
* [.column](#Twig+column) ⇒ <code>number</code>
|
|
104
122
|
* [.index](#Twig+index) ⇒ <code>number</code>
|
|
105
123
|
* [.name](#Twig+name) ⇒ <code>string</code>
|
|
106
124
|
* [.tag](#Twig+tag) ⇒ <code>string</code>
|
|
107
125
|
* [.text](#Twig+text) ⇒ <code>string</code>
|
|
108
126
|
* [.text](#Twig+text)
|
|
127
|
+
* [.pin](#Twig+pin)
|
|
128
|
+
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
129
|
+
* [.text](#Twig+text)
|
|
109
130
|
* [.close](#Twig+close)
|
|
110
131
|
* [.addChild](#Twig+addChild) ℗
|
|
111
132
|
* [.writer](#Twig+writer) ⇒ <code>XMLWriter</code>
|
|
112
133
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
134
|
+
* [.attributes](#Twig+attributes) ⇒ <code>object</code>
|
|
113
135
|
* [.hasAttribute](#Twig+hasAttribute) ⇒ <code>boolean</code>
|
|
114
136
|
* [.attribute](#Twig+attribute) ⇒ <code>object</code>
|
|
115
137
|
* [.root](#Twig+root) ⇒ [<code>Twig</code>](#Twig)
|
|
@@ -136,8 +158,8 @@ Otherwise the element name must be equal to the string or Regular Expression. Yo
|
|
|
136
158
|
* [.purge()](#Twig+purge)
|
|
137
159
|
* [.purgeUpTo(elt)](#Twig+purgeUpTo)
|
|
138
160
|
* [.setRoot(name)](#Twig+setRoot) ℗
|
|
139
|
-
* [.filterElements(
|
|
140
|
-
* [.
|
|
161
|
+
* [.filterElements(elements, condition)](#Twig+filterElements) ⇒ [<code>Array.<Twig></code>](#Twig)
|
|
162
|
+
* [.testElement(element, condition)](#Twig+testElement) ⇒ <code>boolean</code>
|
|
141
163
|
|
|
142
164
|
<a name="new_Twig_new"></a>
|
|
143
165
|
|
|
@@ -154,7 +176,7 @@ Create a new Twig object
|
|
|
154
176
|
| --- | --- | --- |
|
|
155
177
|
| name | <code>string</code> | The name of the XML element |
|
|
156
178
|
| parent | [<code>Twig</code>](#Twig) | The parent object |
|
|
157
|
-
| attributes | <code>object</code> |
|
|
179
|
+
| attributes | <code>object</code> | Attribute object |
|
|
158
180
|
|
|
159
181
|
<a name="Twig+attributes"></a>
|
|
160
182
|
|
|
@@ -211,27 +233,16 @@ Create a new Twig object
|
|
|
211
233
|
| --- | --- | --- |
|
|
212
234
|
| #parent | [<code>Twig</code>](#Twig) | The parent object. Undefined on root element |
|
|
213
235
|
|
|
214
|
-
<a name="Twig+
|
|
236
|
+
<a name="Twig+pinned"></a>
|
|
215
237
|
|
|
216
|
-
### twig.
|
|
238
|
+
### twig.pinned ℗
|
|
217
239
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
218
240
|
**Access**: private
|
|
219
241
|
**Properties**
|
|
220
242
|
|
|
221
243
|
| Name | Type | Description |
|
|
222
244
|
| --- | --- | --- |
|
|
223
|
-
| #
|
|
224
|
-
|
|
225
|
-
<a name="Twig+level"></a>
|
|
226
|
-
|
|
227
|
-
### twig.level ℗
|
|
228
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
229
|
-
**Access**: private
|
|
230
|
-
**Properties**
|
|
231
|
-
|
|
232
|
-
| Name | Type | Description |
|
|
233
|
-
| --- | --- | --- |
|
|
234
|
-
| #level | <code>number</code> | Root element is level 0, children have 1 and so on |
|
|
245
|
+
| #pinned | <code>boolean</code> | Determines whether twig is needed in partial load |
|
|
235
246
|
|
|
236
247
|
<a name="Twig+isEmpty"></a>
|
|
237
248
|
|
|
@@ -261,20 +272,6 @@ Returns `true` if element has child elements
|
|
|
261
272
|
|
|
262
273
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
263
274
|
**Returns**: <code>boolean</code> - true if has child elements exists
|
|
264
|
-
<a name="Twig+line"></a>
|
|
265
|
-
|
|
266
|
-
### twig.line ⇒ <code>number</code>
|
|
267
|
-
Returns the line where current element is closed
|
|
268
|
-
|
|
269
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
270
|
-
**Returns**: <code>number</code> - Current line
|
|
271
|
-
<a name="Twig+column"></a>
|
|
272
|
-
|
|
273
|
-
### twig.column ⇒ <code>number</code>
|
|
274
|
-
Returns the column where current element is closed
|
|
275
|
-
|
|
276
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
277
|
-
**Returns**: <code>number</code> - Current column
|
|
278
275
|
<a name="Twig+index"></a>
|
|
279
276
|
|
|
280
277
|
### twig.index ⇒ <code>number</code>
|
|
@@ -302,7 +299,7 @@ Returns the name of the element. Synonym for `twig.name`
|
|
|
302
299
|
The text of the element. No matter if given as text or CDATA entity
|
|
303
300
|
|
|
304
301
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
305
|
-
**Returns**: <code>string</code> - Element text
|
|
302
|
+
**Returns**: <code>string</code> - Element text or empty string
|
|
306
303
|
<a name="Twig+text"></a>
|
|
307
304
|
|
|
308
305
|
### twig.text
|
|
@@ -318,17 +315,40 @@ Modifies the text of the element
|
|
|
318
315
|
| --- | --- | --- |
|
|
319
316
|
| value | <code>string</code> | New value of the attribute |
|
|
320
317
|
|
|
321
|
-
<a name="Twig+
|
|
318
|
+
<a name="Twig+pin"></a>
|
|
322
319
|
|
|
323
|
-
### twig.
|
|
324
|
-
|
|
320
|
+
### twig.pin
|
|
321
|
+
Pins the current element. Used for partial reading.
|
|
322
|
+
|
|
323
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
324
|
+
<a name="Twig+pinned"></a>
|
|
325
|
+
|
|
326
|
+
### twig.pinned ⇒ <code>boolean</code>
|
|
327
|
+
Checks if element is pinned
|
|
328
|
+
|
|
329
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
330
|
+
**Returns**: <code>boolean</code> - `true` when the element is pinned
|
|
331
|
+
<a name="Twig+text"></a>
|
|
332
|
+
|
|
333
|
+
### twig.text
|
|
334
|
+
Modifies the text of the element
|
|
325
335
|
|
|
326
336
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
337
|
+
**Throws**:
|
|
338
|
+
|
|
339
|
+
- [<code>UnsupportedType</code>](#UnsupportedType) - If value is not a string or numeric type
|
|
340
|
+
|
|
327
341
|
|
|
328
342
|
| Param | Type | Description |
|
|
329
343
|
| --- | --- | --- |
|
|
330
|
-
|
|
|
344
|
+
| value | <code>string</code> | New value of the attribute |
|
|
345
|
+
|
|
346
|
+
<a name="Twig+close"></a>
|
|
347
|
+
|
|
348
|
+
### twig.close
|
|
349
|
+
Closes the element
|
|
331
350
|
|
|
351
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
332
352
|
<a name="Twig+addChild"></a>
|
|
333
353
|
|
|
334
354
|
### twig.addChild ℗
|
|
@@ -351,20 +371,27 @@ Creates xml-writer from current element
|
|
|
351
371
|
|
|
352
372
|
| Param | Type | Description |
|
|
353
373
|
| --- | --- | --- |
|
|
354
|
-
|
|
|
374
|
+
| par | <code>boolean</code> \| <code>string</code> \| <code>XMLWriter</code> | `true` or intention character or an already created XMLWriter |
|
|
355
375
|
|
|
356
376
|
<a name="Twig+attr"></a>
|
|
357
377
|
|
|
358
378
|
### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
359
|
-
Returns
|
|
379
|
+
Returns attribute value or `null` if not found.<br>
|
|
360
380
|
|
|
361
381
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
362
|
-
**Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the
|
|
382
|
+
**Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
|
|
363
383
|
|
|
364
384
|
| Param | Type | Description |
|
|
365
385
|
| --- | --- | --- |
|
|
366
386
|
| condition | [<code>AttributeCondition</code>](#AttributeCondition) | Optional condition to select attribute |
|
|
367
387
|
|
|
388
|
+
<a name="Twig+attributes"></a>
|
|
389
|
+
|
|
390
|
+
### twig.attributes ⇒ <code>object</code>
|
|
391
|
+
Returns all attributes of the element
|
|
392
|
+
|
|
393
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
394
|
+
**Returns**: <code>object</code> - All XML Attributes
|
|
368
395
|
<a name="Twig+hasAttribute"></a>
|
|
369
396
|
|
|
370
397
|
### twig.hasAttribute ⇒ <code>boolean</code>
|
|
@@ -431,6 +458,7 @@ Returns the next matching element.
|
|
|
431
458
|
|
|
432
459
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
433
460
|
**Returns**: [<code>Twig</code>](#Twig) - - The next element
|
|
461
|
+
**See**: https://www.w3.org/TR/xpath-datamodel-31/#document-order
|
|
434
462
|
|
|
435
463
|
| Param | Type | Description |
|
|
436
464
|
| --- | --- | --- |
|
|
@@ -443,6 +471,7 @@ Returns the previous matching element.
|
|
|
443
471
|
|
|
444
472
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
445
473
|
**Returns**: [<code>Twig</code>](#Twig) - - The previous element
|
|
474
|
+
**See**: https://www.w3.org/TR/xpath-datamodel-31/#document-order
|
|
446
475
|
|
|
447
476
|
| Param | Type | Description |
|
|
448
477
|
| --- | --- | --- |
|
|
@@ -451,7 +480,7 @@ Returns the previous matching element.
|
|
|
451
480
|
<a name="Twig+first"></a>
|
|
452
481
|
|
|
453
482
|
### twig.first ⇒ [<code>Twig</code>](#Twig)
|
|
454
|
-
Returns the first matching element. This is
|
|
483
|
+
Returns the first matching element. This is usually the root element
|
|
455
484
|
|
|
456
485
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
457
486
|
**Returns**: [<code>Twig</code>](#Twig) - - The first element
|
|
@@ -463,7 +492,7 @@ Returns the first matching element. This is usally the first child element
|
|
|
463
492
|
<a name="Twig+last"></a>
|
|
464
493
|
|
|
465
494
|
### twig.last ⇒ [<code>Twig</code>](#Twig)
|
|
466
|
-
Returns the last matching element.
|
|
495
|
+
Returns the last matching element.
|
|
467
496
|
|
|
468
497
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
469
498
|
**Returns**: [<code>Twig</code>](#Twig) - - The last element
|
|
@@ -633,7 +662,7 @@ Purges up to the elt element. This allows you to keep part of the tree in memory
|
|
|
633
662
|
|
|
634
663
|
| Param | Type | Description |
|
|
635
664
|
| --- | --- | --- |
|
|
636
|
-
| elt | [<code>Twig</code>](#Twig) | Up to this element the tree will be purged.
|
|
665
|
+
| elt | [<code>Twig</code>](#Twig) | Up to this element the tree will be purged. The `elt` object itself is not purged.<br> If `undefined` then the current element is purged (i.e. `purge()`) |
|
|
637
666
|
|
|
638
667
|
<a name="Twig+setRoot"></a>
|
|
639
668
|
|
|
@@ -649,7 +678,7 @@ Sets the name of root element. In some cases the root is created before the XML-
|
|
|
649
678
|
|
|
650
679
|
<a name="Twig+filterElements"></a>
|
|
651
680
|
|
|
652
|
-
### twig.filterElements(
|
|
681
|
+
### twig.filterElements(elements, condition) ⇒ [<code>Array.<Twig></code>](#Twig)
|
|
653
682
|
Common function to filter Twig elements from array
|
|
654
683
|
|
|
655
684
|
**Kind**: instance method of [<code>Twig</code>](#Twig)
|
|
@@ -657,12 +686,12 @@ Common function to filter Twig elements from array
|
|
|
657
686
|
|
|
658
687
|
| Param | Type | Description |
|
|
659
688
|
| --- | --- | --- |
|
|
660
|
-
|
|
|
689
|
+
| elements | [<code>Twig</code>](#Twig) \| [<code>Array.<Twig></code>](#Twig) | Array of elements you like to filter or a single element |
|
|
661
690
|
| condition | [<code>ElementCondition</code>](#ElementCondition) | The filter condition |
|
|
662
691
|
|
|
663
|
-
<a name="Twig+
|
|
692
|
+
<a name="Twig+testElement"></a>
|
|
664
693
|
|
|
665
|
-
### twig.
|
|
694
|
+
### twig.testElement(element, condition) ⇒ <code>boolean</code>
|
|
666
695
|
Common function to filter Twig element
|
|
667
696
|
|
|
668
697
|
**Kind**: instance method of [<code>Twig</code>](#Twig)
|
|
@@ -670,7 +699,7 @@ Common function to filter Twig element
|
|
|
670
699
|
|
|
671
700
|
| Param | Type | Description |
|
|
672
701
|
| --- | --- | --- |
|
|
673
|
-
|
|
|
702
|
+
| element | [<code>Twig</code>](#Twig) | Element you like to filter |
|
|
674
703
|
| condition | [<code>ElementCondition</code>](#ElementCondition) | The filter condition |
|
|
675
704
|
|
|
676
705
|
<a name="Twig"></a>
|
|
@@ -686,23 +715,24 @@ Common function to filter Twig element
|
|
|
686
715
|
* [.name](#Twig+name) ℗
|
|
687
716
|
* [.children](#Twig+children) ℗
|
|
688
717
|
* [.parent](#Twig+parent) ℗
|
|
689
|
-
* [.
|
|
690
|
-
* [.level](#Twig+level) ℗
|
|
718
|
+
* [.pinned](#Twig+pinned) ℗
|
|
691
719
|
* [.isEmpty](#Twig+isEmpty) ⇒ <code>boolean</code>
|
|
692
720
|
* [.level](#Twig+level) ⇒ <code>number</code>
|
|
693
721
|
* [.isRoot](#Twig+isRoot) ⇒ <code>boolean</code>
|
|
694
722
|
* [.hasChildren](#Twig+hasChildren) ⇒ <code>boolean</code>
|
|
695
|
-
* [.line](#Twig+line) ⇒ <code>number</code>
|
|
696
|
-
* [.column](#Twig+column) ⇒ <code>number</code>
|
|
697
723
|
* [.index](#Twig+index) ⇒ <code>number</code>
|
|
698
724
|
* [.name](#Twig+name) ⇒ <code>string</code>
|
|
699
725
|
* [.tag](#Twig+tag) ⇒ <code>string</code>
|
|
700
726
|
* [.text](#Twig+text) ⇒ <code>string</code>
|
|
701
727
|
* [.text](#Twig+text)
|
|
728
|
+
* [.pin](#Twig+pin)
|
|
729
|
+
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
730
|
+
* [.text](#Twig+text)
|
|
702
731
|
* [.close](#Twig+close)
|
|
703
732
|
* [.addChild](#Twig+addChild) ℗
|
|
704
733
|
* [.writer](#Twig+writer) ⇒ <code>XMLWriter</code>
|
|
705
734
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
735
|
+
* [.attributes](#Twig+attributes) ⇒ <code>object</code>
|
|
706
736
|
* [.hasAttribute](#Twig+hasAttribute) ⇒ <code>boolean</code>
|
|
707
737
|
* [.attribute](#Twig+attribute) ⇒ <code>object</code>
|
|
708
738
|
* [.root](#Twig+root) ⇒ [<code>Twig</code>](#Twig)
|
|
@@ -729,8 +759,8 @@ Common function to filter Twig element
|
|
|
729
759
|
* [.purge()](#Twig+purge)
|
|
730
760
|
* [.purgeUpTo(elt)](#Twig+purgeUpTo)
|
|
731
761
|
* [.setRoot(name)](#Twig+setRoot) ℗
|
|
732
|
-
* [.filterElements(
|
|
733
|
-
* [.
|
|
762
|
+
* [.filterElements(elements, condition)](#Twig+filterElements) ⇒ [<code>Array.<Twig></code>](#Twig)
|
|
763
|
+
* [.testElement(element, condition)](#Twig+testElement) ⇒ <code>boolean</code>
|
|
734
764
|
|
|
735
765
|
<a name="new_Twig_new"></a>
|
|
736
766
|
|
|
@@ -747,7 +777,7 @@ Create a new Twig object
|
|
|
747
777
|
| --- | --- | --- |
|
|
748
778
|
| name | <code>string</code> | The name of the XML element |
|
|
749
779
|
| parent | [<code>Twig</code>](#Twig) | The parent object |
|
|
750
|
-
| attributes | <code>object</code> |
|
|
780
|
+
| attributes | <code>object</code> | Attribute object |
|
|
751
781
|
|
|
752
782
|
<a name="Twig+attributes"></a>
|
|
753
783
|
|
|
@@ -804,27 +834,16 @@ Create a new Twig object
|
|
|
804
834
|
| --- | --- | --- |
|
|
805
835
|
| #parent | [<code>Twig</code>](#Twig) | The parent object. Undefined on root element |
|
|
806
836
|
|
|
807
|
-
<a name="Twig+
|
|
837
|
+
<a name="Twig+pinned"></a>
|
|
808
838
|
|
|
809
|
-
### twig.
|
|
839
|
+
### twig.pinned ℗
|
|
810
840
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
811
841
|
**Access**: private
|
|
812
842
|
**Properties**
|
|
813
843
|
|
|
814
844
|
| Name | Type | Description |
|
|
815
845
|
| --- | --- | --- |
|
|
816
|
-
| #
|
|
817
|
-
|
|
818
|
-
<a name="Twig+level"></a>
|
|
819
|
-
|
|
820
|
-
### twig.level ℗
|
|
821
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
822
|
-
**Access**: private
|
|
823
|
-
**Properties**
|
|
824
|
-
|
|
825
|
-
| Name | Type | Description |
|
|
826
|
-
| --- | --- | --- |
|
|
827
|
-
| #level | <code>number</code> | Root element is level 0, children have 1 and so on |
|
|
846
|
+
| #pinned | <code>boolean</code> | Determines whether twig is needed in partial load |
|
|
828
847
|
|
|
829
848
|
<a name="Twig+isEmpty"></a>
|
|
830
849
|
|
|
@@ -854,20 +873,6 @@ Returns `true` if element has child elements
|
|
|
854
873
|
|
|
855
874
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
856
875
|
**Returns**: <code>boolean</code> - true if has child elements exists
|
|
857
|
-
<a name="Twig+line"></a>
|
|
858
|
-
|
|
859
|
-
### twig.line ⇒ <code>number</code>
|
|
860
|
-
Returns the line where current element is closed
|
|
861
|
-
|
|
862
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
863
|
-
**Returns**: <code>number</code> - Current line
|
|
864
|
-
<a name="Twig+column"></a>
|
|
865
|
-
|
|
866
|
-
### twig.column ⇒ <code>number</code>
|
|
867
|
-
Returns the column where current element is closed
|
|
868
|
-
|
|
869
|
-
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
870
|
-
**Returns**: <code>number</code> - Current column
|
|
871
876
|
<a name="Twig+index"></a>
|
|
872
877
|
|
|
873
878
|
### twig.index ⇒ <code>number</code>
|
|
@@ -895,7 +900,7 @@ Returns the name of the element. Synonym for `twig.name`
|
|
|
895
900
|
The text of the element. No matter if given as text or CDATA entity
|
|
896
901
|
|
|
897
902
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
898
|
-
**Returns**: <code>string</code> - Element text
|
|
903
|
+
**Returns**: <code>string</code> - Element text or empty string
|
|
899
904
|
<a name="Twig+text"></a>
|
|
900
905
|
|
|
901
906
|
### twig.text
|
|
@@ -911,17 +916,40 @@ Modifies the text of the element
|
|
|
911
916
|
| --- | --- | --- |
|
|
912
917
|
| value | <code>string</code> | New value of the attribute |
|
|
913
918
|
|
|
914
|
-
<a name="Twig+
|
|
919
|
+
<a name="Twig+pin"></a>
|
|
915
920
|
|
|
916
|
-
### twig.
|
|
917
|
-
|
|
921
|
+
### twig.pin
|
|
922
|
+
Pins the current element. Used for partial reading.
|
|
923
|
+
|
|
924
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
925
|
+
<a name="Twig+pinned"></a>
|
|
926
|
+
|
|
927
|
+
### twig.pinned ⇒ <code>boolean</code>
|
|
928
|
+
Checks if element is pinned
|
|
918
929
|
|
|
919
930
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
931
|
+
**Returns**: <code>boolean</code> - `true` when the element is pinned
|
|
932
|
+
<a name="Twig+text"></a>
|
|
933
|
+
|
|
934
|
+
### twig.text
|
|
935
|
+
Modifies the text of the element
|
|
936
|
+
|
|
937
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
938
|
+
**Throws**:
|
|
939
|
+
|
|
940
|
+
- [<code>UnsupportedType</code>](#UnsupportedType) - If value is not a string or numeric type
|
|
941
|
+
|
|
920
942
|
|
|
921
943
|
| Param | Type | Description |
|
|
922
944
|
| --- | --- | --- |
|
|
923
|
-
|
|
|
945
|
+
| value | <code>string</code> | New value of the attribute |
|
|
946
|
+
|
|
947
|
+
<a name="Twig+close"></a>
|
|
948
|
+
|
|
949
|
+
### twig.close
|
|
950
|
+
Closes the element
|
|
924
951
|
|
|
952
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
925
953
|
<a name="Twig+addChild"></a>
|
|
926
954
|
|
|
927
955
|
### twig.addChild ℗
|
|
@@ -944,20 +972,27 @@ Creates xml-writer from current element
|
|
|
944
972
|
|
|
945
973
|
| Param | Type | Description |
|
|
946
974
|
| --- | --- | --- |
|
|
947
|
-
|
|
|
975
|
+
| par | <code>boolean</code> \| <code>string</code> \| <code>XMLWriter</code> | `true` or intention character or an already created XMLWriter |
|
|
948
976
|
|
|
949
977
|
<a name="Twig+attr"></a>
|
|
950
978
|
|
|
951
979
|
### twig.attr ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
952
|
-
Returns
|
|
980
|
+
Returns attribute value or `null` if not found.<br>
|
|
953
981
|
|
|
954
982
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
955
|
-
**Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the
|
|
983
|
+
**Returns**: <code>string</code> \| <code>number</code> \| <code>object</code> - - The value of the attribute or `null` if the does not exist
|
|
956
984
|
|
|
957
985
|
| Param | Type | Description |
|
|
958
986
|
| --- | --- | --- |
|
|
959
987
|
| condition | [<code>AttributeCondition</code>](#AttributeCondition) | Optional condition to select attribute |
|
|
960
988
|
|
|
989
|
+
<a name="Twig+attributes"></a>
|
|
990
|
+
|
|
991
|
+
### twig.attributes ⇒ <code>object</code>
|
|
992
|
+
Returns all attributes of the element
|
|
993
|
+
|
|
994
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
995
|
+
**Returns**: <code>object</code> - All XML Attributes
|
|
961
996
|
<a name="Twig+hasAttribute"></a>
|
|
962
997
|
|
|
963
998
|
### twig.hasAttribute ⇒ <code>boolean</code>
|
|
@@ -1024,6 +1059,7 @@ Returns the next matching element.
|
|
|
1024
1059
|
|
|
1025
1060
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
1026
1061
|
**Returns**: [<code>Twig</code>](#Twig) - - The next element
|
|
1062
|
+
**See**: https://www.w3.org/TR/xpath-datamodel-31/#document-order
|
|
1027
1063
|
|
|
1028
1064
|
| Param | Type | Description |
|
|
1029
1065
|
| --- | --- | --- |
|
|
@@ -1036,6 +1072,7 @@ Returns the previous matching element.
|
|
|
1036
1072
|
|
|
1037
1073
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
1038
1074
|
**Returns**: [<code>Twig</code>](#Twig) - - The previous element
|
|
1075
|
+
**See**: https://www.w3.org/TR/xpath-datamodel-31/#document-order
|
|
1039
1076
|
|
|
1040
1077
|
| Param | Type | Description |
|
|
1041
1078
|
| --- | --- | --- |
|
|
@@ -1044,7 +1081,7 @@ Returns the previous matching element.
|
|
|
1044
1081
|
<a name="Twig+first"></a>
|
|
1045
1082
|
|
|
1046
1083
|
### twig.first ⇒ [<code>Twig</code>](#Twig)
|
|
1047
|
-
Returns the first matching element. This is
|
|
1084
|
+
Returns the first matching element. This is usually the root element
|
|
1048
1085
|
|
|
1049
1086
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
1050
1087
|
**Returns**: [<code>Twig</code>](#Twig) - - The first element
|
|
@@ -1056,7 +1093,7 @@ Returns the first matching element. This is usally the first child element
|
|
|
1056
1093
|
<a name="Twig+last"></a>
|
|
1057
1094
|
|
|
1058
1095
|
### twig.last ⇒ [<code>Twig</code>](#Twig)
|
|
1059
|
-
Returns the last matching element.
|
|
1096
|
+
Returns the last matching element.
|
|
1060
1097
|
|
|
1061
1098
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
1062
1099
|
**Returns**: [<code>Twig</code>](#Twig) - - The last element
|
|
@@ -1226,7 +1263,7 @@ Purges up to the elt element. This allows you to keep part of the tree in memory
|
|
|
1226
1263
|
|
|
1227
1264
|
| Param | Type | Description |
|
|
1228
1265
|
| --- | --- | --- |
|
|
1229
|
-
| elt | [<code>Twig</code>](#Twig) | Up to this element the tree will be purged.
|
|
1266
|
+
| elt | [<code>Twig</code>](#Twig) | Up to this element the tree will be purged. The `elt` object itself is not purged.<br> If `undefined` then the current element is purged (i.e. `purge()`) |
|
|
1230
1267
|
|
|
1231
1268
|
<a name="Twig+setRoot"></a>
|
|
1232
1269
|
|
|
@@ -1242,7 +1279,7 @@ Sets the name of root element. In some cases the root is created before the XML-
|
|
|
1242
1279
|
|
|
1243
1280
|
<a name="Twig+filterElements"></a>
|
|
1244
1281
|
|
|
1245
|
-
### twig.filterElements(
|
|
1282
|
+
### twig.filterElements(elements, condition) ⇒ [<code>Array.<Twig></code>](#Twig)
|
|
1246
1283
|
Common function to filter Twig elements from array
|
|
1247
1284
|
|
|
1248
1285
|
**Kind**: instance method of [<code>Twig</code>](#Twig)
|
|
@@ -1250,12 +1287,12 @@ Common function to filter Twig elements from array
|
|
|
1250
1287
|
|
|
1251
1288
|
| Param | Type | Description |
|
|
1252
1289
|
| --- | --- | --- |
|
|
1253
|
-
|
|
|
1290
|
+
| elements | [<code>Twig</code>](#Twig) \| [<code>Array.<Twig></code>](#Twig) | Array of elements you like to filter or a single element |
|
|
1254
1291
|
| condition | [<code>ElementCondition</code>](#ElementCondition) | The filter condition |
|
|
1255
1292
|
|
|
1256
|
-
<a name="Twig+
|
|
1293
|
+
<a name="Twig+testElement"></a>
|
|
1257
1294
|
|
|
1258
|
-
### twig.
|
|
1295
|
+
### twig.testElement(element, condition) ⇒ <code>boolean</code>
|
|
1259
1296
|
Common function to filter Twig element
|
|
1260
1297
|
|
|
1261
1298
|
**Kind**: instance method of [<code>Twig</code>](#Twig)
|
|
@@ -1263,7 +1300,7 @@ Common function to filter Twig element
|
|
|
1263
1300
|
|
|
1264
1301
|
| Param | Type | Description |
|
|
1265
1302
|
| --- | --- | --- |
|
|
1266
|
-
|
|
|
1303
|
+
| element | [<code>Twig</code>](#Twig) | Element you like to filter |
|
|
1267
1304
|
| condition | [<code>ElementCondition</code>](#ElementCondition) | The filter condition |
|
|
1268
1305
|
|
|
1269
1306
|
<a name="UnsupportedParser"></a>
|
|
@@ -1321,12 +1358,16 @@ Generic error for unsupported data types
|
|
|
1321
1358
|
| condition | <code>\*</code> | The condition value |
|
|
1322
1359
|
| t | <code>Array.<string></code> | List of supported data types |
|
|
1323
1360
|
|
|
1324
|
-
<a name="
|
|
1361
|
+
<a name="Root"></a>
|
|
1325
1362
|
|
|
1326
|
-
##
|
|
1327
|
-
|
|
1363
|
+
## Root : <code>RootHandler</code>
|
|
1364
|
+
`
|
|
1328
1365
|
|
|
1329
|
-
**Kind**: global
|
|
1366
|
+
**Kind**: global constant
|
|
1367
|
+
<a name="Any"></a>
|
|
1368
|
+
|
|
1369
|
+
## Any : <code>AnyHandler</code>
|
|
1370
|
+
**Kind**: global constant
|
|
1330
1371
|
<a name="createParser"></a>
|
|
1331
1372
|
|
|
1332
1373
|
## createParser(handler, options)
|
|
@@ -1335,12 +1376,12 @@ Create a new Twig parser
|
|
|
1335
1376
|
**Kind**: global function
|
|
1336
1377
|
**Throws**:
|
|
1337
1378
|
|
|
1338
|
-
- [<code>UnsupportedParser</code>](#UnsupportedParser) - For an unsupported parser. Currently `expat`
|
|
1379
|
+
- [<code>UnsupportedParser</code>](#UnsupportedParser) - For an unsupported parser. Currently `expat` and `sax` (default) are supported.
|
|
1339
1380
|
|
|
1340
1381
|
|
|
1341
1382
|
| Param | Type | Description |
|
|
1342
1383
|
| --- | --- | --- |
|
|
1343
|
-
| handler | [<code>TwigHandler</code>](#TwigHandler) \| [<code>Array.<TwigHandler></code>](#TwigHandler) |
|
|
1384
|
+
| handler | [<code>TwigHandler</code>](#TwigHandler) \| [<code>Array.<TwigHandler></code>](#TwigHandler) | Object or array of element specification and function to handle elements |
|
|
1344
1385
|
| options | [<code>ParserOptions</code>](#ParserOptions) | Object of optional options |
|
|
1345
1386
|
|
|
1346
1387
|
<a name="ParserOptions"></a>
|
|
@@ -1349,16 +1390,16 @@ Create a new Twig parser
|
|
|
1349
1390
|
Optional settings for the Twig parser
|
|
1350
1391
|
|
|
1351
1392
|
**Kind**: global typedef
|
|
1352
|
-
**Default**: <code>{ method: '
|
|
1393
|
+
**Default**: <code>{ method: 'sax', encoding: 'UTF-8', xmlns: false, trim: true, resumeAfterError: false, partial: false }</code>
|
|
1353
1394
|
|
|
1354
1395
|
| Param | Type | Description |
|
|
1355
1396
|
| --- | --- | --- |
|
|
1356
|
-
| method | <code>string</code> | The
|
|
1397
|
+
| method | <code>string</code> | The underlying parser. Either `'sax'` or `'expat'`. |
|
|
1357
1398
|
| encoding | <code>string</code> | Encoding of the XML File. Applies only to `expat` parser. |
|
|
1358
|
-
| xmlns | <code>boolean</code> | If true
|
|
1359
|
-
| trim | <code>boolean</code> | If true
|
|
1360
|
-
| resumeAfterError | <code>boolean</code> | If true then parser continues reading after an error.
|
|
1361
|
-
| partial | <code>boolean</code> | It true then unhandled elements are purged. |
|
|
1399
|
+
| xmlns | <code>boolean</code> | If `true`, then namespaces are accessible by `namespace` property. |
|
|
1400
|
+
| trim | <code>boolean</code> | If `true`, then turn any whitespace into a single space. Text and comments are trimmed. |
|
|
1401
|
+
| resumeAfterError | <code>boolean</code> | If `true` then parser continues reading after an error. Otherwise it throws exception. |
|
|
1402
|
+
| partial | <code>boolean</code> | It `true` then unhandled elements are purged. |
|
|
1362
1403
|
|
|
1363
1404
|
**Example**
|
|
1364
1405
|
```js
|
|
@@ -1367,15 +1408,22 @@ Optional settings for the Twig parser
|
|
|
1367
1408
|
<a name="TwigHandler"></a>
|
|
1368
1409
|
|
|
1369
1410
|
## TwigHandler
|
|
1370
|
-
Reference to handler functions for Twig objects.<br>
|
|
1411
|
+
Reference to handler functions for Twig objects.<br>
|
|
1371
1412
|
|
|
1372
1413
|
**Kind**: global typedef
|
|
1373
1414
|
|
|
1374
1415
|
| Param | Type | Description |
|
|
1375
1416
|
| --- | --- | --- |
|
|
1376
|
-
|
|
|
1377
|
-
|
|
|
1417
|
+
| tag | [<code>HandlerCondition</code>](#HandlerCondition) | Element specification |
|
|
1418
|
+
| function | [<code>HandlerFunction</code>](#HandlerFunction) | Definition of handler function, either anonymous or explicit function |
|
|
1419
|
+
| event | <code>string</code> | Type of the event to be emitted |
|
|
1420
|
+
|
|
1421
|
+
<a name="HandlerCondition"></a>
|
|
1378
1422
|
|
|
1423
|
+
## HandlerCondition : <code>string</code> \| <code>RegExp</code> \| [<code>HandlerConditionFilter</code>](#HandlerConditionFilter) \| <code>RootHandler</code> \| <code>AnyHandler</code>
|
|
1424
|
+
Condition to specify when handler shall be called<br>
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
If `RegExp` then the element name must match the Regular Expression
|
|
1382
|
-
If [HandlerConditionFilter](#HandlerConditionFilter) then function must return `true`
|
|
1383
|
-
Use `Twig.Root` to call the handler on root element, i.e. when the end of document is reached
|
|
1384
|
-
Use `Twig.Any` to call the handler on every element
|
|
1425
|
+
|
|
1426
|
+
**Kind**: global typedef
|
|
1385
1427
|
<a name="HandlerFunction"></a>
|
|
1386
1428
|
|
|
1387
1429
|
## HandlerFunction
|
|
@@ -1387,23 +1435,34 @@ Handler function for Twig objects, i.e. the way you like to use the XML element.
|
|
|
1387
1435
|
| --- | --- | --- |
|
|
1388
1436
|
| elt | [<code>Twig</code>](#Twig) | The current Twig element on which the function was called. |
|
|
1389
1437
|
|
|
1438
|
+
<a name="HandlerConditionFilter"></a>
|
|
1439
|
+
|
|
1440
|
+
## HandlerConditionFilter : <code>function</code>
|
|
1441
|
+
Custom filter function to specify when handler shall be called
|
|
1442
|
+
|
|
1443
|
+
**Kind**: global typedef
|
|
1444
|
+
|
|
1445
|
+
| Param | Type | Description |
|
|
1446
|
+
| --- | --- | --- |
|
|
1447
|
+
| name | <code>string</code> | Name of the element |
|
|
1448
|
+
|
|
1390
1449
|
<a name="ElementCondition"></a>
|
|
1391
1450
|
|
|
1392
1451
|
## ElementCondition : <code>string</code> \| <code>RegExp</code> \| [<code>ElementConditionFilter</code>](#ElementConditionFilter) \| [<code>Twig</code>](#Twig)
|
|
1393
|
-
Optional condition to get elements<br>
|
|
1394
|
-
If `undefined`, then all elements are returned.<br>
|
|
1395
|
-
If `string` then the element name must be equal to the string
|
|
1396
|
-
If `RegExp` then the element name must match the Regular Expression
|
|
1397
|
-
If [ElementConditionFilter](#ElementConditionFilter) then the element must filter function
|
|
1398
|
-
Use [Twig](#Twig) object to find a specific element (rarely used in `createParser(handler)`)
|
|
1452
|
+
Optional condition to get elements<br>
|
|
1399
|
-
If `undefined`, then all elements are returned.<br>
|
|
1400
|
-
If `string` then the element name must be equal to the string
|
|
1401
|
-
If `RegExp` then the element name must match the Regular Expression
|
|
1402
|
-
If [ElementConditionFilter](#ElementConditionFilter) then function must return `true`
|
|
1403
|
-
Use [Twig](#Twig) object to find a specific element
|
|
1404
1453
|
|
|
1405
1454
|
**Kind**: global typedef
|
|
1406
1455
|
<a name="ElementConditionFilter"></a>
|
|
1407
1456
|
|
|
1408
1457
|
## ElementConditionFilter : <code>function</code>
|
|
1409
|
-
Custom filter function to
|
|
1458
|
+
Custom filter function to select desired elements
|
|
1410
1459
|
|
|
1411
1460
|
**Kind**: global typedef
|
|
1412
1461
|
|
|
1413
1462
|
| Param | Type | Description |
|
|
1414
1463
|
| --- | --- | --- |
|
|
1415
1464
|
| name | <code>string</code> | Name of the element |
|
|
1416
|
-
| elt | [<code>Twig</code>](#Twig) | The
|
|
1465
|
+
| elt | [<code>Twig</code>](#Twig) | The Twig object |
|
|
1417
1466
|
|
|
1418
1467
|
<a name="AttributeCondition"></a>
|
|
1419
1468
|
|