@xyd-js/openapi-sampler 0.1.0-build.138
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/CHANGELOG.md +848 -0
- package/LICENSE +21 -0
- package/dist/index.js +3422 -0
- package/dist/index.js.map +1 -0
- package/package.json +21 -0
- package/rollup.config.js +20 -0
- package/src/index.d.ts +46 -0
- package/src/index.js +4 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3422 @@
|
|
|
1
|
+
function pad(number) {
|
|
2
|
+
if (number < 10) {
|
|
3
|
+
return '0' + number;
|
|
4
|
+
}
|
|
5
|
+
return number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function toRFCDateTime(date, omitTime, omitDate, milliseconds) {
|
|
9
|
+
var res = omitDate ? '' : (date.getUTCFullYear() +
|
|
10
|
+
'-' + pad(date.getUTCMonth() + 1) +
|
|
11
|
+
'-' + pad(date.getUTCDate()));
|
|
12
|
+
if (!omitTime) {
|
|
13
|
+
res += 'T' + pad(date.getUTCHours()) +
|
|
14
|
+
':' + pad(date.getUTCMinutes()) +
|
|
15
|
+
':' + pad(date.getUTCSeconds()) +
|
|
16
|
+
('') +
|
|
17
|
+
'Z';
|
|
18
|
+
}
|
|
19
|
+
return res;
|
|
20
|
+
}
|
|
21
|
+
function ensureMinLength(sample, min) {
|
|
22
|
+
if (min > sample.length) {
|
|
23
|
+
return sample.repeat(Math.trunc(min / sample.length) + 1).substring(0, min);
|
|
24
|
+
}
|
|
25
|
+
return sample;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function mergeDeep(...objects) {
|
|
29
|
+
const isObject = obj => obj && typeof obj === 'object';
|
|
30
|
+
|
|
31
|
+
return objects.reduce((prev, obj) => {
|
|
32
|
+
Object.keys(obj || {}).forEach(key => {
|
|
33
|
+
const pVal = prev[key];
|
|
34
|
+
const oVal = obj[key];
|
|
35
|
+
|
|
36
|
+
if (isObject(pVal) && isObject(oVal)) {
|
|
37
|
+
prev[key] = mergeDeep(pVal, oVal);
|
|
38
|
+
} else {
|
|
39
|
+
prev[key] = oVal;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return prev;
|
|
44
|
+
}, Array.isArray(objects[objects.length - 1]) ? [] : {});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// deterministic UUID sampler
|
|
48
|
+
|
|
49
|
+
function uuid(str) {
|
|
50
|
+
var hash = hashCode(str);
|
|
51
|
+
var random = jsf32(hash, hash, hash, hash);
|
|
52
|
+
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
53
|
+
var r = (random() * 16) % 16 | 0;
|
|
54
|
+
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
55
|
+
});
|
|
56
|
+
return uuid;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getResultForCircular(type) {
|
|
60
|
+
return {
|
|
61
|
+
value: type === 'object' ?
|
|
62
|
+
{}
|
|
63
|
+
: type === 'array' ? [] : undefined
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function popSchemaStack(seenSchemasStack, context) {
|
|
68
|
+
if (context) seenSchemasStack.pop();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getXMLAttributes(schema) {
|
|
72
|
+
return {
|
|
73
|
+
name: schema?.xml?.name || '',
|
|
74
|
+
prefix: schema?.xml?.prefix || '',
|
|
75
|
+
namespace: schema?.xml?.namespace || null,
|
|
76
|
+
attribute: schema?.xml?.attribute ?? false,
|
|
77
|
+
wrapped: schema?.xml?.wrapped ?? false,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function applyXMLAttributes(result, schema = {}, context = {}) {
|
|
82
|
+
const { value: oldValue } = result;
|
|
83
|
+
const { propertyName: oldPropertyName } = context;
|
|
84
|
+
const { name, prefix, namespace, attribute, wrapped } =
|
|
85
|
+
getXMLAttributes(schema);
|
|
86
|
+
let propertyName = name || oldPropertyName ? `${prefix ? prefix + ':' : ''}${name || oldPropertyName}` : null;
|
|
87
|
+
|
|
88
|
+
let value = typeof oldValue === 'object'
|
|
89
|
+
? Array.isArray(oldValue)
|
|
90
|
+
? [...oldValue]
|
|
91
|
+
: { ...oldValue }
|
|
92
|
+
: oldValue;
|
|
93
|
+
|
|
94
|
+
if (attribute && propertyName) {
|
|
95
|
+
propertyName = `$${propertyName}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (namespace) {
|
|
99
|
+
if (typeof value === 'object') {
|
|
100
|
+
value[`$xmlns${prefix ? ':' + prefix : ''}`] = namespace;
|
|
101
|
+
} else {
|
|
102
|
+
value = { [`$xmlns${prefix ? ':' + prefix : ''}`]: namespace, ['#text']: value };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (schema.type === 'array') {
|
|
107
|
+
if (wrapped && Array.isArray(value)) {
|
|
108
|
+
value = { [propertyName]: [...value] };
|
|
109
|
+
}
|
|
110
|
+
if (!wrapped) {
|
|
111
|
+
propertyName = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (schema.example !== undefined && !wrapped) {
|
|
115
|
+
propertyName = schema.items?.xml?.name || propertyName;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (schema.oneOf || schema.anyOf || schema.allOf || schema.$ref) {
|
|
119
|
+
propertyName = null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
propertyName,
|
|
124
|
+
value,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function hashCode(str) {
|
|
129
|
+
var hash = 0;
|
|
130
|
+
if (str.length == 0) return hash;
|
|
131
|
+
for (var i = 0; i < str.length; i++) {
|
|
132
|
+
var char = str.charCodeAt(i);
|
|
133
|
+
hash = ((hash << 5) - hash) + char;
|
|
134
|
+
hash = hash & hash;
|
|
135
|
+
}
|
|
136
|
+
return hash;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function jsf32(a, b, c, d) {
|
|
140
|
+
return function () {
|
|
141
|
+
a |= 0; b |= 0; c |= 0; d |= 0;
|
|
142
|
+
var t = a - (b << 27 | b >>> 5) | 0;
|
|
143
|
+
a = b ^ (c << 17 | c >>> 15);
|
|
144
|
+
b = c + d | 0;
|
|
145
|
+
c = d + t | 0;
|
|
146
|
+
d = a + t | 0;
|
|
147
|
+
return (d >>> 0) / 4294967296;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function allOfSample(into, children, options, spec, context) {
|
|
152
|
+
let res = traverse(into, options, spec);
|
|
153
|
+
const subSamples = [];
|
|
154
|
+
|
|
155
|
+
for (let subSchema of children) {
|
|
156
|
+
const { type, readOnly, writeOnly, value } = traverse({ type: res.type, ...subSchema }, options, spec, context);
|
|
157
|
+
if (res.type && type && type !== res.type) {
|
|
158
|
+
console.warn('allOf: schemas with different types can\'t be merged');
|
|
159
|
+
res.type = type;
|
|
160
|
+
}
|
|
161
|
+
res.type = res.type || type;
|
|
162
|
+
res.readOnly = res.readOnly || readOnly;
|
|
163
|
+
res.writeOnly = res.writeOnly || writeOnly;
|
|
164
|
+
if (value != null) subSamples.push(value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (res.type === 'object') {
|
|
168
|
+
res.value = mergeDeep(res.value || {}, ...subSamples.filter(sample => typeof sample === 'object'));
|
|
169
|
+
return res;
|
|
170
|
+
} else {
|
|
171
|
+
if (res.type === 'array') {
|
|
172
|
+
// TODO: implement arrays
|
|
173
|
+
if (!options.quiet) console.warn('OpenAPI Sampler: found allOf with "array" type. Result may be incorrect');
|
|
174
|
+
}
|
|
175
|
+
const lastSample = subSamples[subSamples.length - 1];
|
|
176
|
+
res.value = lastSample != null ? lastSample : res.value;
|
|
177
|
+
return res;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const schemaKeywordTypes = {
|
|
182
|
+
multipleOf: 'number',
|
|
183
|
+
maximum: 'number',
|
|
184
|
+
exclusiveMaximum: 'number',
|
|
185
|
+
minimum: 'number',
|
|
186
|
+
exclusiveMinimum: 'number',
|
|
187
|
+
|
|
188
|
+
maxLength: 'string',
|
|
189
|
+
minLength: 'string',
|
|
190
|
+
pattern: 'string',
|
|
191
|
+
|
|
192
|
+
items: 'array',
|
|
193
|
+
maxItems: 'array',
|
|
194
|
+
minItems: 'array',
|
|
195
|
+
uniqueItems: 'array',
|
|
196
|
+
additionalItems: 'array',
|
|
197
|
+
|
|
198
|
+
maxProperties: 'object',
|
|
199
|
+
minProperties: 'object',
|
|
200
|
+
required: 'object',
|
|
201
|
+
additionalProperties: 'object',
|
|
202
|
+
properties: 'object',
|
|
203
|
+
patternProperties: 'object',
|
|
204
|
+
dependencies: 'object'
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
function inferType(schema) {
|
|
208
|
+
if (schema.type !== undefined) {
|
|
209
|
+
return Array.isArray(schema.type) ? schema.type.length === 0 ? null : schema.type[0] : schema.type;
|
|
210
|
+
}
|
|
211
|
+
const keywords = Object.keys(schemaKeywordTypes);
|
|
212
|
+
for (var i = 0; i < keywords.length; i++) {
|
|
213
|
+
let keyword = keywords[i];
|
|
214
|
+
let type = schemaKeywordTypes[keyword];
|
|
215
|
+
if (schema[keyword] !== undefined) {
|
|
216
|
+
return type;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function getDefaultExportFromCjs (x) {
|
|
224
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
228
|
+
var toString = Object.prototype.toString;
|
|
229
|
+
|
|
230
|
+
var foreach = function forEach (obj, fn, ctx) {
|
|
231
|
+
if (toString.call(fn) !== '[object Function]') {
|
|
232
|
+
throw new TypeError('iterator must be a function');
|
|
233
|
+
}
|
|
234
|
+
var l = obj.length;
|
|
235
|
+
if (l === +l) {
|
|
236
|
+
for (var i = 0; i < l; i++) {
|
|
237
|
+
fn.call(ctx, obj[i], i, obj);
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
for (var k in obj) {
|
|
241
|
+
if (hasOwn.call(obj, k)) {
|
|
242
|
+
fn.call(ctx, obj[k], k, obj);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
var each = foreach;
|
|
249
|
+
var jsonPointer = api;
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Convenience wrapper around the api.
|
|
254
|
+
* Calls `.get` when called with an `object` and a `pointer`.
|
|
255
|
+
* Calls `.set` when also called with `value`.
|
|
256
|
+
* If only supplied `object`, returns a partially applied function, mapped to the object.
|
|
257
|
+
*
|
|
258
|
+
* @param {Object} obj
|
|
259
|
+
* @param {String|Array} pointer
|
|
260
|
+
* @param value
|
|
261
|
+
* @returns {*}
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
function api (obj, pointer, value) {
|
|
265
|
+
// .set()
|
|
266
|
+
if (arguments.length === 3) {
|
|
267
|
+
return api.set(obj, pointer, value);
|
|
268
|
+
}
|
|
269
|
+
// .get()
|
|
270
|
+
if (arguments.length === 2) {
|
|
271
|
+
return api.get(obj, pointer);
|
|
272
|
+
}
|
|
273
|
+
// Return a partially applied function on `obj`.
|
|
274
|
+
var wrapped = api.bind(api, obj);
|
|
275
|
+
|
|
276
|
+
// Support for oo style
|
|
277
|
+
for (var name in api) {
|
|
278
|
+
if (api.hasOwnProperty(name)) {
|
|
279
|
+
wrapped[name] = api[name].bind(wrapped, obj);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return wrapped;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Lookup a json pointer in an object
|
|
288
|
+
*
|
|
289
|
+
* @param {Object} obj
|
|
290
|
+
* @param {String|Array} pointer
|
|
291
|
+
* @returns {*}
|
|
292
|
+
*/
|
|
293
|
+
api.get = function get (obj, pointer) {
|
|
294
|
+
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
|
|
295
|
+
|
|
296
|
+
for (var i = 0; i < refTokens.length; ++i) {
|
|
297
|
+
var tok = refTokens[i];
|
|
298
|
+
if (!(typeof obj == 'object' && tok in obj)) {
|
|
299
|
+
throw new Error('Invalid reference token: ' + tok);
|
|
300
|
+
}
|
|
301
|
+
obj = obj[tok];
|
|
302
|
+
}
|
|
303
|
+
return obj;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Sets a value on an object
|
|
308
|
+
*
|
|
309
|
+
* @param {Object} obj
|
|
310
|
+
* @param {String|Array} pointer
|
|
311
|
+
* @param value
|
|
312
|
+
*/
|
|
313
|
+
api.set = function set (obj, pointer, value) {
|
|
314
|
+
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer),
|
|
315
|
+
nextTok = refTokens[0];
|
|
316
|
+
|
|
317
|
+
if (refTokens.length === 0) {
|
|
318
|
+
throw Error('Can not set the root object');
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
for (var i = 0; i < refTokens.length - 1; ++i) {
|
|
322
|
+
var tok = refTokens[i];
|
|
323
|
+
if (typeof tok !== 'string' && typeof tok !== 'number') {
|
|
324
|
+
tok = String(tok);
|
|
325
|
+
}
|
|
326
|
+
if (tok === "__proto__" || tok === "constructor" || tok === "prototype") {
|
|
327
|
+
continue
|
|
328
|
+
}
|
|
329
|
+
if (tok === '-' && Array.isArray(obj)) {
|
|
330
|
+
tok = obj.length;
|
|
331
|
+
}
|
|
332
|
+
nextTok = refTokens[i + 1];
|
|
333
|
+
|
|
334
|
+
if (!(tok in obj)) {
|
|
335
|
+
if (nextTok.match(/^(\d+|-)$/)) {
|
|
336
|
+
obj[tok] = [];
|
|
337
|
+
} else {
|
|
338
|
+
obj[tok] = {};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
obj = obj[tok];
|
|
342
|
+
}
|
|
343
|
+
if (nextTok === '-' && Array.isArray(obj)) {
|
|
344
|
+
nextTok = obj.length;
|
|
345
|
+
}
|
|
346
|
+
obj[nextTok] = value;
|
|
347
|
+
return this;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Removes an attribute
|
|
352
|
+
*
|
|
353
|
+
* @param {Object} obj
|
|
354
|
+
* @param {String|Array} pointer
|
|
355
|
+
*/
|
|
356
|
+
api.remove = function (obj, pointer) {
|
|
357
|
+
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
|
|
358
|
+
var finalToken = refTokens[refTokens.length -1];
|
|
359
|
+
if (finalToken === undefined) {
|
|
360
|
+
throw new Error('Invalid JSON pointer for remove: "' + pointer + '"');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
var parent = api.get(obj, refTokens.slice(0, -1));
|
|
364
|
+
if (Array.isArray(parent)) {
|
|
365
|
+
var index = +finalToken;
|
|
366
|
+
if (finalToken === '' && isNaN(index)) {
|
|
367
|
+
throw new Error('Invalid array index: "' + finalToken + '"');
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
Array.prototype.splice.call(parent, index, 1);
|
|
371
|
+
} else {
|
|
372
|
+
delete parent[finalToken];
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Returns a (pointer -> value) dictionary for an object
|
|
378
|
+
*
|
|
379
|
+
* @param obj
|
|
380
|
+
* @param {function} descend
|
|
381
|
+
* @returns {}
|
|
382
|
+
*/
|
|
383
|
+
api.dict = function dict (obj, descend) {
|
|
384
|
+
var results = {};
|
|
385
|
+
api.walk(obj, function (value, pointer) {
|
|
386
|
+
results[pointer] = value;
|
|
387
|
+
}, descend);
|
|
388
|
+
return results;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Iterates over an object
|
|
393
|
+
* Iterator: function (value, pointer) {}
|
|
394
|
+
*
|
|
395
|
+
* @param obj
|
|
396
|
+
* @param {function} iterator
|
|
397
|
+
* @param {function} descend
|
|
398
|
+
*/
|
|
399
|
+
api.walk = function walk (obj, iterator, descend) {
|
|
400
|
+
var refTokens = [];
|
|
401
|
+
|
|
402
|
+
descend = descend || function (value) {
|
|
403
|
+
var type = Object.prototype.toString.call(value);
|
|
404
|
+
return type === '[object Object]' || type === '[object Array]';
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
(function next (cur) {
|
|
408
|
+
each(cur, function (value, key) {
|
|
409
|
+
refTokens.push(String(key));
|
|
410
|
+
if (descend(value)) {
|
|
411
|
+
next(value);
|
|
412
|
+
} else {
|
|
413
|
+
iterator(value, api.compile(refTokens));
|
|
414
|
+
}
|
|
415
|
+
refTokens.pop();
|
|
416
|
+
});
|
|
417
|
+
}(obj));
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Tests if an object has a value for a json pointer
|
|
422
|
+
*
|
|
423
|
+
* @param obj
|
|
424
|
+
* @param pointer
|
|
425
|
+
* @returns {boolean}
|
|
426
|
+
*/
|
|
427
|
+
api.has = function has (obj, pointer) {
|
|
428
|
+
try {
|
|
429
|
+
api.get(obj, pointer);
|
|
430
|
+
} catch (e) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Escapes a reference token
|
|
438
|
+
*
|
|
439
|
+
* @param str
|
|
440
|
+
* @returns {string}
|
|
441
|
+
*/
|
|
442
|
+
api.escape = function escape (str) {
|
|
443
|
+
return str.toString().replace(/~/g, '~0').replace(/\//g, '~1');
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Unescapes a reference token
|
|
448
|
+
*
|
|
449
|
+
* @param str
|
|
450
|
+
* @returns {string}
|
|
451
|
+
*/
|
|
452
|
+
api.unescape = function unescape (str) {
|
|
453
|
+
return str.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Converts a json pointer into a array of reference tokens
|
|
458
|
+
*
|
|
459
|
+
* @param pointer
|
|
460
|
+
* @returns {Array}
|
|
461
|
+
*/
|
|
462
|
+
api.parse = function parse (pointer) {
|
|
463
|
+
if (pointer === '') { return []; }
|
|
464
|
+
if (pointer.charAt(0) !== '/') { throw new Error('Invalid JSON pointer: ' + pointer); }
|
|
465
|
+
return pointer.substring(1).split(/\//).map(api.unescape);
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Builds a json pointer from a array of reference tokens
|
|
470
|
+
*
|
|
471
|
+
* @param refTokens
|
|
472
|
+
* @returns {string}
|
|
473
|
+
*/
|
|
474
|
+
api.compile = function compile (refTokens) {
|
|
475
|
+
if (refTokens.length === 0) { return ''; }
|
|
476
|
+
return '/' + refTokens.map(api.escape).join('/');
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
var JsonPointer = /*@__PURE__*/getDefaultExportFromCjs(jsonPointer);
|
|
480
|
+
|
|
481
|
+
let $refCache = {};
|
|
482
|
+
// for circular JS references we use additional array and not object as we need to compare entire schemas and not strings
|
|
483
|
+
let seenSchemasStack = [];
|
|
484
|
+
|
|
485
|
+
function clearCache() {
|
|
486
|
+
$refCache = {};
|
|
487
|
+
seenSchemasStack = [];
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function inferExample(schema) {
|
|
491
|
+
let example;
|
|
492
|
+
if (schema.const !== undefined) {
|
|
493
|
+
example = schema.const;
|
|
494
|
+
} else if (schema.examples !== undefined && schema.examples.length) {
|
|
495
|
+
example = schema.examples[0];
|
|
496
|
+
} else if (schema.enum !== undefined && schema.enum.length) {
|
|
497
|
+
example = schema.enum[0];
|
|
498
|
+
} else if (schema.default !== undefined) {
|
|
499
|
+
example = schema.default;
|
|
500
|
+
}
|
|
501
|
+
return example;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function tryInferExample(schema) {
|
|
505
|
+
const example = inferExample(schema);
|
|
506
|
+
// case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords
|
|
507
|
+
if (example !== undefined) {
|
|
508
|
+
return {
|
|
509
|
+
value: example,
|
|
510
|
+
readOnly: schema.readOnly,
|
|
511
|
+
writeOnly: schema.writeOnly,
|
|
512
|
+
type: null,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function traverse(schema, options, spec, context) {
|
|
519
|
+
// checking circular JS references by checking context
|
|
520
|
+
// because context is passed only when traversing through nested objects happens
|
|
521
|
+
if (context) {
|
|
522
|
+
if (seenSchemasStack.includes(schema)) return getResultForCircular(inferType(schema));
|
|
523
|
+
seenSchemasStack.push(schema);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
if (context && context.depth > options.maxSampleDepth) {
|
|
528
|
+
popSchemaStack(seenSchemasStack, context);
|
|
529
|
+
return getResultForCircular(inferType(schema));
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (schema.$ref) {
|
|
533
|
+
if (!spec) {
|
|
534
|
+
throw new Error('Your schema contains $ref. You must provide full specification in the third parameter.');
|
|
535
|
+
}
|
|
536
|
+
let ref = decodeURIComponent(schema.$ref);
|
|
537
|
+
if (ref.startsWith('#')) {
|
|
538
|
+
ref = ref.substring(1);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const referenced = JsonPointer.get(spec, ref);
|
|
542
|
+
let result;
|
|
543
|
+
|
|
544
|
+
if ($refCache[ref] !== true) {
|
|
545
|
+
$refCache[ref] = true;
|
|
546
|
+
const traverseResult = traverse(referenced, options, spec, context);
|
|
547
|
+
if (options.format === 'xml') {
|
|
548
|
+
const {propertyName, value} = applyXMLAttributes(traverseResult, referenced, context);
|
|
549
|
+
result = {...traverseResult, value: {[propertyName || 'root']: value}};
|
|
550
|
+
} else {
|
|
551
|
+
result = traverseResult;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
$refCache[ref] = false;
|
|
555
|
+
} else {
|
|
556
|
+
const referencedType = inferType(referenced);
|
|
557
|
+
result = getResultForCircular(referencedType);
|
|
558
|
+
}
|
|
559
|
+
popSchemaStack(seenSchemasStack, context);
|
|
560
|
+
return result;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (schema.example !== undefined) {
|
|
564
|
+
popSchemaStack(seenSchemasStack, context);
|
|
565
|
+
return {
|
|
566
|
+
value: schema.example,
|
|
567
|
+
readOnly: schema.readOnly,
|
|
568
|
+
writeOnly: schema.writeOnly,
|
|
569
|
+
type: schema.type,
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (schema.allOf !== undefined) {
|
|
574
|
+
popSchemaStack(seenSchemasStack, context);
|
|
575
|
+
return tryInferExample(schema) || allOfSample(
|
|
576
|
+
{ ...schema, allOf: undefined },
|
|
577
|
+
schema.allOf,
|
|
578
|
+
options,
|
|
579
|
+
spec,
|
|
580
|
+
context,
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (schema.oneOf && schema.oneOf.length) {
|
|
585
|
+
if (schema.anyOf) {
|
|
586
|
+
if (!options.quiet) console.warn('oneOf and anyOf are not supported on the same level. Skipping anyOf');
|
|
587
|
+
}
|
|
588
|
+
popSchemaStack(seenSchemasStack, context);
|
|
589
|
+
|
|
590
|
+
// Make sure to pass down readOnly and writeOnly annotations from the parent
|
|
591
|
+
const firstOneOf = Object.assign({
|
|
592
|
+
readOnly: schema.readOnly,
|
|
593
|
+
writeOnly: schema.writeOnly
|
|
594
|
+
}, schema.oneOf[0]);
|
|
595
|
+
|
|
596
|
+
return traverseOneOrAnyOf(schema, firstOneOf)
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (schema.anyOf && schema.anyOf.length) {
|
|
600
|
+
popSchemaStack(seenSchemasStack, context);
|
|
601
|
+
|
|
602
|
+
// Make sure to pass down readOnly and writeOnly annotations from the parent
|
|
603
|
+
const firstAnyOf = Object.assign({
|
|
604
|
+
readOnly: schema.readOnly,
|
|
605
|
+
writeOnly: schema.writeOnly
|
|
606
|
+
}, schema.anyOf[0]);
|
|
607
|
+
|
|
608
|
+
return traverseOneOrAnyOf(schema, firstAnyOf)
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (schema.if && schema.then) {
|
|
612
|
+
popSchemaStack(seenSchemasStack, context);
|
|
613
|
+
const { if: ifSchema, then, ...rest } = schema;
|
|
614
|
+
return traverse(mergeDeep(rest, ifSchema, then), options, spec, context);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
let example = inferExample(schema);
|
|
618
|
+
let type = null;
|
|
619
|
+
if (example === undefined) {
|
|
620
|
+
example = null;
|
|
621
|
+
type = schema.type;
|
|
622
|
+
if (Array.isArray(type) && schema.type.length > 0) {
|
|
623
|
+
type = schema.type[0];
|
|
624
|
+
}
|
|
625
|
+
if (!type) {
|
|
626
|
+
type = inferType(schema);
|
|
627
|
+
}
|
|
628
|
+
let sampler = _samplers[type];
|
|
629
|
+
if (sampler) {
|
|
630
|
+
example = sampler(schema, options, spec, context);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
popSchemaStack(seenSchemasStack, context);
|
|
635
|
+
return {
|
|
636
|
+
value: example,
|
|
637
|
+
readOnly: schema.readOnly,
|
|
638
|
+
writeOnly: schema.writeOnly,
|
|
639
|
+
type: type
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
function traverseOneOrAnyOf(schema, selectedSubSchema) {
|
|
643
|
+
const inferred = tryInferExample(schema);
|
|
644
|
+
if (inferred !== undefined) {
|
|
645
|
+
return inferred;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const localExample = traverse({...schema, oneOf: undefined, anyOf: undefined }, options, spec, context);
|
|
649
|
+
const subSchemaExample = traverse(selectedSubSchema, options, spec, context);
|
|
650
|
+
|
|
651
|
+
if (typeof localExample.value === 'object' && typeof subSchemaExample.value === 'object') {
|
|
652
|
+
const mergedExample = mergeDeep(localExample.value, subSchemaExample.value);
|
|
653
|
+
return {...subSchemaExample, value: mergedExample };
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return subSchemaExample;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function sampleArray(schema, options = {}, spec, context) {
|
|
661
|
+
const depth = (context && context.depth || 1);
|
|
662
|
+
|
|
663
|
+
let arrayLength = Math.min(schema.maxItems != undefined ? schema.maxItems : Infinity, schema.minItems || 1);
|
|
664
|
+
// for the sake of simplicity, we're treating `contains` in a similar way to `items`
|
|
665
|
+
const items = schema.prefixItems || schema.items || schema.contains;
|
|
666
|
+
if (Array.isArray(items)) {
|
|
667
|
+
arrayLength = Math.max(arrayLength, items.length);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
let itemSchemaGetter = itemNumber => {
|
|
671
|
+
if (Array.isArray(items)) {
|
|
672
|
+
return items[itemNumber] || {};
|
|
673
|
+
}
|
|
674
|
+
return items || {};
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
let res = [];
|
|
678
|
+
if (!items) return res;
|
|
679
|
+
|
|
680
|
+
for (let i = 0; i < arrayLength; i++) {
|
|
681
|
+
let itemSchema = itemSchemaGetter(i);
|
|
682
|
+
let { value: sample } = traverse(itemSchema, options, spec, {depth: depth + 1});
|
|
683
|
+
if (options?.format === 'xml') {
|
|
684
|
+
const { value, propertyName } = applyXMLAttributes({value: sample}, itemSchema, context);
|
|
685
|
+
if (propertyName) {
|
|
686
|
+
if (!res?.[propertyName]) {
|
|
687
|
+
res = { ...res, [propertyName]: [] };
|
|
688
|
+
}
|
|
689
|
+
res[propertyName].push(value);
|
|
690
|
+
} else {
|
|
691
|
+
res = {...res, ...value};
|
|
692
|
+
}
|
|
693
|
+
} else {
|
|
694
|
+
res.push(sample);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (options?.format === 'xml' && depth === 1) {
|
|
699
|
+
const { value, propertyName } = applyXMLAttributes({value: null}, schema, context);
|
|
700
|
+
if (propertyName) {
|
|
701
|
+
if (value) {
|
|
702
|
+
res = Array.isArray(res) ? { [propertyName]: {...value, ...res.map(item => ({['#text']: {...item}}))} } : { [propertyName]: {...res, ...value }};
|
|
703
|
+
} else {
|
|
704
|
+
res = { [propertyName]: res };
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return res;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function sampleBoolean(schema) {
|
|
712
|
+
return true; // let be optimistic :)
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function sampleNumber(schema) {
|
|
716
|
+
let res = 0;
|
|
717
|
+
if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double')) {
|
|
718
|
+
res = 0.1;
|
|
719
|
+
}
|
|
720
|
+
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { //legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans
|
|
721
|
+
if (schema.maximum && schema.minimum) {
|
|
722
|
+
res = schema.exclusiveMinimum ? Math.floor(schema.minimum) + 1 : schema.minimum;
|
|
723
|
+
if ((schema.exclusiveMaximum && res >= schema.maximum) ||
|
|
724
|
+
((!schema.exclusiveMaximum && res > schema.maximum))) {
|
|
725
|
+
res = (schema.maximum + schema.minimum) / 2;
|
|
726
|
+
}
|
|
727
|
+
return res;
|
|
728
|
+
}
|
|
729
|
+
if (schema.minimum) {
|
|
730
|
+
if (schema.exclusiveMinimum) {
|
|
731
|
+
return Math.floor(schema.minimum) + 1;
|
|
732
|
+
} else {
|
|
733
|
+
return schema.minimum;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
if (schema.maximum) {
|
|
737
|
+
if (schema.exclusiveMaximum) {
|
|
738
|
+
return (schema.maximum > 0) ? 0 : Math.floor(schema.maximum) - 1;
|
|
739
|
+
} else {
|
|
740
|
+
return (schema.maximum > 0) ? 0 : schema.maximum;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
if (schema.minimum) {
|
|
745
|
+
return schema.minimum;
|
|
746
|
+
}
|
|
747
|
+
if (schema.exclusiveMinimum) {
|
|
748
|
+
res = Math.floor(schema.exclusiveMinimum) + 1;
|
|
749
|
+
|
|
750
|
+
if (res === schema.exclusiveMaximum) {
|
|
751
|
+
res = (res + Math.floor(schema.exclusiveMaximum) - 1) / 2;
|
|
752
|
+
}
|
|
753
|
+
} else if (schema.exclusiveMaximum) {
|
|
754
|
+
res = Math.floor(schema.exclusiveMaximum) - 1;
|
|
755
|
+
} else if (schema.maximum) {
|
|
756
|
+
res = schema.maximum;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
return res;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function sampleObject(schema, options = {}, spec, context) {
|
|
764
|
+
let res = {};
|
|
765
|
+
const depth = (context && context.depth || 1);
|
|
766
|
+
|
|
767
|
+
if (schema && typeof schema.properties === 'object') {
|
|
768
|
+
|
|
769
|
+
// Prepare for skipNonRequired option
|
|
770
|
+
const requiredProperties = Array.isArray(schema.required) ? schema.required : [];
|
|
771
|
+
const requiredPropertiesMap = {};
|
|
772
|
+
|
|
773
|
+
for (const requiredProperty of requiredProperties) {
|
|
774
|
+
requiredPropertiesMap[requiredProperty] = true;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
Object.keys(schema.properties).forEach(propertyName => {
|
|
778
|
+
// skip before traverse that could be costly
|
|
779
|
+
if (options.skipNonRequired && !requiredPropertiesMap.hasOwnProperty(propertyName)) {
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const sample = traverse(schema.properties[propertyName], options, spec, { propertyName, depth: depth + 1 });
|
|
784
|
+
if (options.skipReadOnly && sample.readOnly) {
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
if (options.skipWriteOnly && sample.writeOnly) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (options?.format === 'xml') {
|
|
793
|
+
const { propertyName: newPropertyName, value } = applyXMLAttributes(sample, schema.properties[propertyName], { propertyName });
|
|
794
|
+
if (newPropertyName) {
|
|
795
|
+
res[newPropertyName] = value;
|
|
796
|
+
} else {
|
|
797
|
+
res = { ...res, ...value };
|
|
798
|
+
}
|
|
799
|
+
} else {
|
|
800
|
+
res[propertyName] = sample.value;
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
if (schema && typeof schema.additionalProperties === 'object') {
|
|
806
|
+
const propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property';
|
|
807
|
+
res[`${String(propertyName)}1`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
|
|
808
|
+
res[`${String(propertyName)}2`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// Strictly enforce maxProperties constraint
|
|
812
|
+
if (schema && typeof schema.properties === 'object' && schema.maxProperties !== undefined && Object.keys(res).length > schema.maxProperties) {
|
|
813
|
+
const filteredResult = {};
|
|
814
|
+
let propertiesAdded = 0;
|
|
815
|
+
|
|
816
|
+
// Always include required properties first, if present
|
|
817
|
+
const requiredProperties = Array.isArray(schema.required) ? schema.required : [];
|
|
818
|
+
requiredProperties.forEach(propName => {
|
|
819
|
+
if (res[propName] !== undefined) {
|
|
820
|
+
filteredResult[propName] = res[propName];
|
|
821
|
+
propertiesAdded++;
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
// Add other properties until maxProperties is reached
|
|
826
|
+
Object.keys(res).forEach(propName => {
|
|
827
|
+
if (propertiesAdded < schema.maxProperties && !filteredResult.hasOwnProperty(propName)) {
|
|
828
|
+
filteredResult[propName] = res[propName];
|
|
829
|
+
propertiesAdded++;
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
res = filteredResult;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
return res;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
Faker - Copyright (c) 2022-2023
|
|
841
|
+
|
|
842
|
+
This software consists of voluntary contributions made by many individuals.
|
|
843
|
+
For exact contribution history, see the revision history
|
|
844
|
+
available at https://github.com/faker-js/faker
|
|
845
|
+
|
|
846
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
847
|
+
a copy of this software and associated documentation files (the
|
|
848
|
+
"Software"), to deal in the Software without restriction, including
|
|
849
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
850
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
851
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
852
|
+
the following conditions:
|
|
853
|
+
|
|
854
|
+
The above copyright notice and this permission notice shall be
|
|
855
|
+
included in all copies or substantial portions of the Software.
|
|
856
|
+
|
|
857
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
858
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
859
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
860
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
861
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
862
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
863
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
864
|
+
|
|
865
|
+
===
|
|
866
|
+
|
|
867
|
+
From: https://github.com/faker-js/faker/commit/a9f98046c7d5eeaabe12fc587024c06d683800b8
|
|
868
|
+
To: https://github.com/faker-js/faker/commit/29234378807c4141588861f69421bf20b5ac635e
|
|
869
|
+
|
|
870
|
+
Based on faker.js, copyright Marak Squires and contributor, what follows below is the original license.
|
|
871
|
+
|
|
872
|
+
===
|
|
873
|
+
|
|
874
|
+
faker.js - Copyright (c) 2020
|
|
875
|
+
Marak Squires
|
|
876
|
+
http://github.com/marak/faker.js/
|
|
877
|
+
|
|
878
|
+
faker.js was inspired by and has used data definitions from:
|
|
879
|
+
|
|
880
|
+
* https://github.com/stympy/faker/ - Copyright (c) 2007-2010 Benjamin Curtis
|
|
881
|
+
* http://search.cpan.org/~jasonk/Data-Faker-0.07/ - Copyright 2004-2005 by Jason Kohles
|
|
882
|
+
|
|
883
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
884
|
+
a copy of this software and associated documentation files (the
|
|
885
|
+
"Software"), to deal in the Software without restriction, including
|
|
886
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
887
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
888
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
889
|
+
the following conditions:
|
|
890
|
+
|
|
891
|
+
The above copyright notice and this permission notice shall be
|
|
892
|
+
included in all copies or substantial portions of the Software.
|
|
893
|
+
|
|
894
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
895
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
896
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
897
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
898
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
899
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
900
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
901
|
+
*/
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* @param {number} min - inclusive
|
|
906
|
+
* @param {number} _max - inclusive
|
|
907
|
+
* @returns {number}
|
|
908
|
+
*/
|
|
909
|
+
function intSample(min, _max) {
|
|
910
|
+
return min;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Returns a number based on given RegEx-based quantifier symbol or quantifier values.
|
|
915
|
+
*
|
|
916
|
+
* @param {string} quantifierSymbol Quantifier symbols can be either of these: `?`, `*`, `+`.
|
|
917
|
+
* @param {string} quantifierMin Quantifier minimum value. If given without a maximum, this will be used as the quantifier value.
|
|
918
|
+
* @param {string} quantifierMax Quantifier maximum value. Will randomly get a value between the minimum and maximum if both are provided.
|
|
919
|
+
*
|
|
920
|
+
* @returns {number} a random number based on the given quantifier parameters.
|
|
921
|
+
*
|
|
922
|
+
* @example
|
|
923
|
+
* getRepetitionsBasedOnQuantifierParameters('*', null, null) // 3
|
|
924
|
+
* getRepetitionsBasedOnQuantifierParameters(null, 10, null) // 10
|
|
925
|
+
* getRepetitionsBasedOnQuantifierParameters(null, 5, 8) // 6
|
|
926
|
+
*
|
|
927
|
+
* @since 8.0.0
|
|
928
|
+
*/
|
|
929
|
+
function getRepetitionsBasedOnQuantifierParameters(
|
|
930
|
+
quantifierSymbol,
|
|
931
|
+
quantifierMin,
|
|
932
|
+
quantifierMax
|
|
933
|
+
) {
|
|
934
|
+
let repetitions = 1;
|
|
935
|
+
if (quantifierSymbol) {
|
|
936
|
+
switch (quantifierSymbol) {
|
|
937
|
+
case '?': {
|
|
938
|
+
repetitions = 0 ;
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
case '*': {
|
|
943
|
+
repetitions = intSample(0);
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
case '+': {
|
|
948
|
+
repetitions = intSample(1);
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
default:
|
|
953
|
+
throw new Error('Unknown quantifier symbol provided.');
|
|
954
|
+
}
|
|
955
|
+
} else if (quantifierMin != null && quantifierMax != null) {
|
|
956
|
+
repetitions = intSample(parseInt(quantifierMin));
|
|
957
|
+
} else if (quantifierMin != null && quantifierMax == null) {
|
|
958
|
+
repetitions = parseInt(quantifierMin);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
return repetitions;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Generates a string matching the given regex like expressions.
|
|
966
|
+
*
|
|
967
|
+
* This function doesn't provide full support of actual `RegExp`.
|
|
968
|
+
* Features such as grouping, anchors and character classes are not supported.
|
|
969
|
+
* If you are looking for a library that randomly generates strings based on
|
|
970
|
+
* `RegExp`s, see [randexp.js](https://github.com/fent/randexp.js)
|
|
971
|
+
*
|
|
972
|
+
* Supported patterns:
|
|
973
|
+
* - `x{times}` => Repeat the `x` exactly `times` times.
|
|
974
|
+
* - `x{min,max}` => Repeat the `x` `min` to `max` times.
|
|
975
|
+
* - `[x-y]` => Randomly get a character between `x` and `y` (inclusive).
|
|
976
|
+
* - `[x-y]{times}` => Randomly get a character between `x` and `y` (inclusive) and repeat it `times` times.
|
|
977
|
+
* - `[x-y]{min,max}` => Randomly get a character between `x` and `y` (inclusive) and repeat it `min` to `max` times.
|
|
978
|
+
* - `[^...]` => Randomly get an ASCII number or letter character that is not in the given range. (e.g. `[^0-9]` will get a random non-numeric character).
|
|
979
|
+
* - `[-...]` => Include dashes in the range. Must be placed after the negate character `^` and before any character sets if used (e.g. `[^-0-9]` will not get any numeric characters or dashes).
|
|
980
|
+
* - `/[x-y]/i` => Randomly gets an uppercase or lowercase character between `x` and `y` (inclusive).
|
|
981
|
+
* - `x?` => Randomly decide to include or not include `x`.
|
|
982
|
+
* - `[x-y]?` => Randomly decide to include or not include characters between `x` and `y` (inclusive).
|
|
983
|
+
* - `x*` => Repeat `x` 0 or more times.
|
|
984
|
+
* - `[x-y]*` => Repeat characters between `x` and `y` (inclusive) 0 or more times.
|
|
985
|
+
* - `x+` => Repeat `x` 1 or more times.
|
|
986
|
+
* - `[x-y]+` => Repeat characters between `x` and `y` (inclusive) 1 or more times.
|
|
987
|
+
* - `.` => returns a wildcard ASCII character that can be any number, character or symbol. Can be combined with quantifiers as well.
|
|
988
|
+
*
|
|
989
|
+
* @param {string | RegExp} pattern The template string/RegExp to generate a matching string for.
|
|
990
|
+
* @returns {string} A string matching the given pattern.
|
|
991
|
+
*
|
|
992
|
+
* @throws If min value is more than max value in quantifier. e.g. `#{10,5}`
|
|
993
|
+
* @throws If invalid quantifier symbol is passed in.
|
|
994
|
+
*
|
|
995
|
+
* @example
|
|
996
|
+
* regexSample('#{5}') // '#####'
|
|
997
|
+
* regexSample('#{2,9}') // '#######'
|
|
998
|
+
* regexSample('[1-7]') // '5'
|
|
999
|
+
* regexSample('#{3}test[1-5]') // '###test3'
|
|
1000
|
+
* regexSample('[0-9a-dmno]') // '5'
|
|
1001
|
+
* regexSample('[^a-zA-Z0-8]') // '9'
|
|
1002
|
+
* regexSample('[a-d0-6]{2,8}') // 'a0dc45b0'
|
|
1003
|
+
* regexSample('[-a-z]{5}') // 'a-zab'
|
|
1004
|
+
* regexSample(/[A-Z0-9]{4}-[A-Z0-9]{4}/) // 'BS4G-485H'
|
|
1005
|
+
* regexSample(/[A-Z]{5}/i) // 'pDKfh'
|
|
1006
|
+
* regexSample(/.{5}/) // '14(#B'
|
|
1007
|
+
* regexSample(/Joh?n/) // 'Jon'
|
|
1008
|
+
* regexSample(/ABC*DE/) // 'ABDE'
|
|
1009
|
+
* regexSample(/bee+p/) // 'beeeeeeeep'
|
|
1010
|
+
*
|
|
1011
|
+
* @since 8.0.0
|
|
1012
|
+
*/
|
|
1013
|
+
function regexSample$1(pattern) {
|
|
1014
|
+
let isCaseInsensitive = false;
|
|
1015
|
+
|
|
1016
|
+
if (pattern instanceof RegExp) {
|
|
1017
|
+
isCaseInsensitive = pattern.flags.includes('i');
|
|
1018
|
+
pattern = pattern.toString();
|
|
1019
|
+
pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
let min;
|
|
1023
|
+
let max;
|
|
1024
|
+
let repetitions;
|
|
1025
|
+
|
|
1026
|
+
// Deal with single wildcards
|
|
1027
|
+
const SINGLE_CHAR_REG =
|
|
1028
|
+
/([.A-Za-z0-9])(?:\{(\d+)(?:\,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
|
|
1029
|
+
let token = pattern.match(SINGLE_CHAR_REG);
|
|
1030
|
+
while (token != null) {
|
|
1031
|
+
const quantifierMin = token[2];
|
|
1032
|
+
const quantifierMax = token[3];
|
|
1033
|
+
const quantifierSymbol = token[4];
|
|
1034
|
+
|
|
1035
|
+
repetitions = getRepetitionsBasedOnQuantifierParameters(
|
|
1036
|
+
quantifierSymbol,
|
|
1037
|
+
quantifierMin,
|
|
1038
|
+
quantifierMax
|
|
1039
|
+
);
|
|
1040
|
+
|
|
1041
|
+
pattern =
|
|
1042
|
+
pattern.slice(0, token.index) +
|
|
1043
|
+
token[1].repeat(repetitions) +
|
|
1044
|
+
pattern.slice(token.index + token[0].length);
|
|
1045
|
+
token = pattern.match(SINGLE_CHAR_REG);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/;
|
|
1049
|
+
const RANGE_ALPHANUMEMRIC_REG =
|
|
1050
|
+
/\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:\,(\d+)|)\}|(\?|\*|\+)|)/;
|
|
1051
|
+
// Deal with character classes with quantifiers `[a-z0-9]{min[, max]}`
|
|
1052
|
+
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
|
|
1053
|
+
while (token != null) {
|
|
1054
|
+
const isNegated = token[1] === '^';
|
|
1055
|
+
const includesDash = token[2] === '-';
|
|
1056
|
+
const quantifierMin = token[4];
|
|
1057
|
+
const quantifierMax = token[5];
|
|
1058
|
+
const quantifierSymbol = token[6];
|
|
1059
|
+
|
|
1060
|
+
const rangeCodes = [];
|
|
1061
|
+
|
|
1062
|
+
let ranges = token[3];
|
|
1063
|
+
let range = ranges.match(SINGLE_RANGE_REG);
|
|
1064
|
+
|
|
1065
|
+
if (includesDash) {
|
|
1066
|
+
// 45 is the ascii code for '-'
|
|
1067
|
+
rangeCodes.push(45);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
while (range != null) {
|
|
1071
|
+
if (range[0].indexOf('-') === -1) {
|
|
1072
|
+
// handle non-ranges
|
|
1073
|
+
if (isCaseInsensitive && isNaN(Number(range[0]))) {
|
|
1074
|
+
rangeCodes.push(range[0].toUpperCase().charCodeAt(0));
|
|
1075
|
+
rangeCodes.push(range[0].toLowerCase().charCodeAt(0));
|
|
1076
|
+
} else {
|
|
1077
|
+
rangeCodes.push(range[0].charCodeAt(0));
|
|
1078
|
+
}
|
|
1079
|
+
} else {
|
|
1080
|
+
// handle ranges
|
|
1081
|
+
const rangeMinMax = range[0].split('-').map((x) => x.charCodeAt(0));
|
|
1082
|
+
min = rangeMinMax[0];
|
|
1083
|
+
max = rangeMinMax[1];
|
|
1084
|
+
// throw error if min larger than max
|
|
1085
|
+
if (min > max) {
|
|
1086
|
+
throw new Error('Character range provided is out of order.');
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
for (let i = min; i <= max; i++) {
|
|
1090
|
+
if (isCaseInsensitive && isNaN(Number(String.fromCharCode(i)))) {
|
|
1091
|
+
const ch = String.fromCharCode(i);
|
|
1092
|
+
rangeCodes.push(ch.toUpperCase().charCodeAt(0));
|
|
1093
|
+
rangeCodes.push(ch.toLowerCase().charCodeAt(0));
|
|
1094
|
+
} else {
|
|
1095
|
+
rangeCodes.push(i);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
ranges = ranges.substring(range[0].length);
|
|
1101
|
+
range = ranges.match(SINGLE_RANGE_REG);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
repetitions = getRepetitionsBasedOnQuantifierParameters(
|
|
1105
|
+
quantifierSymbol,
|
|
1106
|
+
quantifierMin,
|
|
1107
|
+
quantifierMax
|
|
1108
|
+
);
|
|
1109
|
+
|
|
1110
|
+
if (isNegated) {
|
|
1111
|
+
let index = -1;
|
|
1112
|
+
// 0-9
|
|
1113
|
+
for (let i = 48; i <= 57; i++) {
|
|
1114
|
+
index = rangeCodes.indexOf(i);
|
|
1115
|
+
if (index > -1) {
|
|
1116
|
+
rangeCodes.splice(index, 1);
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
rangeCodes.push(i);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// A-Z
|
|
1124
|
+
for (let i = 65; i <= 90; i++) {
|
|
1125
|
+
index = rangeCodes.indexOf(i);
|
|
1126
|
+
if (index > -1) {
|
|
1127
|
+
rangeCodes.splice(index, 1);
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
rangeCodes.push(i);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// a-z
|
|
1135
|
+
for (let i = 97; i <= 122; i++) {
|
|
1136
|
+
index = rangeCodes.indexOf(i);
|
|
1137
|
+
if (index > -1) {
|
|
1138
|
+
rangeCodes.splice(index, 1);
|
|
1139
|
+
continue;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
rangeCodes.push(i);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const generatedString = Array.from(
|
|
1147
|
+
{ length: repetitions },
|
|
1148
|
+
() => String.fromCharCode(rangeCodes[intSample(0)]),
|
|
1149
|
+
).join('');
|
|
1150
|
+
|
|
1151
|
+
pattern =
|
|
1152
|
+
pattern.slice(0, token.index) +
|
|
1153
|
+
generatedString +
|
|
1154
|
+
pattern.slice(token.index + token[0].length);
|
|
1155
|
+
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
const RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/;
|
|
1159
|
+
// Deal with quantifier ranges `{min,max}`
|
|
1160
|
+
token = pattern.match(RANGE_REP_REG);
|
|
1161
|
+
while (token != null) {
|
|
1162
|
+
min = parseInt(token[2]);
|
|
1163
|
+
max = parseInt(token[3]);
|
|
1164
|
+
// throw error if min larger than max
|
|
1165
|
+
if (min > max) {
|
|
1166
|
+
throw new Error('Numbers out of order in {} quantifier.');
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
repetitions = intSample(min);
|
|
1170
|
+
pattern =
|
|
1171
|
+
pattern.slice(0, token.index) +
|
|
1172
|
+
token[1].repeat(repetitions) +
|
|
1173
|
+
pattern.slice(token.index + token[0].length);
|
|
1174
|
+
token = pattern.match(RANGE_REP_REG);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
const REP_REG = /(.)\{(\d+)\}/;
|
|
1178
|
+
// Deal with repeat `{num}`
|
|
1179
|
+
token = pattern.match(REP_REG);
|
|
1180
|
+
while (token != null) {
|
|
1181
|
+
repetitions = parseInt(token[2]);
|
|
1182
|
+
pattern =
|
|
1183
|
+
pattern.slice(0, token.index) +
|
|
1184
|
+
token[1].repeat(repetitions) +
|
|
1185
|
+
pattern.slice(token.index + token[0].length);
|
|
1186
|
+
token = pattern.match(REP_REG);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
return pattern;
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
const passwordSymbols = 'qwerty!@#$%^123456';
|
|
1193
|
+
|
|
1194
|
+
function emailSample() {
|
|
1195
|
+
return 'user@example.com';
|
|
1196
|
+
}
|
|
1197
|
+
function idnEmailSample() {
|
|
1198
|
+
return 'пошта@укр.нет';
|
|
1199
|
+
}
|
|
1200
|
+
function passwordSample(min, max) {
|
|
1201
|
+
let res = 'pa$$word';
|
|
1202
|
+
if (min > res.length) {
|
|
1203
|
+
res += '_';
|
|
1204
|
+
res += ensureMinLength(passwordSymbols, min - res.length).substring(0, min - res.length);
|
|
1205
|
+
}
|
|
1206
|
+
return res;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
function commonDateTimeSample({ min, max, omitTime, omitDate }) {
|
|
1210
|
+
let res = toRFCDateTime(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate);
|
|
1211
|
+
if (res.length < min) {
|
|
1212
|
+
console.warn(`Using minLength = ${min} is incorrect with format "date-time"`);
|
|
1213
|
+
}
|
|
1214
|
+
if (max && res.length > max) {
|
|
1215
|
+
console.warn(`Using maxLength = ${max} is incorrect with format "date-time"`);
|
|
1216
|
+
}
|
|
1217
|
+
return res;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function dateTimeSample(min, max) {
|
|
1221
|
+
return commonDateTimeSample({ min, max, omitTime: false, omitDate: false });
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
function dateSample(min, max) {
|
|
1225
|
+
return commonDateTimeSample({ min, max, omitTime: true, omitDate: false });
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
function timeSample(min, max) {
|
|
1229
|
+
return commonDateTimeSample({ min, max, omitTime: false, omitDate: true }).slice(1);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
function defaultSample(min, max, _propertyName, pattern, enablePatterns = false) {
|
|
1233
|
+
if (pattern && enablePatterns) {
|
|
1234
|
+
return regexSample$1(pattern);
|
|
1235
|
+
}
|
|
1236
|
+
let res = ensureMinLength('string', min);
|
|
1237
|
+
if (max && res.length > max) {
|
|
1238
|
+
res = res.substring(0, max);
|
|
1239
|
+
}
|
|
1240
|
+
return res;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
function ipv4Sample() {
|
|
1244
|
+
return '192.168.0.1';
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
function ipv6Sample() {
|
|
1248
|
+
return '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
function hostnameSample() {
|
|
1252
|
+
return 'example.com';
|
|
1253
|
+
}
|
|
1254
|
+
function idnHostnameSample() {
|
|
1255
|
+
return 'приклад.укр';
|
|
1256
|
+
}
|
|
1257
|
+
function uriSample() {
|
|
1258
|
+
return 'http://example.com';
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function uriReferenceSample() {
|
|
1262
|
+
return '../dictionary';
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
function uriTemplateSample() {
|
|
1266
|
+
return 'http://example.com/{endpoint}';
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
function iriSample() {
|
|
1270
|
+
return 'http://example.com/entity/1';
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
function iriReferenceSample() {
|
|
1274
|
+
return '/entity/1';
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
function uuidSample(_min, _max, propertyName) {
|
|
1278
|
+
return uuid(propertyName || 'id');
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
function jsonPointerSample() {
|
|
1282
|
+
return '/json/pointer';
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function relativeJsonPointerSample() {
|
|
1286
|
+
return '1/relative/json/pointer';
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
function regexSample() {
|
|
1290
|
+
return '/regex/';
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
const stringFormats = {
|
|
1294
|
+
'email': emailSample,
|
|
1295
|
+
'idn-email': idnEmailSample, // https://tools.ietf.org/html/rfc6531#section-3.3
|
|
1296
|
+
'password': passwordSample,
|
|
1297
|
+
'date-time': dateTimeSample,
|
|
1298
|
+
'date': dateSample,
|
|
1299
|
+
'time': timeSample, // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
|
|
1300
|
+
'ipv4': ipv4Sample,
|
|
1301
|
+
'ipv6': ipv6Sample,
|
|
1302
|
+
'hostname': hostnameSample,
|
|
1303
|
+
'idn-hostname': idnHostnameSample, // https://tools.ietf.org/html/rfc5890#section-2.3.2.3
|
|
1304
|
+
'iri': iriSample, // https://tools.ietf.org/html/rfc3987
|
|
1305
|
+
'iri-reference': iriReferenceSample,
|
|
1306
|
+
'uri': uriSample,
|
|
1307
|
+
'uri-reference': uriReferenceSample, // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
|
|
1308
|
+
'uri-template': uriTemplateSample,
|
|
1309
|
+
'uuid': uuidSample,
|
|
1310
|
+
'default': defaultSample,
|
|
1311
|
+
'json-pointer': jsonPointerSample,
|
|
1312
|
+
'relative-json-pointer': relativeJsonPointerSample, // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
|
|
1313
|
+
'regex': regexSample,
|
|
1314
|
+
};
|
|
1315
|
+
|
|
1316
|
+
function sampleString(schema, options, spec, context) {
|
|
1317
|
+
let format = schema.format || 'default';
|
|
1318
|
+
let sampler = stringFormats[format] || defaultSample;
|
|
1319
|
+
let propertyName = context && context.propertyName;
|
|
1320
|
+
return sampler(
|
|
1321
|
+
schema.minLength || 0,
|
|
1322
|
+
schema.maxLength,
|
|
1323
|
+
propertyName,
|
|
1324
|
+
schema.pattern,
|
|
1325
|
+
options?.enablePatterns
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
var validator$2 = {};
|
|
1330
|
+
|
|
1331
|
+
var util$3 = {};
|
|
1332
|
+
|
|
1333
|
+
(function (exports) {
|
|
1334
|
+
|
|
1335
|
+
const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD';
|
|
1336
|
+
const nameChar = nameStartChar + '\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040';
|
|
1337
|
+
const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*';
|
|
1338
|
+
const regexName = new RegExp('^' + nameRegexp + '$');
|
|
1339
|
+
|
|
1340
|
+
const getAllMatches = function(string, regex) {
|
|
1341
|
+
const matches = [];
|
|
1342
|
+
let match = regex.exec(string);
|
|
1343
|
+
while (match) {
|
|
1344
|
+
const allmatches = [];
|
|
1345
|
+
allmatches.startIndex = regex.lastIndex - match[0].length;
|
|
1346
|
+
const len = match.length;
|
|
1347
|
+
for (let index = 0; index < len; index++) {
|
|
1348
|
+
allmatches.push(match[index]);
|
|
1349
|
+
}
|
|
1350
|
+
matches.push(allmatches);
|
|
1351
|
+
match = regex.exec(string);
|
|
1352
|
+
}
|
|
1353
|
+
return matches;
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
const isName = function(string) {
|
|
1357
|
+
const match = regexName.exec(string);
|
|
1358
|
+
return !(match === null || typeof match === 'undefined');
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
exports.isExist = function(v) {
|
|
1362
|
+
return typeof v !== 'undefined';
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
exports.isEmptyObject = function(obj) {
|
|
1366
|
+
return Object.keys(obj).length === 0;
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Copy all the properties of a into b.
|
|
1371
|
+
* @param {*} target
|
|
1372
|
+
* @param {*} a
|
|
1373
|
+
*/
|
|
1374
|
+
exports.merge = function(target, a, arrayMode) {
|
|
1375
|
+
if (a) {
|
|
1376
|
+
const keys = Object.keys(a); // will return an array of own properties
|
|
1377
|
+
const len = keys.length; //don't make it inline
|
|
1378
|
+
for (let i = 0; i < len; i++) {
|
|
1379
|
+
if (arrayMode === 'strict') {
|
|
1380
|
+
target[keys[i]] = [ a[keys[i]] ];
|
|
1381
|
+
} else {
|
|
1382
|
+
target[keys[i]] = a[keys[i]];
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
/* exports.merge =function (b,a){
|
|
1388
|
+
return Object.assign(b,a);
|
|
1389
|
+
} */
|
|
1390
|
+
|
|
1391
|
+
exports.getValue = function(v) {
|
|
1392
|
+
if (exports.isExist(v)) {
|
|
1393
|
+
return v;
|
|
1394
|
+
} else {
|
|
1395
|
+
return '';
|
|
1396
|
+
}
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
// const fakeCall = function(a) {return a;};
|
|
1400
|
+
// const fakeCallNoReturn = function() {};
|
|
1401
|
+
|
|
1402
|
+
exports.isName = isName;
|
|
1403
|
+
exports.getAllMatches = getAllMatches;
|
|
1404
|
+
exports.nameRegexp = nameRegexp;
|
|
1405
|
+
} (util$3));
|
|
1406
|
+
|
|
1407
|
+
const util$2 = util$3;
|
|
1408
|
+
|
|
1409
|
+
const defaultOptions$2 = {
|
|
1410
|
+
allowBooleanAttributes: false, //A tag can have attributes without any value
|
|
1411
|
+
unpairedTags: []
|
|
1412
|
+
};
|
|
1413
|
+
|
|
1414
|
+
//const tagsPattern = new RegExp("<\\/?([\\w:\\-_\.]+)\\s*\/?>","g");
|
|
1415
|
+
validator$2.validate = function (xmlData, options) {
|
|
1416
|
+
options = Object.assign({}, defaultOptions$2, options);
|
|
1417
|
+
|
|
1418
|
+
//xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
|
|
1419
|
+
//xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag
|
|
1420
|
+
//xmlData = xmlData.replace(/(<!DOCTYPE[\s\w\"\.\/\-\:]+(\[.*\])*\s*>)/g,"");//Remove DOCTYPE
|
|
1421
|
+
const tags = [];
|
|
1422
|
+
let tagFound = false;
|
|
1423
|
+
|
|
1424
|
+
//indicates that the root tag has been closed (aka. depth 0 has been reached)
|
|
1425
|
+
let reachedRoot = false;
|
|
1426
|
+
|
|
1427
|
+
if (xmlData[0] === '\ufeff') {
|
|
1428
|
+
// check for byte order mark (BOM)
|
|
1429
|
+
xmlData = xmlData.substr(1);
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
for (let i = 0; i < xmlData.length; i++) {
|
|
1433
|
+
|
|
1434
|
+
if (xmlData[i] === '<' && xmlData[i+1] === '?') {
|
|
1435
|
+
i+=2;
|
|
1436
|
+
i = readPI(xmlData,i);
|
|
1437
|
+
if (i.err) return i;
|
|
1438
|
+
}else if (xmlData[i] === '<') {
|
|
1439
|
+
//starting of tag
|
|
1440
|
+
//read until you reach to '>' avoiding any '>' in attribute value
|
|
1441
|
+
let tagStartPos = i;
|
|
1442
|
+
i++;
|
|
1443
|
+
|
|
1444
|
+
if (xmlData[i] === '!') {
|
|
1445
|
+
i = readCommentAndCDATA(xmlData, i);
|
|
1446
|
+
continue;
|
|
1447
|
+
} else {
|
|
1448
|
+
let closingTag = false;
|
|
1449
|
+
if (xmlData[i] === '/') {
|
|
1450
|
+
//closing tag
|
|
1451
|
+
closingTag = true;
|
|
1452
|
+
i++;
|
|
1453
|
+
}
|
|
1454
|
+
//read tagname
|
|
1455
|
+
let tagName = '';
|
|
1456
|
+
for (; i < xmlData.length &&
|
|
1457
|
+
xmlData[i] !== '>' &&
|
|
1458
|
+
xmlData[i] !== ' ' &&
|
|
1459
|
+
xmlData[i] !== '\t' &&
|
|
1460
|
+
xmlData[i] !== '\n' &&
|
|
1461
|
+
xmlData[i] !== '\r'; i++
|
|
1462
|
+
) {
|
|
1463
|
+
tagName += xmlData[i];
|
|
1464
|
+
}
|
|
1465
|
+
tagName = tagName.trim();
|
|
1466
|
+
//console.log(tagName);
|
|
1467
|
+
|
|
1468
|
+
if (tagName[tagName.length - 1] === '/') {
|
|
1469
|
+
//self closing tag without attributes
|
|
1470
|
+
tagName = tagName.substring(0, tagName.length - 1);
|
|
1471
|
+
//continue;
|
|
1472
|
+
i--;
|
|
1473
|
+
}
|
|
1474
|
+
if (!validateTagName(tagName)) {
|
|
1475
|
+
let msg;
|
|
1476
|
+
if (tagName.trim().length === 0) {
|
|
1477
|
+
msg = "Invalid space after '<'.";
|
|
1478
|
+
} else {
|
|
1479
|
+
msg = "Tag '"+tagName+"' is an invalid name.";
|
|
1480
|
+
}
|
|
1481
|
+
return getErrorObject('InvalidTag', msg, getLineNumberForPosition(xmlData, i));
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
const result = readAttributeStr(xmlData, i);
|
|
1485
|
+
if (result === false) {
|
|
1486
|
+
return getErrorObject('InvalidAttr', "Attributes for '"+tagName+"' have open quote.", getLineNumberForPosition(xmlData, i));
|
|
1487
|
+
}
|
|
1488
|
+
let attrStr = result.value;
|
|
1489
|
+
i = result.index;
|
|
1490
|
+
|
|
1491
|
+
if (attrStr[attrStr.length - 1] === '/') {
|
|
1492
|
+
//self closing tag
|
|
1493
|
+
const attrStrStart = i - attrStr.length;
|
|
1494
|
+
attrStr = attrStr.substring(0, attrStr.length - 1);
|
|
1495
|
+
const isValid = validateAttributeString(attrStr, options);
|
|
1496
|
+
if (isValid === true) {
|
|
1497
|
+
tagFound = true;
|
|
1498
|
+
//continue; //text may presents after self closing tag
|
|
1499
|
+
} else {
|
|
1500
|
+
//the result from the nested function returns the position of the error within the attribute
|
|
1501
|
+
//in order to get the 'true' error line, we need to calculate the position where the attribute begins (i - attrStr.length) and then add the position within the attribute
|
|
1502
|
+
//this gives us the absolute index in the entire xml, which we can use to find the line at last
|
|
1503
|
+
return getErrorObject(isValid.err.code, isValid.err.msg, getLineNumberForPosition(xmlData, attrStrStart + isValid.err.line));
|
|
1504
|
+
}
|
|
1505
|
+
} else if (closingTag) {
|
|
1506
|
+
if (!result.tagClosed) {
|
|
1507
|
+
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
|
|
1508
|
+
} else if (attrStr.trim().length > 0) {
|
|
1509
|
+
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
1510
|
+
} else if (tags.length === 0) {
|
|
1511
|
+
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos));
|
|
1512
|
+
} else {
|
|
1513
|
+
const otg = tags.pop();
|
|
1514
|
+
if (tagName !== otg.tagName) {
|
|
1515
|
+
let openPos = getLineNumberForPosition(xmlData, otg.tagStartPos);
|
|
1516
|
+
return getErrorObject('InvalidTag',
|
|
1517
|
+
"Expected closing tag '"+otg.tagName+"' (opened in line "+openPos.line+", col "+openPos.col+") instead of closing tag '"+tagName+"'.",
|
|
1518
|
+
getLineNumberForPosition(xmlData, tagStartPos));
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
//when there are no more tags, we reached the root level.
|
|
1522
|
+
if (tags.length == 0) {
|
|
1523
|
+
reachedRoot = true;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
} else {
|
|
1527
|
+
const isValid = validateAttributeString(attrStr, options);
|
|
1528
|
+
if (isValid !== true) {
|
|
1529
|
+
//the result from the nested function returns the position of the error within the attribute
|
|
1530
|
+
//in order to get the 'true' error line, we need to calculate the position where the attribute begins (i - attrStr.length) and then add the position within the attribute
|
|
1531
|
+
//this gives us the absolute index in the entire xml, which we can use to find the line at last
|
|
1532
|
+
return getErrorObject(isValid.err.code, isValid.err.msg, getLineNumberForPosition(xmlData, i - attrStr.length + isValid.err.line));
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
//if the root level has been reached before ...
|
|
1536
|
+
if (reachedRoot === true) {
|
|
1537
|
+
return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, i));
|
|
1538
|
+
} else if(options.unpairedTags.indexOf(tagName) !== -1); else {
|
|
1539
|
+
tags.push({tagName, tagStartPos});
|
|
1540
|
+
}
|
|
1541
|
+
tagFound = true;
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
//skip tag text value
|
|
1545
|
+
//It may include comments and CDATA value
|
|
1546
|
+
for (i++; i < xmlData.length; i++) {
|
|
1547
|
+
if (xmlData[i] === '<') {
|
|
1548
|
+
if (xmlData[i + 1] === '!') {
|
|
1549
|
+
//comment or CADATA
|
|
1550
|
+
i++;
|
|
1551
|
+
i = readCommentAndCDATA(xmlData, i);
|
|
1552
|
+
continue;
|
|
1553
|
+
} else if (xmlData[i+1] === '?') {
|
|
1554
|
+
i = readPI(xmlData, ++i);
|
|
1555
|
+
if (i.err) return i;
|
|
1556
|
+
} else {
|
|
1557
|
+
break;
|
|
1558
|
+
}
|
|
1559
|
+
} else if (xmlData[i] === '&') {
|
|
1560
|
+
const afterAmp = validateAmpersand(xmlData, i);
|
|
1561
|
+
if (afterAmp == -1)
|
|
1562
|
+
return getErrorObject('InvalidChar', "char '&' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
1563
|
+
i = afterAmp;
|
|
1564
|
+
}else {
|
|
1565
|
+
if (reachedRoot === true && !isWhiteSpace(xmlData[i])) {
|
|
1566
|
+
return getErrorObject('InvalidXml', "Extra text at the end", getLineNumberForPosition(xmlData, i));
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
} //end of reading tag text value
|
|
1570
|
+
if (xmlData[i] === '<') {
|
|
1571
|
+
i--;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
} else {
|
|
1575
|
+
if ( isWhiteSpace(xmlData[i])) {
|
|
1576
|
+
continue;
|
|
1577
|
+
}
|
|
1578
|
+
return getErrorObject('InvalidChar', "char '"+xmlData[i]+"' is not expected.", getLineNumberForPosition(xmlData, i));
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
if (!tagFound) {
|
|
1583
|
+
return getErrorObject('InvalidXml', 'Start tag expected.', 1);
|
|
1584
|
+
}else if (tags.length == 1) {
|
|
1585
|
+
return getErrorObject('InvalidTag', "Unclosed tag '"+tags[0].tagName+"'.", getLineNumberForPosition(xmlData, tags[0].tagStartPos));
|
|
1586
|
+
}else if (tags.length > 0) {
|
|
1587
|
+
return getErrorObject('InvalidXml', "Invalid '"+
|
|
1588
|
+
JSON.stringify(tags.map(t => t.tagName), null, 4).replace(/\r?\n/g, '')+
|
|
1589
|
+
"' found.", {line: 1, col: 1});
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
return true;
|
|
1593
|
+
};
|
|
1594
|
+
|
|
1595
|
+
function isWhiteSpace(char){
|
|
1596
|
+
return char === ' ' || char === '\t' || char === '\n' || char === '\r';
|
|
1597
|
+
}
|
|
1598
|
+
/**
|
|
1599
|
+
* Read Processing insstructions and skip
|
|
1600
|
+
* @param {*} xmlData
|
|
1601
|
+
* @param {*} i
|
|
1602
|
+
*/
|
|
1603
|
+
function readPI(xmlData, i) {
|
|
1604
|
+
const start = i;
|
|
1605
|
+
for (; i < xmlData.length; i++) {
|
|
1606
|
+
if (xmlData[i] == '?' || xmlData[i] == ' ') {
|
|
1607
|
+
//tagname
|
|
1608
|
+
const tagname = xmlData.substr(start, i - start);
|
|
1609
|
+
if (i > 5 && tagname === 'xml') {
|
|
1610
|
+
return getErrorObject('InvalidXml', 'XML declaration allowed only at the start of the document.', getLineNumberForPosition(xmlData, i));
|
|
1611
|
+
} else if (xmlData[i] == '?' && xmlData[i + 1] == '>') {
|
|
1612
|
+
//check if valid attribut string
|
|
1613
|
+
i++;
|
|
1614
|
+
break;
|
|
1615
|
+
} else {
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
return i;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function readCommentAndCDATA(xmlData, i) {
|
|
1624
|
+
if (xmlData.length > i + 5 && xmlData[i + 1] === '-' && xmlData[i + 2] === '-') {
|
|
1625
|
+
//comment
|
|
1626
|
+
for (i += 3; i < xmlData.length; i++) {
|
|
1627
|
+
if (xmlData[i] === '-' && xmlData[i + 1] === '-' && xmlData[i + 2] === '>') {
|
|
1628
|
+
i += 2;
|
|
1629
|
+
break;
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
} else if (
|
|
1633
|
+
xmlData.length > i + 8 &&
|
|
1634
|
+
xmlData[i + 1] === 'D' &&
|
|
1635
|
+
xmlData[i + 2] === 'O' &&
|
|
1636
|
+
xmlData[i + 3] === 'C' &&
|
|
1637
|
+
xmlData[i + 4] === 'T' &&
|
|
1638
|
+
xmlData[i + 5] === 'Y' &&
|
|
1639
|
+
xmlData[i + 6] === 'P' &&
|
|
1640
|
+
xmlData[i + 7] === 'E'
|
|
1641
|
+
) {
|
|
1642
|
+
let angleBracketsCount = 1;
|
|
1643
|
+
for (i += 8; i < xmlData.length; i++) {
|
|
1644
|
+
if (xmlData[i] === '<') {
|
|
1645
|
+
angleBracketsCount++;
|
|
1646
|
+
} else if (xmlData[i] === '>') {
|
|
1647
|
+
angleBracketsCount--;
|
|
1648
|
+
if (angleBracketsCount === 0) {
|
|
1649
|
+
break;
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
} else if (
|
|
1654
|
+
xmlData.length > i + 9 &&
|
|
1655
|
+
xmlData[i + 1] === '[' &&
|
|
1656
|
+
xmlData[i + 2] === 'C' &&
|
|
1657
|
+
xmlData[i + 3] === 'D' &&
|
|
1658
|
+
xmlData[i + 4] === 'A' &&
|
|
1659
|
+
xmlData[i + 5] === 'T' &&
|
|
1660
|
+
xmlData[i + 6] === 'A' &&
|
|
1661
|
+
xmlData[i + 7] === '['
|
|
1662
|
+
) {
|
|
1663
|
+
for (i += 8; i < xmlData.length; i++) {
|
|
1664
|
+
if (xmlData[i] === ']' && xmlData[i + 1] === ']' && xmlData[i + 2] === '>') {
|
|
1665
|
+
i += 2;
|
|
1666
|
+
break;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
return i;
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
const doubleQuote = '"';
|
|
1675
|
+
const singleQuote = "'";
|
|
1676
|
+
|
|
1677
|
+
/**
|
|
1678
|
+
* Keep reading xmlData until '<' is found outside the attribute value.
|
|
1679
|
+
* @param {string} xmlData
|
|
1680
|
+
* @param {number} i
|
|
1681
|
+
*/
|
|
1682
|
+
function readAttributeStr(xmlData, i) {
|
|
1683
|
+
let attrStr = '';
|
|
1684
|
+
let startChar = '';
|
|
1685
|
+
let tagClosed = false;
|
|
1686
|
+
for (; i < xmlData.length; i++) {
|
|
1687
|
+
if (xmlData[i] === doubleQuote || xmlData[i] === singleQuote) {
|
|
1688
|
+
if (startChar === '') {
|
|
1689
|
+
startChar = xmlData[i];
|
|
1690
|
+
} else if (startChar !== xmlData[i]) ; else {
|
|
1691
|
+
startChar = '';
|
|
1692
|
+
}
|
|
1693
|
+
} else if (xmlData[i] === '>') {
|
|
1694
|
+
if (startChar === '') {
|
|
1695
|
+
tagClosed = true;
|
|
1696
|
+
break;
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
attrStr += xmlData[i];
|
|
1700
|
+
}
|
|
1701
|
+
if (startChar !== '') {
|
|
1702
|
+
return false;
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
return {
|
|
1706
|
+
value: attrStr,
|
|
1707
|
+
index: i,
|
|
1708
|
+
tagClosed: tagClosed
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
/**
|
|
1713
|
+
* Select all the attributes whether valid or invalid.
|
|
1714
|
+
*/
|
|
1715
|
+
const validAttrStrRegxp = new RegExp('(\\s*)([^\\s=]+)(\\s*=)?(\\s*([\'"])(([\\s\\S])*?)\\5)?', 'g');
|
|
1716
|
+
|
|
1717
|
+
//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
|
|
1718
|
+
|
|
1719
|
+
function validateAttributeString(attrStr, options) {
|
|
1720
|
+
//console.log("start:"+attrStr+":end");
|
|
1721
|
+
|
|
1722
|
+
//if(attrStr.trim().length === 0) return true; //empty string
|
|
1723
|
+
|
|
1724
|
+
const matches = util$2.getAllMatches(attrStr, validAttrStrRegxp);
|
|
1725
|
+
const attrNames = {};
|
|
1726
|
+
|
|
1727
|
+
for (let i = 0; i < matches.length; i++) {
|
|
1728
|
+
if (matches[i][1].length === 0) {
|
|
1729
|
+
//nospace before attribute name: a="sd"b="saf"
|
|
1730
|
+
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' has no space in starting.", getPositionFromMatch(matches[i]))
|
|
1731
|
+
} else if (matches[i][3] !== undefined && matches[i][4] === undefined) {
|
|
1732
|
+
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' is without value.", getPositionFromMatch(matches[i]));
|
|
1733
|
+
} else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
|
|
1734
|
+
//independent attribute: ab
|
|
1735
|
+
return getErrorObject('InvalidAttr', "boolean attribute '"+matches[i][2]+"' is not allowed.", getPositionFromMatch(matches[i]));
|
|
1736
|
+
}
|
|
1737
|
+
/* else if(matches[i][6] === undefined){//attribute without value: ab=
|
|
1738
|
+
return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
|
|
1739
|
+
} */
|
|
1740
|
+
const attrName = matches[i][2];
|
|
1741
|
+
if (!validateAttrName(attrName)) {
|
|
1742
|
+
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is an invalid name.", getPositionFromMatch(matches[i]));
|
|
1743
|
+
}
|
|
1744
|
+
if (!attrNames.hasOwnProperty(attrName)) {
|
|
1745
|
+
//check for duplicate attribute.
|
|
1746
|
+
attrNames[attrName] = 1;
|
|
1747
|
+
} else {
|
|
1748
|
+
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is repeated.", getPositionFromMatch(matches[i]));
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
return true;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
function validateNumberAmpersand(xmlData, i) {
|
|
1756
|
+
let re = /\d/;
|
|
1757
|
+
if (xmlData[i] === 'x') {
|
|
1758
|
+
i++;
|
|
1759
|
+
re = /[\da-fA-F]/;
|
|
1760
|
+
}
|
|
1761
|
+
for (; i < xmlData.length; i++) {
|
|
1762
|
+
if (xmlData[i] === ';')
|
|
1763
|
+
return i;
|
|
1764
|
+
if (!xmlData[i].match(re))
|
|
1765
|
+
break;
|
|
1766
|
+
}
|
|
1767
|
+
return -1;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
function validateAmpersand(xmlData, i) {
|
|
1771
|
+
// https://www.w3.org/TR/xml/#dt-charref
|
|
1772
|
+
i++;
|
|
1773
|
+
if (xmlData[i] === ';')
|
|
1774
|
+
return -1;
|
|
1775
|
+
if (xmlData[i] === '#') {
|
|
1776
|
+
i++;
|
|
1777
|
+
return validateNumberAmpersand(xmlData, i);
|
|
1778
|
+
}
|
|
1779
|
+
let count = 0;
|
|
1780
|
+
for (; i < xmlData.length; i++, count++) {
|
|
1781
|
+
if (xmlData[i].match(/\w/) && count < 20)
|
|
1782
|
+
continue;
|
|
1783
|
+
if (xmlData[i] === ';')
|
|
1784
|
+
break;
|
|
1785
|
+
return -1;
|
|
1786
|
+
}
|
|
1787
|
+
return i;
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
function getErrorObject(code, message, lineNumber) {
|
|
1791
|
+
return {
|
|
1792
|
+
err: {
|
|
1793
|
+
code: code,
|
|
1794
|
+
msg: message,
|
|
1795
|
+
line: lineNumber.line || lineNumber,
|
|
1796
|
+
col: lineNumber.col,
|
|
1797
|
+
},
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
function validateAttrName(attrName) {
|
|
1802
|
+
return util$2.isName(attrName);
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
// const startsWithXML = /^xml/i;
|
|
1806
|
+
|
|
1807
|
+
function validateTagName(tagname) {
|
|
1808
|
+
return util$2.isName(tagname) /* && !tagname.match(startsWithXML) */;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
//this function returns the line number for the character at the given index
|
|
1812
|
+
function getLineNumberForPosition(xmlData, index) {
|
|
1813
|
+
const lines = xmlData.substring(0, index).split(/\r?\n/);
|
|
1814
|
+
return {
|
|
1815
|
+
line: lines.length,
|
|
1816
|
+
|
|
1817
|
+
// column number is last line's length + 1, because column numbering starts at 1:
|
|
1818
|
+
col: lines[lines.length - 1].length + 1
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
//this function returns the position of the first character of match within attrStr
|
|
1823
|
+
function getPositionFromMatch(match) {
|
|
1824
|
+
return match.startIndex + match[1].length;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
var OptionsBuilder = {};
|
|
1828
|
+
|
|
1829
|
+
const defaultOptions$1 = {
|
|
1830
|
+
preserveOrder: false,
|
|
1831
|
+
attributeNamePrefix: '@_',
|
|
1832
|
+
attributesGroupName: false,
|
|
1833
|
+
textNodeName: '#text',
|
|
1834
|
+
ignoreAttributes: true,
|
|
1835
|
+
removeNSPrefix: false, // remove NS from tag name or attribute name if true
|
|
1836
|
+
allowBooleanAttributes: false, //a tag can have attributes without any value
|
|
1837
|
+
//ignoreRootElement : false,
|
|
1838
|
+
parseTagValue: true,
|
|
1839
|
+
parseAttributeValue: false,
|
|
1840
|
+
trimValues: true, //Trim string values of tag and attributes
|
|
1841
|
+
cdataPropName: false,
|
|
1842
|
+
numberParseOptions: {
|
|
1843
|
+
hex: true,
|
|
1844
|
+
leadingZeros: true,
|
|
1845
|
+
eNotation: true
|
|
1846
|
+
},
|
|
1847
|
+
tagValueProcessor: function(tagName, val) {
|
|
1848
|
+
return val;
|
|
1849
|
+
},
|
|
1850
|
+
attributeValueProcessor: function(attrName, val) {
|
|
1851
|
+
return val;
|
|
1852
|
+
},
|
|
1853
|
+
stopNodes: [], //nested tags will not be parsed even for errors
|
|
1854
|
+
alwaysCreateTextNode: false,
|
|
1855
|
+
isArray: () => false,
|
|
1856
|
+
commentPropName: false,
|
|
1857
|
+
unpairedTags: [],
|
|
1858
|
+
processEntities: true,
|
|
1859
|
+
htmlEntities: false,
|
|
1860
|
+
ignoreDeclaration: false,
|
|
1861
|
+
ignorePiTags: false,
|
|
1862
|
+
transformTagName: false,
|
|
1863
|
+
transformAttributeName: false,
|
|
1864
|
+
updateTag: function(tagName, jPath, attrs){
|
|
1865
|
+
return tagName
|
|
1866
|
+
},
|
|
1867
|
+
// skipEmptyListItem: false
|
|
1868
|
+
};
|
|
1869
|
+
|
|
1870
|
+
const buildOptions$1 = function(options) {
|
|
1871
|
+
return Object.assign({}, defaultOptions$1, options);
|
|
1872
|
+
};
|
|
1873
|
+
|
|
1874
|
+
OptionsBuilder.buildOptions = buildOptions$1;
|
|
1875
|
+
OptionsBuilder.defaultOptions = defaultOptions$1;
|
|
1876
|
+
|
|
1877
|
+
class XmlNode{
|
|
1878
|
+
constructor(tagname) {
|
|
1879
|
+
this.tagname = tagname;
|
|
1880
|
+
this.child = []; //nested tags, text, cdata, comments in order
|
|
1881
|
+
this[":@"] = {}; //attributes map
|
|
1882
|
+
}
|
|
1883
|
+
add(key,val){
|
|
1884
|
+
// this.child.push( {name : key, val: val, isCdata: isCdata });
|
|
1885
|
+
if(key === "__proto__") key = "#__proto__";
|
|
1886
|
+
this.child.push( {[key]: val });
|
|
1887
|
+
}
|
|
1888
|
+
addChild(node) {
|
|
1889
|
+
if(node.tagname === "__proto__") node.tagname = "#__proto__";
|
|
1890
|
+
if(node[":@"] && Object.keys(node[":@"]).length > 0){
|
|
1891
|
+
this.child.push( { [node.tagname]: node.child, [":@"]: node[":@"] });
|
|
1892
|
+
}else {
|
|
1893
|
+
this.child.push( { [node.tagname]: node.child });
|
|
1894
|
+
}
|
|
1895
|
+
};
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
var xmlNode$1 = XmlNode;
|
|
1899
|
+
|
|
1900
|
+
const util$1 = util$3;
|
|
1901
|
+
|
|
1902
|
+
//TODO: handle comments
|
|
1903
|
+
function readDocType$1(xmlData, i){
|
|
1904
|
+
|
|
1905
|
+
const entities = {};
|
|
1906
|
+
if( xmlData[i + 3] === 'O' &&
|
|
1907
|
+
xmlData[i + 4] === 'C' &&
|
|
1908
|
+
xmlData[i + 5] === 'T' &&
|
|
1909
|
+
xmlData[i + 6] === 'Y' &&
|
|
1910
|
+
xmlData[i + 7] === 'P' &&
|
|
1911
|
+
xmlData[i + 8] === 'E')
|
|
1912
|
+
{
|
|
1913
|
+
i = i+9;
|
|
1914
|
+
let angleBracketsCount = 1;
|
|
1915
|
+
let hasBody = false, comment = false;
|
|
1916
|
+
let exp = "";
|
|
1917
|
+
for(;i<xmlData.length;i++){
|
|
1918
|
+
if (xmlData[i] === '<' && !comment) { //Determine the tag type
|
|
1919
|
+
if( hasBody && isEntity(xmlData, i)){
|
|
1920
|
+
i += 7;
|
|
1921
|
+
let entityName, val;
|
|
1922
|
+
[entityName, val,i] = readEntityExp(xmlData,i+1);
|
|
1923
|
+
if(val.indexOf("&") === -1) //Parameter entities are not supported
|
|
1924
|
+
entities[ validateEntityName(entityName) ] = {
|
|
1925
|
+
regx : RegExp( `&${entityName};`,"g"),
|
|
1926
|
+
val: val
|
|
1927
|
+
};
|
|
1928
|
+
}
|
|
1929
|
+
else if( hasBody && isElement(xmlData, i)) i += 8;//Not supported
|
|
1930
|
+
else if( hasBody && isAttlist(xmlData, i)) i += 8;//Not supported
|
|
1931
|
+
else if( hasBody && isNotation(xmlData, i)) i += 9;//Not supported
|
|
1932
|
+
else if( isComment) comment = true;
|
|
1933
|
+
else throw new Error("Invalid DOCTYPE");
|
|
1934
|
+
|
|
1935
|
+
angleBracketsCount++;
|
|
1936
|
+
exp = "";
|
|
1937
|
+
} else if (xmlData[i] === '>') { //Read tag content
|
|
1938
|
+
if(comment){
|
|
1939
|
+
if( xmlData[i - 1] === "-" && xmlData[i - 2] === "-"){
|
|
1940
|
+
comment = false;
|
|
1941
|
+
angleBracketsCount--;
|
|
1942
|
+
}
|
|
1943
|
+
}else {
|
|
1944
|
+
angleBracketsCount--;
|
|
1945
|
+
}
|
|
1946
|
+
if (angleBracketsCount === 0) {
|
|
1947
|
+
break;
|
|
1948
|
+
}
|
|
1949
|
+
}else if( xmlData[i] === '['){
|
|
1950
|
+
hasBody = true;
|
|
1951
|
+
}else {
|
|
1952
|
+
exp += xmlData[i];
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
if(angleBracketsCount !== 0){
|
|
1956
|
+
throw new Error(`Unclosed DOCTYPE`);
|
|
1957
|
+
}
|
|
1958
|
+
}else {
|
|
1959
|
+
throw new Error(`Invalid Tag instead of DOCTYPE`);
|
|
1960
|
+
}
|
|
1961
|
+
return {entities, i};
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
function readEntityExp(xmlData,i){
|
|
1965
|
+
//External entities are not supported
|
|
1966
|
+
// <!ENTITY ext SYSTEM "http://normal-website.com" >
|
|
1967
|
+
|
|
1968
|
+
//Parameter entities are not supported
|
|
1969
|
+
// <!ENTITY entityname "&anotherElement;">
|
|
1970
|
+
|
|
1971
|
+
//Internal entities are supported
|
|
1972
|
+
// <!ENTITY entityname "replacement text">
|
|
1973
|
+
|
|
1974
|
+
//read EntityName
|
|
1975
|
+
let entityName = "";
|
|
1976
|
+
for (; i < xmlData.length && (xmlData[i] !== "'" && xmlData[i] !== '"' ); i++) {
|
|
1977
|
+
// if(xmlData[i] === " ") continue;
|
|
1978
|
+
// else
|
|
1979
|
+
entityName += xmlData[i];
|
|
1980
|
+
}
|
|
1981
|
+
entityName = entityName.trim();
|
|
1982
|
+
if(entityName.indexOf(" ") !== -1) throw new Error("External entites are not supported");
|
|
1983
|
+
|
|
1984
|
+
//read Entity Value
|
|
1985
|
+
const startChar = xmlData[i++];
|
|
1986
|
+
let val = "";
|
|
1987
|
+
for (; i < xmlData.length && xmlData[i] !== startChar ; i++) {
|
|
1988
|
+
val += xmlData[i];
|
|
1989
|
+
}
|
|
1990
|
+
return [entityName, val, i];
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
function isComment(xmlData, i){
|
|
1994
|
+
if(xmlData[i+1] === '!' &&
|
|
1995
|
+
xmlData[i+2] === '-' &&
|
|
1996
|
+
xmlData[i+3] === '-') return true
|
|
1997
|
+
return false
|
|
1998
|
+
}
|
|
1999
|
+
function isEntity(xmlData, i){
|
|
2000
|
+
if(xmlData[i+1] === '!' &&
|
|
2001
|
+
xmlData[i+2] === 'E' &&
|
|
2002
|
+
xmlData[i+3] === 'N' &&
|
|
2003
|
+
xmlData[i+4] === 'T' &&
|
|
2004
|
+
xmlData[i+5] === 'I' &&
|
|
2005
|
+
xmlData[i+6] === 'T' &&
|
|
2006
|
+
xmlData[i+7] === 'Y') return true
|
|
2007
|
+
return false
|
|
2008
|
+
}
|
|
2009
|
+
function isElement(xmlData, i){
|
|
2010
|
+
if(xmlData[i+1] === '!' &&
|
|
2011
|
+
xmlData[i+2] === 'E' &&
|
|
2012
|
+
xmlData[i+3] === 'L' &&
|
|
2013
|
+
xmlData[i+4] === 'E' &&
|
|
2014
|
+
xmlData[i+5] === 'M' &&
|
|
2015
|
+
xmlData[i+6] === 'E' &&
|
|
2016
|
+
xmlData[i+7] === 'N' &&
|
|
2017
|
+
xmlData[i+8] === 'T') return true
|
|
2018
|
+
return false
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
function isAttlist(xmlData, i){
|
|
2022
|
+
if(xmlData[i+1] === '!' &&
|
|
2023
|
+
xmlData[i+2] === 'A' &&
|
|
2024
|
+
xmlData[i+3] === 'T' &&
|
|
2025
|
+
xmlData[i+4] === 'T' &&
|
|
2026
|
+
xmlData[i+5] === 'L' &&
|
|
2027
|
+
xmlData[i+6] === 'I' &&
|
|
2028
|
+
xmlData[i+7] === 'S' &&
|
|
2029
|
+
xmlData[i+8] === 'T') return true
|
|
2030
|
+
return false
|
|
2031
|
+
}
|
|
2032
|
+
function isNotation(xmlData, i){
|
|
2033
|
+
if(xmlData[i+1] === '!' &&
|
|
2034
|
+
xmlData[i+2] === 'N' &&
|
|
2035
|
+
xmlData[i+3] === 'O' &&
|
|
2036
|
+
xmlData[i+4] === 'T' &&
|
|
2037
|
+
xmlData[i+5] === 'A' &&
|
|
2038
|
+
xmlData[i+6] === 'T' &&
|
|
2039
|
+
xmlData[i+7] === 'I' &&
|
|
2040
|
+
xmlData[i+8] === 'O' &&
|
|
2041
|
+
xmlData[i+9] === 'N') return true
|
|
2042
|
+
return false
|
|
2043
|
+
}
|
|
2044
|
+
|
|
2045
|
+
function validateEntityName(name){
|
|
2046
|
+
if (util$1.isName(name))
|
|
2047
|
+
return name;
|
|
2048
|
+
else
|
|
2049
|
+
throw new Error(`Invalid entity name ${name}`);
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
var DocTypeReader = readDocType$1;
|
|
2053
|
+
|
|
2054
|
+
const hexRegex = /^[-+]?0x[a-fA-F0-9]+$/;
|
|
2055
|
+
const numRegex = /^([\-\+])?(0*)([0-9]*(\.[0-9]*)?)$/;
|
|
2056
|
+
// const octRegex = /^0x[a-z0-9]+/;
|
|
2057
|
+
// const binRegex = /0x[a-z0-9]+/;
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
const consider = {
|
|
2061
|
+
hex : true,
|
|
2062
|
+
// oct: false,
|
|
2063
|
+
leadingZeros: true,
|
|
2064
|
+
decimalPoint: "\.",
|
|
2065
|
+
eNotation: true,
|
|
2066
|
+
//skipLike: /regex/
|
|
2067
|
+
};
|
|
2068
|
+
|
|
2069
|
+
function toNumber$1(str, options = {}){
|
|
2070
|
+
options = Object.assign({}, consider, options );
|
|
2071
|
+
if(!str || typeof str !== "string" ) return str;
|
|
2072
|
+
|
|
2073
|
+
let trimmedStr = str.trim();
|
|
2074
|
+
|
|
2075
|
+
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
|
|
2076
|
+
else if(str==="0") return 0;
|
|
2077
|
+
else if (options.hex && hexRegex.test(trimmedStr)) {
|
|
2078
|
+
return parse_int(trimmedStr, 16);
|
|
2079
|
+
// }else if (options.oct && octRegex.test(str)) {
|
|
2080
|
+
// return Number.parseInt(val, 8);
|
|
2081
|
+
}else if (trimmedStr.search(/[eE]/)!== -1) { //eNotation
|
|
2082
|
+
const notation = trimmedStr.match(/^([-\+])?(0*)([0-9]*(\.[0-9]*)?[eE][-\+]?[0-9]+)$/);
|
|
2083
|
+
// +00.123 => [ , '+', '00', '.123', ..
|
|
2084
|
+
if(notation){
|
|
2085
|
+
// console.log(notation)
|
|
2086
|
+
if(options.leadingZeros){ //accept with leading zeros
|
|
2087
|
+
trimmedStr = (notation[1] || "") + notation[3];
|
|
2088
|
+
}else {
|
|
2089
|
+
if(notation[2] === "0" && notation[3][0]=== ".");else {
|
|
2090
|
+
return str;
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
return options.eNotation ? Number(trimmedStr) : str;
|
|
2094
|
+
}else {
|
|
2095
|
+
return str;
|
|
2096
|
+
}
|
|
2097
|
+
// }else if (options.parseBin && binRegex.test(str)) {
|
|
2098
|
+
// return Number.parseInt(val, 2);
|
|
2099
|
+
}else {
|
|
2100
|
+
//separate negative sign, leading zeros, and rest number
|
|
2101
|
+
const match = numRegex.exec(trimmedStr);
|
|
2102
|
+
// +00.123 => [ , '+', '00', '.123', ..
|
|
2103
|
+
if(match){
|
|
2104
|
+
const sign = match[1];
|
|
2105
|
+
const leadingZeros = match[2];
|
|
2106
|
+
let numTrimmedByZeros = trimZeros(match[3]); //complete num without leading zeros
|
|
2107
|
+
//trim ending zeros for floating number
|
|
2108
|
+
|
|
2109
|
+
if(!options.leadingZeros && leadingZeros.length > 0 && sign && trimmedStr[2] !== ".") return str; //-0123
|
|
2110
|
+
else if(!options.leadingZeros && leadingZeros.length > 0 && !sign && trimmedStr[1] !== ".") return str; //0123
|
|
2111
|
+
else if(options.leadingZeros && leadingZeros===str) return 0; //00
|
|
2112
|
+
|
|
2113
|
+
else {//no leading zeros or leading zeros are allowed
|
|
2114
|
+
const num = Number(trimmedStr);
|
|
2115
|
+
const numStr = "" + num;
|
|
2116
|
+
|
|
2117
|
+
if(numStr.search(/[eE]/) !== -1){ //given number is long and parsed to eNotation
|
|
2118
|
+
if(options.eNotation) return num;
|
|
2119
|
+
else return str;
|
|
2120
|
+
}else if(trimmedStr.indexOf(".") !== -1){ //floating number
|
|
2121
|
+
if(numStr === "0" && (numTrimmedByZeros === "") ) return num; //0.0
|
|
2122
|
+
else if(numStr === numTrimmedByZeros) return num; //0.456. 0.79000
|
|
2123
|
+
else if( sign && numStr === "-"+numTrimmedByZeros) return num;
|
|
2124
|
+
else return str;
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
if(leadingZeros){
|
|
2128
|
+
return (numTrimmedByZeros === numStr) || (sign+numTrimmedByZeros === numStr) ? num : str
|
|
2129
|
+
}else {
|
|
2130
|
+
return (trimmedStr === numStr) || (trimmedStr === sign+numStr) ? num : str
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
}else { //non-numeric string
|
|
2134
|
+
return str;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
/**
|
|
2140
|
+
*
|
|
2141
|
+
* @param {string} numStr without leading zeros
|
|
2142
|
+
* @returns
|
|
2143
|
+
*/
|
|
2144
|
+
function trimZeros(numStr){
|
|
2145
|
+
if(numStr && numStr.indexOf(".") !== -1){//float
|
|
2146
|
+
numStr = numStr.replace(/0+$/, ""); //remove ending zeros
|
|
2147
|
+
if(numStr === ".") numStr = "0";
|
|
2148
|
+
else if(numStr[0] === ".") numStr = "0"+numStr;
|
|
2149
|
+
else if(numStr[numStr.length-1] === ".") numStr = numStr.substr(0,numStr.length-1);
|
|
2150
|
+
return numStr;
|
|
2151
|
+
}
|
|
2152
|
+
return numStr;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
function parse_int(numStr, base){
|
|
2156
|
+
//polyfill
|
|
2157
|
+
if(parseInt) return parseInt(numStr, base);
|
|
2158
|
+
else if(Number.parseInt) return Number.parseInt(numStr, base);
|
|
2159
|
+
else if(window && window.parseInt) return window.parseInt(numStr, base);
|
|
2160
|
+
else throw new Error("parseInt, Number.parseInt, window.parseInt are not supported")
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
var strnum = toNumber$1;
|
|
2164
|
+
|
|
2165
|
+
function getIgnoreAttributesFn$2(ignoreAttributes) {
|
|
2166
|
+
if (typeof ignoreAttributes === 'function') {
|
|
2167
|
+
return ignoreAttributes
|
|
2168
|
+
}
|
|
2169
|
+
if (Array.isArray(ignoreAttributes)) {
|
|
2170
|
+
return (attrName) => {
|
|
2171
|
+
for (const pattern of ignoreAttributes) {
|
|
2172
|
+
if (typeof pattern === 'string' && attrName === pattern) {
|
|
2173
|
+
return true
|
|
2174
|
+
}
|
|
2175
|
+
if (pattern instanceof RegExp && pattern.test(attrName)) {
|
|
2176
|
+
return true
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
}
|
|
2181
|
+
return () => false
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
var ignoreAttributes = getIgnoreAttributesFn$2;
|
|
2185
|
+
|
|
2186
|
+
///@ts-check
|
|
2187
|
+
|
|
2188
|
+
const util = util$3;
|
|
2189
|
+
const xmlNode = xmlNode$1;
|
|
2190
|
+
const readDocType = DocTypeReader;
|
|
2191
|
+
const toNumber = strnum;
|
|
2192
|
+
const getIgnoreAttributesFn$1 = ignoreAttributes;
|
|
2193
|
+
|
|
2194
|
+
// const regx =
|
|
2195
|
+
// '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
|
|
2196
|
+
// .replace(/NAME/g, util.nameRegexp);
|
|
2197
|
+
|
|
2198
|
+
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
|
|
2199
|
+
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
|
|
2200
|
+
|
|
2201
|
+
let OrderedObjParser$1 = class OrderedObjParser{
|
|
2202
|
+
constructor(options){
|
|
2203
|
+
this.options = options;
|
|
2204
|
+
this.currentNode = null;
|
|
2205
|
+
this.tagsNodeStack = [];
|
|
2206
|
+
this.docTypeEntities = {};
|
|
2207
|
+
this.lastEntities = {
|
|
2208
|
+
"apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
|
|
2209
|
+
"gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
|
|
2210
|
+
"lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
|
|
2211
|
+
"quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
|
|
2212
|
+
};
|
|
2213
|
+
this.ampEntity = { regex: /&(amp|#38|#x26);/g, val : "&"};
|
|
2214
|
+
this.htmlEntities = {
|
|
2215
|
+
"space": { regex: /&(nbsp|#160);/g, val: " " },
|
|
2216
|
+
// "lt" : { regex: /&(lt|#60);/g, val: "<" },
|
|
2217
|
+
// "gt" : { regex: /&(gt|#62);/g, val: ">" },
|
|
2218
|
+
// "amp" : { regex: /&(amp|#38);/g, val: "&" },
|
|
2219
|
+
// "quot" : { regex: /&(quot|#34);/g, val: "\"" },
|
|
2220
|
+
// "apos" : { regex: /&(apos|#39);/g, val: "'" },
|
|
2221
|
+
"cent" : { regex: /&(cent|#162);/g, val: "¢" },
|
|
2222
|
+
"pound" : { regex: /&(pound|#163);/g, val: "£" },
|
|
2223
|
+
"yen" : { regex: /&(yen|#165);/g, val: "¥" },
|
|
2224
|
+
"euro" : { regex: /&(euro|#8364);/g, val: "€" },
|
|
2225
|
+
"copyright" : { regex: /&(copy|#169);/g, val: "©" },
|
|
2226
|
+
"reg" : { regex: /&(reg|#174);/g, val: "®" },
|
|
2227
|
+
"inr" : { regex: /&(inr|#8377);/g, val: "₹" },
|
|
2228
|
+
"num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
|
|
2229
|
+
"num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
|
|
2230
|
+
};
|
|
2231
|
+
this.addExternalEntities = addExternalEntities;
|
|
2232
|
+
this.parseXml = parseXml;
|
|
2233
|
+
this.parseTextData = parseTextData;
|
|
2234
|
+
this.resolveNameSpace = resolveNameSpace;
|
|
2235
|
+
this.buildAttributesMap = buildAttributesMap;
|
|
2236
|
+
this.isItStopNode = isItStopNode;
|
|
2237
|
+
this.replaceEntitiesValue = replaceEntitiesValue$1;
|
|
2238
|
+
this.readStopNodeData = readStopNodeData;
|
|
2239
|
+
this.saveTextToParentTag = saveTextToParentTag;
|
|
2240
|
+
this.addChild = addChild;
|
|
2241
|
+
this.ignoreAttributesFn = getIgnoreAttributesFn$1(this.options.ignoreAttributes);
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
};
|
|
2245
|
+
|
|
2246
|
+
function addExternalEntities(externalEntities){
|
|
2247
|
+
const entKeys = Object.keys(externalEntities);
|
|
2248
|
+
for (let i = 0; i < entKeys.length; i++) {
|
|
2249
|
+
const ent = entKeys[i];
|
|
2250
|
+
this.lastEntities[ent] = {
|
|
2251
|
+
regex: new RegExp("&"+ent+";","g"),
|
|
2252
|
+
val : externalEntities[ent]
|
|
2253
|
+
};
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
/**
|
|
2258
|
+
* @param {string} val
|
|
2259
|
+
* @param {string} tagName
|
|
2260
|
+
* @param {string} jPath
|
|
2261
|
+
* @param {boolean} dontTrim
|
|
2262
|
+
* @param {boolean} hasAttributes
|
|
2263
|
+
* @param {boolean} isLeafNode
|
|
2264
|
+
* @param {boolean} escapeEntities
|
|
2265
|
+
*/
|
|
2266
|
+
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
|
|
2267
|
+
if (val !== undefined) {
|
|
2268
|
+
if (this.options.trimValues && !dontTrim) {
|
|
2269
|
+
val = val.trim();
|
|
2270
|
+
}
|
|
2271
|
+
if(val.length > 0){
|
|
2272
|
+
if(!escapeEntities) val = this.replaceEntitiesValue(val);
|
|
2273
|
+
|
|
2274
|
+
const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
|
|
2275
|
+
if(newval === null || newval === undefined){
|
|
2276
|
+
//don't parse
|
|
2277
|
+
return val;
|
|
2278
|
+
}else if(typeof newval !== typeof val || newval !== val){
|
|
2279
|
+
//overwrite
|
|
2280
|
+
return newval;
|
|
2281
|
+
}else if(this.options.trimValues){
|
|
2282
|
+
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
2283
|
+
}else {
|
|
2284
|
+
const trimmedVal = val.trim();
|
|
2285
|
+
if(trimmedVal === val){
|
|
2286
|
+
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
2287
|
+
}else {
|
|
2288
|
+
return val;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
function resolveNameSpace(tagname) {
|
|
2296
|
+
if (this.options.removeNSPrefix) {
|
|
2297
|
+
const tags = tagname.split(':');
|
|
2298
|
+
const prefix = tagname.charAt(0) === '/' ? '/' : '';
|
|
2299
|
+
if (tags[0] === 'xmlns') {
|
|
2300
|
+
return '';
|
|
2301
|
+
}
|
|
2302
|
+
if (tags.length === 2) {
|
|
2303
|
+
tagname = prefix + tags[1];
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
return tagname;
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
//TODO: change regex to capture NS
|
|
2310
|
+
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
|
|
2311
|
+
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
|
|
2312
|
+
|
|
2313
|
+
function buildAttributesMap(attrStr, jPath, tagName) {
|
|
2314
|
+
if (this.options.ignoreAttributes !== true && typeof attrStr === 'string') {
|
|
2315
|
+
// attrStr = attrStr.replace(/\r?\n/g, ' ');
|
|
2316
|
+
//attrStr = attrStr || attrStr.trim();
|
|
2317
|
+
|
|
2318
|
+
const matches = util.getAllMatches(attrStr, attrsRegx);
|
|
2319
|
+
const len = matches.length; //don't make it inline
|
|
2320
|
+
const attrs = {};
|
|
2321
|
+
for (let i = 0; i < len; i++) {
|
|
2322
|
+
const attrName = this.resolveNameSpace(matches[i][1]);
|
|
2323
|
+
if (this.ignoreAttributesFn(attrName, jPath)) {
|
|
2324
|
+
continue
|
|
2325
|
+
}
|
|
2326
|
+
let oldVal = matches[i][4];
|
|
2327
|
+
let aName = this.options.attributeNamePrefix + attrName;
|
|
2328
|
+
if (attrName.length) {
|
|
2329
|
+
if (this.options.transformAttributeName) {
|
|
2330
|
+
aName = this.options.transformAttributeName(aName);
|
|
2331
|
+
}
|
|
2332
|
+
if(aName === "__proto__") aName = "#__proto__";
|
|
2333
|
+
if (oldVal !== undefined) {
|
|
2334
|
+
if (this.options.trimValues) {
|
|
2335
|
+
oldVal = oldVal.trim();
|
|
2336
|
+
}
|
|
2337
|
+
oldVal = this.replaceEntitiesValue(oldVal);
|
|
2338
|
+
const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPath);
|
|
2339
|
+
if(newVal === null || newVal === undefined){
|
|
2340
|
+
//don't parse
|
|
2341
|
+
attrs[aName] = oldVal;
|
|
2342
|
+
}else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
|
|
2343
|
+
//overwrite
|
|
2344
|
+
attrs[aName] = newVal;
|
|
2345
|
+
}else {
|
|
2346
|
+
//parse
|
|
2347
|
+
attrs[aName] = parseValue(
|
|
2348
|
+
oldVal,
|
|
2349
|
+
this.options.parseAttributeValue,
|
|
2350
|
+
this.options.numberParseOptions
|
|
2351
|
+
);
|
|
2352
|
+
}
|
|
2353
|
+
} else if (this.options.allowBooleanAttributes) {
|
|
2354
|
+
attrs[aName] = true;
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
if (!Object.keys(attrs).length) {
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
if (this.options.attributesGroupName) {
|
|
2362
|
+
const attrCollection = {};
|
|
2363
|
+
attrCollection[this.options.attributesGroupName] = attrs;
|
|
2364
|
+
return attrCollection;
|
|
2365
|
+
}
|
|
2366
|
+
return attrs
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
|
|
2370
|
+
const parseXml = function(xmlData) {
|
|
2371
|
+
xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
|
|
2372
|
+
const xmlObj = new xmlNode('!xml');
|
|
2373
|
+
let currentNode = xmlObj;
|
|
2374
|
+
let textData = "";
|
|
2375
|
+
let jPath = "";
|
|
2376
|
+
for(let i=0; i< xmlData.length; i++){//for each char in XML data
|
|
2377
|
+
const ch = xmlData[i];
|
|
2378
|
+
if(ch === '<'){
|
|
2379
|
+
// const nextIndex = i+1;
|
|
2380
|
+
// const _2ndChar = xmlData[nextIndex];
|
|
2381
|
+
if( xmlData[i+1] === '/') {//Closing Tag
|
|
2382
|
+
const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.");
|
|
2383
|
+
let tagName = xmlData.substring(i+2,closeIndex).trim();
|
|
2384
|
+
|
|
2385
|
+
if(this.options.removeNSPrefix){
|
|
2386
|
+
const colonIndex = tagName.indexOf(":");
|
|
2387
|
+
if(colonIndex !== -1){
|
|
2388
|
+
tagName = tagName.substr(colonIndex+1);
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
if(this.options.transformTagName) {
|
|
2393
|
+
tagName = this.options.transformTagName(tagName);
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
if(currentNode){
|
|
2397
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
//check if last tag of nested tag was unpaired tag
|
|
2401
|
+
const lastTagName = jPath.substring(jPath.lastIndexOf(".")+1);
|
|
2402
|
+
if(tagName && this.options.unpairedTags.indexOf(tagName) !== -1 ){
|
|
2403
|
+
throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
|
|
2404
|
+
}
|
|
2405
|
+
let propIndex = 0;
|
|
2406
|
+
if(lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1 ){
|
|
2407
|
+
propIndex = jPath.lastIndexOf('.', jPath.lastIndexOf('.')-1);
|
|
2408
|
+
this.tagsNodeStack.pop();
|
|
2409
|
+
}else {
|
|
2410
|
+
propIndex = jPath.lastIndexOf(".");
|
|
2411
|
+
}
|
|
2412
|
+
jPath = jPath.substring(0, propIndex);
|
|
2413
|
+
|
|
2414
|
+
currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
|
|
2415
|
+
textData = "";
|
|
2416
|
+
i = closeIndex;
|
|
2417
|
+
} else if( xmlData[i+1] === '?') {
|
|
2418
|
+
|
|
2419
|
+
let tagData = readTagExp(xmlData,i, false, "?>");
|
|
2420
|
+
if(!tagData) throw new Error("Pi Tag is not closed.");
|
|
2421
|
+
|
|
2422
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
2423
|
+
if( (this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags);else {
|
|
2424
|
+
|
|
2425
|
+
const childNode = new xmlNode(tagData.tagName);
|
|
2426
|
+
childNode.add(this.options.textNodeName, "");
|
|
2427
|
+
|
|
2428
|
+
if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){
|
|
2429
|
+
childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath, tagData.tagName);
|
|
2430
|
+
}
|
|
2431
|
+
this.addChild(currentNode, childNode, jPath);
|
|
2432
|
+
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
i = tagData.closeIndex + 1;
|
|
2437
|
+
} else if(xmlData.substr(i + 1, 3) === '!--') {
|
|
2438
|
+
const endIndex = findClosingIndex(xmlData, "-->", i+4, "Comment is not closed.");
|
|
2439
|
+
if(this.options.commentPropName){
|
|
2440
|
+
const comment = xmlData.substring(i + 4, endIndex - 2);
|
|
2441
|
+
|
|
2442
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
2443
|
+
|
|
2444
|
+
currentNode.add(this.options.commentPropName, [ { [this.options.textNodeName] : comment } ]);
|
|
2445
|
+
}
|
|
2446
|
+
i = endIndex;
|
|
2447
|
+
} else if( xmlData.substr(i + 1, 2) === '!D') {
|
|
2448
|
+
const result = readDocType(xmlData, i);
|
|
2449
|
+
this.docTypeEntities = result.entities;
|
|
2450
|
+
i = result.i;
|
|
2451
|
+
}else if(xmlData.substr(i + 1, 2) === '![') {
|
|
2452
|
+
const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
|
|
2453
|
+
const tagExp = xmlData.substring(i + 9,closeIndex);
|
|
2454
|
+
|
|
2455
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
2456
|
+
|
|
2457
|
+
let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true, true);
|
|
2458
|
+
if(val == undefined) val = "";
|
|
2459
|
+
|
|
2460
|
+
//cdata should be set even if it is 0 length string
|
|
2461
|
+
if(this.options.cdataPropName){
|
|
2462
|
+
currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]);
|
|
2463
|
+
}else {
|
|
2464
|
+
currentNode.add(this.options.textNodeName, val);
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
i = closeIndex + 2;
|
|
2468
|
+
}else {//Opening tag
|
|
2469
|
+
let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
|
|
2470
|
+
let tagName= result.tagName;
|
|
2471
|
+
const rawTagName = result.rawTagName;
|
|
2472
|
+
let tagExp = result.tagExp;
|
|
2473
|
+
let attrExpPresent = result.attrExpPresent;
|
|
2474
|
+
let closeIndex = result.closeIndex;
|
|
2475
|
+
|
|
2476
|
+
if (this.options.transformTagName) {
|
|
2477
|
+
tagName = this.options.transformTagName(tagName);
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2480
|
+
//save text as child node
|
|
2481
|
+
if (currentNode && textData) {
|
|
2482
|
+
if(currentNode.tagname !== '!xml'){
|
|
2483
|
+
//when nested tag is found
|
|
2484
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath, false);
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
|
|
2488
|
+
//check if last tag was unpaired tag
|
|
2489
|
+
const lastTag = currentNode;
|
|
2490
|
+
if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1 ){
|
|
2491
|
+
currentNode = this.tagsNodeStack.pop();
|
|
2492
|
+
jPath = jPath.substring(0, jPath.lastIndexOf("."));
|
|
2493
|
+
}
|
|
2494
|
+
if(tagName !== xmlObj.tagname){
|
|
2495
|
+
jPath += jPath ? "." + tagName : tagName;
|
|
2496
|
+
}
|
|
2497
|
+
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) {
|
|
2498
|
+
let tagContent = "";
|
|
2499
|
+
//self-closing tag
|
|
2500
|
+
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
|
2501
|
+
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
|
|
2502
|
+
tagName = tagName.substr(0, tagName.length - 1);
|
|
2503
|
+
jPath = jPath.substr(0, jPath.length - 1);
|
|
2504
|
+
tagExp = tagName;
|
|
2505
|
+
}else {
|
|
2506
|
+
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
2507
|
+
}
|
|
2508
|
+
i = result.closeIndex;
|
|
2509
|
+
}
|
|
2510
|
+
//unpaired tag
|
|
2511
|
+
else if(this.options.unpairedTags.indexOf(tagName) !== -1){
|
|
2512
|
+
|
|
2513
|
+
i = result.closeIndex;
|
|
2514
|
+
}
|
|
2515
|
+
//normal tag
|
|
2516
|
+
else {
|
|
2517
|
+
//read until closing tag is found
|
|
2518
|
+
const result = this.readStopNodeData(xmlData, rawTagName, closeIndex + 1);
|
|
2519
|
+
if(!result) throw new Error(`Unexpected end of ${rawTagName}`);
|
|
2520
|
+
i = result.i;
|
|
2521
|
+
tagContent = result.tagContent;
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
const childNode = new xmlNode(tagName);
|
|
2525
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
2526
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
2527
|
+
}
|
|
2528
|
+
if(tagContent) {
|
|
2529
|
+
tagContent = this.parseTextData(tagContent, tagName, jPath, true, attrExpPresent, true, true);
|
|
2530
|
+
}
|
|
2531
|
+
|
|
2532
|
+
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
|
2533
|
+
childNode.add(this.options.textNodeName, tagContent);
|
|
2534
|
+
|
|
2535
|
+
this.addChild(currentNode, childNode, jPath);
|
|
2536
|
+
}else {
|
|
2537
|
+
//selfClosing tag
|
|
2538
|
+
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
|
2539
|
+
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
|
|
2540
|
+
tagName = tagName.substr(0, tagName.length - 1);
|
|
2541
|
+
jPath = jPath.substr(0, jPath.length - 1);
|
|
2542
|
+
tagExp = tagName;
|
|
2543
|
+
}else {
|
|
2544
|
+
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
if(this.options.transformTagName) {
|
|
2548
|
+
tagName = this.options.transformTagName(tagName);
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
const childNode = new xmlNode(tagName);
|
|
2552
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
2553
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
2554
|
+
}
|
|
2555
|
+
this.addChild(currentNode, childNode, jPath);
|
|
2556
|
+
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
|
2557
|
+
}
|
|
2558
|
+
//opening tag
|
|
2559
|
+
else {
|
|
2560
|
+
const childNode = new xmlNode( tagName);
|
|
2561
|
+
this.tagsNodeStack.push(currentNode);
|
|
2562
|
+
|
|
2563
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
2564
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
2565
|
+
}
|
|
2566
|
+
this.addChild(currentNode, childNode, jPath);
|
|
2567
|
+
currentNode = childNode;
|
|
2568
|
+
}
|
|
2569
|
+
textData = "";
|
|
2570
|
+
i = closeIndex;
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
}else {
|
|
2574
|
+
textData += xmlData[i];
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
return xmlObj.child;
|
|
2578
|
+
};
|
|
2579
|
+
|
|
2580
|
+
function addChild(currentNode, childNode, jPath){
|
|
2581
|
+
const result = this.options.updateTag(childNode.tagname, jPath, childNode[":@"]);
|
|
2582
|
+
if(result === false);else if(typeof result === "string"){
|
|
2583
|
+
childNode.tagname = result;
|
|
2584
|
+
currentNode.addChild(childNode);
|
|
2585
|
+
}else {
|
|
2586
|
+
currentNode.addChild(childNode);
|
|
2587
|
+
}
|
|
2588
|
+
}
|
|
2589
|
+
|
|
2590
|
+
const replaceEntitiesValue$1 = function(val){
|
|
2591
|
+
|
|
2592
|
+
if(this.options.processEntities){
|
|
2593
|
+
for(let entityName in this.docTypeEntities){
|
|
2594
|
+
const entity = this.docTypeEntities[entityName];
|
|
2595
|
+
val = val.replace( entity.regx, entity.val);
|
|
2596
|
+
}
|
|
2597
|
+
for(let entityName in this.lastEntities){
|
|
2598
|
+
const entity = this.lastEntities[entityName];
|
|
2599
|
+
val = val.replace( entity.regex, entity.val);
|
|
2600
|
+
}
|
|
2601
|
+
if(this.options.htmlEntities){
|
|
2602
|
+
for(let entityName in this.htmlEntities){
|
|
2603
|
+
const entity = this.htmlEntities[entityName];
|
|
2604
|
+
val = val.replace( entity.regex, entity.val);
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
val = val.replace( this.ampEntity.regex, this.ampEntity.val);
|
|
2608
|
+
}
|
|
2609
|
+
return val;
|
|
2610
|
+
};
|
|
2611
|
+
function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
|
|
2612
|
+
if (textData) { //store previously collected data as textNode
|
|
2613
|
+
if(isLeafNode === undefined) isLeafNode = currentNode.child.length === 0;
|
|
2614
|
+
|
|
2615
|
+
textData = this.parseTextData(textData,
|
|
2616
|
+
currentNode.tagname,
|
|
2617
|
+
jPath,
|
|
2618
|
+
false,
|
|
2619
|
+
currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false,
|
|
2620
|
+
isLeafNode);
|
|
2621
|
+
|
|
2622
|
+
if (textData !== undefined && textData !== "")
|
|
2623
|
+
currentNode.add(this.options.textNodeName, textData);
|
|
2624
|
+
textData = "";
|
|
2625
|
+
}
|
|
2626
|
+
return textData;
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
//TODO: use jPath to simplify the logic
|
|
2630
|
+
/**
|
|
2631
|
+
*
|
|
2632
|
+
* @param {string[]} stopNodes
|
|
2633
|
+
* @param {string} jPath
|
|
2634
|
+
* @param {string} currentTagName
|
|
2635
|
+
*/
|
|
2636
|
+
function isItStopNode(stopNodes, jPath, currentTagName){
|
|
2637
|
+
const allNodesExp = "*." + currentTagName;
|
|
2638
|
+
for (const stopNodePath in stopNodes) {
|
|
2639
|
+
const stopNodeExp = stopNodes[stopNodePath];
|
|
2640
|
+
if( allNodesExp === stopNodeExp || jPath === stopNodeExp ) return true;
|
|
2641
|
+
}
|
|
2642
|
+
return false;
|
|
2643
|
+
}
|
|
2644
|
+
|
|
2645
|
+
/**
|
|
2646
|
+
* Returns the tag Expression and where it is ending handling single-double quotes situation
|
|
2647
|
+
* @param {string} xmlData
|
|
2648
|
+
* @param {number} i starting index
|
|
2649
|
+
* @returns
|
|
2650
|
+
*/
|
|
2651
|
+
function tagExpWithClosingIndex(xmlData, i, closingChar = ">"){
|
|
2652
|
+
let attrBoundary;
|
|
2653
|
+
let tagExp = "";
|
|
2654
|
+
for (let index = i; index < xmlData.length; index++) {
|
|
2655
|
+
let ch = xmlData[index];
|
|
2656
|
+
if (attrBoundary) {
|
|
2657
|
+
if (ch === attrBoundary) attrBoundary = "";//reset
|
|
2658
|
+
} else if (ch === '"' || ch === "'") {
|
|
2659
|
+
attrBoundary = ch;
|
|
2660
|
+
} else if (ch === closingChar[0]) {
|
|
2661
|
+
if(closingChar[1]){
|
|
2662
|
+
if(xmlData[index + 1] === closingChar[1]){
|
|
2663
|
+
return {
|
|
2664
|
+
data: tagExp,
|
|
2665
|
+
index: index
|
|
2666
|
+
}
|
|
2667
|
+
}
|
|
2668
|
+
}else {
|
|
2669
|
+
return {
|
|
2670
|
+
data: tagExp,
|
|
2671
|
+
index: index
|
|
2672
|
+
}
|
|
2673
|
+
}
|
|
2674
|
+
} else if (ch === '\t') {
|
|
2675
|
+
ch = " ";
|
|
2676
|
+
}
|
|
2677
|
+
tagExp += ch;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
function findClosingIndex(xmlData, str, i, errMsg){
|
|
2682
|
+
const closingIndex = xmlData.indexOf(str, i);
|
|
2683
|
+
if(closingIndex === -1){
|
|
2684
|
+
throw new Error(errMsg)
|
|
2685
|
+
}else {
|
|
2686
|
+
return closingIndex + str.length - 1;
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
|
|
2690
|
+
function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
|
|
2691
|
+
const result = tagExpWithClosingIndex(xmlData, i+1, closingChar);
|
|
2692
|
+
if(!result) return;
|
|
2693
|
+
let tagExp = result.data;
|
|
2694
|
+
const closeIndex = result.index;
|
|
2695
|
+
const separatorIndex = tagExp.search(/\s/);
|
|
2696
|
+
let tagName = tagExp;
|
|
2697
|
+
let attrExpPresent = true;
|
|
2698
|
+
if(separatorIndex !== -1){//separate tag name and attributes expression
|
|
2699
|
+
tagName = tagExp.substring(0, separatorIndex);
|
|
2700
|
+
tagExp = tagExp.substring(separatorIndex + 1).trimStart();
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
const rawTagName = tagName;
|
|
2704
|
+
if(removeNSPrefix){
|
|
2705
|
+
const colonIndex = tagName.indexOf(":");
|
|
2706
|
+
if(colonIndex !== -1){
|
|
2707
|
+
tagName = tagName.substr(colonIndex+1);
|
|
2708
|
+
attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
|
|
2712
|
+
return {
|
|
2713
|
+
tagName: tagName,
|
|
2714
|
+
tagExp: tagExp,
|
|
2715
|
+
closeIndex: closeIndex,
|
|
2716
|
+
attrExpPresent: attrExpPresent,
|
|
2717
|
+
rawTagName: rawTagName,
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
/**
|
|
2721
|
+
* find paired tag for a stop node
|
|
2722
|
+
* @param {string} xmlData
|
|
2723
|
+
* @param {string} tagName
|
|
2724
|
+
* @param {number} i
|
|
2725
|
+
*/
|
|
2726
|
+
function readStopNodeData(xmlData, tagName, i){
|
|
2727
|
+
const startIndex = i;
|
|
2728
|
+
// Starting at 1 since we already have an open tag
|
|
2729
|
+
let openTagCount = 1;
|
|
2730
|
+
|
|
2731
|
+
for (; i < xmlData.length; i++) {
|
|
2732
|
+
if( xmlData[i] === "<"){
|
|
2733
|
+
if (xmlData[i+1] === "/") {//close tag
|
|
2734
|
+
const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
|
|
2735
|
+
let closeTagName = xmlData.substring(i+2,closeIndex).trim();
|
|
2736
|
+
if(closeTagName === tagName){
|
|
2737
|
+
openTagCount--;
|
|
2738
|
+
if (openTagCount === 0) {
|
|
2739
|
+
return {
|
|
2740
|
+
tagContent: xmlData.substring(startIndex, i),
|
|
2741
|
+
i : closeIndex
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
i=closeIndex;
|
|
2746
|
+
} else if(xmlData[i+1] === '?') {
|
|
2747
|
+
const closeIndex = findClosingIndex(xmlData, "?>", i+1, "StopNode is not closed.");
|
|
2748
|
+
i=closeIndex;
|
|
2749
|
+
} else if(xmlData.substr(i + 1, 3) === '!--') {
|
|
2750
|
+
const closeIndex = findClosingIndex(xmlData, "-->", i+3, "StopNode is not closed.");
|
|
2751
|
+
i=closeIndex;
|
|
2752
|
+
} else if(xmlData.substr(i + 1, 2) === '![') {
|
|
2753
|
+
const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
|
|
2754
|
+
i=closeIndex;
|
|
2755
|
+
} else {
|
|
2756
|
+
const tagData = readTagExp(xmlData, i, '>');
|
|
2757
|
+
|
|
2758
|
+
if (tagData) {
|
|
2759
|
+
const openTagName = tagData && tagData.tagName;
|
|
2760
|
+
if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
|
|
2761
|
+
openTagCount++;
|
|
2762
|
+
}
|
|
2763
|
+
i=tagData.closeIndex;
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
}//end for loop
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
function parseValue(val, shouldParse, options) {
|
|
2771
|
+
if (shouldParse && typeof val === 'string') {
|
|
2772
|
+
//console.log(options)
|
|
2773
|
+
const newval = val.trim();
|
|
2774
|
+
if(newval === 'true' ) return true;
|
|
2775
|
+
else if(newval === 'false' ) return false;
|
|
2776
|
+
else return toNumber(val, options);
|
|
2777
|
+
} else {
|
|
2778
|
+
if (util.isExist(val)) {
|
|
2779
|
+
return val;
|
|
2780
|
+
} else {
|
|
2781
|
+
return '';
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
|
|
2787
|
+
var OrderedObjParser_1 = OrderedObjParser$1;
|
|
2788
|
+
|
|
2789
|
+
var node2json = {};
|
|
2790
|
+
|
|
2791
|
+
/**
|
|
2792
|
+
*
|
|
2793
|
+
* @param {array} node
|
|
2794
|
+
* @param {any} options
|
|
2795
|
+
* @returns
|
|
2796
|
+
*/
|
|
2797
|
+
function prettify$1(node, options){
|
|
2798
|
+
return compress( node, options);
|
|
2799
|
+
}
|
|
2800
|
+
|
|
2801
|
+
/**
|
|
2802
|
+
*
|
|
2803
|
+
* @param {array} arr
|
|
2804
|
+
* @param {object} options
|
|
2805
|
+
* @param {string} jPath
|
|
2806
|
+
* @returns object
|
|
2807
|
+
*/
|
|
2808
|
+
function compress(arr, options, jPath){
|
|
2809
|
+
let text;
|
|
2810
|
+
const compressedObj = {};
|
|
2811
|
+
for (let i = 0; i < arr.length; i++) {
|
|
2812
|
+
const tagObj = arr[i];
|
|
2813
|
+
const property = propName$1(tagObj);
|
|
2814
|
+
let newJpath = "";
|
|
2815
|
+
if(jPath === undefined) newJpath = property;
|
|
2816
|
+
else newJpath = jPath + "." + property;
|
|
2817
|
+
|
|
2818
|
+
if(property === options.textNodeName){
|
|
2819
|
+
if(text === undefined) text = tagObj[property];
|
|
2820
|
+
else text += "" + tagObj[property];
|
|
2821
|
+
}else if(property === undefined){
|
|
2822
|
+
continue;
|
|
2823
|
+
}else if(tagObj[property]){
|
|
2824
|
+
|
|
2825
|
+
let val = compress(tagObj[property], options, newJpath);
|
|
2826
|
+
const isLeaf = isLeafTag(val, options);
|
|
2827
|
+
|
|
2828
|
+
if(tagObj[":@"]){
|
|
2829
|
+
assignAttributes( val, tagObj[":@"], newJpath, options);
|
|
2830
|
+
}else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
|
|
2831
|
+
val = val[options.textNodeName];
|
|
2832
|
+
}else if(Object.keys(val).length === 0){
|
|
2833
|
+
if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
|
|
2834
|
+
else val = "";
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2837
|
+
if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
|
|
2838
|
+
if(!Array.isArray(compressedObj[property])) {
|
|
2839
|
+
compressedObj[property] = [ compressedObj[property] ];
|
|
2840
|
+
}
|
|
2841
|
+
compressedObj[property].push(val);
|
|
2842
|
+
}else {
|
|
2843
|
+
//TODO: if a node is not an array, then check if it should be an array
|
|
2844
|
+
//also determine if it is a leaf node
|
|
2845
|
+
if (options.isArray(property, newJpath, isLeaf )) {
|
|
2846
|
+
compressedObj[property] = [val];
|
|
2847
|
+
}else {
|
|
2848
|
+
compressedObj[property] = val;
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
}
|
|
2854
|
+
// if(text && text.length > 0) compressedObj[options.textNodeName] = text;
|
|
2855
|
+
if(typeof text === "string"){
|
|
2856
|
+
if(text.length > 0) compressedObj[options.textNodeName] = text;
|
|
2857
|
+
}else if(text !== undefined) compressedObj[options.textNodeName] = text;
|
|
2858
|
+
return compressedObj;
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2861
|
+
function propName$1(obj){
|
|
2862
|
+
const keys = Object.keys(obj);
|
|
2863
|
+
for (let i = 0; i < keys.length; i++) {
|
|
2864
|
+
const key = keys[i];
|
|
2865
|
+
if(key !== ":@") return key;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
function assignAttributes(obj, attrMap, jpath, options){
|
|
2870
|
+
if (attrMap) {
|
|
2871
|
+
const keys = Object.keys(attrMap);
|
|
2872
|
+
const len = keys.length; //don't make it inline
|
|
2873
|
+
for (let i = 0; i < len; i++) {
|
|
2874
|
+
const atrrName = keys[i];
|
|
2875
|
+
if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
|
|
2876
|
+
obj[atrrName] = [ attrMap[atrrName] ];
|
|
2877
|
+
} else {
|
|
2878
|
+
obj[atrrName] = attrMap[atrrName];
|
|
2879
|
+
}
|
|
2880
|
+
}
|
|
2881
|
+
}
|
|
2882
|
+
}
|
|
2883
|
+
|
|
2884
|
+
function isLeafTag(obj, options){
|
|
2885
|
+
const { textNodeName } = options;
|
|
2886
|
+
const propCount = Object.keys(obj).length;
|
|
2887
|
+
|
|
2888
|
+
if (propCount === 0) {
|
|
2889
|
+
return true;
|
|
2890
|
+
}
|
|
2891
|
+
|
|
2892
|
+
if (
|
|
2893
|
+
propCount === 1 &&
|
|
2894
|
+
(obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
|
|
2895
|
+
) {
|
|
2896
|
+
return true;
|
|
2897
|
+
}
|
|
2898
|
+
|
|
2899
|
+
return false;
|
|
2900
|
+
}
|
|
2901
|
+
node2json.prettify = prettify$1;
|
|
2902
|
+
|
|
2903
|
+
const { buildOptions} = OptionsBuilder;
|
|
2904
|
+
const OrderedObjParser = OrderedObjParser_1;
|
|
2905
|
+
const { prettify} = node2json;
|
|
2906
|
+
const validator$1 = validator$2;
|
|
2907
|
+
|
|
2908
|
+
let XMLParser$1 = class XMLParser{
|
|
2909
|
+
|
|
2910
|
+
constructor(options){
|
|
2911
|
+
this.externalEntities = {};
|
|
2912
|
+
this.options = buildOptions(options);
|
|
2913
|
+
|
|
2914
|
+
}
|
|
2915
|
+
/**
|
|
2916
|
+
* Parse XML dats to JS object
|
|
2917
|
+
* @param {string|Buffer} xmlData
|
|
2918
|
+
* @param {boolean|Object} validationOption
|
|
2919
|
+
*/
|
|
2920
|
+
parse(xmlData,validationOption){
|
|
2921
|
+
if(typeof xmlData === "string");else if( xmlData.toString){
|
|
2922
|
+
xmlData = xmlData.toString();
|
|
2923
|
+
}else {
|
|
2924
|
+
throw new Error("XML data is accepted in String or Bytes[] form.")
|
|
2925
|
+
}
|
|
2926
|
+
if( validationOption){
|
|
2927
|
+
if(validationOption === true) validationOption = {}; //validate with default options
|
|
2928
|
+
|
|
2929
|
+
const result = validator$1.validate(xmlData, validationOption);
|
|
2930
|
+
if (result !== true) {
|
|
2931
|
+
throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
const orderedObjParser = new OrderedObjParser(this.options);
|
|
2935
|
+
orderedObjParser.addExternalEntities(this.externalEntities);
|
|
2936
|
+
const orderedResult = orderedObjParser.parseXml(xmlData);
|
|
2937
|
+
if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
2938
|
+
else return prettify(orderedResult, this.options);
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
/**
|
|
2942
|
+
* Add Entity which is not by default supported by this library
|
|
2943
|
+
* @param {string} key
|
|
2944
|
+
* @param {string} value
|
|
2945
|
+
*/
|
|
2946
|
+
addEntity(key, value){
|
|
2947
|
+
if(value.indexOf("&") !== -1){
|
|
2948
|
+
throw new Error("Entity value can't have '&'")
|
|
2949
|
+
}else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
|
|
2950
|
+
throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'")
|
|
2951
|
+
}else if(value === "&"){
|
|
2952
|
+
throw new Error("An entity with value '&' is not permitted");
|
|
2953
|
+
}else {
|
|
2954
|
+
this.externalEntities[key] = value;
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
};
|
|
2958
|
+
|
|
2959
|
+
var XMLParser_1 = XMLParser$1;
|
|
2960
|
+
|
|
2961
|
+
const EOL = "\n";
|
|
2962
|
+
|
|
2963
|
+
/**
|
|
2964
|
+
*
|
|
2965
|
+
* @param {array} jArray
|
|
2966
|
+
* @param {any} options
|
|
2967
|
+
* @returns
|
|
2968
|
+
*/
|
|
2969
|
+
function toXml(jArray, options) {
|
|
2970
|
+
let indentation = "";
|
|
2971
|
+
if (options.format && options.indentBy.length > 0) {
|
|
2972
|
+
indentation = EOL;
|
|
2973
|
+
}
|
|
2974
|
+
return arrToStr(jArray, options, "", indentation);
|
|
2975
|
+
}
|
|
2976
|
+
|
|
2977
|
+
function arrToStr(arr, options, jPath, indentation) {
|
|
2978
|
+
let xmlStr = "";
|
|
2979
|
+
let isPreviousElementTag = false;
|
|
2980
|
+
|
|
2981
|
+
for (let i = 0; i < arr.length; i++) {
|
|
2982
|
+
const tagObj = arr[i];
|
|
2983
|
+
const tagName = propName(tagObj);
|
|
2984
|
+
if(tagName === undefined) continue;
|
|
2985
|
+
|
|
2986
|
+
let newJPath = "";
|
|
2987
|
+
if (jPath.length === 0) newJPath = tagName;
|
|
2988
|
+
else newJPath = `${jPath}.${tagName}`;
|
|
2989
|
+
|
|
2990
|
+
if (tagName === options.textNodeName) {
|
|
2991
|
+
let tagText = tagObj[tagName];
|
|
2992
|
+
if (!isStopNode(newJPath, options)) {
|
|
2993
|
+
tagText = options.tagValueProcessor(tagName, tagText);
|
|
2994
|
+
tagText = replaceEntitiesValue(tagText, options);
|
|
2995
|
+
}
|
|
2996
|
+
if (isPreviousElementTag) {
|
|
2997
|
+
xmlStr += indentation;
|
|
2998
|
+
}
|
|
2999
|
+
xmlStr += tagText;
|
|
3000
|
+
isPreviousElementTag = false;
|
|
3001
|
+
continue;
|
|
3002
|
+
} else if (tagName === options.cdataPropName) {
|
|
3003
|
+
if (isPreviousElementTag) {
|
|
3004
|
+
xmlStr += indentation;
|
|
3005
|
+
}
|
|
3006
|
+
xmlStr += `<![CDATA[${tagObj[tagName][0][options.textNodeName]}]]>`;
|
|
3007
|
+
isPreviousElementTag = false;
|
|
3008
|
+
continue;
|
|
3009
|
+
} else if (tagName === options.commentPropName) {
|
|
3010
|
+
xmlStr += indentation + `<!--${tagObj[tagName][0][options.textNodeName]}-->`;
|
|
3011
|
+
isPreviousElementTag = true;
|
|
3012
|
+
continue;
|
|
3013
|
+
} else if (tagName[0] === "?") {
|
|
3014
|
+
const attStr = attr_to_str(tagObj[":@"], options);
|
|
3015
|
+
const tempInd = tagName === "?xml" ? "" : indentation;
|
|
3016
|
+
let piTextNodeName = tagObj[tagName][0][options.textNodeName];
|
|
3017
|
+
piTextNodeName = piTextNodeName.length !== 0 ? " " + piTextNodeName : ""; //remove extra spacing
|
|
3018
|
+
xmlStr += tempInd + `<${tagName}${piTextNodeName}${attStr}?>`;
|
|
3019
|
+
isPreviousElementTag = true;
|
|
3020
|
+
continue;
|
|
3021
|
+
}
|
|
3022
|
+
let newIdentation = indentation;
|
|
3023
|
+
if (newIdentation !== "") {
|
|
3024
|
+
newIdentation += options.indentBy;
|
|
3025
|
+
}
|
|
3026
|
+
const attStr = attr_to_str(tagObj[":@"], options);
|
|
3027
|
+
const tagStart = indentation + `<${tagName}${attStr}`;
|
|
3028
|
+
const tagValue = arrToStr(tagObj[tagName], options, newJPath, newIdentation);
|
|
3029
|
+
if (options.unpairedTags.indexOf(tagName) !== -1) {
|
|
3030
|
+
if (options.suppressUnpairedNode) xmlStr += tagStart + ">";
|
|
3031
|
+
else xmlStr += tagStart + "/>";
|
|
3032
|
+
} else if ((!tagValue || tagValue.length === 0) && options.suppressEmptyNode) {
|
|
3033
|
+
xmlStr += tagStart + "/>";
|
|
3034
|
+
} else if (tagValue && tagValue.endsWith(">")) {
|
|
3035
|
+
xmlStr += tagStart + `>${tagValue}${indentation}</${tagName}>`;
|
|
3036
|
+
} else {
|
|
3037
|
+
xmlStr += tagStart + ">";
|
|
3038
|
+
if (tagValue && indentation !== "" && (tagValue.includes("/>") || tagValue.includes("</"))) {
|
|
3039
|
+
xmlStr += indentation + options.indentBy + tagValue + indentation;
|
|
3040
|
+
} else {
|
|
3041
|
+
xmlStr += tagValue;
|
|
3042
|
+
}
|
|
3043
|
+
xmlStr += `</${tagName}>`;
|
|
3044
|
+
}
|
|
3045
|
+
isPreviousElementTag = true;
|
|
3046
|
+
}
|
|
3047
|
+
|
|
3048
|
+
return xmlStr;
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
function propName(obj) {
|
|
3052
|
+
const keys = Object.keys(obj);
|
|
3053
|
+
for (let i = 0; i < keys.length; i++) {
|
|
3054
|
+
const key = keys[i];
|
|
3055
|
+
if(!obj.hasOwnProperty(key)) continue;
|
|
3056
|
+
if (key !== ":@") return key;
|
|
3057
|
+
}
|
|
3058
|
+
}
|
|
3059
|
+
|
|
3060
|
+
function attr_to_str(attrMap, options) {
|
|
3061
|
+
let attrStr = "";
|
|
3062
|
+
if (attrMap && !options.ignoreAttributes) {
|
|
3063
|
+
for (let attr in attrMap) {
|
|
3064
|
+
if(!attrMap.hasOwnProperty(attr)) continue;
|
|
3065
|
+
let attrVal = options.attributeValueProcessor(attr, attrMap[attr]);
|
|
3066
|
+
attrVal = replaceEntitiesValue(attrVal, options);
|
|
3067
|
+
if (attrVal === true && options.suppressBooleanAttributes) {
|
|
3068
|
+
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}`;
|
|
3069
|
+
} else {
|
|
3070
|
+
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}="${attrVal}"`;
|
|
3071
|
+
}
|
|
3072
|
+
}
|
|
3073
|
+
}
|
|
3074
|
+
return attrStr;
|
|
3075
|
+
}
|
|
3076
|
+
|
|
3077
|
+
function isStopNode(jPath, options) {
|
|
3078
|
+
jPath = jPath.substr(0, jPath.length - options.textNodeName.length - 1);
|
|
3079
|
+
let tagName = jPath.substr(jPath.lastIndexOf(".") + 1);
|
|
3080
|
+
for (let index in options.stopNodes) {
|
|
3081
|
+
if (options.stopNodes[index] === jPath || options.stopNodes[index] === "*." + tagName) return true;
|
|
3082
|
+
}
|
|
3083
|
+
return false;
|
|
3084
|
+
}
|
|
3085
|
+
|
|
3086
|
+
function replaceEntitiesValue(textValue, options) {
|
|
3087
|
+
if (textValue && textValue.length > 0 && options.processEntities) {
|
|
3088
|
+
for (let i = 0; i < options.entities.length; i++) {
|
|
3089
|
+
const entity = options.entities[i];
|
|
3090
|
+
textValue = textValue.replace(entity.regex, entity.val);
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
return textValue;
|
|
3094
|
+
}
|
|
3095
|
+
var orderedJs2Xml = toXml;
|
|
3096
|
+
|
|
3097
|
+
//parse Empty Node as self closing node
|
|
3098
|
+
const buildFromOrderedJs = orderedJs2Xml;
|
|
3099
|
+
const getIgnoreAttributesFn = ignoreAttributes;
|
|
3100
|
+
|
|
3101
|
+
const defaultOptions = {
|
|
3102
|
+
attributeNamePrefix: '@_',
|
|
3103
|
+
attributesGroupName: false,
|
|
3104
|
+
textNodeName: '#text',
|
|
3105
|
+
ignoreAttributes: true,
|
|
3106
|
+
cdataPropName: false,
|
|
3107
|
+
format: false,
|
|
3108
|
+
indentBy: ' ',
|
|
3109
|
+
suppressEmptyNode: false,
|
|
3110
|
+
suppressUnpairedNode: true,
|
|
3111
|
+
suppressBooleanAttributes: true,
|
|
3112
|
+
tagValueProcessor: function(key, a) {
|
|
3113
|
+
return a;
|
|
3114
|
+
},
|
|
3115
|
+
attributeValueProcessor: function(attrName, a) {
|
|
3116
|
+
return a;
|
|
3117
|
+
},
|
|
3118
|
+
preserveOrder: false,
|
|
3119
|
+
commentPropName: false,
|
|
3120
|
+
unpairedTags: [],
|
|
3121
|
+
entities: [
|
|
3122
|
+
{ regex: new RegExp("&", "g"), val: "&" },//it must be on top
|
|
3123
|
+
{ regex: new RegExp(">", "g"), val: ">" },
|
|
3124
|
+
{ regex: new RegExp("<", "g"), val: "<" },
|
|
3125
|
+
{ regex: new RegExp("\'", "g"), val: "'" },
|
|
3126
|
+
{ regex: new RegExp("\"", "g"), val: """ }
|
|
3127
|
+
],
|
|
3128
|
+
processEntities: true,
|
|
3129
|
+
stopNodes: [],
|
|
3130
|
+
// transformTagName: false,
|
|
3131
|
+
// transformAttributeName: false,
|
|
3132
|
+
oneListGroup: false
|
|
3133
|
+
};
|
|
3134
|
+
|
|
3135
|
+
function Builder(options) {
|
|
3136
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
3137
|
+
if (this.options.ignoreAttributes === true || this.options.attributesGroupName) {
|
|
3138
|
+
this.isAttribute = function(/*a*/) {
|
|
3139
|
+
return false;
|
|
3140
|
+
};
|
|
3141
|
+
} else {
|
|
3142
|
+
this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes);
|
|
3143
|
+
this.attrPrefixLen = this.options.attributeNamePrefix.length;
|
|
3144
|
+
this.isAttribute = isAttribute;
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
this.processTextOrObjNode = processTextOrObjNode;
|
|
3148
|
+
|
|
3149
|
+
if (this.options.format) {
|
|
3150
|
+
this.indentate = indentate;
|
|
3151
|
+
this.tagEndChar = '>\n';
|
|
3152
|
+
this.newLine = '\n';
|
|
3153
|
+
} else {
|
|
3154
|
+
this.indentate = function() {
|
|
3155
|
+
return '';
|
|
3156
|
+
};
|
|
3157
|
+
this.tagEndChar = '>';
|
|
3158
|
+
this.newLine = '';
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
|
|
3162
|
+
Builder.prototype.build = function(jObj) {
|
|
3163
|
+
if(this.options.preserveOrder){
|
|
3164
|
+
return buildFromOrderedJs(jObj, this.options);
|
|
3165
|
+
}else {
|
|
3166
|
+
if(Array.isArray(jObj) && this.options.arrayNodeName && this.options.arrayNodeName.length > 1){
|
|
3167
|
+
jObj = {
|
|
3168
|
+
[this.options.arrayNodeName] : jObj
|
|
3169
|
+
};
|
|
3170
|
+
}
|
|
3171
|
+
return this.j2x(jObj, 0, []).val;
|
|
3172
|
+
}
|
|
3173
|
+
};
|
|
3174
|
+
|
|
3175
|
+
Builder.prototype.j2x = function(jObj, level, ajPath) {
|
|
3176
|
+
let attrStr = '';
|
|
3177
|
+
let val = '';
|
|
3178
|
+
const jPath = ajPath.join('.');
|
|
3179
|
+
for (let key in jObj) {
|
|
3180
|
+
if(!Object.prototype.hasOwnProperty.call(jObj, key)) continue;
|
|
3181
|
+
if (typeof jObj[key] === 'undefined') {
|
|
3182
|
+
// supress undefined node only if it is not an attribute
|
|
3183
|
+
if (this.isAttribute(key)) {
|
|
3184
|
+
val += '';
|
|
3185
|
+
}
|
|
3186
|
+
} else if (jObj[key] === null) {
|
|
3187
|
+
// null attribute should be ignored by the attribute list, but should not cause the tag closing
|
|
3188
|
+
if (this.isAttribute(key)) {
|
|
3189
|
+
val += '';
|
|
3190
|
+
} else if (key === this.options.cdataPropName) {
|
|
3191
|
+
val += '';
|
|
3192
|
+
} else if (key[0] === '?') {
|
|
3193
|
+
val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
3194
|
+
} else {
|
|
3195
|
+
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
3196
|
+
}
|
|
3197
|
+
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
3198
|
+
} else if (jObj[key] instanceof Date) {
|
|
3199
|
+
val += this.buildTextValNode(jObj[key], key, '', level);
|
|
3200
|
+
} else if (typeof jObj[key] !== 'object') {
|
|
3201
|
+
//premitive type
|
|
3202
|
+
const attr = this.isAttribute(key);
|
|
3203
|
+
if (attr && !this.ignoreAttributesFn(attr, jPath)) {
|
|
3204
|
+
attrStr += this.buildAttrPairStr(attr, '' + jObj[key]);
|
|
3205
|
+
} else if (!attr) {
|
|
3206
|
+
//tag value
|
|
3207
|
+
if (key === this.options.textNodeName) {
|
|
3208
|
+
let newval = this.options.tagValueProcessor(key, '' + jObj[key]);
|
|
3209
|
+
val += this.replaceEntitiesValue(newval);
|
|
3210
|
+
} else {
|
|
3211
|
+
val += this.buildTextValNode(jObj[key], key, '', level);
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
} else if (Array.isArray(jObj[key])) {
|
|
3215
|
+
//repeated nodes
|
|
3216
|
+
const arrLen = jObj[key].length;
|
|
3217
|
+
let listTagVal = "";
|
|
3218
|
+
let listTagAttr = "";
|
|
3219
|
+
for (let j = 0; j < arrLen; j++) {
|
|
3220
|
+
const item = jObj[key][j];
|
|
3221
|
+
if (typeof item === 'undefined') ; else if (item === null) {
|
|
3222
|
+
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
|
3223
|
+
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
3224
|
+
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
3225
|
+
} else if (typeof item === 'object') {
|
|
3226
|
+
if(this.options.oneListGroup){
|
|
3227
|
+
const result = this.j2x(item, level + 1, ajPath.concat(key));
|
|
3228
|
+
listTagVal += result.val;
|
|
3229
|
+
if (this.options.attributesGroupName && item.hasOwnProperty(this.options.attributesGroupName)) {
|
|
3230
|
+
listTagAttr += result.attrStr;
|
|
3231
|
+
}
|
|
3232
|
+
}else {
|
|
3233
|
+
listTagVal += this.processTextOrObjNode(item, key, level, ajPath);
|
|
3234
|
+
}
|
|
3235
|
+
} else {
|
|
3236
|
+
if (this.options.oneListGroup) {
|
|
3237
|
+
let textValue = this.options.tagValueProcessor(key, item);
|
|
3238
|
+
textValue = this.replaceEntitiesValue(textValue);
|
|
3239
|
+
listTagVal += textValue;
|
|
3240
|
+
} else {
|
|
3241
|
+
listTagVal += this.buildTextValNode(item, key, '', level);
|
|
3242
|
+
}
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
if(this.options.oneListGroup){
|
|
3246
|
+
listTagVal = this.buildObjectNode(listTagVal, key, listTagAttr, level);
|
|
3247
|
+
}
|
|
3248
|
+
val += listTagVal;
|
|
3249
|
+
} else {
|
|
3250
|
+
//nested node
|
|
3251
|
+
if (this.options.attributesGroupName && key === this.options.attributesGroupName) {
|
|
3252
|
+
const Ks = Object.keys(jObj[key]);
|
|
3253
|
+
const L = Ks.length;
|
|
3254
|
+
for (let j = 0; j < L; j++) {
|
|
3255
|
+
attrStr += this.buildAttrPairStr(Ks[j], '' + jObj[key][Ks[j]]);
|
|
3256
|
+
}
|
|
3257
|
+
} else {
|
|
3258
|
+
val += this.processTextOrObjNode(jObj[key], key, level, ajPath);
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
return {attrStr: attrStr, val: val};
|
|
3263
|
+
};
|
|
3264
|
+
|
|
3265
|
+
Builder.prototype.buildAttrPairStr = function(attrName, val){
|
|
3266
|
+
val = this.options.attributeValueProcessor(attrName, '' + val);
|
|
3267
|
+
val = this.replaceEntitiesValue(val);
|
|
3268
|
+
if (this.options.suppressBooleanAttributes && val === "true") {
|
|
3269
|
+
return ' ' + attrName;
|
|
3270
|
+
} else return ' ' + attrName + '="' + val + '"';
|
|
3271
|
+
};
|
|
3272
|
+
|
|
3273
|
+
function processTextOrObjNode (object, key, level, ajPath) {
|
|
3274
|
+
const result = this.j2x(object, level + 1, ajPath.concat(key));
|
|
3275
|
+
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
|
|
3276
|
+
return this.buildTextValNode(object[this.options.textNodeName], key, result.attrStr, level);
|
|
3277
|
+
} else {
|
|
3278
|
+
return this.buildObjectNode(result.val, key, result.attrStr, level);
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
Builder.prototype.buildObjectNode = function(val, key, attrStr, level) {
|
|
3283
|
+
if(val === ""){
|
|
3284
|
+
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
|
|
3285
|
+
else {
|
|
3286
|
+
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
|
|
3287
|
+
}
|
|
3288
|
+
}else {
|
|
3289
|
+
|
|
3290
|
+
let tagEndExp = '</' + key + this.tagEndChar;
|
|
3291
|
+
let piClosingChar = "";
|
|
3292
|
+
|
|
3293
|
+
if(key[0] === "?") {
|
|
3294
|
+
piClosingChar = "?";
|
|
3295
|
+
tagEndExp = "";
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
// attrStr is an empty string in case the attribute came as undefined or null
|
|
3299
|
+
if ((attrStr || attrStr === '') && val.indexOf('<') === -1) {
|
|
3300
|
+
return ( this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' + val + tagEndExp );
|
|
3301
|
+
} else if (this.options.commentPropName !== false && key === this.options.commentPropName && piClosingChar.length === 0) {
|
|
3302
|
+
return this.indentate(level) + `<!--${val}-->` + this.newLine;
|
|
3303
|
+
}else {
|
|
3304
|
+
return (
|
|
3305
|
+
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
|
|
3306
|
+
val +
|
|
3307
|
+
this.indentate(level) + tagEndExp );
|
|
3308
|
+
}
|
|
3309
|
+
}
|
|
3310
|
+
};
|
|
3311
|
+
|
|
3312
|
+
Builder.prototype.closeTag = function(key){
|
|
3313
|
+
let closeTag = "";
|
|
3314
|
+
if(this.options.unpairedTags.indexOf(key) !== -1){ //unpaired
|
|
3315
|
+
if(!this.options.suppressUnpairedNode) closeTag = "/";
|
|
3316
|
+
}else if(this.options.suppressEmptyNode){ //empty
|
|
3317
|
+
closeTag = "/";
|
|
3318
|
+
}else {
|
|
3319
|
+
closeTag = `></${key}`;
|
|
3320
|
+
}
|
|
3321
|
+
return closeTag;
|
|
3322
|
+
};
|
|
3323
|
+
|
|
3324
|
+
Builder.prototype.buildTextValNode = function(val, key, attrStr, level) {
|
|
3325
|
+
if (this.options.cdataPropName !== false && key === this.options.cdataPropName) {
|
|
3326
|
+
return this.indentate(level) + `<![CDATA[${val}]]>` + this.newLine;
|
|
3327
|
+
}else if (this.options.commentPropName !== false && key === this.options.commentPropName) {
|
|
3328
|
+
return this.indentate(level) + `<!--${val}-->` + this.newLine;
|
|
3329
|
+
}else if(key[0] === "?") {//PI tag
|
|
3330
|
+
return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
|
|
3331
|
+
}else {
|
|
3332
|
+
let textValue = this.options.tagValueProcessor(key, val);
|
|
3333
|
+
textValue = this.replaceEntitiesValue(textValue);
|
|
3334
|
+
|
|
3335
|
+
if( textValue === ''){
|
|
3336
|
+
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
|
|
3337
|
+
}else {
|
|
3338
|
+
return this.indentate(level) + '<' + key + attrStr + '>' +
|
|
3339
|
+
textValue +
|
|
3340
|
+
'</' + key + this.tagEndChar;
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
};
|
|
3344
|
+
|
|
3345
|
+
Builder.prototype.replaceEntitiesValue = function(textValue){
|
|
3346
|
+
if(textValue && textValue.length > 0 && this.options.processEntities){
|
|
3347
|
+
for (let i=0; i<this.options.entities.length; i++) {
|
|
3348
|
+
const entity = this.options.entities[i];
|
|
3349
|
+
textValue = textValue.replace(entity.regex, entity.val);
|
|
3350
|
+
}
|
|
3351
|
+
}
|
|
3352
|
+
return textValue;
|
|
3353
|
+
};
|
|
3354
|
+
|
|
3355
|
+
function indentate(level) {
|
|
3356
|
+
return this.options.indentBy.repeat(level);
|
|
3357
|
+
}
|
|
3358
|
+
|
|
3359
|
+
function isAttribute(name /*, options*/) {
|
|
3360
|
+
if (name.startsWith(this.options.attributeNamePrefix) && name !== this.options.textNodeName) {
|
|
3361
|
+
return name.substr(this.attrPrefixLen);
|
|
3362
|
+
} else {
|
|
3363
|
+
return false;
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
|
|
3367
|
+
var json2xml = Builder;
|
|
3368
|
+
|
|
3369
|
+
const validator = validator$2;
|
|
3370
|
+
const XMLParser = XMLParser_1;
|
|
3371
|
+
const XMLBuilder = json2xml;
|
|
3372
|
+
|
|
3373
|
+
var fxp = {
|
|
3374
|
+
XMLParser: XMLParser,
|
|
3375
|
+
XMLValidator: validator,
|
|
3376
|
+
XMLBuilder: XMLBuilder
|
|
3377
|
+
};
|
|
3378
|
+
|
|
3379
|
+
var _samplers = {};
|
|
3380
|
+
|
|
3381
|
+
const defaults = {
|
|
3382
|
+
skipReadOnly: false,
|
|
3383
|
+
maxSampleDepth: 15,
|
|
3384
|
+
};
|
|
3385
|
+
|
|
3386
|
+
function convertJsonToXml(obj, schema) {
|
|
3387
|
+
if (!obj) {
|
|
3388
|
+
throw new Error('Unknown format output for building XML.');
|
|
3389
|
+
}
|
|
3390
|
+
if (Array.isArray(obj) || Object.keys(obj).length > 1) {
|
|
3391
|
+
obj = { [schema?.xml?.name || 'root']: obj }; // XML document must contain one root element
|
|
3392
|
+
}
|
|
3393
|
+
const builder = new fxp.XMLBuilder({
|
|
3394
|
+
ignoreAttributes : false,
|
|
3395
|
+
format: true,
|
|
3396
|
+
attributeNamePrefix: '$',
|
|
3397
|
+
textNodeName: '#text',
|
|
3398
|
+
});
|
|
3399
|
+
return builder.build(obj);
|
|
3400
|
+
}
|
|
3401
|
+
|
|
3402
|
+
function sample(schema, options, spec) {
|
|
3403
|
+
let opts = Object.assign({}, defaults, options);
|
|
3404
|
+
clearCache();
|
|
3405
|
+
let result = traverse(schema, opts, spec).value;
|
|
3406
|
+
if (opts?.format === 'xml') {
|
|
3407
|
+
return convertJsonToXml(result, schema);
|
|
3408
|
+
}
|
|
3409
|
+
return result;
|
|
3410
|
+
}
|
|
3411
|
+
function _registerSampler(type, sampler) {
|
|
3412
|
+
_samplers[type] = sampler;
|
|
3413
|
+
}
|
|
3414
|
+
_registerSampler('array', sampleArray);
|
|
3415
|
+
_registerSampler('boolean', sampleBoolean);
|
|
3416
|
+
_registerSampler('integer', sampleNumber);
|
|
3417
|
+
_registerSampler('number', sampleNumber);
|
|
3418
|
+
_registerSampler('object', sampleObject);
|
|
3419
|
+
_registerSampler('string', sampleString);
|
|
3420
|
+
|
|
3421
|
+
export { sample };
|
|
3422
|
+
//# sourceMappingURL=index.js.map
|