Browse Source

Improved coverage and removed unused code

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
054ff554f5
  1. 8
      lib/parser_inline.js
  2. 28
      lib/renderer.js
  3. 22
      test/misc.js

8
lib/parser_inline.js

@ -30,13 +30,7 @@ var _rules = [
var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ]; var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ];
function validateLink(url) { function validateLink(url) {
var str = ''; var str = decodeURI(url).trim().toLowerCase();
try {
str = decodeURI(url).trim().toLowerCase();
} catch (_) {}
if (!str) { return false; }
if (str.indexOf(':') >= 0 && BAD_PROTOCOLS.indexOf(str.split(':')[0]) >= 0) { if (str.indexOf(':') >= 0 && BAD_PROTOCOLS.indexOf(str.split(':')[0]) >= 0) {
return false; return false;

28
lib/renderer.js

@ -9,19 +9,6 @@ var replaceEntities = require('./common/utils').replaceEntities;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Helpers // Helpers
function escapeUrl(str) {
try {
return encodeURI(str);
} catch (__) {}
return '';
}
function unescapeUrl(str) {
try {
return decodeURI(str);
} catch (__) {}
return '';
}
var HTML_ESCAPE_TEST_RE = /[&<>"]/; var HTML_ESCAPE_TEST_RE = /[&<>"]/;
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g; var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
var HTML_REPLACEMENTS = { var HTML_REPLACEMENTS = {
@ -44,13 +31,11 @@ function escapeHtml(str) {
function nextToken(tokens, idx) { function nextToken(tokens, idx) {
if (++idx >= tokens.length - 2) { return idx; } if (++idx >= tokens.length - 2) { return idx; }
if (tokens[idx].type === 'paragraph_open' && tokens[idx].tight) { if ((tokens[idx].type === 'paragraph_open' && tokens[idx].tight) &&
if (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) { (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) &&
if (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight) { (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight)) {
return nextToken(tokens, idx + 2); return nextToken(tokens, idx + 2);
} }
}
}
return idx; return idx;
} }
@ -72,8 +57,7 @@ var rules = {};
rules.abbr_open = function (tokens, idx/*, options*/) { rules.abbr_open = function (tokens, idx/*, options*/) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
return '<abbr' + title + '>';
}; };
rules.abbr_close = function (/*tokens, idx, options*/) { rules.abbr_close = function (/*tokens, idx, options*/) {
return '</abbr>'; return '</abbr>';
@ -170,7 +154,7 @@ rules.paragraph_close = function (tokens, idx /*, options*/) {
rules.link_open = function (tokens, idx /*, options*/) { rules.link_open = function (tokens, idx /*, options*/) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
return '<a href="' + escapeHtml(escapeUrl(unescapeUrl(replaceEntities(tokens[idx].href)))) + '"' + title + '>'; return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>';
}; };
rules.link_close = function (/*tokens, idx, options*/) { rules.link_close = function (/*tokens, idx, options*/) {
return '</a>'; return '</a>';
@ -178,7 +162,7 @@ rules.link_close = function (/*tokens, idx, options*/) {
rules.image = function (tokens, idx, options) { rules.image = function (tokens, idx, options) {
var src = ' src="' + escapeHtml(escapeUrl(tokens[idx].src)) + '"'; var src = ' src="' + escapeHtml(encodeURI(tokens[idx].src)) + '"';
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"';
var suffix = options.xhtmlOut ? ' /' : ''; var suffix = options.xhtmlOut ? ' /' : '';

22
test/misc.js

@ -92,6 +92,28 @@ describe('API', function () {
assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\n'); assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\n');
}); });
it('force hardbreaks', function () {
var md = new Remarkable({ breaks: true });
assert.strictEqual(md.render('a\nb'), '<p>a<br>\nb</p>\n');
});
it('xhtmlOut enabled', function () {
var md = new Remarkable({ breaks: true, xhtmlOut: true });
assert.strictEqual(md.render('---'), '<hr />\n');
assert.strictEqual(md.render('![]()'), '<p><img src="" alt="" /></p>\n');
assert.strictEqual(md.render('a\nb'), '<p>a<br />\nb</p>\n');
});
it('xhtmlOut disabled', function () {
var md = new Remarkable();
assert.strictEqual(md.render('---'), '<hr>\n');
assert.strictEqual(md.render('![]()'), '<p><img src="" alt=""></p>\n');
assert.strictEqual(md.render('a \\\nb'), '<p>a <br>\nb</p>\n');
});
}); });

Loading…
Cancel
Save