Saturday, April 18, 2009

Javascript - forward tool - copy clipboard filter html content

Javascript - forward tool - copy clipboard filter html content

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>論壇轉帖工具 http://www.ip138.com</title>
<script language=javascript>
<!--
function SymError()
{
return true;
}
window.onerror = SymError;
//-->
</script>
<script language="JavaScript" src="js/bbcode.js"></script>
<script language="javascript1.2">
function delnull(){
var str=document.getElementById('text').value;
str=str.replace(/([\s]*\r\n[\s]*){2,}/gm,"\r\n");
document.getElementById('text').value=str;
}
function refix(){
var str=document.getElementById('text').value;
str=str.replace(/([\s]*\r\n[\s]*){2,}/gm,"\r\n\r\n");
document.getElementById('text').value=str;
}
function url(){
var str=document.getElementById('text').value;
str=str.replace(/^[ ]*(http:\/\/|^mms:\/\/|rtsp:\/\/|pnm:\/\/|ftp:\/\/|mmst:\/\/|mmsu:\/\/)([^\r\n]*)$/igm,"[url]$1$2[/url]");
document.getElementById('text').value=str;
}

function addbr(){
var str=document.getElementById('text').value;
str=str.replace(/\r\n/gm,"\r\n\r\n");
document.getElementById('text').value=str;
}

function html_trans(str){
str = str.replace(/\r/g,"");
str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)=("|')?[^>]+?\2(?: |>)/ig,"");
str = str.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,"");

if(set.fontbash.checked){
str = str.replace(/<span[^>]*?display\s*?:\s*?none[^>]*?>([\w\W]*?)<\/span>/ig,"");
str = str.replace(/<span[^>]*?font\s*?-\s*?size\s*?:\s*(0px|0pt)[^>]*?>([\w\W]*?)<\/span>/ig,"");
str = str.replace(/<font([^>]+)(0px|0pt)+([^>]*)>([\w\W]*?)<\/font>/ig,"");
}

