Newer
Older
CVSS_3.0_GUI / dist / CVSS_3.0_Calc-1.0.0-linux-x64 / node_modules / nwjs-builder-phoenix / node_modules / plist / dist / plist-parse.js
root on 7 May 2019 110 KB Initial commit
  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. DOMParser.prototype.parseFromString = function(source,mimeType){
  2076. var options = this.options;
  2077. var sax = new XMLReader();
  2078. var domBuilder = options.domBuilder || new DOMHandler();//contentHandler and LexicalHandler
  2079. var errorHandler = options.errorHandler;
  2080. var locator = options.locator;
  2081. var defaultNSMap = options.xmlns||{};
  2082. var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"}
  2083. if(locator){
  2084. domBuilder.setDocumentLocator(locator)
  2085. }
  2086. sax.errorHandler = buildErrorHandler(errorHandler,domBuilder,locator);
  2087. sax.domBuilder = options.domBuilder || domBuilder;
  2088. if(/\/x?html?$/.test(mimeType)){
  2089. entityMap.nbsp = '\xa0';
  2090. entityMap.copy = '\xa9';
  2091. defaultNSMap['']= 'http://www.w3.org/1999/xhtml';
  2092. }
  2093. if(source){
  2094. sax.parse(source,defaultNSMap,entityMap);
  2095. }else{
  2096. sax.errorHandler.error("invalid document source");
  2097. }
  2098. return domBuilder.document;
  2099. }
  2100. function buildErrorHandler(errorImpl,domBuilder,locator){
  2101. if(!errorImpl){
  2102. if(domBuilder instanceof DOMHandler){
  2103. return domBuilder;
  2104. }
  2105. errorImpl = domBuilder ;
  2106. }
  2107. var errorHandler = {}
  2108. var isCallback = errorImpl instanceof Function;
  2109. locator = locator||{}
  2110. function build(key){
  2111. var fn = errorImpl[key];
  2112. if(!fn){
  2113. if(isCallback){
  2114. fn = errorImpl.length == 2?function(msg){errorImpl(key,msg)}:errorImpl;
  2115. }else{
  2116. var i=arguments.length;
  2117. while(--i){
  2118. if(fn = errorImpl[arguments[i]]){
  2119. break;
  2120. }
  2121. }
  2122. }
  2123. }
  2124. errorHandler[key] = fn && function(msg){
  2125. fn(msg+_locator(locator));
  2126. }||function(){};
  2127. }
  2128. build('warning','warn');
  2129. build('error','warn','warning');
  2130. build('fatalError','warn','warning','error');
  2131. return errorHandler;
  2132. }
  2133. /**
  2134. * +ContentHandler+ErrorHandler
  2135. * +LexicalHandler+EntityResolver2
  2136. * -DeclHandler-DTDHandler
  2137. *
  2138. * DefaultHandler:EntityResolver, DTDHandler, ContentHandler, ErrorHandler
  2139. * DefaultHandler2:DefaultHandler,LexicalHandler, DeclHandler, EntityResolver2
  2140. * @link http://www.saxproject.org/apidoc/org/xml/sax/helpers/DefaultHandler.html
  2141. */
  2142. function DOMHandler() {
  2143. this.cdata = false;
  2144. }
  2145. function position(locator,node){
  2146. node.lineNumber = locator.lineNumber;
  2147. node.columnNumber = locator.columnNumber;
  2148. }
  2149. /**
  2150. * @see org.xml.sax.ContentHandler#startDocument
  2151. * @link http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html
  2152. */
  2153. DOMHandler.prototype = {
  2154. startDocument : function() {
  2155. this.document = new DOMImplementation().createDocument(null, null, null);
  2156. if (this.locator) {
  2157. this.document.documentURI = this.locator.systemId;
  2158. }
  2159. },
  2160. startElement:function(namespaceURI, localName, qName, attrs) {
  2161. var doc = this.document;
  2162. var el = doc.createElementNS(namespaceURI, qName||localName);
  2163. var len = attrs.length;
  2164. appendElement(this, el);
  2165. this.currentElement = el;
  2166. this.locator && position(this.locator,el)
  2167. for (var i = 0 ; i < len; i++) {
  2168. var namespaceURI = attrs.getURI(i);
  2169. var value = attrs.getValue(i);
  2170. var qName = attrs.getQName(i);
  2171. var attr = doc.createAttributeNS(namespaceURI, qName);
  2172. if( attr.getOffset){
  2173. position(attr.getOffset(1),attr)
  2174. }
  2175. attr.value = attr.nodeValue = value;
  2176. el.setAttributeNode(attr)
  2177. }
  2178. },
  2179. endElement:function(namespaceURI, localName, qName) {
  2180. var current = this.currentElement
  2181. var tagName = current.tagName;
  2182. this.currentElement = current.parentNode;
  2183. },
  2184. startPrefixMapping:function(prefix, uri) {
  2185. },
  2186. endPrefixMapping:function(prefix) {
  2187. },
  2188. processingInstruction:function(target, data) {
  2189. var ins = this.document.createProcessingInstruction(target, data);
  2190. this.locator && position(this.locator,ins)
  2191. appendElement(this, ins);
  2192. },
  2193. ignorableWhitespace:function(ch, start, length) {
  2194. },
  2195. characters:function(chars, start, length) {
  2196. chars = _toString.apply(this,arguments)
  2197. //console.log(chars)
  2198. if(this.currentElement && chars){
  2199. if (this.cdata) {
  2200. var charNode = this.document.createCDATASection(chars);
  2201. this.currentElement.appendChild(charNode);
  2202. } else {
  2203. var charNode = this.document.createTextNode(chars);
  2204. this.currentElement.appendChild(charNode);
  2205. }
  2206. this.locator && position(this.locator,charNode)
  2207. }
  2208. },
  2209. skippedEntity:function(name) {
  2210. },
  2211. endDocument:function() {
  2212. this.document.normalize();
  2213. },
  2214. setDocumentLocator:function (locator) {
  2215. if(this.locator = locator){// && !('lineNumber' in locator)){
  2216. locator.lineNumber = 0;
  2217. }
  2218. },
  2219. //LexicalHandler
  2220. comment:function(chars, start, length) {
  2221. chars = _toString.apply(this,arguments)
  2222. var comm = this.document.createComment(chars);
  2223. this.locator && position(this.locator,comm)
  2224. appendElement(this, comm);
  2225. },
  2226. startCDATA:function() {
  2227. //used in characters() methods
  2228. this.cdata = true;
  2229. },
  2230. endCDATA:function() {
  2231. this.cdata = false;
  2232. },
  2233. startDTD:function(name, publicId, systemId) {
  2234. var impl = this.document.implementation;
  2235. if (impl && impl.createDocumentType) {
  2236. var dt = impl.createDocumentType(name, publicId, systemId);
  2237. this.locator && position(this.locator,dt)
  2238. appendElement(this, dt);
  2239. }
  2240. },
  2241. /**
  2242. * @see org.xml.sax.ErrorHandler
  2243. * @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
  2244. */
  2245. warning:function(error) {
  2246. console.warn(error,_locator(this.locator));
  2247. },
  2248. error:function(error) {
  2249. console.error(error,_locator(this.locator));
  2250. },
  2251. fatalError:function(error) {
  2252. console.error(error,_locator(this.locator));
  2253. throw error;
  2254. }
  2255. }
  2256. function _locator(l){
  2257. if(l){
  2258. return '\n@'+(l.systemId ||'')+'#[line:'+l.lineNumber+',col:'+l.columnNumber+']'
  2259. }
  2260. }
  2261. function _toString(chars,start,length){
  2262. if(typeof chars == 'string'){
  2263. return chars.substr(start,length)
  2264. }else{//java sax connect width xmldom on rhino(what about: "? && !(chars instanceof String)")
  2265. if(chars.length >= start+length || start){
  2266. return new java.lang.String(chars,start,length)+'';
  2267. }
  2268. return chars;
  2269. }
  2270. }
  2271.  
  2272. /*
  2273. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/LexicalHandler.html
  2274. * used method of org.xml.sax.ext.LexicalHandler:
  2275. * #comment(chars, start, length)
  2276. * #startCDATA()
  2277. * #endCDATA()
  2278. * #startDTD(name, publicId, systemId)
  2279. *
  2280. *
  2281. * IGNORED method of org.xml.sax.ext.LexicalHandler:
  2282. * #endDTD()
  2283. * #startEntity(name)
  2284. * #endEntity(name)
  2285. *
  2286. *
  2287. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/DeclHandler.html
  2288. * IGNORED method of org.xml.sax.ext.DeclHandler
  2289. * #attributeDecl(eName, aName, type, mode, value)
  2290. * #elementDecl(name, model)
  2291. * #externalEntityDecl(name, publicId, systemId)
  2292. * #internalEntityDecl(name, value)
  2293. * @link http://www.saxproject.org/apidoc/org/xml/sax/ext/EntityResolver2.html
  2294. * IGNORED method of org.xml.sax.EntityResolver2
  2295. * #resolveEntity(String name,String publicId,String baseURI,String systemId)
  2296. * #resolveEntity(publicId, systemId)
  2297. * #getExternalSubset(name, baseURI)
  2298. * @link http://www.saxproject.org/apidoc/org/xml/sax/DTDHandler.html
  2299. * IGNORED method of org.xml.sax.DTDHandler
  2300. * #notationDecl(name, publicId, systemId) {};
  2301. * #unparsedEntityDecl(name, publicId, systemId, notationName) {};
  2302. */
  2303. "endDTD,startEntity,endEntity,attributeDecl,elementDecl,externalEntityDecl,internalEntityDecl,resolveEntity,getExternalSubset,notationDecl,unparsedEntityDecl".replace(/\w+/g,function(key){
  2304. DOMHandler.prototype[key] = function(){return null}
  2305. })
  2306.  
  2307. /* 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 */
  2308. function appendElement (hander,node) {
  2309. if (!hander.currentElement) {
  2310. hander.document.appendChild(node);
  2311. } else {
  2312. hander.currentElement.appendChild(node);
  2313. }
  2314. }//appendChild and setAttributeNS are preformance key
  2315.  
  2316. if(typeof require == 'function'){
  2317. var XMLReader = require('./sax').XMLReader;
  2318. var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
  2319. exports.XMLSerializer = require('./dom').XMLSerializer ;
  2320. exports.DOMParser = DOMParser;
  2321. }
  2322.  
  2323. },{"./dom":8,"./sax":9}],8:[function(require,module,exports){
  2324. /*
  2325. * DOM Level 2
  2326. * Object DOMException
  2327. * @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html
  2328. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
  2329. */
  2330.  
  2331. function copy(src,dest){
  2332. for(var p in src){
  2333. dest[p] = src[p];
  2334. }
  2335. }
  2336. /**
  2337. ^\w+\.prototype\.([_\w]+)\s*=\s*((?:.*\{\s*?[\r\n][\s\S]*?^})|\S.*?(?=[;\r\n]));?
  2338. ^\w+\.prototype\.([_\w]+)\s*=\s*(\S.*?(?=[;\r\n]));?
  2339. */
  2340. function _extends(Class,Super){
  2341. var pt = Class.prototype;
  2342. if(Object.create){
  2343. var ppt = Object.create(Super.prototype)
  2344. pt.__proto__ = ppt;
  2345. }
  2346. if(!(pt instanceof Super)){
  2347. function t(){};
  2348. t.prototype = Super.prototype;
  2349. t = new t();
  2350. copy(pt,t);
  2351. Class.prototype = pt = t;
  2352. }
  2353. if(pt.constructor != Class){
  2354. if(typeof Class != 'function'){
  2355. console.error("unknow Class:"+Class)
  2356. }
  2357. pt.constructor = Class
  2358. }
  2359. }
  2360. var htmlns = 'http://www.w3.org/1999/xhtml' ;
  2361. // Node Types
  2362. var NodeType = {}
  2363. var ELEMENT_NODE = NodeType.ELEMENT_NODE = 1;
  2364. var ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE = 2;
  2365. var TEXT_NODE = NodeType.TEXT_NODE = 3;
  2366. var CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE = 4;
  2367. var ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE = 5;
  2368. var ENTITY_NODE = NodeType.ENTITY_NODE = 6;
  2369. var PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE = 7;
  2370. var COMMENT_NODE = NodeType.COMMENT_NODE = 8;
  2371. var DOCUMENT_NODE = NodeType.DOCUMENT_NODE = 9;
  2372. var DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE = 10;
  2373. var DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE = 11;
  2374. var NOTATION_NODE = NodeType.NOTATION_NODE = 12;
  2375.  
  2376. // ExceptionCode
  2377. var ExceptionCode = {}
  2378. var ExceptionMessage = {};
  2379. var INDEX_SIZE_ERR = ExceptionCode.INDEX_SIZE_ERR = ((ExceptionMessage[1]="Index size error"),1);
  2380. var DOMSTRING_SIZE_ERR = ExceptionCode.DOMSTRING_SIZE_ERR = ((ExceptionMessage[2]="DOMString size error"),2);
  2381. var HIERARCHY_REQUEST_ERR = ExceptionCode.HIERARCHY_REQUEST_ERR = ((ExceptionMessage[3]="Hierarchy request error"),3);
  2382. var WRONG_DOCUMENT_ERR = ExceptionCode.WRONG_DOCUMENT_ERR = ((ExceptionMessage[4]="Wrong document"),4);
  2383. var INVALID_CHARACTER_ERR = ExceptionCode.INVALID_CHARACTER_ERR = ((ExceptionMessage[5]="Invalid character"),5);
  2384. var NO_DATA_ALLOWED_ERR = ExceptionCode.NO_DATA_ALLOWED_ERR = ((ExceptionMessage[6]="No data allowed"),6);
  2385. var NO_MODIFICATION_ALLOWED_ERR = ExceptionCode.NO_MODIFICATION_ALLOWED_ERR = ((ExceptionMessage[7]="No modification allowed"),7);
  2386. var NOT_FOUND_ERR = ExceptionCode.NOT_FOUND_ERR = ((ExceptionMessage[8]="Not found"),8);
  2387. var NOT_SUPPORTED_ERR = ExceptionCode.NOT_SUPPORTED_ERR = ((ExceptionMessage[9]="Not supported"),9);
  2388. var INUSE_ATTRIBUTE_ERR = ExceptionCode.INUSE_ATTRIBUTE_ERR = ((ExceptionMessage[10]="Attribute in use"),10);
  2389. //level2
  2390. var INVALID_STATE_ERR = ExceptionCode.INVALID_STATE_ERR = ((ExceptionMessage[11]="Invalid state"),11);
  2391. var SYNTAX_ERR = ExceptionCode.SYNTAX_ERR = ((ExceptionMessage[12]="Syntax error"),12);
  2392. var INVALID_MODIFICATION_ERR = ExceptionCode.INVALID_MODIFICATION_ERR = ((ExceptionMessage[13]="Invalid modification"),13);
  2393. var NAMESPACE_ERR = ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14]="Invalid namespace"),14);
  2394. var INVALID_ACCESS_ERR = ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15]="Invalid access"),15);
  2395.  
  2396.  
  2397. function DOMException(code, message) {
  2398. if(message instanceof Error){
  2399. var error = message;
  2400. }else{
  2401. error = this;
  2402. Error.call(this, ExceptionMessage[code]);
  2403. this.message = ExceptionMessage[code];
  2404. if(Error.captureStackTrace) Error.captureStackTrace(this, DOMException);
  2405. }
  2406. error.code = code;
  2407. if(message) this.message = this.message + ": " + message;
  2408. return error;
  2409. };
  2410. DOMException.prototype = Error.prototype;
  2411. copy(ExceptionCode,DOMException)
  2412. /**
  2413. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177
  2414. * 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.
  2415. * The items in the NodeList are accessible via an integral index, starting from 0.
  2416. */
  2417. function NodeList() {
  2418. };
  2419. NodeList.prototype = {
  2420. /**
  2421. * The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
  2422. * @standard level1
  2423. */
  2424. length:0,
  2425. /**
  2426. * 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.
  2427. * @standard level1
  2428. * @param index unsigned long
  2429. * Index into the collection.
  2430. * @return Node
  2431. * The node at the indexth position in the NodeList, or null if that is not a valid index.
  2432. */
  2433. item: function(index) {
  2434. return this[index] || null;
  2435. }
  2436. };
  2437. function LiveNodeList(node,refresh){
  2438. this._node = node;
  2439. this._refresh = refresh
  2440. _updateLiveList(this);
  2441. }
  2442. function _updateLiveList(list){
  2443. var inc = list._node._inc || list._node.ownerDocument._inc;
  2444. if(list._inc != inc){
  2445. var ls = list._refresh(list._node);
  2446. //console.log(ls.length)
  2447. __set__(list,'length',ls.length);
  2448. copy(ls,list);
  2449. list._inc = inc;
  2450. }
  2451. }
  2452. LiveNodeList.prototype.item = function(i){
  2453. _updateLiveList(this);
  2454. return this[i];
  2455. }
  2456.  
  2457. _extends(LiveNodeList,NodeList);
  2458. /**
  2459. *
  2460. * 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.
  2461. * NamedNodeMap objects in the DOM are live.
  2462. * used for attributes or DocumentType entities
  2463. */
  2464. function NamedNodeMap() {
  2465. };
  2466.  
  2467. function _findNodeIndex(list,node){
  2468. var i = list.length;
  2469. while(i--){
  2470. if(list[i] === node){return i}
  2471. }
  2472. }
  2473.  
  2474. function _addNamedNode(el,list,newAttr,oldAttr){
  2475. if(oldAttr){
  2476. list[_findNodeIndex(list,oldAttr)] = newAttr;
  2477. }else{
  2478. list[list.length++] = newAttr;
  2479. }
  2480. if(el){
  2481. newAttr.ownerElement = el;
  2482. var doc = el.ownerDocument;
  2483. if(doc){
  2484. oldAttr && _onRemoveAttribute(doc,el,oldAttr);
  2485. _onAddAttribute(doc,el,newAttr);
  2486. }
  2487. }
  2488. }
  2489. function _removeNamedNode(el,list,attr){
  2490. var i = _findNodeIndex(list,attr);
  2491. if(i>=0){
  2492. var lastIndex = list.length-1
  2493. while(i<lastIndex){
  2494. list[i] = list[++i]
  2495. }
  2496. list.length = lastIndex;
  2497. if(el){
  2498. var doc = el.ownerDocument;
  2499. if(doc){
  2500. _onRemoveAttribute(doc,el,attr);
  2501. attr.ownerElement = null;
  2502. }
  2503. }
  2504. }else{
  2505. throw DOMException(NOT_FOUND_ERR,new Error())
  2506. }
  2507. }
  2508. NamedNodeMap.prototype = {
  2509. length:0,
  2510. item:NodeList.prototype.item,
  2511. getNamedItem: function(key) {
  2512. // if(key.indexOf(':')>0 || key == 'xmlns'){
  2513. // return null;
  2514. // }
  2515. var i = this.length;
  2516. while(i--){
  2517. var attr = this[i];
  2518. if(attr.nodeName == key){
  2519. return attr;
  2520. }
  2521. }
  2522. },
  2523. setNamedItem: function(attr) {
  2524. var el = attr.ownerElement;
  2525. if(el && el!=this._ownerElement){
  2526. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  2527. }
  2528. var oldAttr = this.getNamedItem(attr.nodeName);
  2529. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  2530. return oldAttr;
  2531. },
  2532. /* returns Node */
  2533. setNamedItemNS: function(attr) {// raises: WRONG_DOCUMENT_ERR,NO_MODIFICATION_ALLOWED_ERR,INUSE_ATTRIBUTE_ERR
  2534. var el = attr.ownerElement, oldAttr;
  2535. if(el && el!=this._ownerElement){
  2536. throw new DOMException(INUSE_ATTRIBUTE_ERR);
  2537. }
  2538. oldAttr = this.getNamedItemNS(attr.namespaceURI,attr.localName);
  2539. _addNamedNode(this._ownerElement,this,attr,oldAttr);
  2540. return oldAttr;
  2541. },
  2542.  
  2543. /* returns Node */
  2544. removeNamedItem: function(key) {
  2545. var attr = this.getNamedItem(key);
  2546. _removeNamedNode(this._ownerElement,this,attr);
  2547. return attr;
  2548. },// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
  2549. //for level2
  2550. removeNamedItemNS:function(namespaceURI,localName){
  2551. var attr = this.getNamedItemNS(namespaceURI,localName);
  2552. _removeNamedNode(this._ownerElement,this,attr);
  2553. return attr;
  2554. },
  2555. getNamedItemNS: function(namespaceURI, localName) {
  2556. var i = this.length;
  2557. while(i--){
  2558. var node = this[i];
  2559. if(node.localName == localName && node.namespaceURI == namespaceURI){
  2560. return node;
  2561. }
  2562. }
  2563. return null;
  2564. }
  2565. };
  2566. /**
  2567. * @see http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-102161490
  2568. */
  2569. function DOMImplementation(/* Object */ features) {
  2570. this._features = {};
  2571. if (features) {
  2572. for (var feature in features) {
  2573. this._features = features[feature];
  2574. }
  2575. }
  2576. };
  2577.  
  2578. DOMImplementation.prototype = {
  2579. hasFeature: function(/* string */ feature, /* string */ version) {
  2580. var versions = this._features[feature.toLowerCase()];
  2581. if (versions && (!version || version in versions)) {
  2582. return true;
  2583. } else {
  2584. return false;
  2585. }
  2586. },
  2587. // Introduced in DOM Level 2:
  2588. createDocument:function(namespaceURI, qualifiedName, doctype){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR,WRONG_DOCUMENT_ERR
  2589. var doc = new Document();
  2590. doc.doctype = doctype;
  2591. if(doctype){
  2592. doc.appendChild(doctype);
  2593. }
  2594. doc.implementation = this;
  2595. doc.childNodes = new NodeList();
  2596. if(qualifiedName){
  2597. var root = doc.createElementNS(namespaceURI,qualifiedName);
  2598. doc.appendChild(root);
  2599. }
  2600. return doc;
  2601. },
  2602. // Introduced in DOM Level 2:
  2603. createDocumentType:function(qualifiedName, publicId, systemId){// raises:INVALID_CHARACTER_ERR,NAMESPACE_ERR
  2604. var node = new DocumentType();
  2605. node.name = qualifiedName;
  2606. node.nodeName = qualifiedName;
  2607. node.publicId = publicId;
  2608. node.systemId = systemId;
  2609. // Introduced in DOM Level 2:
  2610. //readonly attribute DOMString internalSubset;
  2611. //TODO:..
  2612. // readonly attribute NamedNodeMap entities;
  2613. // readonly attribute NamedNodeMap notations;
  2614. return node;
  2615. }
  2616. };
  2617.  
  2618.  
  2619. /**
  2620. * @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
  2621. */
  2622.  
  2623. function Node() {
  2624. };
  2625.  
  2626. Node.prototype = {
  2627. firstChild : null,
  2628. lastChild : null,
  2629. previousSibling : null,
  2630. nextSibling : null,
  2631. attributes : null,
  2632. parentNode : null,
  2633. childNodes : null,
  2634. ownerDocument : null,
  2635. nodeValue : null,
  2636. namespaceURI : null,
  2637. prefix : null,
  2638. localName : null,
  2639. // Modified in DOM Level 2:
  2640. insertBefore:function(newChild, refChild){//raises
  2641. return _insertBefore(this,newChild,refChild);
  2642. },
  2643. replaceChild:function(newChild, oldChild){//raises
  2644. this.insertBefore(newChild,oldChild);
  2645. if(oldChild){
  2646. this.removeChild(oldChild);
  2647. }
  2648. },
  2649. removeChild:function(oldChild){
  2650. return _removeChild(this,oldChild);
  2651. },
  2652. appendChild:function(newChild){
  2653. return this.insertBefore(newChild,null);
  2654. },
  2655. hasChildNodes:function(){
  2656. return this.firstChild != null;
  2657. },
  2658. cloneNode:function(deep){
  2659. return cloneNode(this.ownerDocument||this,this,deep);
  2660. },
  2661. // Modified in DOM Level 2:
  2662. normalize:function(){
  2663. var child = this.firstChild;
  2664. while(child){
  2665. var next = child.nextSibling;
  2666. if(next && next.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE){
  2667. this.removeChild(next);
  2668. child.appendData(next.data);
  2669. }else{
  2670. child.normalize();
  2671. child = next;
  2672. }
  2673. }
  2674. },
  2675. // Introduced in DOM Level 2:
  2676. isSupported:function(feature, version){
  2677. return this.ownerDocument.implementation.hasFeature(feature,version);
  2678. },
  2679. // Introduced in DOM Level 2:
  2680. hasAttributes:function(){
  2681. return this.attributes.length>0;
  2682. },
  2683. lookupPrefix:function(namespaceURI){
  2684. var el = this;
  2685. while(el){
  2686. var map = el._nsMap;
  2687. //console.dir(map)
  2688. if(map){
  2689. for(var n in map){
  2690. if(map[n] == namespaceURI){
  2691. return n;
  2692. }
  2693. }
  2694. }
  2695. el = el.nodeType == 2?el.ownerDocument : el.parentNode;
  2696. }
  2697. return null;
  2698. },
  2699. // Introduced in DOM Level 3:
  2700. lookupNamespaceURI:function(prefix){
  2701. var el = this;
  2702. while(el){
  2703. var map = el._nsMap;
  2704. //console.dir(map)
  2705. if(map){
  2706. if(prefix in map){
  2707. return map[prefix] ;
  2708. }
  2709. }
  2710. el = el.nodeType == 2?el.ownerDocument : el.parentNode;
  2711. }
  2712. return null;
  2713. },
  2714. // Introduced in DOM Level 3:
  2715. isDefaultNamespace:function(namespaceURI){
  2716. var prefix = this.lookupPrefix(namespaceURI);
  2717. return prefix == null;
  2718. }
  2719. };
  2720.  
  2721.  
  2722. function _xmlEncoder(c){
  2723. return c == '<' && '&lt;' ||
  2724. c == '>' && '&gt;' ||
  2725. c == '&' && '&amp;' ||
  2726. c == '"' && '&quot;' ||
  2727. '&#'+c.charCodeAt()+';'
  2728. }
  2729.  
  2730.  
  2731. copy(NodeType,Node);
  2732. copy(NodeType,Node.prototype);
  2733.  
  2734. /**
  2735. * @param callback return true for continue,false for break
  2736. * @return boolean true: break visit;
  2737. */
  2738. function _visitNode(node,callback){
  2739. if(callback(node)){
  2740. return true;
  2741. }
  2742. if(node = node.firstChild){
  2743. do{
  2744. if(_visitNode(node,callback)){return true}
  2745. }while(node=node.nextSibling)
  2746. }
  2747. }
  2748.  
  2749.  
  2750.  
  2751. function Document(){
  2752. }
  2753. function _onAddAttribute(doc,el,newAttr){
  2754. doc && doc._inc++;
  2755. var ns = newAttr.namespaceURI ;
  2756. if(ns == 'http://www.w3.org/2000/xmlns/'){
  2757. //update namespace
  2758. el._nsMap[newAttr.prefix?newAttr.localName:''] = newAttr.value
  2759. }
  2760. }
  2761. function _onRemoveAttribute(doc,el,newAttr,remove){
  2762. doc && doc._inc++;
  2763. var ns = newAttr.namespaceURI ;
  2764. if(ns == 'http://www.w3.org/2000/xmlns/'){
  2765. //update namespace
  2766. delete el._nsMap[newAttr.prefix?newAttr.localName:'']
  2767. }
  2768. }
  2769. function _onUpdateChild(doc,el,newChild){
  2770. if(doc && doc._inc){
  2771. doc._inc++;
  2772. //update childNodes
  2773. var cs = el.childNodes;
  2774. if(newChild){
  2775. cs[cs.length++] = newChild;
  2776. }else{
  2777. //console.log(1)
  2778. var child = el.firstChild;
  2779. var i = 0;
  2780. while(child){
  2781. cs[i++] = child;
  2782. child =child.nextSibling;
  2783. }
  2784. cs.length = i;
  2785. }
  2786. }
  2787. }
  2788.  
  2789. /**
  2790. * attributes;
  2791. * children;
  2792. *
  2793. * writeable properties:
  2794. * nodeValue,Attr:value,CharacterData:data
  2795. * prefix
  2796. */
  2797. function _removeChild(parentNode,child){
  2798. var previous = child.previousSibling;
  2799. var next = child.nextSibling;
  2800. if(previous){
  2801. previous.nextSibling = next;
  2802. }else{
  2803. parentNode.firstChild = next
  2804. }
  2805. if(next){
  2806. next.previousSibling = previous;
  2807. }else{
  2808. parentNode.lastChild = previous;
  2809. }
  2810. _onUpdateChild(parentNode.ownerDocument,parentNode);
  2811. return child;
  2812. }
  2813. /**
  2814. * preformance key(refChild == null)
  2815. */
  2816. function _insertBefore(parentNode,newChild,nextChild){
  2817. var cp = newChild.parentNode;
  2818. if(cp){
  2819. cp.removeChild(newChild);//remove and update
  2820. }
  2821. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  2822. var newFirst = newChild.firstChild;
  2823. if (newFirst == null) {
  2824. return newChild;
  2825. }
  2826. var newLast = newChild.lastChild;
  2827. }else{
  2828. newFirst = newLast = newChild;
  2829. }
  2830. var pre = nextChild ? nextChild.previousSibling : parentNode.lastChild;
  2831.  
  2832. newFirst.previousSibling = pre;
  2833. newLast.nextSibling = nextChild;
  2834. if(pre){
  2835. pre.nextSibling = newFirst;
  2836. }else{
  2837. parentNode.firstChild = newFirst;
  2838. }
  2839. if(nextChild == null){
  2840. parentNode.lastChild = newLast;
  2841. }else{
  2842. nextChild.previousSibling = newLast;
  2843. }
  2844. do{
  2845. newFirst.parentNode = parentNode;
  2846. }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
  2847. _onUpdateChild(parentNode.ownerDocument||parentNode,parentNode);
  2848. //console.log(parentNode.lastChild.nextSibling == null)
  2849. if (newChild.nodeType == DOCUMENT_FRAGMENT_NODE) {
  2850. newChild.firstChild = newChild.lastChild = null;
  2851. }
  2852. return newChild;
  2853. }
  2854. function _appendSingleChild(parentNode,newChild){
  2855. var cp = newChild.parentNode;
  2856. if(cp){
  2857. var pre = parentNode.lastChild;
  2858. cp.removeChild(newChild);//remove and update
  2859. var pre = parentNode.lastChild;
  2860. }
  2861. var pre = parentNode.lastChild;
  2862. newChild.parentNode = parentNode;
  2863. newChild.previousSibling = pre;
  2864. newChild.nextSibling = null;
  2865. if(pre){
  2866. pre.nextSibling = newChild;
  2867. }else{
  2868. parentNode.firstChild = newChild;
  2869. }
  2870. parentNode.lastChild = newChild;
  2871. _onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
  2872. return newChild;
  2873. //console.log("__aa",parentNode.lastChild.nextSibling == null)
  2874. }
  2875. Document.prototype = {
  2876. //implementation : null,
  2877. nodeName : '#document',
  2878. nodeType : DOCUMENT_NODE,
  2879. doctype : null,
  2880. documentElement : null,
  2881. _inc : 1,
  2882. insertBefore : function(newChild, refChild){//raises
  2883. if(newChild.nodeType == DOCUMENT_FRAGMENT_NODE){
  2884. var child = newChild.firstChild;
  2885. while(child){
  2886. var next = child.nextSibling;
  2887. this.insertBefore(child,refChild);
  2888. child = next;
  2889. }
  2890. return newChild;
  2891. }
  2892. if(this.documentElement == null && newChild.nodeType == 1){
  2893. this.documentElement = newChild;
  2894. }
  2895. return _insertBefore(this,newChild,refChild),(newChild.ownerDocument = this),newChild;
  2896. },
  2897. removeChild : function(oldChild){
  2898. if(this.documentElement == oldChild){
  2899. this.documentElement = null;
  2900. }
  2901. return _removeChild(this,oldChild);
  2902. },
  2903. // Introduced in DOM Level 2:
  2904. importNode : function(importedNode,deep){
  2905. return importNode(this,importedNode,deep);
  2906. },
  2907. // Introduced in DOM Level 2:
  2908. getElementById : function(id){
  2909. var rtv = null;
  2910. _visitNode(this.documentElement,function(node){
  2911. if(node.nodeType == 1){
  2912. if(node.getAttribute('id') == id){
  2913. rtv = node;
  2914. return true;
  2915. }
  2916. }
  2917. })
  2918. return rtv;
  2919. },
  2920. //document factory method:
  2921. createElement : function(tagName){
  2922. var node = new Element();
  2923. node.ownerDocument = this;
  2924. node.nodeName = tagName;
  2925. node.tagName = tagName;
  2926. node.childNodes = new NodeList();
  2927. var attrs = node.attributes = new NamedNodeMap();
  2928. attrs._ownerElement = node;
  2929. return node;
  2930. },
  2931. createDocumentFragment : function(){
  2932. var node = new DocumentFragment();
  2933. node.ownerDocument = this;
  2934. node.childNodes = new NodeList();
  2935. return node;
  2936. },
  2937. createTextNode : function(data){
  2938. var node = new Text();
  2939. node.ownerDocument = this;
  2940. node.appendData(data)
  2941. return node;
  2942. },
  2943. createComment : function(data){
  2944. var node = new Comment();
  2945. node.ownerDocument = this;
  2946. node.appendData(data)
  2947. return node;
  2948. },
  2949. createCDATASection : function(data){
  2950. var node = new CDATASection();
  2951. node.ownerDocument = this;
  2952. node.appendData(data)
  2953. return node;
  2954. },
  2955. createProcessingInstruction : function(target,data){
  2956. var node = new ProcessingInstruction();
  2957. node.ownerDocument = this;
  2958. node.tagName = node.target = target;
  2959. node.nodeValue= node.data = data;
  2960. return node;
  2961. },
  2962. createAttribute : function(name){
  2963. var node = new Attr();
  2964. node.ownerDocument = this;
  2965. node.name = name;
  2966. node.nodeName = name;
  2967. node.localName = name;
  2968. node.specified = true;
  2969. return node;
  2970. },
  2971. createEntityReference : function(name){
  2972. var node = new EntityReference();
  2973. node.ownerDocument = this;
  2974. node.nodeName = name;
  2975. return node;
  2976. },
  2977. // Introduced in DOM Level 2:
  2978. createElementNS : function(namespaceURI,qualifiedName){
  2979. var node = new Element();
  2980. var pl = qualifiedName.split(':');
  2981. var attrs = node.attributes = new NamedNodeMap();
  2982. node.childNodes = new NodeList();
  2983. node.ownerDocument = this;
  2984. node.nodeName = qualifiedName;
  2985. node.tagName = qualifiedName;
  2986. node.namespaceURI = namespaceURI;
  2987. if(pl.length == 2){
  2988. node.prefix = pl[0];
  2989. node.localName = pl[1];
  2990. }else{
  2991. //el.prefix = null;
  2992. node.localName = qualifiedName;
  2993. }
  2994. attrs._ownerElement = node;
  2995. return node;
  2996. },
  2997. // Introduced in DOM Level 2:
  2998. createAttributeNS : function(namespaceURI,qualifiedName){
  2999. var node = new Attr();
  3000. var pl = qualifiedName.split(':');
  3001. node.ownerDocument = this;
  3002. node.nodeName = qualifiedName;
  3003. node.name = qualifiedName;
  3004. node.namespaceURI = namespaceURI;
  3005. node.specified = true;
  3006. if(pl.length == 2){
  3007. node.prefix = pl[0];
  3008. node.localName = pl[1];
  3009. }else{
  3010. //el.prefix = null;
  3011. node.localName = qualifiedName;
  3012. }
  3013. return node;
  3014. }
  3015. };
  3016. _extends(Document,Node);
  3017.  
  3018.  
  3019. function Element() {
  3020. this._nsMap = {};
  3021. };
  3022. Element.prototype = {
  3023. nodeType : ELEMENT_NODE,
  3024. hasAttribute : function(name){
  3025. return this.getAttributeNode(name)!=null;
  3026. },
  3027. getAttribute : function(name){
  3028. var attr = this.getAttributeNode(name);
  3029. return attr && attr.value || '';
  3030. },
  3031. getAttributeNode : function(name){
  3032. return this.attributes.getNamedItem(name);
  3033. },
  3034. setAttribute : function(name, value){
  3035. var attr = this.ownerDocument.createAttribute(name);
  3036. attr.value = attr.nodeValue = "" + value;
  3037. this.setAttributeNode(attr)
  3038. },
  3039. removeAttribute : function(name){
  3040. var attr = this.getAttributeNode(name)
  3041. attr && this.removeAttributeNode(attr);
  3042. },
  3043. //four real opeartion method
  3044. appendChild:function(newChild){
  3045. if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
  3046. return this.insertBefore(newChild,null);
  3047. }else{
  3048. return _appendSingleChild(this,newChild);
  3049. }
  3050. },
  3051. setAttributeNode : function(newAttr){
  3052. return this.attributes.setNamedItem(newAttr);
  3053. },
  3054. setAttributeNodeNS : function(newAttr){
  3055. return this.attributes.setNamedItemNS(newAttr);
  3056. },
  3057. removeAttributeNode : function(oldAttr){
  3058. return this.attributes.removeNamedItem(oldAttr.nodeName);
  3059. },
  3060. //get real attribute name,and remove it by removeAttributeNode
  3061. removeAttributeNS : function(namespaceURI, localName){
  3062. var old = this.getAttributeNodeNS(namespaceURI, localName);
  3063. old && this.removeAttributeNode(old);
  3064. },
  3065. hasAttributeNS : function(namespaceURI, localName){
  3066. return this.getAttributeNodeNS(namespaceURI, localName)!=null;
  3067. },
  3068. getAttributeNS : function(namespaceURI, localName){
  3069. var attr = this.getAttributeNodeNS(namespaceURI, localName);
  3070. return attr && attr.value || '';
  3071. },
  3072. setAttributeNS : function(namespaceURI, qualifiedName, value){
  3073. var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName);
  3074. attr.value = attr.nodeValue = value;
  3075. this.setAttributeNode(attr)
  3076. },
  3077. getAttributeNodeNS : function(namespaceURI, localName){
  3078. return this.attributes.getNamedItemNS(namespaceURI, localName);
  3079. },
  3080. getElementsByTagName : function(tagName){
  3081. return new LiveNodeList(this,function(base){
  3082. var ls = [];
  3083. _visitNode(base,function(node){
  3084. if(node !== base && node.nodeType == ELEMENT_NODE && (tagName === '*' || node.tagName == tagName)){
  3085. ls.push(node);
  3086. }
  3087. });
  3088. return ls;
  3089. });
  3090. },
  3091. getElementsByTagNameNS : function(namespaceURI, localName){
  3092. return new LiveNodeList(this,function(base){
  3093. var ls = [];
  3094. _visitNode(base,function(node){
  3095. if(node !== base && node.nodeType === ELEMENT_NODE && node.namespaceURI === namespaceURI && (localName === '*' || node.localName == localName)){
  3096. ls.push(node);
  3097. }
  3098. });
  3099. return ls;
  3100. });
  3101. }
  3102. };
  3103. Document.prototype.getElementsByTagName = Element.prototype.getElementsByTagName;
  3104. Document.prototype.getElementsByTagNameNS = Element.prototype.getElementsByTagNameNS;
  3105.  
  3106.  
  3107. _extends(Element,Node);
  3108. function Attr() {
  3109. };
  3110. Attr.prototype.nodeType = ATTRIBUTE_NODE;
  3111. _extends(Attr,Node);
  3112.  
  3113.  
  3114. function CharacterData() {
  3115. };
  3116. CharacterData.prototype = {
  3117. data : '',
  3118. substringData : function(offset, count) {
  3119. return this.data.substring(offset, offset+count);
  3120. },
  3121. appendData: function(text) {
  3122. text = this.data+text;
  3123. this.nodeValue = this.data = text;
  3124. this.length = text.length;
  3125. },
  3126. insertData: function(offset,text) {
  3127. this.replaceData(offset,0,text);
  3128. },
  3129. appendChild:function(newChild){
  3130. //if(!(newChild instanceof CharacterData)){
  3131. throw new Error(ExceptionMessage[3])
  3132. //}
  3133. return Node.prototype.appendChild.apply(this,arguments)
  3134. },
  3135. deleteData: function(offset, count) {
  3136. this.replaceData(offset,count,"");
  3137. },
  3138. replaceData: function(offset, count, text) {
  3139. var start = this.data.substring(0,offset);
  3140. var end = this.data.substring(offset+count);
  3141. text = start + text + end;
  3142. this.nodeValue = this.data = text;
  3143. this.length = text.length;
  3144. }
  3145. }
  3146. _extends(CharacterData,Node);
  3147. function Text() {
  3148. };
  3149. Text.prototype = {
  3150. nodeName : "#text",
  3151. nodeType : TEXT_NODE,
  3152. splitText : function(offset) {
  3153. var text = this.data;
  3154. var newText = text.substring(offset);
  3155. text = text.substring(0, offset);
  3156. this.data = this.nodeValue = text;
  3157. this.length = text.length;
  3158. var newNode = this.ownerDocument.createTextNode(newText);
  3159. if(this.parentNode){
  3160. this.parentNode.insertBefore(newNode, this.nextSibling);
  3161. }
  3162. return newNode;
  3163. }
  3164. }
  3165. _extends(Text,CharacterData);
  3166. function Comment() {
  3167. };
  3168. Comment.prototype = {
  3169. nodeName : "#comment",
  3170. nodeType : COMMENT_NODE
  3171. }
  3172. _extends(Comment,CharacterData);
  3173.  
  3174. function CDATASection() {
  3175. };
  3176. CDATASection.prototype = {
  3177. nodeName : "#cdata-section",
  3178. nodeType : CDATA_SECTION_NODE
  3179. }
  3180. _extends(CDATASection,CharacterData);
  3181.  
  3182.  
  3183. function DocumentType() {
  3184. };
  3185. DocumentType.prototype.nodeType = DOCUMENT_TYPE_NODE;
  3186. _extends(DocumentType,Node);
  3187.  
  3188. function Notation() {
  3189. };
  3190. Notation.prototype.nodeType = NOTATION_NODE;
  3191. _extends(Notation,Node);
  3192.  
  3193. function Entity() {
  3194. };
  3195. Entity.prototype.nodeType = ENTITY_NODE;
  3196. _extends(Entity,Node);
  3197.  
  3198. function EntityReference() {
  3199. };
  3200. EntityReference.prototype.nodeType = ENTITY_REFERENCE_NODE;
  3201. _extends(EntityReference,Node);
  3202.  
  3203. function DocumentFragment() {
  3204. };
  3205. DocumentFragment.prototype.nodeName = "#document-fragment";
  3206. DocumentFragment.prototype.nodeType = DOCUMENT_FRAGMENT_NODE;
  3207. _extends(DocumentFragment,Node);
  3208.  
  3209.  
  3210. function ProcessingInstruction() {
  3211. }
  3212. ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
  3213. _extends(ProcessingInstruction,Node);
  3214. function XMLSerializer(){}
  3215. XMLSerializer.prototype.serializeToString = function(node){
  3216. var buf = [];
  3217. serializeToString(node,buf);
  3218. return buf.join('');
  3219. }
  3220. Node.prototype.toString =function(){
  3221. return XMLSerializer.prototype.serializeToString(this);
  3222. }
  3223. function serializeToString(node,buf){
  3224. switch(node.nodeType){
  3225. case ELEMENT_NODE:
  3226. var attrs = node.attributes;
  3227. var len = attrs.length;
  3228. var child = node.firstChild;
  3229. var nodeName = node.tagName;
  3230. var isHTML = htmlns === node.namespaceURI
  3231. buf.push('<',nodeName);
  3232. for(var i=0;i<len;i++){
  3233. serializeToString(attrs.item(i),buf,isHTML);
  3234. }
  3235. if(child || isHTML && !/^(?:meta|link|img|br|hr|input)$/i.test(nodeName)){
  3236. buf.push('>');
  3237. //if is cdata child node
  3238. if(isHTML && /^script$/i.test(nodeName)){
  3239. if(child){
  3240. buf.push(child.data);
  3241. }
  3242. }else{
  3243. while(child){
  3244. serializeToString(child,buf);
  3245. child = child.nextSibling;
  3246. }
  3247. }
  3248. buf.push('</',nodeName,'>');
  3249. }else{
  3250. buf.push('/>');
  3251. }
  3252. return;
  3253. case DOCUMENT_NODE:
  3254. case DOCUMENT_FRAGMENT_NODE:
  3255. var child = node.firstChild;
  3256. while(child){
  3257. serializeToString(child,buf);
  3258. child = child.nextSibling;
  3259. }
  3260. return;
  3261. case ATTRIBUTE_NODE:
  3262. return buf.push(' ',node.name,'="',node.value.replace(/[<&"]/g,_xmlEncoder),'"');
  3263. case TEXT_NODE:
  3264. return buf.push(node.data.replace(/[<&]/g,_xmlEncoder));
  3265. case CDATA_SECTION_NODE:
  3266. return buf.push( '<![CDATA[',node.data,']]>');
  3267. case COMMENT_NODE:
  3268. return buf.push( "<!--",node.data,"-->");
  3269. case DOCUMENT_TYPE_NODE:
  3270. var pubid = node.publicId;
  3271. var sysid = node.systemId;
  3272. buf.push('<!DOCTYPE ',node.name);
  3273. if(pubid){
  3274. buf.push(' PUBLIC "',pubid);
  3275. if (sysid && sysid!='.') {
  3276. buf.push( '" "',sysid);
  3277. }
  3278. buf.push('">');
  3279. }else if(sysid && sysid!='.'){
  3280. buf.push(' SYSTEM "',sysid,'">');
  3281. }else{
  3282. var sub = node.internalSubset;
  3283. if(sub){
  3284. buf.push(" [",sub,"]");
  3285. }
  3286. buf.push(">");
  3287. }
  3288. return;
  3289. case PROCESSING_INSTRUCTION_NODE:
  3290. return buf.push( "<?",node.target," ",node.data,"?>");
  3291. case ENTITY_REFERENCE_NODE:
  3292. return buf.push( '&',node.nodeName,';');
  3293. //case ENTITY_NODE:
  3294. //case NOTATION_NODE:
  3295. default:
  3296. buf.push('??',node.nodeName);
  3297. }
  3298. }
  3299. function importNode(doc,node,deep){
  3300. var node2;
  3301. switch (node.nodeType) {
  3302. case ELEMENT_NODE:
  3303. node2 = node.cloneNode(false);
  3304. node2.ownerDocument = doc;
  3305. //var attrs = node2.attributes;
  3306. //var len = attrs.length;
  3307. //for(var i=0;i<len;i++){
  3308. //node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
  3309. //}
  3310. case DOCUMENT_FRAGMENT_NODE:
  3311. break;
  3312. case ATTRIBUTE_NODE:
  3313. deep = true;
  3314. break;
  3315. //case ENTITY_REFERENCE_NODE:
  3316. //case PROCESSING_INSTRUCTION_NODE:
  3317. ////case TEXT_NODE:
  3318. //case CDATA_SECTION_NODE:
  3319. //case COMMENT_NODE:
  3320. // deep = false;
  3321. // break;
  3322. //case DOCUMENT_NODE:
  3323. //case DOCUMENT_TYPE_NODE:
  3324. //cannot be imported.
  3325. //case ENTITY_NODE:
  3326. //case NOTATION_NODE:
  3327. //can not hit in level3
  3328. //default:throw e;
  3329. }
  3330. if(!node2){
  3331. node2 = node.cloneNode(false);//false
  3332. }
  3333. node2.ownerDocument = doc;
  3334. node2.parentNode = null;
  3335. if(deep){
  3336. var child = node.firstChild;
  3337. while(child){
  3338. node2.appendChild(importNode(doc,child,deep));
  3339. child = child.nextSibling;
  3340. }
  3341. }
  3342. return node2;
  3343. }
  3344. //
  3345. //var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,
  3346. // attributes:1,childNodes:1,parentNode:1,documentElement:1,doctype,};
  3347. function cloneNode(doc,node,deep){
  3348. var node2 = new node.constructor();
  3349. for(var n in node){
  3350. var v = node[n];
  3351. if(typeof v != 'object' ){
  3352. if(v != node2[n]){
  3353. node2[n] = v;
  3354. }
  3355. }
  3356. }
  3357. if(node.childNodes){
  3358. node2.childNodes = new NodeList();
  3359. }
  3360. node2.ownerDocument = doc;
  3361. switch (node2.nodeType) {
  3362. case ELEMENT_NODE:
  3363. var attrs = node.attributes;
  3364. var attrs2 = node2.attributes = new NamedNodeMap();
  3365. var len = attrs.length
  3366. attrs2._ownerElement = node2;
  3367. for(var i=0;i<len;i++){
  3368. node2.setAttributeNode(cloneNode(doc,attrs.item(i),true));
  3369. }
  3370. break;;
  3371. case ATTRIBUTE_NODE:
  3372. deep = true;
  3373. }
  3374. if(deep){
  3375. var child = node.firstChild;
  3376. while(child){
  3377. node2.appendChild(cloneNode(doc,child,deep));
  3378. child = child.nextSibling;
  3379. }
  3380. }
  3381. return node2;
  3382. }
  3383.  
  3384. function __set__(object,key,value){
  3385. object[key] = value
  3386. }
  3387. //do dynamic
  3388. try{
  3389. if(Object.defineProperty){
  3390. Object.defineProperty(LiveNodeList.prototype,'length',{
  3391. get:function(){
  3392. _updateLiveList(this);
  3393. return this.$$length;
  3394. }
  3395. });
  3396. Object.defineProperty(Node.prototype,'textContent',{
  3397. get:function(){
  3398. return getTextContent(this);
  3399. },
  3400. set:function(data){
  3401. switch(this.nodeType){
  3402. case 1:
  3403. case 11:
  3404. while(this.firstChild){
  3405. this.removeChild(this.firstChild);
  3406. }
  3407. if(data || String(data)){
  3408. this.appendChild(this.ownerDocument.createTextNode(data));
  3409. }
  3410. break;
  3411. default:
  3412. //TODO:
  3413. this.data = data;
  3414. this.value = value;
  3415. this.nodeValue = data;
  3416. }
  3417. }
  3418. })
  3419. function getTextContent(node){
  3420. switch(node.nodeType){
  3421. case 1:
  3422. case 11:
  3423. var buf = [];
  3424. node = node.firstChild;
  3425. while(node){
  3426. if(node.nodeType!==7 && node.nodeType !==8){
  3427. buf.push(getTextContent(node));
  3428. }
  3429. node = node.nextSibling;
  3430. }
  3431. return buf.join('');
  3432. default:
  3433. return node.nodeValue;
  3434. }
  3435. }
  3436. __set__ = function(object,key,value){
  3437. //console.log(value)
  3438. object['$$'+key] = value
  3439. }
  3440. }
  3441. }catch(e){//ie8
  3442. }
  3443.  
  3444. if(typeof require == 'function'){
  3445. exports.DOMImplementation = DOMImplementation;
  3446. exports.XMLSerializer = XMLSerializer;
  3447. }
  3448.  
  3449. },{}],9:[function(require,module,exports){
  3450. //[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]
  3451. //[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
  3452. //[5] Name ::= NameStartChar (NameChar)*
  3453. 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
  3454. var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\u00B7\u0300-\u036F\\ux203F-\u2040]");
  3455. var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
  3456. //var tagNamePattern = /^[a-zA-Z_][\w\-\.]*(?:\:[a-zA-Z_][\w\-\.]*)?$/
  3457. //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(',')
  3458.  
  3459. //S_TAG, S_ATTR, S_EQ, S_V
  3460. //S_ATTR_S, S_E, S_S, S_C
  3461. var S_TAG = 0;//tag name offerring
  3462. var S_ATTR = 1;//attr name offerring
  3463. var S_ATTR_S=2;//attr name end and space offer
  3464. var S_EQ = 3;//=space?
  3465. var S_V = 4;//attr value(no quot value only)
  3466. var S_E = 5;//attr value end and no space(quot end)
  3467. var S_S = 6;//(attr value end || tag end ) && (space offer)
  3468. var S_C = 7;//closed el<el />
  3469.  
  3470. function XMLReader(){
  3471. }
  3472.  
  3473. XMLReader.prototype = {
  3474. parse:function(source,defaultNSMap,entityMap){
  3475. var domBuilder = this.domBuilder;
  3476. domBuilder.startDocument();
  3477. _copy(defaultNSMap ,defaultNSMap = {})
  3478. parse(source,defaultNSMap,entityMap,
  3479. domBuilder,this.errorHandler);
  3480. domBuilder.endDocument();
  3481. }
  3482. }
  3483. function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
  3484. function fixedFromCharCode(code) {
  3485. // String.prototype.fromCharCode does not supports
  3486. // > 2 bytes unicode chars directly
  3487. if (code > 0xffff) {
  3488. code -= 0x10000;
  3489. var surrogate1 = 0xd800 + (code >> 10)
  3490. , surrogate2 = 0xdc00 + (code & 0x3ff);
  3491.  
  3492. return String.fromCharCode(surrogate1, surrogate2);
  3493. } else {
  3494. return String.fromCharCode(code);
  3495. }
  3496. }
  3497. function entityReplacer(a){
  3498. var k = a.slice(1,-1);
  3499. if(k in entityMap){
  3500. return entityMap[k];
  3501. }else if(k.charAt(0) === '#'){
  3502. return fixedFromCharCode(parseInt(k.substr(1).replace('x','0x')))
  3503. }else{
  3504. errorHandler.error('entity not found:'+a);
  3505. return a;
  3506. }
  3507. }
  3508. function appendText(end){//has some bugs
  3509. var xt = source.substring(start,end).replace(/&#?\w+;/g,entityReplacer);
  3510. locator&&position(start);
  3511. domBuilder.characters(xt,0,end-start);
  3512. start = end
  3513. }
  3514. function position(start,m){
  3515. while(start>=endPos && (m = linePattern.exec(source))){
  3516. startPos = m.index;
  3517. endPos = startPos + m[0].length;
  3518. locator.lineNumber++;
  3519. //console.log('line++:',locator,startPos,endPos)
  3520. }
  3521. locator.columnNumber = start-startPos+1;
  3522. }
  3523. var startPos = 0;
  3524. var endPos = 0;
  3525. var linePattern = /.+(?:\r\n?|\n)|.*$/g
  3526. var locator = domBuilder.locator;
  3527. var parseStack = [{currentNSMap:defaultNSMapCopy}]
  3528. var closeMap = {};
  3529. var start = 0;
  3530. while(true){
  3531. var i = source.indexOf('<',start);
  3532. if(i<0){
  3533. if(!source.substr(start).match(/^\s*$/)){
  3534. var doc = domBuilder.document;
  3535. var text = doc.createTextNode(source.substr(start));
  3536. doc.appendChild(text);
  3537. domBuilder.currentElement = text;
  3538. }
  3539. return;
  3540. }
  3541. if(i>start){
  3542. appendText(i);
  3543. }
  3544. switch(source.charAt(i+1)){
  3545. case '/':
  3546. var end = source.indexOf('>',i+3);
  3547. var tagName = source.substring(i+2,end);
  3548. var config = parseStack.pop();
  3549. var localNSMap = config.localNSMap;
  3550. if(config.tagName != tagName){
  3551. errorHandler.fatalError("end tag name: "+tagName+' is not match the current start tagName:'+config.tagName );
  3552. }
  3553. domBuilder.endElement(config.uri,config.localName,tagName);
  3554. if(localNSMap){
  3555. for(var prefix in localNSMap){
  3556. domBuilder.endPrefixMapping(prefix) ;
  3557. }
  3558. }
  3559. end++;
  3560. break;
  3561. // end elment
  3562. case '?':// <?...?>
  3563. locator&&position(i);
  3564. end = parseInstruction(source,i,domBuilder);
  3565. break;
  3566. case '!':// <!doctype,<![CDATA,<!--
  3567. locator&&position(i);
  3568. end = parseDCC(source,i,domBuilder,errorHandler);
  3569. break;
  3570. default:
  3571. try{
  3572. locator&&position(i);
  3573. var el = new ElementAttributes();
  3574. //elStartEnd
  3575. var end = parseElementStartPart(source,i,el,entityReplacer,errorHandler);
  3576. var len = el.length;
  3577. //position fixed
  3578. if(len && locator){
  3579. var backup = copyLocator(locator,{});
  3580. for(var i = 0;i<len;i++){
  3581. var a = el[i];
  3582. position(a.offset);
  3583. a.offset = copyLocator(locator,{});
  3584. }
  3585. copyLocator(backup,locator);
  3586. }
  3587. if(!el.closed && fixSelfClosed(source,end,el.tagName,closeMap)){
  3588. el.closed = true;
  3589. if(!entityMap.nbsp){
  3590. errorHandler.warning('unclosed xml attribute');
  3591. }
  3592. }
  3593. appendElement(el,domBuilder,parseStack);
  3594. if(el.uri === 'http://www.w3.org/1999/xhtml' && !el.closed){
  3595. end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
  3596. }else{
  3597. end++;
  3598. }
  3599. }catch(e){
  3600. errorHandler.error('element parse error: '+e);
  3601. end = -1;
  3602. }
  3603.  
  3604. }
  3605. if(end<0){
  3606. //TODO: 这里有可能sax回退,有位置错误风险
  3607. appendText(i+1);
  3608. }else{
  3609. start = end;
  3610. }
  3611. }
  3612. }
  3613. function copyLocator(f,t){
  3614. t.lineNumber = f.lineNumber;
  3615. t.columnNumber = f.columnNumber;
  3616. return t;
  3617. }
  3618.  
  3619. /**
  3620. * @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
  3621. * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
  3622. */
  3623. function parseElementStartPart(source,start,el,entityReplacer,errorHandler){
  3624. var attrName;
  3625. var value;
  3626. var p = ++start;
  3627. var s = S_TAG;//status
  3628. while(true){
  3629. var c = source.charAt(p);
  3630. switch(c){
  3631. case '=':
  3632. if(s === S_ATTR){//attrName
  3633. attrName = source.slice(start,p);
  3634. s = S_EQ;
  3635. }else if(s === S_ATTR_S){
  3636. s = S_EQ;
  3637. }else{
  3638. //fatalError: equal must after attrName or space after attrName
  3639. throw new Error('attribute equal must after attrName');
  3640. }
  3641. break;
  3642. case '\'':
  3643. case '"':
  3644. if(s === S_EQ){//equal
  3645. start = p+1;
  3646. p = source.indexOf(c,start)
  3647. if(p>0){
  3648. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  3649. el.add(attrName,value,start-1);
  3650. s = S_E;
  3651. }else{
  3652. //fatalError: no end quot match
  3653. throw new Error('attribute value no end \''+c+'\' match');
  3654. }
  3655. }else if(s == S_V){
  3656. value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  3657. //console.log(attrName,value,start,p)
  3658. el.add(attrName,value,start);
  3659. //console.dir(el)
  3660. errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
  3661. start = p+1;
  3662. s = S_E
  3663. }else{
  3664. //fatalError: no equal before
  3665. throw new Error('attribute value must after "="');
  3666. }
  3667. break;
  3668. case '/':
  3669. switch(s){
  3670. case S_TAG:
  3671. el.setTagName(source.slice(start,p));
  3672. case S_E:
  3673. case S_S:
  3674. case S_C:
  3675. s = S_C;
  3676. el.closed = true;
  3677. case S_V:
  3678. case S_ATTR:
  3679. case S_ATTR_S:
  3680. break;
  3681. //case S_EQ:
  3682. default:
  3683. throw new Error("attribute invalid close char('/')")
  3684. }
  3685. break;
  3686. case ''://end document
  3687. //throw new Error('unexpected end of input')
  3688. errorHandler.error('unexpected end of input');
  3689. case '>':
  3690. switch(s){
  3691. case S_TAG:
  3692. el.setTagName(source.slice(start,p));
  3693. case S_E:
  3694. case S_S:
  3695. case S_C:
  3696. break;//normal
  3697. case S_V://Compatible state
  3698. case S_ATTR:
  3699. value = source.slice(start,p);
  3700. if(value.slice(-1) === '/'){
  3701. el.closed = true;
  3702. value = value.slice(0,-1)
  3703. }
  3704. case S_ATTR_S:
  3705. if(s === S_ATTR_S){
  3706. value = attrName;
  3707. }
  3708. if(s == S_V){
  3709. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  3710. el.add(attrName,value.replace(/&#?\w+;/g,entityReplacer),start)
  3711. }else{
  3712. errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
  3713. el.add(value,value,start)
  3714. }
  3715. break;
  3716. case S_EQ:
  3717. throw new Error('attribute value missed!!');
  3718. }
  3719. // console.log(tagName,tagNamePattern,tagNamePattern.test(tagName))
  3720. return p;
  3721. /*xml space '\x20' | #x9 | #xD | #xA; */
  3722. case '\u0080':
  3723. c = ' ';
  3724. default:
  3725. if(c<= ' '){//space
  3726. switch(s){
  3727. case S_TAG:
  3728. el.setTagName(source.slice(start,p));//tagName
  3729. s = S_S;
  3730. break;
  3731. case S_ATTR:
  3732. attrName = source.slice(start,p)
  3733. s = S_ATTR_S;
  3734. break;
  3735. case S_V:
  3736. var value = source.slice(start,p).replace(/&#?\w+;/g,entityReplacer);
  3737. errorHandler.warning('attribute "'+value+'" missed quot(")!!');
  3738. el.add(attrName,value,start)
  3739. case S_E:
  3740. s = S_S;
  3741. break;
  3742. //case S_S:
  3743. //case S_EQ:
  3744. //case S_ATTR_S:
  3745. // void();break;
  3746. //case S_C:
  3747. //ignore warning
  3748. }
  3749. }else{//not space
  3750. //S_TAG, S_ATTR, S_EQ, S_V
  3751. //S_ATTR_S, S_E, S_S, S_C
  3752. switch(s){
  3753. //case S_TAG:void();break;
  3754. //case S_ATTR:void();break;
  3755. //case S_V:void();break;
  3756. case S_ATTR_S:
  3757. errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead!!')
  3758. el.add(attrName,attrName,start);
  3759. start = p;
  3760. s = S_ATTR;
  3761. break;
  3762. case S_E:
  3763. errorHandler.warning('attribute space is required"'+attrName+'"!!')
  3764. case S_S:
  3765. s = S_ATTR;
  3766. start = p;
  3767. break;
  3768. case S_EQ:
  3769. s = S_V;
  3770. start = p;
  3771. break;
  3772. case S_C:
  3773. throw new Error("elements closed character '/' and '>' must be connected to");
  3774. }
  3775. }
  3776. }
  3777. p++;
  3778. }
  3779. }
  3780. /**
  3781. * @return end of the elementStartPart(end of elementEndPart for selfClosed el)
  3782. */
  3783. function appendElement(el,domBuilder,parseStack){
  3784. var tagName = el.tagName;
  3785. var localNSMap = null;
  3786. var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
  3787. var i = el.length;
  3788. while(i--){
  3789. var a = el[i];
  3790. var qName = a.qName;
  3791. var value = a.value;
  3792. var nsp = qName.indexOf(':');
  3793. if(nsp>0){
  3794. var prefix = a.prefix = qName.slice(0,nsp);
  3795. var localName = qName.slice(nsp+1);
  3796. var nsPrefix = prefix === 'xmlns' && localName
  3797. }else{
  3798. localName = qName;
  3799. prefix = null
  3800. nsPrefix = qName === 'xmlns' && ''
  3801. }
  3802. //can not set prefix,because prefix !== ''
  3803. a.localName = localName ;
  3804. //prefix == null for no ns prefix attribute
  3805. if(nsPrefix !== false){//hack!!
  3806. if(localNSMap == null){
  3807. localNSMap = {}
  3808. //console.log(currentNSMap,0)
  3809. _copy(currentNSMap,currentNSMap={})
  3810. //console.log(currentNSMap,1)
  3811. }
  3812. currentNSMap[nsPrefix] = localNSMap[nsPrefix] = value;
  3813. a.uri = 'http://www.w3.org/2000/xmlns/'
  3814. domBuilder.startPrefixMapping(nsPrefix, value)
  3815. }
  3816. }
  3817. var i = el.length;
  3818. while(i--){
  3819. a = el[i];
  3820. var prefix = a.prefix;
  3821. if(prefix){//no prefix attribute has no namespace
  3822. if(prefix === 'xml'){
  3823. a.uri = 'http://www.w3.org/XML/1998/namespace';
  3824. }if(prefix !== 'xmlns'){
  3825. a.uri = currentNSMap[prefix]
  3826. //{console.log('###'+a.qName,domBuilder.locator.systemId+'',currentNSMap,a.uri)}
  3827. }
  3828. }
  3829. }
  3830. var nsp = tagName.indexOf(':');
  3831. if(nsp>0){
  3832. prefix = el.prefix = tagName.slice(0,nsp);
  3833. localName = el.localName = tagName.slice(nsp+1);
  3834. }else{
  3835. prefix = null;//important!!
  3836. localName = el.localName = tagName;
  3837. }
  3838. //no prefix element has default namespace
  3839. var ns = el.uri = currentNSMap[prefix || ''];
  3840. domBuilder.startElement(ns,localName,tagName,el);
  3841. //endPrefixMapping and startPrefixMapping have not any help for dom builder
  3842. //localNSMap = null
  3843. if(el.closed){
  3844. domBuilder.endElement(ns,localName,tagName);
  3845. if(localNSMap){
  3846. for(prefix in localNSMap){
  3847. domBuilder.endPrefixMapping(prefix)
  3848. }
  3849. }
  3850. }else{
  3851. el.currentNSMap = currentNSMap;
  3852. el.localNSMap = localNSMap;
  3853. parseStack.push(el);
  3854. }
  3855. }
  3856. function parseHtmlSpecialContent(source,elStartEnd,tagName,entityReplacer,domBuilder){
  3857. if(/^(?:script|textarea)$/i.test(tagName)){
  3858. var elEndStart = source.indexOf('</'+tagName+'>',elStartEnd);
  3859. var text = source.substring(elStartEnd+1,elEndStart);
  3860. if(/[&<]/.test(text)){
  3861. if(/^script$/i.test(tagName)){
  3862. //if(!/\]\]>/.test(text)){
  3863. //lexHandler.startCDATA();
  3864. domBuilder.characters(text,0,text.length);
  3865. //lexHandler.endCDATA();
  3866. return elEndStart;
  3867. //}
  3868. }//}else{//text area
  3869. text = text.replace(/&#?\w+;/g,entityReplacer);
  3870. domBuilder.characters(text,0,text.length);
  3871. return elEndStart;
  3872. //}
  3873. }
  3874. }
  3875. return elStartEnd+1;
  3876. }
  3877. function fixSelfClosed(source,elStartEnd,tagName,closeMap){
  3878. //if(tagName in closeMap){
  3879. var pos = closeMap[tagName];
  3880. if(pos == null){
  3881. //console.log(tagName)
  3882. pos = closeMap[tagName] = source.lastIndexOf('</'+tagName+'>')
  3883. }
  3884. return pos<elStartEnd;
  3885. //}
  3886. }
  3887. function _copy(source,target){
  3888. for(var n in source){target[n] = source[n]}
  3889. }
  3890. function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
  3891. var next= source.charAt(start+2)
  3892. switch(next){
  3893. case '-':
  3894. if(source.charAt(start + 3) === '-'){
  3895. var end = source.indexOf('-->',start+4);
  3896. //append comment source.substring(4,end)//<!--
  3897. if(end>start){
  3898. domBuilder.comment(source,start+4,end-start-4);
  3899. return end+3;
  3900. }else{
  3901. errorHandler.error("Unclosed comment");
  3902. return -1;
  3903. }
  3904. }else{
  3905. //error
  3906. return -1;
  3907. }
  3908. default:
  3909. if(source.substr(start+3,6) == 'CDATA['){
  3910. var end = source.indexOf(']]>',start+9);
  3911. domBuilder.startCDATA();
  3912. domBuilder.characters(source,start+9,end-start-9);
  3913. domBuilder.endCDATA()
  3914. return end+3;
  3915. }
  3916. //<!DOCTYPE
  3917. //startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
  3918. var matchs = split(source,start);
  3919. var len = matchs.length;
  3920. if(len>1 && /!doctype/i.test(matchs[0][0])){
  3921. var name = matchs[1][0];
  3922. var pubid = len>3 && /^public$/i.test(matchs[2][0]) && matchs[3][0]
  3923. var sysid = len>4 && matchs[4][0];
  3924. var lastMatch = matchs[len-1]
  3925. domBuilder.startDTD(name,pubid && pubid.replace(/^(['"])(.*?)\1$/,'$2'),
  3926. sysid && sysid.replace(/^(['"])(.*?)\1$/,'$2'));
  3927. domBuilder.endDTD();
  3928. return lastMatch.index+lastMatch[0].length
  3929. }
  3930. }
  3931. return -1;
  3932. }
  3933.  
  3934.  
  3935.  
  3936. function parseInstruction(source,start,domBuilder){
  3937. var end = source.indexOf('?>',start);
  3938. if(end){
  3939. var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
  3940. if(match){
  3941. var len = match[0].length;
  3942. domBuilder.processingInstruction(match[1], match[2]) ;
  3943. return end+2;
  3944. }else{//error
  3945. return -1;
  3946. }
  3947. }
  3948. return -1;
  3949. }
  3950.  
  3951. /**
  3952. * @param source
  3953. */
  3954. function ElementAttributes(source){
  3955. }
  3956. ElementAttributes.prototype = {
  3957. setTagName:function(tagName){
  3958. if(!tagNamePattern.test(tagName)){
  3959. throw new Error('invalid tagName:'+tagName)
  3960. }
  3961. this.tagName = tagName
  3962. },
  3963. add:function(qName,value,offset){
  3964. if(!tagNamePattern.test(qName)){
  3965. throw new Error('invalid attribute:'+qName)
  3966. }
  3967. this[this.length++] = {qName:qName,value:value,offset:offset}
  3968. },
  3969. length:0,
  3970. getLocalName:function(i){return this[i].localName},
  3971. getOffset:function(i){return this[i].offset},
  3972. getQName:function(i){return this[i].qName},
  3973. getURI:function(i){return this[i].uri},
  3974. getValue:function(i){return this[i].value}
  3975. // ,getIndex:function(uri, localName)){
  3976. // if(localName){
  3977. //
  3978. // }else{
  3979. // var qName = uri
  3980. // }
  3981. // },
  3982. // getValue:function(){return this.getValue(this.getIndex.apply(this,arguments))},
  3983. // getType:function(uri,localName){}
  3984. // getType:function(i){},
  3985. }
  3986.  
  3987.  
  3988.  
  3989.  
  3990. function _set_proto_(thiz,parent){
  3991. thiz.__proto__ = parent;
  3992. return thiz;
  3993. }
  3994. if(!(_set_proto_({},_set_proto_.prototype) instanceof _set_proto_)){
  3995. _set_proto_ = function(thiz,parent){
  3996. function p(){};
  3997. p.prototype = parent;
  3998. p = new p();
  3999. for(parent in thiz){
  4000. p[parent] = thiz[parent];
  4001. }
  4002. return p;
  4003. }
  4004. }
  4005.  
  4006. function split(source,start){
  4007. var match;
  4008. var buf = [];
  4009. var reg = /'[^']+'|"[^"]+"|[^\s<>\/=]+=?|(\/?\s*>|<)/g;
  4010. reg.lastIndex = start;
  4011. reg.exec(source);//skip <
  4012. while(match = reg.exec(source)){
  4013. buf.push(match);
  4014. if(match[1])return buf;
  4015. }
  4016. }
  4017.  
  4018. if(typeof require == 'function'){
  4019. exports.XMLReader = XMLReader;
  4020. }
  4021.  
  4022.  
  4023. },{}]},{},[1])(1)
  4024. });
Buy Me A Coffee