convert = function (unicode) { if(unicode.indexOf("-") > -1) { var parts = []; var s = unicode.split('-'); for(var i = 0; i < s.length; i++) { var part = parseInt(s[i], 16); if (part >= 0x10000 && part <= 0x10FFFF) { var hi = Math.floor((part - 0x10000) / 0x400) + 0xD800; var lo = ((part - 0x10000) % 0x400) + 0xDC00; part = (String.fromCharCode(hi) + String.fromCharCode(lo)); } else { part = String.fromCharCode(part); } parts.push(part); } return parts.join(''); } else { var s = parseInt(unicode, 16); if (s >= 0x10000 && s <= 0x10FFFF) { var hi = Math.floor((s - 0x10000) / 0x400) + 0xD800; var lo = ((s - 0x10000) % 0x400) + 0xDC00; return (String.fromCharCode(hi) + String.fromCharCode(lo)); } else { return String.fromCharCode(s); } } }
n/a
escapeHTML = function (string) { var escaped = { '&' : '&', '<' : '<', '>' : '>', '"' : '"', '\'': ''' }; return string.replace(/[&<>"']/g, function (match) { return escaped[match]; }); }
n/a
escapeRegExp = function (string) { return string.replace(/[-[\]{}()*+?.,;:&\\^$#\s]/g, "\\$&"); }
n/a
getUnicodeReplacementRegEx = function () { ns.memoizeReplacement(); return ns.unicodeReplacementRegEx; }
n/a
mapEmojioneList = function (addToMapStorage) { for (var shortname in ns.emojioneList) { if (!ns.emojioneList.hasOwnProperty(shortname)) { continue; } for (var i = 0, len = ns.emojioneList[shortname].unicode.length; i < len; i++) { var unicode = ns.emojioneList[shortname].unicode[i]; addToMapStorage(unicode, shortname); } } }
n/a
mapUnicodeCharactersToShort = function () { ns.memoizeReplacement(); return ns.memMapShortToUnicodeCharacters; }
n/a
mapUnicodeToShort = function () { if (!ns.memMapShortToUnicode) { ns.memMapShortToUnicode = {}; ns.mapEmojioneList(function (unicode, shortname) { ns.memMapShortToUnicode[unicode] = shortname; }); } return ns.memMapShortToUnicode; }
n/a
memoizeReplacement = function () { if (!ns.unicodeReplacementRegEx || !ns.memMapShortToUnicodeCharacters) { var unicodeList = []; ns.memMapShortToUnicodeCharacters = {}; ns.mapEmojioneList(function (unicode, shortname) { var emojiCharacter = ns.convert(unicode); if(ns.emojioneList[shortname].isCanonical) { ns.memMapShortToUnicodeCharacters[emojiCharacter] = shortname; } unicodeList.push(emojiCharacter); }); ns.unicodeReplacementRegEx = unicodeList.join('|'); } }
n/a
objectFlip = function (obj) { var key, tmp_obj = {}; for (key in obj) { if (obj.hasOwnProperty(key)) { tmp_obj[obj[key]] = key; } } return tmp_obj; }
n/a
replaceAll = function (string, find, replacementList) { var escapedFind = ns.escapeRegExp(find); var search = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|("+escapedFind +")", "gi"); // callback prevents replacing anything inside of these common html tags as well as between an <object></object> tag var replace = function(entire, m1) { return ((typeof m1 === 'undefined') || (m1 === '')) ? entire : replacementList[m1]; }; return string.replace(search,replace); }
n/a
shortnameToAscii = function (str) { var unicode, // something to keep in mind here is that array flip will destroy // half of the ascii text "emojis" because the unicode numbers are duplicated // this is ok for what it's being used for unicodeToAscii = ns.objectFlip(ns.asciiList); str = str.replace(ns.regShortNames, function(shortname) { if( (typeof shortname === 'undefined') || (shortname === '') || (!(shortname in ns.emojioneList)) ) { // if the shortname doesnt exist just return the entire match return shortname; } else { unicode = ns.emojioneList[shortname].unicode[ns.emojioneList[shortname].unicode.length-1]; if(typeof unicodeToAscii[unicode] !== 'undefined') { return unicodeToAscii[unicode]; } else { return shortname; } } }); return str; }
n/a
shortnameToImage = function (str) { // replace regular shortnames first var replaceWith,unicode,alt,title; str = str.replace(ns.regShortNames, function(shortname) { if( (typeof shortname === 'undefined') || (shortname === '') || (!(shortname in ns.emojioneList)) ) { // if the shortname doesnt exist just return the entire match return shortname; } else { unicode = ns.emojioneList[shortname].unicode[ns.emojioneList[shortname].unicode.length-1]; title = ns.imageTitleTag ? 'title="'+shortname+'"' : ''; // depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname alt = (ns.unicodeAlt) ? ns.convert(unicode.toUpperCase()) : shortname; if(ns.imageType === 'png') { if(ns.sprites) { replaceWith = '<span class="emojione emojione-'+unicode+'" ' + title + '>'+alt+'</span>'; } else { replaceWith = '<img class="emojione" alt="'+alt+'" ' + title + ' src="'+ns.imagePathPNG+unicode+'.png'+ns.cacheBustParam +'"/>'; } } else { // svg if(ns.sprites) { replaceWith = '<svg class="emojione"><description>'+alt+'</description><use xlink:href="'+ns.imagePathSVGSprites +'#emoji-'+unicode+'"></use></svg>'; } else { replaceWith = '<object class="emojione" data="'+ns.imagePathSVG+unicode+'.svg'+ns.cacheBustParam+'" type="image /svg+xml" standby="'+alt+'">'+alt+'</object>'; } } return replaceWith; } }); // if ascii smileys are turned on, then we'll replace them! if (ns.ascii) { str = str.replace(ns.regAscii, function(entire, m1, m2, m3) { if( (typeof m3 === 'undefined') || (m3 === '') || (!(ns.unescapeHTML(m3) in ns.asciiList)) ) { // if the ascii doesnt exist just return the entire match return entire; } m3 = ns.unescapeHTML(m3); unicode = ns.asciiList[m3]; title = ns.imageTitleTag ? 'title="'+ns.escapeHTML(m3)+'"' : ''; // depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname alt = (ns.unicodeAlt) ? ns.convert(unicode.toUpperCase()) : ns.escapeHTML(m3); if(ns.imageType === 'png') { if(ns.sprites) { replaceWith = m2+'<span class="emojione emojione-'+unicode+'" ' + title + '>'+alt+'</span>'; } else { replaceWith = m2+'<img class="emojione" alt="'+alt+'" ' + title + ' src="'+ns.imagePathPNG+unicode+'.png'+ns .cacheBustParam+'"/>'; } } else { // svg if(ns.sprites) { replaceWith = '<svg class="emojione"><description>'+alt+'</description><use xlink:href="'+ns.imagePathSVGSprites +'#emoji-'+unicode+'"></use></svg>'; } else { replaceWith = m2+'<object class="emojione" data="'+ns.imagePathSVG+unicode+'.svg'+ns.cacheBustParam+'" type=" image/svg+xml" standby="'+alt+'">'+alt+'</object>'; } } return replaceWith; }); } return str; }
n/a
shortnameToUnicode = function (str) { // replace regular shortnames first var unicode,fname,uc; str = str.replace(ns.regShortNames, function(shortname) { if( (typeof shortname === 'undefined') || (shortname === '') || (!(shortname in ns.emojioneList)) ) { // if the shortname doesnt exist just return the entire match return shortname; } unicode = ns.emojioneList[shortname].unicode[0].toUpperCase(); fname = ns.emojioneList[shortname].fname; uc = ns.emojioneList[shortname].uc; //return ns.convert(unicode); return ns.convert(uc); }); // if ascii smileys are turned on, then we'll replace them! if (ns.ascii) { str = str.replace(ns.regAscii, function(entire, m1, m2, m3) { if( (typeof m3 === 'undefined') || (m3 === '') || (!(ns.unescapeHTML(m3) in ns.asciiList)) ) { // if the shortname doesnt exist just return the entire match return entire; } m3 = ns.unescapeHTML(m3); unicode = ns.asciiList[m3].toUpperCase(); return m2+ns.convert(unicode); }); } return str; }
n/a
toImage = function (str) { str = ns.unicodeToImage(str); str = ns.shortnameToImage(str); return str; }
n/a
toShort = function (str) { var find = ns.getUnicodeReplacementRegEx(), replacementList = ns.mapUnicodeCharactersToShort(); return ns.replaceAll(str, find,replacementList); }
n/a
unescapeHTML = function (string) { var unescaped = { '&' : '&', '&' : '&', '&' : '&', '<' : '<', '<' : '<', '<' : '<', '>' : '>', '>' : '>', '>' : '>', '"' : '"', '"' : '"', '"' : '"', ''' : '\'', ''' : '\'', ''' : '\'' }; return string.replace(/&(?:amp|#38|#x26|lt|#60|#x3C|gt|#62|#x3E|apos|#39|#x27|quot|#34|#x22);/ig, function (match) { return unescaped[match]; }); }
n/a
unicodeToImage = function (str) { var replaceWith,unicode,short,fname,alt,title; var mappedUnicode = ns.mapUnicodeToShort(); str = str.replace(ns.regUnicode, function(unicodeChar) { if( (typeof unicodeChar === 'undefined') || (unicodeChar === '') || (!(unicodeChar in ns.jsEscapeMap)) ) { // if the unicodeChar doesnt exist just return the entire match return unicodeChar; } else { // get the unicode codepoint from the actual char unicode = ns.jsEscapeMap[unicodeChar]; //then map to shortname and locate the filename short = mappedUnicode[unicode]; fname = ns.emojioneList[short].fname; // depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname alt = (ns.unicodeAlt) ? ns.convert(unicode.toUpperCase()) : short; title = ns.imageTitleTag ? 'title="'+short+'"' : ''; if(ns.imageType === 'png') { if(ns.sprites) { replaceWith = '<span class="emojione emojione-'+unicode+'" ' + title + '>'+alt+'</span>'; } else { replaceWith = '<img class="emojione" alt="'+alt+'" ' + title + ' src="'+ns.imagePathPNG+fname+'.png'+ns.cacheBustParam +'"/>'; } } else { // svg if(ns.sprites) { replaceWith = '<svg class="emojione"><description>'+alt+'</description><use xlink:href="'+ns.imagePathSVGSprites +'#emoji-'+unicode+'"></use></svg>'; } else { replaceWith = '<img class="emojione" alt="'+alt+'" ' + title + ' src="'+ns.imagePathSVG+fname+'.svg'+ns.cacheBustParam +'"/>'; } } return replaceWith; } }); return str; }
n/a
unifyUnicode = function (str) { str = ns.toShort(str); str = ns.shortnameToUnicode(str); return str; }
n/a