From 267ac38ca06c722ed8158e008c5309f864a5a97a Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 3 Jun 2019 22:52:57 -0500 Subject: [PATCH 1/6] add nested_blockquote test --- test/specs/new/nested_blockquote.html | 6 ++++++ test/specs/new/nested_blockquote.md | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 test/specs/new/nested_blockquote.html create mode 100644 test/specs/new/nested_blockquote.md diff --git a/test/specs/new/nested_blockquote.html b/test/specs/new/nested_blockquote.html new file mode 100644 index 0000000000..fffc937bce --- /dev/null +++ b/test/specs/new/nested_blockquote.html @@ -0,0 +1,6 @@ +
+

a

+
+

b

+
+
diff --git a/test/specs/new/nested_blockquote.md b/test/specs/new/nested_blockquote.md new file mode 100644 index 0000000000..f84c7a9f4f --- /dev/null +++ b/test/specs/new/nested_blockquote.md @@ -0,0 +1,2 @@ +>a +>>b From e683d42fc576df2665672fb3238e1e062605c570 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 3 Jun 2019 22:53:33 -0500 Subject: [PATCH 2/6] fix nested blockquote at the end --- lib/marked.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/marked.js b/lib/marked.js index 6964429d35..acae154bd3 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -1261,7 +1261,7 @@ Parser.prototype.tok = function() { body = ''; - while (this.next().type !== 'blockquote_end') { + while (this.peek() && this.next().type !== 'blockquote_end') { body += this.tok(); } From 9cc317ede051e494ce89bcb79f9e60f779605b70 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 4 Jun 2019 00:31:58 -0500 Subject: [PATCH 3/6] expand tests --- test/specs/new/nested_blockquote.html | 11 +++++++++++ test/specs/new/nested_blockquote.md | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/test/specs/new/nested_blockquote.html b/test/specs/new/nested_blockquote.html index fffc937bce..3406e60e8b 100644 --- a/test/specs/new/nested_blockquote.html +++ b/test/specs/new/nested_blockquote.html @@ -4,3 +4,14 @@

b

+
+
+

c

+
+
+
+
+

d

+
+

e

+
diff --git a/test/specs/new/nested_blockquote.md b/test/specs/new/nested_blockquote.md index f84c7a9f4f..ddf930b33a 100644 --- a/test/specs/new/nested_blockquote.md +++ b/test/specs/new/nested_blockquote.md @@ -1,2 +1,9 @@ >a +> >>b + +>>c + +>>d +> +>e From 416e476ca9f94b1dbc4f65e0c4facf468ec9d175 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 4 Jun 2019 00:32:20 -0500 Subject: [PATCH 4/6] fix not totally nested --- lib/marked.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index acae154bd3..2ff367c4a8 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -193,6 +193,7 @@ Lexer.prototype.token = function(src, top) { item, listStart, listItems, + lines, t, space, i, @@ -310,7 +311,7 @@ Lexer.prototype.token = function(src, top) { blockquote = cap[0].replace(/^ *> ?/gm, ''); count = 1; - while (blockquote.match(/^ {0,3}>/)) { + while (lineMatch(blockquote, /^ {0,3}>/)) { count++; this.tokens.push({ type: 'blockquote_start' @@ -1188,7 +1189,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,16 +1262,13 @@ Parser.prototype.tok = function() { body = ''; - while (this.peek() && 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.peek().type !== 'blockquote_end') { + this.next(); + body += this.tok(); + } body = this.renderer.blockquote(body); + this.next(); } return body; @@ -1280,7 +1278,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 +1294,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 +1514,24 @@ function splitCells(tableRow, count) { return cells; } +function lineMatch(str, reg, some) { + var lines = str.split('\n'); + for (var i = 0; i < lines.length; i++) { + var match = lines[i].match(reg); + if (some && match) { + return true; + } else if (!some && !match) { + return false; + } + } + + if (some) { + return false; + } else { + 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. From 50984f49d171016f47a3323ef4dd2eec8407b108 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 4 Jun 2019 08:24:38 -0500 Subject: [PATCH 5/6] clean up code --- lib/marked.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index 2ff367c4a8..d8065ed513 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -193,7 +193,6 @@ Lexer.prototype.token = function(src, top) { item, listStart, listItems, - lines, t, space, i, @@ -1263,12 +1262,10 @@ Parser.prototype.tok = function() { body = ''; while (count-- > 0) { - while (this.peek() && this.peek().type !== 'blockquote_end') { - this.next(); + while (this.peek() && this.next().type !== 'blockquote_end') { body += this.tok(); } body = this.renderer.blockquote(body); - this.next(); } return body; From 40dd186200aa7f80cb93ef7a89dd8be640ed4d86 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 4 Jun 2019 15:08:32 -0500 Subject: [PATCH 6/6] trim linesMatch --- lib/marked.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index d8065ed513..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 (lineMatch(blockquote, /^ {0,3}>/)) { + while (linesMatch(blockquote, /^ {0,3}>/)) { count++; this.tokens.push({ type: 'blockquote_start' @@ -1511,22 +1511,15 @@ function splitCells(tableRow, count) { return cells; } -function lineMatch(str, reg, some) { +function linesMatch(str, reg) { var lines = str.split('\n'); for (var i = 0; i < lines.length; i++) { - var match = lines[i].match(reg); - if (some && match) { - return true; - } else if (!some && !match) { + if (!lines[i].match(reg)) { return false; } } - if (some) { - return false; - } else { - return true; - } + return true; } // Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').