diff --git a/www/common/common-util.js b/www/common/common-util.js new file mode 100644 index 000000000..29b25c4fb --- /dev/null +++ b/www/common/common-util.js @@ -0,0 +1,85 @@ +define([], function () { + var Util = {}; + + var find = Util.find = function (map, path) { + return (map && path.reduce(function (p, n) { + return typeof(p[n]) !== 'undefined' && p[n]; + }, map)); + }; + + var fixHTML = Util.fixHTML = function (str) { + if (!str) { return ''; } + return str.replace(/[<>&"']/g, function (x) { + return ({ "<": "<", ">": ">", "&": "&", '"': """, "'": "'" })[x]; + }); + }; + + var hexToBase64 = Util.hexToBase64 = function (hex) { + var hexArray = hex + .replace(/\r|\n/g, "") + .replace(/([\da-fA-F]{2}) ?/g, "0x$1 ") + .replace(/ +$/, "") + .split(" "); + var byteString = String.fromCharCode.apply(null, hexArray); + return window.btoa(byteString).replace(/\//g, '-').slice(0,-2); + }; + + var base64ToHex = Util.base64ToHex = function (b64String) { + var hexArray = []; + atob(b64String.replace(/-/g, '/')).split("").forEach(function(e){ + var h = e.charCodeAt(0).toString(16); + if (h.length === 1) { h = "0"+h; } + hexArray.push(h); + }); + return hexArray.join(""); + }; + + var uint8ArrayToHex = Util.uint8ArrayToHex = function (a) { + // call slice so Uint8Arrays work as expected + return Array.prototype.slice.call(a).map(function (e, i) { + var n = Number(e & 0xff).toString(16); + if (n === 'NaN') { + throw new Error('invalid input resulted in NaN'); + } + + switch (n.length) { + case 0: return '00'; // just being careful, shouldn't happen + case 1: return '0' + n; + case 2: return n; + default: throw new Error('unexpected value'); + } + }).join(''); + }; + + var deduplicateString = Util.deduplicateString = function (array) { + var a = array.slice(); + for(var i=0; i&"']/g, function (x) { - return ({ "<": "<", ">": ">", "&": "&", '"': """, "'": "'" })[x]; - }); - }; - - var truncate = common.truncate = function (text, len) { if (typeof(text) === 'string' && text.length > len) { return text.slice(0, len) + '…'; @@ -194,36 +192,6 @@ load pinpad dynamically only after you know that it will be needed */ return text; }; - var hexToBase64 = common.hexToBase64 = function (hex) { - var hexArray = hex - .replace(/\r|\n/g, "") - .replace(/([\da-fA-F]{2}) ?/g, "0x$1 ") - .replace(/ +$/, "") - .split(" "); - var byteString = String.fromCharCode.apply(null, hexArray); - return window.btoa(byteString).replace(/\//g, '-').slice(0,-2); - }; - - var base64ToHex = common.base64ToHex = function (b64String) { - var hexArray = []; - atob(b64String.replace(/-/g, '/')).split("").forEach(function(e){ - var h = e.charCodeAt(0).toString(16); - if (h.length === 1) { h = "0"+h; } - hexArray.push(h); - }); - return hexArray.join(""); - }; - - var deduplicateString = common.deduplicateString = function (array) { - var a = array.slice(); - for(var i=0; i= 56) { @@ -257,7 +225,6 @@ load pinpad dynamically only after you know that it will be needed */ } return '/1/view/' + hexToBase64(chanKey) + '/' + Crypto.b64RemoveSlashes(keys.viewKeyStr); }; - var getHashFromKeys = common.getHashFromKeys = getEditHashFromKeys; var specialHashes = common.specialHashes = ['iframe']; @@ -333,24 +300,6 @@ load pinpad dynamically only after you know that it will be needed */ return hashes; }; - - var uint8ArrayToHex = common.uint8ArrayToHex = function (a) { - // call slice so Uint8Arrays work as expected - return Array.prototype.slice.call(a).map(function (e, i) { - var n = Number(e & 0xff).toString(16); - if (n === 'NaN') { - throw new Error('invalid input resulted in NaN'); - } - - switch (n.length) { - case 0: return '00'; // just being careful, shouldn't happen - case 1: return '0' + n; - case 2: return n; - default: throw new Error('unexpected value'); - } - }).join(''); - }; - var createChannelId = common.createChannelId = function () { var id = uint8ArrayToHex(Crypto.Nacl.randomBytes(16)); if (id.length !== 32 || /[^a-f0-9]/.test(id)) { @@ -367,14 +316,6 @@ load pinpad dynamically only after you know that it will be needed */ return '/1/edit/' + [channelId, key].join('/'); }; - var replaceHash = common.replaceHash = function (hash) { - if (window.history && window.history.replaceState) { - if (!/^#/.test(hash)) { hash = '#' + hash; } - return void window.history.replaceState({}, window.document.title, hash); - } - window.location.hash = hash; - }; - var storageKey = common.storageKey = 'CryptPad_RECENTPADS'; /* @@ -458,10 +399,6 @@ load pinpad dynamically only after you know that it will be needed */ }); }; - var getHash = common.getHash = function () { - return window.location.hash.slice(1); - }; - var getRelativeHref = common.getRelativeHref = function (href) { if (!href) { return; } if (href.indexOf('#') === -1) { return; } @@ -823,34 +760,6 @@ load pinpad dynamically only after you know that it will be needed */ }); }; - // STORAGE - var causesNamingConflict = common.causesNamingConflict = function (title, cb) { - var href = window.location.href; - - var parsed = parsePadUrl(href); - getRecentPads(function (err, pads) { - if (err) { - cb(err); - return; - } - var conflicts = pads.some(function (pad) { - // another pad is already using that title - if (pad.title === title) { - var p = parsePadUrl(pad.href); - - if (p.type === parsed.type && p.hash === parsed.hash) { - // the duplicate pad has the same type and hash - // allow renames - } else { - // it's an entirely different pad... it conflicts - return true; - } - } - }); - cb(void 0, conflicts); - }); - }; - var newPadNameKey = common.newPadNameKey = "newPadName"; var newPadPathKey = common.newPadPathKey = "newPadPath"; @@ -1018,14 +927,6 @@ load pinpad dynamically only after you know that it will be needed */ $('#' + LOADING).find('p').html(error || Messages.error); }; - /* - * Saving files - */ - var fixFileName = common.fixFileName = function (filename) { - return filename.replace(/ /g, '-').replace(/[\/\?]/g, '_') - .replace(/_+/g, '_'); - }; - var importContent = common.importContent = function (type, f) { return function () { var $files = $('').click();