JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6
[ckeditor.git] / _source / plugins / bbcode / plugin.js
diff --git a/_source/plugins/bbcode/plugin.js b/_source/plugins/bbcode/plugin.js
new file mode 100644 (file)
index 0000000..69de69d
--- /dev/null
@@ -0,0 +1,924 @@
+/*\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+(function()\r
+{\r
+       CKEDITOR.on( 'dialogDefinition', function( ev )\r
+       {\r
+               var tab, name = ev.data.name,\r
+                       definition = ev.data.definition;\r
+\r
+               if ( name == 'link' )\r
+               {\r
+                       definition.removeContents( 'target' );\r
+                       definition.removeContents( 'upload' );\r
+                       definition.removeContents( 'advanced' );\r
+                       tab = definition.getContents( 'info' );\r
+                       tab.remove( 'emailSubject' );\r
+                       tab.remove( 'emailBody' );\r
+               }\r
+               else if ( name == 'image' )\r
+               {\r
+                       definition.removeContents( 'advanced' );\r
+                       tab = definition.getContents( 'Link' );\r
+                       tab.remove( 'cmbTarget' );\r
+                       tab = definition.getContents( 'info' );\r
+                       tab.remove( 'txtAlt' );\r
+                       tab.remove( 'basic' );\r
+               }\r
+       });\r
+\r
+       var bbcodeMap = { 'b' : 'strong', 'u': 'u', 'i' : 'em', 'color' : 'span', 'size' : 'span', 'quote' : 'blockquote', 'code' : 'code', 'url' : 'a', 'email' : 'span', 'img' : 'span', '*' : 'li', 'list' : 'ol' },\r
+                       convertMap = { 'strong' : 'b' , 'b' : 'b', 'u': 'u', 'em' : 'i', 'i': 'i', 'code' : 'code', 'li' : '*' },\r
+                       tagnameMap = { 'strong' : 'b', 'em' : 'i', 'u' : 'u', 'li' : '*', 'ul' : 'list', 'ol' : 'list', 'code' : 'code', 'a' : 'link', 'img' : 'img', 'blockquote' : 'quote' },\r
+                       stylesMap = { 'color' : 'color', 'size' : 'font-size' },\r
+                       attributesMap = { 'url' : 'href', 'email' : 'mailhref', 'quote': 'cite', 'list' : 'listType' };\r
+\r
+       // List of block-like tags.\r
+       var dtd =  CKEDITOR.dtd,\r
+               blockLikeTags = CKEDITOR.tools.extend( { table:1 }, dtd.$block, dtd.$listItem, dtd.$tableContent, dtd.$list );\r
+\r
+       var semicolonFixRegex = /\s*(?:;\s*|$)/;\r
+       function serializeStyleText( stylesObject )\r
+       {\r
+               var styleText = '';\r
+               for ( var style in stylesObject )\r
+               {\r
+                       var styleVal = stylesObject[ style ],\r
+                               text = ( style + ':' + styleVal ).replace( semicolonFixRegex, ';' );\r
+\r
+                       styleText += text;\r
+               }\r
+               return styleText;\r
+       }\r
+\r
+       function parseStyleText( styleText )\r
+       {\r
+               var retval = {};\r
+               ( styleText || '' )\r
+                               .replace( /"/g, '"' )\r
+                               .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g, function( match, name, value )\r
+               {\r
+                       retval[ name.toLowerCase() ] = value;\r
+               } );\r
+               return retval;\r
+       }\r
+\r
+       function RGBToHex( cssStyle )\r
+       {\r
+               return cssStyle.replace( /(?:rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\))/gi, function( match, red, green, blue )\r
+                       {\r
+                               red = parseInt( red, 10 ).toString( 16 );\r
+                               green = parseInt( green, 10 ).toString( 16 );\r
+                               blue = parseInt( blue, 10 ).toString( 16 );\r
+                               var color = [red, green, blue] ;\r
+\r
+                               // Add padding zeros if the hex value is less than 0x10.\r
+                               for ( var i = 0 ; i < color.length ; i++ )\r
+                                       color[i] = String( '0' + color[i] ).slice( -2 ) ;\r
+\r
+                               return '#' + color.join( '' ) ;\r
+                        });\r
+       }\r
+\r
+       // Maintain the map of smiley-to-description.\r
+       var smileyMap = {"smiley":":)","sad":":(","wink":";)","laugh":":D","cheeky":":P","blush":":*)","surprise":":-o","indecision":":|","angry":">:(","angel":"o:)","cool":"8-)","devil":">:-)","crying":";(","kiss":":-*" },\r
+               smileyReverseMap = {},\r
+               smileyRegExp = [];\r
+\r
+       // Build regexp for the list of smiley text.\r
+       for ( var i in smileyMap )\r
+       {\r
+               smileyReverseMap[ smileyMap[ i ] ] = i;\r
+               smileyRegExp.push( smileyMap[ i ].replace( /\(|\)|\:|\/|\*|\-|\|/g, function( match ) { return '\\' + match; } ) );\r
+       }\r
+\r
+       smileyRegExp = new RegExp( smileyRegExp.join( '|' ), 'g' );\r
+\r
+       var decodeHtml = ( function ()\r
+       {\r
+               var regex = [],\r
+                       entities =\r
+                       {\r
+                               nbsp    : '\u00A0',             // IE | FF\r
+                               shy             : '\u00AD',             // IE\r
+                               gt              : '\u003E',             // IE | FF |   --   | Opera\r
+                               lt              : '\u003C'              // IE | FF | Safari | Opera\r
+                       };\r
+\r
+               for ( var entity in entities )\r
+                       regex.push( entity );\r
+\r
+               regex = new RegExp( '&(' + regex.join( '|' ) + ');', 'g' );\r
+\r
+               return function( html )\r
+               {\r
+                       return html.replace( regex, function( match, entity )\r
+                       {\r
+                               return entities[ entity ];\r
+                       });\r
+               };\r
+       })();\r
+\r
+       CKEDITOR.BBCodeParser = function()\r
+       {\r
+               this._ =\r
+               {\r
+                       bbcPartsRegex : /(?:\[([^\/\]=]*?)(?:=([^\]]*?))?\])|(?:\[\/([a-z]{1,16})\])/ig\r
+               };\r
+       };\r
+\r
+       CKEDITOR.BBCodeParser.prototype =\r
+       {\r
+               parse : function( bbcode )\r
+               {\r
+                       var parts,\r
+                                       part,\r
+                                       lastIndex = 0;\r
+\r
+                       while ( ( parts = this._.bbcPartsRegex.exec( bbcode ) ) )\r
+                       {\r
+                               var tagIndex = parts.index;\r
+                               if ( tagIndex > lastIndex )\r
+                               {\r
+                                       var text = bbcode.substring( lastIndex, tagIndex );\r
+                                       this.onText( text, 1 );\r
+                               }\r
+\r
+                               lastIndex = this._.bbcPartsRegex.lastIndex;\r
+\r
+                               /*\r
+                                "parts" is an array with the following items:\r
+                                0 : The entire match for opening/closing tags and line-break;\r
+                                1 : line-break;\r
+                                2 : open of tag excludes option;\r
+                                3 : tag option;\r
+                                4 : close of tag;\r
+                                */\r
+\r
+                               // Opening tag\r
+                               if ( ( part = parts[ 1 ] ) )\r
+                               {\r
+                                       part = part.toLowerCase();\r
+\r
+                                       var tagName = bbcodeMap[ part ],\r
+                                                       attribs = {},\r
+                                                       styles = {},\r
+                                                       optionPart = parts[ 2 ];\r
+\r
+                                       if ( optionPart )\r
+                                       {\r
+                                               if ( part == 'list' )\r
+                                               {\r
+                                                       if ( !isNaN( optionPart ) )\r
+                                                               optionPart = 'decimal';\r
+                                                       else if ( /^[a-z]+$/.test( optionPart ) )\r
+                                                               optionPart = 'lower-alpha';\r
+                                                       else if ( /^[A-Z]+$/.test( optionPart ) )\r
+                                                               optionPart = 'upper-alpha';\r
+                                               }\r
+\r
+                                               if ( stylesMap[ part ] )\r
+                                               {\r
+                                                       // Font size represents percentage.\r
+                                                       if ( part == 'size' )\r
+                                                               optionPart += '%';\r
+\r
+                                                       styles[ stylesMap[ part ] ] = optionPart;\r
+                                                       attribs.style = serializeStyleText( styles );\r
+                                               }\r
+                                               else if ( attributesMap[ part ] )\r
+                                                       attribs[ attributesMap[ part ] ] = optionPart;\r
+                                       }\r
+\r
+                                       // Two special handling - image and email, protect them\r
+                                       // as "span" with an attribute marker.\r
+                                       if ( part == 'email' || part == 'img' )\r
+                                               attribs[ 'bbcode' ] = part;\r
+\r
+                                       this.onTagOpen( tagName, attribs, CKEDITOR.dtd.$empty[ tagName ] );\r
+                               }\r
+                               // Closing tag\r
+                               else if ( ( part = parts[ 3 ] ) )\r
+                                       this.onTagClose( bbcodeMap[ part ] );\r
+                       }\r
+\r
+                       if ( bbcode.length > lastIndex )\r
+                               this.onText( bbcode.substring( lastIndex, bbcode.length ), 1 );\r
+               }\r
+       };\r
+\r
+       /**\r
+        * Creates a {@link CKEDITOR.htmlParser.fragment} from an HTML string.\r
+        * @param {String} source The HTML to be parsed, filling the fragment.\r
+        * @param {Number} [fixForBody=false] Wrap body with specified element if needed.\r
+        * @returns CKEDITOR.htmlParser.fragment The fragment created.\r
+        * @example\r
+        * var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' );\r
+        * alert( fragment.children[0].name );  "b"\r
+        * alert( fragment.children[1].value );  " Text"\r
+        */\r
+       CKEDITOR.htmlParser.fragment.fromBBCode = function( source )\r
+       {\r
+               var parser = new CKEDITOR.BBCodeParser(),\r
+                       fragment = new CKEDITOR.htmlParser.fragment(),\r
+                       pendingInline = [],\r
+                       pendingBrs = 0,\r
+                       currentNode = fragment,\r
+                       returnPoint;\r
+\r
+               function checkPending( newTagName )\r
+               {\r
+                       if ( pendingInline.length > 0 )\r
+                       {\r
+                               for ( var i = 0 ; i < pendingInline.length ; i++ )\r
+                               {\r
+                                       var pendingElement = pendingInline[ i ],\r
+                                               pendingName = pendingElement.name,\r
+                                               pendingDtd = CKEDITOR.dtd[ pendingName ],\r
+                                               currentDtd = currentNode.name && CKEDITOR.dtd[ currentNode.name ];\r
+\r
+                                       if ( ( !currentDtd || currentDtd[ pendingName ] ) && ( !newTagName || !pendingDtd || pendingDtd[ newTagName ] || !CKEDITOR.dtd[ newTagName ] ) )\r
+                                       {\r
+                                               // Get a clone for the pending element.\r
+                                               pendingElement = pendingElement.clone();\r
+\r
+                                               // Add it to the current node and make it the current,\r
+                                               // so the new element will be added inside of it.\r
+                                               pendingElement.parent = currentNode;\r
+                                               currentNode = pendingElement;\r
+\r
+                                               // Remove the pending element (back the index by one\r
+                                               // to properly process the next entry).\r
+                                               pendingInline.splice( i, 1 );\r
+                                               i--;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+\r
+               function checkPendingBrs( tagName, closing )\r
+               {\r
+                       var len = currentNode.children.length,\r
+                               previous = len > 0 && currentNode.children[ len - 1 ],\r
+                               lineBreakParent = !previous && BBCodeWriter.getRule( tagnameMap[ currentNode.name ], 'breakAfterOpen' ),\r
+                               lineBreakPrevious = previous && previous.type == CKEDITOR.NODE_ELEMENT && BBCodeWriter.getRule( tagnameMap[ previous.name ], 'breakAfterClose' ),\r
+                               lineBreakCurrent = tagName && BBCodeWriter.getRule( tagnameMap[ tagName ], closing ? 'breakBeforeClose' : 'breakBeforeOpen' );\r
+\r
+                       if ( pendingBrs && ( lineBreakParent || lineBreakPrevious || lineBreakCurrent ) )\r
+                               pendingBrs--;\r
+\r
+                       // 1. Either we're at the end of block, where it requires us to compensate the br filler\r
+                       // removing logic (from htmldataprocessor).\r
+                       // 2. Or we're at the end of pseudo block, where it requires us to compensate\r
+                       // the bogus br effect.\r
+                       if ( pendingBrs && tagName in blockLikeTags )\r
+                               pendingBrs++;\r
+\r
+                       while ( pendingBrs && pendingBrs-- )\r
+                               currentNode.children.push( previous = new CKEDITOR.htmlParser.element( 'br' ) );\r
+               }\r
+\r
+               function addElement( node, target )\r
+               {\r
+                       checkPendingBrs( node.name, 1 );\r
+\r
+                       target = target || currentNode || fragment;\r
+\r
+                       var len = target.children.length,\r
+                               previous = len > 0 && target.children[ len - 1 ] || null;\r
+\r
+                       node.previous = previous;\r
+                       node.parent = target;\r
+\r
+                       target.children.push( node );\r
+\r
+                       if ( node.returnPoint )\r
+                       {\r
+                               currentNode = node.returnPoint;\r
+                               delete node.returnPoint;\r
+                       }\r
+               }\r
+\r
+               parser.onTagOpen = function( tagName, attributes, selfClosing )\r
+               {\r
+                       var element = new CKEDITOR.htmlParser.element( tagName, attributes );\r
+\r
+                       // This is a tag to be removed if empty, so do not add it immediately.\r
+                       if ( CKEDITOR.dtd.$removeEmpty[ tagName ] )\r
+                       {\r
+                               pendingInline.push( element );\r
+                               return;\r
+                       }\r
+\r
+                       var currentName = currentNode.name;\r
+\r
+                       var currentDtd = currentName\r
+                               && ( CKEDITOR.dtd[ currentName ]\r
+                                       || ( currentNode._.isBlockLike ? CKEDITOR.dtd.div : CKEDITOR.dtd.span ) );\r
+\r
+                       // If the element cannot be child of the current element.\r
+                       if ( currentDtd && !currentDtd[ tagName ] )\r
+                       {\r
+                               var reApply = false,\r
+                                       addPoint;   // New position to start adding nodes.\r
+\r
+                               // If the element name is the same as the current element name,\r
+                               // then just close the current one and append the new one to the\r
+                               // parent. This situation usually happens with <p>, <li>, <dt> and\r
+                               // <dd>, specially in IE. Do not enter in this if block in this case.\r
+                               if ( tagName == currentName )\r
+                                       addElement( currentNode, currentNode.parent );\r
+                               else if ( tagName in CKEDITOR.dtd.$listItem )\r
+                               {\r
+                                       parser.onTagOpen( 'ul', {} );\r
+                                       addPoint = currentNode;\r
+                                       reApply = true;\r
+                               }\r
+                               else\r
+                               {\r
+                                       addElement( currentNode, currentNode.parent );\r
+\r
+                                       // The current element is an inline element, which\r
+                                       // cannot hold the new one. Put it in the pending list,\r
+                                       // and try adding the new one after it.\r
+                                       pendingInline.unshift( currentNode );\r
+                                       reApply = true;\r
+                               }\r
+\r
+                               if ( addPoint )\r
+                                       currentNode = addPoint;\r
+                               // Try adding it to the return point, or the parent element.\r
+                               else\r
+                                       currentNode = currentNode.returnPoint || currentNode.parent;\r
+\r
+                               if ( reApply )\r
+                               {\r
+                                       parser.onTagOpen.apply( this, arguments );\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
+                       checkPending( tagName );\r
+                       checkPendingBrs( tagName );\r
+\r
+                       element.parent = currentNode;\r
+                       element.returnPoint = returnPoint;\r
+                       returnPoint = 0;\r
+\r
+                       if ( element.isEmpty )\r
+                               addElement( element );\r
+                       else\r
+                               currentNode = element;\r
+               };\r
+\r
+               parser.onTagClose = function( tagName )\r
+               {\r
+                       // Check if there is any pending tag to be closed.\r
+                       for ( var i = pendingInline.length - 1 ; i >= 0 ; i-- )\r
+                       {\r
+                               // If found, just remove it from the list.\r
+                               if ( tagName == pendingInline[ i ].name )\r
+                               {\r
+                                       pendingInline.splice( i, 1 );\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
+                       var pendingAdd = [],\r
+                               newPendingInline = [],\r
+                               candidate = currentNode;\r
+\r
+                       while ( candidate.type && candidate.name != tagName )\r
+                       {\r
+                               // If this is an inline element, add it to the pending list, if we're\r
+                               // really closing one of the parents element later, they will continue\r
+                               // after it.\r
+                               if ( !candidate._.isBlockLike )\r
+                                       newPendingInline.unshift( candidate );\r
+\r
+                               // This node should be added to it's parent at this point. But,\r
+                               // it should happen only if the closing tag is really closing\r
+                               // one of the nodes. So, for now, we just cache it.\r
+                               pendingAdd.push( candidate );\r
+\r
+                               candidate = candidate.parent;\r
+                       }\r
+\r
+                       if ( candidate.type )\r
+                       {\r
+                               // Add all elements that have been found in the above loop.\r
+                               for ( i = 0 ; i < pendingAdd.length ; i++ )\r
+                               {\r
+                                       var node = pendingAdd[ i ];\r
+                                       addElement( node, node.parent );\r
+                               }\r
+\r
+                               currentNode = candidate;\r
+\r
+\r
+                               addElement( candidate, candidate.parent );\r
+\r
+                               // The parent should start receiving new nodes now, except if\r
+                               // addElement changed the currentNode.\r
+                               if ( candidate == currentNode )\r
+                                       currentNode = currentNode.parent;\r
+\r
+                               pendingInline = pendingInline.concat( newPendingInline );\r
+                       }\r
+               };\r
+\r
+               parser.onText = function( text )\r
+               {\r
+                       var currentDtd = CKEDITOR.dtd[ currentNode.name ];\r
+                       if ( !currentDtd || currentDtd[ '#' ] )\r
+                       {\r
+                               checkPendingBrs();\r
+                               checkPending();\r
+\r
+                               text.replace(/([\r\n])|[^\r\n]*/g, function( piece, lineBreak )\r
+                               {\r
+                                       if ( lineBreak !== undefined && lineBreak.length )\r
+                                               pendingBrs++;\r
+                                       else if ( piece.length )\r
+                                       {\r
+                                               var lastIndex = 0;\r
+\r
+                                               // Create smiley from text emotion.\r
+                                               piece.replace( smileyRegExp, function( match, index )\r
+                                               {\r
+                                                       addElement( new CKEDITOR.htmlParser.text( piece.substring( lastIndex, index ) ), currentNode );\r
+                                                       addElement( new CKEDITOR.htmlParser.element( 'smiley', { 'desc': smileyReverseMap[ match ] } ), currentNode );\r
+                                                       lastIndex = index + match.length;\r
+                                               });\r
+\r
+                                               if ( lastIndex != piece.length )\r
+                                                       addElement( new CKEDITOR.htmlParser.text( piece.substring( lastIndex, piece.length ) ), currentNode );\r
+                                       }\r
+                               });\r
+                       }\r
+               };\r
+\r
+               // Parse it.\r
+               parser.parse( CKEDITOR.tools.htmlEncode( source ) );\r
+\r
+               // Close all hanging nodes.\r
+               while ( currentNode.type )\r
+               {\r
+                       var parent = currentNode.parent,\r
+                               node = currentNode;\r
+\r
+                       addElement( node, parent );\r
+                       currentNode = parent;\r
+               }\r
+\r
+               return fragment;\r
+       };\r
+\r
+       CKEDITOR.htmlParser.BBCodeWriter = CKEDITOR.tools.createClass(\r
+       {\r
+               $ : function()\r
+               {\r
+                       this._ =\r
+                       {\r
+                               output : [],\r
+                               rules : []\r
+                       };\r
+\r
+                       // List and list item.\r
+                       this.setRules( 'list',\r
+                  {\r
+                          breakBeforeOpen : 1,\r
+                          breakAfterOpen : 1,\r
+                          breakBeforeClose : 1,\r
+                          breakAfterClose : 1\r
+                  } );\r
+\r
+                       this.setRules( '*',\r
+                  {\r
+                          breakBeforeOpen : 1,\r
+                          breakAfterOpen : 0,\r
+                          breakBeforeClose : 1,\r
+                          breakAfterClose : 0\r
+                  } );\r
+\r
+                       this.setRules( 'quote',\r
+                  {\r
+                          breakBeforeOpen : 1,\r
+                          breakAfterOpen : 0,\r
+                          breakBeforeClose : 0,\r
+                          breakAfterClose : 1\r
+                  } );\r
+               },\r
+\r
+               proto :\r
+               {\r
+                       /**\r
+                        * Sets formatting rules for a given tag. The possible rules are:\r
+                        * <ul>\r
+                        *      <li><b>breakBeforeOpen</b>: break line before the opener tag for this element.</li>\r
+                        *      <li><b>breakAfterOpen</b>: break line after the opener tag for this element.</li>\r
+                        *      <li><b>breakBeforeClose</b>: break line before the closer tag for this element.</li>\r
+                        *      <li><b>breakAfterClose</b>: break line after the closer tag for this element.</li>\r
+                        * </ul>\r
+                        *\r
+                        * All rules default to "false". Each call to the function overrides\r
+                        * already present rules, leaving the undefined untouched.\r
+                        *\r
+                        * @param {String} tagName The tag name to which set the rules.\r
+                        * @param {Object} rules An object containing the element rules.\r
+                        * @example\r
+                        * // Break line before and after "img" tags.\r
+                        * writer.setRules( 'list',\r
+                        *     {\r
+                        *         breakBeforeOpen : true\r
+                        *         breakAfterOpen : true\r
+                        *     });\r
+                        */\r
+                       setRules : function( tagName, rules )\r
+                       {\r
+                               var currentRules = this._.rules[ tagName ];\r
+\r
+                               if ( currentRules )\r
+                                       CKEDITOR.tools.extend( currentRules, rules, true );\r
+                               else\r
+                                       this._.rules[ tagName ] = rules;\r
+                       },\r
+\r
+                       getRule : function( tagName, ruleName )\r
+                       {\r
+                               return this._.rules[ tagName ] && this._.rules[ tagName ][ ruleName ];\r
+                       },\r
+\r
+                       openTag : function( tag, attributes )\r
+                       {\r
+                               if ( tag in bbcodeMap )\r
+                               {\r
+                                       if ( this.getRule( tag, 'breakBeforeOpen' ) )\r
+                                               this.lineBreak( 1 );\r
+\r
+                                       this.write( '[', tag );\r
+                                       var option = attributes.option;\r
+                                       option && this.write( '=', option );\r
+                                       this.write( ']' );\r
+\r
+                                       if ( this.getRule( tag, 'breakAfterOpen' ) )\r
+                                               this.lineBreak( 1 );\r
+                               }\r
+                               else if ( tag == 'br' )\r
+                                       this._.output.push( '\n' );\r
+                       },\r
+\r
+                       openTagClose : function() { },\r
+                       attribute : function() { },\r
+\r
+                       closeTag : function( tag )\r
+                       {\r
+                               if ( tag in bbcodeMap )\r
+                               {\r
+                                       if ( this.getRule( tag, 'breakBeforeClose' ) )\r
+                                               this.lineBreak( 1 );\r
+\r
+                                       tag != '*' && this.write( '[/', tag, ']' );\r
+\r
+                                       if ( this.getRule( tag, 'breakAfterClose' ) )\r
+                                               this.lineBreak( 1 );\r
+                               }\r
+                       },\r
+\r
+                       text : function( text )\r
+                       {\r
+                               this.write( text );\r
+                       },\r
+\r
+                       /**\r
+                        * Writes a comment.\r
+                        * @param {String} comment The comment text.\r
+                        * @example\r
+                        * // Writes "&lt;!-- My comment --&gt;".\r
+                        * writer.comment( ' My comment ' );\r
+                        */\r
+                       comment : function() {},\r
+\r
+                       /*\r
+                       * Output line-break for formatting.\r
+                        */\r
+                       lineBreak : function()\r
+                       {\r
+                               // Avoid line break when:\r
+                               // 1) Previous tag already put one.\r
+                               // 2) We're at output start.\r
+                               if ( !this._.hasLineBreak && this._.output.length )\r
+                               {\r
+                                       this.write( '\n' );\r
+                                       this._.hasLineBreak = 1;\r
+                               }\r
+                       },\r
+\r
+                       write : function()\r
+                       {\r
+                               this._.hasLineBreak = 0;\r
+                               var data = Array.prototype.join.call( arguments, '' );\r
+                               this._.output.push( data );\r
+                       },\r
+\r
+                       reset : function()\r
+                       {\r
+                               this._.output = [];\r
+                               this._.hasLineBreak = 0;\r
+                       },\r
+\r
+                       getHtml : function( reset )\r
+                       {\r
+                               var bbcode = this._.output.join( '' );\r
+\r
+                               if ( reset )\r
+                                       this.reset();\r
+\r
+                               return decodeHtml ( bbcode );\r
+                       }\r
+               }\r
+       });\r
+\r
+       var BBCodeWriter = new CKEDITOR.htmlParser.BBCodeWriter();\r
+\r
+       CKEDITOR.plugins.add( 'bbcode',\r
+         {\r
+                 requires : [ 'htmldataprocessor', 'entities' ],\r
+                 beforeInit : function( editor )\r
+                 {\r
+                         // Adapt some critical editor configuration for better support\r
+                         // of BBCode environment.\r
+                         var config = editor.config;\r
+                         CKEDITOR.tools.extend( config,\r
+                         {\r
+                                 enterMode : CKEDITOR.ENTER_BR,\r
+                                 basicEntities: false,\r
+                                 entities : false,\r
+                                 fillEmptyBlocks : false\r
+                         }, true );\r
+                 },\r
+                 init : function( editor )\r
+                 {\r
+                         var config = editor.config;\r
+\r
+                         function BBCodeToHtml( code )\r
+                         {\r
+                                 var fragment = CKEDITOR.htmlParser.fragment.fromBBCode( code ),\r
+                                                 writer = new CKEDITOR.htmlParser.basicWriter();\r
+\r
+                                 fragment.writeHtml( writer, dataFilter );\r
+                                 return writer.getHtml( true );\r
+                         }\r
+\r
+                         var dataFilter = new CKEDITOR.htmlParser.filter();\r
+                         dataFilter.addRules(\r
+                         {\r
+                                 elements :\r
+                                 {\r
+                                         'blockquote' : function( element )\r
+                                         {\r
+                                                 var quoted = new CKEDITOR.htmlParser.element( 'div' );\r
+                                                 quoted.children = element.children;\r
+                                                 element.children = [ quoted ];\r
+                                                 var citeText = element.attributes.cite;\r
+                                                 if ( citeText )\r
+                                                 {\r
+                                                         var cite = new CKEDITOR.htmlParser.element( 'cite' );\r
+                                                         cite.add( new CKEDITOR.htmlParser.text( citeText.replace( /^"|"$/g, '' ) ) );\r
+                                                         delete element.attributes.cite;\r
+                                                         element.children.unshift( cite );\r
+                                                 }\r
+                                         },\r
+                                         'span' : function( element )\r
+                                         {\r
+                                                 var bbcode;\r
+                                                 if ( ( bbcode = element.attributes.bbcode ) )\r
+                                                 {\r
+                                                         if ( bbcode == 'img' )\r
+                                                         {\r
+                                                                 element.name = 'img';\r
+                                                                 element.attributes.src = element.children[ 0 ].value;\r
+                                                                 element.children = [];\r
+                                                         }\r
+                                                         else if ( bbcode == 'email' )\r
+                                                         {\r
+                                                                 element.name = 'a';\r
+                                                                 element.attributes.href = 'mailto:' + element.children[ 0 ].value;\r
+                                                         }\r
+\r
+                                                         delete element.attributes.bbcode;\r
+                                                 }\r
+                                         },\r
+                                         'ol' : function ( element )\r
+                                         {\r
+                                                 if ( element.attributes.listType )\r
+                                                 {\r
+                                                         if ( element.attributes.listType != 'decimal' )\r
+                                                                 element.attributes.style = 'list-style-type:' + element.attributes.listType;\r
+                                                 }\r
+                                                 else\r
+                                                         element.name = 'ul';\r
+\r
+                                                 delete element.attributes.listType;\r
+                                         },\r
+                                         a : function( element )\r
+                                         {\r
+                                                 if ( !element.attributes.href )\r
+                                                         element.attributes.href = element.children[ 0 ].value;\r
+                                         },\r
+                                         'smiley' : function( element )\r
+                                         {\r
+                                                       element.name = 'img';\r
+\r
+                                                       var description = element.attributes.desc,\r
+                                                               image = config.smiley_images[ CKEDITOR.tools.indexOf( config.smiley_descriptions, description ) ],\r
+                                                               src = CKEDITOR.tools.htmlEncode( config.smiley_path + image );\r
+\r
+                                                 element.attributes =\r
+                                                 {\r
+                                                         src : src,\r
+                                                         'data-cke-saved-src' : src,\r
+                                                         title :  description,\r
+                                                         alt : description\r
+                                                 };\r
+                                         }\r
+                                 }\r
+                         } );\r
+\r
+                         editor.dataProcessor.htmlFilter.addRules(\r
+                         {\r
+                               elements :\r
+                               {\r
+                                       $ : function( element )\r
+                                       {\r
+                                               var attributes = element.attributes,\r
+                                                               style = parseStyleText( attributes.style ),\r
+                                                               value;\r
+\r
+                                               var tagName = element.name;\r
+                                               if ( tagName in convertMap )\r
+                                                       tagName = convertMap[ tagName ];\r
+                                               else if ( tagName == 'span' )\r
+                                               {\r
+                                                       if ( ( value = style.color ) )\r
+                                                       {\r
+                                                               tagName = 'color';\r
+                                                               value = RGBToHex( value );\r
+                                                       }\r
+                                                       else if ( ( value = style[ 'font-size' ] ) )\r
+                                                       {\r
+                                                               var percentValue = value.match( /(\d+)%$/ );\r
+                                                               if ( percentValue )\r
+                                                               {\r
+                                                                       value = percentValue[ 1 ];\r
+                                                                       tagName = 'size';\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                               else if ( tagName == 'ol' || tagName == 'ul' )\r
+                                               {\r
+                                                       if ( ( value = style[ 'list-style-type'] ) )\r
+                                                       {\r
+                                                               switch ( value )\r
+                                                               {\r
+                                                                       case 'lower-alpha':\r
+                                                                               value = 'a';\r
+                                                                               break;\r
+                                                                       case 'upper-alpha':\r
+                                                                               value = 'A';\r
+                                                                               break;\r
+                                                               }\r
+                                                       }\r
+                                                       else if ( tagName == 'ol' )\r
+                                                               value = 1;\r
+\r
+                                                       tagName = 'list';\r
+                                               }\r
+                                               else if ( tagName == 'blockquote' )\r
+                                               {\r
+                                                       try\r
+                                                       {\r
+                                                               var cite = element.children[ 0 ],\r
+                                                                               quoted = element.children[ 1 ],\r
+                                                                               citeText = cite.name == 'cite' && cite.children[ 0 ].value;\r
+\r
+                                                               if ( citeText )\r
+                                                               {\r
+                                                                       value = '"' + citeText + '"';\r
+                                                                       element.children = quoted.children;\r
+                                                               }\r
+\r
+                                                       }\r
+                                                       catch( er )\r
+                                                       {\r
+                                                       }\r
+\r
+                                                       tagName = 'quote';\r
+                                               }\r
+                                               else if ( tagName == 'a' )\r
+                                               {\r
+                                                       if ( ( value = attributes.href ) )\r
+                                                       {\r
+                                                               if ( value.indexOf( 'mailto:' ) !== -1 )\r
+                                                               {\r
+                                                                       tagName = 'email';\r
+                                                                       // [email] should have a single text child with email address.\r
+                                                                       element.children = [ new CKEDITOR.htmlParser.text( value.replace( 'mailto:', '' ) ) ];\r
+                                                                       value = '';\r
+                                                               }\r
+                                                               else\r
+                                                               {\r
+                                                                       var singleton = element.children.length == 1 && element.children[ 0 ];\r
+                                                                       if ( singleton\r
+                                                                                       && singleton.type == CKEDITOR.NODE_TEXT\r
+                                                                                       && singleton.value == value )\r
+                                                                               value = '';\r
+\r
+                                                                       tagName = 'url';\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                               else if ( tagName == 'img' )\r
+                                               {\r
+                                                       element.isEmpty = 0;\r
+\r
+                                                       // Translate smiley (image) to text emotion.\r
+                                                       var src = attributes[ 'data-cke-saved-src' ];\r
+                                                       if ( src && src.indexOf( editor.config.smiley_path ) != -1 )\r
+                                                               return new CKEDITOR.htmlParser.text( smileyMap[ attributes.alt ] );\r
+                                                       else\r
+                                                               element.children = [ new CKEDITOR.htmlParser.text( src ) ];\r
+                                               }\r
+\r
+                                               element.name = tagName;\r
+                                               value && ( element.attributes.option = value );\r
+\r
+                                               return null;\r
+                                       },\r
+\r
+                                       // Remove any bogus br from the end of a pseudo block,\r
+                                       // e.g. <div>some text<br /><p>paragraph</p></div>\r
+                                       br : function( element )\r
+                                       {\r
+                                               var next = element.next;\r
+                                               if ( next && next.name in blockLikeTags )\r
+                                                       return false;\r
+                                       }\r
+                               }\r
+                         }, 1 );\r
+\r
+                         editor.dataProcessor.writer = BBCodeWriter;\r
+\r
+                         editor.on( 'editingBlockReady', function ()\r
+                         {\r
+                                 var wysiwyg = editor._.modes[ 'wysiwyg' ];\r
+                                 wysiwyg.loadData = CKEDITOR.tools.override( wysiwyg.loadData, function( org )\r
+                                 {\r
+                                         return function( data )\r
+                                         {\r
+                                                 return ( org.call( this, BBCodeToHtml( data ) ) );\r
+                                         };\r
+                                 } );\r
+                         } );\r
+                 },\r
+\r
+                 afterInit : function( editor )\r
+                 {\r
+                         var filters;\r
+                         if ( editor._.elementsPath  )\r
+                         {\r
+                                 // Eliminate irrelevant elements from displaying, e.g body and p.\r
+                                 if ( ( filters = editor._.elementsPath.filters ) )\r
+                                       filters.push( function( element )\r
+                                               {\r
+                                                       var htmlName = element.getName(),\r
+                                                               name = tagnameMap[ htmlName ] || false;\r
+\r
+                                                       // Specialized anchor presents as email.\r
+                                                       if ( name == 'link' && element.getAttribute( 'href' ).indexOf( 'mailto:' ) === 0 )\r
+                                                               name = 'email';\r
+                                                       // Styled span could be either size or color.\r
+                                                       else if ( htmlName == 'span' )\r
+                                                       {\r
+                                                               if ( element.getStyle( 'font-size' ) )\r
+                                                                       name = 'size';\r
+                                                               else if ( element.getStyle( 'color' ) )\r
+                                                                       name = 'color';\r
+                                                       }\r
+                                                       else if ( name == 'img' )\r
+                                                       {\r
+                                                               var src = element.data( 'cke-saved-src' );\r
+                                                               if ( src && src.indexOf( editor.config.smiley_path ) === 0 )\r
+                                                                       name = 'smiley';\r
+                                                       }\r
+\r
+                                                       return name;\r
+                                               });\r
+                         }\r
+                 }\r
+         } );\r
+\r
+})();\r