xml-twig 1.3.1 → 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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/test.js +2 -2
  3. package/twig.js +7 -11
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.3.1",
8
+ "version": "1.3.2",
9
9
  "main": "twig.js",
10
10
  "directories": {
11
11
  "doc": "doc"
package/test.js CHANGED
@@ -2,10 +2,10 @@ const fs = require('fs');
2
2
  const twig = require('./twig.js');
3
3
 
4
4
 
5
- //const parser = twig.createParser({ tag: twig.Any, function: anyHandler }, { method: "sax" });
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
- //fs.createReadStream(`${__dirname}/samples/bookstore.xml`).pipe(parser);
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,8 +483,6 @@ function reset() {
483
483
  current = undefined;
484
484
  }
485
485
 
486
- let closeXMLWriterElement = true;
487
-
488
486
  /**
489
487
  * Generic class modeling a XML Node
490
488
  * @class Twig
@@ -766,7 +764,7 @@ class Twig {
766
764
  * @returns {string} The XML-Tree which is currently available in RAM - no valid XML Structure
767
765
  */
768
766
  debug = function () {
769
- return this.root().writer(true, this).output;
767
+ return this.root().writer(true, true).output;
770
768
  };
771
769
 
772
770
  /**
@@ -774,17 +772,16 @@ class Twig {
774
772
  * @param {external:XMLWriter} xw - The writer object
775
773
  * @param {Twig[]} childArray - Array of child elements
776
774
  */
777
- #addChild = function (xw, childArray, startElement) {
775
+ #addChild = function (xw, childArray, cur, debug) {
778
776
  for (let elt of childArray) {
779
777
  xw.startElement(elt.name);
780
778
  for (let [key, val] of Object.entries(elt.attributes))
781
779
  xw.writeAttribute(key, val);
782
780
  if (elt.text !== null)
783
781
  xw.text(elt.text);
784
- this.#addChild(xw, elt.children(), startElement);
785
- if (elt === startElement) closeXMLWriterElement = false;
782
+ this.#addChild(xw, elt.children(), elt, debug);
786
783
  }
787
- if (closeXMLWriterElement) xw.endElement();
784
+ if (!debug || Object.isSealed(cur)) xw.endElement();
788
785
  };
789
786
 
790
787
  /**
@@ -792,18 +789,17 @@ class Twig {
792
789
  * @param {?boolean|string|external:XMLWriter} par - `true` or intention character or an already created XMLWriter
793
790
  * @returns {external:XMLWriter}
794
791
  */
795
- writer = function (par, startElement) {
792
+ writer = function (par, debug) {
796
793
  const XMLWriter = require('xml-writer');
797
794
  let xw = par instanceof XMLWriter ? par : new XMLWriter(par);
798
- closeXMLWriterElement = true
799
795
 
800
796
  xw.startElement(this.#name);
801
797
  for (let [key, val] of Object.entries(this.#attributes))
802
798
  xw.writeAttribute(key, val);
803
799
  if (this.#text !== null)
804
800
  xw.text(this.#text);
805
- this.#addChild(xw, this.#children, startElement);
806
- if (closeXMLWriterElement) xw.endElement();
801
+ this.#addChild(xw, this.#children, this, debug);
802
+ if (!debug || Object.isSealed(this)) xw.endElement();
807
803
  return xw;
808
804
  };
809
805