From 0e3e1171e6ab2ac6b5f72e2e39fa3fd2758678e3 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 11 Oct 2014 03:30:29 +0400 Subject: [PATCH] Moved string helpers to utilities --- lib/common/utils.js | 74 ++++++++++++++++++++++++++++++-- lib/helpers.js | 70 +----------------------------- lib/renderer.js | 6 +-- lib/rules_block/heading.js | 3 +- lib/rules_block/hr.js | 5 +-- lib/rules_inline/autolink.js | 2 +- lib/rules_inline/entity.js | 6 +-- lib/rules_typographer/linkify.js | 2 +- 8 files changed, 82 insertions(+), 86 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index 452812e..8b0fc94 100644 --- a/lib/common/utils.js +++ b/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(/') >= 0) { str = str.replace(/>/g, '>'); } + if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '"'); } + 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; diff --git a/lib/helpers.js b/lib/helpers.js index af2cc2f..564b684 100644 --- a/lib/helpers.js +++ b/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, '&'); } - if (str.indexOf('<') >= 0) { str = str.replace(/') >= 0) { str = str.replace(/>/g, '>'); } - if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '"'); } - 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; diff --git a/lib/renderer.js b/lib/renderer.js index f9d5af6..45d76d4 100644 --- a/lib/renderer.js +++ b/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) { diff --git a/lib/rules_block/heading.js b/lib/rules_block/heading.js index 09a7786..c664aa1 100644 --- a/lib/rules_block/heading.js +++ b/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); diff --git a/lib/rules_block/hr.js b/lib/rules_block/hr.js index 93f610a..2c49bc6 100644 --- a/lib/rules_block/hr.js +++ b/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++; } } diff --git a/lib/rules_inline/autolink.js b/lib/rules_inline/autolink.js index f9724ee..a152df9 100644 --- a/lib/rules_inline/autolink.js +++ b/lib/rules_inline/autolink.js @@ -1,7 +1,7 @@ // Process autolinks '' -var escapeHtml = require('../helpers').escapeHtml; +var escapeHtml = require('../common/utils').escapeHtml; var url_schemas = require('../common/url_schemas'); diff --git a/lib/rules_inline/entity.js b/lib/rules_inline/entity.js index 4c05fc9..8be7e07 100644 --- a/lib/rules_inline/entity.js +++ b/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; diff --git a/lib/rules_typographer/linkify.js b/lib/rules_typographer/linkify.js index 8338ce0..feac8cf 100644 --- a/lib/rules_typographer/linkify.js +++ b/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 = [];