Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
added MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomax committed Sep 27, 2017
1 parent f9a6920 commit 63d04d7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"scripts": {
"build": "browserify ./src/index.js -o slowparse.js --standalone Slowparse",
"test": "node test.js",
"dev": "npm run build && npm run test"
"debug": "npm run build && node inspect test.js",
"test": "npm run build && node test.js"
},
"repository": {
"type": "git",
Expand Down
38 changes: 34 additions & 4 deletions slowparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,14 @@ module.exports = (function(){
start: token.interval.start
};
var openTagName = this.domBuilder.currentNode.nodeName.toLowerCase();
if (closeTagName != openTagName)
throw new ParseError("MISMATCHED_CLOSE_TAG", this, openTagName,
closeTagName, token);
if (closeTagName != openTagName) {

if (this.domBuilder.currentNode.closeWarnings) {
throw new ParseError("MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING", this, closeTagName, token);
}

throw new ParseError("MISMATCHED_CLOSE_TAG", this, openTagName, closeTagName, token);
}
this._parseEndCloseTag();
}

Expand All @@ -999,12 +1004,25 @@ module.exports = (function(){
var activeTagName = activeTagNode.nodeName.toLowerCase();
if(this._knownOmittableCloseTags(activeTagName, tagName)) {
this.domBuilder.popElement();

if (!this.domBuilder.currentNode.closeWarnings) {
this.domBuilder.currentNode.closeWarnings = [];
}

var childNodes = this.domBuilder.currentNode.childNodes,
position = childNodes.length - 1;

this.domBuilder.currentNode.closeWarnings.push({
tagName: activeTagName,
position: position,
parseInfo: childNodes[position].parseInfo
});
}
}
// Store currentNode as the parentTagNode
parentTagNode = this.domBuilder.currentNode;
this.domBuilder.pushElement(tagName, parseInfo, nameSpace);

this.domBuilder.pushElement(tagName, parseInfo, nameSpace);
if (!this.stream.end())
this._parseEndOpenTag(tagName);
}
Expand Down Expand Up @@ -1468,6 +1486,18 @@ module.exports = (function() {
cursor: closeTag.start
};
},
MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING: function(parser, closeTagName, token) {
var warnings = parser.domBuilder.currentNode.closeWarnings,
tag = warnings[0],
closeTag = this._combine({
name: closeTagName
}, token.interval);
return {
openTag: tag.parseInfo.openTag,
closeTag: closeTag,
cursor: closeTag.start
};
},
MISMATCHED_CLOSE_TAG: function(parser, openTagName, closeTagName, token) {
var openTag = this._combine({
name: openTagName
Expand Down
26 changes: 22 additions & 4 deletions src/HTMLParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,14 @@ module.exports = (function(){
start: token.interval.start
};
var openTagName = this.domBuilder.currentNode.nodeName.toLowerCase();
if (closeTagName != openTagName)
throw new ParseError("MISMATCHED_CLOSE_TAG", this, openTagName,
closeTagName, token);
if (closeTagName != openTagName) {

if (this.domBuilder.currentNode.closeWarnings) {
throw new ParseError("MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING", this, closeTagName, token);
}

throw new ParseError("MISMATCHED_CLOSE_TAG", this, openTagName, closeTagName, token);
}
this._parseEndCloseTag();
}

Expand All @@ -341,12 +346,25 @@ module.exports = (function(){
var activeTagName = activeTagNode.nodeName.toLowerCase();
if(this._knownOmittableCloseTags(activeTagName, tagName)) {
this.domBuilder.popElement();

if (!this.domBuilder.currentNode.closeWarnings) {
this.domBuilder.currentNode.closeWarnings = [];
}

var childNodes = this.domBuilder.currentNode.childNodes,
position = childNodes.length - 1;

this.domBuilder.currentNode.closeWarnings.push({
tagName: activeTagName,
position: position,
parseInfo: childNodes[position].parseInfo
});
}
}
// Store currentNode as the parentTagNode
parentTagNode = this.domBuilder.currentNode;
this.domBuilder.pushElement(tagName, parseInfo, nameSpace);

this.domBuilder.pushElement(tagName, parseInfo, nameSpace);
if (!this.stream.end())
this._parseEndOpenTag(tagName);
}
Expand Down
12 changes: 12 additions & 0 deletions src/ParseErrorBuilders.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ module.exports = (function() {
cursor: closeTag.start
};
},
MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING: function(parser, closeTagName, token) {
var warnings = parser.domBuilder.currentNode.closeWarnings,
tag = warnings[0],
closeTag = this._combine({
name: closeTagName
}, token.interval);
return {
openTag: tag.parseInfo.openTag,
closeTag: closeTag,
cursor: closeTag.start
};
},
MISMATCHED_CLOSE_TAG: function(parser, openTagName, closeTagName, token) {
var openTag = this._combine({
name: openTagName
Expand Down
9 changes: 7 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ var Slowparse = require("./slowparse.js"),
validators = require("./test/node/qunit-shim.js")(Slowparse, JSDOM);

console.log("Testing Slowparse library:");
var failureCount = require("./test/test-slowparse.js")(Slowparse, window, document, validators);
if (failureCount > 0) { console.log(failureCount + " tests failed."); }

var testRunner = require("./test/test-slowparse.js");
var failureCount = testRunner(Slowparse, window, document, validators);

if (failureCount > 0) {
console.log(`${failureCount} test${failureCount>1 ? 's' : ''} failed.`);
}

process.exit(failureCount);
12 changes: 12 additions & 0 deletions test/test-slowparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,18 @@ module.exports = function(Slowparse, window, document, validators) {
});


test("correctly flag the opening tag for a missing closing tag", function () {
var html = '<body><p><h1><a href="">test</a></h1></p></body>';
var result = parse(html);
equal(result.error, {
type: 'MISMATCHED_CLOSE_TAG_DUE_TO_EARLIER_AUTO_CLOSING',
openTag: { start: 6, end: 9 },
closeTag: { name: 'p', start: 37, end: 40 },
cursor: 37
});
});


// specifically CSS testing

test("parsing empty CSS document", function() {
Expand Down

0 comments on commit 63d04d7

Please sign in to comment.