Adds char/byte location information to elements. #52

Closed
wants to merge 4 commits into
from
View
@@ -0,0 +1,3 @@
+.DS_Store
+*.swp
+
View
@@ -116,7 +116,7 @@ function Parser (handler, options) {
this._elements.push(element);
}
- this.writeHandler();
+ this.writeHandler(true);
this._handler.done();
}
@@ -133,6 +133,7 @@ function Parser (handler, options) {
, col: 0
, charOffset: 0
, inBuffer: 0
+ , character: 0
};
this._parseState = ElementType.Text;
this._prevTagSep = '';
@@ -346,7 +347,7 @@ function Parser (handler, options) {
//Add all tags and non-empty text elements to the element list
if (element.raw != "" || element.type != ElementType.Text) {
if (this._options.includeLocation && !element.location) {
- element.location = this.getLocation(element.type == ElementType.Tag);
+ element.location = this.getLocation(element.type == ElementType.Tag, element.type == ElementType.Script);
}
this.parseAttribs(element);
this._elements.push(element);
@@ -384,24 +385,29 @@ function Parser (handler, options) {
this.writeHandler();
}
- Parser.prototype.getLocation = function Parser$getLocation (startTag) {
+ Parser.prototype.getLocation = function Parser$getLocation (startTag, scriptTag) {
var c,
l = this._location,
end = this._current - (startTag ? 1 : 0),
chunk = startTag && l.charOffset == 0 && this._current == 0;
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)
+ , col: l.col + (chunk ? 0: 1) - (scriptTag ? 1 : 0)
+ , character: l.character + (chunk ? 0: 1) - (scriptTag ? 1 : 0) - 1
+
};
}
View

Large diffs are not rendered by default.

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)
View
@@ -30,7 +30,7 @@ exports.options = {
handler: {}
, parser: { includeLocation: true }
};
-exports.html = "<html>\r\n\n\t<title>The Title</title><body>\nHello world\r\n\n</body>\n\n</html>";
+exports.html = "<html>\r\n\n\t<title>The Title</title><body>\nHello world\r\n\n</body>\n\n<script src=\"test.js\"></script><script>var test = true;</script></html>";
exports.expected = [
{
raw: 'html',
@@ -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,10 +97,44 @@ exports.expected = [
type: 'text',
location: {
line: 6,
- col: 8
+ col: 8,
+ character: 62
}
+ }, {
+ raw: 'script src="test.js"',
+ data: 'script src="test.js"',
+ type: 'script',
+ name: 'script',
+ location: {
+ line: 8,
+ col: 1,
+ character: 64
+ },
+ attribs: {
+ src: 'test.js'
+ }
+ }, {
+ raw: 'script',
+ data: 'script',
+ type: 'script',
+ name: 'script',
+ location: {
+ line: 8,
+ col: 32,
+ character: 95
+ },
+ children: [
+ {
+ raw: 'var test = true;',
+ data: 'var test = true;',
+ type: 'text',
+ location: {
+ line: 8,
+ col: 40,
+ character: 103
+ }
+ }
+ ]
}]
- }
- ];
-
+ }];
})();