Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add partial text events #271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!DOCTYPE` declaration. Argument: doctype string.

`processinginstruction` - Stuff like `<?xml foo="blerg" ?>`. Argument:
Expand Down
8 changes: 8 additions & 0 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

sax.EVENTS = [
'text',
'partialtext',
'processinginstruction',
'sgmldeclaration',
'doctype',
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/buffer-overrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require(__dirname).test({
'attributes': {},
'isSelfClosing': false
}],
['partialtext', 'yo'],
['text', 'yo'],
['closetag', 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ']
]
Expand Down
1 change: 1 addition & 0 deletions test/entity-elem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}],
Expand Down
1 change: 1 addition & 0 deletions test/entity-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
]
Expand Down
1 change: 1 addition & 0 deletions test/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
25 changes: 25 additions & 0 deletions test/partial-text.js
Original file line number Diff line number Diff line change
@@ -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('<A><B>foo ')
parser.write('bar')
parser.write(' world</')
parser.write('B>')
parser.write('<C>full node</C></A>')
parser.close()