fix(HTML Parser): fix nasty bug where malformed HTML would hang showdown

When feeding malformed HTML to showdown, the library would enter an infinite loop,
effectively halting showdown's execution.

Closes #393
This commit is contained in:
Estevao Soares dos Santos 2017-06-02 03:29:29 +01:00
parent 1eee6afe29
commit 6566c72cc1
7 changed files with 13 additions and 4 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -50,17 +50,24 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
for (var i = 0; i < blockTags.length; ++i) {
var opTagPos,
var opTagPos, ii = 0,
rgx1 = new RegExp('^ {0,3}<' + blockTags[i] + '\\b[^>]*>', 'im'),
patLeft = '<' + blockTags[i] + '\\b[^>]*>',
patRight = '</' + blockTags[i] + '>';
// 1. Look for the first position of the first opening HTML tag in the text
while ((opTagPos = showdown.helper.regexIndexOf(text, rgx1)) !== -1) {
//2. Split the text in that position
var subTexts = showdown.helper.splitAtIndex(text, opTagPos);
var subTexts = showdown.helper.splitAtIndex(text, opTagPos),
//3. Match recursively
subTexts[1] = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');
text = subTexts[0].concat(subTexts[1]);
newSubText1 = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');
// prevent an infinite loop
if (newSubText1 === subTexts[1]) {
break;
}
text = subTexts[0].concat(newSubText1);
ii++;
}
}
// HR SPECIAL CASE

View File

@ -0,0 +1 @@
<p><p>malformed<p></p>

View File

@ -0,0 +1 @@
<p>malformed<p>