Created
August 4, 2011 04:48
-
-
Save ambar/1124523 to your computer and use it in GitHub Desktop.
jQuery.fn.emotion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>$.fn.emotion</title> | |
<style type="text/css"> | |
/* comment (笑) */ | |
</style> | |
</head> | |
<body> | |
<ul> | |
<li data-id="144324"> | |
<a href="#">(困)(笑)(苦)(茶杯)(无辜)</a> | |
<p><script>alert("test")</script> (笑) <script>alert("test")</script></p> | |
<p> </p> | |
<p><a href="http://www.example.com/11691727" class="name">jessica</a></p> | |
<div class="msg_Con"> | |
(笑)(苦)(茶杯)(无辜) | |
</div>(笑) | |
<p>(笑)A(苦)B(茶杯)(无辜)</p> | |
<!-- (笑) --> | |
</li> | |
</ul> | |
C(笑) | |
<script type="text/javascript" src="js/jquery-latest.min.js"></script> | |
<script type="text/javascript"> | |
// comment (笑) | |
var emotions = ["困", "傻眼", "糗", "无语", "衰", "可怜", "怒", "大嘴", "猥琐", "苦", "寒", "大哭", "色", "吐", "僵尸", "晕", "闭嘴", "笑", "大笑", "睡觉", "白眼", "无辜", "可爱", "惊讶", "酷", "烟", "糖葫芦", "心", "手机", "茶杯", "炸弹", "禁", "礼物", "金币", "骷髅", "音乐", "时间", "信", "创可贴", "奖状"]; | |
var regexs = $.map(emotions,function(v) { | |
return new RegExp('('+v+')','g'); | |
}) | |
var path = "http://static.example.com/img/emo/"; | |
Number.prototype.pad = function(len) { | |
var n = String(this); | |
len = len || 2; | |
while(n.length < len) { | |
n = '0' + n; | |
} | |
return n; | |
}; | |
$.htmlencode = function(html){ | |
return html ? $('<div>').text(html).html().replace(/"/g,'"') : ''; | |
} | |
$.emotion = function(txt) { | |
$.each(regexs,function(i,r) { | |
txt = txt.replace(r,function(m) { | |
return '<img alt="'+emotions[i]+'" src="'+path+ (i+1).pad() +'.gif" />'; | |
}) | |
}) | |
return txt; | |
}; | |
$.fn.emotion = function () { | |
return this.each(function () { | |
$('*:not(script)',this).andSelf() | |
.contents().filter(function() { | |
return this.nodeType === 3 && $.trim(this.nodeValue); | |
}) | |
.replaceWith(function() { | |
return $.emotion( $.htmlencode(this.nodeValue) ); | |
}); | |
}); | |
}; | |
$(function() { | |
// $(this) | |
$(this.body) | |
// $('li:first') | |
// $('li:first p') | |
// .first() | |
.emotion() | |
}) | |
</script> | |
</body> | |
</html> |
Author
ambar
commented
Aug 4, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment