xml-toolkit 1.0.40 → 1.0.41

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/index.js +17 -19
  2. package/lib/XMLPrinter.js +92 -27
  3. package/package.json +1 -1
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
 
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,57 @@ const XMLPrinter = class {
178
198
 
179
199
  this.openElement (node.name)
180
200
 
201
+ if (this.stack.length === 1) {
202
+
203
+ const {namespacesMap} = node; if (namespacesMap != null) {
204
+
205
+ let min = Infinity; for (const name of namespacesMap.keys ()) {
206
+
207
+ const {length} = name
208
+
209
+ if (min > length) min = length
210
+
211
+ }
212
+
213
+ min += 2
214
+
215
+ const prefixes = new Set ()
216
+
217
+ const checkName = name => {
218
+
219
+ if (name.length < min) return
220
+
221
+ const pos = name.indexOf (':'); if (pos === -1) return
222
+
223
+ const prefix = name.substring (0, pos); if (!namespacesMap.has (prefix)) return
224
+
225
+ prefixes.add (prefix)
226
+
227
+ }
228
+
229
+ const checkNames = n => {
230
+
231
+ checkName (n.name)
232
+
233
+ for (const [name, value] of n.attributes.entries ()) {
234
+ checkName (name)
235
+ checkName (value)
236
+ }
237
+
238
+ for (const child of n.children) if (child.type === SAXEvent.TYPES.END_ELEMENT) checkNames (child); else checkName (child.src)
239
+
240
+ }
241
+
242
+ checkNames (node)
243
+
244
+ for (const prefix of prefixes.values ())
245
+
246
+ this.writeAttribute ('xmlns:' + prefix, namespacesMap.get (prefix))
247
+
248
+ }
249
+
250
+ }
251
+
181
252
  for (const [name, value] of node.attributes.entries ()) this.writeAttribute (name, value)
182
253
 
183
254
  for (const child of node.children) this.writeNode (child)
@@ -188,23 +259,17 @@ const XMLPrinter = class {
188
259
 
189
260
  }
190
261
 
191
- writeXMLDecl (encoding, standalone) {
262
+ writeXMLDecl ({encoding, standalone} = {}) {
192
263
 
193
264
  if (this.stack.length !== 0) throw Error ('The document is already started')
194
265
 
195
- this.append ('<?xml version="1.0"')
266
+ this.text += '<?xml version="1.0"'
196
267
 
197
- if (encoding != null) this.append (` encoding="${encoding}"`)
268
+ if (encoding != null) this.text += ` encoding="${encoding}"`
198
269
 
199
- if (standalone != null) {
200
-
201
- if (standalone !== 'yes' && standalone !== 'no') throw Error ('Invalid `standalone`: ' + standalone)
202
-
203
- this.append (` standalone="${standalone}"`)
204
-
205
- }
270
+ if (standalone != null) this.text += ` standalone="${standalone ? 'yes' : 'no'}"`
206
271
 
207
- this.append ('?>')
272
+ this.text += '?>'
208
273
 
209
274
  return this
210
275
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.0.40",
3
+ "version": "1.0.41",
4
4
  "description": "XML parser, marshaller, SOAP adapter",
5
5
  "main": "index.js",
6
6
  "files": [