diff --git a/jquery.cleditor.bbcode.js b/jquery.cleditor.bbcode.js index 6c92d7f..b0ed59d 100644 --- a/jquery.cleditor.bbcode.js +++ b/jquery.cleditor.bbcode.js @@ -1,134 +1,203 @@ -/** - @preserve CLEditor BBCode Plugin v1.0.0 - http://premiumsoftware.net/cleditor - requires CLEditor v1.3.0 or later - - Copyright 2010, Chris Landowski, Premium Software, LLC - Dual licensed under the MIT or GPL Version 2 licenses. -*/ - -// ==ClosureCompiler== -// @compilation_level SIMPLE_OPTIMIZATIONS -// @output_file_name jquery.cleditor.bbcode.min.js -// ==/ClosureCompiler== - -/* - - The CLEditor useCSS optional parameter should be set to false for this plugin - to function properly. - - Supported HTML and BBCode Tags: - - Bold Hello - [b]Hello[/b] - Italics Hello - [i]Hello[/i] - Underlined Hello - [u]Hello[/u] - Strikethrough Hello - [s]Hello[/s] - Unordered Lists - [list][*]Red[/*][*]Green[/*][*]Blue[/*][/list] - Ordered Lists
  1. Red
  2. Blue
  3. Green
- [list=1][*]Red[/*][*]Green[/*][*]Blue[/*][/list] - Images - [img]http://premiumsoftware.net/image.jpg[/img] - Links Premium Software - [url=http://premiumsoftware.net]Premium Software[/url] - -*/ - -(function($) { - - // BBCode only supports a small subset of HTML, so remove - // any toolbar buttons that are not currently supported. - $.cleditor.defaultOptions.controls = - "bold italic underline strikethrough removeformat | bullets numbering | " + - "undo redo | image link unlink | cut copy paste pastetext | print source"; - - // Save the previously assigned callback handlers - var oldAreaCallback = $.cleditor.defaultOptions.updateTextArea; - var oldFrameCallback = $.cleditor.defaultOptions.updateFrame; - - // Wireup the updateTextArea callback handler - $.cleditor.defaultOptions.updateTextArea = function(html) { - - // Fire the previously assigned callback handler - if (oldAreaCallback) - html = oldAreaCallback(html); - - // Convert the HTML to BBCode - return $.cleditor.convertHTMLtoBBCode(html); - - } - - // Wireup the updateFrame callback handler - $.cleditor.defaultOptions.updateFrame = function(code) { - - // Fire the previously assigned callback handler - if (oldFrameCallback) - code = oldFrameCallback(code); - - // Convert the BBCode to HTML - return $.cleditor.convertBBCodeToHTML(code); - - } - - // Expose the convertHTMLtoBBCode method - $.cleditor.convertHTMLtoBBCode = function(html) { - - $.each([ - [/[\r|\n]/g, ""], - [//gi, "\n"], - [/(.*?)<\/b>/gi, "[b]$1[/b]"], - [/(.*?)<\/strong>/gi, "[b]$1[/b]"], - [/(.*?)<\/i>/gi, "[i]$1[/i]"], - [/(.*?)<\/em>/gi, "[i]$1[/i]"], - [/(.*?)<\/u>/gi, "[u]$1[/u]"], - [/(.*?)<\/ins>/gi, "[u]$1[/u]"], - [/(.*?)<\/strike>/gi, "[s]$1[/s]"], - [/(.*?)<\/del>/gi, "[s]$1[/s]"], - [/(.*?)<\/a>/gi, "[url=$1]$2[/url]"], - [//gi, "[img]$1[/img]"], - [/
    /gi, "[list]"], - [/<\/ul>/gi, "[/list]"], - [/
      /gi, "[list=1]"], - [/<\/ol>/gi, "[/list]"], - [/
    1. /gi, "[*]"], - [/<\/li>/gi, "[/*]"], - [/<.*?>(.*?)<\/.*?>/g, "$1"] - ], function(index, item) { - html = html.replace(item[0], item[1]); - }); - - return html; - - } - - // Expose the convertBBCodeToHTML method - $.cleditor.convertBBCodeToHTML = function(code) { - - $.each([ - [/\r/g, ""], - [/\n/g, "
      "], - [/\[b\](.*?)\[\/b\]/gi, "$1"], - [/\[i\](.*?)\[\/i\]/gi, "$1"], - [/\[u\](.*?)\[\/u\]/gi, "$1"], - [/\[s\](.*?)\[\/s\]/gi, "$1"], - [/\[url=(.*?)\](.*?)\[\/url\]/gi, "$2"], - [/\[img\](.*?)\[\/img\]/gi, ""], - [/\[list\](.*?)\[\/list\]/gi, "
        $1
      "], - [/\[list=1\](.*?)\[\/list\]/gi, "
        $1
      "], - [/\[list\]/gi, "
        "], - [/\[list=1\]/gi, "
          "], - [/\[\*\](.*?)\[\/\*\]/g, "
        1. $1
        2. "], - [/\[\*\]/g, "
        3. "] - ], function(index, item) { - code = code.replace(item[0], item[1]); - }); - - return code; - - } - -})(jQuery); \ No newline at end of file +/** + @preserve CLEditor BBCode Plugin v1.0.0 + http://premiumsoftware.net/cleditor + requires CLEditor v1.3.0 or later + + Copyright 2010, Chris Landowski, Premium Software, LLC + Dual licensed under the MIT or GPL Version 2 licenses. +*/ + +// ==ClosureCompiler== +// @compilation_level SIMPLE_OPTIMIZATIONS +// @output_file_name jquery.cleditor.bbcode.min.js +// ==/ClosureCompiler== + +/* + + The CLEditor useCSS optional parameter should be set to false for this plugin + to function properly. + + Supported HTML and BBCode Tags: + + Bold Hello + [b]Hello[/b] + Italics Hello + [i]Hello[/i] + Underlined Hello + [u]Hello[/u] + Strikethrough Hello + [s]Hello[/s] + Font Hello + [font=impact]Hello[/font] + Color Hello + [color=ff00ff]Hello[/color] + Size Hello + [size=6]Hello[/size] + Unordered Lists
          • Red
          • Blue
          • Green
          + [list][*]Red[/*][*]Green[/*][*]Blue[/*][/list] + Ordered Lists
          1. Red
          2. Blue
          3. Green
          + [list=1][*]Red[/*][*]Green[/*][*]Blue[/*][/list] + Images + [img]http://premiumsoftware.net/image.jpg[/img] + Links Premium Software + [url=http://premiumsoftware.net]Premium Software[/url] + +*/ +/* +2011-07-09 Sam Clarke : + Added color tag + Added font tag + Added size tag + Changed so that <> will be converted to <> when going from source to WYSIWYG stopping HTML from being manually entered when in BBCode mode + Fixed   not being converted to a space in source view + Fixed div tags sometimes not being removed in chrome when contents is multi-line + */ + +(function($) +{ + // BBCode only supports a small subset of HTML, so remove + // any toolbar buttons that are not currently supported. + $.cleditor.defaultOptions.controls = + "bold italic underline strikethrough removeformat | font color size | bullets numbering | " + + "undo redo | image link unlink | cut copy paste pastetext | print source"; + + // Save the previously assigned callback handlers + var oldAreaCallback = $.cleditor.defaultOptions.updateTextArea; + var oldFrameCallback = $.cleditor.defaultOptions.updateFrame; + + // Wireup the updateTextArea callback handler + $.cleditor.defaultOptions.updateTextArea = function(html) + { + // Fire the previously assigned callback handler + if (oldAreaCallback) + html = oldAreaCallback(html); + + // Convert the HTML to BBCode + return $.cleditor.convertHTMLtoBBCode(html); + + } + + // Wireup the updateFrame callback handler + $.cleditor.defaultOptions.updateFrame = function(code) + { + // Fire the previously assigned callback handler + if (oldFrameCallback) + code = oldFrameCallback(code); + + // Convert the BBCode to HTML + return $.cleditor.convertBBCodeToHTML(code); + } + + // Expose the convertHTMLtoBBCode method + $.cleditor.convertHTMLtoBBCode = function(html) + { + $.each([ + [//gi, "[b]"], + [/<\/b>/gi, "[/b]"], + [//gi, "[b]"], + [/<\/strong>/gi, "[/b]"], + [//gi, "[i]"], + [/<\/i>/gi, "[/i]"], + [//gi, "[i]"], + [/<\/em>/gi, "[/i]"], + [//gi, "[u]"], + [/<\/u>/gi, "[/u]"], + [//gi, "[u]"], + [/<\/ins>/gi, "[/u]"], + [//gi, "[s]"], + [/<\/strike>/gi, "[/s]"], + [//gi, "[s]"], + [/<\/del>/gi, "[/s]"], + [/]*?href="([^"]*?)".*?>([\s\S]*?)<\/a>/gi, "[url=$1]$2[/url]"], + [/]*?src="([^"]*?)"[^>]*?>/gi, "[img]$1[/img]"], + [/
            /gi, "[list]"], + [/<\/ul>/gi, "[/list]"], + [/
              /gi, "[list=1]"], + [/<\/ol>/gi, "[/list]"], + [/
            1. /gi, "[*]"], + [/<\/li>/gi, "[/*]"], + + // new font bbcodes. They are complicated and ugly but on the plus side they do handle nesting correctly! :) + [/]*?color="?([^">]*?)"?>((?:[\s\S](?!]*?color))*?)<\/font>/gi, "[color=$1]$2[/color]"], + [/]*?size="?([^">]*?)"?>((?:[\s\S](?!]*?size))*?)<\/font>/gi, "[size=$1]$2[/size]"], + [/]*?face="?'?([^'">]*?)'?"?>((?:[\s\S](?!]*?face))*?)<\/font>/gi, "[font=$1]$2[/font]"], + + // line breaks + [/[\r|\n]/g, ""], + [/<\/p>/gi, "\n"], + [/
              ]*?>/gi, ""], // divs seem to alias paragraphs in chrome. When you create a blank line it fills + // the divs with a br which causes duplicate line-breaks. One for the paragraph (div) + // and one for the BR. To fix remove the br if all that is contained in it + [/<\/div>/gi, "\n"], // divs seem to paragraphs in chrome so treat them like it + [/]*?>/gi, "\n"], + + //remove any leftover html + [/<[^>]*?>/g, ""], + + // HTML elements + [/ |\u00a0/gi, " "], + [/&/gi, "&"], + [/</gi, "<"], + [/>/gi, ">"], + [/"/gi, "\""] + ], + function(index, item) + { + // Keep running the replace untill all matches are + // replaced. Needed for the nested bbcodes. + var oldHtml=html; + + while((html = html.replace(item[0],item[1])) != oldHtml) + oldHtml=html; + }); + + return html; + } + + // Expose the convertBBCodeToHTML method + $.cleditor.convertBBCodeToHTML = function(code) + { + $.each([ + [/&/g, "&"], + [//g, ">"], + [/\r/g, ""], + [/\n/g, "
              "], + + [/\[color=([^\]]*?)\]/gi, ""], + [/\[\/color\]/gi, ""], + [/\[size=([^\]]*?)\]/gi, ""], + [/\[\/size\]/gi, ""], + [/\[font=([^\]]*?)\]/gi, ""], + [/\[\/font\]/gi, ""], + + [/\[b\]/gi, ""], + [/\[\/b\]/gi, ""], + [/\[i\]/gi, ""], + [/\[\/i\]/gi, ""], + [/\[u\]/gi, ""], + [/\[\/u\]/gi, ""], + [/\[s\]/gi, ""], + [/\[\/s\]/gi, ""], + + [/\[url=(.*?)\](.*?)\[\/url\]/gi, "$2"], + [/\[url\](.*?)\[\/url\]/gi, "$1"], + [/\[img\](.*?)\[\/img\]/gi, ""], + [/\[list\](.*?)\[\/list\]/gi, "
                $1
              "], + [/\[list=1\](.*?)\[\/list\]/gi, "
                $1
              "], + [/\[list\]/gi, "
                "], + [/\[list=1\]/gi, "
                  "], + [/\[\*\](.*?)\[\/\*\]/g, "
                1. $1
                2. "], + [/\[\*\]/g, "
                3. "], + + // replace all double spaces not inside HTML + [/ (?=([^\<\>]*?<|[^\<\>]*?$))/g, "  "] + ], + function(index, item) + { + code = code.replace(item[0], item[1]); + }); + + return code; + } +})(jQuery); diff --git a/jquery.cleditor.bbcode.min.js b/jquery.cleditor.bbcode.min.js new file mode 100644 index 0000000..35c64a2 --- /dev/null +++ b/jquery.cleditor.bbcode.min.js @@ -0,0 +1,14 @@ +/* + CLEditor BBCode Plugin v1.0.0 + http://premiumsoftware.net/cleditor + requires CLEditor v1.3.0 or later + + Copyright 2010, Chris Landowski, Premium Software, LLC + Dual licensed under the MIT or GPL Version 2 licenses. +*/ +(function(b){b.cleditor.defaultOptions.controls="bold italic underline strikethrough removeformat | font color size | bullets numbering | undo redo | image link unlink | cut copy paste pastetext | print source";var c=b.cleditor.defaultOptions.updateTextArea,d=b.cleditor.defaultOptions.updateFrame;b.cleditor.defaultOptions.updateTextArea=function(a){c&&(a=c(a));return b.cleditor.convertHTMLtoBBCode(a)};b.cleditor.defaultOptions.updateFrame=function(a){d&&(a=d(a));return b.cleditor.convertBBCodeToHTML(a)}; +b.cleditor.convertHTMLtoBBCode=function(a){b.each([[//gi,"[b]"],[/<\/b>/gi,"[/b]"],[//gi,"[b]"],[/<\/strong>/gi,"[/b]"],[//gi,"[i]"],[/<\/i>/gi,"[/i]"],[//gi,"[i]"],[/<\/em>/gi,"[/i]"],[//gi,"[u]"],[/<\/u>/gi,"[/u]"],[//gi,"[u]"],[/<\/ins>/gi,"[/u]"],[//gi,"[s]"],[/<\/strike>/gi,"[/s]"],[//gi,"[s]"],[/<\/del>/gi,"[/s]"],[/]*?href="([^"]*?)".*?>([\s\S]*?)<\/a>/gi,"[url=$1]$2[/url]"],[/]*?src="([^"]*?)"[^>]*?>/gi,"[img]$1[/img]"],[/
                    /gi,"[list]"], +[/<\/ul>/gi,"[/list]"],[/
                      /gi,"[list=1]"],[/<\/ol>/gi,"[/list]"],[/
                    1. /gi,"[*]"],[/<\/li>/gi,"[/*]"],[/]*?color="?([^">]*?)"?>((?:[\s\S](?!]*?color))*?)<\/font>/gi,"[color=$1]$2[/color]"],[/]*?size="?([^">]*?)"?>((?:[\s\S](?!]*?size))*?)<\/font>/gi,"[size=$1]$2[/size]"],[/]*?face="?'?([^'">]*?)'?"?>((?:[\s\S](?!]*?face))*?)<\/font>/gi,"[font=$1]$2[/font]"],[/[\r|\n]/g,""],[/<\/p>/gi,"\n"],[/
                      ]*?>/gi,""],[/<\/div>/gi,"\n"],[/]*?>/gi, +"\n"],[/<[^>]*?>/g,""],[/ |\u00a0/gi," "],[/&/gi,"&"],[/</gi,"<"],[/>/gi,">"],[/"/gi,'"']],function(b,e){for(var c=a;(a=a.replace(e[0],e[1]))!=c;)c=a});return a};b.cleditor.convertBBCodeToHTML=function(a){b.each([[/&/g,"&"],[//g,">"],[/\r/g,""],[/\n/g,"
                      "],[/\[color=([^\]]*?)\]/gi,''],[/\[\/color\]/gi,""],[/\[size=([^\]]*?)\]/gi,''],[/\[\/size\]/gi,""],[/\[font=([^\]]*?)\]/gi,''],[/\[\/font\]/gi, +""],[/\[b\]/gi,""],[/\[\/b\]/gi,""],[/\[i\]/gi,""],[/\[\/i\]/gi,""],[/\[u\]/gi,""],[/\[\/u\]/gi,""],[/\[s\]/gi,""],[/\[\/s\]/gi,""],[/\[url=(.*?)\](.*?)\[\/url\]/gi,'$2'],[/\[url\](.*?)\[\/url\]/gi,'$1'],[/\[img\](.*?)\[\/img\]/gi,''],[/\[list\](.*?)\[\/list\]/gi,"
                        $1
                      "],[/\[list=1\](.*?)\[\/list\]/gi,"
                        $1
                      "],[/\[list\]/gi,"
                        "],[/\[list=1\]/gi,"
                          "],[/\[\*\](.*?)\[\/\*\]/g,"
                        1. $1
                        2. "], +[/\[\*\]/g,"
                        3. "],[/ (?=([^\<\>]*?<|[^\<\>]*?$))/g,"  "]],function(b,c){a=a.replace(c[0],c[1])});return a}})(jQuery); \ No newline at end of file