From cc8a45288b59ca10a8fedaed9028072021be9999 Mon Sep 17 00:00:00 2001 From: Federico Soave Date: Sun, 13 Jan 2019 19:00:46 +0100 Subject: [PATCH 1/4] Allow only ONE level of nested parenthesis in link uris (as previously intended in 9f20c4682). Reverts 9c976cda1. Fixes #1405. Disable cm test 468. --- lib/marked.js | 2 +- test/new/link_redos.html | 5 +++++ test/new/link_redos.md | 2 ++ test/specs/commonmark/commonmark-spec.js | 4 ++-- 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 test/new/link_redos.html create mode 100644 test/new/link_redos.md diff --git a/lib/marked.js b/lib/marked.js index b4cfd3f727..9389b8cb2c 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -571,7 +571,7 @@ inline.tag = edit(inline.tag) .getRegex(); inline._label = /(?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?/; -inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\]*\)|[^\s\x00-\x1f()\\])*?)/; +inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\()]*\)|[^\s\x00-\x1f()\\])*?)/; inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/; inline.link = edit(inline.link) diff --git a/test/new/link_redos.html b/test/new/link_redos.html new file mode 100644 index 0000000000..fdbeb4d337 --- /dev/null +++ b/test/new/link_redos.html @@ -0,0 +1,5 @@ + + diff --git a/test/new/link_redos.md b/test/new/link_redos.md new file mode 100644 index 0000000000..effafa37ea --- /dev/null +++ b/test/new/link_redos.md @@ -0,0 +1,2 @@ +* 伪类:[:active](https://developer.mozilla.org/en-US/docs/Web/CSS/:active)、[:any-link](https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link)、[:blank](https://developer.mozilla.org/en-US/docs/Web/CSS/:blank)、[:checked](https://developer.mozilla.org/en-US/docs/Web/CSS/:checked)、[:current](https://developer.mozilla.org/en-US/docs/Web/CSS/:current)、[:default](https://developer.mozilla.org/en-US/docs/Web/CSS/:default)、[:defined](https://developer.mozilla.org/en-US/docs/Web/CSS/:defined)、[:dir()](https://developer.mozilla.org/en-US/docs/Web/CSS/:dir)、[:disabled](https://developer.mozilla.org/en-US/docs/Web/CSS/:disabled)、[:drop](https://developer.mozilla.org/en-US/docs/Web/CSS/:drop)、[:empty](https://developer.mozilla.org/en-US/docs/Web/CSS/:empty)、[:enabled](https://developer.mozilla.org/en-US/docs/Web/CSS/:enabled)、[:first](https://developer.mozilla.org/en-US/docs/Web/CSS/:first)、[:first-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child)、[:first-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type)、[:fullscreen](https://developer.mozilla.org/en-US/docs/Web/CSS/:fullscreen)、[:future](https://developer.mozilla.org/en-US/docs/Web/CSS/:future)、[:focus](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus)、[:focus-visible](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible)、[:focus-within](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within)、[:has()](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)、[:host](https://developer.mozilla.org/en-US/docs/Web/CSS/:host)、[:host()](https://developer.mozilla.org/en-US/docs/Web/CSS/:host())、[:host-context()](https://developer.mozilla.org/en-US/docs/Web/CSS/:host-context())、[:hover](https://developer.mozilla.org/en-US/docs/Web/CSS/:hover)、[:indeterminate](https://developer.mozilla.org/en-US/docs/Web/CSS/:indeterminate)、[:in-range](https://developer.mozilla.org/en-US/docs/Web/CSS/:in-range)、[:invalid](https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid)、[:is()](https://developer.mozilla.org/en-US/docs/Web/CSS/:is)、[:lang()](https://developer.mozilla.org/en-US/docs/Web/CSS/:lang)、[:last-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child)、[:last-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type)、[:left](https://developer.mozilla.org/en-US/docs/Web/CSS/:left)、[:link](https://developer.mozilla.org/en-US/docs/Web/CSS/:link)、[:local-link](https://developer.mozilla.org/en-US/docs/Web/CSS/:local-link)、[:not()](https://developer.mozilla.org/en-US/docs/Web/CSS/:not)、[:nth-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child)、[:nth-col()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-col)、[:nth-last-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child)、[:nth-last-col()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-col)、[:nth-last-of-type()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-of-type)、[:nth-of-type()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type)、[:only-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child)、[:only-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type)、[:optional](https://developer.mozilla.org/en-US/docs/Web/CSS/:optional)、[:out-of-range](https://developer.mozilla.org/en-US/docs/Web/CSS/:out-of-range)、[:past](https://developer.mozilla.org/en-US/docs/Web/CSS/:past)、[:placeholder-shown](https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown)、[:read-only](https://developer.mozilla.org/en-US/docs/Web/CSS/:read-only)、[:read-write](https://developer.mozilla.org/en-US/docs/Web/CSS/:read-write)、[:required](https://developer.mozilla.org/en-US/docs/Web/CSS/:required)、[:right](https://developer.mozilla.org/en-US/docs/Web/CSS/:right)、[:root](https://developer.mozilla.org/en-US/docs/Web/CSS/:root)、[:scope](https://developer.mozilla.org/en-US/docs/Web/CSS/:scope)、[:target](https://developer.mozilla.org/en-US/docs/Web/CSS/:target)、[:target-within](https://developer.mozilla.org/en-US/docs/Web/CSS/:target-within)、[:user-invalid](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid)、[:valid](https://developer.mozilla.org/en-US/docs/Web/CSS/:valid)、[:visited](https://developer.mozilla.org/en-US/docs/Web/CSS/:visited)、[:where()](https://developer.mozilla.org/en-US/docs/Web/CSS/:where) +* 伪元素:[::after (:after)](https://developer.mozilla.org/en-US/docs/Web/CSS/::after)、[::backdrop](https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop)、[::before (:before)](https://developer.mozilla.org/en-US/docs/Web/CSS/::before)、[::cue (:cue)](https://developer.mozilla.org/en-US/docs/Web/CSS/::cue)、[::first-letter (:first-letter)](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter)、[::first-line (:first-line)](https://developer.mozilla.org/en-US/docs/Web/CSS/::first-line)、[::grammar-error](https://developer.mozilla.org/en-US/docs/Web/CSS/::grammar-error)、[::marker](https://developer.mozilla.org/en-US/docs/Web/CSS/::marker)、[::placeholder](https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder)、[::selection](https://developer.mozilla.org/en-US/docs/Web/CSS/::selection)、[::slotted()](https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted)、[::spelling-error](https://developer.mozilla.org/en-US/docs/Web/CSS/::spelling-error) \ No newline at end of file diff --git a/test/specs/commonmark/commonmark-spec.js b/test/specs/commonmark/commonmark-spec.js index ec270bb831..fbbe47b321 100644 --- a/test/specs/commonmark/commonmark-spec.js +++ b/test/specs/commonmark/commonmark-spec.js @@ -46,7 +46,7 @@ var messenger = new Messenger(); |Entity and numeric character references | 9 of 12 | 75%| |Code spans | 11 of 17 | 65%| |Emphasis and strong emphasis | 79 of 128 | 61%| -|Links | 69 of 84 | 82%| +|Links | 68 of 84 | 81%| |Images | 15 of 22 | 68%| |Autolinks | 15 of 19 | 79%| |Raw HTML | 19 of 21 | 90%| @@ -352,7 +352,7 @@ describe('CommonMark 0.28 Links', function() { var section = 'Links'; // var shouldPassButFails = []; - var shouldPassButFails = [474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509]; + var shouldPassButFails = [468, 474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509]; var willNotBeAttemptedByCoreTeam = []; From 77a7826fbefd2b72a65bb5b47fbc5ff65b05d1d9 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 7 Feb 2019 10:44:10 -0600 Subject: [PATCH 2/4] use function to check for end of link --- lib/marked.js | 29 +++++++++++++++++++++++- test/specs/commonmark/commonmark-spec.js | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/marked.js b/lib/marked.js index 9389b8cb2c..a8b41a5774 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -571,7 +571,7 @@ inline.tag = edit(inline.tag) .getRegex(); inline._label = /(?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?/; -inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f\\()]*\)|[^\s\x00-\x1f()\\])*?)/; +inline._href = /\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)/; inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/; inline.link = edit(inline.link) @@ -719,6 +719,12 @@ InlineLexer.prototype.output = function(src) { // link if (cap = this.rules.link.exec(src)) { + var lastParenIndex = findClosingBracket(cap[2], '()'); + if (lastParenIndex > -1) { + var removeChars = cap[2].length - lastParenIndex; + cap[2] = cap[2].substring(0, lastParenIndex); + cap[0] = cap[0].substring(0, cap[0].length - removeChars); + } src = src.substring(cap[0].length); this.inLink = true; href = cap[2]; @@ -1503,6 +1509,27 @@ function rtrim(str, c, invert) { return str.substr(0, str.length - suffLen); } +function findClosingBracket(str, b) { + if (str.indexOf(b[1]) === -1) { + return -1; + } + var level = 0; + for (var i = 0; i < str.length; i++) { + if (str[i] === '\\') { + i++; + } else if (str[i] === b[0]) { + level++; + continue; + } else if (str[i] === b[1]) { + level--; + if (level < 0) { + return i; + } + } + } + return -1; +} + /** * Marked */ diff --git a/test/specs/commonmark/commonmark-spec.js b/test/specs/commonmark/commonmark-spec.js index fbbe47b321..dae7714243 100644 --- a/test/specs/commonmark/commonmark-spec.js +++ b/test/specs/commonmark/commonmark-spec.js @@ -352,7 +352,7 @@ describe('CommonMark 0.28 Links', function() { var section = 'Links'; // var shouldPassButFails = []; - var shouldPassButFails = [468, 474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509]; + var shouldPassButFails = [474, 478, 483, 489, 490, 491, 495, 496, 497, 499, 503, 504, 507, 508, 509]; var willNotBeAttemptedByCoreTeam = []; From 91a21c2bcda42679bdc608ef3449e8f482217176 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 7 Feb 2019 10:52:09 -0600 Subject: [PATCH 3/4] update percent complete --- test/specs/commonmark/commonmark-spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/specs/commonmark/commonmark-spec.js b/test/specs/commonmark/commonmark-spec.js index dae7714243..ec270bb831 100644 --- a/test/specs/commonmark/commonmark-spec.js +++ b/test/specs/commonmark/commonmark-spec.js @@ -46,7 +46,7 @@ var messenger = new Messenger(); |Entity and numeric character references | 9 of 12 | 75%| |Code spans | 11 of 17 | 65%| |Emphasis and strong emphasis | 79 of 128 | 61%| -|Links | 68 of 84 | 81%| +|Links | 69 of 84 | 82%| |Images | 15 of 22 | 68%| |Autolinks | 15 of 19 | 79%| |Raw HTML | 19 of 21 | 90%| From ceee1784fcea181890e300e9646625da9c87a3ab Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 8 Feb 2019 08:14:59 -0600 Subject: [PATCH 4/4] remove continue --- lib/marked.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/marked.js b/lib/marked.js index a8b41a5774..8ffcf36f54 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -1519,7 +1519,6 @@ function findClosingBracket(str, b) { i++; } else if (str[i] === b[0]) { level++; - continue; } else if (str[i] === b[1]) { level--; if (level < 0) {