Permalink
Browse files

Support for getting the position of the start of the opentag/closetag

  • Loading branch information...
1 parent 862eeca commit 1e640ee54047ed32e1349d171512a3de5d286ddb @smh smh committed with Jul 8, 2011
Showing with 13 additions and 8 deletions.
  1. +2 −0 README.md
  2. +2 −0 lib/sax.js
  3. +9 −8 test/parser-position.js
View
@@ -112,6 +112,8 @@ At all times, the parser object will have the following members:
`line`, `column`, `position` - Indications of the position in the XML document where
the parser currently is looking.
+`startTagPosition` - Indicates the position where the current tag starts.
+
`closed` - Boolean indicating whether or not the parser can be written to. If it's
`true`, then wait for the `ready` event to write again.
View
@@ -573,9 +573,11 @@ function write (chunk) {
} else if (is(whitespace, c)) {
// wait for it...
} else if (is(nameStart,c)) {
+ parser.startTagPosition = parser.position - 1
parser.state = S.OPEN_TAG
parser.tagName = c
} else if (c === "/") {
+ parser.startTagPosition = parser.position - 1
parser.state = S.CLOSE_TAG
parser.tagName = ""
} else if (c === "?") {
View
@@ -5,7 +5,9 @@ function testPosition(chunks, expectedEvents) {
var parser = sax.parser();
expectedEvents.forEach(function(expectation) {
parser['on' + expectation[0]] = function() {
- assert.equal(parser.position, expectation[1]);
+ for (var prop in expectation[1]) {
+ assert.equal(parser[prop], expectation[1][prop]);
+ }
}
});
chunks.forEach(function(chunk) {
@@ -14,14 +16,13 @@ function testPosition(chunks, expectedEvents) {
};
testPosition(['<div>abcdefgh</div>'],
- [ ['opentag', 5]
- , ['text', 19]
- , ['closetag', 19]
+ [ ['opentag', { position: 5, startTagPosition: 1 }]
+ , ['text', { position: 19, startTagPosition: 14 }]
+ , ['closetag', { position: 19, startTagPosition: 14 }]
]);
testPosition(['<div>abcde','fgh</div>'],
- [ ['opentag', 5]
- , ['text', 19]
- , ['closetag', 19]
+ [ ['opentag', { position: 5, startTagPosition: 1 }]
+ , ['text', { position: 19, startTagPosition: 14 }]
+ , ['closetag', { position: 19, startTagPosition: 14 }]
]);
-

0 comments on commit 1e640ee

Please sign in to comment.