xml-twig 1.3.0 → 1.3.2
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 +16 -0
- package/package.json +1 -1
- package/test.js +2 -2
- package/twig.js +14 -7
package/doc/twig.md
CHANGED
|
@@ -143,6 +143,7 @@ You can specify a <code>function</code> or a <code>event</code> name</p>
|
|
|
143
143
|
* [.pin](#Twig+pin)
|
|
144
144
|
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
145
145
|
* [.close](#Twig+close)
|
|
146
|
+
* [.debug](#Twig+debug) ⇒ <code>string</code>
|
|
146
147
|
* [.addChild](#Twig+addChild) ℗
|
|
147
148
|
* [.writer](#Twig+writer) ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
148
149
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
@@ -355,6 +356,13 @@ Checks if element is pinned
|
|
|
355
356
|
Closes the element
|
|
356
357
|
|
|
357
358
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
359
|
+
<a name="Twig+debug"></a>
|
|
360
|
+
|
|
361
|
+
### twig.debug ⇒ <code>string</code>
|
|
362
|
+
XML-Twig for dummies :-)
|
|
363
|
+
|
|
364
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
365
|
+
**Returns**: <code>string</code> - The XML-Tree which is currently available in RAM - no valid XML Structure
|
|
358
366
|
<a name="Twig+addChild"></a>
|
|
359
367
|
|
|
360
368
|
### twig.addChild ℗
|
|
@@ -752,6 +760,7 @@ Common function to filter Twig element
|
|
|
752
760
|
* [.pin](#Twig+pin)
|
|
753
761
|
* [.pinned](#Twig+pinned) ⇒ <code>boolean</code>
|
|
754
762
|
* [.close](#Twig+close)
|
|
763
|
+
* [.debug](#Twig+debug) ⇒ <code>string</code>
|
|
755
764
|
* [.addChild](#Twig+addChild) ℗
|
|
756
765
|
* [.writer](#Twig+writer) ⇒ [<code>XMLWriter</code>](https://www.npmjs.com/package/xml-writer)
|
|
757
766
|
* [.attr](#Twig+attr) ⇒ <code>string</code> \| <code>number</code> \| <code>object</code>
|
|
@@ -964,6 +973,13 @@ Checks if element is pinned
|
|
|
964
973
|
Closes the element
|
|
965
974
|
|
|
966
975
|
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
976
|
+
<a name="Twig+debug"></a>
|
|
977
|
+
|
|
978
|
+
### twig.debug ⇒ <code>string</code>
|
|
979
|
+
XML-Twig for dummies :-)
|
|
980
|
+
|
|
981
|
+
**Kind**: instance property of [<code>Twig</code>](#Twig)
|
|
982
|
+
**Returns**: <code>string</code> - The XML-Tree which is currently available in RAM - no valid XML Structure
|
|
967
983
|
<a name="Twig+addChild"></a>
|
|
968
984
|
|
|
969
985
|
### twig.addChild ℗
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -2,10 +2,10 @@ const fs = require('fs');
|
|
|
2
2
|
const twig = require('./twig.js');
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const parser = twig.createParser({ tag: twig.Any, function: anyHandler }, { method: "sax" });
|
|
6
6
|
//const parser = twig.createParser({ tag: twig.Any, function: anyHandler }, { method: "expat" });
|
|
7
7
|
//const parser = twig.createParser({ tag: twig.Any, function: anyHandler }, { method: "saxophone" });
|
|
8
|
-
|
|
8
|
+
fs.createReadStream(`${__dirname}/samples/bookstore.xml`).pipe(parser);
|
|
9
9
|
//fs.createReadStream(`${__dirname}/samples/breakfast-menu.xml`).pipe(parser);
|
|
10
10
|
|
|
11
11
|
//const parser = twig.createParser({ tag: twig.Any, function: nsHandler }, { method: "sax", xmlns: true });
|
package/twig.js
CHANGED
|
@@ -483,7 +483,6 @@ function reset() {
|
|
|
483
483
|
current = undefined;
|
|
484
484
|
}
|
|
485
485
|
|
|
486
|
-
|
|
487
486
|
/**
|
|
488
487
|
* Generic class modeling a XML Node
|
|
489
488
|
* @class Twig
|
|
@@ -760,21 +759,29 @@ class Twig {
|
|
|
760
759
|
Object.seal(this);
|
|
761
760
|
};
|
|
762
761
|
|
|
762
|
+
/**
|
|
763
|
+
* XML-Twig for dummies :-)
|
|
764
|
+
* @returns {string} The XML-Tree which is currently available in RAM - no valid XML Structure
|
|
765
|
+
*/
|
|
766
|
+
debug = function () {
|
|
767
|
+
return this.root().writer(true, true).output;
|
|
768
|
+
};
|
|
769
|
+
|
|
763
770
|
/**
|
|
764
771
|
* Internal recursive function used by `writer()`
|
|
765
772
|
* @param {external:XMLWriter} xw - The writer object
|
|
766
773
|
* @param {Twig[]} childArray - Array of child elements
|
|
767
774
|
*/
|
|
768
|
-
#addChild = function (xw, childArray) {
|
|
775
|
+
#addChild = function (xw, childArray, cur, debug) {
|
|
769
776
|
for (let elt of childArray) {
|
|
770
777
|
xw.startElement(elt.name);
|
|
771
778
|
for (let [key, val] of Object.entries(elt.attributes))
|
|
772
779
|
xw.writeAttribute(key, val);
|
|
773
780
|
if (elt.text !== null)
|
|
774
781
|
xw.text(elt.text);
|
|
775
|
-
this.#addChild(xw, elt.children());
|
|
782
|
+
this.#addChild(xw, elt.children(), elt, debug);
|
|
776
783
|
}
|
|
777
|
-
xw.endElement();
|
|
784
|
+
if (!debug || Object.isSealed(cur)) xw.endElement();
|
|
778
785
|
};
|
|
779
786
|
|
|
780
787
|
/**
|
|
@@ -782,7 +789,7 @@ class Twig {
|
|
|
782
789
|
* @param {?boolean|string|external:XMLWriter} par - `true` or intention character or an already created XMLWriter
|
|
783
790
|
* @returns {external:XMLWriter}
|
|
784
791
|
*/
|
|
785
|
-
writer = function (par) {
|
|
792
|
+
writer = function (par, debug) {
|
|
786
793
|
const XMLWriter = require('xml-writer');
|
|
787
794
|
let xw = par instanceof XMLWriter ? par : new XMLWriter(par);
|
|
788
795
|
|
|
@@ -791,8 +798,8 @@ class Twig {
|
|
|
791
798
|
xw.writeAttribute(key, val);
|
|
792
799
|
if (this.#text !== null)
|
|
793
800
|
xw.text(this.#text);
|
|
794
|
-
this.#addChild(xw, this.#children);
|
|
795
|
-
xw.endElement();
|
|
801
|
+
this.#addChild(xw, this.#children, this, debug);
|
|
802
|
+
if (!debug || Object.isSealed(this)) xw.endElement();
|
|
796
803
|
return xw;
|
|
797
804
|
};
|
|
798
805
|
|