Skip to content

Commit

Permalink
Fixed bug with position in chunked text-nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
smh authored and isaacs committed Jul 8, 2011
1 parent 5aef708 commit 70b69ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/sax.js
Expand Up @@ -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);
}
Expand Down
27 changes: 27 additions & 0 deletions test/parser-position.js
@@ -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.