Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for extracting the source location, i.e. line- and colu…

…mn-number. The feature can be enabled by setting `includeLocation` to true on the parser instance.
  • Loading branch information...
commit d2c6059500d005f7ebf055dfc87e6af3c02e99fc 1 parent 67dc4c2
Felix Gnass fgnass authored
Showing with 27 additions and 0 deletions.
  1. +27 −0 lib/node-htmlparser.js
27 lib/node-htmlparser.js
View
@@ -123,6 +123,7 @@ function Parser (handler) {
this._elementsCurrent = 0;
this._current = 0;
this._next = 0;
+ this._line = 0;
this._parseState = ElementType.Text;
this._prevTagSep = '';
this._tagStack = [];
@@ -214,6 +215,11 @@ function Parser (handler) {
, type: this._parseState
};
+ if (this.includeLocation) {
+ element.location = this.getLocation();
+ this._line = element.location.line;
+ }
+
var elementName = this.parseTagName(element.data);
//This section inspects the current tag stack and modifies the current
@@ -360,6 +366,27 @@ function Parser (handler) {
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);
+ if (c == '\n') {
+ line++;
+ col = 0;
+ }
+ else if (c != '\r') {
+ col++;
+ }
+ }
+ return {
+ line: line,
+ col: col
+ };
+ }
+
//Checks the handler to make it is an object with the right "interface"
Parser.prototype.validateHandler = function Parser$validateHandler (handler) {
if ((typeof handler) != "object")
Please sign in to comment.
Something went wrong with that request. Please try again.