From 04ee60d3c400e4a824ff3f1fb5ed8d9e7e7facbd Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Thu, 1 Jan 2015 06:47:04 +0300 Subject: [PATCH] Removed unused code & improved tests coverage for some rules. --- lib/common/html_re.js | 2 +- lib/common/utils.js | 2 +- lib/renderer.js | 30 --------- lib/rules_inline/del.js | 12 ++-- lib/rules_inline/emphasis.js | 27 ++++---- .../markdown-it/commonmark_extras.txt | 63 ++++++++++++++----- 6 files changed, 68 insertions(+), 68 deletions(-) diff --git a/lib/common/html_re.js b/lib/common/html_re.js index f4264dc..44c7d2a 100644 --- a/lib/common/html_re.js +++ b/lib/common/html_re.js @@ -11,7 +11,7 @@ function replace(regex, options) { if (!name) { return new RegExp(regex, options); } - val = val.source || val; + val = val.source; regex = regex.replace(name, val); return self; }; diff --git a/lib/common/utils.js b/lib/common/utils.js index 6b9756e..1ba49ac 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -10,7 +10,7 @@ function isString(obj) { return _class(obj) === '[object String]'; } var _hasOwnProperty = Object.prototype.hasOwnProperty; function has(object, key) { - return object ? _hasOwnProperty.call(object, key) : false; + return _hasOwnProperty.call(object, key); } // Merge objects diff --git a/lib/renderer.js b/lib/renderer.js index 78a693f..f6685f7 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -210,36 +210,6 @@ rules.del_close = function (/* tokens, idx, options, env */) { }; -rules.ins_open = function (/* tokens, idx, options, env */) { - return ''; -}; -rules.ins_close = function (/* tokens, idx, options, env */) { - return ''; -}; - - -rules.mark_open = function (/* tokens, idx, options, env */) { - return ''; -}; -rules.mark_close = function (/* tokens, idx, options, env */) { - return ''; -}; - - -rules.sub_open = function (/*tokens, idx, options, env */) { - return ''; -}; -rules.sub_close = function (/*tokens, idx, options, env */) { - return ''; -}; -rules.sup_open = function (/*tokens, idx, options, env */) { - return ''; -}; -rules.sup_close = function (/*tokens, idx, options, env */) { - return ''; -}; - - rules.hardbreak = function (tokens, idx, options /*, env */) { return options.xhtmlOut ? '
\n' : '
\n'; }; diff --git a/lib/rules_inline/del.js b/lib/rules_inline/del.js index 11b204b..4e35a08 100644 --- a/lib/rules_inline/del.js +++ b/lib/rules_inline/del.js @@ -47,7 +47,8 @@ module.exports = function(state, silent) { startCount = res.delims; if (!res.can_open) { state.pos += startCount; - if (!silent) { state.pending += state.src.slice(start, state.pos); } + // Earlier we checked !silent, but this implementation does not need it + state.pending += state.src.slice(start, state.pos); return true; } @@ -89,11 +90,10 @@ module.exports = function(state, silent) { state.posMax = state.pos; state.pos = start + 2; - if (!silent) { - state.push({ type: 'del_open', level: state.level++ }); - state.md.inline.tokenize(state); - state.push({ type: 'del_close', level: --state.level }); - } + // Earlier we checked !silent, but this implementation does not need it + state.push({ type: 'del_open', level: state.level++ }); + state.md.inline.tokenize(state); + state.push({ type: 'del_close', level: --state.level }); state.pos = state.posMax + 2; state.posMax = max; diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index 45f359b..8030a9f 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -62,7 +62,8 @@ module.exports = function emphasis(state, silent) { startCount = res.delims; if (!res.can_open) { state.pos += startCount; - if (!silent) { state.pending += state.src.slice(start, state.pos); } + // Earlier we checked !silent, but this implementation does not need it + state.pending += state.src.slice(start, state.pos); return true; } @@ -118,20 +119,20 @@ module.exports = function emphasis(state, silent) { state.posMax = state.pos; state.pos = start + startCount; - if (!silent) { - // we have `startCount` starting and ending markers, - // now trying to serialize them into tokens - for (count = startCount; count > 1; count -= 2) { - state.push({ type: 'strong_open', level: state.level++ }); - } - if (count % 2) { state.push({ type: 'em_open', level: state.level++ }); } + // Earlier we checked !silent, but this implementation does not need it + + // we have `startCount` starting and ending markers, + // now trying to serialize them into tokens + for (count = startCount; count > 1; count -= 2) { + state.push({ type: 'strong_open', level: state.level++ }); + } + if (count % 2) { state.push({ type: 'em_open', level: state.level++ }); } - state.md.inline.tokenize(state); + state.md.inline.tokenize(state); - if (count % 2) { state.push({ type: 'em_close', level: --state.level }); } - for (count = startCount; count > 1; count -= 2) { - state.push({ type: 'strong_close', level: --state.level }); - } + if (count % 2) { state.push({ type: 'em_close', level: --state.level }); } + for (count = startCount; count > 1; count -= 2) { + state.push({ type: 'strong_close', level: --state.level }); } state.pos = state.posMax + startCount; diff --git a/test/fixtures/markdown-it/commonmark_extras.txt b/test/fixtures/markdown-it/commonmark_extras.txt index 478a75b..98a8e2a 100644 --- a/test/fixtures/markdown-it/commonmark_extras.txt +++ b/test/fixtures/markdown-it/commonmark_extras.txt @@ -1,5 +1,4 @@ Regression tests for link backtracking optimizations: - . [[some unrelated text [link] @@ -10,7 +9,6 @@ Regression tests for link backtracking optimizations: This is not a valid emphasis, because \n considered a whitespace: - . **test ** @@ -33,7 +31,6 @@ test Link label has priority over emphasis (not covered by commonmark tests): - . [**link]()** @@ -45,7 +42,6 @@ Link label has priority over emphasis (not covered by commonmark tests): Issue #55: - . ![test] @@ -57,7 +53,6 @@ Issue #55: Should unescape only needed things in link destinations/titles: - . [test](<\f\o\o\>\\>) . @@ -72,7 +67,6 @@ Should unescape only needed things in link destinations/titles: Not a closing tag - . . @@ -81,7 +75,6 @@ Not a closing tag Not a list item - . 1.list . @@ -89,8 +82,31 @@ Not a list item . -Coverage. Direcctive can terminate paragraph. +Normalize link destination, but not text inside it: +. + +. +

http://example.com/α%CE%B2γ%CE%B4

+. + +Autolinks do not allow escaping: +. + +. +

http://example.com/\[\

+. + + +Should not throw exception on mailformed URI +. +[foo](<%test>) +. +

foo

+. + + +Coverage. Directive can terminate paragraph. . a +** . -

http://example.com/α%CE%B2γ%CE%B4

+

foo@bar.com

. -Autolinks do not allow escaping: +Coverage. Unpaired nested backtick (silent mode) . - +*`foo* . -

http://example.com/\[\

+

`foo

. -Should not throw exception on mailformed URI +Coverage. Entities. +. +*&* + +* * +*&* . -[foo](<%test>) +

&

+

+

&

. -

foo

+ + +Coverage. Escape. +. +*\a* +. +

\a

.