|
| 1 | +diff --git a/lib/sax.js b/lib/sax.js |
| 2 | +index d35adf1a319b7f064d7a7e43c46630ed5ef587ff..4c5a08844f3be4ddceb12285d3fd133213dcc7e0 100644 |
| 3 | +--- a/lib/sax.js |
| 4 | ++++ b/lib/sax.js |
| 5 | +@@ -4,8 +4,6 @@ |
| 6 | + return new SAXParser(strict, opt) |
| 7 | + } |
| 8 | + sax.SAXParser = SAXParser |
| 9 | +- sax.SAXStream = SAXStream |
| 10 | +- sax.createStream = createStream |
| 11 | + |
| 12 | + // When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns. |
| 13 | + // When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)), |
| 14 | +@@ -191,123 +189,6 @@ |
| 15 | + }, |
| 16 | + } |
| 17 | + |
| 18 | +- var Stream |
| 19 | +- try { |
| 20 | +- Stream = require('stream').Stream |
| 21 | +- } catch (ex) { |
| 22 | +- Stream = function () {} |
| 23 | +- } |
| 24 | +- if (!Stream) Stream = function () {} |
| 25 | +- |
| 26 | +- var streamWraps = sax.EVENTS.filter(function (ev) { |
| 27 | +- return ev !== 'error' && ev !== 'end' |
| 28 | +- }) |
| 29 | +- |
| 30 | +- function createStream(strict, opt) { |
| 31 | +- return new SAXStream(strict, opt) |
| 32 | +- } |
| 33 | +- |
| 34 | +- function SAXStream(strict, opt) { |
| 35 | +- if (!(this instanceof SAXStream)) { |
| 36 | +- return new SAXStream(strict, opt) |
| 37 | +- } |
| 38 | +- |
| 39 | +- Stream.apply(this) |
| 40 | +- |
| 41 | +- this._parser = new SAXParser(strict, opt) |
| 42 | +- this.writable = true |
| 43 | +- this.readable = true |
| 44 | +- |
| 45 | +- var me = this |
| 46 | +- |
| 47 | +- this._parser.onend = function () { |
| 48 | +- me.emit('end') |
| 49 | +- } |
| 50 | +- |
| 51 | +- this._parser.onerror = function (er) { |
| 52 | +- me.emit('error', er) |
| 53 | +- |
| 54 | +- // if didn't throw, then means error was handled. |
| 55 | +- // go ahead and clear error, so we can write again. |
| 56 | +- me._parser.error = null |
| 57 | +- } |
| 58 | +- |
| 59 | +- this._decoder = null |
| 60 | +- |
| 61 | +- streamWraps.forEach(function (ev) { |
| 62 | +- Object.defineProperty(me, 'on' + ev, { |
| 63 | +- get: function () { |
| 64 | +- return me._parser['on' + ev] |
| 65 | +- }, |
| 66 | +- set: function (h) { |
| 67 | +- if (!h) { |
| 68 | +- me.removeAllListeners(ev) |
| 69 | +- me._parser['on' + ev] = h |
| 70 | +- return h |
| 71 | +- } |
| 72 | +- me.on(ev, h) |
| 73 | +- }, |
| 74 | +- enumerable: true, |
| 75 | +- configurable: false, |
| 76 | +- }) |
| 77 | +- }) |
| 78 | +- } |
| 79 | +- |
| 80 | +- SAXStream.prototype = Object.create(Stream.prototype, { |
| 81 | +- constructor: { |
| 82 | +- value: SAXStream, |
| 83 | +- }, |
| 84 | +- }) |
| 85 | +- |
| 86 | +- SAXStream.prototype.write = function (data) { |
| 87 | +- if ( |
| 88 | +- typeof Buffer === 'function' && |
| 89 | +- typeof Buffer.isBuffer === 'function' && |
| 90 | +- Buffer.isBuffer(data) |
| 91 | +- ) { |
| 92 | +- if (!this._decoder) { |
| 93 | +- this._decoder = new TextDecoder('utf8') |
| 94 | +- } |
| 95 | +- data = this._decoder.decode(data, { stream: true }) |
| 96 | +- } |
| 97 | +- |
| 98 | +- this._parser.write(data.toString()) |
| 99 | +- this.emit('data', data) |
| 100 | +- return true |
| 101 | +- } |
| 102 | +- |
| 103 | +- SAXStream.prototype.end = function (chunk) { |
| 104 | +- if (chunk && chunk.length) { |
| 105 | +- this.write(chunk) |
| 106 | +- } |
| 107 | +- // Flush any remaining decoded data from the TextDecoder |
| 108 | +- if (this._decoder) { |
| 109 | +- var remaining = this._decoder.decode() |
| 110 | +- if (remaining) { |
| 111 | +- this._parser.write(remaining) |
| 112 | +- this.emit('data', remaining) |
| 113 | +- } |
| 114 | +- } |
| 115 | +- this._parser.end() |
| 116 | +- return true |
| 117 | +- } |
| 118 | +- |
| 119 | +- SAXStream.prototype.on = function (ev, handler) { |
| 120 | +- var me = this |
| 121 | +- if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) { |
| 122 | +- me._parser['on' + ev] = function () { |
| 123 | +- var args = |
| 124 | +- arguments.length === 1 ? |
| 125 | +- [arguments[0]] |
| 126 | +- : Array.apply(null, arguments) |
| 127 | +- args.splice(0, 0, ev) |
| 128 | +- me.emit.apply(me, args) |
| 129 | +- } |
| 130 | +- } |
| 131 | +- |
| 132 | +- return Stream.prototype.on.call(me, ev, handler) |
| 133 | +- } |
| 134 | +- |
| 135 | + // this really needs to be replaced with character classes. |
| 136 | + // XML allows all manner of ridiculous numbers and digits. |
| 137 | + var CDATA = '[CDATA[' |
0 commit comments