Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…re it off.
  • Loading branch information...
commit db196132e22b0e28347af2c100b6ba3e977f16b4 1 parent 2b012f1
@fgnass fgnass authored
Showing with 14 additions and 10 deletions.
  1. +14 −10 lib/node-htmlparser.js
View
24 lib/node-htmlparser.js
@@ -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,6 +364,10 @@ 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;
@@ -368,14 +375,11 @@ function Parser (handler) {
}
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') {
@@ -383,7 +387,7 @@ function Parser (handler) {
}
}
return {
- line: line,
+ line: this._lines.offset + this._lines.inBuffer,
col: col
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.