--- /dev/null
+/*\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
+\r
+ /**\r
+ * Add to collection with DUP examination.\r
+ * @param {Object} collection\r
+ * @param {Object} element\r
+ * @param {Object} database\r
+ */\r
+ function addSafely( collection, element, database )\r
+ {\r
+ // 1. IE doesn't support customData on text nodes;\r
+ // 2. Text nodes never get chance to appear twice;\r
+ if ( !element.is || !element.getCustomData( 'block_processed' ) )\r
+ {\r
+ element.is && CKEDITOR.dom.element.setMarker( database, element, 'block_processed', true );\r
+ collection.push( element );\r
+ }\r
+ }\r
+\r
+ function getNonEmptyChildren( element )\r
+ {\r
+ var retval = [];\r
+ var children = element.getChildren();\r
+ for ( var i = 0 ; i < children.count() ; i++ )\r
+ {\r
+ var child = children.getItem( i );\r
+ if ( ! ( child.type === CKEDITOR.NODE_TEXT\r
+ && ( /^[ \t\n\r]+$/ ).test( child.getText() ) ) )\r
+ retval.push( child );\r
+ }\r
+ return retval;\r
+ }\r
+\r
+\r
+ /**\r
+ * Dialog reused by both 'creatediv' and 'editdiv' commands.\r
+ * @param {Object} editor\r
+ * @param {String} command The command name which indicate what the current command is.\r
+ */\r
+ function divDialog( editor, command )\r
+ {\r
+ // Definition of elements at which div operation should stopped.\r
+ var divLimitDefinition = ( function(){\r
+\r
+ // Customzie from specialize blockLimit elements\r
+ var definition = CKEDITOR.tools.extend( {}, CKEDITOR.dtd.$blockLimit );\r
+\r
+ // Exclude 'div' itself.\r
+ delete definition.div;\r
+\r
+ // Exclude 'td' and 'th' when 'wrapping table'\r
+ if ( editor.config.div_wrapTable )\r
+ {\r
+ delete definition.td;\r
+ delete definition.th;\r
+ }\r
+ return definition;\r
+ })();\r
+\r
+ // DTD of 'div' element\r
+ var dtd = CKEDITOR.dtd.div;\r
+\r
+ /**\r
+ * Get the first div limit element on the element's path.\r
+ * @param {Object} element\r
+ */\r
+ function getDivLimitElement( element )\r
+ {\r
+ var pathElements = new CKEDITOR.dom.elementPath( element ).elements;\r
+ var divLimit;\r
+ for ( var i = 0; i < pathElements.length ; i++ )\r
+ {\r
+ if ( pathElements[ i ].getName() in divLimitDefinition )\r
+ {\r
+ divLimit = pathElements[ i ];\r
+ break;\r
+ }\r
+ }\r
+ return divLimit;\r
+ }\r
+\r
+ /**\r
+ * Init all fields' setup/commit function.\r
+ * @memberof divDialog\r
+ */\r
+ function setupFields()\r
+ {\r
+ this.foreach( function( field )\r
+ {\r
+ // Exclude layout container elements\r
+ if ( /^(?!vbox|hbox)/.test( field.type ) )\r
+ {\r
+ if ( !field.setup )\r
+ {\r
+ // Read the dialog fields values from the specified\r
+ // element attributes.\r
+ field.setup = function( element )\r
+ {\r
+ field.setValue( element.getAttribute( field.id ) || '' );\r
+ };\r
+ }\r
+ if ( !field.commit )\r
+ {\r
+ // Set element attributes assigned by the dialog\r
+ // fields.\r
+ field.commit = function( element )\r
+ {\r
+ var fieldValue = this.getValue();\r
+ // ignore default element attribute values\r
+ if ( 'dir' == field.id && element.getComputedStyle( 'direction' ) == fieldValue )\r
+ return;\r
+\r
+ if ( fieldValue )\r
+ element.setAttribute( field.id, fieldValue );\r
+ else\r
+ element.removeAttribute( field.id );\r
+ };\r
+ }\r
+ }\r
+ } );\r
+ }\r
+\r
+ /**\r
+ * Wrapping 'div' element around appropriate blocks among the selected ranges.\r
+ * @param {Object} editor\r
+ */\r
+ function createDiv( editor )\r
+ {\r
+ // new adding containers OR detected pre-existed containers.\r
+ var containers = [];\r
+ // node markers store.\r
+ var database = {};\r
+ // All block level elements which contained by the ranges.\r
+ var containedBlocks = [], block;\r
+\r
+ // Get all ranges from the selection.\r
+ var selection = editor.document.getSelection(),\r
+ ranges = selection.getRanges();\r
+ var bookmarks = selection.createBookmarks();\r
+ var i, iterator;\r
+\r
+ // Calcualte a default block tag if we need to create blocks.\r
+ var blockTag = editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p';\r
+\r
+ // collect all included elements from dom-iterator\r
+ for ( i = 0 ; i < ranges.length ; i++ )\r
+ {\r
+ iterator = ranges[ i ].createIterator();\r
+ while ( ( block = iterator.getNextParagraph() ) )\r
+ {\r
+ // include contents of blockLimit elements.\r
+ if ( block.getName() in divLimitDefinition )\r
+ {\r
+ var j, childNodes = block.getChildren();\r
+ for ( j = 0 ; j < childNodes.count() ; j++ )\r
+ addSafely( containedBlocks, childNodes.getItem( j ) , database );\r
+ }\r
+ else\r
+ {\r
+ // Bypass dtd disallowed elements.\r
+ while ( !dtd[ block.getName() ] && block.getName() != 'body' )\r
+ block = block.getParent();\r
+ addSafely( containedBlocks, block, database );\r
+ }\r
+ }\r
+ }\r
+\r
+ CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+ var blockGroups = groupByDivLimit( containedBlocks );\r
+ var ancestor, blockEl, divElement;\r
+\r
+ for ( i = 0 ; i < blockGroups.length ; i++ )\r
+ {\r
+ var currentNode = blockGroups[ i ][ 0 ];\r
+\r
+ // Calculate the common parent node of all contained elements.\r
+ ancestor = currentNode.getParent();\r
+ for ( j = 1 ; j < blockGroups[ i ].length; j++ )\r
+ ancestor = ancestor.getCommonAncestor( blockGroups[ i ][ j ] );\r
+\r
+ divElement = new CKEDITOR.dom.element( 'div', editor.document );\r
+\r
+ // Normalize the blocks in each group to a common parent.\r
+ for ( j = 0; j < blockGroups[ i ].length ; j++ )\r
+ {\r
+ currentNode = blockGroups[ i ][ j ];\r
+\r
+ while ( !currentNode.getParent().equals( ancestor ) )\r
+ currentNode = currentNode.getParent();\r
+\r
+ // This could introduce some duplicated elements in array.\r
+ blockGroups[ i ][ j ] = currentNode;\r
+ }\r
+\r
+ // Wrapped blocks counting\r
+ var fixedBlock = null;\r
+ for ( j = 0 ; j < blockGroups[ i ].length ; j++ )\r
+ {\r
+ currentNode = blockGroups[ i ][ j ];\r
+\r
+ // Avoid DUP elements introduced by grouping.\r
+ if ( !( currentNode.getCustomData && currentNode.getCustomData( 'block_processed' ) ) )\r
+ {\r
+ currentNode.is && CKEDITOR.dom.element.setMarker( database, currentNode, 'block_processed', true );\r
+\r
+ // Establish new container, wrapping all elements in this group.\r
+ if ( !j )\r
+ divElement.insertBefore( currentNode );\r
+\r
+ divElement.append( currentNode );\r
+ }\r
+ }\r
+\r
+ CKEDITOR.dom.element.clearAllMarkers( database );\r
+ containers.push( divElement );\r
+ }\r
+\r
+ selection.selectBookmarks( bookmarks );\r
+ return containers;\r
+ }\r
+\r
+ function getDiv( editor )\r
+ {\r
+ var path = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() ),\r
+ blockLimit = path.blockLimit,\r
+ div = blockLimit && blockLimit.getAscendant( 'div', true );\r
+ return div;\r
+ }\r
+ /**\r
+ * Divide a set of nodes to different groups by their path's blocklimit element.\r
+ * Note: the specified nodes should be in source order naturally, which mean they are supposed to producea by following class:\r
+ * * CKEDITOR.dom.range.Iterator\r
+ * * CKEDITOR.dom.domWalker\r
+ * @return {Array []} the grouped nodes\r
+ */\r
+ function groupByDivLimit( nodes )\r
+ {\r
+ var groups = [],\r
+ lastDivLimit = null,\r
+ path, block;\r
+ for ( var i = 0 ; i < nodes.length ; i++ )\r
+ {\r
+ block = nodes[i];\r
+ var limit = getDivLimitElement( block );\r
+ if ( !limit.equals( lastDivLimit ) )\r
+ {\r
+ lastDivLimit = limit ;\r
+ groups.push( [] ) ;\r
+ }\r
+ groups[ groups.length - 1 ].push( block ) ;\r
+ }\r
+ return groups;\r
+ }\r
+\r
+ // Synchronous field values to other impacted fields is required, e.g. div styles\r
+ // change should also alter inline-style text.\r
+ function commitInternally( targetFields )\r
+ {\r
+ var dialog = this.getDialog(),\r
+ element = dialog._element && dialog._element.clone()\r
+ || new CKEDITOR.dom.element( 'div', editor.document );\r
+\r
+ // Commit this field and broadcast to target fields.\r
+ this.commit( element, true );\r
+\r
+ targetFields = [].concat( targetFields );\r
+ var length = targetFields.length, field;\r
+ for ( var i = 0; i < length; i++ )\r
+ {\r
+ field = dialog.getContentElement.apply( dialog, targetFields[ i ].split( ':' ) );\r
+ field && field.setup && field.setup( element, true );\r
+ }\r
+ }\r
+\r
+\r
+ // Registered 'CKEDITOR.style' instances.\r
+ var styles = {} ;\r
+ /**\r
+ * Hold a collection of created block container elements.\r
+ */\r
+ var containers = [];\r
+ /**\r
+ * @type divDialog\r
+ */\r
+ return {\r
+ title : editor.lang.div.title,\r
+ minWidth : 400,\r
+ minHeight : 165,\r
+ contents :\r
+ [\r
+ {\r
+ id :'info',\r
+ label :editor.lang.common.generalTab,\r
+ title :editor.lang.common.generalTab,\r
+ elements :\r
+ [\r
+ {\r
+ type :'hbox',\r
+ widths : [ '50%', '50%' ],\r
+ children :\r
+ [\r
+ {\r
+ id :'elementStyle',\r
+ type :'select',\r
+ style :'width: 100%;',\r
+ label :editor.lang.div.styleSelectLabel,\r
+ 'default' : '',\r
+ // Options are loaded dynamically.\r
+ items :\r
+ [\r
+ [ editor.lang.common.notSet , '' ]\r
+ ],\r
+ onChange : function()\r
+ {\r
+ commitInternally.call( this, [ 'info:class', 'advanced:dir', 'advanced:style' ] );\r
+ },\r
+ setup : function( element )\r
+ {\r
+ for ( var name in styles )\r
+ styles[ name ].checkElementRemovable( element, true ) && this.setValue( name );\r
+ },\r
+ commit: function( element )\r
+ {\r
+ var styleName;\r
+ if ( ( styleName = this.getValue() ) )\r
+ {\r
+ var style = styles[ styleName ];\r
+ var customData = element.getCustomData( 'elementStyle' ) || '';\r
+\r
+ style.applyToObject( element );\r
+ element.setCustomData( 'elementStyle', customData + style._.definition.attributes.style );\r
+ }\r
+ }\r
+ },\r
+ {\r
+ id :'class',\r
+ type :'text',\r
+ label :editor.lang.common.cssClass,\r
+ 'default' : ''\r
+ }\r
+ ]\r
+ }\r
+ ]\r
+ },\r
+ {\r
+ id :'advanced',\r
+ label :editor.lang.common.advancedTab,\r
+ title :editor.lang.common.advancedTab,\r
+ elements :\r
+ [\r
+ {\r
+ type :'vbox',\r
+ padding :1,\r
+ children :\r
+ [\r
+ {\r
+ type :'hbox',\r
+ widths : [ '50%', '50%' ],\r
+ children :\r
+ [\r
+ {\r
+ type :'text',\r
+ id :'id',\r
+ label :editor.lang.common.id,\r
+ 'default' : ''\r
+ },\r
+ {\r
+ type :'text',\r
+ id :'lang',\r
+ label :editor.lang.link.langCode,\r
+ 'default' : ''\r
+ }\r
+ ]\r
+ },\r
+ {\r
+ type :'hbox',\r
+ children :\r
+ [\r
+ {\r
+ type :'text',\r
+ id :'style',\r
+ style :'width: 100%;',\r
+ label :editor.lang.common.cssStyle,\r
+ 'default' : '',\r
+ commit : function( element )\r
+ {\r
+ // Merge with 'elementStyle', which is of higher priority.\r
+ var merged = this.getValue() + ( element.getCustomData( 'elementStyle' ) || '' );\r
+ element.setAttribute( 'style', merged );\r
+ }\r
+ }\r
+ ]\r
+ },\r
+ {\r
+ type :'hbox',\r
+ children :\r
+ [\r
+ {\r
+ type :'text',\r
+ id :'title',\r
+ style :'width: 100%;',\r
+ label :editor.lang.common.advisoryTitle,\r
+ 'default' : ''\r
+ }\r
+ ]\r
+ },\r
+ {\r
+ type :'select',\r
+ id :'dir',\r
+ style :'width: 100%;',\r
+ label :editor.lang.common.langDir,\r
+ 'default' : '',\r
+ items :\r
+ [\r
+ [ editor.lang.common.notSet , '' ],\r
+ [\r
+ editor.lang.common.langDirLtr,\r
+ 'ltr'\r
+ ],\r
+ [\r
+ editor.lang.common.langDirRtl,\r
+ 'rtl'\r
+ ]\r
+ ]\r
+ }\r
+ ]\r
+ }\r
+ ]\r
+ }\r
+ ],\r
+ onLoad : function()\r
+ {\r
+ setupFields.call( this );\r
+\r
+ // Preparing for the 'elementStyle' field.\r
+ var dialog = this,\r
+ stylesField = this.getContentElement( 'info', 'elementStyle' );\r
+\r
+ // Reuse the 'stylescombo' plugin's styles definition.\r
+ editor.getStylesSet( function( stylesDefinitions )\r
+ {\r
+ var styleName;\r
+\r
+ if ( stylesDefinitions )\r
+ {\r
+ // Digg only those styles that apply to 'div'.\r
+ for ( var i = 0 ; i < stylesDefinitions.length ; i++ )\r
+ {\r
+ var styleDefinition = stylesDefinitions[ i ];\r
+ if ( styleDefinition.element && styleDefinition.element == 'div' )\r
+ {\r
+ styleName = styleDefinition.name;\r
+ styles[ styleName ] = new CKEDITOR.style( styleDefinition );\r
+\r
+ // Populate the styles field options with style name.\r
+ stylesField.items.push( [ styleName, styleName ] );\r
+ stylesField.add( styleName, styleName );\r
+ }\r
+ }\r
+ }\r
+\r
+ // We should disable the content element\r
+ // it if no options are available at all.\r
+ stylesField[ stylesField.items.length > 1 ? 'enable' : 'disable' ]();\r
+\r
+ // Now setup the field value manually.\r
+ setTimeout( function() { stylesField.setup( dialog._element ); }, 0 );\r
+ } );\r
+ },\r
+ onShow : function()\r
+ {\r
+ // Whether always create new container regardless of existed\r
+ // ones.\r
+ if ( command == 'editdiv' )\r
+ {\r
+ // Try to discover the containers that already existed in\r
+ // ranges\r
+ var div = getDiv( editor );\r
+ // update dialog field values\r
+ div && this.setupContent( this._element = div );\r
+ }\r
+ },\r
+ onOk : function()\r
+ {\r
+ if ( command == 'editdiv' )\r
+ containers = [ this._element ];\r
+ else\r
+ containers = createDiv( editor, true );\r
+\r
+ // Update elements attributes\r
+ var size = containers.length;\r
+ for ( var i = 0; i < size; i++ )\r
+ {\r
+ this.commitContent( containers[ i ] );\r
+\r
+ // Remove empty 'style' attribute.\r
+ !containers[ i ].getAttribute( 'style' ) && containers[ i ].removeAttribute( 'style' );\r
+ }\r
+\r
+ this.hide();\r
+ },\r
+ onHide : function()\r
+ {\r
+ // Remove style only when editing existing DIV. (#6315)\r
+ if ( command == 'editdiv' )\r
+ this._element.removeCustomData( 'elementStyle' );\r
+ delete this._element;\r
+ }\r
+ };\r
+ }\r
+\r
+ CKEDITOR.dialog.add( 'creatediv', function( editor )\r
+ {\r
+ return divDialog( editor, 'creatediv' );\r
+ } );\r
+ CKEDITOR.dialog.add( 'editdiv', function( editor )\r
+ {\r
+ return divDialog( editor, 'editdiv' );\r
+ } );\r
+} )();\r
+\r
+/*\r
+ * @name CKEDITOR.config.div_wrapTable\r
+ * Whether to wrap the whole table instead of indivisual cells when created 'div' in table cell.\r
+ * @type Boolean\r
+ * @default false\r
+ * @example config.div_wrapTable = true;\r
+ */\r