Permalink
Browse files

Simpler algorithm for emoji matching

Avoid having to deal with this UCS-2 madness.
  • Loading branch information...
1 parent 9bbbe3a commit df292980fa44d7e33ac3bdf1ae7272764e277e23 @lautis committed Oct 30, 2011
Showing with 12 additions and 39 deletions.
  1. +12 −39 emojie.js
View
@@ -1,40 +1,9 @@
(function(window, undefined) {
var emojis = {
+ "\ud83d": true,
"\ud83d\ude04": "1f604"
}
- // UCS-2, how nice is that?
- // Stolen from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/charAt#Example_2:.C2.A0Getting_whole_characters
- function getWholeChar(str, i) {
- var code = str.charCodeAt(i);
-
- if (isNaN(code)) {
- return ''; // Position not found
- }
- if (code < 0xD800 || code > 0xDFFF) {
- return str.charAt(i);
- }
- if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
- if (str.length <= (i+1)) {
- throw 'High surrogate without following low surrogate';
- }
- var next = str.charCodeAt(i+1);
- if (0xDC00 > next || next > 0xDFFF) {
- throw 'High surrogate without following low surrogate';
- }
- return str.charAt(i)+str.charAt(i+1);
- }
- // Low surrogate (0xDC00 <= code && code <= 0xDFFF)
- if (i === 0) {
- throw 'Low surrogate without preceding high surrogate';
- }
- var prev = str.charCodeAt(i-1);
- if (0xD800 > prev || prev > 0xDBFF) { // (could change last hex to 0xDB7F to treat high private surrogates as single characters)
- throw 'Low surrogate without preceding high surrogate';
- }
- return false; // We can pass over low surrogates now as the second component in a pair which we have already processed
- }
-
function traverseTextNodes(root, callback) {
var children = root.childNodes;
var length = children.length;
@@ -66,17 +35,21 @@
return function replacer(node) {
var string = node.textContent || node.data;
var i = 0;
- var chr, emoji, rest;
+ var emoji, rest;
+ var buffer = "";
- while (i < string.length) {
- chr = getWholeChar(string, i);
- if (emojis[chr]) {
+ for (i = 0; i < string.length; i++) {
+ buffer += string[i];
+ if (emojis[buffer] === true) {
+ continue;
+ } else if (emojis[buffer]) {
emoji = node.splitText(i);
- rest = emoji.splitText(chr.length);
- node.parentNode.replaceChild(emojiElement(chr, src), emoji);
+ rest = emoji.splitText(buffer.length);
+ node.parentNode.replaceChild(emojiElement(buffer, src), emoji);
return replacer(rest);
+ } else {
+ buffer = "";
}
- i += chr.length;
}
}
}

0 comments on commit df29298

Please sign in to comment.