Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested blockquote error #1495

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}

Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions test/specs/new/nested_blockquote.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<blockquote>
<p>a</p>
<blockquote>
<p>b</p>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<p>c</p>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<p>d</p>
</blockquote>
<p>e</p>
</blockquote>
9 changes: 9 additions & 0 deletions test/specs/new/nested_blockquote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
>a
>
>>b

>>c

>>d
>
>e