Browse Source

Moved html escape helper to utils

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
9c436cebc1
  1. 37
      lib/common/utils.js
  2. 21
      lib/renderer.js

37
lib/common/utils.js

@ -27,6 +27,7 @@ function assign(obj /*from1, from2, from3, ...*/) {
return obj; return obj;
} }
////////////////////////////////////////////////////////////////////////////////
var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g; var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
@ -35,6 +36,8 @@ function unescapeMd(str) {
return str.replace(UNESCAPE_MD_RE, '$1'); return str.replace(UNESCAPE_MD_RE, '$1');
} }
////////////////////////////////////////////////////////////////////////////////
function isValidEntityCode(c) { function isValidEntityCode(c) {
/*eslint no-bitwise:0*/ /*eslint no-bitwise:0*/
// broken sequence // broken sequence
@ -91,10 +94,34 @@ function replaceEntities(str) {
return str.replace(NAMED_ENTITY_RE, replaceEntityPattern); return str.replace(NAMED_ENTITY_RE, replaceEntityPattern);
} }
////////////////////////////////////////////////////////////////////////////////
var HTML_ESCAPE_TEST_RE = /[&<>"]/;
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
var HTML_REPLACEMENTS = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;'
};
function replaceUnsafeChar(ch) {
return HTML_REPLACEMENTS[ch];
}
function escapeHtml(str) {
if (HTML_ESCAPE_TEST_RE.test(str)) {
return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar);
}
return str;
}
////////////////////////////////////////////////////////////////////////////////
exports.assign = assign; exports.assign = assign;
exports.isString = isString; exports.isString = isString;
exports.unescapeMd = unescapeMd; exports.unescapeMd = unescapeMd;
exports.isValidEntityCode = isValidEntityCode; exports.isValidEntityCode = isValidEntityCode;
exports.fromCodePoint = fromCodePoint; exports.fromCodePoint = fromCodePoint;
exports.replaceEntities = replaceEntities; exports.replaceEntities = replaceEntities;
exports.escapeHtml = escapeHtml;

21
lib/renderer.js

@ -4,31 +4,12 @@
var assign = require('./common/utils').assign; var assign = require('./common/utils').assign;
var unescapeMd = require('./common/utils').unescapeMd; var unescapeMd = require('./common/utils').unescapeMd;
var replaceEntities = require('./common/utils').replaceEntities; var replaceEntities = require('./common/utils').replaceEntities;
var escapeHtml = require('./common/utils').escapeHtml;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Helpers // Helpers
var HTML_ESCAPE_TEST_RE = /[&<>"]/;
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
var HTML_REPLACEMENTS = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;'
};
function replaceUnsafeChar(ch) {
return HTML_REPLACEMENTS[ch];
}
function escapeHtml(str) {
if (HTML_ESCAPE_TEST_RE.test(str)) {
return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar);
}
return 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) &&

Loading…
Cancel
Save