Browse Source

Moved string helpers to utilities

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
0e3e1171e6
  1. 74
      lib/common/utils.js
  2. 70
      lib/helpers.js
  3. 6
      lib/renderer.js
  4. 3
      lib/rules_block/heading.js
  5. 5
      lib/rules_block/hr.js
  6. 2
      lib/rules_inline/autolink.js
  7. 6
      lib/rules_inline/entity.js
  8. 2
      lib/rules_typographer/linkify.js

74
lib/common/utils.js

@ -1,10 +1,11 @@
// Utilities
'use strics';
//
'use strict';
// Merge objects
//
exports.assign = function (obj /*from1, from2, from3, ...*/) {
function assign(obj /*from1, from2, from3, ...*/) {
var sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) {
var source = sources.shift();
@ -22,4 +23,71 @@ exports.assign = function (obj /*from1, from2, from3, ...*/) {
}
return obj;
};
}
function escapeHtml(str) {
if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&'); }
if (str.indexOf('<') >= 0) { str = str.replace(/</g, '&lt;'); }
if (str.indexOf('>') >= 0) { str = str.replace(/>/g, '&gt;'); }
if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '&quot;'); }
return str;
}
var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
function unescapeMd(str) {
if (str.indexOf('\\') < 0) { return str; }
return str.replace(UNESCAPE_MD_RE, '$1');
}
function isValidEntityCode(c) {
/*eslint no-bitwise:0*/
// broken sequence
if (c >= 0xD800 && c <= 0xDFFF) { return false; }
if (c >= 0xF5 && c <= 0xFF) { return false; }
if (c === 0xC0 || c === 0xC1) { return false; }
// never used
if (c >= 0xFDD0 && c <= 0xFDEF) { return false; }
if ((c & 0xFFFF) === 0xFFFF || (c & 0xFFFF) === 0xFFFE) { return false; }
// control codes
if (c <= 0x1F) { return false; }
if (c >= 0x7F && c <= 0x9F) { return false; }
// out of range
if (c > 0x10FFFF) { return false; }
return true;
}
function fromCodePoint(c) {
/*eslint no-bitwise:0*/
if (c > 0xffff) {
c -= 0x10000;
var surrogate1 = 0xd800 + (c >> 10),
surrogate2 = 0xdc00 + (c & 0x3ff);
return String.fromCharCode(surrogate1, surrogate2);
}
return String.fromCharCode(c);
}
var NAMED_ENTITY_RE = /&([a-z][a-z0-9]{1,31});/gi;
var entities = require('./entities');
function replaceEntities(str) {
if (str.indexOf('&') < 0) { return str; }
return str.replace(NAMED_ENTITY_RE, function(match, name) {
if (entities.hasOwnProperty(name)) {
return entities[name];
}
return match;
});
}
exports.assign = assign;
exports.escapeHtml = escapeHtml;
exports.unescapeMd = unescapeMd;
exports.isValidEntityCode = isValidEntityCode;
exports.fromCodePoint = fromCodePoint;
exports.replaceEntities = replaceEntities;

70
lib/helpers.js

