Permalink
Browse files

Line numbers now start at 1. Optimized the loop so that it starts whe…

…re it off.
  • Loading branch information...
1 parent 2b012f1 commit db196132e22b0e28347af2c100b6ba3e977f16b4 Felix Gnass committed Nov 12, 2010
Showing with 14 additions and 10 deletions.
  1. +14 −10 lib/node-htmlparser.js
View
@@ -123,7 +123,11 @@ function Parser (handler) {
this._elementsCurrent = 0;
this._current = 0;
this._next = 0;
- this._line = 0;
+ this._lines = {
+ offset: 1,
+ charOffset: 0,
+ inBuffer: 0
+ };
this._parseState = ElementType.Text;
this._prevTagSep = '';
this._tagStack = [];
@@ -217,7 +221,6 @@ function Parser (handler) {
if (this.includeLocation) {
element.location = this.getLocation();
- this._line = element.location.line;
}
var elementName = this.parseTagName(element.data);
@@ -361,29 +364,30 @@ function Parser (handler) {
this._prevTagSep = tagSep;
}
+ this._lines.offset += this._lines.inBuffer;
+ this._lines.inBuffer = 0;
+ this._lines.charOffset = 0;
+
this._buffer = (this._current <= bufferEnd) ? this._buffer.substring(this._current) : "";
this._current = 0;
this.writeHandler();
}
Parser.prototype.getLocation = function() {
- var i, c,
- line = this._line,
- col = 0;
-
- for (i=0; i < this._current; i++) {
- c = this._buffer.charAt(i);
+ var c, col = 0;
+ for (; this._lines.charOffset < this._current; this._lines.charOffset++) {
+ c = this._buffer.charAt(this._lines.charOffset);
if (c == '\n') {
- line++;
+ this._lines.inBuffer++;
col = 0;
}
else if (c != '\r') {
col++;
}
}
return {
- line: line,
+ line: this._lines.offset + this._lines.inBuffer,
col: col
};
}

0 comments on commit db19613

Please sign in to comment.