Update clearHtmlCommentText helper to match HTML specification better; "--" within a comment does not make it invalid (fixes #361).

This commit is contained in:
David Anson 2021-01-28 21:45:53 -08:00
parent df4aa9f4e8
commit 838afe0a00
4 changed files with 37 additions and 21 deletions

View file

@ -128,11 +128,13 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
} }
var comment = text.slice(i + htmlCommentBegin.length, j); var comment = text.slice(i + htmlCommentBegin.length, j);
if ((comment.length > 0) && if ((comment.length > 0) &&
(comment[0] !== ">") && !comment.startsWith(">") &&
(comment[comment.length - 1] !== "-") && !comment.startsWith("->") &&
!comment.includes("--") && !comment.endsWith("<!-") &&
(text.slice(i, j + htmlCommentEnd.length) !comment.includes("<!--") &&
.search(inlineCommentRe) === -1)) { // !comment.includes("-->") &&
!comment.includes("--!>") &&
(text.slice(i, j + htmlCommentEnd.length).search(inlineCommentRe) === -1)) {
var blanks = comment var blanks = comment
.replace(/[^\r\n]/g, " ") .replace(/[^\r\n]/g, " ")
.replace(/ ([\r\n])/g, "\\$1"); .replace(/ ([\r\n])/g, "\\$1");

View file

@ -114,12 +114,16 @@ module.exports.clearHtmlCommentText = function clearHtmlCommentText(text) {
break; break;
} }
const comment = text.slice(i + htmlCommentBegin.length, j); const comment = text.slice(i + htmlCommentBegin.length, j);
if ((comment.length > 0) && if (
(comment[0] !== ">") && (comment.length > 0) &&
(comment[comment.length - 1] !== "-") && !comment.startsWith(">") &&
!comment.includes("--") && !comment.startsWith("->") &&
(text.slice(i, j + htmlCommentEnd.length) !comment.endsWith("<!-") &&
.search(inlineCommentRe) === -1)) { !comment.includes("<!--") &&
// !comment.includes("-->") &&
!comment.includes("--!>") &&
(text.slice(i, j + htmlCommentEnd.length).search(inlineCommentRe) === -1)
) {
const blanks = comment const blanks = comment
.replace(/[^\r\n]/g, " ") .replace(/[^\r\n]/g, " ")
.replace(/ ([\r\n])/g, "\\$1"); .replace(/ ([\r\n])/g, "\\$1");

View file

@ -20,7 +20,7 @@ Hard tab
<!-- <!--
Hard tab {MD010} Hard tab {MD010}
Invalid--comment Invalid--!>comment
Hard tab {MD010} Hard tab {MD010}
--> -->

View file

@ -12,6 +12,7 @@ test("clearHtmlCommentTextValid", (t) => {
"<!-- text -->", "<!-- text -->",
"<!--text-->", "<!--text-->",
"<!-- -->", "<!-- -->",
"<!-- -- -->",
"<!---->", "<!---->",
"<!---text-->", "<!---text-->",
"<!--text-text-->", "<!--text-text-->",
@ -49,6 +50,7 @@ test("clearHtmlCommentTextValid", (t) => {
"<!-- -->", "<!-- -->",
"<!-- -->", "<!-- -->",
"<!-- -->", "<!-- -->",
"<!-- -->",
"<!---->", "<!---->",
"<!-- -->", "<!-- -->",
"<!-- -->", "<!-- -->",
@ -94,18 +96,26 @@ test("clearHtmlCommentTextInvalid", (t) => {
"<!->", "<!->",
"<!-->", "<!-->",
"<!--->", "<!--->",
"<!-->-->",
"<!--->-->",
"<!----->",
"<!------>",
"<!-- -- -->",
"<!-->-->",
"<!--> -->", "<!--> -->",
"<!--->-->",
"<!-->text-->", "<!-->text-->",
"<!--->text-->", "<!--->text-->",
"<!--text--->", "<!---->",
"<!--te--xt-->" // Restrictions from specification
"<!-->-->",
"<!-->t-->",
"<!--->-->",
"<!--->t-->",
"<!--<!--t-->",
"<!--t<!---->",
"<!--t<!--t-->",
// "<!---->t-->",
// "<!--t-->-->",
// "<!--t-->t-->",
"<!----!>t-->",
"<!--t--!>-->",
"<!--t--!>t-->",
"<!--<!--->",
"<!--t<!--->"
]; ];
const actual = helpers.clearHtmlCommentText(invalidComments.join("\n")); const actual = helpers.clearHtmlCommentText(invalidComments.join("\n"));
const expected = invalidComments.join("\n"); const expected = invalidComments.join("\n");