Permalink
Browse files

Fixes bug where scripts were in the wrong location.

Note: this fix is sorta hackey. The root cause lies elsewhere.
  • Loading branch information...
Ryan J Daw
Ryan J Daw committed Jun 15, 2012
1 parent fc18383 commit 6436064f9117c316a0eb43413afc87e95e33ffd3
Showing with 42 additions and 8 deletions.
  1. +5 −4 lib/htmlparser.js
  2. +37 −4 tests/22-position_data.js
View
@@ -347,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);
@@ -385,7 +385,7 @@ 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),
@@ -405,8 +405,9 @@ function Parser (handler, options) {
}
return {
line: l.row + l.inBuffer + 1
- , col: l.col + (chunk ? 0: 1)
- , character: l.character - (chunk ? 1: 0)
+ , col: l.col + (chunk ? 0: 1) - (scriptTag ? 1 : 0)
+ , character: l.character + (chunk ? 0: 1) - (scriptTag ? 1 : 0) - 1
+
};
}
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',
@@ -100,8 +100,41 @@ exports.expected = [
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
+ }
+ }
+ ]
}]
- }
- ];
-
+ }];
})();

0 comments on commit 6436064

Please sign in to comment.