xml-toolkit 1.0.46 → 1.0.47
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/lib/XMLNode.js +34 -2
- package/lib/XMLParser.js +0 -1
- package/lib/XMLReader.js +28 -6
- package/package.json +1 -1
package/lib/XMLNode.js
CHANGED
|
@@ -9,6 +9,7 @@ const TYPE = Symbol ('_type')
|
|
|
9
9
|
const PARENT = Symbol ('_parent')
|
|
10
10
|
const LEVEL = Symbol ('_level')
|
|
11
11
|
const NS_MAP = '_ns_map'
|
|
12
|
+
const NO_CHILDREN = []
|
|
12
13
|
|
|
13
14
|
const m2o = m => Object.fromEntries (m.entries ())
|
|
14
15
|
|
|
@@ -23,8 +24,39 @@ const XMLNode = class extends SAXEvent {
|
|
|
23
24
|
this [ENTITY_RESOLVER] = entityResolver
|
|
24
25
|
this [PARENT] = null
|
|
25
26
|
this [LEVEL] = 0
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get children () {
|
|
31
|
+
|
|
32
|
+
return this.children = this.isLeaf ? NO_CHILDREN : []
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
set children (value) {
|
|
37
|
+
|
|
38
|
+
Object.defineProperty (this, 'children', {value,
|
|
39
|
+
writable: true,
|
|
40
|
+
configurable: false,
|
|
41
|
+
enumerable: false,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get isLeaf () {
|
|
47
|
+
|
|
48
|
+
switch (this [TYPE]) {
|
|
49
|
+
|
|
50
|
+
case SAXEvent.TYPES.START_ELEMENT:
|
|
51
|
+
return this.isSelfEnclosed
|
|
52
|
+
|
|
53
|
+
case SAXEvent.TYPES.END_ELEMENT:
|
|
54
|
+
return false
|
|
55
|
+
|
|
56
|
+
default:
|
|
57
|
+
return true
|
|
58
|
+
|
|
59
|
+
}
|
|
28
60
|
|
|
29
61
|
}
|
|
30
62
|
|
package/lib/XMLParser.js
CHANGED
package/lib/XMLReader.js
CHANGED
|
@@ -7,6 +7,8 @@ const XMLLexer = require ('./XMLLexer.js')
|
|
|
7
7
|
const OPT_SRC = Symbol ('_src')
|
|
8
8
|
const OPT_SAX = Symbol ('_sax')
|
|
9
9
|
|
|
10
|
+
const NO_CHILDREN = []; NO_CHILDREN.push = () => {}
|
|
11
|
+
|
|
10
12
|
const XMLReader = class extends Transform {
|
|
11
13
|
|
|
12
14
|
constructor (options = {}) {
|
|
@@ -44,10 +46,6 @@ const XMLReader = class extends Transform {
|
|
|
44
46
|
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
let {collect} = options; delete options.collect
|
|
48
|
-
assert (collect == null || typeof collect === 'function', 'options.collect must be a function, not ' + typeof collect)
|
|
49
|
-
if (collect == null && filterElements != null) collect = filterElements
|
|
50
|
-
|
|
51
49
|
const {filter} = options; delete options.filter
|
|
52
50
|
assert (filter == null || typeof filter === 'function', 'options.filter must be a function, not ' + typeof filter)
|
|
53
51
|
|
|
@@ -59,7 +57,6 @@ const XMLReader = class extends Transform {
|
|
|
59
57
|
this.stripSpace = options.stripSpace
|
|
60
58
|
this.useEntities = options.useEntities
|
|
61
59
|
this.useNamespaces = options.useNamespaces
|
|
62
|
-
this.collect = collect || null
|
|
63
60
|
this.filter = filter || null
|
|
64
61
|
this.map = map || null
|
|
65
62
|
|
|
@@ -141,7 +138,32 @@ const XMLReader = class extends Transform {
|
|
|
141
138
|
|
|
142
139
|
if (type !== null) xmlNode.type = type
|
|
143
140
|
|
|
144
|
-
const {filter} = this; if (filter !== null
|
|
141
|
+
const {filter} = this; if (filter !== null) {
|
|
142
|
+
|
|
143
|
+
if (!filter (xmlNode)) return
|
|
144
|
+
|
|
145
|
+
let node = xmlNode; while (true) {
|
|
146
|
+
|
|
147
|
+
node = node.parent; if (!node) break
|
|
148
|
+
|
|
149
|
+
if (node.children === NO_CHILDREN || node.toBePublished) continue
|
|
150
|
+
|
|
151
|
+
const end = new XMLNode ('</' + node.src.slice (1), this.entityResolver)
|
|
152
|
+
|
|
153
|
+
if (filter (end)) {
|
|
154
|
+
|
|
155
|
+
node.toBePublished = true
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
|
|
160
|
+
node.children = NO_CHILDREN
|
|
161
|
+
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
145
167
|
|
|
146
168
|
const {map} = this, value = map === null ? xmlNode : map (xmlNode)
|
|
147
169
|
|