xml-toolkit 1.0.46 → 1.0.48
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/lib/XMLSchema.js +44 -37
- 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
|
|
package/lib/XMLSchema.js
CHANGED
|
@@ -3,42 +3,6 @@ const TYPES = Symbol ('_types')
|
|
|
3
3
|
const FORM_U = 'unqualified'
|
|
4
4
|
const FORM_Q = 'qualified'
|
|
5
5
|
|
|
6
|
-
const adjustNode = node => {
|
|
7
|
-
|
|
8
|
-
node.children = node.children
|
|
9
|
-
.filter (i => i.localName !== 'annotation')
|
|
10
|
-
.map (node => adjustNode (node))
|
|
11
|
-
|
|
12
|
-
const {attributes, namespacesMap} = node, splitNs = name => {
|
|
13
|
-
|
|
14
|
-
if (!attributes.has (name)) return
|
|
15
|
-
|
|
16
|
-
const value = attributes.get (name), pos = value.indexOf (':')
|
|
17
|
-
|
|
18
|
-
attributes.set (name, [value.substring (pos + 1), namespacesMap.get (value.substring (0, pos))])
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
switch (node.localName) {
|
|
23
|
-
|
|
24
|
-
case 'attribute':
|
|
25
|
-
case 'element':
|
|
26
|
-
case 'group':
|
|
27
|
-
splitNs ('ref')
|
|
28
|
-
splitNs ('type')
|
|
29
|
-
break
|
|
30
|
-
|
|
31
|
-
case 'extension':
|
|
32
|
-
case 'restriction':
|
|
33
|
-
splitNs ('base')
|
|
34
|
-
break
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return node
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
6
|
const XMLSchema = class extends Map {
|
|
43
7
|
|
|
44
8
|
static namespaceURI = 'http://www.w3.org/2001/XMLSchema'
|
|
@@ -56,6 +20,49 @@ const XMLSchema = class extends Map {
|
|
|
56
20
|
this.setSource (node)
|
|
57
21
|
|
|
58
22
|
}
|
|
23
|
+
|
|
24
|
+
adjustNode (node) {
|
|
25
|
+
|
|
26
|
+
node.children = node.children
|
|
27
|
+
.filter (i => i.localName !== 'annotation')
|
|
28
|
+
.map (node => this.adjustNode (node))
|
|
29
|
+
|
|
30
|
+
const {attributes, namespacesMap} = node, splitNs = name => {
|
|
31
|
+
|
|
32
|
+
if (!attributes.has (name)) return
|
|
33
|
+
|
|
34
|
+
const value = attributes.get (name), pos = value.indexOf (':')
|
|
35
|
+
|
|
36
|
+
attributes.set (name,
|
|
37
|
+
|
|
38
|
+
pos === -1 ? [value, this.targetNamespace] :
|
|
39
|
+
|
|
40
|
+
[value.substring (pos + 1), namespacesMap.get (value.substring (0, pos))]
|
|
41
|
+
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
switch (node.localName) {
|
|
47
|
+
|
|
48
|
+
case 'attribute':
|
|
49
|
+
case 'attributeGroup':
|
|
50
|
+
case 'element':
|
|
51
|
+
case 'group':
|
|
52
|
+
splitNs ('ref')
|
|
53
|
+
splitNs ('type')
|
|
54
|
+
break
|
|
55
|
+
|
|
56
|
+
case 'extension':
|
|
57
|
+
case 'restriction':
|
|
58
|
+
splitNs ('base')
|
|
59
|
+
break
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return node
|
|
64
|
+
|
|
65
|
+
}
|
|
59
66
|
|
|
60
67
|
getType (localName) {
|
|
61
68
|
|
|
@@ -65,7 +72,7 @@ const XMLSchema = class extends Map {
|
|
|
65
72
|
|
|
66
73
|
setSource (node) {
|
|
67
74
|
|
|
68
|
-
node = adjustNode (node).detach ()
|
|
75
|
+
node = this.adjustNode (node).detach ()
|
|
69
76
|
|
|
70
77
|
const {attributes, children} = node
|
|
71
78
|
|