@ -3,10 +3,6 @@
'use strict';
function isWhiteSpace(ch) {
return ch === 0x20;
}
// Check if line has zero length or contains spaces only
function isEmpty(state, line) {
return state.bMarks[line] + state.tShift[line] >= state.eMarks[line];
@ -25,7 +21,7 @@ function skipEmptyLines(state, from) {
// Skip spaces from given position.
function skipSpaces(state, pos) {
for (var max = state.src.length; pos < max; pos++) {
if (!isWhiteSpace(state.src.charCodeAt(pos))) { break; }
if (state.src.charCodeAt(pos) !== 0x20/* space */) { break; }
}
return pos;
}
@ -83,73 +79,9 @@ function getLines(state, begin, end, indent, keepLastLF) {
}
function escapeHtml(str) {
if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&amp;'); }
if (str.indexOf('<') >= 0) { str = str.replace(/</g, '&lt;'); }
if (str.indexOf('>') >= 0) { str = str.replace(/>/g, '&gt;'); }
if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '&quot;'); }
return str;
}
var UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
function unescapeMd(str) {
if (str.indexOf('\\') < 0) { return str; }
return str.replace(UNESCAPE_MD_RE, '$1');
}
function isValidEntityCode(c) {
/*eslint no-bitwise:0*/
// broken sequence
if (c >= 0xD800 && c <= 0xDFFF) { return false; }
if (c >= 0xF5 && c <= 0xFF) { return false; }
if (c === 0xC0 || c === 0xC1) { return false; }
// never used
if (c >= 0xFDD0 && c <= 0xFDEF) { return false; }
if ((c & 0xFFFF) === 0xFFFF || (c & 0xFFFF) === 0xFFFE) { return false; }
// control codes
if (c <= 0x1F) { return false; }
if (c >= 0x7F && c <= 0x9F) { return false; }
// out of range
if (c > 0x10FFFF) { return false; }
return true;
}
function fromCodePoint(c) {
/*eslint no-bitwise:0*/
if (c > 0xffff) {
c -= 0x10000;
var surrogate1 = 0xd800 + (c >> 10),
surrogate2 = 0xdc00 + (c & 0x3ff);
return String.fromCharCode(surrogate1, surrogate2);
}
return String.fromCharCode(c);
}
var NAMED_ENTITY_RE = /&([a-z][a-z0-9]{1,31});/gi;
var entities = require('./common/entities');
function replaceEntities(str) {
if (str.indexOf('&') < 0) { return str; }
return str.replace(NAMED_ENTITY_RE, function(match, name) {
if (entities.hasOwnProperty(name)) {
return entities[name];
}
return match;
});
}
exports.isWhiteSpace = isWhiteSpace;
exports.isEmpty = isEmpty;
exports.skipEmptyLines = skipEmptyLines;
exports.skipSpaces = skipSpaces;
exports.skipChars = skipChars;
exports.getLines = getLines;
exports.skipCharsBack = skipCharsBack;
exports.escapeHtml = escapeHtml;
exports.unescapeMd = unescapeMd;
exports.isValidEntityCode = isValidEntityCode;
exports.fromCodePoint = fromCodePoint;
exports.replaceEntities = replaceEntities;

6
lib/renderer.js

@ -2,9 +2,9 @@
var assign = require('./common/utils').assign;
var escapeHtml = require('./helpers').escapeHtml;
var unescapeMd = require('./helpers').unescapeMd;
var replaceEntities = require('./helpers').replaceEntities;
var escapeHtml = require('./common/utils').escapeHtml;
var unescapeMd = require('./common/utils').unescapeMd;
var replaceEntities = require('./common/utils').replaceEntities;
function escapeUrl(str) {

3
lib/rules_block/heading.js

@ -3,7 +3,6 @@
'use strict';
var isWhiteSpace = require('../helpers').isWhiteSpace;
var skipSpaces = require('../helpers').skipSpaces;
var skipCharsBack = require('../helpers').skipCharsBack;
@ -27,7 +26,7 @@ module.exports = function heading(state, startLine, endLine, silent) {
ch = state.src.charCodeAt(++pos);
}
if (level > 6 || (pos < max && !isWhiteSpace(ch))) { return false; }
if (level > 6 || (pos < max && ch !== 0x20/* space */)) { return false; }
// skip spaces before heading text
pos = skipSpaces(state, pos);

5
lib/rules_block/hr.js

@ -3,9 +3,6 @@
'use strict';
var isWhiteSpace = require('../helpers').isWhiteSpace;
module.exports = function hr(state, startLine, endLine, silent) {
var marker, cnt, ch,
pos = state.bMarks[startLine],
@ -29,7 +26,7 @@ module.exports = function hr(state, startLine, endLine, silent) {
cnt = 1;
while (pos < max) {
ch = state.src.charCodeAt(pos++);
if (ch !== marker && !isWhiteSpace(ch)) { return false; }
if (ch !== marker && ch !== 0x20/* space */) { return false; }
if (ch === marker) { cnt++; }
}

2
lib/rules_inline/autolink.js

@ -1,7 +1,7 @@
// Process autolinks '<protocol:...>'
var escapeHtml = require('../helpers').escapeHtml;
var escapeHtml = require('../common/utils').escapeHtml;
var url_schemas = require('../common/url_schemas');

6
lib/rules_inline/entity.js

@ -3,9 +3,9 @@
'use strict';
var entities = require('../common/entities');
var escapeHtml = require('../helpers').escapeHtml;
var isValidEntityCode = require('../helpers').isValidEntityCode;
var fromCodePoint = require('../helpers').fromCodePoint;
var escapeHtml = require('../common/utils').escapeHtml;
var isValidEntityCode = require('../common/utils').isValidEntityCode;
var fromCodePoint = require('../common/utils').fromCodePoint;
var DIGITAL_RE = /^&#((?:x[a-f0-9]{1,8}|[0-9]{1,8}));/i;

2
lib/rules_typographer/linkify.js

@ -6,7 +6,7 @@
var Autolinker = require('autolinker');
var escapeHtml = require('../helpers').escapeHtml;
var escapeHtml = require('../common/utils').escapeHtml;
var links = [];

Loading…
Cancel
Save