Browse Source

Removed unused code & improved tests coverage for some rules.

pull/24/head
Vitaly Puzrin 10 years ago
parent
commit
04ee60d3c4
  1. 2
      lib/common/html_re.js
  2. 2
      lib/common/utils.js
  3. 30
      lib/renderer.js
  4. 12
      lib/rules_inline/del.js
  5. 27
      lib/rules_inline/emphasis.js
  6. 63
      test/fixtures/markdown-it/commonmark_extras.txt

2
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;
};

2
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

30
lib/renderer.js

@ -210,36 +210,6 @@ rules.del_close = function (/* tokens, idx, options, env */) {
};
rules.ins_open = function (/* tokens, idx, options, env */) {
return '<ins>';
};
rules.ins_close = function (/* tokens, idx, options, env */) {
return '</ins>';
};
rules.mark_open = function (/* tokens, idx, options, env */) {
return '<mark>';
};
rules.mark_close = function (/* tokens, idx, options, env */) {
return '</mark>';
};
rules.sub_open = function (/*tokens, idx, options, env */) {
return '<sub>';
};
rules.sub_close = function (/*tokens, idx, options, env */) {
return '</sub>';
};
rules.sup_open = function (/*tokens, idx, options, env */) {
return '<sup>';
};
rules.sup_close = function (/*tokens, idx, options, env */) {
return '</sup>';
};
rules.hardbreak = function (tokens, idx, options /*, env */) {
return options.xhtmlOut ? '<br />\n' : '<br>\n';
};

12
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;

27
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;

63
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
.
</ 123>
.
@ -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>
.
<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/α%CE%B2γ%CE%B4</a></p>
.
Autolinks do not allow escaping:
.
<http://example.com/\[\>
.
<p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
.
Should not throw exception on mailformed URI
.
[foo](<&#x25;test>)
.
<p><a href="%25test">foo</a></p>
.
Coverage. Directive can terminate paragraph.
.
a
<?php
@ -99,27 +115,40 @@ a
<?php
.
Normalize link destination, but not text inside it:
Coverage. Nested email autolink (silent mode)
.
<http://example.com/α%CE%B2γ%CE%B4>
*<foo@bar.com>*
.
<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/α%CE%B2γ%CE%B4</a></p>
<p><em><a href="mailto:foo@bar.com">foo@bar.com</a></em></p>
.
Autolinks do not allow escaping:
Coverage. Unpaired nested backtick (silent mode)
.
<http://example.com/\[\>
*`foo*
.
<p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
<p><em>`foo</em></p>
.
Should not throw exception on mailformed URI
Coverage. Entities.
.
*&*
*&#x20;*
*&amp;*
.
[foo](<&#x25;test>)
<p><em>&amp;</em></p>
<p><em> </em></p>
<p><em>&amp;</em></p>
.
<p><a href="%25test">foo</a></p>
Coverage. Escape.
.
*\a*
.
<p><em>\a</em></p>
.

Loading…
Cancel
Save