xml-toolkit 1.0.40 → 1.0.42

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/index.js CHANGED
@@ -1,22 +1,20 @@
1
- for (const name of [
2
-
3
- 'XMLLexer',
4
- 'XMLReader',
5
- 'SAXEvent',
6
- 'AttributesMap',
7
- 'MoxyLikeJsonEncoder',
8
- 'XMLNode',
9
- 'XMLSchemata',
10
- 'SOAP11',
11
- 'SOAP12',
12
- 'EntityResolver',
13
- 'XMLIterator',
14
- 'XMLParser',
15
- 'SOAPFault',
16
- 'SOAPEncoding',
17
- 'XMLPrinter',
18
-
19
- ]) module.exports [name] = require ('./lib/' + name)
1
+ module.exports = {
2
+ AttributesMap: require ('./lib/AttributesMap'),
3
+ EntityResolver: require ('./lib/EntityResolver'),
4
+ MoxyLikeJsonEncoder: require ('./lib/MoxyLikeJsonEncoder'),
5
+ SOAP11: require ('./lib/SOAP11'),
6
+ SOAP12: require ('./lib/SOAP12'),
7
+ SOAPEncoding: require ('./lib/SOAPEncoding'),
8
+ SOAPFault: require ('./lib/SOAPFault'),
9
+ SAXEvent: require ('./lib/SAXEvent'),
10
+ XMLIterator: require ('./lib/XMLIterator'),
11
+ XMLNode: require ('./lib/XMLNode'),
12
+ XMLLexer: require ('./lib/XMLLexer'),
13
+ XMLParser: require ('./lib/XMLParser'),
14
+ XMLPrinter: require ('./lib/XMLPrinter'),
15
+ XMLReader: require ('./lib/XMLReader'),
16
+ XMLSchemata: require ('./lib/XMLSchemata'),
17
+ }
20
18
 
21
19
  module.exports.SOAP = v => {switch (String (v)) {
22
20
 
@@ -24,6 +24,8 @@ const NamespacesMap = class extends Map {
24
24
 
25
25
  super (parent.namespacesMap)
26
26
 
27
+ this.delete ('')
28
+
27
29
  }
28
30
 
29
31
  }
package/lib/XMLPrinter.js CHANGED
@@ -77,6 +77,8 @@ const XMLPrinter = class {
77
77
 
78
78
  }
79
79
 
80
+ this.decl = o.decl
81
+
80
82
  this.reset ()
81
83
 
82
84
  }
@@ -87,31 +89,36 @@ const XMLPrinter = class {
87
89
  this.stack = []
88
90
  this.isOpening = false
89
91
 
92
+ const {decl} = this; if (decl) this.writeXMLDecl (decl)
93
+
94
+ return this
95
+
90
96
  }
91
97
 
92
98
  newLine (delta = 0) {
93
99
 
94
- return this.EOL + this.space.repeat (this.level + this.stack.length + delta)
100
+ this.text += `${this.EOL}${this.space.repeat (this.level + this.stack.length + delta)}`
95
101
 
96
102
  }
97
103
 
