PageRenderTime 114ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/connect-web/src/main/webapp/components/jquery-xheditor/src/ubb.js

http://focus-sns.googlecode.com/
JavaScript | 237 lines | 210 code | 17 blank | 10 comment | 30 complexity | c63c44f5c0d22bed9d33efb35d2ae4b2 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-3.0
  1. /*!
  2. * WYSIWYG UBB Editor support for xhEditor
  3. * @requires xhEditor
  4. *
  5. * @author Yanis.Wang<yanis.wang@gmail.com>
  6. * @site http://xheditor.com/
  7. * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
  8. *
  9. * @Version: 0.9.7 (build 110331)
  10. */
  11. function ubb2html(sUBB)
  12. {
  13. var i,sHtml=String(sUBB),arrcode=new Array(),cnum=0;
  14. sHtml=sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
  15. sHtml=sHtml.replace(/\r?\n/g,"<br />");
  16. sHtml=sHtml.replace(/\[code\s*(?:=\s*([^\]]+?))?\]([\s\S]*?)\[\/code\]/ig,function(all,t,c){//code????
  17. cnum++;arrcode[cnum]=all;
  18. return "[\tubbcodeplace_"+cnum+"\t]";
  19. });
  20. sHtml=sHtml.replace(/\[(\/?)(b|u|i|s|sup|sub)\]/ig,'<$1$2>');
  21. sHtml=sHtml.replace(/\[color\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<font color="$1">');
  22. sHtml=sHtml.replace(/\[size\s*=\s*(\d+?)\s*\]/ig,'<font size="$1">');
  23. sHtml=sHtml.replace(/\[font\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<font face="$1">');
  24. sHtml=sHtml.replace(/\[\/(color|size|font)\]/ig,'</font>');
  25. sHtml=sHtml.replace(/\[back\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<span style="background-color:$1;">');
  26. sHtml=sHtml.replace(/\[\/back\]/ig,'</span>');
  27. for(i=0;i<3;i++)sHtml=sHtml.replace(/\[align\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\](((?!\[align(?:\s+[^\]]+)?\])[\s\S])*?)\[\/align\]/ig,'<p align="$1">$2</p>');
  28. sHtml=sHtml.replace(/\[img\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/img\]/ig,'<img src="$1" alt="" />');
  29. sHtml=sHtml.replace(/\[img\s*=([^,\]]*)(?:\s*,\s*(\d*%?)\s*,\s*(\d*%?)\s*)?(?:,?\s*(\w+))?\s*\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*)?\s*\[\/img\]/ig,function(all,alt,p1,p2,p3,src){
  30. var str='<img src="'+src+'" alt="'+alt+'"',a=p3?p3:(!isNum(p1)?p1:'');
  31. if(isNum(p1))str+=' width="'+p1+'"';
  32. if(isNum(p2))str+=' height="'+p2+'"'
  33. if(a)str+=' align="'+a+'"';
  34. str+=' />';
  35. return str;
  36. });
  37. sHtml=sHtml.replace(/\[emot\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\/\]/ig,'<img emot="$1" />');
  38. sHtml=sHtml.replace(/\[url\]\s*(((?!")[\s\S])*?)(?:"[\s\S]*?)?\s*\[\/url\]/ig,'<a href="$1">$1</a>');
  39. sHtml=sHtml.replace(/\[url\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]*?)\s*\[\/url\]/ig,'<a href="$1">$2</a>');
  40. sHtml=sHtml.replace(/\[email\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/email\]/ig,'<a href="mailto:$1">$1</a>');
  41. sHtml=sHtml.replace(/\[email\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]+?)\s*\[\/email\]/ig,'<a href="mailto:$1">$2</a>');
  42. sHtml=sHtml.replace(/\[quote\]([\s\S]*?)\[\/quote\]/ig,'<blockquote>$1</blockquote>');
  43. sHtml=sHtml.replace(/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/flash\]/ig,function(all,w,h,url){
  44. if(!w)w=480;if(!h)h=400;
  45. return '<embed type="application/x-shockwave-flash" src="'+url+'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="true" loop="true" width="'+w+'" height="'+h+'"/>';
  46. });
  47. sHtml=sHtml.replace(/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/media\]/ig,function(all,w,h,play,url){
  48. if(!w)w=480;if(!h)h=400;
  49. return '<embed type="application/x-mplayer2" src="'+url+'" enablecontextmenu="false" autostart="'+(play=='1'?'true':'false')+'" width="'+w+'" height="'+h+'"/>';
  50. });
  51. sHtml=sHtml.replace(/\[table\s*(?:=\s*(\d{1,4}%?)\s*(?:,\s*([^\]"]+)(?:"[^\]]*?)?)?)?\s*\]/ig,function(all,w,b){
  52. var str='<table';
  53. if(w)str+=' width="'+w+'"';
  54. if(b)str+=' bgcolor="'+b+'"';
  55. return str+'>';
  56. });
  57. sHtml=sHtml.replace(/\[tr\s*(?:=\s*([^\]"]+?)(?:"[^\]]*?)?)?\s*\]/ig,function(all,bg){
  58. return '<tr'+(bg?' bgcolor="'+bg+'"':'')+'>';
  59. });
  60. sHtml=sHtml.replace(/\[td\s*(?:=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\s*\]/ig,function(all,col,row,w){
  61. return '<td'+(col>1?' colspan="'+col+'"':'')+(row>1?' rowspan="'+row+'"':'')+(w?' width="'+w+'"':'')+'>';
  62. });
  63. sHtml=sHtml.replace(/\[\/(table|tr|td)\]/ig,'</$1>');
  64. sHtml=sHtml.replace(/\[\*\]((?:(?!\[\*\]|\[\/list\]|\[list\s*(?:=[^\]]+)?\])[\s\S])+)/ig,'<li>$1</li>');
  65. sHtml=sHtml.replace(/\[list\s*(?:=\s*([^\]"]+?)(?:"[^\]]*?)?)?\s*\]/ig,function(all,type){
  66. var str='<ul';
  67. if(type)str+=' type="'+type+'"';
  68. return str+'>';
  69. });
  70. sHtml=sHtml.replace(/\[\/list\]/ig,'</ul>');
  71. for(i=1;i<=cnum;i++)sHtml=sHtml.replace("[\tubbcodeplace_"+i+"\t]", arrcode[i]);
  72. sHtml=sHtml.replace(/(^|<\/?\w+(?:\s+[^>]*?)?>)([^<$]+)/ig, function(all,tag,text){
  73. return tag+text.replace(/[\t ]/g,function(c){return {'\t':'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',' ':'&nbsp;'}[c];});
  74. });
  75. function isNum(s){if(s!=null&&s!='')return !isNaN(s);else return false;}
  76. return sHtml;
  77. }
  78. function html2ubb(sHtml)
  79. {
  80. var mapSize={'xx-small':1,'8pt':1,'x-small':2,'10pt':2,'small':3,'12pt':3,'medium':4,'14pt':4,'large':5,'18pt':5,'x-large':6,'24pt':6,'xx-large':7,'36pt':7};
  81. var regSrc=/\s+src\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i,regWidth=/\s+width\s*=\s*(["']?)\s*(\d+(?:\.\d+)?%?)\s*\1(\s|$)/i,regHeight=/\s+height\s*=\s*(["']?)\s*(\d+(?:\.\d+)?%?)\s*\1(\s|$)/i,regBg=/(?:background|background-color|bgcolor)\s*[:=]\s*(["']?)\s*((rgb\s*\(\s*\d{1,3}%?,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*\))|(#[0-9a-f]{3,6})|([a-z]{1,20}))\s*\1/i
  82. var i,sUBB=String(sHtml),arrcode=new Array(),cnum=0;
  83. sUBB=sUBB.replace(/\s*\r?\n\s*/g,'');
  84. sUBB = sUBB.replace(/<(script|style)(\s+[^>]*?)?>[\s\S]*?<\/\1>/ig, '');
  85. sUBB = sUBB.replace(/<!--[\s\S]*?-->/ig,'');
  86. sUBB=sUBB.replace(/<br\s*?\/?>/ig,"\r\n");
  87. sUBB=sUBB.replace(/\[code\s*(=\s*([^\]]+?))?\]([\s\S]*?)\[\/code\]/ig,function(all,t,c){//code????
  88. cnum++;arrcode[cnum]=all;
  89. return "[\tubbcodeplace_"+cnum+"\t]";
  90. });
  91. sUBB=sUBB.replace(/<(\/?)(b|u|i|s)(\s+[^>]*?)?>/ig,'[$1$2]');
  92. sUBB=sUBB.replace(/<(\/?)strong(\s+[^>]*?)?>/ig,'[$1b]');
  93. sUBB=sUBB.replace(/<(\/?)em(\s+[^>]*?)?>/ig,'[$1i]');
  94. sUBB=sUBB.replace(/<(\/?)(strike|del)(\s+[^>]*?)?>/ig,'[$1s]');
  95. sUBB=sUBB.replace(/<(\/?)(sup|sub)(\s+[^>]*?)?>/ig,'[$1$2]');
  96. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(span)(?:\s+[^>]*?)?\s+style\s*=\s*"((?:[^"]*?;)*\s*(?:font-family|font-size|color|background|background-color)\s*:[^"]*)"(?: [^>]+)?>(((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S])*?<\/\1>)*?<\/\1>)*?)<\/\1>/ig,function(all,tag,style,content){
  97. var face=style.match(/(?:^|;)\s*font-family\s*:\s*([^;]+)/i),size=style.match(/(?:^|;)\s*font-size\s*:\s*([^;]+)/i),color=style.match(/(?:^|;)\s*color\s*:\s*([^;]+)/i),back=style.match(/(?:^|;)\s*(?:background|background-color)\s*:\s*([^;]+)/i),str=content;
  98. if(face)str='[font='+face[1]+']'+str+'[/font]';
  99. if(size)
  100. {
  101. size=mapSize[size[1].toLowerCase()];
  102. if(size)str='[size='+size+']'+str+'[/size]';
  103. }
  104. if(color)str='[color='+formatColor(color[1])+']'+str+'[/color]';
  105. if(back)str='[back='+formatColor(back[1])+']'+str+'[/back]';
  106. return str;
  107. });
  108. function formatColor(c)
  109. {
  110. var matchs;
  111. if(matchs=c.match(/\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/i)){c=(matchs[1]*65536+matchs[2]*256+matchs[3]*1).toString(16);while(c.length<6)c='0'+c;c='#'+c;}
  112. c=c.replace(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i,'#$1$1$2$2$3$3');
  113. return c;
  114. }
  115. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(div|p)(?:\s+[^>]*?)?[\s"';]\s*(?:text-)?align\s*[=:]\s*(["']?)\s*(left|center|right)\s*\2[^>]*>(((?!<\1(\s+[^>]*?)?>)[\s\S])+?)<\/\1>/ig,'[align=$3]$4[/align]');
  116. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(center)(?:\s+[^>]*?)?>(((?!<\1(\s+[^>]*?)?>)[\s\S])*?)<\/\1>/ig,'[align=center]$2[/align]');
  117. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(p|div)(?:\s+[^>]*?)?\s+style\s*=\s*"((?:[^"]*?;)*\s*text-align\s*:[^"]*)"(?: [^>]+)?>(((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S])*?<\/\1>)*?<\/\1>)*?)<\/\1>/ig,function(all,tag,style,content){
  118. });
  119. sUBB=sUBB.replace(/<a(?:\s+[^>]*?)?\s+href=(["'])\s*(.+?)\s*\1[^>]*>\s*([\s\S]*?)\s*<\/a>/ig,function(all,q,url,text){
  120. if(!(url&&text))return '';
  121. var tag='url',str;
  122. if(url.match(/^mailto:/i))
  123. {
  124. tag='email';
  125. url=url.replace(/mailto:(.+?)/i,'$1');
  126. }
  127. str='['+tag;
  128. if(url!=text)str+='='+url;
  129. return str+']'+text+'[/'+tag+']';
  130. });
  131. sUBB=sUBB.replace(/<img(\s+[^>]*?)\/?>/ig,function(all,attr){
  132. var emot=attr.match(/\s+emot\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i);
  133. if(emot)return '[emot='+emot[2]+'/]';
  134. var url=attr.match(regSrc),alt=attr.match(/\s+alt\s*=\s*(["']?)\s*(.*?)\s*\1(\s|$)/i),w=attr.match(regWidth),h=attr.match(regHeight),align=attr.match(/\s+align\s*=\s*(["']?)\s*(\w+)\s*\1(\s|$)/i),str='[img',p='';
  135. if(!url)return '';
  136. p+=alt[2];
  137. if(w||h)p+=','+(w?w[2]:'')+','+(h?h[2]:'');
  138. if(align)p+=','+align[2];
  139. if(p)str+='='+p;
  140. str+=']'+url[2]+'[/img]';
  141. return str;
  142. });
  143. sUBB=sUBB.replace(/<blockquote(?:\s+[^>]*?)?>([\s\S]+?)<\/blockquote>/ig,'[quote]$1[/quote]');
  144. sUBB=sUBB.replace(/<embed((?:\s+[^>]*?)?(?:\s+type\s*=\s*"\s*application\/x-shockwave-flash\s*"|\s+classid\s*=\s*"\s*clsid:d27cdb6e-ae6d-11cf-96b8-4445535400000\s*")[^>]*?)\/?>/ig,function(all,attr){
  145. var url=attr.match(regSrc),w=attr.match(regWidth),h=attr.match(regHeight),str='[flash';
  146. if(!url)return '';
  147. if(w&&h)str+='='+w[2]+','+h[2];
  148. str+=']'+url[2];
  149. return str+'[/flash]';
  150. });
  151. sUBB=sUBB.replace(/<embed((?:\s+[^>]*?)?(?:\s+type\s*=\s*"\s*application\/x-mplayer2\s*"|\s+classid\s*=\s*"\s*clsid:6bf52a52-394a-11d3-b153-00c04f79faa6\s*")[^>]*?)\/?>/ig,function(all,attr){
  152. var url=attr.match(regSrc),w=attr.match(regWidth),h=attr.match(regHeight),p=attr.match(/\s+autostart\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i),str='[media',auto='0';
  153. if(!url)return '';
  154. if(p)if(p[2]=='true')auto='1';
  155. if(w&&h)str+='='+w[2]+','+h[2]+','+auto;
  156. str+=']'+url[2];
  157. return str+'[/media]';
  158. });
  159. sUBB=sUBB.replace(/<table(\s+[^>]*?)?>/ig,function(all,attr){
  160. var str='[table';
  161. if(attr)
  162. {
  163. var w=attr.match(regWidth),b=attr.match(regBg);
  164. if(w)
  165. {
  166. str+='='+w[2];
  167. if(b)str+=','+b[2];
  168. }
  169. }
  170. return str+']';
  171. });
  172. sUBB=sUBB.replace(/<tr(\s+[^>]*?)?>/ig,function(all,attr){
  173. var str='[tr';
  174. if(attr)
  175. {
  176. var bg=attr.match(regBg)
  177. if(bg)str+='='+bg[2];
  178. }
  179. return str+']';
  180. });
  181. sUBB=sUBB.replace(/<(?:th|td)(\s+[^>]*?)?>/ig,function(all,attr){
  182. var str='[td';
  183. if(attr)
  184. {
  185. var col=attr.match(/\s+colspan\s*=\s*(["']?)\s*(\d+)\s*\1(\s|$)/i),row=attr.match(/\s+rowspan\s*=\s*(["']?)\s*(\d+)\s*\1(\s|$)/i),w=attr.match(regWidth);
  186. col=col?col[2]:1;
  187. row=row?row[2]:1;
  188. if(col>1||row>1||w)str+='='+col+','+row;
  189. if(w)str+=','+w[2];
  190. }
  191. return str+']';
  192. });
  193. sUBB=sUBB.replace(/<\/(table|tr)>/ig,'[/$1]');
  194. sUBB=sUBB.replace(/<\/(th|td)>/ig,'[/td]');
  195. sUBB=sUBB.replace(/<ul(\s+[^>]*?)?>/ig,function(all,attr){
  196. var t;
  197. if(attr)t=attr.match(/\s+type\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i);
  198. return '[list'+(t?'='+t[2]:'')+']';
  199. });
  200. sUBB=sUBB.replace(/<ol(\s+[^>]*?)?>/ig,'[list=1]');
  201. sUBB=sUBB.replace(/<li(\s+[^>]*?)?>/ig,'[*]');
  202. sUBB=sUBB.replace(/<\/li>/ig,'');
  203. sUBB=sUBB.replace(/<\/(ul|ol)>/ig,'[/list]');
  204. sUBB=sUBB.replace(/<h([1-6])(\s+[^>]*?)?>/ig,function(all,n){return '\r\n\r\n[size='+(7-n)+'][b]'});
  205. sUBB=sUBB.replace(/<\/h[1-6]>/ig,'[/b][/size]\r\n\r\n');
  206. sUBB=sUBB.replace(/<address(\s+[^>]*?)?>/ig,'\r\n[i]');
  207. sUBB=sUBB.replace(/<\/address>/ig,'[i]\r\n');
  208. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(p)(?:\s+[^>]*?)?>(((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S])*?<\/\1>)*?<\/\1>)*?)<\/\1>/ig,"\r\n\r\n$2\r\n\r\n");
  209. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(div)(?:\s+[^>]*?)?>(((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S]|<\1(\s+[^>]*?)?>((?!<\1(\s+[^>]*?)?>)[\s\S])*?<\/\1>)*?<\/\1>)*?)<\/\1>/ig,"\r\n$2\r\n");
  210. sUBB=sUBB.replace(/((\s|&nbsp;)*\r?\n){3,}/g,"\r\n\r\n");//????2???
  211. sUBB=sUBB.replace(/^((\s|&nbsp;)*\r?\n)+/g,'');//??????
  212. sUBB=sUBB.replace(/((\s|&nbsp;)*\r?\n)+$/g,'');//??????
  213. for(i=1;i<=cnum;i++)sUBB=sUBB.replace("[\tubbcodeplace_"+i+"\t]", arrcode[i]);
  214. sUBB=sUBB.replace(/<[^<>]+?>/g,'');//????HTML??
  215. var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
  216. sUBB=sUBB.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
  217. return sUBB;
  218. }