UNPKG

upfront-editable

Version:
75 lines (63 loc) 1.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.trimRight = trimRight; exports.trimLeft = trimLeft; exports.trim = trim; exports.isString = isString; exports.regexp = regexp; exports.escapeHtml = escapeHtml; exports.browserEscapeHtml = browserEscapeHtml; var toString = Object.prototype.toString; var htmlCharacters = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' // TODO: replace with lodash methods };function trimRight(text) { return text.replace(/\s+$/, ''); } function trimLeft(text) { return text.replace(/^\s+/, ''); } function trim(text) { return text.replace(/^\s+|\s+$/g, ''); } function isString(obj) { return toString.call(obj) === '[object String]'; } /** * Turn any string into a regular expression. * This can be used to search or replace a string conveniently. */ function regexp(str, flags) { if (!flags) flags = 'g'; var escapedStr = str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); return new RegExp(escapedStr, flags); } /** * Escape HTML characters <, > and & * Usage: escapeHtml('<div>') * * @param { String } * @param { Boolean } Optional. If true " and ' will also be escaped. * @return { String } Escaped Html you can assign to innerHTML of an element. */ // TODO: replace with npm.im/he function escapeHtml(s, forAttribute) { return s.replace(forAttribute ? /[&<>'"]/g : /[&<>]/g, function (c) { // "' return htmlCharacters[c]; }); } /** * Escape a string the browser way. */ function browserEscapeHtml(str) { var div = document.createElement('div'); div.appendChild(document.createTextNode(str)); return div.innerHTML; }