ubb.js

来自「asp的bbs程序」· JavaScript 代码 · 共 1,023 行 · 第 1/3 页

JS
1,023
字号
/*
  By Hangring
  #2007.11.02#
  ---
  xhtml与ubb转换
*/
var UBB = {
    host : '',
    
    ubbToXhtml : function (ubb) {//return ubb;
        //ubb = ubb.Trim();
        // [code] store
        var codes = [];
        ubb = this.codeStore(ubb, codes);

        ubb = this.htmlEntities(ubb);

        ubb = ubb.replace(/\[([^\]]*?)\]/g, function (str, arg1) {return '[' + UBB.removeCrlf(arg1) + ']'});
        ubb = ubb.replace(/\r\n|\r|\n/g, '<br/>');
        ubb = this.replaceSpaceToNbsp(ubb);

        // [font]
        while (/\[font=[^\]]+?\](?:(?!\[font=[^\]]+?\])[\s\S])*?\[\/font\]/i.test(ubb)) {
            ubb = ubb.replace(/\[font=([^\]]+?)\]((?:(?!\[font=[^\]]+?\])[\s\S])*?)\[\/font\]/i, '<font face="$1">$2</font>');
        }

        // [size]
        while (/\[size=[^\]]*?\](?:(?!\[size=[^\]]+?\])[\s\S])*?\[\/size\]/i.test(ubb)) {
            ubb = ubb.replace(/\[size=([^\]]*?)\]((?:(?!\[size=[^\]]+?\])[\s\S])*?)\[\/size\]/i, '<font size="$1">$2</font>');
        }

        // [b], [i], [u]
        while (/\[(b|i|u)\](?:(?!\[\1\])[\s\S])*?\[\/\1\]/i.test(ubb)) {
            ubb = ubb.replace(/\[(b|i|u)\]((?:(?!\[\1\])[\s\S])*?)\[\/\1\]/i, '<$1>$2</$1>');
        }
        //ubb = ubb.replace(/\[(\/?(?:b|i|u))\]/gi, '<$1>');

        // [color]
        while (/\[color=[^\]]+?\](?:(?!\[color=[^\]]+?\])[\s\S])*?\[\/color\]/i.test(ubb)) {
            ubb = ubb.replace(/\[color=([^\]]+?)\]((?:(?!\[color=[^\]]+?\])[\s\S])*?)\[\/color\]/i, '<font color="$1">$2</font>');
        }

        // [bgcolor]
        while (/\[bgcolor=[^\]]+?\](?:(?!\[bgcolor=[^\]]+?\])[\s\S])*?\[\/bgcolor\]/i.test(ubb)) {
            ubb = ubb.replace(/\[bgcolor=([^\]]+?)\]((?:(?!\[bgcolor=[^\]]+?\])[\s\S])*?)\[\/bgcolor\]/i, '<font style="background-color: $1">$2</font>');
        }

        // [url]
        while (/\[url=[^\]]+?\](?:(?!\[url=[^\]]+?\])[\s\S])*?\[\/url\]/i.test(ubb)) {
            ubb = ubb.replace(/\[url=([^\]]+?)\]((?:(?!\[url=[^\]]+?\])[\s\S])*?)\[\/url\]/i, function (str, src, text) {
                var className = '';
                if (/#file:([0-9a-z]{8}[0-9a-z]{4}[0-9a-z]{4}[0-9a-z]{4}[0-9a-z]{12}|\d+)#/.exec(src))
                    className = ' class="downloadfiles"';
                return '<a href="' + src + '"' + className + '>' + text + '</a>';
            });
        }
        
        // [em:],[face:]
        ubb = this.faceUbbToHtml(ubb);

        // [img]
        //while (/\[img\](?:(?!\[img\])[\s\S])+?\[\/img\]/i.test(ubb)) {
            ubb = ubb.replace(/\[img\]((?:(?!\[img\]).)+?)\[\/img\]/gi, '<img src="$1" alt="" />');
        //}
        //while (/\[img(=[^\]]*?)?\](?:(?!\[img(=[^\]]*?)?\])[\s\S])+?\[\/img\]/i.test(ubb)) {
            ubb = ubb.replace(/\[img=(\d+),(\d+)\]((?:(?!\[img(?:=[^\]]*?)?\])[\s\S])+?)\[\/img\]/gi, function (str,w, h, src) {
                return getImageHtml(src, w, h, 'px');
            });
            ubb = ubb.replace(/\[img=(\d+%),(\d+%)\]((?:(?!\[img(?:=[^\]]*?)?\])[\s\S])+?)\[\/img\]/gi, function (str,w, h, src) {
                return getImageHtml(src, w, h, '%');
            });
            ubb = ubb.replace(/\[img=(\d+%?)\]((?:(?!\[img(?:=[^\]]*?)?\])[\s\S])+?)\[\/img\]/gi, function (str,wh, src) {
                var unit = '';
                if (Browser.IsIE && /^\d+$/.test(wh))
                    unit = 'px';
                return getImageHtml(src, wh, wh, unit);
            });
            //ubb = ubb.replace(/\[img(?:=[^\]]*?)?\]((?:(?!\[img(?:=[^\]]*?)?\])[\s\S])+?)\[\/img\]/i, '<img src="$1" alt="" />');
        //}
        function getImageHtml (src, w, h, unit) {
            return Browser.IsIE ? '<img src="' + src + '" style="width:' + w + unit + ';height:' + h + unit + '" alt="" />'
                                : '<img src="' + src + '" width="' + w + '" height="' + h + '" alt="" />';
        }

        // [list]
        while (/\[list(=[^\]]*?)?\]((?!\[list(=[^\]]*?)?\])[\s\S])*?\[\/list\]/i.test(ubb)) {
            ubb = ubb.replace(/\[list(=[^\]]*?)?\]((?!\[list(=[^\]]*?)?\])[\s\S])*?\[\/list\]/i, function(str){
                if (str.indexOf('[list=') != -1) {
                    str = str.replace(/\[list\](?:(?!\[\*\])[\s\S])*?\[\*\]/i, '<ol><li>');
                    str = str.replace(/\[list=([^\]]*?)\](?:(?!\[\*\])[\s\S])*?\[\*\]/i, '<ol type="$1"><li>');
                    str = str.replace(/\[\/list\]/i, '</li></ol>');
                }
                else {
                    str = str.replace(/\[list\](?:(?!\[\*\])[\s\S])*?\[\*\]/i, '<ul><li>');
                    str = str.replace(/\[\/list\]/i, '</li></ul>');
                }
                str = str.replace(/\[\*\]/g, '</li><li>');
                return str;
            });
        }

        // [table]
        ubb = this.tableUbbToHtml(ubb);

        // [align]
        while (/\[align=[^\]]+?\](?:(?!\[align=[^\]]+?\])[\s\S])*?\[\/align\]/i.test(ubb)) {
            ubb = ubb.replace(/\[align=[^\]]+?\](?:(?!\[align=[^\]]+?\])[\s\S])*?\[\/align\]/i, function (str) {
                if (Browser.IsIE) {
                    return str.replace(/\[align=([^\]]+?)\]((?:(?!\[align=[^\]]+?\])[\s\S])*?)\[\/align\]/i, '<p align="$1">$2</p>');
                }
                else {
                    return str.replace(/\[align=([^\]]+?)\]((?:(?!\[align=[^\]]+?\])[\s\S])*?)\[\/align\]/i, '<div align="$1">$2</div>');
                }
            });
        }

        // [sup], [sub]
        while (/\[(sup)\](?:(?!\[\1\])[\s\S])+?\[\/\1\]/i.test(ubb)) {
            ubb = ubb.replace(/\[(sup)\]((?:(?!\[\1\])[\s\S])+?)\[\/\1\]/i, '<$1>$2</$1>');
        }
        while (/\[(sub)\](?:(?!\[\1\])[\s\S])+?\[\/\1\]/i.test(ubb)) {
            ubb = ubb.replace(/\[(sub)\]((?:(?!\[\1\])[\s\S])+?)\[\/\1\]/i, '<$1>$2</$1>');
        }

        // [fly]
        while (/\[fly=[^\]]+?\](?:(?!\[fly=[^\]]+?\])[\s\S])+?\[\/fly\]/i.test(ubb)) {
            ubb = ubb.replace(/\[fly=([^\]]+?)\]((?:(?!\[fly=[^\]]+?\])[\s\S])+?)\[\/fly\]/i, '<marquee direction="$1">$2</marquee>');
        }

        // [flash]

        // [wmv]

        // [wma]

        // [rm]

        // [ra]

        // [qq]

        // [msn], [live]

        // [email]

        // [quote]

        // [br]
        ubb = ubb.replace(/\[br\]/gi, '<br/>');

        // [indent]
        //ubb = ubb.replace(/\[indent\]((?:(?!\[indent\])[\s\S])*?)\[\/indent\]/gi, '<blockquote class="quote">$1</blockquote>');
        while (/\[indent\](?:(?!\[indent\])[\s\S])+?\[\/indent\]/i.test(ubb)) {
            ubb = ubb.replace(/\[indent\]((?:(?!\[indent\])[\s\S])+?)\[\/indent\]/i, '<blockquote>$1</blockquote>');
        }
        
        // [quote]
        while (/\[quote\](?:(?!\[quote\])[\s\S])+?\[\/quote\]/i.test(ubb)) {
            ubb = ubb.replace(/\[quote\]((?:(?!\[quote\])[\s\S])+?)\[\/quote\]/i, '<div class="maxcode-quote">$1</div>');
        }

