diff --git a/lib/marked.js b/lib/marked.js index 6964429d35..fc0eee4615 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -310,7 +310,7 @@ Lexer.prototype.token = function(src, top) { blockquote = cap[0].replace(/^ *> ?/gm, ''); count = 1; - while (blockquote.match(/^ {0,3}>/)) { + while (linesMatch(blockquote, /^ {0,3}>/)) { count++; this.tokens.push({ type: 'blockquote_start' @@ -1188,7 +1188,7 @@ Parser.prototype.peek = function() { Parser.prototype.parseText = function() { var body = this.token.text; - while (this.peek().type === 'text') { + while (this.peek() && this.peek().type === 'text') { body += '\n' + this.next().text; } @@ -1261,15 +1261,10 @@ Parser.prototype.tok = function() { body = ''; - while (this.next().type !== 'blockquote_end') { - body += this.tok(); - } - - while (this.peek() && this.peek().type === 'blockquote_end') { - this.next(); - } - - for (i = 0; i < count; i++) { + while (count-- > 0) { + while (this.peek() && this.next().type !== 'blockquote_end') { + body += this.tok(); + } body = this.renderer.blockquote(body); } @@ -1280,7 +1275,7 @@ Parser.prototype.tok = function() { var ordered = this.token.ordered, start = this.token.start; - while (this.next().type !== 'list_end') { + while (this.peek() && this.next().type !== 'list_end') { body += this.tok(); } @@ -1296,7 +1291,7 @@ Parser.prototype.tok = function() { body += this.renderer.checkbox(checked); } - while (this.next().type !== 'list_item_end') { + while (this.peek() && this.next().type !== 'list_item_end') { body += !loose && this.token.type === 'text' ? this.parseText() : this.tok(); @@ -1516,6 +1511,17 @@ function splitCells(tableRow, count) { return cells; } +function linesMatch(str, reg) { + var lines = str.split('\n'); + for (var i = 0; i < lines.length; i++) { + if (!lines[i].match(reg)) { + return false; + } + } + + return true; +} + // Remove trailing 'c's. Equivalent to str.replace(/c*$/, ''). // /c*$/ is vulnerable to REDOS. // invert: Remove suffix of non-c chars instead. Default falsey. diff --git a/test/specs/new/nested_blockquote.html b/test/specs/new/nested_blockquote.html new file mode 100644 index 0000000000..3406e60e8b --- /dev/null +++ b/test/specs/new/nested_blockquote.html @@ -0,0 +1,17 @@ +
+

a

+
+

b

+
+
+
+
+

c

+
+
+
+
+

d

+
+

e

+
diff --git a/test/specs/new/nested_blockquote.md b/test/specs/new/nested_blockquote.md new file mode 100644 index 0000000000..ddf930b33a --- /dev/null +++ b/test/specs/new/nested_blockquote.md @@ -0,0 +1,9 @@ +>a +> +>>b + +>>c + +>>d +> +>e