98
- append (s) {
104
+ get lastCharCode () {
99
105
 
100
- this.text += s
106
+ const {text} = this
101
107
 
102
- this.lastCharCode = s.charCodeAt (s.length - 1)
108
+ return text.charCodeAt (text.length - 1)
103
109
 
104
110
  }
105
111
 
106
112
  openElement (name) {
107
113
 
108
114
  if (this.stack.length !== 0) {
109
- if (this.attrSpace && this.lastCharCode === CH_QQ) this.append (this.newLine (-1))
110
- if (this.isOpening) this.append (`>`)
111
- if (this.space) this.append (this.newLine ())
115
+ if (this.attrSpace && this.lastCharCode === CH_QQ) this.newLine (-1)
116
+ if (this.isOpening) this.text += `>`
112
117
  }
113
118
 
114
- this.append (`<${name}`)
119
+ if (this.space && this.text) this.newLine ()
120
+
121
+ this.text += `<${name}`
115
122
  this.stack.push (name)
116
123
 
117
124
  this.isOpening = true
@@ -128,7 +135,20 @@ const XMLPrinter = class {
128
135
 
129
136
  if (typeof value !== 'string') throw Error ('The attribute value must be a string, not ' + (typeof value))
130
137
 
131
- this.append (`${this.attrSpace ? `${this.newLine (-1)}${this.attrSpace}` : ' '}${name}="${this.ESC_ATTR.escape (value)}"`)
138
+ if (this.attrSpace) {
139
+
140
+ this.newLine (-1)
141
+
142
+ this.text += this.attrSpace
143
+
144
+ }
145
+ else {
146
+
147
+ this.text += ' '
148
+
149
+ }
150
+
151
+ this.text += `${name}="${this.ESC_ATTR.escape (value)}"`
132
152
 
133
153
  return this
134
154
 
@@ -143,12 +163,12 @@ const XMLPrinter = class {
143
163
  if (value.length === 0) return this
144
164
 
145
165
  if (this.isOpening) {
146
- if (this.attrSpace && this.lastCharCode === CH_QQ) this.append (this.newLine (-1))
147
- this.append (`>`)
166
+ if (this.attrSpace && this.lastCharCode === CH_QQ) this.newLine (-1)
167
+ this.text += `>`
148
168
  this.isOpening = false
149
169
  }
150
170
 
151
- this.append (this.ESC_BODY.escape (value))
171
+ this.text += this.ESC_BODY.escape (value)
152
172
 
153
173
  return this
154
174
 
@@ -159,13 +179,13 @@ const XMLPrinter = class {
159
179
  const name = this.stack.pop ()
160
180
 
161
181
  if (this.isOpening) {
162
- if (this.attrSpace && this.lastCharCode === CH_QQ) this.append (this.newLine ())
163
- this.append (` />`)
182
+ if (this.attrSpace && this.lastCharCode === CH_QQ) this.newLine ()
183
+ this.text += ` />`
164
184
  this.isOpening = false
165
185
  }
166
186
  else {
167
- if (this.space && this.lastCharCode === CH_GT) this.append (this.newLine ())
168
- this.append (`</${name}>`)
187
+ if (this.space && this.lastCharCode === CH_GT) this.newLine ()
188
+ this.text += `</${name}>`
169
189
  }
170
190
 
171
191
  return this
@@ -178,6 +198,59 @@ const XMLPrinter = class {
178
198
 
179
199
  this.openElement (node.name)
180
200
 
201
+ const {namespacesMap} = node; if (namespacesMap != null && namespacesMap.has ('')) this.writeAttribute ('xmlns', namespacesMap.get (''))
202
+
203
+ if (this.stack.length === 1) {
204
+
205
+ if (namespacesMap != null) {
206
+
207
+ let min = Infinity; for (const name of namespacesMap.keys ()) {
208
+
209
+ const {length} = name
210
+
211
+ if (min > length) min = length
212
+
213
+ }
214
+
215
+ min += 2
216
+
217
+ const prefixes = new Set ()
218
+
219
+ const checkName = name => {
220
+
221
+ if (name.length < min) return
222
+
223
+ const pos = name.indexOf (':'); if (pos === -1) return
224
+
225
+ const prefix = name.substring (0, pos); if (!namespacesMap.has (prefix)) return
226
+
227
+ prefixes.add (prefix)
228
+
229
+ }
230
+
231
+ const checkNames = n => {
232
+
233
+ checkName (n.name)
234
+
235
+ for (const [name, value] of n.attributes.entries ()) {
236
+ checkName (name)
237
+ checkName (value)
238
+ }
239
+
240
+ for (const child of n.children) if (child.type === SAXEvent.TYPES.END_ELEMENT) checkNames (child); else checkName (child.src)
241
+
242
+ }
243
+
244
+ checkNames (node)
245
+
246
+ for (const prefix of prefixes.values ())
247
+
248
+ this.writeAttribute ('xmlns:' + prefix, namespacesMap.get (prefix))
249
+
250
+ }
251
+
252
+ }
253
+
181
254
  for (const [name, value] of node.attributes.entries ()) this.writeAttribute (name, value)
182
255
 
183
256
  for (const child of node.children) this.writeNode (child)
@@ -188,23 +261,17 @@ const XMLPrinter = class {
188
261
 
189
262
  }
190
263
 
191
- writeXMLDecl (encoding, standalone) {
264
+ writeXMLDecl ({encoding, standalone} = {}) {
192
265
 
193
266
  if (this.stack.length !== 0) throw Error ('The document is already started')
194
267
 
195
- this.append ('<?xml version="1.0"')
268
+ this.text += '<?xml version="1.0"'
196
269
 
197
- if (encoding != null) this.append (` encoding="${encoding}"`)
270
+ if (encoding != null) this.text += ` encoding="${encoding}"`
198
271
 
199
- if (standalone != null) {
200
-
201
- if (standalone !== 'yes' && standalone !== 'no') throw Error ('Invalid `standalone`: ' + standalone)
202
-
203
- this.append (` standalone="${standalone}"`)
204
-
205
- }
272
+ if (standalone != null) this.text += ` standalone="${standalone ? 'yes' : 'no'}"`
206
273
 
207
- this.append ('?>')
274
+ this.text += '?>'
208
275
 
209
276
  return this
210
277
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.0.40",
3
+ "version": "1.0.42",
4
4
  "description": "XML parser, marshaller, SOAP adapter",
5
5
  "main": "index.js",
6
6
  "files": [