Permalink
Browse files

Fixed bug with position in chunked text-nodes.

  • Loading branch information...
1 parent 5aef708 commit 70b69ae4726f4f3ccd15cff4b5694916e1fed713 @smh smh committed with isaacs Jul 8, 2011
Showing with 34 additions and 5 deletions.
  1. +7 −5 lib/sax.js
  2. +27 −0 test/parser-position.js
View
@@ -278,11 +278,13 @@ function write (chunk) {
var starti = i-1;
while (c && c!=="<" && c!=="&") {
c = chunk.charAt(i++);
- parser.position ++;
- if (c === "\n") {
- parser.line ++;
- parser.column = 0;
- } else parser.column ++;
+ if (c) {
+ parser.position ++;
+ if (c === "\n") {
+ parser.line ++;
+ parser.column = 0;
+ } else parser.column ++;
+ }
}
parser.textNode += chunk.substring(starti, i-1);
}
@@ -0,0 +1,27 @@
+var sax = require("../lib/sax"),
+ assert = require("assert")
+
+function testPosition(chunks, expectedEvents) {
+ var parser = sax.parser();
+ expectedEvents.forEach(function(expectation) {
+ parser['on' + expectation[0]] = function() {
+ assert.equal(parser.position, expectation[1]);
+ }
+ });
+ chunks.forEach(function(chunk) {
+ parser.write(chunk);
+ });
+};
+
+testPosition(['<div>abcdefgh</div>'],
+ [ ['opentag', 5]
+ , ['text', 19]
+ , ['closetag', 19]
+ ]);
+
+testPosition(['<div>abcde','fgh</div>'],
+ [ ['opentag', 5]
+ , ['text', 19]
+ , ['closetag', 19]
+ ]);
+

0 comments on commit 70b69ae

Please sign in to comment.