Skip to content

Instantly share code, notes, and snippets.

@ambar
Created August 4, 2011 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambar/1124523 to your computer and use it in GitHub Desktop.
Save ambar/1124523 to your computer and use it in GitHub Desktop.
jQuery.fn.emotion
<html>
<head>
<title>$.fn.emotion</title>
<style type="text/css">
/* comment (笑) */
</style>
</head>
<body>
<ul>
<li data-id="144324">
<a href="#">(困)(笑)(苦)(茶杯)(无辜)</a>
<p>&lt;script&gt;alert(&quot;test&quot;)&lt;/script&gt; (笑) &lt;script&gt;alert(&quot;test&quot;)&lt;/script&gt;</p>
<p> </p>
<p>&lt;a href=&quot;http://www.example.com/11691727&quot; class=&quot;name&quot;&gt;jessica&lt;/a&gt;</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,'&quot;') : '';
}
$.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>
@ambar
Copy link
Author

ambar commented Aug 4, 2011

// opt
return $(document.createTextNode(txt)).add('<img src="emo.gif" />');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment