Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit ba30bf6f28935f26c7a73dbd40af3bcad30e5a3c 1 parent b32a817
@smh smh authored isaacs committed
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
2  README.md
@@ -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
2  lib/sax.js
@@ -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
17 test/parser-position.js
@@ -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 }]
]);
-
Please sign in to comment.
Something went wrong with that request. Please try again.