xml-toolkit 1.1.3 → 1.1.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.
@@ -1,18 +1,19 @@
1
1
  const XMLNode = require ('./XMLNode.js')
2
2
  const SAXEvent = require ('./SAXEvent.js')
3
3
  const EntityResolver = require ('./EntityResolver.js')
4
-
5
- const CH_LF = '\n'.charCodeAt (0)
4
+ const XMLPosition = require ('./XMLPosition.js')
6
5
 
7
6
  const XMLIterator = class {
8
7
 
8
+ #position = new XMLPosition ()
9
+
9
10
  constructor (src, options = {}) {
10
11
 
11
- this.src = src
12
- this.options = options
13
- this.pos = 0
14
- this.selfEnclosed = null
15
- this.entityResolver = options.entityResolver ?? new EntityResolver ()
12
+ this.src = src
13
+ this.options = options
14
+ this.absolutePosition = 0
15
+ this.selfEnclosed = null
16
+ this.entityResolver = options.entityResolver ?? new EntityResolver ()
16
17
 
17
18
  }
18
19
 
@@ -34,21 +35,7 @@ const XMLIterator = class {
34
35
 
35
36
  get linePos () {
36
37
 
37
- const {pos} = this
38
-
39
- let l = 1, c = -1
40
-
41
- for (let i = pos; i >= 0; i --) {
42
-
43
- if (this.src.charCodeAt (i) !== CH_LF) continue
44
-
45
- l ++; if (c !== -1) continue
46
-
47
- c = pos - i
48
-
49
- }
50
-
51
- return [l, c]
38
+ return this.#position.toJSON ()
52
39
 
53
40
  }
54
41
 
@@ -56,18 +43,24 @@ const XMLIterator = class {
56
43
 
57
44
  if (this.selfEnclosed !== null) return this.autoClose ()
58
45
 
59
- const {src, pos, entityResolver} = this
46
+ const {src, absolutePosition, entityResolver} = this
60
47
 
61
- if (src.length - pos < 1) return {done: true}
48
+ if (src.length - absolutePosition < 1) return {done: true}
62
49
 
63
- const value = new XMLNode (src.slice (pos), entityResolver)
50
+ const value = new XMLNode (src.slice (absolutePosition), entityResolver)
64
51
 
65
52
  value.trim ()
66
-
67
- const {length} = value.src; if (length === 0) return {done: true}
68
53
 
69
- this.pos += length
54
+ {
55
+
56
+ const {src} = value, {length} = src; if (length === 0) return {done: true}
70
57
 
58
+ this.absolutePosition += length
59
+
60
+ this.#position.scan (src)
61
+
62
+ }
63
+
71
64
  if (value.isStartElement && value.isSelfEnclosed) this.selfEnclosed = value
72
65
 
73
66
  return {value}
package/lib/XMLParser.js CHANGED
@@ -93,7 +93,7 @@ const XMLParser = class {
93
93
 
94
94
  case SAXEvent.TYPES.END_ELEMENT:
95
95
 
96
- if (this.element === null) throw new Error (`Unbalanced end element tag "${node.src}" occured at position ${nodes.pos}`)
96
+ if (this.element === null) throw new Error (`Unbalanced end element tag "${node.src}" occured at position ${nodes.absolutePosition}`)
97
97
  node ['_ns_map'] = this.element ['_ns_map']
98
98
  this.element.type = type
99
99
  this.element = this.element.parent
@@ -0,0 +1,37 @@
1
+ const CH_LF = '\n'.charCodeAt (0)
2
+
3
+ class XMLPosition {
4
+
5
+ #line = 0
6
+ #localPosition = 0
7
+ #lastLine = 0
8
+ #lastLocalPosition = 0
9
+
10
+ scan (chunk) {
11
+
12
+ this.#lastLine = this.#line
13
+ this.#lastLocalPosition = this.#localPosition
14
+
15
+ for (let i = 0; i < chunk.length; i ++) switch (chunk.charCodeAt (i)) {
16
+
17
+ case CH_LF:
18
+ this.#line ++
19
+ this.#localPosition = 0
20
+ break
21
+
22
+ default:
23
+ this.#localPosition ++
24
+
25
+ }
26
+
27
+ }
28
+
29
+ toJSON () {
30
+
31
+ return [this.#lastLine + 1, this.#lastLocalPosition + 1]
32
+
33
+ }
34
+
35
+ }
36
+
37
+ module.exports = XMLPosition
package/lib/XMLReader.js CHANGED
@@ -1,8 +1,9 @@
1
- const assert = require ('assert')
2
- const {Transform} = require ('stream')
3
- const SAXEvent = require ('./SAXEvent.js')
4
- const XMLNode = require ('./XMLNode.js')
5
- const XMLLexer = require ('./XMLLexer.js')
1
+ const assert = require ('assert')
2
+ const {Transform} = require ('stream')
3
+ const SAXEvent = require ('./SAXEvent.js')
4
+ const XMLNode = require ('./XMLNode.js')
5
+ const XMLLexer = require ('./XMLLexer.js')
6
+ const XMLPosition = require ('./XMLPosition.js')
6
7
  const XMLValidatior = require ('./validation/XMLValidatior.js')
7
8
 
8
9
  const OPT_SRC = Symbol ('_src')
@@ -10,10 +11,10 @@ const OPT_SAX = Symbol ('_sax')
10
11
 
11
12
  const NO_CHILDREN = []; NO_CHILDREN.push = () => {}
12
13
 
13
- const CH_LF = '\n'.charCodeAt (0)
14
-
15
14
  const XMLReader = class extends Transform {
16
15
 
16
+ #position = new XMLPosition ()
17
+
17
18
  constructor (options = {}) {
18
19
 
19
20
  options.decodeStrings = false
@@ -70,16 +71,15 @@ const XMLReader = class extends Transform {
70
71
  this.useNamespaces = options.useNamespaces
71
72
  this.filter = filter || null
72
73
  this.map = map || null
73
-
74
- this.lineNo = 1
75
- this.lineStart = 0
74
+
75
+ this.line = 0
76
+ this.localPosition = 0
76
77
 
77
78
  if (this.useEntities) this.entityResolver = new (require ('./EntityResolver.js')) ()
78
79
 
79
80
  this.text = ''
80
81
  this.element = null
81
82
  this.position = 0
82
- this.pos = 0 // TODO: cleanup
83
83
 
84
84
  this [OPT_SRC] = null; this.on ('pipe', src => this [OPT_SRC] = src)
85
85
 
@@ -252,7 +252,7 @@ const XMLReader = class extends Transform {
252
252
 
253
253
  get linePos () {
254
254
 
255
- return [this.lineNo, this.pos - this.lineStart]
255
+ return this.#position.toJSON ()
256
256
 
257
257
  }
258
258
 
@@ -260,17 +260,7 @@ const XMLReader = class extends Transform {
260
260
 
261
261
  const {length} = chunk; if (length !== 0) {
262
262
 
263
- for (let i = 0; i < length; i ++) {
264
-
265
- this.pos ++
266
-
267
- if (chunk.charCodeAt (i) !== CH_LF) continue
268
-
269
- this.lineNo ++
270
-
271
- this.lineStart = this.pos
272
-
273
- }
263
+ this.#position.scan (chunk)
274
264
 
275
265
  let e = new XMLNode (chunk, this.entityResolver), {type} = e, {element} = this
276
266
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "XML parser, marshaller, SOAP adapter",
5
5
  "main": "index.js",
6
6
  "files": [