/*
        // [hide]
        while (/\[hide\](?:(?!\[hide\])[\s\S])+?\[\/hide\]/i.test(ubb)) {
            ubb = ubb.replace(/\[hide\]((?:(?!\[hide\])[\s\S])+?)\[\/hide\]/i, '<blockquote>$1</blockquote>');
        }
*/

        // 取回空格
        ubb = this.retrieveSpace(ubb);

        // [code] restore [safari error]
        ubb = this.codeRestore(ubb, codes, 'html');

        return ubb;
    },

    xhtmlToUbb : function (xhtml) {//alert(xhtml);//return xhtml;
        // [code] store
        var codes = [];
        xhtml = this.codeStore(xhtml, codes);

        xhtml = xhtml.replace(/<([^>]*?)>/g, function (str, arg1) {return '<' + UBB.removeCrlf(arg1) + '>'});
        xhtml = xhtml.replace(/<br[^>]*?>/gi, '\r\n');
        xhtml = this.replaceNbspToSpace(xhtml);

        // [quote]
        // OLD:
        while (/<div[^>]+class="maxcode-quote"[^>]*>\s*<dl>\s*<dt>(?:(?!<\/?dt>)[\s\S])+?<\/dt>\s*<dd>(?:(?!<div[^>]+class="maxcode-quote"[^>]*>)[\s\S])+?<\/dd>\s*<\/dl>\s*<\/div>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<div[^>]+class="maxcode-quote"[^>]*>\s*<dl>\s*<dt>(?:(?!<\/?dt>)[\s\S])+?<\/dt>\s*<dd>((?:(?!<div[^>]+class="maxcode-quote"[^>]*>)[\s\S])+?)<\/dd>\s*<\/dl>\s*<\/div>/i, '[quote]$1[/quote]');
        }
        /*
        NEW: 
        <div class="maxcode-quote">
            [content]
            <p class="maxcode-quoteauthor"> [author] </p>
        </div>
        */
        while (/<div[^>]+class="maxcode-quote"[^>]*>(?:<div[^>]*>(?:(?!<\/div>)[\s\S])*<\/div>|(?:(?!<\/?div[^>]*>)[\s\S]))*?<\/div>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<div[^>]+class="maxcode-quote"[^>]*>((?:<div[^>]*>(?:(?!<\/div>)[\s\S])*<\/div>|(?:(?!<\/?div[^>]*>)[\s\S]))*?)<\/div>/i, function (ubb, content) {
                content = content.replace(/<p[^>]+class="maxcode-quoteauthor"[^>]*>((?:(?!<\/?p[^>]*>)[\s\S])*)<\/p>/gi, '[align=right]$1[/align]');
                return '[quote]' + content + '[/quote]';
            });
        }

        // [font], [size], [color], [bgcolor]
        while (/<font[^>]*?>(?:(?!<font[^>]*?>)[\s\S])*?<\/font>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<font[^>]*?>(?:(?!<font[^>]*?>)[\s\S])*?<\/font>/i, function (str) {
                var arr;
                var font = {};
                //alert(str);

                arr = /<font[^>]*?>((?:(?!<font>)[\s\S])*?)<\/font>/i.exec(str);
                font.value = arr[1] || '';

                // 避免诡异的ie时而有引号,时而无引号
                arr = /<font[^>]+?(face="[^"]+?")[^>]*?>/i.exec(str);
                font.face = arr && arr[1] ? arr[1].substring(6, arr[1].length - 1) : '';
                if (! font.face) {
                    arr = /<font[^>]+?(face=[^\s>]+)[^>]*?>/i.exec(str);
                    font.face = arr && arr[1] ? arr[1].substring(5, arr[1].length).replace('"', '') : '';
                }

                arr = /<font[^>]+?(size="[^"]+?")[^>]*?>/i.exec(str);
                font.size = arr && arr[1] ? arr[1].substring(6, arr[1].length - 1) : '';
                if (! font.size) {
                    arr = /<font[^>]+?(size=[^\s>]+)[^>]*?>/i.exec(str);
                    font.size = arr && arr[1] ? arr[1].substring(5, arr[1].length).replace('"', '') : '';
                }
                //arr = /<font[\s\S]+?(size=[^\s>]+)[^>]*?>/i.exec(str);
                //font.size = arr && arr[1] ? arr[1].substring(5, arr[1].length).replace('"', '') : '';

                arr = /<font[^>]+?(color="[^"]+?")[^>]*?>/i.exec(str);
                font.color = arr && arr[1] ? arr[1].substring(7, arr[1].length - 1) : '';
                if (! font.color) {
                    arr = /<font[^>]+?(color=[^\s>]+)[^>]*?>/i.exec(str);
                    font.color = arr && arr[1] ? arr[1].substring(6, arr[1].length).replace('"', '') : '';
                }
                //arr = /<font[\s\S]+?(color=[^\s>]+)[^>]*?>/i.exec(str);
                //font.color = arr && arr[1] ? arr[1].substring(6, arr[1].length).replace('"', '') : '';

                arr = /<font[\s\S]+?style="background-color:\x01+([^"]+?);?"[^>]*?>/i.exec(str);
                font.bgcolor = arr && arr[1];

                str = font.value;
                //alert('[' + font.face + '] [' + font.size + '] [' + font.color + '] [' + font.bgcolor + '] ' + str);
                if (font.face) {
                    str = '[font=' + font.face + ']' + str + '[/font]';
                }
                if (font.size) {
                    str = '[size=' + font.size + ']' + str + '[/size]';
                }
                if (font.color) {
                    str = '[color=' + font.color + ']' + str + '[/color]';
                }
                if (font.bgcolor) {
                    str = '[bgcolor=' + font.bgcolor + ']' + str + '[/bgcolor]';
                }

                return str;
            });
        }

        // [color]
        while (/<span[^>]+?style="color:\x01*[^"]+?">(?:(?!<span[^>]+?style="color:\x01*[^"]+?">)[\s\S])*?<\/span>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<span[^>]+?style="color:\x01*([^"]+?);?">((?:(?!<span[^>]+?style="color:[^"]+?">)[\s\S])*?)<\/span>/i, '[color=$1]$2[/color]');
        }

        // [bgcolor]
        while (/<span[^>]+?style="background\-color:\x01*[^"]+?">(?:(?!<span[^>]+?style="background\-color:\x01*[^"]+?">)[\s\S])+?<\/span>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<span[^>]+?style="background\-color:\x01*([^"]+?);?">((?:(?!<span[^>]+?style="background\-color:[^"]+?">)[\s\S])+?)<\/span>/i, '[bgcolor=$1]$2[/bgcolor]');
        }

        // [b], [i], [u]
        xhtml = xhtml.replace(/<(\/?(?:b|i|u))>/ig, '[$1]');

        // [b]
        while (/<strong>(?:(?!<strong>)[\s\S])*?<\/strong>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<strong>((?:(?!<strong>)[\s\S])*?)<\/strong>/i, '[b]$1[/b]');
        }

        // [i]
        while (/<em>(?:(?!<em>)[\s\S])*?<\/em>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<em>((?:(?!<em>)[\s\S])*?)<\/em>/i, '[i]$1[/i]');
        }

        // [u]
        //while (/<u>(?:(?!<u>)[\s\S])*?<\/u>/.test(xhtml)) {
        //  xhtml = xhtml.replace(/<u>((?:(?!<u>)[\s\S])*?)<\/u>/, '[u]$1[/u]');
        //}

        // for safari
        // [b],[i],[u],[color],[bgcolor]
        /*
        var span = /<span[^>]+?style="[^"]+?"[^>]*?>(?:(?!<span[^>]+?style="[^"]+?"[^>]*?>)[\s\S])*?<\/span>/i;
        while (span.test(xhtml)) {
            xhtml = xhtml.replace(span, function (str) {
                
            });
        }
        */
        // [b]
        while (/<span[^>]+style="font\-weight:\x01+bold;"[^>]*>(?:(?!<span[^>]+style="font\-weight:\x01+bold;"[^>]*>)[\s\S])*?<\/span>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<span[^>]+style="font\-weight:\x01+bold;"[^>]*>((?:(?!<span[^>]+style="font\-weight:\x01+bold;"[^>]*>)[\s\S])*?)<\/span>/i, '[b]$1[/b]');
        }
        // [i]
        while (/<span[^>]+style="font\-style:\x01+italic;">(?:(?!<span[^>]+style="font\-style:\x01+italic;">)[\s\S])*?<\/span>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<span[^>]+style="font\-style:\x01+italic;">((?:(?!<span[^>]+style="font\-style:\x01+italic;"[^>]*>)[\s\S])*?)<\/span>/i, '[i]$1[/i]');
        }
        // [u]
        while (/<span[^>]+style="text\-decoration:\x01+underline;">(?:(?!<span[^>]+style="text\-decoration:\x01+underline;"[^>]*>)[\s\S])*?<\/span>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<span[^>]+style="text\-decoration:\x01+underline;">((?:(?!<span[^>]+style="text\-decoration:\x01+underline;"[^>]*>)[\s\S])*?)<\/span>/i, '[u]$1[/u]');
        }

        // [url]
        while (/<a[^>]+?href="(?:[^"]*?)"[^>]*?>(?:(?!<a[^>]*?>)[\s\S])*?<\/a>/i.test(xhtml)) {
            xhtml = xhtml.replace(/<a[^>]+?href="([^"]*?)"[^>]*?>((?:(?!<a[^>]*?>)[\s\S])*?)<\/a>/i, '[url=$1]$2[/url]');
        }
        
        // [em:],[face:]
        xhtml = this.faceHtmlToUbb(xhtml);

        // [img]
        //xhtml = xhtml.replace(/<img[^>]+?src="([^"]+?)"[^>]*?\/?>/gi, '[img]$1[/img]');
        xhtml = xhtml.replace(/<img[^>]+?src="([^"]+?)"[^>]*?\/?>/gi, function (str, src) {
            //var arr = /src="([^"]+?)"/.exec(str);
            //var src = arr[1];
            var arr;
            
            // Gecko...
            arr = (/width=(\d+)/i.exec(str)) || (/width="(\d+%?)"/i.exec(str));
            var width = arr && arr[1] ? arr[1] : 0;
            arr = (/height=(\d+)/i.exec(str)) || (/height="(\d+%?)"/i.exec(str));
            var height = arr && arr[1] ? arr[1] : 0;
            
            // IE...
            arr = /width:\x01*(\d+(?:px|%))/i.exec(str);
            if (arr && arr[1]) width = arr[1].replace('px', '');
            arr = /height:\x01*(\d+(?:px|%))/i.exec(str);
            if (arr && arr[1]) height = arr[1].replace('px', '');
            

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?