Permalink
Browse files

Adds character position to getLocation

Location data now includes a `character` position, which is
the position (in terms of characters) in the stream processed
so far.
  • Loading branch information...
1 parent e01ac67 commit fc183834ebec825d5f4efb4c8734018f8d787e1d Ryan J Daw committed Jun 14, 2012
Showing with 28 additions and 31 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 lib/htmlparser.js
  3. +1 −22 lib/htmlparser.min.js
  4. +1 −1 package.json
  5. +6 −1 runtests.js
  6. +14 −7 tests/22-position_data.js
View
@@ -1,2 +1,3 @@
.DS_Store
+*.swp
View
@@ -133,6 +133,7 @@ function Parser (handler, options) {
, col: 0
, charOffset: 0
, inBuffer: 0
+ , character: 0
};
this._parseState = ElementType.Text;
this._prevTagSep = '';
@@ -392,16 +393,20 @@ function Parser (handler, options) {
for (; l.charOffset < end; l.charOffset++) {
c = this._buffer.charAt(l.charOffset);
+ l.character++;
+
if (c == '\n') {
l.inBuffer++;
l.col = 0;
+
} else if (c != '\r') {
l.col++;
}
}
return {
line: l.row + l.inBuffer + 1
, col: l.col + (chunk ? 0: 1)
+ , character: l.character - (chunk ? 1: 0)
};
}
View
Oops, something went wrong.
View
@@ -1,7 +1,7 @@
{
"name": "htmlparser"
, "description": "Forgiving HTML/XML/RSS Parser in JS for *both* Node and Browsers"
- , "version": "1.7.6"
+ , "version": "1.7.7"
, "author": "Chris Winberry <chris@winberry.net>"
, "contributors": []
, "repository": {
View
@@ -30,10 +30,15 @@ var testFiles = fs.readdirSync(testFolder);
var testCount = 0;
var failedCount = 0;
for (var i in testFiles) {
- testCount++;
var fileParts = testFiles[i].split(".");
fileParts.pop();
var moduleName = fileParts.join(".");
+
+ if (moduleName.charAt(0) === '.') {
+ continue;
+ }
+ testCount++;
+
var test = require(testFolder + "/" + moduleName);
var handlerCallback = function handlerCallback (error) {
if (error)
@@ -39,15 +39,17 @@ exports.expected = [
name: 'html',
location: {
line: 1,
- col: 1
+ col: 1,
+ character: 0
},
children: [{
raw: '\r\n\n\t',
data: '\r\n\n\t',
type: 'text',
location: {
line: 1,
- col: 7
+ col: 7,
+ character: 6
}
}, {
raw: 'title',
@@ -56,15 +58,17 @@ exports.expected = [
name: 'title',
location: {
line: 3,
- col: 2
+ col: 2,
+ character: 10,
},
children: [{
raw: 'The Title',
data: 'The Title',
type: 'text',
location: {
line: 3,
- col: 9
+ col: 9,
+ character: 17
}
}]
}, {
@@ -74,15 +78,17 @@ exports.expected = [
name: 'body',
location: {
line: 3,
- col: 26
+ col: 26,
+ character: 34,
},
children: [{
raw: '\nHello world\r\n\n',
data: '\nHello world\r\n\n',
type: 'text',
location: {
line: 3,
- col: 32
+ col: 32,
+ character: 40,
}
}]
}, {
@@ -91,7 +97,8 @@ exports.expected = [
type: 'text',
location: {
line: 6,
- col: 8
+ col: 8,
+ character: 62
}
}]
}

0 comments on commit fc18383

Please sign in to comment.