plist 0.4.3 → 1.2.0

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.
@@ -0,0 +1,4055 @@
1
+ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.plist = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
+ (function (Buffer){
3
+
4
+ /**
5
+ * Module dependencies.
6
+ */
7
+
8
+ var deprecate = require('util-deprecate');
9
+ var DOMParser = require('xmldom').DOMParser;
10
+
11
+ /**
12
+ * Module exports.
13
+ */
14
+
15
+ exports.parse = parse;
16
+ exports.parseString = deprecate(parseString, '`parseString()` is deprecated. ' +
17
+ 'It\'s not actually async. Use `parse()` instead.');
18
+ exports.parseStringSync = deprecate(parseStringSync, '`parseStringSync()` is ' +
19
+ 'deprecated. Use `parse()` instead.');
20
+
21
+ /**
22
+ * We ignore raw text (usually whitespace), <!-- xml comments -->,
23
+ * and raw CDATA nodes.
24
+ *
25
+ * @param {Element} node
26
+ * @returns {Boolean}
27
+ * @api private
28
+ */
29
+
30
+ function shouldIgnoreNode (node) {
31
+ return node.nodeType === 3 // text
32
+ || node.nodeType === 8 // comment
33
+ || node.nodeType === 4; // cdata
34
+ }
35
+
36
+
37
+ /**
38
+ * Parses a Plist XML string. Returns an Object.
39
+ *
40
+ * @param {String} xml - the XML String to decode
41
+ * @returns {Mixed} the decoded value from the Plist XML
42
+ * @api public
43
+ */
44
+
45
+ function parse (xml) {
46
+ var doc = new DOMParser().parseFromString(xml);
47
+ if (doc.documentElement.nodeName !== 'plist') {
48
+ throw new Error('malformed document. First element should be <plist>');
49
+ }
50
+ var plist = parsePlistXML(doc.documentElement);
51
+
52
+ // the root <plist> node gets interpreted as an Array,
53
+ // so pull out the inner data first
54
+ if (plist.length == 1) plist = plist[0];
55
+
56
+ return plist;
57
+ }
58
+
59
+ /**
60
+ * Parses a Plist XML string. Returns an Object. Takes a `callback` function.
61
+ *
62
+ * @param {String} xml - the XML String to decode
63
+ * @param {Function} callback - callback function
64
+ * @returns {Mixed} the decoded value from the Plist XML
65
+ * @api public
66
+ * @deprecated not actually async. use parse() instead
67
+ */
68
+
69
+ function parseString (xml, callback) {
70
+ var doc, error, plist;
71
+ try {
72
+ doc = new DOMParser().parseFromString(xml);
73
+ plist = parsePlistXML(doc.documentElement);
74
+ } catch(e) {
75
+ error = e;
76
+ }
77
+ callback(error, plist);
78
+ }
79
+
80
+ /**
81
+ * Parses a Plist XML string. Returns an Object.
82
+ *
83
+ * @param {String} xml - the XML String to decode
84
+ * @param {Function} callback - callback function
85
+ * @returns {Mixed} the decoded value from the Plist XML
86
+ * @api public
87
+ * @deprecated use parse() instead
88
+ */
89
+
90
+ function parseStringSync (xml) {
91
+ var doc = new DOMParser().parseFromString(xml);
92
+ var plist;
93
+ if (doc.documentElement.nodeName !== 'plist') {
94
+ throw new Error('malformed document. First element should be <plist>');
95
+ }
96
+ plist = parsePlistXML(doc.documentElement);
97
+
98
+ // if the plist is an array with 1 element, pull it out of the array
99
+ if (plist.length == 1) {
100
+ plist = plist[0];
101
+ }
102
+ return plist;
103
+ }
104
+
105
+ /**
106
+ * Convert an XML based plist document into a JSON representation.
107
+ *
108
+ * @param {Object} xml_node - current XML node in the plist
109
+ * @returns {Mixed} built up JSON object
110
+ * @api private
111
+ */
112
+
113
+ function parsePlistXML (node) {
114
+ var i, new_obj, key, val, new_arr, res, d;
115
+
116
+ if (!node)
117
+ return null;
118
+
119
+ if (node.nodeName === 'plist') {
120
+ new_arr = [];
121
+ for (i=0; i < node.childNodes.length; i++) {
122
+ // ignore comment nodes (text)
123
+ if (!shouldIgnoreNode(node.childNodes[i])) {
124
+ new_arr.push( parsePlistXML(node.childNodes[i]));
125
+ }
126
+ }
127
+ return new_arr;
128
+
129
+ } else if (node.nodeName === 'dict') {
130
+ new_obj = {};
131
+ key = null;
132
+ for (i=0; i < node.childNodes.length; i++) {
133
+ // ignore comment nodes (text)
134
+ if (!shouldIgnoreNode(node.childNodes[i])) {
135
+ if (key === null) {
136
+ key = parsePlistXML(node.childNodes[i]);
137
+ } else {
138
+ new_obj[key] = parsePlistXML(node.childNodes[i]);
139
+ key = null;
140
+ }
141
+ }
142
+ }
143
+ return new_obj;
144
+
145
+ } else if (node.nodeName === 'array') {
146
+ new_arr = [];
147
+ for (i=0; i < node.childNodes.length; i++) {
148
+ // ignore comment nodes (text)
149
+ if (!shouldIgnoreNode(node.childNodes[i])) {
150
+ res = parsePlistXML(node.childNodes[i]);
151
+ if (null != res) new_arr.push(res);
152
+ }
153
+ }
154
+ return new_arr;
155
+
156
+ } else if (node.nodeName === '#text') {
157
+ // TODO: what should we do with text types? (CDATA sections)
158
+
159
+ } else if (node.nodeName === 'key') {
160
+ return node.childNodes[0].nodeValue;
161
+
162
+ } else if (node.nodeName === 'string') {
163
+ res = '';
164
+ for (d=0; d < node.childNodes.length; d++) {
165
+ res += node.childNodes[d].nodeValue;
166
+ }
167
+ return res;
168
+
169
+ } else if (node.nodeName === 'integer') {
170
+ // parse as base 10 integer
171
+ return parseInt(node.childNodes[0].nodeValue, 10);
172
+
173
+ } else if (node.nodeName === 'real') {
174
+ res = '';
175
+ for (d=0; d < node.childNodes.length; d++) {
176
+ if (node.childNodes[d].nodeType === 3) {
177
+ res += node.childNodes[d].nodeValue;
178
+ }
179
+ }
180
+ return parseFloat(res);
181
+
182
+ } else if (node.nodeName === 'data') {
183
+ res = '';
184
+ for (d=0; d < node.childNodes.length; d++) {
185
+ if (node.childNodes[d].nodeType === 3) {
186
+ res += node.childNodes[d].nodeValue.replace(/\s+/g, '');
187
+ }
188
+ }
189
+
190
+ // decode base64 data to a Buffer instance
191
+ return new Buffer(res, 'base64');
192
+
193
+ } else if (node.nodeName === 'date') {
194
+ return new Date(node.childNodes[0].nodeValue);
195
+
196
+ } else if (node.nodeName === 'true') {
197
+ return true;
198
+
199
+ } else if (node.nodeName === 'false') {
200
+ return false;
201
+ }
202
+ }
203
+
204
+ }).call(this,require("buffer").Buffer)
205
+ },{"buffer":3,"util-deprecate":6,"xmldom":7}],2:[function(require,module,exports){
206
+ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
207
+
208
+ ;(function (exports) {
209
+ 'use strict';
210
+
211
+ var Arr = (typeof Uint8Array !== 'undefined')
212
+ ? Uint8Array
213
+ : Array
214
+
215
+ var PLUS = '+'.charCodeAt(0)
216
+ var SLASH = '/'.charCodeAt(0)
217
+ var NUMBER = '0'.charCodeAt(0)
218
+ var LOWER = 'a'.charCodeAt(0)
219
+ var UPPER = 'A'.charCodeAt(0)
220
+ var PLUS_URL_SAFE = '-'.charCodeAt(0)
221
+ var SLASH_URL_SAFE = '_'.charCodeAt(0)
222
+
223
+ function decode (elt) {
224
+ var code = elt.charCodeAt(0)
225
+ if (code === PLUS ||
226
+ code === PLUS_URL_SAFE)
227
+ return 62 // '+'
228
+ if (code === SLASH ||
229
+ code === SLASH_URL_SAFE)
230
+ return 63 // '/'
231
+ if (code < NUMBER)
232
+ return -1 //no match
233
+ if (code < NUMBER + 10)
234
+ return code - NUMBER + 26 + 26
235
+ if (code < UPPER + 26)
236
+ return code - UPPER
237
+ if (code < LOWER + 26)
238
+ return code - LOWER + 26
239
+ }
240
+
241
+ function b64ToByteArray (b64) {
242
+ var i, j, l, tmp, placeHolders, arr
243
+
244
+ if (b64.length % 4 > 0) {
245
+ throw new Error('Invalid string. Length must be a multiple of 4')
246
+ }
247
+
248
+ // the number of equal signs (place holders)
249
+ // if there are two placeholders, than the two characters before it
250
+ // represent one byte
251
+ // if there is only one, then the three characters before it represent 2 bytes
252
+ // this is just a cheap hack to not do indexOf twice
253
+ var len = b64.length
254
+ placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0
255
+
256
+ // base64 is 4/3 + up to two characters of the original data
257
+ arr = new Arr(b64.length * 3 / 4 - placeHolders)
258
+
259
+ // if there are placeholders, only get up to the last complete 4 chars
260
+ l = placeHolders > 0 ? b64.length - 4 : b64.length
261
+
262
+ var L = 0
263
+
264
+ function push (v) {
265
+ arr[L++] = v
266
+ }
267
+
268
+ for (i = 0, j = 0; i < l; i += 4, j += 3) {
269
+ tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
270
+ push((tmp & 0xFF0000) >> 16)
271
+ push((tmp & 0xFF00) >> 8)
272
+ push(tmp & 0xFF)
273
+ }
274
+
275
+ if (placeHolders === 2) {
276
+ tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
277
+ push(tmp & 0xFF)
278
+ } else if (placeHolders === 1) {
279
+ tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
280
+ push((tmp >> 8) & 0xFF)
281
+ push(tmp & 0xFF)
282
+ }
283
+
284
+ return arr
285
+ }
286
+
287
+ function uint8ToBase64 (uint8) {
288
+ var i,
289
+ extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
290
+ output = "",
291
+ temp, length
292
+
293
+ function encode (num) {
294
+ return lookup.charAt(num)
295
+ }
296
+
297
+ function tripletToBase64 (num) {
298
+ return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
299
+ }
300
+
301
+ // go through the array every three bytes, we'll deal with trailing stuff later
302
+ for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
303
+ temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
304
+ output += tripletToBase64(temp)
305
+ }
306
+
307
+ // pad the end with zeros, but make sure to not forget the extra bytes
308
+ switch (extraBytes) {
309
+ case 1:
310
+ temp = uint8[uint8.length - 1]
311
+ output += encode(temp >> 2)
312
+ output += encode((temp << 4) & 0x3F)
313
+ output += '=='
314
+ break
315
+ case 2:
316
+ temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
317
+ output += encode(temp >> 10)
318
+ output += encode((temp >> 4) & 0x3F)
319
+ output += encode((temp << 2) & 0x3F)
320
+ output += '='
321
+ break
322
+ }
323
+
324
+ return output
325
+ }
326
+
327
+ exports.toByteArray = b64ToByteArray
328
+ exports.fromByteArray = uint8ToBase64
329
+ }(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
330
+
331
+ },{}],3:[function(require,module,exports){
332
+ (function (global){
333
+ /*!
334
+ * The buffer module from node.js, for the browser.
335
+ *
336
+ * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
337
+ * @license MIT
338
+ */
339
+ /* eslint-disable no-proto */
340
+
341
+ var base64 = require('base64-js')
342
+ var ieee754 = require('ieee754')
343
+ var isArray = require('is-array')
344
+
345
+ exports.Buffer = Buffer
346
+ exports.SlowBuffer = SlowBuffer
347
+ exports.INSPECT_MAX_BYTES = 50
348
+ Buffer.poolSize = 8192 // not used by this implementation
349
+
350
+ var rootParent = {}
351
+
352
+ /**
353
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
354
+ * === true Use Uint8Array implementation (fastest)
355
+ * === false Use Object implementation (most compatible, even IE6)
356
+ *
357
+ * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
358
+ * Opera 11.6+, iOS 4.2+.
359
+ *
360
+ * Due to various browser bugs, sometimes the Object implementation will be used even
361
+ * when the browser supports typed arrays.
362
+ *
363
+ * Note:
364
+ *
365
+ * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
366
+ * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
367
+ *
368
+ * - Safari 5-7 lacks support for changing the `Object.prototype.constructor` property
369
+ * on objects.
370
+ *
371
+ * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
372
+ *
373
+ * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
374
+ * incorrect length in some situations.
375
+
376
+ * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
377
+ * get the Object implementation, which is slower but behaves correctly.
378
+ */
379
+ Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
380
+ ? global.TYPED_ARRAY_SUPPORT
381
+ : typedArraySupport()
382
+
383
+ function typedArraySupport () {
384
+ function Bar () {}
385
+ try {
386
+ var arr = new Uint8Array(1)
387
+ arr.foo = function () { return 42 }
388
+ arr.constructor = Bar
389
+ return arr.foo() === 42 && // typed array instances can be augmented
390
+ arr.constructor === Bar && // constructor can be set
391
+ typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
392
+ arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
393
+ } catch (e) {
394
+ return false
395
+ }
396
+ }
397
+
398
+ function kMaxLength () {
399
+ return Buffer.TYPED_ARRAY_SUPPORT
400
+ ? 0x7fffffff
401
+ : 0x3fffffff
402
+ }
403
+
404
+ /**
405
+ * Class: Buffer
406
+ * =============
407
+ *
408
+ * The Buffer constructor returns instances of `Uint8Array` that are augmented
409
+ * with function properties for all the node `Buffer` API functions. We use
410
+ * `Uint8Array` so that square bracket notation works as expected -- it returns
411
+ * a single octet.
412
+ *
413
+ * By augmenting the instances, we can avoid modifying the `Uint8Array`
414
+ * prototype.
415
+ */
416
+ function Buffer (arg) {
417
+ if (!(this instanceof Buffer)) {
418
+ // Avoid going through an ArgumentsAdaptorTrampoline in the common case.
419
+ if (arguments.length > 1) return new Buffer(arg, arguments[1])
420
+ return new Buffer(arg)
421
+ }
422
+
423
+ this.length = 0
424
+ this.parent = undefined
425
+
426
+ // Common case.
427
+ if (typeof arg === 'number') {
428
+ return fromNumber(this, arg)
429
+ }
430
+
431
+ // Slightly less common case.
432
+ if (typeof arg === 'string') {
433
+ return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8')
434
+ }
435
+
436
+ // Unusual.
437
+ return fromObject(this, arg)
438
+ }
439
+
440
+ function fromNumber (that, length) {
441
+ that = allocate(that, length < 0 ? 0 : checked(length) | 0)
442
+ if (!Buffer.TYPED_ARRAY_SUPPORT) {
443
+ for (var i = 0; i < length; i++) {
444
+ that[i] = 0
445
+ }
446
+ }
447
+ return that
448
+ }
449
+
450
+ function fromString (that, string, encoding) {
451
+ if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8'
452
+
453
+ // Assumption: byteLength() return value is always < kMaxLength.
454
+ var length = byteLength(string, encoding) | 0
455
+ that = allocate(that, length)
456
+
457
+ that.write(string, encoding)
458
+ return that
459
+ }
460
+
461
+ function fromObject (that, object) {
462
+ if (Buffer.isBuffer(object)) return fromBuffer(that, object)
463
+
464
+ if (isArray(object)) return fromArray(that, object)
465
+
466
+ if (object == null) {
467
+ throw new TypeError('must start with number, buffer, array or string')
468
+ }
469
+
470
+ if (typeof ArrayBuffer !== 'undefined') {
471
+ if (object.buffer instanceof ArrayBuffer) {
472
+ return fromTypedArray(that, object)
473
+ }
474
+ if (object instanceof ArrayBuffer) {
475
+ return fromArrayBuffer(that, object)
476
+ }
477
+ }
478
+
479
+ if (object.length) return fromArrayLike(that, object)
480
+
481
+ return fromJsonObject(that, object)
482
+ }
483
+
484
+ function fromBuffer (that, buffer) {
485
+ var length = checked(buffer.length) | 0
486
+ that = allocate(that, length)
487
+ buffer.copy(that, 0, 0, length)
488
+ return that
489
+ }
490
+
491
+ function fromArray (that, array) {
492
+ var length = checked(array.length) | 0
493
+ that = allocate(that, length)
494
+ for (var i = 0; i < length; i += 1) {
495
+ that[i] = array[i] & 255
496
+ }
497
+ return that
498
+ }
499
+
500
+ // Duplicate of fromArray() to keep fromArray() monomorphic.
501
+ function fromTypedArray (that, array) {
502
+ var length = checked(array.length) | 0
503
+ that = allocate(that, length)
504
+ // Truncating the elements is probably not what people expect from typed
505
+ // arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior
506
+ // of the old Buffer constructor.
507
+ for (var i = 0; i < length; i += 1) {
508
+ that[i] = array[i] & 255
509
+ }
510
+ return that
511
+ }
512
+
513
+ function fromArrayBuffer (that, array) {
514
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
515
+ // Return an augmented `Uint8Array` instance, for best performance
516
+ array.byteLength
517
+ that = Buffer._augment(new Uint8Array(array))
518
+ } else {
519
+ // Fallback: Return an object instance of the Buffer class
520
+ that = fromTypedArray(that, new Uint8Array(array))
521
+ }
522
+ return that
523
+ }
524
+
525
+ function fromArrayLike (that, array) {
526
+ var length = checked(array.length) | 0
527
+ that = allocate(that, length)
528
+ for (var i = 0; i < length; i += 1) {
529
+ that[i] = array[i] & 255
530
+ }
531
+ return that
532
+ }
533
+
534
+ // Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object.
535
+ // Returns a zero-length buffer for inputs that don't conform to the spec.
536
+ function fromJsonObject (that, object) {
537
+ var array
538
+ var length = 0
539
+
540
+ if (object.type === 'Buffer' && isArray(object.data)) {
541
+ array = object.data
542
+ length = checked(array.length) | 0
543
+ }
544
+ that = allocate(that, length)
545
+
546
+ for (var i = 0; i < length; i += 1) {
547
+ that[i] = array[i] & 255
548
+ }
549
+ return that
550
+ }
551
+
552
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
553
+ Buffer.prototype.__proto__ = Uint8Array.prototype
554
+ Buffer.__proto__ = Uint8Array
555
+ }
556
+
557
+ function allocate (that, length) {
558
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
559
+ // Return an augmented `Uint8Array` instance, for best performance
560
+ that = Buffer._augment(new Uint8Array(length))
561
+ that.__proto__ = Buffer.prototype
562
+ } else {
563
+ // Fallback: Return an object instance of the Buffer class
564
+ that.length = length
565
+ that._isBuffer = true
566
+ }
567
+
568
+ var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1
569
+ if (fromPool) that.parent = rootParent
570
+
571
+ return that
572
+ }
573
+
574
+ function checked (length) {
575
+ // Note: cannot use `length < kMaxLength` here because that fails when
576
+ // length is NaN (which is otherwise coerced to zero.)
577
+ if (length >= kMaxLength()) {
578
+ throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
579
+ 'size: 0x' + kMaxLength().toString(16) + ' bytes')
580
+ }
581
+ return length | 0
582
+ }
583
+
584
+ function SlowBuffer (subject, encoding) {
585
+ if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding)
586
+
587
+ var buf = new Buffer(subject, encoding)
588
+ delete buf.parent
589
+ return buf
590
+ }
591
+
592
+ Buffer.isBuffer = function isBuffer (b) {
593
+ return !!(b != null && b._isBuffer)
594
+ }
595
+
596
+ Buffer.compare = function compare (a, b) {
597
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
598
+ throw new TypeError('Arguments must be Buffers')
599
+ }
600
+
601
+ if (a === b) return 0
602
+
603
+ var x = a.length
604
+ var y = b.length
605
+
606
+ var i = 0
607
+ var len = Math.min(x, y)
608
+ while (i < len) {
609
+ if (a[i] !== b[i]) break
610
+
611
+ ++i
612
+ }
613
+
614
+ if (i !== len) {
615
+ x = a[i]
616
+ y = b[i]
617
+ }
618
+
619
+ if (x < y) return -1
620
+ if (y < x) return 1
621
+ return 0
622
+ }
623
+
624
+ Buffer.isEncoding = function isEncoding (encoding) {
625
+ switch (String(encoding).toLowerCase()) {
626
+ case 'hex':
627
+ case 'utf8':
628
+ case 'utf-8':
629
+ case 'ascii':
630
+ case 'binary':
631
+ case 'base64':
632
+ case 'raw':
633
+ case 'ucs2':
634
+ case 'ucs-2':
635
+ case 'utf16le':
636
+ case 'utf-16le':
637
+ return true
638
+ default:
639
+ return false
640
+ }
641
+ }
642
+
643
+ Buffer.concat = function concat (list, length) {
644
+ if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.')
645
+
646
+ if (list.length === 0) {
647
+ return new Buffer(0)
648
+ }
649
+
650
+ var i
651
+ if (length === undefined) {
652
+ length = 0
653
+ for (i = 0; i < list.length; i++) {
654
+ length += list[i].length
655
+ }
656
+ }
657
+
658
+ var buf = new Buffer(length)
659
+ var pos = 0
660
+ for (i = 0; i < list.length; i++) {
661
+ var item = list[i]
662
+ item.copy(buf, pos)
663
+ pos += item.length
664
+ }
665
+ return buf
666
+ }
667
+
668
+ function byteLength (string, encoding) {
669
+ if (typeof string !== 'string') string = '' + string
670
+
671
+ var len = string.length
672
+ if (len === 0) return 0
673
+
674
+ // Use a for loop to avoid recursion
675
+ var loweredCase = false
676
+ for (;;) {
677
+ switch (encoding) {
678
+ case 'ascii':
679
+ case 'binary':
680
+ // Deprecated
681
+ case 'raw':
682
+ case 'raws':
683
+ return len
684
+ case 'utf8':
685
+ case 'utf-8':
686
+ return utf8ToBytes(string).length
687
+ case 'ucs2':
688
+ case 'ucs-2':
689
+ case 'utf16le':
690
+ case 'utf-16le':
691
+ return len * 2
692
+ case 'hex':
693
+ return len >>> 1
694
+ case 'base64':
695
+ return base64ToBytes(string).length
696
+ default:
697
+ if (loweredCase) return utf8ToBytes(string).length // assume utf8
698
+ encoding = ('' + encoding).toLowerCase()
699
+ loweredCase = true
700
+ }
701
+ }
702
+ }
703
+ Buffer.byteLength = byteLength
704
+
705
+ // pre-set for values that may exist in the future
706
+ Buffer.prototype.length = undefined
707
+ Buffer.prototype.parent = undefined
708
+
709
+ function slowToString (encoding, start, end) {
710
+ var loweredCase = false
711
+
712
+ start = start | 0
713
+ end = end === undefined || end === Infinity ? this.length : end | 0
714
+
715
+ if (!encoding) encoding = 'utf8'
716
+ if (start < 0) start = 0
717
+ if (end > this.length) end = this.length
718
+ if (end <= start) return ''
719
+
720
+ while (true) {
721
+ switch (encoding) {
722
+ case 'hex':
723
+ return hexSlice(this, start, end)
724
+
725
+ case 'utf8':
726
+ case 'utf-8':
727
+ return utf8Slice(this, start, end)
728
+
729
+ case 'ascii':
730
+ return asciiSlice(this, start, end)
731
+
732
+ case 'binary':
733
+ return binarySlice(this, start, end)
734
+
735
+ case 'base64':
736
+ return base64Slice(this, start, end)
737
+
738
+ case 'ucs2':
739
+ case 'ucs-2':
740
+ case 'utf16le':
741
+ case 'utf-16le':
742
+ return utf16leSlice(this, start, end)
743
+
744
+ default:
745
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
746
+ encoding = (encoding + '').toLowerCase()
747
+ loweredCase = true
748
+ }
749
+ }
750
+ }
751
+
752
+ Buffer.prototype.toString = function toString () {
753
+ var length = this.length | 0
754
+ if (length === 0) return ''
755
+ if (arguments.length === 0) return utf8Slice(this, 0, length)
756
+ return slowToString.apply(this, arguments)
757
+ }
758
+
759
+ Buffer.prototype.equals = function equals (b) {
760
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
761
+ if (this === b) return true
762
+ return Buffer.compare(this, b) === 0
763
+ }
764
+
765
+ Buffer.prototype.inspect = function inspect () {
766
+ var str = ''
767
+ var max = exports.INSPECT_MAX_BYTES
768
+ if (this.length > 0) {
769
+ str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
770
+ if (this.length > max) str += ' ... '
771
+ }
772
+ return '<Buffer ' + str + '>'
773
+ }
774
+
775
+ Buffer.prototype.compare = function compare (b) {
776
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
777
+ if (this === b) return 0
778
+ return Buffer.compare(this, b)
779
+ }
780
+
781
+ Buffer.prototype.indexOf = function indexOf (val, byteOffset) {
782
+ if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff
783
+ else if (byteOffset < -0x80000000) byteOffset = -0x80000000
784
+ byteOffset >>= 0
785
+
786
+ if (this.length === 0) return -1
787
+ if (byteOffset >= this.length) return -1
788
+
789
+ // Negative offsets start from the end of the buffer
790
+ if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0)
791
+
792
+ if (typeof val === 'string') {
793
+ if (val.length === 0) return -1 // special case: looking for empty string always fails
794
+ return String.prototype.indexOf.call(this, val, byteOffset)
795
+ }
796
+ if (Buffer.isBuffer(val)) {
797
+ return arrayIndexOf(this, val, byteOffset)
798
+ }
799
+ if (typeof val === 'number') {
800
+ if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') {
801
+ return Uint8Array.prototype.indexOf.call(this, val, byteOffset)
802
+ }
803
+ return arrayIndexOf(this, [ val ], byteOffset)
804
+ }
805
+
806
+ function arrayIndexOf (arr, val, byteOffset) {
807
+ var foundIndex = -1
808
+ for (var i = 0; byteOffset + i < arr.length; i++) {
809
+ if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) {
810
+ if (foundIndex === -1) foundIndex = i
811
+ if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex
812
+ } else {
813
+ foundIndex = -1
814
+ }
815
+ }
816
+ return -1
817
+ }
818
+
819
+ throw new TypeError('val must be string, number or Buffer')
820
+ }
821
+
822
+ // `get` is deprecated
823
+ Buffer.prototype.get = function get (offset) {
824
+ console.log('.get() is deprecated. Access using array indexes instead.')
825
+ return this.readUInt8(offset)
826
+ }
827
+
828
+ // `set` is deprecated
829
+ Buffer.prototype.set = function set (v, offset) {
830
+ console.log('.set() is deprecated. Access using array indexes instead.')
831
+ return this.writeUInt8(v, offset)
832
+ }
833
+
834
+ function hexWrite (buf, string, offset, length) {
835
+ offset = Number(offset) || 0
836
+ var remaining = buf.length - offset
837
+ if (!length) {
838
+ length = remaining
839
+ } else {
840
+ length = Number(length)
841
+ if (length > remaining) {
842
+ length = remaining
843
+ }
844
+ }
845
+
846
+ // must be an even number of digits
847
+ var strLen = string.length
848
+ if (strLen % 2 !== 0) throw new Error('Invalid hex string')
849
+
850
+ if (length > strLen / 2) {
851
+ length = strLen / 2
852
+ }
853
+ for (var i = 0; i < length; i++) {
854
+ var parsed = parseInt(string.substr(i * 2, 2), 16)
855
+ if (isNaN(parsed)) throw new Error('Invalid hex string')
856
+ buf[offset + i] = parsed
857
+ }
858
+ return i
859
+ }
860
+
861
+ function utf8Write (buf, string, offset, length) {
862
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
863
+ }
864
+
865
+ function asciiWrite (buf, string, offset, length) {
866
+ return blitBuffer(asciiToBytes(string), buf, offset, length)
867
+ }
868
+
869
+ function binaryWrite (buf, string, offset, length) {
870
+ return asciiWrite(buf, string, offset, length)
871
+ }
872
+
873
+ function base64Write (buf, string, offset, length) {
874
+ return blitBuffer(base64ToBytes(string), buf, offset, length)
875
+ }
876
+
877
+ function ucs2Write (buf, string, offset, length) {
878
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
879
+ }
880
+
881
+ Buffer.prototype.write = function write (string, offset, length, encoding) {
882
+ // Buffer#write(string)
883
+ if (offset === undefined) {
884
+ encoding = 'utf8'
885
+ length = this.length
886
+ offset = 0
887
+ // Buffer#write(string, encoding)
888
+ } else if (length === undefined && typeof offset === 'string') {
889
+ encoding = offset
890
+ length = this.length
891
+ offset = 0
892
+ // Buffer#write(string, offset[, length][, encoding])
893
+ } else if (isFinite(offset)) {
894
+ offset = offset | 0
895
+ if (isFinite(length)) {
896
+ length = length | 0
897
+ if (encoding === undefined) encoding = 'utf8'
898
+ } else {
899
+ encoding = length
900
+ length = undefined
901
+ }
902
+ // legacy write(string, encoding, offset, length) - remove in v0.13
903
+ } else {
904
+ var swap = encoding
905
+ encoding = offset
906
+ offset = length | 0
907
+ length = swap
908
+ }
909
+
910
+ var remaining = this.length - offset
911
+ if (length === undefined || length > remaining) length = remaining
912
+
913
+ if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
914
+ throw new RangeError('attempt to write outside buffer bounds')
915
+ }
916
+
917
+ if (!encoding) encoding = 'utf8'
918
+
919
+ var loweredCase = false
920
+ for (;;) {
921
+ switch (encoding) {
922
+ case 'hex':
923
+ return hexWrite(this, string, offset, length)
924
+
925
+ case 'utf8':
926
+ case 'utf-8':
927
+ return utf8Write(this, string, offset, length)
928
+
929
+ case 'ascii':
930
+ return asciiWrite(this, string, offset, length)
931
+
932
+ case 'binary':
933
+ return binaryWrite(this, string, offset, length)
934
+
935
+ case 'base64':
936
+ // Warning: maxLength not taken into account in base64Write
937
+ return base64Write(this, string, offset, length)
938
+
939
+ case 'ucs2':
940
+ case 'ucs-2':
941
+ case 'utf16le':
942
+ case 'utf-16le':
943
+ return ucs2Write(this, string, offset, length)
944
+
945
+ default:
946
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
947
+ encoding = ('' + encoding).toLowerCase()
948
+ loweredCase = true
949
+ }
950
+ }
951
+ }
952
+
953
+ Buffer.prototype.toJSON = function toJSON () {
954
+ return {
955
+ type: 'Buffer',
956
+ data: Array.prototype.slice.call(this._arr || this, 0)
957
+ }
958
+ }
959
+
960
+ function base64Slice (buf, start, end) {
961
+ if (start === 0 && end === buf.length) {
962
+ return base64.fromByteArray(buf)
963
+ } else {
964
+ return base64.fromByteArray(buf.slice(start, end))
965
+ }
966
+ }
967
+
968
+ function utf8Slice (buf, start, end) {
969
+ end = Math.min(buf.length, end)
970
+ var res = []
971
+
972
+ var i = start
973
+ while (i < end) {
974
+ var firstByte = buf[i]
975
+ var codePoint = null
976
+ var bytesPerSequence = (firstByte > 0xEF) ? 4
977
+ : (firstByte > 0xDF) ? 3
978
+ : (firstByte > 0xBF) ? 2
979
+ : 1
980
+
981
+ if (i + bytesPerSequence <= end) {
982
+ var secondByte, thirdByte, fourthByte, tempCodePoint
983
+
984
+ switch (bytesPerSequence) {
985
+ case 1:
986
+ if (firstByte < 0x80) {
987
+ codePoint = firstByte
988
+ }
989
+ break
990
+ case 2:
991
+ secondByte = buf[i + 1]
992
+ if ((secondByte & 0xC0) === 0x80) {
993
+ tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
994
+ if (tempCodePoint > 0x7F) {
995
+ codePoint = tempCodePoint
996
+ }
997
+ }
998
+ break
999
+ case 3:
1000
+ secondByte = buf[i + 1]
1001
+ thirdByte = buf[i + 2]
1002
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
1003
+ tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
1004
+ if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
1005
+ codePoint = tempCodePoint
1006
+ }
1007
+ }
1008
+ break
1009
+ case 4:
1010
+ secondByte = buf[i + 1]
1011
+ thirdByte = buf[i + 2]
1012
+ fourthByte = buf[i + 3]
1013
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
1014
+ tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
1015
+ if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
1016
+ codePoint = tempCodePoint
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+
1022
+ if (codePoint === null) {
1023
+ // we did not generate a valid codePoint so insert a
1024
+ // replacement char (U+FFFD) and advance only 1 byte
1025
+ codePoint = 0xFFFD
1026
+ bytesPerSequence = 1
1027
+ } else if (codePoint > 0xFFFF) {
1028
+ // encode to utf16 (surrogate pair dance)
1029
+ codePoint -= 0x10000
1030
+ res.push(codePoint >>> 10 & 0x3FF | 0xD800)
1031
+ codePoint = 0xDC00 | codePoint & 0x3FF
1032
+ }
1033
+
1034
+ res.push(codePoint)
1035
+ i += bytesPerSequence
1036
+ }
1037
+
1038
+ return decodeCodePointsArray(res)
1039
+ }
1040
+
1041
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
1042
+ // the lowest limit is Chrome, with 0x10000 args.
1043
+ // We go 1 magnitude less, for safety
1044
+ var MAX_ARGUMENTS_LENGTH = 0x1000
1045
+
1046
+ function decodeCodePointsArray (codePoints) {
1047
+ var len = codePoints.length
1048
+ if (len <= MAX_ARGUMENTS_LENGTH) {
1049
+ return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
1050
+ }
1051
+
1052
+ // Decode in chunks to avoid "call stack size exceeded".
1053
+ var res = ''
1054
+ var i = 0
1055
+ while (i < len) {
1056
+ res += String.fromCharCode.apply(
1057
+ String,
1058
+ codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
1059
+ )
1060
+ }
1061
+ return res
1062
+ }
1063
+
1064
+ function asciiSlice (buf, start, end) {
1065
+ var ret = ''
1066
+ end = Math.min(buf.length, end)
1067
+
1068
+ for (var i = start; i < end; i++) {
1069
+ ret += String.fromCharCode(buf[i] & 0x7F)
1070
+ }
1071
+ return ret
1072
+ }
1073
+
1074
+ function binarySlice (buf, start, end) {
1075
+ var ret = ''
1076
+ end = Math.min(buf.length, end)
1077
+
1078
+ for (var i = start; i < end; i++) {
1079
+ ret += String.fromCharCode(buf[i])
1080
+ }
1081
+ return ret
1082
+ }
1083
+
1084
+ function hexSlice (buf, start, end) {
1085
+ var len = buf.length
1086
+
1087
+ if (!start || start < 0) start = 0
1088
+ if (!end || end < 0 || end > len) end = len
1089
+
1090
+ var out = ''
1091
+ for (var i = start; i < end; i++) {
1092
+ out += toHex(buf[i])
1093
+ }
1094
+ return out
1095
+ }
1096
+
1097
+ function utf16leSlice (buf, start, end) {
1098
+ var bytes = buf.slice(start, end)
1099
+ var res = ''
1100
+ for (var i = 0; i < bytes.length; i += 2) {
1101
+ res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
1102
+ }
1103
+ return res
1104
+ }
1105
+
1106
+ Buffer.prototype.slice = function slice (start, end) {
1107
+ var len = this.length
1108
+ start = ~~start
1109
+ end = end === undefined ? len : ~~end
1110
+
1111
+ if (start < 0) {
1112
+ start += len
1113
+ if (start < 0) start = 0
1114
+ } else if (start > len) {
1115
+ start = len
1116
+ }
1117
+
1118
+ if (end < 0) {
1119
+ end += len
1120
+ if (end < 0) end = 0
1121
+ } else if (end > len) {
1122
+ end = len
1123
+ }
1124
+
1125
+ if (end < start) end = start
1126
+
1127
+ var newBuf
1128
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1129
+ newBuf = Buffer._augment(this.subarray(start, end))
1130
+ } else {
1131
+ var sliceLen = end - start
1132
+ newBuf = new Buffer(sliceLen, undefined)
1133
+ for (var i = 0; i < sliceLen; i++) {
1134
+ newBuf[i] = this[i + start]
1135
+ }
1136
+ }
1137
+
1138
+ if (newBuf.length) newBuf.parent = this.parent || this
1139
+
1140
+ return newBuf
1141
+ }
1142
+
1143
+ /*
1144
+ * Need to make sure that buffer isn't trying to write out of bounds.
1145
+ */
1146
+ function checkOffset (offset, ext, length) {
1147
+ if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
1148
+ if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
1149
+ }
1150
+
1151
+ Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
1152
+ offset = offset | 0
1153
+ byteLength = byteLength | 0
1154
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1155
+
1156
+ var val = this[offset]
1157
+ var mul = 1
1158
+ var i = 0
1159
+ while (++i < byteLength && (mul *= 0x100)) {
1160
+ val += this[offset + i] * mul
1161
+ }
1162
+
1163
+ return val
1164
+ }
1165
+
1166
+ Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
1167
+ offset = offset | 0
1168
+ byteLength = byteLength | 0
1169
+ if (!noAssert) {
1170
+ checkOffset(offset, byteLength, this.length)
1171
+ }
1172
+
1173
+ var val = this[offset + --byteLength]
1174
+ var mul = 1
1175
+ while (byteLength > 0 && (mul *= 0x100)) {
1176
+ val += this[offset + --byteLength] * mul
1177
+ }
1178
+
1179
+ return val
1180
+ }
1181
+
1182
+ Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
1183
+ if (!noAssert) checkOffset(offset, 1, this.length)
1184
+ return this[offset]
1185
+ }
1186
+
1187
+ Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
1188
+ if (!noAssert) checkOffset(offset, 2, this.length)
1189
+ return this[offset] | (this[offset + 1] << 8)
1190
+ }
1191
+
1192
+ Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
1193
+ if (!noAssert) checkOffset(offset, 2, this.length)
1194
+ return (this[offset] << 8) | this[offset + 1]
1195
+ }
1196
+
1197
+ Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
1198
+ if (!noAssert) checkOffset(offset, 4, this.length)
1199
+
1200
+ return ((this[offset]) |
1201
+ (this[offset + 1] << 8) |
1202
+ (this[offset + 2] << 16)) +
1203
+ (this[offset + 3] * 0x1000000)
1204
+ }
1205
+
1206
+ Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
1207
+ if (!noAssert) checkOffset(offset, 4, this.length)
1208
+
1209
+ return (this[offset] * 0x1000000) +
1210
+ ((this[offset + 1] << 16) |
1211
+ (this[offset + 2] << 8) |
1212
+ this[offset + 3])
1213
+ }
1214
+
1215
+ Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
1216
+ offset = offset | 0
1217
+ byteLength = byteLength | 0
1218
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1219
+
1220
+ var val = this[offset]
1221
+ var mul = 1
1222
+ var i = 0
1223
+ while (++i < byteLength && (mul *= 0x100)) {
1224
+ val += this[offset + i] * mul
1225
+ }
1226
+ mul *= 0x80
1227
+
1228
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength)
1229
+
1230
+ return val
1231
+ }
1232
+
1233
+ Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
1234
+ offset = offset | 0
1235
+ byteLength = byteLength | 0
1236
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1237
+
1238
+ var i = byteLength
1239
+ var mul = 1
1240
+ var val = this[offset + --i]
1241
+ while (i > 0 && (mul *= 0x100)) {
1242
+ val += this[offset + --i] * mul
1243
+ }
1244
+ mul *= 0x80
1245
+
1246
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength)
1247
+
1248
+ return val
1249
+ }
1250
+
1251
+ Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
1252
+ if (!noAssert) checkOffset(offset, 1, this.length)
1253
+ if (!(this[offset] & 0x80)) return (this[offset])
1254
+ return ((0xff - this[offset] + 1) * -1)
1255
+ }
1256
+
1257
+ Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
1258
+ if (!noAssert) checkOffset(offset, 2, this.length)
1259
+ var val = this[offset] | (this[offset + 1] << 8)
1260
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
1261
+ }
1262
+
1263
+ Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
1264
+ if (!noAssert) checkOffset(offset, 2, this.length)
1265
+ var val = this[offset + 1] | (this[offset] << 8)
1266
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
1267
+ }
1268
+
1269
+ Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
1270
+ if (!noAssert) checkOffset(offset, 4, this.length)
1271
+
1272
+ return (this[offset]) |
1273
+ (this[offset + 1] << 8) |
1274
+ (this[offset + 2] << 16) |
1275
+ (this[offset + 3] << 24)
1276
+ }
1277
+
1278
+ Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
1279
+ if (!noAssert) checkOffset(offset, 4, this.length)
1280
+
1281
+ return (this[offset] << 24) |
1282
+ (this[offset + 1] << 16) |
1283
+ (this[offset + 2] << 8) |
1284
+ (this[offset + 3])
1285
+ }
1286
+
1287
+ Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
1288
+ if (!noAssert) checkOffset(offset, 4, this.length)
1289
+ return ieee754.read(this, offset, true, 23, 4)
1290
+ }
1291
+
1292
+ Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
1293
+ if (!noAssert) checkOffset(offset, 4, this.length)
1294
+ return ieee754.read(this, offset, false, 23, 4)
1295
+ }
1296
+
1297
+ Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
1298
+ if (!noAssert) checkOffset(offset, 8, this.length)
1299
+ return ieee754.read(this, offset, true, 52, 8)
1300
+ }
1301
+
1302
+ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
1303
+ if (!noAssert) checkOffset(offset, 8, this.length)
1304
+ return ieee754.read(this, offset, false, 52, 8)
1305
+ }
1306
+
1307
+ function checkInt (buf, value, offset, ext, max, min) {
1308
+ if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance')
1309
+ if (value > max || value < min) throw new RangeError('value is out of bounds')
1310
+ if (offset + ext > buf.length) throw new RangeError('index out of range')
1311
+ }
1312
+
1313
+ Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
1314
+ value = +value
1315
+ offset = offset | 0
1316
+ byteLength = byteLength | 0
1317
+ if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
1318
+
1319
+ var mul = 1
1320
+ var i = 0
1321
+ this[offset] = value & 0xFF
1322
+ while (++i < byteLength && (mul *= 0x100)) {
1323
+ this[offset + i] = (value / mul) & 0xFF
1324
+ }
1325
+
1326
+ return offset + byteLength
1327
+ }
1328
+
1329
+ Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
1330
+ value = +value
1331
+ offset = offset | 0
1332
+ byteLength = byteLength | 0
1333
+ if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
1334
+
1335
+ var i = byteLength - 1
1336
+ var mul = 1
1337
+ this[offset + i] = value & 0xFF
1338
+ while (--i >= 0 && (mul *= 0x100)) {
1339
+ this[offset + i] = (value / mul) & 0xFF
1340
+ }
1341
+
1342
+ return offset + byteLength
1343
+ }
1344
+
1345
+ Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
1346
+ value = +value
1347
+ offset = offset | 0
1348
+ if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
1349
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
1350
+ this[offset] = (value & 0xff)
1351
+ return offset + 1
1352
+ }
1353
+
1354
+ function objectWriteUInt16 (buf, value, offset, littleEndian) {
1355
+ if (value < 0) value = 0xffff + value + 1
1356
+ for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
1357
+ buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
1358
+ (littleEndian ? i : 1 - i) * 8
1359
+ }
1360
+ }
1361
+
1362
+ Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
1363
+ value = +value
1364
+ offset = offset | 0
1365
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
1366
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1367
+ this[offset] = (value & 0xff)
1368
+ this[offset + 1] = (value >>> 8)
1369
+ } else {
1370
+ objectWriteUInt16(this, value, offset, true)
1371
+ }
1372
+ return offset + 2
1373
+ }
1374
+
1375
+ Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
1376
+ value = +value
1377
+ offset = offset | 0
1378
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
1379
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1380
+ this[offset] = (value >>> 8)
1381
+ this[offset + 1] = (value & 0xff)
1382
+ } else {
1383
+ objectWriteUInt16(this, value, offset, false)
1384
+ }
1385
+ return offset + 2
1386
+ }
1387
+
1388
+ function objectWriteUInt32 (buf, value, offset, littleEndian) {
1389
+ if (value < 0) value = 0xffffffff + value + 1
1390
+ for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
1391
+ buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
1392
+ }
1393
+ }
1394
+
1395
+ Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
1396
+ value = +value
1397
+ offset = offset | 0
1398
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
1399
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1400
+ this[offset + 3] = (value >>> 24)
1401
+ this[offset + 2] = (value >>> 16)
1402
+ this[offset + 1] = (value >>> 8)
1403
+ this[offset] = (value & 0xff)
1404
+ } else {
1405
+ objectWriteUInt32(this, value, offset, true)
1406
+ }
1407
+ return offset + 4
1408
+ }
1409
+
1410
+ Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
1411
+ value = +value
1412
+ offset = offset | 0
1413
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
1414
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1415
+ this[offset] = (value >>> 24)
1416
+ this[offset + 1] = (value >>> 16)
1417
+ this[offset + 2] = (value >>> 8)
1418
+ this[offset + 3] = (value & 0xff)
1419
+ } else {
1420
+ objectWriteUInt32(this, value, offset, false)
1421
+ }
1422
+ return offset + 4
1423
+ }
1424
+
1425
+ Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
1426
+ value = +value
1427
+ offset = offset | 0
1428
+ if (!noAssert) {
1429
+ var limit = Math.pow(2, 8 * byteLength - 1)
1430
+
1431
+ checkInt(this, value, offset, byteLength, limit - 1, -limit)
1432
+ }
1433
+
1434
+ var i = 0
1435
+ var mul = 1
1436
+ var sub = value < 0 ? 1 : 0
1437
+ this[offset] = value & 0xFF
1438
+ while (++i < byteLength && (mul *= 0x100)) {
1439
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
1440
+ }
1441
+
1442
+ return offset + byteLength
1443
+ }
1444
+
1445
+ Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
1446
+ value = +value
1447
+ offset = offset | 0
1448
+ if (!noAssert) {
1449
+ var limit = Math.pow(2, 8 * byteLength - 1)
1450
+
1451
+ checkInt(this, value, offset, byteLength, limit - 1, -limit)
1452
+ }
1453
+
1454
+ var i = byteLength - 1
1455
+ var mul = 1
1456
+ var sub = value < 0 ? 1 : 0
1457
+ this[offset + i] = value & 0xFF
1458
+ while (--i >= 0 && (mul *= 0x100)) {
1459
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
1460
+ }
1461
+
1462
+ return offset + byteLength
1463
+ }
1464
+
1465
+ Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
1466
+ value = +value
1467
+ offset = offset | 0
1468
+ if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
1469
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
1470
+ if (value < 0) value = 0xff + value + 1
1471
+ this[offset] = (value & 0xff)
1472
+ return offset + 1
1473
+ }
1474
+
1475
+ Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
1476
+ value = +value
1477
+ offset = offset | 0
1478
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1479
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1480
+ this[offset] = (value & 0xff)
1481
+ this[offset + 1] = (value >>> 8)
1482
+ } else {
1483
+ objectWriteUInt16(this, value, offset, true)
1484
+ }
1485
+ return offset + 2
1486
+ }
1487
+
1488
+ Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
1489
+ value = +value
1490
+ offset = offset | 0
1491
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1492
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1493
+ this[offset] = (value >>> 8)
1494
+ this[offset + 1] = (value & 0xff)
1495
+ } else {
1496
+ objectWriteUInt16(this, value, offset, false)
1497
+ }
1498
+ return offset + 2
1499
+ }
1500
+
1501
+ Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
1502
+ value = +value
1503
+ offset = offset | 0
1504
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1505
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1506
+ this[offset] = (value & 0xff)
1507
+ this[offset + 1] = (value >>> 8)
1508
+ this[offset + 2] = (value >>> 16)
1509
+ this[offset + 3] = (value >>> 24)
1510
+ } else {
1511
+ objectWriteUInt32(this, value, offset, true)
1512
+ }
1513
+ return offset + 4
1514
+ }
1515
+
1516
+ Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
1517
+ value = +value
1518
+ offset = offset | 0
1519
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1520
+ if (value < 0) value = 0xffffffff + value + 1
1521
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1522
+ this[offset] = (value >>> 24)
1523
+ this[offset + 1] = (value >>> 16)
1524
+ this[offset + 2] = (value >>> 8)
1525
+ this[offset + 3] = (value & 0xff)
1526
+ } else {
1527
+ objectWriteUInt32(this, value, offset, false)
1528
+ }
1529
+ return offset + 4
1530
+ }
1531
+
1532
+ function checkIEEE754 (buf, value, offset, ext, max, min) {
1533
+ if (value > max || value < min) throw new RangeError('value is out of bounds')
1534
+ if (offset + ext > buf.length) throw new RangeError('index out of range')
1535
+ if (offset < 0) throw new RangeError('index out of range')
1536
+ }
1537
+
1538
+ function writeFloat (buf, value, offset, littleEndian, noAssert) {
1539
+ if (!noAssert) {
1540
+ checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
1541
+ }
1542
+ ieee754.write(buf, value, offset, littleEndian, 23, 4)
1543
+ return offset + 4
1544
+ }
1545
+
1546
+ Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
1547
+ return writeFloat(this, value, offset, true, noAssert)
1548
+ }
1549
+
1550
+ Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
1551
+ return writeFloat(this, value, offset, false, noAssert)
1552
+ }
1553
+
1554
+ function writeDouble (buf, value, offset, littleEndian, noAssert) {
1555
+ if (!noAssert) {
1556
+ checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
1557
+ }
1558
+ ieee754.write(buf, value, offset, littleEndian, 52, 8)
1559
+ return offset + 8
1560
+ }
1561
+
1562
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
1563
+ return writeDouble(this, value, offset, true, noAssert)
1564
+ }
1565
+
1566
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
1567
+ return writeDouble(this, value, offset, false, noAssert)
1568
+ }
1569
+
1570
+ // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
1571
+ Buffer.prototype.copy = function copy (target, targetStart, start, end) {
1572
+ if (!start) start = 0
1573
+ if (!end && end !== 0) end = this.length
1574
+ if (targetStart >= target.length) targetStart = target.length
1575
+ if (!targetStart) targetStart = 0
1576
+ if (end > 0 && end < start) end = start
1577
+
1578
+ // Copy 0 bytes; we're done
1579
+ if (end === start) return 0
1580
+ if (target.length === 0 || this.length === 0) return 0
1581
+
1582
+ // Fatal error conditions
1583
+ if (targetStart < 0) {
1584
+ throw new RangeError('targetStart out of bounds')
1585
+ }
1586
+ if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
1587
+ if (end < 0) throw new RangeError('sourceEnd out of bounds')
1588
+
1589
+ // Are we oob?
1590
+ if (end > this.length) end = this.length
1591
+ if (target.length - targetStart < end - start) {
1592
+ end = target.length - targetStart + start
1593
+ }
1594
+
1595
+ var len = end - start
1596
+ var i
1597
+
1598
+ if (this === target && start < targetStart && targetStart < end) {
1599
+ // descending copy from end
1600
+ for (i = len - 1; i >= 0; i--) {
1601
+ target[i + targetStart] = this[i + start]
1602
+ }
1603
+ } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
1604
+ // ascending copy from start
1605
+ for (i = 0; i < len; i++) {
1606
+ target[i + targetStart] = this[i + start]
1607
+ }
1608
+ } else {
1609
+ target._set(this.subarray(start, start + len), targetStart)
1610
+ }
1611
+
1612
+ return len
1613
+ }
1614
+
1615
+ // fill(value, start=0, end=buffer.length)
1616
+ Buffer.prototype.fill = function fill (value, start, end) {
1617
+ if (!value) value = 0
1618
+ if (!start) start = 0
1619
+ if (!end) end = this.length
1620
+
1621
+ if (end < start) throw new RangeError('end < start')
1622
+
1623
+ // Fill 0 bytes; we're done
1624
+ if (end === start) return
1625
+ if (this.length === 0) return
1626
+
1627
+ if (start < 0 || start >= this.length) throw new RangeError('start out of bounds')
1628
+ if (end < 0 || end > this.length) throw new RangeError('end out of bounds')
1629
+
1630
+ var i
1631
+ if (typeof value === 'number') {
1632
+ for (i = start; i < end; i++) {
1633
+ this[i] = value
1634
+ }
1635
+ } else {
1636
+ var bytes = utf8ToBytes(value.toString())
1637
+ var len = bytes.length
1638
+ for (i = start; i < end; i++) {
1639
+ this[i] = bytes[i % len]
1640
+ }
1641
+ }
1642
+
1643
+ return this
1644
+ }
1645
+
1646
+ /**
1647
+ * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.
1648
+ * Added in Node 0.12. Only available in browsers that support ArrayBuffer.
1649
+ */
1650
+ Buffer.prototype.toArrayBuffer = function toArrayBuffer () {
1651
+ if (typeof Uint8Array !== 'undefined') {
1652
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1653
+ return (new Buffer(this)).buffer
1654
+ } else {
1655
+ var buf = new Uint8Array(this.length)
1656
+ for (var i = 0, len = buf.length; i < len; i += 1) {
1657
+ buf[i] = this[i]
1658
+ }
1659
+ return buf.buffer
1660
+ }
1661
+ } else {
1662
+ throw new TypeError('Buffer.toArrayBuffer not supported in this browser')
1663
+ }
1664
+ }
1665
+
1666
+ // HELPER FUNCTIONS
1667
+ // ================
1668
+
1669
+ var BP = Buffer.prototype
1670
+
1671
+ /**
1672
+ * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods
1673
+ */
1674
+ Buffer._augment = function _augment (arr) {
1675
+ arr.constructor = Buffer
1676
+ arr._isBuffer = true
1677
+
1678
+ // save reference to original Uint8Array set method before overwriting
1679
+ arr._set = arr.set
1680
+
1681
+ // deprecated
1682
+ arr.get = BP.get
1683
+ arr.set = BP.set
1684
+
1685
+ arr.write = BP.write
1686
+ arr.toString = BP.toString
1687
+ arr.toLocaleString = BP.toString
1688
+ arr.toJSON = BP.toJSON
1689
+ arr.equals = BP.equals
1690
+ arr.compare = BP.compare
1691
+ arr.indexOf = BP.indexOf
1692
+ arr.copy = BP.copy
1693
+ arr.slice = BP.slice
1694
+ arr.readUIntLE = BP.readUIntLE
1695
+ arr.readUIntBE = BP.readUIntBE
1696
+ arr.readUInt8 = BP.readUInt8
1697
+ arr.readUInt16LE = BP.readUInt16LE
1698
+ arr.readUInt16BE = BP.readUInt16BE
1699
+ arr.readUInt32LE = BP.readUInt32LE
1700
+ arr.readUInt32BE = BP.readUInt32BE
1701
+ arr.readIntLE = BP.readIntLE
1702
+ arr.readIntBE = BP.readIntBE
1703
+ arr.readInt8 = BP.readInt8
1704
+ arr.readInt16LE = BP.readInt16LE
1705
+ arr.readInt16BE = BP.readInt16BE
1706
+ arr.readInt32LE = BP.readInt32LE
1707
+ arr.readInt32BE = BP.readInt32BE
1708
+ arr.readFloatLE = BP.readFloatLE
1709
+ arr.readFloatBE = BP.readFloatBE
1710
+ arr.readDoubleLE = BP.readDoubleLE
1711
+ arr.readDoubleBE = BP.readDoubleBE
1712
+ arr.writeUInt8 = BP.writeUInt8
1713
+ arr.writeUIntLE = BP.writeUIntLE
1714
+ arr.writeUIntBE = BP.writeUIntBE
1715
+ arr.writeUInt16LE = BP.writeUInt16LE
1716
+ arr.writeUInt16BE = BP.writeUInt16BE
1717
+ arr.writeUInt32LE = BP.writeUInt32LE
1718
+ arr.writeUInt32BE = BP.writeUInt32BE
1719
+ arr.writeIntLE = BP.writeIntLE
1720
+ arr.writeIntBE = BP.writeIntBE
1721
+ arr.writeInt8 = BP.writeInt8
1722
+ arr.writeInt16LE = BP.writeInt16LE
1723
+ arr.writeInt16BE = BP.writeInt16BE
1724
+ arr.writeInt32LE = BP.writeInt32LE
1725
+ arr.writeInt32BE = BP.writeInt32BE
1726
+ arr.writeFloatLE = BP.writeFloatLE
1727
+ arr.writeFloatBE = BP.writeFloatBE
1728
+ arr.writeDoubleLE = BP.writeDoubleLE
1729
+ arr.writeDoubleBE = BP.writeDoubleBE
1730
+ arr.fill = BP.fill
1731
+ arr.inspect = BP.inspect
1732
+ arr.toArrayBuffer = BP.toArrayBuffer
1733
+
1734
+ return arr
1735
+ }
1736
+
1737
+ var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g
1738
+
1739
+ function base64clean (str) {
1740
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
1741
+ str = stringtrim(str).replace(INVALID_BASE64_RE, '')
1742
+ // Node converts strings with length < 2 to ''
1743
+ if (str.length < 2) return ''
1744
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
1745
+ while (str.length % 4 !== 0) {
1746
+ str = str + '='
1747
+ }
1748
+ return str
1749
+ }
1750
+
1751
+ function stringtrim (str) {
1752
+ if (str.trim) return str.trim()
1753
+ return str.replace(/^\s+|\s+$/g, '')
1754
+ }
1755
+
1756
+ function toHex (n) {
1757
+ if (n < 16) return '0' + n.toString(16)
1758
+ return n.toString(16)
1759
+ }
1760
+
1761
+ function utf8ToBytes (string, units) {
1762
+ units = units || Infinity
1763
+ var codePoint
1764
+ var length = string.length
1765
+ var leadSurrogate = null
1766
+ var bytes = []
1767
+
1768
+ for (var i = 0; i < length; i++) {
1769
+ codePoint = string.charCodeAt(i)
1770
+
1771
+ // is surrogate component
1772
+ if (codePoint > 0xD7FF && codePoint < 0xE000) {
1773
+ // last char was a lead
1774
+ if (!leadSurrogate) {
1775
+ // no lead yet
1776
+ if (codePoint > 0xDBFF) {
1777
+ // unexpected trail
1778
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1779
+ continue
1780
+ } else if (i + 1 === length) {
1781
+ // unpaired lead
1782
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1783
+ continue
1784
+ }
1785
+
1786
+ // valid lead
1787
+ leadSurrogate = codePoint
1788
+
1789
+ continue
1790
+ }
1791
+
1792
+ // 2 leads in a row
1793
+ if (codePoint < 0xDC00) {
1794
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1795
+ leadSurrogate = codePoint
1796
+ continue
1797
+ }
1798
+
1799
+ // valid surrogate pair
1800
+ codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
1801
+ } else if (leadSurrogate) {
1802
+ // valid bmp char, but last char was a lead
1803
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
1804
+ }
1805
+
1806
+ leadSurrogate = null
1807
+
1808
+ // encode utf8
1809
+ if (codePoint < 0x80) {
1810
+ if ((units -= 1) < 0) break
1811
+ bytes.push(codePoint)
1812
+ } else if (codePoint < 0x800) {
1813
+ if ((units -= 2) < 0) break
1814
+ bytes.push(
1815
+ codePoint >> 0x6 | 0xC0,
1816
+ codePoint & 0x3F | 0x80
1817
+ )
1818
+ } else if (codePoint < 0x10000) {
1819
+ if ((units -= 3) < 0) break
1820
+ bytes.push(
1821
+ codePoint >> 0xC | 0xE0,
1822
+ codePoint >> 0x6 & 0x3F | 0x80,
1823
+ codePoint & 0x3F | 0x80
1824
+ )
1825
+ } else if (codePoint < 0x110000) {
1826
+ if ((units -= 4) < 0) break
1827
+ bytes.push(
1828
+ codePoint >> 0x12 | 0xF0,
1829
+ codePoint >> 0xC & 0x3F | 0x80,
1830
+ codePoint >> 0x6 & 0x3F | 0x80,
1831
+ codePoint & 0x3F | 0x80
1832
+ )
1833
+ } else {
1834
+ throw new Error('Invalid code point')
1835
+ }
1836
+ }
1837
+
1838
+ return bytes
1839
+ }
1840
+
1841
+ function asciiToBytes (str) {
1842
+ var byteArray = []
1843
+ for (var i = 0; i < str.length; i++) {
1844
+ // Node's code seems to be doing this and not & 0x7F..
1845
+ byteArray.push(str.charCodeAt(i) & 0xFF)
1846
+ }
1847
+ return byteArray
1848
+ }
1849
+
1850
+ function utf16leToBytes (str, units) {
1851
+ var c, hi, lo
1852
+ var byteArray = []
1853
+ for (var i = 0; i < str.length; i++) {
1854
+ if ((units -= 2) < 0) break
1855
+
1856
+ c = str.charCodeAt(i)
1857
+ hi = c >> 8
1858
+ lo = c % 256
1859
+ byteArray.push(lo)
1860
+ byteArray.push(hi)
1861
+ }
1862
+
1863
+ return byteArray
1864
+ }
1865
+
1866
+ function base64ToBytes (str) {
1867
+ return base64.toByteArray(base64clean(str))
1868
+ }
1869
+
1870
+ function blitBuffer (src, dst, offset, length) {
1871
+ for (var i = 0; i < length; i++) {
1872
+ if ((i + offset >= dst.length) || (i >= src.length)) break
1873
+ dst[i + offset] = src[i]
1874
+ }
1875
+ return i
1876
+ }
1877
+
1878
+ }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
1879
+ },{"base64-js":2,"ieee754":4,"is-array":5}],4:[function(require,module,exports){
1880
+ exports.read = function (buffer, offset, isLE, mLen, nBytes) {
1881
+ var e, m
1882
+ var eLen = nBytes * 8 - mLen - 1
1883
+ var eMax = (1 << eLen) - 1
1884
+ var eBias = eMax >> 1
1885
+ var nBits = -7
1886
+ var i = isLE ? (nBytes - 1) : 0
1887
+ var d = isLE ? -1 : 1
1888
+ var s = buffer[offset + i]
1889
+
1890
+ i += d
1891
+
1892
+ e = s & ((1 << (-nBits)) - 1)
1893
+ s >>= (-nBits)
1894
+ nBits += eLen
1895
+ for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
1896
+
1897
+ m = e & ((1 << (-nBits)) - 1)
1898
+ e >>= (-nBits)
1899
+ nBits += mLen
1900
+ for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
1901
+
1902
+ if (e === 0) {
1903
+ e = 1 - eBias
1904
+ } else if (e === eMax) {
1905
+ return m ? NaN : ((s ? -1 : 1) * Infinity)
1906
+ } else {
1907
+ m = m + Math.pow(2, mLen)
1908
+ e = e - eBias
1909
+ }
1910
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
1911
+ }
1912
+
1913
+ exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1914
+ var e, m, c
1915
+ var eLen = nBytes * 8 - mLen - 1
1916
+ var eMax = (1 << eLen) - 1
1917
+ var eBias = eMax >> 1
1918
+ var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
1919
+ var i = isLE ? 0 : (nBytes - 1)
1920
+ var d = isLE ? 1 : -1
1921
+ var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
1922
+
1923
+ value = Math.abs(value)
1924
+
1925
+ if (isNaN(value) || value === Infinity) {
1926
+ m = isNaN(value) ? 1 : 0
1927
+ e = eMax
1928
+ } else {
1929
+ e = Math.floor(Math.log(value) / Math.LN2)
1930
+ if (value * (c = Math.pow(2, -e)) < 1) {
1931
+ e--
1932
+ c *= 2
1933
+ }
1934
+ if (e + eBias >= 1) {
1935
+ value += rt / c
1936
+ } else {
1937
+ value += rt * Math.pow(2, 1 - eBias)
1938
+ }
1939
+ if (value * c >= 2) {
1940
+ e++
1941
+ c /= 2
1942
+ }
1943
+
1944
+ if (e + eBias >= eMax) {
1945
+ m = 0
1946
+ e = eMax
1947
+ } else if (e + eBias >= 1) {
1948
+ m = (value * c - 1) * Math.pow(2, mLen)
1949
+ e = e + eBias
1950
+ } else {
1951
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
1952
+ e = 0
1953
+ }
1954
+ }
1955
+
1956
+ for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
1957
+
1958
+ e = (e << mLen) | m
1959
+ eLen += mLen
1960
+ for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
1961
+
1962
+ buffer[offset + i - d] |= s * 128
1963
+ }
1964
+
1965
+ },{}],5:[function(require,module,exports){
1966
+
1967
+ /**
1968
+ * isArray
1969
+ */
1970
+
1971
+ var isArray = Array.isArray;
1972
+
1973
+ /**
1974
+ * toString
1975
+ */
1976
+
1977
+ var str = Object.prototype.toString;
1978
+
1979
+ /**
1980
+ * Whether or not the given `val`
1981
+ * is an array.
1982
+ *
1983
+ * example:
1984
+ *
1985
+ * isArray([]);
1986
+ * // > true
1987
+ * isArray(arguments);
1988
+ * // > false
1989
+ * isArray('');
1990
+ * // > false
1991
+ *
1992
+ * @param {mixed} val
1993
+ * @return {bool}
1994
+ */
1995
+
1996
+ module.exports = isArray || function (val) {
1997
+ return !! val && '[object Array]' == str.call(val);
1998
+ };
1999
+
2000
+ },{}],6:[function(require,module,exports){
2001
+ (function (global){
2002
+
2003
+ /**
2004
+ * Module exports.
2005
+ */
2006
+
2007
+ module.exports = deprecate;
2008
+
2009
+ /**
2010
+ * Mark that a method should not be used.
2011
+ * Returns a modified function which warns once by default.
2012
+ *
2013
+ * If `localStorage.noDeprecation = true` is set, then it is a no-op.
2014
+ *
2015
+ * If `localStorage.throwDeprecation = true` is set, then deprecated functions
2016
+ * will throw an Error when invoked.
2017
+ *
2018
+ * If `localStorage.traceDeprecation = true` is set, then deprecated functions
2019
+ * will invoke `console.trace()` instead of `console.error()`.
2020
+ *
2021
+ * @param {Function} fn - the function to deprecate
2022
+ * @param {String} msg - the string to print to the console when `fn` is invoked
2023
+ * @returns {Function} a new "deprecated" version of `fn`
2024
+ * @api public
2025
+ */
2026
+
2027
+ function deprecate (fn, msg) {
2028
+ if (config('noDeprecation')) {
2029
+ return fn;
2030
+ }
2031
+
2032
+ var warned = false;
2033
+ function deprecated() {
2034
+ if (!warned) {
2035
+ if (config('throwDeprecation')) {
2036
+ throw new Error(msg);
2037
+ } else if (config('traceDeprecation')) {
2038
+ console.trace(msg);
2039
+ } else {
2040
+ console.warn(msg);
2041
+ }
2042
+ warned = true;
2043
+ }
2044
+ return fn.apply(this, arguments);
2045
+ }
2046
+
2047
+ return deprecated;
2048
+ }
2049
+
2050
+ /**
2051
+ * Checks `localStorage` for boolean values for the given `name`.
2052
+ *
2053
+ * @param {String} name
2054
+ * @returns {Boolean}
2055
+ * @api private
2056
+ */
2057
+
2058
+ function config (name) {
2059
+ // accessing global.localStorage can trigger a DOMException in sandboxed iframes
2060
+ try {
2061
+ if (!global.localStorage) return false;
2062
+ } catch (_) {
2063
+ return false;
2064
+ }
2065
+ var val = global.localStorage[name];
2066
+ if (null == val) return false;
2067
+ return String(val).toLowerCase() === 'true';
2068
+ }
2069
+
2070
+ }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
2071
+ },{}],7:[function(require,module,exports){
2072
+ function DOMParser(options){
2073
+ this.options = options ||{locator:{}};
2074
+
2075
+ }
2076
+ DOMParser.prototype.parseFromString = function(source,mimeType){
2077
+ var options = this.options;
2078
+ var sax = new XMLReader();
2079
+ var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
2080
+ var errorHandler = options.errorHandler;
2081
+ var locator = options.locator;
2082
+ var defaultNSMap = options.xmlns||{};
2083
+ var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"}
2084
+ if(locator){
2085
+ domBuilder.setDocumentLocator(locator)
2086
+ }
2087
+
2088
+ sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
2089
+ sax.domBuilder = options.domBuilder || domBuilder;
2090
+ if(/\/x?html?$/.test(mimeType)){
2091
+ entityMap.nbsp = '\xa0';
2092
+ entityMap.copy = '\xa9';
2093
+ defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
2094
+ }
2095
+ if(source){
2096
+ sax.parse(source,defaultNSMap,entityMap);
2097
+ }else{
2098
+ sax.errorHandler.error("invalid document source");
2099
+ }
2100
+ return domBuilder.document;
2101
+ }
2102
+ function buildErrorHandler(errorImpl,domBuilder,locator){
2103
+ if(!errorImpl){
2104
+ if(domBuilder instanceof DOMHandler){
2105
+ return domBuilder;
2106
+ }
2107
+ errorImpl = domBuilder ;
2108
+ }
2109
+ var errorHandler = {}
2110
+ var isCallback = errorImpl instanceof Function;
2111
+ locator = locator||{}
2112
+ function build(key){
2113
+ var fn = errorImpl[key];
2114
+ if(!fn){
2115
+ if(isCallback){
2116
+ fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
2117
+ }else{
2118
+ var i=arguments.length;
2119
+ while(--i){
2120
+ if(fn = errorImpl[arguments[i]]){
2121
+ break;
2122
+ }
2123
+ }
2124
+ }
2125
+ }
2126
+ errorHandler[key] = fn && function(msg){
2127
+ fn(msg+_locator(locator));
2128
+ }||function(){};
2129
+ }
2130
+ build('warning','warn');
2131
+ build('error','warn','warning');
2132
+ build('fatalError','warn','warning','error');
2133
+ return errorHandler;
2134
+ }
2135
+ /**
2136
+ * +ContentHandler+ErrorHandler
2137
+ * +LexicalHandler+EntityResolver2
2138
+ * -DeclHandler-DTDHandler
2139
+ *
2140
+ * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler
2141
+ * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2
2142
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html
2143
+ */
2144
+ function DOMHandler() {
2145
+ this.cdata = false;
2146
+ }
2147
+ function position(locator,node){
2148
+ node.lineNumber = locator.lineNumber;
2149
+ node.columnNumber = locator.columnNumber;
2150
+ }
2151
+ /**
2152
+ * @see org.xml.sax.ContentHandler#startDocument
2153
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html
2154
+ */
2155
+ DOMHandler.prototype = {
2156
+ startDocument : function() {
2157
+ this.document = new DOMImplementation().createDocument(null, null, null);
2158
+ if (this.locator) {
2159
+ this.document.documentURI = this.locator.systemId;
2160
+ }
2161
+ },
2162
+ startElement:function(namespaceURI, localName, qName, attrs) {
2163
+ var doc = this.document;
2164
+ var el = doc.createElementNS(namespaceURI, qName||localName);
2165
+ var len = attrs.length;
2166
+ appendElement(this, el);
2167
+ this.currentElement = el;
2168
+
2169
+ this.locator && position(this.locator,el)
2170
+ for (var i = 0 ; i < len; i++) {
2171
+ var namespaceURI = attrs.getURI(i);
2172
+ var value = attrs.getValue(i);
2173
+ var qName = attrs.getQName(i);
2174
+ var attr = doc.createAttributeNS(namespaceURI, qName);
2175
+ if( attr.getOffset){
2176
+ position(attr.getOffset(1),attr)
2177
+ }
2178
+ attr.value = attr.nodeValue = value;
2179
+ el.setAttributeNode(attr)
2180
+ }
2181
+ },
2182
+ endElement:function(namespaceURI, localName, qName) {
2183
+ var current = this.currentElement
2184
+ var tagName = current.tagName;
2185
+ this.currentElement = current.parentNode;
2186
+ },
2187
+ startPrefixMapping:function(prefix, uri) {
2188
+ },
2189
+ endPrefixMapping:function(prefix) {
2190
+ },
2191
+ processingInstruction:function(target, data) {
2192
+ var ins = this.document.createProcessingInstruction(target, data);
2193
+ this.locator && position(this.locator,ins)
2194
+ appendElement(this, ins);
2195
+ },
2196
+ ignorableWhitespace:function(ch, start, length) {
2197
+ },
2198
+ characters:function(chars, start, length) {
2199
+ chars = _toString.apply(this,arguments)
2200
+ //console.log(chars)
2201
+ if(this.currentElement && chars){
2202
+ if (this.cdata) {
2203
+ var charNode = this.document.createCDATASection(chars);
2204
+ this.currentElement.appendChild(charNode);
2205
+ } else {
2206
+ var charNode = this.document.createTextNode(chars);
2207
+ this.currentElement.appendChild(charNode);
2208
+ }
2209
+ this.locator && position(this.locator,charNode)
2210
+ }
2211
+ },
2212
+ skippedEntity:function(name) {
2213
+ },
2214
+ endDocument:function() {
2215
+ this.document.normalize();
2216
+ },
2217
+ setDocumentLocator:function (locator) {
2218
+ if(this.locator = locator){// && !('lineNumber' in locator)){
2219
+ locator.lineNumber = 0;
2220
+ }
2221
+ },
2222
+ //LexicalHandler
2223
+ comment:function(chars, start, length) {
2224
+ chars = _toString.apply(this,arguments)
2225
+ var comm = this.document.createComment(chars);
2226
+ this.locator && position(this.locator,comm)
2227
+ appendElement(this, comm);
2228
+ },
2229
+
2230
+ startCDATA:function() {
2231
+ //used in characters() methods
2232
+ this.cdata = true;
2233
+ },
2234
+ endCDATA:function() {
2235
+ this.cdata = false;
2236
+ },
2237
+
2238
+ startDTD:function(name, publicId, systemId) {
2239
+ var impl = this.document.implementation;
2240
+ if (impl && impl.createDocumentType) {
2241
+ var dt = impl.createDocumentType(name, publicId, systemId);
2242
+ this.locator && position(this.locator,dt)
2243
+ appendElement(this, dt);
2244
+ }
2245
+ },
2246
+ /**
2247
+ * @see org.xml.sax.ErrorHandler
2248
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
2249
+ */
2250
+ warning:function(error) {
2251
+ console.warn(error,_locator(this.locator));
2252
+ },
2253
+ error:function(error) {
2254
+ console.error(error,_locator(this.locator));
2255
+ },
2256
+ fatalError:function(error) {
2257
+ console.error(error,_locator(this.locator));
2258
+ throw error;
2259
+ }
2260
+ }
2261
+ function _locator(l){
2262
+ if(l){
2263
+ return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']'
2264
+ }
2265
+ }
2266
+ function _toString(chars,start,length){
2267
+ if(typeof chars == 'string'){
2268
+ return chars.substr(start,length)
2269
+ }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)")
2270
+ if(chars.length >= start+length || start){
2271
+ return new java.lang.String(chars,start,length)+'';
2272
+ }
2273
+ return chars;
2274
+ }
2275
+ }
2276
+
2277
+ /*
2278
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html
2279
+ * used method of org.xml.sax.ext.LexicalHandler:
2280
+ * #comment(chars, start, length)
2281
+ * #startCDATA()
2282
+ * #endCDATA()
2283
+ * #startDTD(name, publicId, systemId)
2284
+ *
2285
+ *
2286
+ * IGNORED method of org.xml.sax.ext.LexicalHandler:
2287
+ * #endDTD()
2288
+ * #startEntity(name)
2289
+ * #endEntity(name)
2290
+ *
2291
+ *
2292
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html
2293
+ * IGNORED method of org.xml.sax.ext.DeclHandler
2294
+ * #attributeDecl(eName, aName, type, mode, value)
2295
+ * #elementDecl(name, model)
2296
+ * #externalEntityDecl(name, publicId, systemId)
2297
+ * #internalEntityDecl(name, value)
2298
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html
2299
+ * IGNORED method of org.xml.sax.EntityResolver2
2300
+ * #resolveEntity(String name,String publicId,String baseURI,String systemId)
2301
+ * #resolveEntity(publicId, systemId)
2302
+ * #getExternalSubset(name, baseURI)
2303
+ * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html
2304
+ * IGNORED method of org.xml.sax.DTDHandler
2305
+ * #notationDecl(name, publicId, systemId) {};
2306
+ * #unparsedEntityDecl(name, publicId, systemId, notationName) {};
2307
+ */
2308
+ "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){
2309
+ DOMHandler.prototype[key] = function(){return null}
2310
+ })
2311
+
2312
+ /* Private static helpers treated below as private instance methods, so don't need to add these to the public API; we might use a Relator to also get rid of non-standard public properties */
2313
+ function appendElement (hander,node) {
2314
+ if (!hander.currentElement) {
2315
+ hander.document.appendChild(node);
2316
+ } else {
2317
+ hander.currentElement.appendChild(node);
2318
+ }
2319
+ }//appendChild and setAttributeNS are preformance key
2320
+
2321
+ if(typeof require == 'function'){
2322
+ var XMLReader = require('./sax').XMLReader;
2323
+ var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
2324
+ exports.XMLSerializer = require('./dom').XMLSerializer ;
2325
+ exports.DOMParser = DOMParser;
2326
+ }
2327
+
2328
+ },{"./dom":8,"./sax":9}],8:[function(require,module,exports){
2329
+ /*
2330
+ * DOM Level 2
2331
+ * Object DOMException
2332
+ * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
2333
+ * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
2334
+ */
2335
+
2336
+ function copy(src,dest){
2337
+ for(var p in src){
2338
+ dest[p] = src[p];
2339
+ }
2340
+ }
2341
+ /**
2342
+ ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
2343
+ ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
2344
+ */
2345
+ function _extends(Class,Super){
2346
+ var pt = Class.prototype;
2347
+ if(Object.create){
2348
+ var ppt = Object.create(Super.prototype)
2349
+ pt.__proto__ = ppt;
2350
+ }
2351
+ if(!(pt instanceof Super)){
2352
+ function t(){};
2353
+ t.prototype = Super.prototype;
2354
+ t = new t();
2355
+ copy(pt,t);
2356
+ Class.prototype = pt = t;
2357
+ }
2358
+ if(pt.constructor != Class){
2359
+ if(typeof Class != 'function'){
2360
+ console.error("unknow Class:"+Class)
2361
+ }
2362
+ pt.constructor = Class
2363
+ }
2364
+ }
2365
+ var htmlns = 'http://www.w3.org/1999/xhtml' ;
2366
+ // Node Types
2367
+ var NodeType = {}
2368
+ var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
2369
+ var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2;
2370
+ var TEXT_NODE = NodeType.TEXT_NODE = 3;
2371
+ var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4;
2372
+ var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5;
2373
+ var ENTITY_NODE = NodeType.ENTITY_NODE = 6;
2374
+ var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
2375
+ var COMMENT_NODE = NodeType.COMMENT_NODE = 8;
2376
+ var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9;
2377
+ var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10;
2378
+ var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11;
2379
+ var NOTATION_NODE = NodeType.NOTATION_NODE = 12;
2380
+
2381
+ // ExceptionCode
2382
+ var ExceptionCode = {}
2383
+ var ExceptionMessage = {};
2384
+ var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1);
2385
+ var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2);
2386
+ var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3);
2387
+ var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4);
2388
+ var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5);
2389
+ var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6);
2390
+ var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
2391
+ var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8);
2392
+ var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9);
2393
+ var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10);
2394
+ //level2
2395
+ var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11);
2396
+ var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12);
2397
+ var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13);
2398
+ var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14);
2399
+ var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15);
2400
+
2401
+
2402
+ function DOMException(code, message) {
2403
+ if(message instanceof Error){
2404
+ var error = message;
2405
+ }else{
2406
+ error = this;
2407
+ Error.call(this, ExceptionMessage[code]);
2408
+ this.message = ExceptionMessage[code];
2409
+ if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
2410
+ }
2411
+ error.code = code;
2412
+ if(message) this.message = this.message + ": " + message;
2413
+ return error;
2414
+ };
2415
+ DOMException.prototype = Error.prototype;
2416
+ copy(ExceptionCode,DOMException)
2417
+ /**
2418
+ * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
2419
+ * The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live.
2420
+ * The items in the NodeList are accessible via an integral index, starting from 0.
2421
+ */
2422
+ function NodeList() {
2423
+ };
2424
+ NodeList.prototype = {
2425
+ /**
2426
+ * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
2427
+ * @standard level1
2428
+ */
2429
+ length:0,
2430
+ /**
2431
+ * Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.
2432
+ * @standard level1
2433
+ * @param index unsigned long
2434
+ * Index into the collection.
2435
+ * @return Node
2436
+ * The node at the indexth position in the NodeList, or null if that is not a valid index.
2437
+ */
2438
+ item: function(index) {
2439
+ return this[index] || null;
2440
+ }
2441
+ };
2442
+ function LiveNodeList(node,refresh){
2443
+ this._node = node;
2444
+ this._refresh = refresh
2445
+ _updateLiveList(this);
2446
+ }
2447
+ function _updateLiveList(list){
2448
+ var inc = list._node._inc || list._node.ownerDocument._inc;
2449
+ if(list._inc != inc){
2450
+ var ls = list._refresh(list._node);
2451
+ //console.log(ls.length)
2452
+ __set__(list,'length',ls.length);
2453
+ copy(ls,list);
2454
+ list._inc = inc;
2455
+ }
2456
+ }
2457
+ LiveNodeList.prototype.item = function(i){
2458
+ _updateLiveList(this);
2459
+ return this[i];
2460
+ }
2461
+
2462
+ _extends(LiveNodeList,NodeList);
2463
+ /**
2464
+ *
2465
+ * Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can be accessed by name. Note that NamedNodeMap does not inherit from NodeList; NamedNodeMaps are not maintained in any particular order. Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index, but this is simply to allow convenient enumeration of the contents of a NamedNodeMap, and does not imply that the DOM specifies an order to these Nodes.
2466
+ * NamedNodeMap objects in the DOM are live.
2467
+ * used for attributes or DocumentType entities
2468
+ */
2469
+ function NamedNodeMap() {
2470
+ };
2471
+
2472
+ function _findNodeIndex(list,node){
2473
+ var i = list.length;
2474
+ while(i--){
2475
+ if(list[i] === node){return i}
2476
+ }
2477
+ }
2478
+
2479
+ function _addNamedNode(el,list,newAttr,oldAttr){
2480
+ if(oldAttr){
2481
+ list[_findNodeIndex(list,oldAttr)] = newAttr;
2482
+ }else{
2483
+ list[list.length++] = newAttr;
2484
+ }
2485
+ if(el){
2486
+ newAttr.ownerElement = el;
2487
+ var doc = el.ownerDocument;
2488
+ if(doc){
2489
+ oldAttr && _onRemoveAttribute(doc,el,oldAttr);
2490
+ _onAddAttribute(doc,el,newAttr);
2491
+ }
2492
+ }
2493
+ }
2494
+ function _removeNamedNode(el,list,attr){
2495
+ var i = _findNodeIndex(list,attr);
2496
+ if(i>=0){
2497
+ var lastIndex = list.length-1
2498
+ while(i<lastIndex){
2499
+ list[i] = list[++i]
2500
+ }
2501
+ list.length = lastIndex;
2502
+ if(el){
2503
+ var doc = el.ownerDocument;
2504
+ if(doc){
2505
+ _onRemoveAttribute(doc,el,attr);
2506
+ attr.ownerElement = null;
2507
+ }
2508
+ }
2509
+ }else{
2510
+ throw DOMException(NOT_FOUND_ERR,new Error())
2511
+ }
2512
+ }
2513
+ NamedNodeMap.prototype = {
2514
+ length:0,
2515
+ item:NodeList.prototype.item,
2516
+ getNamedItem: function(key) {
2517
+ // if(key.indexOf(':')>0 || key == 'xmlns'){
2518
+ // return null;
2519
+ // }
2520
+ var i = this.length;
2521
+ while(i--){
2522
+ var attr = this[i];
2523
+ if(attr.nodeName == key){
2524
+ return attr;
2525
+ }
2526
+ }
2527
+ },
2528
+ setNamedItem: function(attr) {
2529
+ var el = attr.ownerElement;
2530
+ if(el && el!=this._ownerElement){
2531
+ throw new DOMException(INUSE_ATTRIBUTE_ERR);
2532
+ }
2533
+ var oldAttr = this.getNamedItem(attr.nodeName);
2534
+ _addNamedNode(this._ownerElement,this,attr,oldAttr);
2535
+ return oldAttr;
2536
+ },
2537
+ /* returns Node */
2538
+ setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
2539
+ var el = attr.ownerElement, oldAttr;
2540
+ if(el && el!=this._ownerElement){
2541
+ throw new DOMException(INUSE_ATTRIBUTE_ERR);
2542
+ }
2543
+ oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
2544
+ _addNamedNode(this._ownerElement,this,attr,oldAttr);
2545
+ return oldAttr;
2546
+ },
2547
+
2548
+ /* returns Node */
2549
+ removeNamedItem: function(key) {
2550
+ var attr = this.getNamedItem(key);
2551
+ _removeNamedNode(this._ownerElement,this,attr);
2552
+ return attr;
2553
+
2554
+
2555
+ },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
2556
+
2557
+ //for level2
2558
+ removeNamedItemNS:function(namespaceURI,localName){
2559
+ var attr = this.getNamedItemNS(namespaceURI,localName);
2560
+ _removeNamedNode(this._ownerElement,this,attr);
2561
+ return attr;
2562
+ },
2563
+ getNamedItemNS: function(namespaceURI, localName) {
2564
+ var i = this.length;
2565
+ while(i--){
2566
+ var node = this[i];
2567
+ if(node.localName == localName && node.namespaceURI == namespaceURI){
2568
+ return node;
2569
+ }
2570
+ }
2571
+ return null;
2572
+ }
2573
+ };
2574
+ /**
2575
+ * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490
2576
+ */
2577
+ function DOMImplementation(/* Object */ features) {
2578
+ this._features = {};
2579
+ if (features) {
2580
+ for (var feature in features) {
2581
+ this._features = features[feature];
2582
+ }
2583
+ }
2584
+ };
2585
+
2586
+ DOMImplementation.prototype = {
2587
+ hasFeature: function(/* string */ feature, /* string */ version) {
2588
+ var versions = this._features[feature.toLowerCase()];
2589
+ if (versions && (!version || version in versions)) {
2590
+ return true;
2591
+ } else {
2592
+ return false;
2593
+ }
2594
+ },
2595
+ // Introduced in DOM Level 2:
2596
+ createDocument:function(namespaceURI, qualifiedName, doctype){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR,WRONG_DOCUMENT_ERR
2597
+ var doc = new Document();
2598
+ doc.doctype = doctype;
2599
+ if(doctype){
2600
+ doc.appendChild(doctype);
2601
+ }
2602
+ doc.implementation = this;
2603
+ doc.childNodes = new NodeList();
2604
+ if(qualifiedName){
2605
+ var root = doc.createElementNS(namespaceURI,qualifiedName);
2606
+ doc.appendChild(root);
2607
+ }
2608
+ return doc;
2609
+ },
2610
+ // Introduced in DOM Level 2:
2611
+ createDocumentType:function(qualifiedName, publicId, systemId){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR
2612
+ var node = new DocumentType();
2613
+ node.name = qualifiedName;
2614
+ node.nodeName = qualifiedName;
2615
+ node.publicId = publicId;
2616
+ node.systemId = systemId;
2617
+ // Introduced in DOM Level 2:
2618
+ //readonly attribute DOMString internalSubset;
2619
+
2620
+ //TODO:..
2621
+ // readonly attribute NamedNodeMap entities;
2622
+ // readonly attribute NamedNodeMap notations;
2623
+ return node;
2624
+ }
2625
+ };
2626
+
2627
+
2628
+ /**
2629
+ * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
2630
+ */
2631
+
2632
+ function Node() {
2633
+ };
2634
+
2635
+ Node.prototype = {
2636
+ firstChild : null,
2637
+ lastChild : null,
2638
+ previousSibling : null,
2639
+ nextSibling : null,
2640
+ attributes : null,
2641
+ parentNode : null,
2642
+ childNodes : null,
2643
+ ownerDocument : null,
2644
+ nodeValue : null,
2645
+ namespaceURI : null,
2646
+ prefix : null,
2647
+ localName : null,
2648
+ // Modified in DOM Level 2:
2649
+ insertBefore:function(newChild, refChild){//raises
2650
+ return _insertBefore(this,newChild,refChild);
2651
+ },
2652
+ replaceChild:function(newChild, oldChild){//raises
2653
+ this.insertBefore(newChild,oldChild);
2654
+ if(oldChild){
2655
+ this.removeChild(oldChild);
2656
+ }
2657
+ },
2658
+ removeChild:function(oldChild){
2659
+ return _removeChild(this,oldChild);
2660
+ },
2661
+ appendChild:function(newChild){
2662
+ return this.insertBefore(newChild,null);
2663
+ },
2664
+ hasChildNodes:function(){
2665
+ return this.firstChild != null;
2666
+ },
2667
+ cloneNode:function(deep){
2668
+ return cloneNode(this.ownerDocument||this,this,deep);
2669
+ },
2670
+ // Modified in DOM Level 2:
2671
+ normalize:function(){
2672
+ var child = this.firstChild;
2673
+ while(child){
2674
+ var next = child.nextSibling;
2675
+ if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
2676
+ this.removeChild(next);
2677
+ child.appendData(next.data);
2678
+ }else{
2679
+ child.normalize();
2680
+ child = next;
2681
+ }
2682
+ }
2683
+ },
2684
+ // Introduced in DOM Level 2:
2685
+ isSupported:function(feature, version){
2686
+ return this.ownerDocument.implementation.hasFeature(feature,version);
2687
+ },
2688
+ // Introduced in DOM Level 2:
2689
+ hasAttributes:function(){
2690
+ return this.attributes.length>0;
2691
+ },
2692
+ lookupPrefix:function(namespaceURI){
2693
+ var el = this;
2694
+ while(el){
2695
+ var map = el._nsMap;
2696
+ //console.dir(map)
2697
+ if(map){
2698
+ for(var n in map){
2699
+ if(map[n] == namespaceURI){
2700
+ return n;
2701
+ }
2702
+ }
2703
+ }
2704
+ el = el.nodeType == 2?el.ownerDocument : el.parentNode;
2705
+ }
2706
+ return null;
2707
+ },
2708
+ // Introduced in DOM Level 3:
2709
+ lookupNamespaceURI:function(prefix){
2710
+ var el = this;
2711
+ while(el){
2712
+ var map = el._nsMap;
2713
+ //console.dir(map)
2714
+ if(map){
2715
+ if(prefix in map){
2716
+ return map[prefix] ;
2717
+ }
2718
+ }
2719
+ el = el.nodeType == 2?el.ownerDocument : el.parentNode;
2720
+ }
2721
+ return null;
2722
+ },
2723
+ // Introduced in DOM Level 3:
2724
+ isDefaultNamespace:function(namespaceURI){
2725
+ var prefix = this.lookupPrefix(namespaceURI);
2726
+ return prefix == null;
2727
+ }
2728
+ };
2729
+
2730
+
2731
+ function _xmlEncoder(c){
2732
+ return c == '<' && '&lt;' ||
2733
+ c == '>' && '&gt;' ||
2734
+ c == '&' && '&amp;' ||
2735
+ c == '"' && '&quot;' ||
2736
+ '&#'+c.charCodeAt()+';'
2737
+ }
2738
+
2739
+
2740
+ copy(NodeType,Node);
2741
+ copy(NodeType,Node.prototype);
2742
+
2743
+ /**
2744
+ * @param callback return true for continue,false for break
2745
+ * @return boolean true: break visit;
2746
+ */
2747
+ function _visitNode(node,callback){
2748
+ if(callback(node)){
2749
+ return true;
2750
+ }
2751
+ if(node = node.firstChild){
2752
+ do{
2753
+ if(_visitNode(node,callback)){return true}
2754
+ }while(node=node.nextSibling)
2755
+ }
2756
+ }
2757
+
2758
+
2759
+
2760
+ function Document(){
2761
+ }
2762
+ function _onAddAttribute(doc,el,newAttr){
2763
+ doc && doc._inc++;
2764
+ var ns = newAttr.namespaceURI ;
2765
+ if(ns == 'http://www.w3.org/2000/xmlns/'){
2766
+ //update namespace
2767
+ el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
2768
+ }
2769
+ }
2770
+ function _onRemoveAttribute(doc,el,newAttr,remove){
2771
+ doc && doc._inc++;
2772
+ var ns = newAttr.namespaceURI ;
2773
+ if(ns == 'http://www.w3.org/2000/xmlns/'){
2774
+ //update namespace
2775
+ delete el._nsMap[newAttr.prefix?newAttr.localName:'']
2776
+ }
2777
+ }
2778
+ function _onUpdateChild(doc,el,newChild){
2779
+ if(doc && doc._inc){
2780
+ doc._inc++;
2781
+ //update childNodes
2782
+ var cs = el.childNodes;
2783
+ if(newChild){
2784
+ cs[cs.length++] = newChild;
2785
+ }else{
2786
+ //console.log(1)
2787
+ var child = el.firstChild;
2788
+ var i = 0;
2789
+ while(child){
2790
+ cs[i++] = child;
2791
+ child =child.nextSibling;
2792
+ }
2793
+ cs.length = i;
2794
+ }
2795
+ }
2796
+ }
2797
+
2798
+ /**
2799
+ * attributes;
2800
+ * children;
2801
+ *
2802
+ * writeable properties:
2803
+ * nodeValue,Attr:value,CharacterData:data
2804
+ * prefix
2805
+ */
2806
+ function _removeChild(parentNode,child){
2807
+ var previous = child.previousSibling;
2808
+ var next = child.nextSibling;
2809
+ if(previous){
2810
+ previous.nextSibling = next;
2811
+ }else{
2812
+ parentNode.firstChild = next
2813
+ }
2814
+ if(next){
2815
+ next.previousSibling = previous;
2816
+ }else{
2817
+ parentNode.lastChild = previous;
2818
+ }
2819
+ _onUpdateChild(parentNode.ownerDocument,parentNode);
2820
+ return child;
2821
+ }
2822
+ /**
2823
+ * preformance key(refChild == null)
2824
+ */
2825
+ function _insertBefore(parentNode,newChild,nextChild){
2826
+ var cp = newChild.parentNode;
2827
+ if(cp){
2828
+ cp.removeChild(newChild);//remove and update
2829
+ }
2830
+ if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
2831
+ var newFirst = newChild.firstChild;
2832
+ if (newFirst == null) {
2833
+ return newChild;
2834
+ }
2835
+ var newLast = newChild.lastChild;
2836
+ }else{
2837
+ newFirst = newLast = newChild;
2838
+ }
2839
+ var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild;
2840
+
2841
+ newFirst.previousSibling = pre;
2842
+ newLast.nextSibling = nextChild;
2843
+
2844
+
2845
+ if(pre){
2846
+ pre.nextSibling = newFirst;
2847
+ }else{
2848
+ parentNode.firstChild = newFirst;
2849
+ }
2850
+ if(nextChild == null){
2851
+ parentNode.lastChild = newLast;
2852
+ }else{
2853
+ nextChild.previousSibling = newLast;
2854
+ }
2855
+ do{
2856
+ newFirst.parentNode = parentNode;
2857
+ }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
2858
+ _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode);
2859
+ //console.log(parentNode.lastChild.nextSibling == null)
2860
+ if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) {
2861
+ newChild.firstChild = newChild.lastChild = null;
2862
+ }
2863
+ return newChild;
2864
+ }
2865
+ function _appendSingleChild(parentNode,newChild){
2866
+ var cp = newChild.parentNode;
2867
+ if(cp){
2868
+ var pre = parentNode.lastChild;
2869
+ cp.removeChild(newChild);//remove and update
2870
+ var pre = parentNode.lastChild;
2871
+ }
2872
+ var pre = parentNode.lastChild;
2873
+ newChild.parentNode = parentNode;
2874
+ newChild.previousSibling = pre;
2875
+ newChild.nextSibling = null;
2876
+ if(pre){
2877
+ pre.nextSibling = newChild;
2878
+ }else{
2879
+ parentNode.firstChild = newChild;
2880
+ }
2881
+ parentNode.lastChild = newChild;
2882
+ _onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
2883
+ return newChild;
2884
+ //console.log("__aa",parentNode.lastChild.nextSibling == null)
2885
+ }
2886
+ Document.prototype = {
2887
+ //implementation : null,
2888
+ nodeName : '#document',
2889
+ nodeType : DOCUMENT_NODE,
2890
+ doctype : null,
2891
+ documentElement : null,
2892
+ _inc : 1,
2893
+
2894
+ insertBefore : function(newChild, refChild){//raises
2895
+ if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
2896
+ var child = newChild.firstChild;
2897
+ while(child){
2898
+ var next = child.nextSibling;
2899
+ this.insertBefore(child,refChild);
2900
+ child = next;
2901
+ }
2902
+ return newChild;
2903
+ }
2904
+ if(this.documentElement == null && newChild.nodeType == 1){
2905
+ this.documentElement = newChild;
2906
+ }
2907
+
2908
+ return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild;
2909
+ },
2910
+ removeChild : function(oldChild){
2911
+ if(this.documentElement == oldChild){
2912
+ this.documentElement = null;
2913
+ }
2914
+ return _removeChild(this,oldChild);
2915
+ },
2916
+ // Introduced in DOM Level 2:
2917
+ importNode : function(importedNode,deep){
2918
+ return importNode(this,importedNode,deep);
2919
+ },
2920
+ // Introduced in DOM Level 2:
2921
+ getElementById : function(id){
2922
+ var rtv = null;
2923
+ _visitNode(this.documentElement,function(node){
2924
+ if(node.nodeType == 1){
2925
+ if(node.getAttribute('id') == id){
2926
+ rtv = node;
2927
+ return true;
2928
+ }
2929
+ }
2930
+ })
2931
+ return rtv;
2932
+ },
2933
+
2934
+ //document factory method:
2935
+ createElement : function(tagName){
2936
+ var node = new Element();
2937
+ node.ownerDocument = this;
2938
+ node.nodeName = tagName;
2939
+ node.tagName = tagName;
2940
+ node.childNodes = new NodeList();
2941
+ var attrs = node.attributes = new NamedNodeMap();
2942
+ attrs._ownerElement = node;
2943
+ return node;
2944
+ },
2945
+ createDocumentFragment : function(){
2946
+ var node = new DocumentFragment();
2947
+ node.ownerDocument = this;
2948
+ node.childNodes = new NodeList();
2949
+ return node;
2950
+ },
2951
+ createTextNode : function(data){
2952
+ var node = new Text();
2953
+ node.ownerDocument = this;
2954
+ node.appendData(data)
2955
+ return node;
2956
+ },
2957
+ createComment : function(data){
2958
+ var node = new Comment();
2959
+ node.ownerDocument = this;
2960
+ node.appendData(data)
2961
+ return node;
2962
+ },
2963
+ createCDATASection : function(data){
2964
+ var node = new CDATASection();
2965
+ node.ownerDocument = this;
2966
+ node.appendData(data)
2967
+ return node;
2968
+ },
2969
+ createProcessingInstruction : function(target,data){
2970
+ var node = new ProcessingInstruction();
2971
+ node.ownerDocument = this;
2972
+ node.tagName = node.target = target;
2973
+ node.nodeValue= node.data = data;
2974
+ return node;
2975
+ },
2976
+ createAttribute : function(name){
2977
+ var node = new Attr();
2978
+ node.ownerDocument = this;
2979
+ node.name = name;
2980
+ node.nodeName = name;
2981
+ node.localName = name;
2982
+ node.specified = true;
2983
+ return node;
2984
+ },
2985
+ createEntityReference : function(name){
2986
+ var node = new EntityReference();
2987
+ node.ownerDocument = this;
2988
+ node.nodeName = name;
2989
+ return node;
2990
+ },
2991
+ // Introduced in DOM Level 2:
2992
+ createElementNS : function(namespaceURI,qualifiedName){
2993
+ var node = new Element();
2994
+ var pl = qualifiedName.split(':');
2995
+ var attrs = node.attributes = new NamedNodeMap();
2996
+ node.childNodes = new NodeList();
2997
+ node.ownerDocument = this;
2998
+ node.nodeName = qualifiedName;
2999
+ node.tagName = qualifiedName;
3000
+ node.namespaceURI = namespaceURI;
3001
+ if(pl.length == 2){
3002
+ node.prefix = pl[0];
3003
+ node.localName = pl[1];
3004
+ }else{
3005
+ //el.prefix = null;
3006
+ node.localName = qualifiedName;
3007
+ }
3008
+ attrs._ownerElement = node;
3009
+ return node;
3010
+ },
3011
+ // Introduced in DOM Level 2:
3012
+ createAttributeNS : function(namespaceURI,qualifiedName){
3013
+ var node = new Attr();
3014
+ var pl = qualifiedName.split(':');
3015
+ node.ownerDocument = this;
3016
+ node.nodeName = qualifiedName;
3017
+ node.name = qualifiedName;
3018
+ node.namespaceURI = namespaceURI;
3019
+ node.specified = true;
3020
+ if(pl.length == 2){
3021
+ node.prefix = pl[0];
3022
+ node.localName = pl[1];
3023
+ }else{
3024
+ //el.prefix = null;
3025
+ node.localName = qualifiedName;
3026
+ }
3027
+ return node;
3028
+ }
3029
+ };
3030
+ _extends(Document,Node);
3031
+
3032
+
3033
+ function Element() {
3034
+ this._nsMap = {};
3035
+ };
3036
+ Element.prototype = {
3037
+ nodeType : ELEMENT_NODE,
3038
+ hasAttribute : function(name){
3039
+ return this.getAttributeNode(name)!=null;
3040
+ },
3041
+ getAttribute : function(name){
3042
+ var attr = this.getAttributeNode(name);
3043
+ return attr && attr.value || '';
3044
+ },
3045
+ getAttributeNode : function(name){
3046
+ return this.attributes.getNamedItem(name);
3047
+ },
3048
+ setAttribute : function(name, value){
3049
+ var attr = this.ownerDocument.createAttribute(name);
3050
+ attr.value = attr.nodeValue = "" + value;
3051
+ this.setAttributeNode(attr)
3052
+ },
3053
+ removeAttribute : function(name){
3054
+ var attr = this.getAttributeNode(name)
3055
+ attr && this.removeAttributeNode(attr);
3056
+ },
3057
+
3058
+ //four real opeartion method
3059
+ appendChild:function(newChild){
3060
+ if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
3061
+ return this.insertBefore(newChild,null);
3062
+ }else{
3063
+ return _appendSingleChild(this,newChild);
3064
+ }
3065
+ },
3066
+ setAttributeNode : function(newAttr){
3067
+ return this.attributes.setNamedItem(newAttr);
3068
+ },
3069
+ setAttributeNodeNS : function(newAttr){
3070
+ return this.attributes.setNamedItemNS(newAttr);
3071
+ },
3072
+ removeAttributeNode : function(oldAttr){
3073
+ return this.attributes.removeNamedItem(oldAttr.nodeName);
3074
+ },
3075
+ //get real attribute name,and remove it by removeAttributeNode
3076
+ removeAttributeNS : function(namespaceURI, localName){
3077
+ var old = this.getAttributeNodeNS(namespaceURI, localName);
3078
+ old && this.removeAttributeNode(old);
3079
+ },
3080
+
3081
+ hasAttributeNS : function(namespaceURI, localName){
3082
+ return this.getAttributeNodeNS(namespaceURI, localName)!=null;
3083
+ },
3084
+ getAttributeNS : function(namespaceURI, localName){
3085
+ var attr = this.getAttributeNodeNS(namespaceURI, localName);
3086
+ return attr && attr.value || '';
3087
+ },
3088
+ setAttributeNS : function(namespaceURI, qualifiedName, value){
3089
+ var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
3090
+ attr.value = attr.nodeValue = value;
3091
+ this.setAttributeNode(attr)
3092
+ },
3093
+ getAttributeNodeNS : function(namespaceURI, localName){
3094
+ return this.attributes.getNamedItemNS(namespaceURI, localName);
3095
+ },
3096
+
3097
+ getElementsByTagName : function(tagName){
3098
+ return new LiveNodeList(this,function(base){
3099
+ var ls = [];
3100
+ _visitNode(base,function(node){
3101
+ if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
3102
+ ls.push(node);
3103
+ }
3104
+ });
3105
+ return ls;
3106
+ });
3107
+ },
3108
+ getElementsByTagNameNS : function(namespaceURI, localName){
3109
+ return new LiveNodeList(this,function(base){
3110
+ var ls = [];
3111
+ _visitNode(base,function(node){
3112
+ if(node !== base && node.nodeType === ELEMENT_NODE && node.namespaceURI === namespaceURI && (localName === '*' || node.localName == localName)){
3113
+ ls.push(node);
3114
+ }
3115
+ });
3116
+ return ls;
3117
+ });
3118
+ }
3119
+ };
3120
+ Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
3121
+ Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
3122
+
3123
+
3124
+ _extends(Element,Node);
3125
+ function Attr() {
3126
+ };
3127
+ Attr.prototype.nodeType = ATTRIBUTE_NODE;
3128
+ _extends(Attr,Node);
3129
+
3130
+
3131
+ function CharacterData() {
3132
+ };
3133
+ CharacterData.prototype = {
3134
+ data : '',
3135
+ substringData : function(offset, count) {
3136
+ return this.data.substring(offset, offset+count);
3137
+ },
3138
+ appendData: function(text) {
3139
+ text = this.data+text;
3140
+ this.nodeValue = this.data = text;
3141
+ this.length = text.length;
3142
+ },
3143
+ insertData: function(offset,text) {
3144
+ this.replaceData(offset,0,text);
3145
+
3146
+ },
3147
+ appendChild:function(newChild){
3148
+ //if(!(newChild instanceof CharacterData)){
3149
+ throw new Error(ExceptionMessage[3])
3150
+ //}
3151
+ return Node.prototype.appendChild.apply(this,arguments)
3152
+ },
3153
+ deleteData: function(offset, count) {
3154
+ this.replaceData(offset,count,"");
3155
+ },
3156
+ replaceData: function(offset, count, text) {
3157
+ var start = this.data.substring(0,offset);
3158
+ var end = this.data.substring(offset+count);
3159
+ text = start + text + end;
3160
+ this.nodeValue = this.data = text;
3161
+ this.length = text.length;
3162
+ }
3163
+ }
3164
+ _extends(CharacterData,Node);
3165
+ function Text() {
3166
+ };
3167
+ Text.prototype = {
3168
+ nodeName : "#text",
3169
+ nodeType : TEXT_NODE,
3170
+ splitText : function(offset) {
3171
+ var text = this.data;
3172
+ var newText = text.substring(offset);
3173
+ text = text.substring(0, offset);
3174
+ this.data = this.nodeValue = text;
3175
+ this.length = text.length;
3176
+ var newNode = this.ownerDocument.createTextNode(newText);
3177
+ if(this.parentNode){
3178
+ this.parentNode.insertBefore(newNode, this.nextSibling);
3179
+ }
3180
+ return newNode;
3181
+ }
3182
+ }
3183
+ _extends(Text,CharacterData);
3184
+ function Comment() {
3185
+ };
3186
+ Comment.prototype = {
3187
+ nodeName : "#comment",
3188
+ nodeType : COMMENT_NODE
3189
+ }
3190
+ _extends(Comment,CharacterData);
3191
+
3192
+ function CDATASection() {
3193
+ };
3194
+ CDATASection.prototype = {
3195
+ nodeName : "#cdata-section",
3196
+ nodeType : CDATA_SECTION_NODE
3197
+ }
3198
+ _extends(CDATASection,CharacterData);
3199
+
3200
+
3201
+ function DocumentType() {
3202
+ };
3203
+ DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
3204
+ _extends(DocumentType,Node);
3205
+
3206
+ function Notation() {
3207
+ };
3208
+ Notation.prototype.nodeType = NOTATION_NODE;
3209
+ _extends(Notation,Node);
3210
+
3211
+ function Entity() {
3212
+ };
3213
+ Entity.prototype.nodeType = ENTITY_NODE;
3214
+ _extends(Entity,Node);
3215
+
3216
+ function EntityReference() {
3217
+ };
3218
+ EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
3219
+ _extends(EntityReference,Node);
3220
+
3221
+ function DocumentFragment() {
3222
+ };
3223
+ DocumentFragment.prototype.nodeName = "#document-fragment";
3224
+ DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE;
3225
+ _extends(DocumentFragment,Node);
3226
+
3227
+
3228
+ function ProcessingInstruction() {
3229
+ }
3230
+ ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
3231
+ _extends(ProcessingInstruction,Node);
3232
+ function XMLSerializer(){}
3233
+ XMLSerializer.prototype.serializeToString = function(node){
3234
+ var buf = [];
3235
+ serializeToString(node,buf);
3236
+ return buf.join('');
3237
+ }
3238
+ Node.prototype.toString =function(){
3239
+ return XMLSerializer.prototype.serializeToString(this);
3240
+ }
3241
+ function serializeToString(node,buf){
3242
+ switch(node.nodeType){
3243
+ case ELEMENT_NODE:
3244
+ var attrs = node.attributes;
3245
+ var len = attrs.length;
3246
+ var child = node.firstChild;
3247
+ var nodeName = node.tagName;
3248
+ var isHTML = htmlns === node.namespaceURI
3249
+ buf.push('<',nodeName);
3250
+ for(var i=0;i<len;i++){
3251
+ serializeToString(attrs.item(i),buf,isHTML);
3252
+ }
3253
+ if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
3254
+ buf.push('>');
3255
+ //if is cdata child node
3256
+ if(isHTML && /^script$/i.test(nodeName)){
3257
+ if(child){
3258
+ buf.push(child.data);
3259
+ }
3260
+ }else{
3261
+ while(child){
3262
+ serializeToString(child,buf);
3263
+ child = child.nextSibling;
3264
+ }
3265
+ }
3266
+ buf.push('</',nodeName,'>');
3267
+ }else{
3268
+ buf.push('/>');
3269
+ }
3270
+ return;
3271
+ case DOCUMENT_NODE:
3272
+ case DOCUMENT_FRAGMENT_NODE:
3273
+ var child = node.firstChild;
3274
+ while(child){
3275
+ serializeToString(child,buf);
3276
+ child = child.nextSibling;
3277
+ }
3278
+ return;
3279
+ case ATTRIBUTE_NODE:
3280
+ return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
3281
+ case TEXT_NODE:
3282
+ return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
3283
+ case CDATA_SECTION_NODE:
3284
+ return buf.push( '<![CDATA[',node.data,']]>');
3285
+ case COMMENT_NODE:
3286
+ return buf.push( "<!--",node.data,"-->");
3287
+ case DOCUMENT_TYPE_NODE:
3288
+ var pubid = node.publicId;
3289
+ var sysid = node.systemId;
3290
+ buf.push('<!DOCTYPE ',node.name);
3291
+ if(pubid){
3292
+ buf.push(' PUBLIC "',pubid);
3293
+ if (sysid && sysid!='.') {
3294
+ buf.push( '" "',sysid);
3295
+ }
3296
+ buf.push('">');
3297
+ }else if(sysid && sysid!='.'){
3298
+ buf.push(' SYSTEM "',sysid,'">');
3299
+ }else{
3300
+ var sub = node.internalSubset;
3301
+ if(sub){
3302
+ buf.push(" [",sub,"]");
3303
+ }
3304
+ buf.push(">");
3305
+ }
3306
+ return;
3307
+ case PROCESSING_INSTRUCTION_NODE:
3308
+ return buf.push( "<?",node.target," ",node.data,"?>");
3309
+ case ENTITY_REFERENCE_NODE:
3310
+ return buf.push( '&',node.nodeName,';');
3311
+ //case ENTITY_NODE:
3312
+ //case NOTATION_NODE:
3313
+ default:
3314
+ buf.push('??',node.nodeName);
3315
+ }
3316
+ }
3317
+ function importNode(doc,node,deep){
3318
+ var node2;
3319
+ switch (node.nodeType) {
3320
+ case ELEMENT_NODE:
3321
+ node2 = node.cloneNode(false);
3322
+ node2.ownerDocument = doc;
3323
+ //var attrs = node2.attributes;
3324
+ //var len = attrs.length;
3325
+ //for(var i=0;i<len;i++){
3326
+ //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
3327
+ //}
3328
+ case DOCUMENT_FRAGMENT_NODE:
3329
+ break;
3330
+ case ATTRIBUTE_NODE:
3331
+ deep = true;
3332
+ break;
3333
+ //case ENTITY_REFERENCE_NODE:
3334
+ //case PROCESSING_INSTRUCTION_NODE:
3335
+ ////case TEXT_NODE:
3336
+ //case CDATA_SECTION_NODE:
3337
+ //case COMMENT_NODE:
3338
+ // deep = false;
3339
+ // break;
3340
+ //case DOCUMENT_NODE:
3341
+ //case DOCUMENT_TYPE_NODE:
3342
+ //cannot be imported.
3343
+ //case ENTITY_NODE:
3344
+ //case NOTATION_NODE:
3345
+ //can not hit in level3
3346
+ //default:throw e;
3347
+ }
3348
+ if(!node2){
3349
+ node2 = node.cloneNode(false);//false
3350
+ }
3351
+ node2.ownerDocument = doc;
3352
+ node2.parentNode = null;
3353
+ if(deep){
3354
+ var child = node.firstChild;
3355
+ while(child){
3356
+ node2.appendChild(importNode(doc,child,deep));
3357
+ child = child.nextSibling;
3358
+ }
3359
+ }
3360
+ return node2;
3361
+ }
3362
+ //
3363
+ //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
3364
+ // attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
3365
+ function cloneNode(doc,node,deep){
3366
+ var node2 = new node.constructor();
3367
+ for(var n in node){
3368
+ var v = node[n];
3369
+ if(typeof v != 'object' ){
3370
+ if(v != node2[n]){
3371
+ node2[n] = v;
3372
+ }
3373
+ }
3374
+ }
3375
+ if(node.childNodes){
3376
+ node2.childNodes = new NodeList();
3377
+ }
3378
+ node2.ownerDocument = doc;
3379
+ switch (node2.nodeType) {
3380
+ case ELEMENT_NODE:
3381
+ var attrs = node.attributes;
3382
+ var attrs2 = node2.attributes = new NamedNodeMap();
3383
+ var len = attrs.length
3384
+ attrs2._ownerElement = node2;
3385
+ for(var i=0;i<len;i++){
3386
+ node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
3387
+ }
3388
+ break;;
3389
+ case ATTRIBUTE_NODE:
3390
+ deep = true;
3391
+ }
3392
+ if(deep){
3393
+ var child = node.firstChild;
3394
+ while(child){
3395
+ node2.appendChild(cloneNode(doc,child,deep));
3396
+ child = child.nextSibling;
3397
+ }
3398
+ }
3399
+ return node2;
3400
+ }
3401
+
3402
+ function __set__(object,key,value){
3403
+ object[key] = value
3404
+ }
3405
+ //do dynamic
3406
+ try{
3407
+ if(Object.defineProperty){
3408
+ Object.defineProperty(LiveNodeList.prototype,'length',{
3409
+ get:function(){
3410
+ _updateLiveList(this);
3411
+ return this.$$length;
3412
+ }
3413
+ });
3414
+ Object.defineProperty(Node.prototype,'textContent',{
3415
+ get:function(){
3416
+ return getTextContent(this);
3417
+ },
3418
+ set:function(data){
3419
+ switch(this.nodeType){
3420
+ case 1:
3421
+ case 11:
3422
+ while(this.firstChild){
3423
+ this.removeChild(this.firstChild);
3424
+ }
3425
+ if(data || String(data)){
3426
+ this.appendChild(this.ownerDocument.createTextNode(data));
3427
+ }
3428
+ break;
3429
+ default:
3430
+ //TODO:
3431
+ this.data = data;
3432
+ this.value = value;
3433
+ this.nodeValue = data;
3434
+ }
3435
+ }
3436
+ })
3437
+
3438
+ function getTextContent(node){
3439
+ switch(node.nodeType){
3440
+ case 1:
3441
+ case 11:
3442
+ var buf = [];
3443
+ node = node.firstChild;
3444
+ while(node){
3445
+ if(node.nodeType!==7 && node.nodeType !==8){
3446
+ buf.push(getTextContent(node));
3447
+ }
3448
+ node = node.nextSibling;
3449
+ }
3450
+ return buf.join('');
3451
+ default:
3452
+ return node.nodeValue;
3453
+ }
3454
+ }
3455
+ __set__ = function(object,key,value){
3456
+ //console.log(value)
3457
+ object['$$'+key] = value
3458
+ }
3459
+ }
3460
+ }catch(e){//ie8
3461
+ }
3462
+
3463
+ if(typeof require == 'function'){
3464
+ exports.DOMImplementation = DOMImplementation;
3465
+ exports.XMLSerializer = XMLSerializer;
3466
+ }
3467
+
3468
+ },{}],9:[function(require,module,exports){
3469
+ //[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
3470
+ //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
3471
+ //[5] Name ::= NameStartChar (NameChar)*
3472
+ var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
3473
+ var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\u00B7\u0300-\u036F\\ux203F-\u2040]");
3474
+ var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
3475
+ //var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
3476
+ //var handlers = 'resolveEntity,getExternalSubset,characters,endDocument,endElement,endPrefixMapping,ignorableWhitespace,processingInstruction,setDocumentLocator,skippedEntity,startDocument,startElement,startPrefixMapping,notationDecl,unparsedEntityDecl,error,fatalError,warning,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,comment,endCDATA,endDTD,endEntity,startCDATA,startDTD,startEntity'.split(',')
3477
+
3478
+ //S_TAG, S_ATTR, S_EQ, S_V
3479
+ //S_ATTR_S, S_E, S_S, S_C
3480
+ var S_TAG = 0;//tag name offerring
3481
+ var S_ATTR = 1;//attr name offerring
3482
+ var S_ATTR_S=2;//attr name end and space offer
3483
+ var S_EQ = 3;//=space?
3484
+ var S_V = 4;//attr value(no quot value only)
3485
+ var S_E = 5;//attr value end and no space(quot end)
3486
+ var S_S = 6;//(attr value end || tag end ) && (space offer)
3487
+ var S_C = 7;//closed el<el />
3488
+
3489
+ function XMLReader(){
3490
+
3491
+ }
3492
+
3493
+ XMLReader.prototype = {
3494
+ parse:function(source,defaultNSMap,entityMap){
3495
+ var domBuilder = this.domBuilder;
3496
+ domBuilder.startDocument();
3497
+ _copy(defaultNSMap ,defaultNSMap = {})
3498
+ parse(source,defaultNSMap,entityMap,
3499
+ domBuilder,this.errorHandler);
3500
+ domBuilder.endDocument();
3501
+ }
3502
+ }
3503
+ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
3504
+ function fixedFromCharCode(code) {
3505
+ // String.prototype.fromCharCode does not supports
3506
+ // > 2 bytes unicode chars directly
3507
+ if (code > 0xffff) {
3508
+ code -= 0x10000;
3509
+ var surrogate1 = 0xd800 + (code >> 10)
3510
+ , surrogate2 = 0xdc00 + (code & 0x3ff);
3511
+
3512
+ return String.fromCharCode(surrogate1, surrogate2);
3513
+ } else {
3514
+ return String.fromCharCode(code);
3515
+ }
3516
+ }
3517
+ function entityReplacer(a){
3518
+ var k = a.slice(1,-1);
3519
+ if(k in entityMap){
3520
+ return entityMap[k];
3521
+ }else if(k.charAt(0) === '#'){
3522
+ return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x')))
3523
+ }else{
3524
+ errorHandler.error('entity not found:'+a);
3525
+ return a;
3526
+ }
3527
+ }
3528
+ function appendText(end){//has some bugs
3529
+ var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer);
3530
+ locator&&position(start);
3531
+ domBuilder.characters(xt,0,end-start);
3532
+ start = end
3533
+ }
3534
+ function position(start,m){
3535
+ while(start>=endPos && (m = linePattern.exec(source))){
3536
+ startPos = m.index;
3537
+ endPos = startPos + m[0].length;
3538
+ locator.lineNumber++;
3539
+ //console.log('line++:',locator,startPos,endPos)
3540
+ }
3541
+ locator.columnNumber = start-startPos+1;
3542
+ }
3543
+ var startPos = 0;
3544
+ var endPos = 0;
3545
+ var linePattern = /.+(?:\r\n?|\n)|.*$/g
3546
+ var locator = domBuilder.locator;
3547
+
3548
+ var parseStack = [{currentNSMap:defaultNSMapCopy}]
3549
+ var closeMap = {};
3550
+ var start = 0;
3551
+ while(true){
3552
+ var i = source.indexOf('<',start);
3553
+ if(i<0){
3554
+ if(!source.substr(start).match(/^\s*$/)){
3555
+ var doc = domBuilder.document;
3556
+ var text = doc.createTextNode(source.substr(start));
3557
+ doc.appendChild(text);
3558
+ domBuilder.currentElement = text;
3559
+ }
3560
+ return;
3561
+ }
3562
+ if(i>start){
3563
+ appendText(i);
3564
+ }
3565
+ switch(source.charAt(i+1)){
3566
+ case '/':
3567
+ var end = source.indexOf('>',i+3);
3568
+ var tagName = source.substring(i+2,end);
3569
+ var config = parseStack.pop();
3570
+ var localNSMap = config.localNSMap;
3571
+
3572
+ if(config.tagName != tagName){
3573
+ errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
3574
+ }
3575
+ domBuilder.endElement(config.uri,config.localName,tagName);
3576
+ if(localNSMap){
3577
+ for(var prefix in localNSMap){
3578
+ domBuilder.endPrefixMapping(prefix) ;
3579
+ }
3580
+ }
3581
+ end++;
3582
+ break;
3583
+ // end elment
3584
+ case '?':// <?...?>
3585
+ locator&&position(i);
3586
+ end = parseInstruction(source,i,domBuilder);
3587
+ break;
3588
+ case '!':// <!doctype,<![CDATA,<!--
3589
+ locator&&position(i);
3590
+ end = parseDCC(source,i,domBuilder,errorHandler);
3591
+ break;
3592
+ default:
3593
+ try{
3594
+ locator&&position(i);
3595
+
3596
+ var el = new ElementAttributes();
3597
+
3598
+ //elStartEnd
3599
+ var end = parseElementStartPart(source,i,el,entityReplacer,errorHandler);
3600
+ var len = el.length;
3601
+ //position fixed
3602
+ if(len && locator){
3603
+ var backup = copyLocator(locator,{});
3604
+ for(var i = 0;i<len;i++){
3605
+ var a = el[i];
3606
+ position(a.offset);
3607
+ a.offset = copyLocator(locator,{});
3608
+ }
3609
+ copyLocator(backup,locator);
3610
+ }
3611
+ if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
3612
+ el.closed = true;
3613
+ if(!entityMap.nbsp){
3614
+ errorHandler.warning('unclosed xml attribute');
3615
+ }
3616
+ }
3617
+ appendElement(el,domBuilder,parseStack);
3618
+
3619
+
3620
+ if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
3621
+ end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
3622
+ }else{
3623
+ end++;
3624
+ }
3625
+ }catch(e){
3626
+ errorHandler.error('element parse error: '+e);
3627
+ end = -1;
3628
+ }
3629
+
3630
+ }
3631
+ if(end<0){
3632
+ //TODO: 这里有可能sax回退,有位置错误风险
3633
+ appendText(i+1);
3634
+ }else{
3635
+ start = end;
3636
+ }
3637
+ }
3638
+ }
3639
+ function copyLocator(f,t){
3640
+ t.lineNumber = f.lineNumber;
3641
+ t.columnNumber = f.columnNumber;
3642
+ return t;
3643
+
3644
+ }
3645
+
3646
+ /**
3647
+ * @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
3648
+ * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
3649
+ */
3650
+ function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
3651
+ var attrName;
3652
+ var value;
3653
+ var p = ++start;
3654
+ var s = S_TAG;//status
3655
+ while(true){
3656
+ var c = source.charAt(p);
3657
+ switch(c){
3658
+ case '=':
3659
+ if(s === S_ATTR){//attrName
3660
+ attrName = source.slice(start,p);
3661
+ s = S_EQ;
3662
+ }else if(s === S_ATTR_S){
3663
+ s = S_EQ;
3664
+ }else{
3665
+ //fatalError: equal must after attrName or space after attrName
3666
+ throw new Error('attribute equal must after attrName');
3667
+ }
3668
+ break;
3669
+ case '\'':
3670
+ case '"':
3671
+ if(s === S_EQ){//equal
3672
+ start = p+1;
3673
+ p = source.indexOf(c,start)
3674
+ if(p>0){
3675
+ value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
3676
+ el.add(attrName,value,start-1);
3677
+ s = S_E;
3678
+ }else{
3679
+ //fatalError: no end quot match
3680
+ throw new Error('attribute value no end \''+c+'\' match');
3681
+ }
3682
+ }else if(s == S_V){
3683
+ value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
3684
+ //console.log(attrName,value,start,p)
3685
+ el.add(attrName,value,start);
3686
+ //console.dir(el)
3687
+ errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
3688
+ start = p+1;
3689
+ s = S_E
3690
+ }else{
3691
+ //fatalError: no equal before
3692
+ throw new Error('attribute value must after "="');
3693
+ }
3694
+ break;
3695
+ case '/':
3696
+ switch(s){
3697
+ case S_TAG:
3698
+ el.setTagName(source.slice(start,p));
3699
+ case S_E:
3700
+ case S_S:
3701
+ case S_C:
3702
+ s = S_C;
3703
+ el.closed = true;
3704
+ case S_V:
3705
+ case S_ATTR:
3706
+ case S_ATTR_S:
3707
+ break;
3708
+ //case S_EQ:
3709
+ default:
3710
+ throw new Error("attribute invalid close char('/')")
3711
+ }
3712
+ break;
3713
+ case ''://end document
3714
+ //throw new Error('unexpected end of input')
3715
+ errorHandler.error('unexpected end of input');
3716
+ case '>':
3717
+ switch(s){
3718
+ case S_TAG:
3719
+ el.setTagName(source.slice(start,p));
3720
+ case S_E:
3721
+ case S_S:
3722
+ case S_C:
3723
+ break;//normal
3724
+ case S_V://Compatible state
3725
+ case S_ATTR:
3726
+ value = source.slice(start,p);
3727
+ if(value.slice(-1) === '/'){
3728
+ el.closed = true;
3729
+ value = value.slice(0,-1)
3730
+ }
3731
+ case S_ATTR_S:
3732
+ if(s === S_ATTR_S){
3733
+ value = attrName;
3734
+ }
3735
+ if(s == S_V){
3736
+ errorHandler.warning('attribute "'+value+'" missed quot(")!!');
3737
+ el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
3738
+ }else{
3739
+ errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
3740
+ el.add(value,value,start)
3741
+ }
3742
+ break;
3743
+ case S_EQ:
3744
+ throw new Error('attribute value missed!!');
3745
+ }
3746
+ // console.log(tagName,tagNamePattern,tagNamePattern.test(tagName))
3747
+ return p;
3748
+ /*xml space '\x20' | #x9 | #xD | #xA; */
3749
+ case '\u0080':
3750
+ c = ' ';
3751
+ default:
3752
+ if(c<= ' '){//space
3753
+ switch(s){
3754
+ case S_TAG:
3755
+ el.setTagName(source.slice(start,p));//tagName
3756
+ s = S_S;
3757
+ break;
3758
+ case S_ATTR:
3759
+ attrName = source.slice(start,p)
3760
+ s = S_ATTR_S;
3761
+ break;
3762
+ case S_V:
3763
+ var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
3764
+ errorHandler.warning('attribute "'+value+'" missed quot(")!!');
3765
+ el.add(attrName,value,start)
3766
+ case S_E:
3767
+ s = S_S;
3768
+ break;
3769
+ //case S_S:
3770
+ //case S_EQ:
3771
+ //case S_ATTR_S:
3772
+ // void();break;
3773
+ //case S_C:
3774
+ //ignore warning
3775
+ }
3776
+ }else{//not space
3777
+ //S_TAG, S_ATTR, S_EQ, S_V
3778
+ //S_ATTR_S, S_E, S_S, S_C
3779
+ switch(s){
3780
+ //case S_TAG:void();break;
3781
+ //case S_ATTR:void();break;
3782
+ //case S_V:void();break;
3783
+ case S_ATTR_S:
3784
+ errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead!!')
3785
+ el.add(attrName,attrName,start);
3786
+ start = p;
3787
+ s = S_ATTR;
3788
+ break;
3789
+ case S_E:
3790
+ errorHandler.warning('attribute space is required"'+attrName+'"!!')
3791
+ case S_S:
3792
+ s = S_ATTR;
3793
+ start = p;
3794
+ break;
3795
+ case S_EQ:
3796
+ s = S_V;
3797
+ start = p;
3798
+ break;
3799
+ case S_C:
3800
+ throw new Error("elements closed character '/' and '>' must be connected to");
3801
+ }
3802
+ }
3803
+ }
3804
+ p++;
3805
+ }
3806
+ }
3807
+ /**
3808
+ * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
3809
+ */
3810
+ function appendElement(el,domBuilder,parseStack){
3811
+ var tagName = el.tagName;
3812
+ var localNSMap = null;
3813
+ var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
3814
+ var i = el.length;
3815
+ while(i--){
3816
+ var a = el[i];
3817
+ var qName = a.qName;
3818
+ var value = a.value;
3819
+ var nsp = qName.indexOf(':');
3820
+ if(nsp>0){
3821
+ var prefix = a.prefix = qName.slice(0,nsp);
3822
+ var localName = qName.slice(nsp+1);
3823
+ var nsPrefix = prefix === 'xmlns' && localName
3824
+ }else{
3825
+ localName = qName;
3826
+ prefix = null
3827
+ nsPrefix = qName === 'xmlns' && ''
3828
+ }
3829
+ //can not set prefix,because prefix !== ''
3830
+ a.localName = localName ;
3831
+ //prefix == null for no ns prefix attribute
3832
+ if(nsPrefix !== false){//hack!!
3833
+ if(localNSMap == null){
3834
+ localNSMap = {}
3835
+ //console.log(currentNSMap,0)
3836
+ _copy(currentNSMap,currentNSMap={})
3837
+ //console.log(currentNSMap,1)
3838
+ }
3839
+ currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value;
3840
+ a.uri = 'http://www.w3.org/2000/xmlns/'
3841
+ domBuilder.startPrefixMapping(nsPrefix, value)
3842
+ }
3843
+ }
3844
+ var i = el.length;
3845
+ while(i--){
3846
+ a = el[i];
3847
+ var prefix = a.prefix;
3848
+ if(prefix){//no prefix attribute has no namespace
3849
+ if(prefix === 'xml'){
3850
+ a.uri = 'http://www.w3.org/XML/1998/namespace';
3851
+ }if(prefix !== 'xmlns'){
3852
+ a.uri = currentNSMap[prefix]
3853
+
3854
+ //{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
3855
+ }
3856
+ }
3857
+ }
3858
+ var nsp = tagName.indexOf(':');
3859
+ if(nsp>0){
3860
+ prefix = el.prefix = tagName.slice(0,nsp);
3861
+ localName = el.localName = tagName.slice(nsp+1);
3862
+ }else{
3863
+ prefix = null;//important!!
3864
+ localName = el.localName = tagName;
3865
+ }
3866
+ //no prefix element has default namespace
3867
+ var ns = el.uri = currentNSMap[prefix || ''];
3868
+ domBuilder.startElement(ns,localName,tagName,el);
3869
+ //endPrefixMapping and startPrefixMapping have not any help for dom builder
3870
+ //localNSMap = null
3871
+ if(el.closed){
3872
+ domBuilder.endElement(ns,localName,tagName);
3873
+ if(localNSMap){
3874
+ for(prefix in localNSMap){
3875
+ domBuilder.endPrefixMapping(prefix)
3876
+ }
3877
+ }
3878
+ }else{
3879
+ el.currentNSMap = currentNSMap;
3880
+ el.localNSMap = localNSMap;
3881
+ parseStack.push(el);
3882
+ }
3883
+ }
3884
+ function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
3885
+ if(/^(?:script|textarea)$/i.test(tagName)){
3886
+ var elEndStart = source.indexOf('</'+tagName+'>',elStartEnd);
3887
+ var text = source.substring(elStartEnd+1,elEndStart);
3888
+ if(/[&<]/.test(text)){
3889
+ if(/^script$/i.test(tagName)){
3890
+ //if(!/\]\]>/.test(text)){
3891
+ //lexHandler.startCDATA();
3892
+ domBuilder.characters(text,0,text.length);
3893
+ //lexHandler.endCDATA();
3894
+ return elEndStart;
3895
+ //}
3896
+ }//}else{//text area
3897
+ text = text.replace(/&#?\w+;/g,entityReplacer);
3898
+ domBuilder.characters(text,0,text.length);
3899
+ return elEndStart;
3900
+ //}
3901
+
3902
+ }
3903
+ }
3904
+ return elStartEnd+1;
3905
+ }
3906
+ function fixSelfClosed(source,elStartEnd,tagName,closeMap){
3907
+ //if(tagName in closeMap){
3908
+ var pos = closeMap[tagName];
3909
+ if(pos == null){
3910
+ //console.log(tagName)
3911
+ pos = closeMap[tagName] = source.lastIndexOf('</'+tagName+'>')
3912
+ }
3913
+ return pos<elStartEnd;
3914
+ //}
3915
+ }
3916
+ function _copy(source,target){
3917
+ for(var n in source){target[n] = source[n]}
3918
+ }
3919
+ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
3920
+ var next= source.charAt(start+2)
3921
+ switch(next){
3922
+ case '-':
3923
+ if(source.charAt(start + 3) === '-'){
3924
+ var end = source.indexOf('-->',start+4);
3925
+ //append comment source.substring(4,end)//<!--
3926
+ if(end>start){
3927
+ domBuilder.comment(source,start+4,end-start-4);
3928
+ return end+3;
3929
+ }else{
3930
+ errorHandler.error("Unclosed comment");
3931
+ return -1;
3932
+ }
3933
+ }else{
3934
+ //error
3935
+ return -1;
3936
+ }
3937
+ default:
3938
+ if(source.substr(start+3,6) == 'CDATA['){
3939
+ var end = source.indexOf(']]>',start+9);
3940
+ domBuilder.startCDATA();
3941
+ domBuilder.characters(source,start+9,end-start-9);
3942
+ domBuilder.endCDATA()
3943
+ return end+3;
3944
+ }
3945
+ //<!DOCTYPE
3946
+ //startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
3947
+ var matchs = split(source,start);
3948
+ var len = matchs.length;
3949
+ if(len>1 && /!doctype/i.test(matchs[0][0])){
3950
+ var name = matchs[1][0];
3951
+ var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
3952
+ var sysid = len>4 && matchs[4][0];
3953
+ var lastMatch = matchs[len-1]
3954
+ domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
3955
+ sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
3956
+ domBuilder.endDTD();
3957
+
3958
+ return lastMatch.index+lastMatch[0].length
3959
+ }
3960
+ }
3961
+ return -1;
3962
+ }
3963
+
3964
+
3965
+
3966
+ function parseInstruction(source,start,domBuilder){
3967
+ var end = source.indexOf('?>',start);
3968
+ if(end){
3969
+ var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
3970
+ if(match){
3971
+ var len = match[0].length;
3972
+ domBuilder.processingInstruction(match[1], match[2]) ;
3973
+ return end+2;
3974
+ }else{//error
3975
+ return -1;
3976
+ }
3977
+ }
3978
+ return -1;
3979
+ }
3980
+
3981
+ /**
3982
+ * @param source
3983
+ */
3984
+ function ElementAttributes(source){
3985
+
3986
+ }
3987
+ ElementAttributes.prototype = {
3988
+ setTagName:function(tagName){
3989
+ if(!tagNamePattern.test(tagName)){
3990
+ throw new Error('invalid tagName:'+tagName)
3991
+ }
3992
+ this.tagName = tagName
3993
+ },
3994
+ add:function(qName,value,offset){
3995
+ if(!tagNamePattern.test(qName)){
3996
+ throw new Error('invalid attribute:'+qName)
3997
+ }
3998
+ this[this.length++] = {qName:qName,value:value,offset:offset}
3999
+ },
4000
+ length:0,
4001
+ getLocalName:function(i){return this[i].localName},
4002
+ getOffset:function(i){return this[i].offset},
4003
+ getQName:function(i){return this[i].qName},
4004
+ getURI:function(i){return this[i].uri},
4005
+ getValue:function(i){return this[i].value}
4006
+ // ,getIndex:function(uri, localName)){
4007
+ // if(localName){
4008
+ //
4009
+ // }else{
4010
+ // var qName = uri
4011
+ // }
4012
+ // },
4013
+ // getValue:function(){return this.getValue(this.getIndex.apply(this,arguments))},
4014
+ // getType:function(uri,localName){}
4015
+ // getType:function(i){},
4016
+ }
4017
+
4018
+
4019
+
4020
+
4021
+ function _set_proto_(thiz,parent){
4022
+ thiz.__proto__ = parent;
4023
+ return thiz;
4024
+ }
4025
+ if(!(_set_proto_({},_set_proto_.prototype) instanceof _set_proto_)){
4026
+ _set_proto_ = function(thiz,parent){
4027
+ function p(){};
4028
+ p.prototype = parent;
4029
+ p = new p();
4030
+ for(parent in thiz){
4031
+ p[parent] = thiz[parent];
4032
+ }
4033
+ return p;
4034
+ }
4035
+ }
4036
+
4037
+ function split(source,start){
4038
+ var match;
4039
+ var buf = [];
4040
+ var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
4041
+ reg.lastIndex = start;
4042
+ reg.exec(source);//skip <
4043
+ while(match = reg.exec(source)){
4044
+ buf.push(match);
4045
+ if(match[1])return buf;
4046
+ }
4047
+ }
4048
+
4049
+ if(typeof require == 'function'){
4050
+ exports.XMLReader = XMLReader;
4051
+ }
4052
+
4053
+
4054
+ },{}]},{},[1])(1)
4055
+ });