diff --git a/README.md b/README.md index afcd3f3..0847867 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,9 @@ this happens *much* more in strict mode. Argument: instance of `Error`. `text` - Text node. Argument: string of text. +`partialtext` - Intermediate text node, emitted when the parser hasn't completed +a full `text` node but is still receiving text. Argument: string of text. + `doctype` - The ``. Argument: diff --git a/lib/sax.js b/lib/sax.js index ffd441a..99fa76d 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -23,6 +23,7 @@ sax.EVENTS = [ 'text', + 'partialtext', 'processinginstruction', 'sgmldeclaration', 'doctype', @@ -191,6 +192,10 @@ me.emit('end') } + this._parser.onpartialtext = function (text) { + me.emit('partialtext', text) + } + this._parser.onerror = function (er) { me.emit('error', er) @@ -1039,6 +1044,9 @@ parser.state = S.TEXT_ENTITY } else { parser.textNode += c + if (c !== '<' && parser.sawRoot && !parser.closedRoot && parser.textNode) { + emit(parser, 'onpartialtext', parser.textNode) + } } } continue diff --git a/test/buffer-overrun.js b/test/buffer-overrun.js index 8d4d7a8..367a69f 100644 --- a/test/buffer-overrun.js +++ b/test/buffer-overrun.js @@ -17,6 +17,7 @@ require(__dirname).test({ 'attributes': {}, 'isSelfClosing': false }], + ['partialtext', 'yo'], ['text', 'yo'], ['closetag', 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'] ] diff --git a/test/entity-elem.js b/test/entity-elem.js index 268bb46..fb0f313 100644 --- a/test/entity-elem.js +++ b/test/entity-elem.js @@ -9,6 +9,7 @@ require(__dirname).test({ ["opentagstart", {name: "A", attributes: {}}], ["attribute", {name: 'ATTR', value: "1.2"}], ["opentag", {name: "A", attributes: {ATTR: "1.2"}, isSelfClosing: false}], + ["partialtext", "2.1"], ["text", "2.1"], ["opentagstart", {name: "B", attributes: {}}], ["attribute", {name: 'ATTR', value: "1.3"}], diff --git a/test/entity-recursive.js b/test/entity-recursive.js index f9fc6a6..2d648c7 100644 --- a/test/entity-recursive.js +++ b/test/entity-recursive.js @@ -7,6 +7,7 @@ require(__dirname).test({ expect: [ ["opentagstart", {name: "A", attributes: {}}], ["opentag", {name: "A", attributes: {}, isSelfClosing: false}], + ["partialtext", "2.1"], ["text", "2.1"], ["closetag", "A"] ] diff --git a/test/flush.js b/test/flush.js index b8b21e9..59dc1d0 100644 --- a/test/flush.js +++ b/test/flush.js @@ -2,6 +2,7 @@ var parser = require(__dirname).test({ expect: [ ['opentagstart', {'name': 'T', attributes: {}}], ['opentag', {'name': 'T', attributes: {}, isSelfClosing: false}], + ['partialtext', 'flush'], ['text', 'flush'], ['text', 'rest'], ['closetag', 'T'] diff --git a/test/partial-text.js b/test/partial-text.js new file mode 100644 index 0000000..fee2181 --- /dev/null +++ b/test/partial-text.js @@ -0,0 +1,25 @@ +var parser = require(__dirname).test({ + expect: [ + ['opentagstart', {'name': 'A', attributes: {}}], + ['opentag', {'name': 'A', attributes: {}, isSelfClosing: false}], + ['opentagstart', {'name': 'B', attributes: {}}], + ['opentag', {'name': 'B', attributes: {}, isSelfClosing: false}], + ['partialtext', 'foo '], + ['partialtext', 'foo bar'], + ['text', 'foo bar world'], + ['closetag', 'B'], + ['opentagstart', {'name': 'C', attributes: {}}], + ['opentag', {'name': 'C', attributes: {}, isSelfClosing: false}], + ['text', 'full node'], + ['closetag', 'C'], + ['closetag', 'A'] + ] + }) + + parser.write('foo ') + parser.write('bar') + parser.write(' world') + parser.write('full node') + parser.close() + \ No newline at end of file