Permalink
Browse files

Changed the way parsing errors are reported a bit. Also added MISMATC…

…HED_CLOSE_TAG error type.
  • Loading branch information...
1 parent d95d8d4 commit c0b38a9381e89552705dcd010b7209937dc79040 @toolness committed Apr 11, 2012
Showing with 38 additions and 12 deletions.
  1. +17 −6 slowparse.js
  2. +21 −6 test/test-slowparse.js
View
@@ -2,7 +2,16 @@ var Slowparse = (function() {
function ParseError(parseInfo) {
this.name = "ParseError";
this.message = parseInfo.type;
- this.parseInfo = parseInfo;
+ this.parseInfo = {
+ type: parseInfo.type
+ };
+ if (parseInfo.node)
+ this.parseInfo.node = parseInfo.node;
+ if (parseInfo.token) {
+ this.parseInfo.value = parseInfo.token.value;
+ this.parseInfo.start = parseInfo.token.start;
+ this.parseInfo.end = parseInfo.token.end;
+ }
}
ParseError.prototype = Error.prototype;
@@ -80,14 +89,17 @@ var Slowparse = (function() {
};
if (tagName.slice(1).toLowerCase() !=
this.domBuilder.currentNode.nodeName.toLowerCase())
- throw new Error("TODO: parse error for unmatching close tag");
+ throw new ParseError({
+ type: "MISMATCHED_CLOSE_TAG",
+ node: this.domBuilder.currentNode,
+ token: token
+ });
this._parseEndCloseTag();
} else {
if (!(tagName && tagName.match(/^[A-Za-z]+$/)))
throw new ParseError({
type: "INVALID_TAG_NAME",
- value: tagName,
- position: token.interval.start + 1
+ token: token
});
this.domBuilder.pushElement(tagName, {
openTag: {
@@ -167,8 +179,7 @@ var Slowparse = (function() {
if (this.domBuilder.currentNode != this.domBuilder.fragment)
throw new ParseError({
type: "UNCLOSED_TAG",
- node: this.domBuilder.currentNode,
- position: this.stream.pos
+ node: this.domBuilder.currentNode
});
}
};
View
@@ -111,27 +111,42 @@ test("parsing of valid HTML", function() {
});
});
-test("parsing of invalid HTML: UNCLOSED_TAG", function() {
+test("parse error: UNCLOSED_TAG", function() {
var html = '<p class="foo">hello there';
var result = Slowparse.HTML(document, html);
var error = result.error;
var p = result.document.childNodes[0];
equal(p.nodeName, "P", "first child of generated DOM is <p>");
equal(error.type, "UNCLOSED_TAG", "parser dies b/c of unclosed tag");
- equal(error.position, html.length, "parser dies at end of string");
equal(error.node, p, "affiliated node of error is <p>");
assertParseInfo(html, p, "p", {
'parseInfo.openTag': '<p class="foo">'
});
});
-test("parsing of invalid HTML: INVALID_TAG_NAME", function() {
+test("parse error: INVALID_TAG_NAME", function() {
var html = '< p>hello there</p>';
var error = Slowparse.HTML(document, html).error;
equal(error.type, "INVALID_TAG_NAME", "parser dies b/c of invalid tag");
- equal(html.slice(error.position,
- error.position + error.value.length), error.value);
- equal(error.value, "");
+ assertParseInfo(html, html.slice(error.start, error.end), error.value);
+ equal(error.value, "<");
+});
+
+test("parse error: MISMATCHED_CLOSE_TAG", function() {
+ var html = '<p>hello there</i>';
+ var result = Slowparse.HTML(document, html);
+ var error = result.error;
+ var p = result.document.childNodes[0];
+
+ equal(error.type, "MISMATCHED_CLOSE_TAG");
+ assertParseInfo(html, html.slice(error.start, error.end), error.value);
+ equal(error.value, "</i");
+
+ equal(p.nodeName, "P", "first child of generated DOM is <p>");
+ equal(error.node, p, "affiliated node of error is <p>");
+ assertParseInfo(html, p, "p", {
+ 'parseInfo.openTag': '<p>'
+ });
});

0 comments on commit c0b38a9

Please sign in to comment.