Skip to content

Commit

Permalink
Fix an issue where a closing tag that does not correspond to any open…
Browse files Browse the repository at this point in the history
… tag in the doc will close out the root.
  • Loading branch information
isaacs committed Mar 24, 2010
1 parent db1104f commit 503522e
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,27 @@ function closeTag (parser) {
parser.state = S.TEXT;
return;
}
do {
if (!parser.strict) parser.tagName = parser.tagName[parser.tagCase]();
var closeTo = parser.tagName, close = parser.tags.pop();
if (!close) {
throw "wtf "+parser.tagName+" "+parser.tags+" "+parser.line+ " "+parser.position;
}
if (closeTo !== close.name) strictFail(parser, "Unexpected close tag.");
parser.tag = close;
parser.tagName = close.name;
// first make sure that the closing tag actually exists.
// <a><b></c></b></a> will close everything, otherwise.
var t = parser.tags.length;
if (!parser.strict) parser.tagName = parser.tagName[parser.tagCase]();
var closeTo = parser.tagName;
while (t --) {
var close = parser.tags[t];
if (close.name !== closeTo) {
// fail the first time in strict mode
strictFail(parser, "Unexpected close tag");
} else break;
}
// didn't find it. we already failed for strict, so just abort.
if (t < 0) return;
var s = parser.tags.length;
while (s --> t) {
parser.tag = parser.tags.pop();
parser.tagName = parser.tag.name;
emitNode(parser, "onclosetag", parser.tagName);
} while (closeTo !== close.name);
if (parser.tags.length === 0) parser.closedRoot = true;
}
if (t === 0) parser.closedRoot = true;
parser.tagName = parser.attribValue = parser.attribName = "";
parser.tag = null;
parser.state = S.TEXT;
Expand Down Expand Up @@ -489,7 +498,8 @@ function write (chunk) {
}
continue;
case S.ATTRIB_VALUE:
if (is(quote, c)) {
if (is(whitespace, c)) continue;
else if (is(quote, c)) {
parser.q = c;
parser.state = S.ATTRIB_VALUE_QUOTED;
} else {
Expand Down Expand Up @@ -571,7 +581,7 @@ function write (chunk) {
}
continue;
default:
throw "Unknown state: " + parser.state;
throw new Error(parser, "Unknown state: " + parser.state);
break;
}
} // while
Expand Down

0 comments on commit 503522e

Please sign in to comment.