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. 30
      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' ];
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;

30
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 '<abbr' + title + '>';
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
};
rules.abbr_close = function (/*tokens, idx, options*/) {
return '</abbr>';
@ -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 '<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*/) {
return '</a>';
@ -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 ? ' /' : '';

22
test/misc.js

@ -92,6 +92,28 @@ describe('API', function () {
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