From 054ff554f5642334ab1637bb5b3cd44a11a5b0dd Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 8 Nov 2014 10:45:13 +0300 Subject: [PATCH] Improved coverage and removed unused code --- lib/parser_inline.js | 8 +------- lib/renderer.js | 30 +++++++----------------------- test/misc.js | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/parser_inline.js b/lib/parser_inline.js index 1e73998..6906a43 100644 --- a/lib/parser_inline.js +++ b/lib/parser_inline.js @@ -30,13 +30,7 @@ var _rules = [ var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ]; function validateLink(url) { - var str = ''; - - try { - str = decodeURI(url).trim().toLowerCase(); - } catch (_) {} - - if (!str) { return false; } + var str = decodeURI(url).trim().toLowerCase(); if (str.indexOf(':') >= 0 && BAD_PROTOCOLS.indexOf(str.split(':')[0]) >= 0) { return false; diff --git a/lib/renderer.js b/lib/renderer.js index d66e225..9bfa05a 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -9,19 +9,6 @@ var replaceEntities = require('./common/utils').replaceEntities; //////////////////////////////////////////////////////////////////////////////// // 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_REPLACE_RE = /[&<>"]/g; var HTML_REPLACEMENTS = { @@ -44,12 +31,10 @@ function escapeHtml(str) { function nextToken(tokens, idx) { if (++idx >= tokens.length - 2) { return idx; } - if (tokens[idx].type === 'paragraph_open' && tokens[idx].tight) { - if (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) { - if (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight) { - return nextToken(tokens, idx + 2); - } - } + if ((tokens[idx].type === 'paragraph_open' && tokens[idx].tight) && + (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) && + (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight)) { + return nextToken(tokens, idx + 2); } return idx; } @@ -72,8 +57,7 @@ var rules = {}; rules.abbr_open = function (tokens, idx/*, options*/) { - var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; - return ''; + return ''; }; rules.abbr_close = function (/*tokens, idx, options*/) { return ''; @@ -170,7 +154,7 @@ rules.paragraph_close = function (tokens, idx /*, options*/) { rules.link_open = function (tokens, idx /*, options*/) { var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; - return ''; + return ''; }; rules.link_close = function (/*tokens, idx, options*/) { return ''; @@ -178,7 +162,7 @@ rules.link_close = 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 alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; var suffix = options.xhtmlOut ? ' /' : ''; diff --git a/test/misc.js b/test/misc.js index 0f4d3e4..59acadd 100644 --- a/test/misc.js +++ b/test/misc.js @@ -92,6 +92,28 @@ describe('API', function () { assert.strictEqual(md.render('```\n&\n```'), '
&\n
\n'); }); + it('force hardbreaks', function () { + var md = new Remarkable({ breaks: true }); + + assert.strictEqual(md.render('a\nb'), '

a
\nb

\n'); + }); + + it('xhtmlOut enabled', function () { + var md = new Remarkable({ breaks: true, xhtmlOut: true }); + + assert.strictEqual(md.render('---'), '
\n'); + assert.strictEqual(md.render('![]()'), '

\n'); + assert.strictEqual(md.render('a\nb'), '

a
\nb

\n'); + }); + + it('xhtmlOut disabled', function () { + var md = new Remarkable(); + + assert.strictEqual(md.render('---'), '
\n'); + assert.strictEqual(md.render('![]()'), '

\n'); + assert.strictEqual(md.render('a \\\nb'), '

a
\nb

\n'); + }); + });