User:Cryptic/smiley.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

//<pre><nowiki>
var smiley = new Array
(
 //         regexp               image name   location width height
 new Array("[:=]\\)|:-\\)",     "Smile.png",    "2/26", 20, 20),
 new Array("[:=]D|:-D",         "Teeth.png",    "7/72", 22, 22),
 new Array("[:=]\\(|:-\\(",     "Sad.png",      "d/d8", 20, 20),
 new Array(";-?[)D]",           "Smile_eye.png","9/94", 20, 20),
 new Array("[:=][pP]|:-[pP]",   "Tongue.png",   "c/c4", 22, 22),
 new Array("[:=]'\\(|:'-\\(",   "Cry.png",      "d/d8", 22, 22),
 new Array("8-?[)D]",           "Shade.png",    "d/dc", 22, 22)
);

function smileys()
{
  var i;
  for (i = 0; i < smiley.length; ++i)
    {
      smiley[i].push(new RegExp("^(" + smiley[i][0] + ")$"));
      smiley[i].push(new RegExp("\\W(" + smiley[i][0] + ")$"));
      smiley[i].push(new RegExp("^(" + smiley[i][0] + ")\\W"));
      smiley[i].push(new RegExp("\\W(" + smiley[i][0] + ")\\W"));
    }
  add_smileys(document);
}

function smiley_image(i)
{
  var n = document.createElement("img");
  n.src = "http://upload.wikimedia.org/wikipedia/commons/" + smiley[i][2] + "/" + smiley[i][1];
  n.width = smiley[i][3];
  n.height = smiley[i][4];
  return n;
}

function add_smileys(n)
{
  if (!n)
    return;
  add_smileys(n.firstChild);
  var i, j, k, a, next = n.nextSibling;
  if (n.nodeType == Node.TEXT_NODE)
    {
      for (i = 0; i < smiley.length; ++i)
        {
          for (j = 5; j <= 8; ++j)
            if (a = smiley[i][j].exec(n.data))
              {
                switch (j)
                  {
                    case 5:
                      // just replace
                      n.parentNode.replaceChild(smiley_image(i), n);
                      break;
                    case 6:
                      // stuff before, but not after
                      k = n.splitText(a.index + 1);
                      n.parentNode.replaceChild(smiley_image(i), k);
                      break;
                    case 7:
                      // stuff after, but not before
                      k = n.splitText(a[1].length);
                      next = smiley_image(i);
                      n.parentNode.replaceChild(next, n);
                      next = next.nextSibling;
                      break;
                    case 8:
                      // stuff before and after
                      k = n.splitText(a.index + 1);
                      j = k.splitText(a[1].length);
                      next = smiley_image(i);
                      n.parentNode.replaceChild(next, k);
                      next = next.nextSibling;
                      break;
                  }
                i = smiley.length;
                break;
              }
        }
    }
  add_smileys(next);
}

addOnloadHook(smileys);
//</nowiki></pre>