if(set.filtrate.checked){
str = str.replace(/[^<]*<([^>]*?)>[^<]*/ig,"<$1>");
var re = /<img[^>]+src=("|')?([^>]+?)\1[^>]*>/gi
//var re = new RegExp(strtm,"ig");
str = str.replace(re,"[img]$2[/img]\r\n\r\n");
}
else{
if(set.stext.checked) str = str.replace(/[^<]*<([^>]*?)>[^<]*/ig,"<$1>");

if(!set.linka.checked) str = str.replace(/<a[^>]+href=[" ]?([^"]+)[" ]?[^>]*>(.*?)<\/a>/ig,"[url=$1]$2[/url]");
if(!set.font_color.checked&&!set.stext.checked)
str = str.replace(/<font([^>]+)color=([^ >]+)([^>]*)>(.*?)<\/font>/ig,"[color=$2]<font$1$3>$4</font>[/color]");
if(!set.font_size.checked&&!set.stext.checked)
str = str.replace(/<font([^>]+)size=([^ >]+)([^>]*)>(.*?)<\/font>/ig,"[size=$2]<font$1$3>$4</font>[/size]");
if(!set.font_face.checked&&!set.stext.checked)
str = str.replace(/<font[^>]+face=([^ >]+)[^>]*>(.*?)<\/font>/ig,"[font=$1]$2[/font]");
if(!set.img.checked) str = str.replace(/<img[^>]+src=[" ]?([^"]+)[" ]?[^>]*>/ig,"\n[img]$1[/img]\n");

if(!set.odj.checked){
str = str.replace(/<object[^>]*?6BF52A52\-394A\-11d3\-B153\-00C04F79FAA6[^>]*?>.*<param[^>]+name\s*=\s*["](url|src)["][^>]+value=[" ]?([^"]+)[" ][^>]*>.*<\/object>/ig,"\n[wmv]$2[/wmv]\n");
str = str.replace(/<object[^>]*?D27CDB6E\-AE6D\-11cf\-96B8\-444553540000[^>]*?>.*<param[^>]+name\s*=\s*["](url|src)["][^>]+value=[" ]?([^"]+)[" ][^>]*>.*<\/object>/ig,"\n[swf]$2[/swf]\n");
str = str.replace(/<embed[^>]*type=["]?application\/x\-shockwave\-flash["]?[^>]*src=[" ]?([^"|^ ]+)[" ]?[^>]*>/ig,"\n[swf]$1[/swf]\n");
str = str.replace(/<embed[^>]*src=["]?([^"|^ ]+)["]?[^>]*type=["]?application\/x\-shockwave\-flash["]?[^>]*>/ig,"\n[swf]$1[/swf]\n");
str = str.replace(/<object[^>]*?CFCDAA03\-8BE4\-11cf\-B84B\-0020AFBBCCFA[^>]*?>.*<param[^>]+name\s*=\s*["](url|src)["][^>]+value=[" ]?([^"]+)[" ][^>]*>.*<\/object>/ig,"\n[rm]$2[/rm]\n");
}
if(!set.font_b.checked&&!set.stext.checked){
str = str.replace(/<([\/]?)b>/ig,"[$1b]");
str = str.replace(/<([\/]?)strong>/ig,"[$1b]");}
if(!set.font_u.checked&&!set.stext.checked)
str = str.replace(/<([\/]?)u>/ig,"[$1u]");
if(!set.font_i.checked&&!set.stext.checked)
str = str.replace(/<([\/]?)i>/ig,"[$1i]");

str = str.replace(/ /g," ");
str = str.replace(/&/g,"&");
str = str.replace(/"/g,"\"");
str = str.replace(/</g,"<");
str = str.replace(/>/g,">");
}
str = str.replace(/<br>/ig,"\n");
str = str.replace(/<p[^>]*?>/ig,"\n\n");
str = str.replace(/<[^>]*?>/g,"");
str = str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");
str = str.replace(/\n+/g,"\n");

return str;
}

function copycode(obj){
obj.select();
objcopy=obj.createTextRange();
objcopy.execCommand("Copy");
}

function trans(){
var str = "";
rtf.focus();
rtf.document.body.innerHTML = "";
rtf.document.execCommand("paste");
str = rtf.document.body.innerHTML;
if(str.length == 0) {
alert("剪貼版不存在超文本數據!");
return "";
}
return html_trans(str);
}

function preview(){
var s = document.getElementById('text').value;
var prewin=window.open("","","");
prewin.document.write(bbcode2html(s));
}

function zhen(str){
strfound=str.replace(/\\/ig,"\\\\");
strfound=strfound.replace(/\[/ig,"\\[");
strfound=strfound.replace(/\]/ig,"\\]");
strfound=strfound.replace(/\{/ig,"\\{");
strfound=strfound.replace(/\}/ig,"\\}");
strfound=strfound.replace(/\|/ig,"\\|");
strfound=strfound.replace(/\//ig,"\\/");
strfound=strfound.replace(/\^/ig,"\\^");
strfound=strfound.replace(/\./ig,"\\.");
strfound=strfound.replace(/\*/ig,"\\*");
strfound=strfound.replace(/\?/ig,"\\?");
strfound=strfound.replace(/\+/ig,"\\+");
return strfound;
}

function replace_star(){
var str=document.getElementById('text').value;
if(!reg.checked)
strfound=zhen(find_text.value);
else
strfound=find_text.value;
var re = new RegExp(strfound,"ig");
str=str.replace(re,replace_text.value);
document.getElementById('text').value=str;
}
</script>
<link href="css/style1.css" rel="stylesheet" type="text/css">
</head>
<body>

<table border="1" bordercolor="#6699CC" cellspacing="0" width="700" align="center" cellpadding="2">
<tr>
<td align=middle bgcolor=#f6f6f6>
<textarea class=textarea onbeforepaste="if(document.getElementById('x_paste').checked){window.clipboardData.setData('text',trans());this.focus();}" id=text style="width:580px;height:200px" name=textarea rows=1 cols=100></textarea>
<iframe id=rtf style="width:0px;height:0px" marginWidth=0 marginHeight=0 src="about:blank" scrolling=no></iframe>
<label for=x_paste></label>
<table borderColor=#f6f6f6 cellSpacing=1 width=600 align=center border=0>
<tr valign=center align=middle>
<td width=80 height=44><input style="width:80px;height:40px" onclick="document.getElementById('text').value += trans();" type=button value=轉換(追加) name=button></td>
<td width=80><input style="width:80px;height:40px" onclick="document.getElementById('text').value = trans()" type=button value=轉換(覆蓋) name=button></td>
<td width=85><input style="width:85px;height:40px" onclick=copycode(window.text) type=button value=複製到剪貼板></td>
<td width=85>
<table cellSpacing=2 cellPadding=0 width=0 border=0>
<tr>
<td><input style="width:85px;height:20px" onclick="preview()" type="button" value="預覽帖子"></td>
</tr>
<tr>
<td><input style="width:85px;height:20px" onclick="document.getElementById('text').value=''" type=button value="清空內容"></td>
</tr>
</table>
</td>
<td width=170>
<table cellSpacing=2 cellPadding=0 width="100%" border=0>
<tr>
<td><input title="自動分析所有超鏈接,並轉換成超鏈接格式 注意:超鏈接必須單獨在一行中!" style="width:80px;" onclick=url() type=button value=分析超鏈接 name=Submit></td>
<td><input title="緊湊格式,刪除所有空行" style="width:80px;" onclick=delnull() type=button value=清除空行 name=Submit></td>
</tr>
<tr>
<td><input title="清除多餘的空行,保持最一個空行" style="width:80px;" onclick=refix() type=button value=清除多餘 name=Submit></td>
<td><input title="為每一行(包括空行)增加一空行" style="width:80px;" onclick=addbr() type=button value=增加空行 name=Submit></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellSpacing=5 cellPadding=0 width=620 border=0>
<form name=set>
<tr align="left">
<td title=轉換的結果中將不含有圖片信息 width="120" height=20><label for=img><input id=img type=checkbox>屏蔽圖片</label></td>
<td title=轉換的結果中將不含有鏈接信息 width="120"><label for=linka><input id=linka type=checkbox>屏蔽超鏈接</label></td>
<td title=轉換的結果中將不含音頻、視頻、FLASH等信息 width="120"><label for=odj><input id=odj type=checkbox>屏蔽對像</label></td>
<td title=轉換的結果中將不含文本信息,同時字體屬性將被自動屏蔽 width="120"><label for=stext><input id=stext type=checkbox>屏蔽文字</label></td>
<td title="為了方便帖子的閱讀,強制把圖片用空行隔開" width="120"><label for=filtrate><input id=filtrate onclick="if(this.checked)window.base_url.style.display='block';else window.base_url.style.display='none';" type=checkbox>啟用圖片過濾</label></td>
</tr><tr align="left">
<td title=屏蔽所有定字體相關的屬性><label for=font><input id=font onclick=font_color.checked=this.checked;font_size.checked=this.checked;font_face.checked=this.checked;font_b.checked=this.checked;font_i.checked=this.checked;font_u.checked=this.checked; type=checkbox>屏蔽字體屬性</label></td>
<td title=屏蔽所有定字體相關的屬性><label><input id=fontbash type=checkbox CHECKED value=checkbox>屏蔽干擾碼</label></td>
<td title=轉換的結果中將不含有字體顏色信息><label for=font_color><input id=font_color type=checkbox>屏蔽字顏色</label></td>
<td title=轉換的結果中將不含有字體尺寸信息><label for=font_size><input id=font_size type=checkbox>屏蔽字體尺寸</label></td>
<td title=轉換的結果中將不含有字體類型信息><label for=font_face><input id=font_face type=checkbox>屏蔽字體類型</label></td>
</tr><tr align="left">
<td title=轉換的結果中將不含有粗體信息><label for=font_b><input id=font_b type=checkbox>屏蔽粗體</label></td>
<td title=轉換的結果中將不含有斜體信息><label for=font_i><input id=font_i type=checkbox>屏蔽斜體</label></td>
<td title=轉換的結果中將不含有下劃線信息><label for=font_u><input id=font_u type=checkbox>屏蔽下劃線</label></td>
<td> </td>
<td> </td>
</tr>
</form>
</table>
<table id="base_url" style="display: none;" border="0" cellpadding="2" cellspacing="0" width="100%">
<tr>
<td align="left" style="padding-left:30px"><font color="#006600">僅保留圖片,圖片之間換行隔開</font><hr color="#000000" size="1" width="100%">
</td>
</tr>
</table>
<table borderColor=#efefef cellSpacing=2 cellPadding=2 width=600 bgcolor=#f6f6f6 border=0>
<tr>
<td>查找 <input id=find_text size=36 name=find_text></td>
<td width="60"><input onclick="set.reset();find_text.value='';replace_text.value='';window.textfield.value='';" type=button value=恢復默認 name=submit></td>
<td width="140" rowspan=2 align=middle><label for=reg><input id=reg type=checkbox checked value=1 name=reg>使用正則語法</label></td>
</tr>
<tr>
<td>替換 <input id=replace_text size=36 name=replace_text></td>
<td><input onclick=replace_star() type=button value="開始替換" name=submit></td>
</tr>
</table>
</td>
</tr>
<tr bgcolor=#f8f8f8>
<td>
<table cellSpacing=0 cellPadding=0 width=600 align=center border=0>
<tr>
<td><p><b>轉貼工具使用說明:</b><br>
1.在網頁中拖動鼠標選中你想要的圖片或文字,然後鼠標右鍵「複製(C)」或按Ctrl+C。<br>
2.進入該頁面,直接按下「轉換(追加)」或者「轉換(覆蓋)」。<br>
3.點擊「複製到剪貼板」,將代碼複製到剪貼板。<br>
4.進入論壇發貼頁面,鼠標右鍵「粘貼(P)」或按Ctrl+V將帖子內容複製到帖子內容框。</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<script>
rtf.document.designMode="On";
</script>

</body>
</html>


js/bbcode.js

/******************************************************************************
Crossday Discuz! Board - HTML <=> BB Code for Discuz!
Copyright 2001-2006 Comsenz Inc. (http://www.comsenz.com)
*******************************************************************************/

var re;
var pcodecount = '-1';
var codecount = 0;
var codehtml = new Array();

function addslashes(str) {
var searcharray = ['\\\\', '\\\'', '\\\/', '\\\(', '\\\)', '\\\[', '\\\]', '\\\{', '\\\}', '\\\^', '\\\$', '\\\?', '\\\.', '\\\*', '\\\+', '\\\|'];
var replacearray = ['\\\\', '\\\'', '\\/', '\\(', '\\)', '\\[', '\\]', '\\{', '\\}', '\\^', '\\$', '\\?', '\\.', '\\*', '\\+', '\\|'];
var len = searcharray.length;

for(var i = 0; i < len; i++) {
re = new RegExp(searcharray[i], "g");
str = str.replace(re, replacearray[i]);
}

return str;
}

function atag(aoptions, text) {
if(trim(text) == '') {
return '';
}

href = getoptionvalue('href', aoptions);

if(href.substr(0, 11) == 'javascript:') {
return trim(recursion('a', text, 'atag'));
} else if(href.substr(0, 7) == 'mailto:') {
tag = 'email';
href = href.substr(7);
} else {
tag = 'url';
}

return '[' + tag + '=' + href + ']' + trim(recursion('a', text, 'atag')) + '[/' + tag + ']';
}

function bbcode2html(str) {

str = trim(str);

if(str == '') {
return '';
}

str= str.replace(/\s*\[code\]([\s\S]+?)\[\/code\]\s*/ig, function($1, $2) {return parsecode($2);});

str = str.replace(/</ig, '<');
str = str.replace(/>/ig, '>');


str= str.replace(/\[url\]\s*(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/ig, function($1, $2, $3) {return cuturl($2 + $3);});
str= str.replace(/\[url=www.([^\[\"']+?)\](.+?)\[\/url\]/ig, '<a href="http://www.$1" target="_blank">$2</a>');
str= str.replace(/\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\]([\s\S]+?)\[\/url\]/ig, '<a href="$1://$2" target="_blank">$3</a>');

str= str.replace(/\[email\](.*?)\[\/email\]/ig, '<a href="mailto:$1">$1</a>');

str= str.replace(/\[email=(.[^\[]*)\](.*?)\[\/email\]/ig, '<a href="mailto:$1" target="_blank">$2</a>');

str = str.replace(/\[color=([^\[\<]+?)\]/ig, '<font color="$1">');

str = str.replace(/\[size=(\d+?)\]/ig, '<font size="$1">');
str = str.replace(/\[size=(\d+(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/ig, '<font style="font-size: $1">');
str = str.replace(/\[font=([^\[\<]+?)\]/ig, '<font face="$1">');
str = str.replace(/\[align=([^\[\<]+?)\]/ig, '<p align="$1">');

re = /\s*\[table(=(\d{1,3}%?))?\][\n\r]*([\s\S]+?)[\n\r]*\[\/table\]\s*/ig;
str = str.replace(re, function($1, $2, $3, $4) {return parsetable($3, $4);});
str = str.replace(re, function($1, $2, $3, $4) {return parsetable($3, $4);});
str = str.replace(re, function($1, $2, $3, $4) {return parsetable($3, $4);});
str = str.replace(re, function($1, $2, $3, $4) {return parsetable($3, $4);});

var searcharray = new Array(
'\\\[\\\/color\\\]', '\\\[\\\/size\\\]', '\\\[\\\/font\\\]', '\\\[\\\/align\\\]', '\\\[b\\\]', '\\\[\\\/b\\\]',
'\\\[i\\\]', '\\\[\\\/i\\\]', '\\\[u\\\]', '\\\[\\\/u\\\]', '\\\[list\\\]', '\\\[list=1\\\]', '\\\[list=a\\\]',
'\\\[list=A\\\]', '\\\[\\\*\\\]', '\\\[\\\/list\\\]', '\\\[indent\\\]', '\\\[\\\/indent\\\]'
);
var replacearray = new Array(
'</font>', '</font>', '</font>', '</p>', '<b>', '</b>', '<i>',
'</i>', '<u>', '</u>', '<ul>', '<ol type=1>', '<ol type=a>',
'<ol type=A>', '<li>', '</ul></ol>', '<blockquote>', '</blockquote>'
);
var len = searcharray.length;
for(var i = 0; i < len; i++) {
re = new RegExp(searcharray[i], "ig");
str = str.replace(re, replacearray[i]);
}

str = str.replace(/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ig, '<img src="$1" border="0" />');
str = str.replace(/\[img=(\d{1,3})[x|\,](\d{1,3})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ig, '<img width="$1" height="$2" src="$3" border="0" alt="" />');



for(var i = 0; i <= pcodecount; i++) {
str = str.replace("[\tDISCUZ_CODE_" + i + "\t]", codehtml[i]);
}

str = str.replace(/\t/ig, '        ');
str = str.replace(/ /ig, '   ');
str = str.replace(/ /ig, '  ');
str = str.replace(/\r\n/ig, '<br />');
str = str.replace(/[\r\n]/ig, '<br />');

return(str);
}

function codetag(text) {
pcodecount++;

text = text.replace(/<br[^\>]*>/ig, "\n");
text = text.replace(/^[\n\r]*([\s\S]+?)[\n\r]*$/ig, '$1');
text = text.replace(/<(\/|)[A-Za-z].*?>/ig, '');

codehtml[pcodecount] = "[code]" + text + "[/code]";
codecount++;
return "[\tDISCUZ_CODE_" + pcodecount + "\t]";
}

function cuturl(url) {
var length = 65;
var urllink = '<a href="' + (url.toLowerCase().substr(0, 4) == 'www.' ? 'http://' + url : url) + '" target="_blank">';
if(url.length > length) {
url = url.substr(0, parseInt(length * 0.5)) + ' ... ' + url.substr(url.length - parseInt(length * 0.3));
}
urllink += url + '</a>';
return urllink;
}

function dpstag(options, text, tagname) {
if(trim(text) == '') {
return '';
}
var pend = parsestyle(options, '', '');
var prepend = pend['prepend'];
var append = pend['append'];
if(in_array(tagname, ['div', 'p'])) {
align = getoptionvalue('align', options);
if(in_array(align, ['left', 'center', 'right'])) {
prepend = '[align=' + align + ']' + prepend;
append += '[/align]';
} else {
append += "\n";
}
}
return prepend + recursion(tagname, text, 'dpstag') + append;
}

function fetchCheckbox(cbn) {
return $(cbn) && $(cbn).checked == true ? 1 : 0;
}

function fetchoptionvalue(option, text) {
if((position = strpos(text, option)) !== false) {
delimiter = position + option.length;
if(text.charAt(delimiter) == '"') {
delimchar = '"';
} else if(text.charAt(delimiter) == '\'') {
delimchar = '\'';
} else {
delimchar = ' ';
}
delimloc = strpos(text, delimchar, delimiter + 1);
if(delimloc === false) {
delimloc = text.length;
} else if(delimchar == '"' || delimchar == '\'') {
delimiter++;
}
return trim(text.substr(delimiter, delimloc - delimiter));
} else {
return '';
}
}

function fonttag(fontoptions, text) {
var prepend = '';
var append = '';
var tags = new Array();
tags = {'font' : 'face=', 'size' : 'size=', 'color' : 'color='};
for(bbcode in tags) {
optionvalue = fetchoptionvalue(tags[bbcode], fontoptions);
if(optionvalue) {
prepend += '[' + bbcode + '=' + optionvalue + ']';
append = '[/' + bbcode + ']' + append;
}
}

var pend = parsestyle(fontoptions, prepend, append);
return pend['prepend'] + recursion('font', text, 'fonttag') + pend['append'];
}

function getoptionvalue(option, text) {
re = new RegExp(option + "(\s+?)?\=(\s+?)?[\"']?(.+?)([\"']|$|>)", "ig");
var matches = re.exec(text);
if(matches != null && matches.length) {
return trim(matches[3]);
}
return '';
}

function html2bbcode(str) {

str = trim(str);

if(str == '' || forumallowhtml || (allowhtml && fetchCheckbox('htmlon'))) {
return str;
}

str= str.replace(/\s*\[code\]([\s\S]+?)\[\/code\]\s*/ig, function($1, $2) {return codetag($2);});
str = str.replace(/<style.*?>[\s\S]*?<\/style>/ig, '');
str = str.replace(/<script.*?>[\s\S]*?<\/script>/ig, '');
str = str.replace(/<noscript.*?>[\s\S]*?<\/noscript>/ig, '');
str = str.replace(/<select.*?>[\s\S]*?<\/select>/ig, '');
str = str.replace(/<object.*?>[\s\S]*?<\/object>/ig, '');
str = str.replace(/<!--[\s\S]*?-->/ig, '');
str = str.replace(/on[a-zA-Z]{3,16}\s?=\s?(["'])[\s\S]*?\1/ig, '');
str = str.replace(/(\r\n|\n|\r)/ig, '');

str = str.replace(/<table([^>]*width[^>]*)>/ig, function($1, $2) {return tabletag($2);});
str = str.replace(/<table[^>]*>/ig, '[table]');
str = str.replace(/<tr[^>]*>/ig, '[tr]');
str = str.replace(/<td>/ig, '[td]');
str = str.replace(/<td([^>]+)>/ig, function($1, $2) {return tdtag($2);});
str = str.replace(/<\/td>/ig, '[/td]');
str = str.replace(/<\/tr>/ig, '[/tr]');
str = str.replace(/<\/table>/ig, '[/table]');

str = str.replace(/<h([0-9]+)[^>]*>(.*)<\/h\\1>/ig, "[size=$1]$2[/size]\n\n");
str = str.replace(/<img[^>]+smilieid=(["']?)(\d+)(\1)[^>]*>/ig, function($1, $2, $3) {return smilies[$3]['code'];});
str = str.replace(/<img([^>]*src[^>]*)>/ig, function($1, $2) {return imgtag($2);});
str = str.replace(/<a\s+?name=(["']?)(.+?)(\1)[\s\S]*?>([\s\S]*?)<\/a>/ig, '$4');
str = str.replace(/<br[^\>]*>/ig, "\n");

str = recursion('b', str, 'simpletag', 'b');
str = recursion('strong', str, 'simpletag', 'b');
str = recursion('i', str, 'simpletag', 'i');
str = recursion('em', str, 'simpletag', 'i');
str = recursion('u', str, 'simpletag', 'u');
str = recursion('a', str, 'atag');
str = recursion('font', str, 'fonttag');
str = recursion('blockquote', str, 'simpletag', 'indent');
str = recursion('ol', str, 'listtag');
str = recursion('ul', str, 'listtag');
str = recursion('div', str, 'dpstag');
str = recursion('p', str, 'dpstag');
str = recursion('span', str, 'dpstag');

str = str.replace(/<[\/\!]*?[^<>]*?>/ig, '');

for(var i = 0; i <= pcodecount; i++) {
str = str.replace("[\tDISCUZ_CODE_" + i + "\t]", codehtml[i]);
}

str = str.replace(/&/ig, '&');
str = str.replace(/ /ig, ' ');
str = str.replace(/</ig, '<');
str = str.replace(/>/ig, '>');

return str
}

function htmlspecialchars(str) {

var f = new Array(
(is_mac && is_ie ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
new RegExp('<', 'g'),
new RegExp('>', 'g'),
new RegExp('"', 'g')
);
var r = new Array(
'&',
'<',
'>',
'"'
);

for(var i = 0; i < f.length; i++) {
str = str.replace(f[i], r[i]);
}

return str;
}

function imgtag(attributes) {
var width = '';
var height = '';

re = /src=(["']?)([\s\S]*?)(\1)/i;
var matches = re.exec(attributes);
if(matches != null) {
var src = matches[2];
} else {
return '';
}

re = /width=(["']?)(\d+)(\1)/i;
var matches = re.exec(attributes);
if(matches != null) {
width = matches[2];
}

re = /height=(["']?)(\d+)(\1)/i;
var matches = re.exec(attributes);
if(matches != null) {
height = matches[2];
}

return width > 0 && height > 0 ?
'[img=' + width + ',' + height + ']' + src + '[/img]' :
'[img]' + src + '[/img]';
}

function listtag(listoptions, text, tagname) {
text = text.replace(/<li>(([\s\S](?!<\/li))*?)(?=<\/?ol|<\/?ul|<li|\[list|\[\/list)/ig, '<li>$1</li>') + (is_opera ? '</li>' : '');
text = recursion('li', text, 'litag');
var opentag = '[list]';
if(tagname == 'ol') {
var listtype = fetchoptionvalue('type=', listoptions);
listtype = listtype != '' ? listtype : '1';
if(in_array(listtype, ['1', 'a', 'A'])) {
opentag = '[list=' + listtype + ']';
}
}
return text ? opentag + recursion(tagname, text, 'listtag') + '[/list]' : '';
}

function litag(listoptions, text) {
return '[*]' + text.replace(/(\s+)$/g, '');
}

function parsecode(text) {
pcodecount++;

text = text.replace(/^[\n\r]*([\s\S]+?)[\n\r]*$/ig, '$1');
text = htmlspecialchars(text);

codehtml[pcodecount] = '[code]' + text + '[/code]';

codecount++;
return "[\tDISCUZ_CODE_" + pcodecount + "\t]";
}

function parsestyle(tagoptions, prepend, append) {
var searchlist = [
['align', true, 'text-align:\\s*(left|center|right);?', 1],
['color', true, '^(?:\\s|)color:\\s*([^;]+);?', 1],
['font', true, 'font-family:\\s*([^;]+);?', 1],
['size', true, 'font-size:\\s*(\\d+(px|pt|in|cm|mm|pc|em|ex|%|));?', 1],
['b', false, 'font-weight:\\s*(bold);?'],
['i', false, 'font-style:\\s*(italic);?'],
['u', false, 'text-decoration:\\s*(underline);?']
];
var style = getoptionvalue('style', tagoptions);
re = /^(?:\s|)color:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)(;?)/ig;
style = style.replace(re, function($1, $2, $3, $4, $5) {return("color:#" + parseInt($2).toString(16) + parseInt($3).toString(16) + parseInt($4).toString(16) + $5);});
var len = searchlist.length;
for(var i = 0; i < len; i++) {
re = new RegExp(searchlist[i][2], "ig");
match = re.exec(style);
if(match != null) {
opnvalue = match[searchlist[i][3]];
prepend += '[' + searchlist[i][0] + (searchlist[i][1] == true ? '=' + opnvalue + ']' : ']');
append = '[/' + searchlist[i][0] + ']' + append;
}
}
return {'prepend' : prepend, 'append' : append};
}

function parsetable(width, str) {
if(typeof width == 'undefined') {
var width = '';
} else {
width = width.substr(width.length - 1, width.length) == '%' ? (width.substr(0, width.length - 1) <= 98 ? width : '98%') : (width <= 560 ? width : '98%');
}

var string = '<table '
+ (width == '' ? '' : 'width="' + width + '" ')
+ 'align="center" class="t_table">';

str = str.replace(/\[td=(\d{1,2}),(\d{1,2})(,(\d{1,3}%?))?\]/ig, '<td colspan="$1" rowspan="$2" width="$4">');
str = str.replace(/\[tr\]/ig, '<tr>');
str = str.replace(/\[td\]/ig, '<td>');
str = str.replace(/\[\/td\]/ig, '</td>');
str = str.replace(/\[\/tr\]/ig, '</tr>');

string += str;
string += '</table>';

return string;
}

function recursion(tagname, text, dofunction, extraargs) {
if(extraargs == null) {
extraargs = '';
}
tagname = tagname.toLowerCase();

var open_tag = '<' + tagname;
var open_tag_len = open_tag.length;
var close_tag = '</' + tagname + '>';
var close_tag_len = close_tag.length;
var beginsearchpos = 0;

do {
var textlower = text.toLowerCase();
var tagbegin = textlower.indexOf(open_tag, beginsearchpos);
if(tagbegin == -1) {
break;
}

var strlen = text.length;

var inquote = '';
var found = false;
var tagnameend = false;
var optionend = 0;
var t_char = '';

for(optionend = tagbegin; optionend <= strlen; optionend++) {
t_char = text.charAt(optionend);
if((t_char == '"' || t_char == "'") && inquote == '') {
inquote = t_char;
} else if((t_char == '"' || t_char == "'") && inquote == t_char) {
inquote = '';
} else if(t_char == '>' && !inquote) {
found = true;
break;
} else if((t_char == '=' || t_char == ' ') && !tagnameend) {
tagnameend = optionend;
}
}

if(!found) {
break;
}
if(!tagnameend) {
tagnameend = optionend;
}

var offset = optionend - (tagbegin + open_tag_len);
var tagoptions = text.substr(tagbegin + open_tag_len, offset)
var acttagname = textlower.substr(tagbegin * 1 + 1, tagnameend - tagbegin - 1);

if(acttagname != tagname) {
beginsearchpos = optionend;
continue;
}

var tagend = textlower.indexOf(close_tag, optionend);
if(tagend == -1) {
break;
}

var nestedopenpos = textlower.indexOf(open_tag, optionend);
while(nestedopenpos != -1 && tagend != -1) {
if(nestedopenpos > tagend) {
break;
}
tagend = textlower.indexOf(close_tag, tagend + close_tag_len);
nestedopenpos = textlower.indexOf(open_tag, nestedopenpos + open_tag_len);
}

if(tagend == -1) {
beginsearchpos = optionend;
continue;
}

var localbegin = optionend + 1;
var localtext = eval(dofunction)(tagoptions, text.substr(localbegin, tagend - localbegin), tagname, extraargs);

text = text.substring(0, tagbegin) + localtext + text.substring(tagend + close_tag_len);

beginsearchpos = tagbegin + localtext.length;

} while(tagbegin != -1);

return text;
}

function simpletag(options, text, tagname, parseto) {
if(trim(text) == '') {
return '';
}
text = recursion(tagname, text, 'simpletag', parseto);
return '[' + parseto + ']' + text + '[/' + parseto + ']';
}

function strpos(haystack, needle, offset) {
if(typeof offset == 'undefined') {
offset = 0;
}

index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

return index == -1 ? false : index;
}

function tabletag(attributes) {
var width = '';
re = /width=(["']?)(\d{1,3}%?)(\1)/ig;
var matches = re.exec(attributes);
if(matches != null && matches.length) {
width = matches[2].substr(matches[2].length - 1, matches[2].length) == '%' ?
(matches[2].substr(0, matches[2].length - 1) <= 98 ? matches[2] : '98%') :
(matches[2] <= 560 ? matches[2] : '98%');
} else {
re = /width\s?:\s?(\d{1,3})([px|%])/ig;
var matches = re.exec(attributes);
if(matches != null && matches.length) {
width = matches[2] == '%' ? (matches[1] <= 98 ? matches[1] : '98%') : (matches[1] <= 560 ? matches[1] : '98%');
}
}
return width == '' ? '[table]' : '[table=' + width + ']';
}

function tdtag(attributes) {

var colspan = 1;
var rowspan = 1;
var width = '';

re = /colspan=(["']?)(\d{1,2})(\1)/ig;
var matches = re.exec(attributes);
if(matches != null) {
colspan = matches[2];
}

re = /rowspan=(["']?)(\d{1,2})(\1)/ig;
var matches = re.exec(attributes);
if(matches != null) {
rowspan = matches[2];
}

re = /width=(["']?)(\d{1,3}%?)(\1)/ig;
var matches = re.exec(attributes);
if(matches != null) {
width = matches[2];
}

return in_array(width, ['', '0', '100%']) ?
(colspan == 1 && rowspan == 1 ? '[td]' : '[td=' + colspan + ',' + rowspan + ']') :
'[td=' + colspan + ',' + rowspan + ',' + width + ']';
}

function trim(str) {
return (str.replace(/(\s+)$/g, '')).replace(/^\s+/g, '');
}

No comments: