PageRenderTime 3459ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/htdocs/xheditor/src/ubb.src.js

https://github.com/pennsong/ShiyishiWeb
JavaScript | 240 lines | 212 code | 18 blank | 10 comment | 30 complexity | d64758565cec0fd230db8dab66158b02 MD5 | raw file
  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.6 (build 100513)
  10. */
  11. function ubb2html(sUBB)
  12. {
  13. var i,sHtml=String(sUBB),arrcode=new Array(),cnum=0;
  14. sHtml=sHtml.replace(/&/ig, '&amp;');
  15. sHtml=sHtml.replace(/[<>]/g,function(c){return {'<':'&lt;','>':'&gt;'}[c];});
  16. sHtml=sHtml.replace(/\r?\n/g,"<br />");
  17. sHtml=sHtml.replace(/\[code\s*(?:=\s*([^\]]+?))?\]([\s\S]*?)\[\/code\]/ig,function(all,t,c){//code特殊处理
  18. cnum++;arrcode[cnum]=all;
  19. return "[\tubbcodeplace_"+cnum+"\t]";
  20. });
  21. sHtml=sHtml.replace(/\[(\/?)(b|u|i|s|sup|sub)\]/ig,'<$1$2>');
  22. sHtml=sHtml.replace(/\[color\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<font color="$1">');
  23. sHtml=sHtml.replace(/\[size\s*=\s*(\d+?)\s*\]/ig,'<font size="$1">');
  24. sHtml=sHtml.replace(/\[font\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<font face="$1">');
  25. sHtml=sHtml.replace(/\[\/(color|size|font)\]/ig,'</font>');
  26. sHtml=sHtml.replace(/\[back\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/ig,'<span style="background-color:$1;">');
  27. sHtml=sHtml.replace(/\[\/back\]/ig,'</span>');
  28. for(i=0;i<3;i++)sHtml=sHtml.replace(/\[align\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\](((?!\[align(?:\s+[^\]]+)?\])[\s\S])*?)\[\/align\]/ig,'<p align="$1">$2</p>');
  29. sHtml=sHtml.replace(/\[img\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/img\]/ig,'<img src="$1" alt="" />');
  30. 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){
  31. var str='<img src="'+src+'" alt="'+alt+'"',a=p3?p3:(!isNum(p1)?p1:'');
  32. if(isNum(p1))str+=' width="'+p1+'"';
  33. if(isNum(p2))str+=' height="'+p2+'"'
  34. if(a)str+=' align="'+a+'"';
  35. str+=' />';
  36. return str;
  37. });
  38. sHtml=sHtml.replace(/\[emot\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\/\]/ig,'<img emot="$1" />');
  39. sHtml=sHtml.replace(/\[url\]\s*(((?!")[\s\S])*?)(?:"[\s\S]*?)?\s*\[\/url\]/ig,'<a href="$1">$1</a>');
  40. sHtml=sHtml.replace(/\[url\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]*?)\s*\[\/url\]/ig,'<a href="$1">$2</a>');
  41. sHtml=sHtml.replace(/\[email\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/email\]/ig,'<a href="mailto:$1">$1</a>');
  42. sHtml=sHtml.replace(/\[email\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]+?)\s*\[\/email\]/ig,'<a href="mailto:$1">$2</a>');
  43. sHtml=sHtml.replace(/\[quote\]([\s\S]*?)\[\/quote\]/ig,'<blockquote>$1</blockquote>');
  44. sHtml=sHtml.replace(/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/flash\]/ig,function(all,w,h,url){
  45. if(!w)w=480;if(!h)h=400;
  46. 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+'"/>';
  47. });
  48. 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){
  49. if(!w)w=480;if(!h)h=400;
  50. return '<embed type="application/x-mplayer2" src="'+url+'" enablecontextmenu="false" autostart="'+(play=='1'?'true':'false')+'" width="'+w+'" height="'+h+'"/>';
  51. });
  52. sHtml=sHtml.replace(/\[table\s*(?:=\s*(\d{1,4}%?)\s*(?:,\s*([^\]"]+)(?:"[^\]]*?)?)?)?\s*\]/ig,function(all,w,b){
  53. var str='<table';
  54. if(w)str+=' width="'+w+'"';
  55. if(b)str+=' bgcolor="'+b+'"';
  56. return str+'>';
  57. });
  58. sHtml=sHtml.replace(/\[tr\s*(?:=\s*([^\]"]+?)(?:"[^\]]*?)?)?\s*\]/ig,function(all,bg){
  59. return '<tr'+(bg?' bgcolor="'+bg+'"':'')+'>';
  60. });
  61. 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){
  62. return '<td'+(col>1?' colspan="'+col+'"':'')+(row>1?' rowspan="'+row+'"':'')+(w?' width="'+w+'"':'')+'>';
  63. });
  64. sHtml=sHtml.replace(/\[\/(table|tr|td)\]/ig,'</$1>');
  65. sHtml=sHtml.replace(/\[\*\]((?:(?!\[\*\]|\[\/list\]|\[list\s*(?:=[^\]]+)?\])[\s\S])+)/ig,'<li>$1</li>');
  66. sHtml=sHtml.replace(/\[list\s*(?:=\s*([^\]"]+?)(?:"[^\]]*?)?)?\s*\]/ig,function(all,type){
  67. var str='<ul';
  68. if(type)str+=' type="'+type+'"';
  69. return str+'>';
  70. });
  71. sHtml=sHtml.replace(/\[\/list\]/ig,'</ul>');
  72. for(i=1;i<=cnum;i++)sHtml=sHtml.replace("[\tubbcodeplace_"+i+"\t]", arrcode[i]);
  73. sHtml=sHtml.replace(/(^|<\/?\w+(?:\s+[^>]*?)?>)([^<$]+)/ig, function(all,tag,text){
  74. return tag+text.replace(/[\t ]/g,function(c){return {'\t':'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',' ':'&nbsp;'}[c];});
  75. });
  76. function isNum(s){if(s!=null&&s!='')return !isNaN(s);else return false;}
  77. return sHtml;
  78. }
  79. function html2ubb(sHtml)
  80. {
  81. 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};
  82. 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
  83. var i,sUBB=String(sHtml),arrcode=new Array(),cnum=0;
  84. sUBB=sUBB.replace(/\s*\r?\n\s*/g,'');
  85. sUBB = sUBB.replace(/<(script|style)(\s+[^>]*?)?>[\s\S]*?<\/\1>/ig, '');
  86. sUBB = sUBB.replace(/<!--[\s\S]*?-->/ig,'');
  87. sUBB=sUBB.replace(/<br\s*?\/?>/ig,"\r\n");
  88. sUBB=sUBB.replace(/\[code\s*(=\s*([^\]]+?))?\]([\s\S]*?)\[\/code\]/ig,function(all,t,c){//code特殊处理
  89. cnum++;arrcode[cnum]=all;
  90. return "[\tubbcodeplace_"+cnum+"\t]";
  91. });
  92. sUBB=sUBB.replace(/<(\/?)(b|u|i|s)(\s+[^>]*?)?>/ig,'[$1$2]');
  93. sUBB=sUBB.replace(/<(\/?)strong(\s+[^>]*?)?>/ig,'[$1b]');
  94. sUBB=sUBB.replace(/<(\/?)em(\s+[^>]*?)?>/ig,'[$1i]');
  95. sUBB=sUBB.replace(/<(\/?)(strike|del)(\s+[^>]*?)?>/ig,'[$1s]');
  96. sUBB=sUBB.replace(/<(\/?)(sup|sub)(\s+[^>]*?)?>/ig,'[$1$2]');
  97. 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){
  98. 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;
  99. if(face)str='[font='+face[1]+']'+str+'[/font]';
  100. if(size)
  101. {
  102. size=mapSize[size[1].toLowerCase()];
  103. if(size)str='[size='+size+']'+str+'[/size]';
  104. }
  105. if(color)str='[color='+formatColor(color[1])+']'+str+'[/color]';
  106. if(back)str='[back='+formatColor(back[1])+']'+str+'[/back]';
  107. return str;
  108. });
  109. function formatColor(c)
  110. {
  111. var matchs;
  112. 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;}
  113. c=c.replace(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i,'#$1$1$2$2$3$3');
  114. return c;
  115. }
  116. 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]');
  117. for(i=0;i<3;i++)sUBB=sUBB.replace(/<(center)(?:\s+[^>]*?)?>(((?!<\1(\s+[^>]*?)?>)[\s\S])*?)<\/\1>/ig,'[align=center]$2[/align]');
  118. 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){
  119. });
  120. sUBB=sUBB.replace(/<a(?:\s+[^>]*?)?\s+href=(["'])\s*(.+?)\s*\1[^>]*>\s*([\s\S]*?)\s*<\/a>/ig,function(all,q,url,text){
  121. if(!(url&&text))return '';
  122. var tag='url',str;
  123. if(url.match(/^mailto:/i))
  124. {
  125. tag='email';
  126. url=url.replace(/mailto:(.+?)/i,'$1');
  127. }
  128. str='['+tag;
  129. if(url!=text)str+='='+url;
  130. return str+']'+text+'[/'+tag+']';
  131. });
  132. sUBB=sUBB.replace(/<img(\s+[^>]*?)\/?>/ig,function(all,attr){
  133. var emot=attr.match(/\s+emot\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i);
  134. if(emot)return '[emot='+emot[2]+'/]';
  135. 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='';
  136. if(!url)return '';
  137. p+=alt[2];
  138. if(w||h)p+=','+(w?w[2]:'')+','+(h?h[2]:'');
  139. if(align)p+=','+align[2];
  140. if(p)str+='='+p;
  141. str+=']'+url[2]+'[/img]';
  142. return str;
  143. });
  144. sUBB=sUBB.replace(/<blockquote(?:\s+[^>]*?)?>([\s\S]+?)<\/blockquote>/ig,'[quote]$1[/quote]');
  145. 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){
  146. var url=attr.match(regSrc),w=attr.match(regWidth),h=attr.match(regHeight),str='[flash';
  147. if(!url)return '';
  148. if(w&&h)str+='='+w[2]+','+h[2];
  149. str+=']'+url[2];
  150. return str+'[/flash]';
  151. });
  152. 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){
  153. 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';
  154. if(!url)return '';
  155. if(p)if(p[2]=='true')auto='1';
  156. if(w&&h)str+='='+w[2]+','+h[2]+','+auto;
  157. str+=']'+url[2];
  158. return str+'[/media]';
  159. });
  160. sUBB=sUBB.replace(/<table(\s+[^>]*?)?>/ig,function(all,attr){
  161. var str='[table';
  162. if(attr)
  163. {
  164. var w=attr.match(regWidth),b=attr.match(regBg);
  165. if(w)
  166. {
  167. str+='='+w[2];
  168. if(b)str+=','+b[2];
  169. }
  170. }
  171. return str+']';
  172. });
  173. sUBB=sUBB.replace(/<tr(\s+[^>]*?)?>/ig,function(all,attr){
  174. var str='[tr';
  175. if(attr)
  176. {
  177. var bg=attr.match(regBg)
  178. if(bg)str+='='+bg[2];
  179. }
  180. return str+']';
  181. });
  182. sUBB=sUBB.replace(/<(?:th|td)(\s+[^>]*?)?>/ig,function(all,attr){
  183. var str='[td';
  184. if(attr)
  185. {
  186. 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);
  187. col=col?col[2]:1;
  188. row=row?row[2]:1;
  189. if(col>1||row>1||w)str+='='+col+','+row;
  190. if(w)str+=','+w[2];
  191. }
  192. return str+']';
  193. });
  194. sUBB=sUBB.replace(/<\/(table|tr)>/ig,'[/$1]');
  195. sUBB=sUBB.replace(/<\/(th|td)>/ig,'[/td]');
  196. sUBB=sUBB.replace(/<ul(\s+[^>]*?)?>/ig,function(all,attr){
  197. var t;
  198. if(attr)t=attr.match(/\s+type\s*=\s*(["']?)\s*(.+?)\s*\1(\s|$)/i);
  199. return '[list'+(t?'='+t[2]:'')+']';
  200. });
  201. sUBB=sUBB.replace(/<ol(\s+[^>]*?)?>/ig,'[list=1]');
  202. sUBB=sUBB.replace(/<li(\s+[^>]*?)?>/ig,'[*]');
  203. sUBB=sUBB.replace(/<\/li>/ig,'');
  204. sUBB=sUBB.replace(/<\/(ul|ol)>/ig,'[/list]');
  205. sUBB=sUBB.replace(/<h([1-6])(\s+[^>]*?)?>/ig,function(all,n){return '\r\n\r\n[size='+(7-n)+'][b]'});
  206. sUBB=sUBB.replace(/<\/h[1-6]>/ig,'[/b][/size]\r\n\r\n');
  207. sUBB=sUBB.replace(/<address(\s+[^>]*?)?>/ig,'\r\n[i]');
  208. sUBB=sUBB.replace(/<\/address>/ig,'[i]\r\n');
  209. 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");
  210. 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");
  211. sUBB=sUBB.replace(/((\s|&nbsp;)*\r?\n){3,}/g,"\r\n\r\n");//限制最多2次换行
  212. sUBB=sUBB.replace(/^((\s|&nbsp;)*\r?\n)+/g,'');//清除开头换行
  213. sUBB=sUBB.replace(/((\s|&nbsp;)*\r?\n)+$/g,'');//清除结尾换行
  214. for(i=1;i<=cnum;i++)sUBB=sUBB.replace("[\tubbcodeplace_"+i+"\t]", arrcode[i]);
  215. sUBB=sUBB.replace(/<[^<>]+?>/g,'');//删除所有HTML标签
  216. sUBB=sUBB.replace(/&lt;/ig, '<');
  217. sUBB=sUBB.replace(/&gt;/ig, '>');
  218. sUBB=sUBB.replace(/&nbsp;/ig, ' ');
  219. sUBB=sUBB.replace(/&amp;/ig, '&');
  220. return sUBB;
  221. }