Skip to content

Commit

Permalink
Support for getting the position of the start of the opentag/closetag
Browse files Browse the repository at this point in the history
  • Loading branch information
smh authored and isaacs committed Sep 17, 2011
1 parent b32a817 commit ba30bf6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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 `line`, `column`, `position` - Indications of the position in the XML document where
the parser currently is looking. 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 `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. `true`, then wait for the `ready` event to write again.


Expand Down
2 changes: 2 additions & 0 deletions lib/sax.js
Expand Up @@ -573,9 +573,11 @@ function write (chunk) {
} else if (is(whitespace, c)) { } else if (is(whitespace, c)) {
// wait for it... // wait for it...
} else if (is(nameStart,c)) { } else if (is(nameStart,c)) {
parser.startTagPosition = parser.position - 1
parser.state = S.OPEN_TAG parser.state = S.OPEN_TAG
parser.tagName = c parser.tagName = c
} else if (c === "/") { } else if (c === "/") {
parser.startTagPosition = parser.position - 1
parser.state = S.CLOSE_TAG parser.state = S.CLOSE_TAG
parser.tagName = "" parser.tagName = ""
} else if (c === "?") { } else if (c === "?") {
Expand Down
17 changes: 9 additions & 8 deletions test/parser-position.js
Expand Up @@ -5,7 +5,9 @@ function testPosition(chunks, expectedEvents) {
var parser = sax.parser(); var parser = sax.parser();
expectedEvents.forEach(function(expectation) { expectedEvents.forEach(function(expectation) {
parser['on' + expectation[0]] = function() { 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) { chunks.forEach(function(chunk) {
Expand All @@ -14,14 +16,13 @@ function testPosition(chunks, expectedEvents) {
}; };


testPosition(['<div>abcdefgh</div>'], testPosition(['<div>abcdefgh</div>'],
[ ['opentag', 5] [ ['opentag', { position: 5, startTagPosition: 1 }]
, ['text', 19] , ['text', { position: 19, startTagPosition: 14 }]
, ['closetag', 19] , ['closetag', { position: 19, startTagPosition: 14 }]
]); ]);


testPosition(['<div>abcde','fgh</div>'], testPosition(['<div>abcde','fgh</div>'],
[ ['opentag', 5] [ ['opentag', { position: 5, startTagPosition: 1 }]
, ['text', 19] , ['text', { position: 19, startTagPosition: 14 }]
, ['closetag', 19] , ['closetag', { position: 19, startTagPosition: 14 }]
]); ]);

0 comments on commit ba30bf6

Please sign in to comment.