| 1 | /* |
| 2 | * Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| 3 | * For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | (function() |
| 7 | { |
| 8 | |
| 9 | /** |
| 10 | * Add to collection with DUP examination. |
| 11 | * @param {Object} collection |
| 12 | * @param {Object} element |
| 13 | * @param {Object} database |
| 14 | */ |
| 15 | function addSafely( collection, element, database ) |
| 16 | { |
| 17 | // 1. IE doesn't support customData on text nodes; |
| 18 | // 2. Text nodes never get chance to appear twice; |
| 19 | if ( !element.is || !element.getCustomData( 'block_processed' ) ) |
| 20 | { |
| 21 | element.is && CKEDITOR.dom.element.setMarker( database, element, 'block_processed', true ); |
| 22 | collection.push( element ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | function getNonEmptyChildren( element ) |
| 27 | { |
| 28 | var retval = []; |
| 29 | var children = element.getChildren(); |
| 30 | for( var i = 0 ; i < children.count() ; i++ ) |
| 31 | { |
| 32 | var child = children.getItem( i ); |
| 33 | if( ! ( child.type === CKEDITOR.NODE_TEXT |
| 34 | && /^[ \t\n\r]+$/.test( child.getText() ) ) ) |
| 35 | retval.push( child ); |
| 36 | } |
| 37 | return retval; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Dialog reused by both 'creatediv' and 'editdiv' commands. |
| 43 | * @param {Object} editor |
| 44 | * @param {String} command The command name which indicate what the current command is. |
| 45 | */ |
| 46 | function divDialog( editor, command ) |
| 47 | { |
| 48 | // Definition of elements at which div operation should stopped. |
| 49 | var divLimitDefinition = ( function(){ |
| 50 | |
| 51 | // Customzie from specialize blockLimit elements |
| 52 | var definition = CKEDITOR.tools.extend( {}, CKEDITOR.dtd.$blockLimit ); |
| 53 | |
| 54 | // Exclude 'div' itself. |
| 55 | delete definition.div; |
| 56 | |
| 57 | // Exclude 'td' and 'th' when 'wrapping table' |
| 58 | if( editor.config.div_wrapTable ) |
| 59 | { |
| 60 | delete definition.td; |
| 61 | delete definition.th; |
| 62 | } |
| 63 | return definition; |
| 64 | })(); |
| 65 | |
| 66 | // DTD of 'div' element |
| 67 | var dtd = CKEDITOR.dtd.div; |
| 68 | |
| 69 | /** |
| 70 | * Get the first div limit element on the element's path. |
| 71 | * @param {Object} element |
| 72 | */ |
| 73 | function getDivLimitElement( element ) |
| 74 | { |
| 75 | var pathElements = new CKEDITOR.dom.elementPath( element ).elements; |
| 76 | var divLimit; |
| 77 | for ( var i = 0; i < pathElements.length ; i++ ) |
| 78 | { |
| 79 | if ( pathElements[ i ].getName() in divLimitDefinition ) |
| 80 | { |
| 81 | divLimit = pathElements[ i ]; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | return divLimit; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Init all fields' setup/commit function. |
| 90 | * @memberof divDialog |
| 91 | */ |
| 92 | function setupFields() |
| 93 | { |
| 94 | this.foreach( function( field ) |
| 95 | { |
| 96 | // Exclude layout container elements |
| 97 | if( /^(?!vbox|hbox)/.test( field.type ) ) |
| 98 | { |
| 99 | if ( !field.setup ) |
| 100 | { |
| 101 | // Read the dialog fields values from the specified |
| 102 | // element attributes. |
| 103 | field.setup = function( element ) |
| 104 | { |
| 105 | field.setValue( element.getAttribute( field.id ) || '' ); |
| 106 | }; |
| 107 | } |
| 108 | if ( !field.commit ) |
| 109 | { |
| 110 | // Set element attributes assigned by the dialog |
| 111 | // fields. |
| 112 | field.commit = function( element ) |
| 113 | { |
| 114 | var fieldValue = this.getValue(); |
| 115 | // ignore default element attribute values |
| 116 | if ( 'dir' == field.id && element.getComputedStyle( 'direction' ) == fieldValue ) |
| 117 | return true; |
| 118 | if ( fieldValue ) |
| 119 | element.setAttribute( field.id, fieldValue ); |
| 120 | else |
| 121 | element.removeAttribute( field.id ); |
| 122 | }; |
| 123 | } |
| 124 | } |
| 125 | } ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Wrapping 'div' element around appropriate blocks among the selected ranges. |
| 130 | * @param {Object} editor |
| 131 | */ |
| 132 | function createDiv( editor ) |
| 133 | { |
| 134 | // new adding containers OR detected pre-existed containers. |
| 135 | var containers = []; |
| 136 | // node markers store. |
| 137 | var database = {}; |
| 138 | // All block level elements which contained by the ranges. |
| 139 | var containedBlocks = [], block; |
| 140 | |
| 141 | // Get all ranges from the selection. |
| 142 | var selection = editor.document.getSelection(); |
| 143 | var ranges = selection.getRanges(); |
| 144 | var bookmarks = selection.createBookmarks(); |
| 145 | var i, iterator; |
| 146 | |
| 147 | // Calcualte a default block tag if we need to create blocks. |
| 148 | var blockTag = editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'; |
| 149 | |
| 150 | // collect all included elements from dom-iterator |
| 151 | for( i = 0 ; i < ranges.length ; i++ ) |
| 152 | { |
| 153 | iterator = ranges[ i ].createIterator(); |
| 154 | while( ( block = iterator.getNextParagraph() ) ) |
| 155 | { |
| 156 | // include contents of blockLimit elements. |
| 157 | if( block.getName() in divLimitDefinition ) |
| 158 | { |
| 159 | var j, childNodes = block.getChildren(); |
| 160 | for ( j = 0 ; j < childNodes.count() ; j++ ) |
| 161 | addSafely( containedBlocks, childNodes.getItem( j ) , database ); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | // Bypass dtd disallowed elements. |
| 166 | while( !dtd[ block.getName() ] && block.getName() != 'body' ) |
| 167 | block = block.getParent(); |
| 168 | addSafely( containedBlocks, block, database ); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| 174 | |
| 175 | var blockGroups = groupByDivLimit( containedBlocks ); |
| 176 | var ancestor, blockEl, divElement; |
| 177 | |
| 178 | for( i = 0 ; i < blockGroups.length ; i++ ) |
| 179 | { |
| 180 | var currentNode = blockGroups[ i ][ 0 ]; |
| 181 | |
| 182 | // Calculate the common parent node of all contained elements. |
| 183 | ancestor = currentNode.getParent(); |
| 184 | for ( var j = 1 ; j < blockGroups[ i ].length; j++ ) |
| 185 | ancestor = ancestor.getCommonAncestor( blockGroups[ i ][ j ] ); |
| 186 | |
| 187 | divElement = new CKEDITOR.dom.element( 'div', editor.document ); |
| 188 | |
| 189 | // Normalize the blocks in each group to a common parent. |
| 190 | for( var j = 0; j < blockGroups[ i ].length ; j++ ) |
| 191 | { |
| 192 | currentNode = blockGroups[ i ][ j ]; |
| 193 | |
| 194 | while( !currentNode.getParent().equals( ancestor ) ) |
| 195 | currentNode = currentNode.getParent(); |
| 196 | |
| 197 | blockGroups[ i ][ j ] = currentNode; |
| 198 | } |
| 199 | |
| 200 | // Wrapped blocks counting |
| 201 | var fixedBlock = null; |
| 202 | for ( var j = 0 ; j < blockGroups[ i ].length ; j++ ) |
| 203 | { |
| 204 | currentNode = blockGroups[ i ][ j ]; |
| 205 | // Avoid DUP |
| 206 | if ( !currentNode.is || !currentNode.getCustomData( 'block_processed' ) ) |
| 207 | { |
| 208 | currentNode.is && CKEDITOR.dom.element.setMarker( database, currentNode, 'block_processed', true ); |
| 209 | |
| 210 | // Establish new container, wrapping all elements in this group. |
| 211 | if ( j == 0 ) |
| 212 | divElement.insertBefore(currentNode); |
| 213 | |
| 214 | // If any non-block nodes are added, shuffle them to new blocks. |
| 215 | if ( currentNode.is |
| 216 | && currentNode.isBlockBoundary() ) |
| 217 | { |
| 218 | divElement.append( currentNode ); |
| 219 | fixedBlock = null; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | if ( !fixedBlock ) |
| 224 | { |
| 225 | fixedBlock = editor.document.createElement( blockTag ); |
| 226 | divElement.append( fixedBlock ); |
| 227 | } |
| 228 | currentNode.move( fixedBlock, true ); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| 234 | containers.push( divElement ); |
| 235 | } |
| 236 | |
| 237 | selection.selectBookmarks( bookmarks ); |
| 238 | return containers; |
| 239 | } |
| 240 | |
| 241 | function getDiv( editor ) |
| 242 | { |
| 243 | var path = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() ), |
| 244 | blockLimit = path.blockLimit, |
| 245 | div = blockLimit && blockLimit.getAscendant( 'div', true ); |
| 246 | return div; |
| 247 | } |
| 248 | /** |
| 249 | * Divide a set of nodes to different groups by their path's blocklimit element. |
| 250 | * Note: the specified nodes should be in source order naturally, which mean they are supposed to producea by following class: |
| 251 | * * CKEDITOR.dom.range.Iterator |
| 252 | * * CKEDITOR.dom.domWalker |
| 253 | * @return {Array []} the grouped nodes |
| 254 | */ |
| 255 | function groupByDivLimit( nodes ) |
| 256 | { |
| 257 | var groups = [], |
| 258 | lastDivLimit = null, |
| 259 | path, block; |
| 260 | for ( var i = 0 ; i < nodes.length ; i++ ) |
| 261 | { |
| 262 | block = nodes[i]; |
| 263 | var limit = getDivLimitElement( block ); |
| 264 | if ( !limit.equals( lastDivLimit ) ) |
| 265 | { |
| 266 | lastDivLimit = limit ; |
| 267 | groups.push( [] ) ; |
| 268 | } |
| 269 | groups[ groups.length - 1 ].push( block ) ; |
| 270 | } |
| 271 | return groups; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Hold a collection of created block container elements. |
| 276 | */ |
| 277 | var containers = []; |
| 278 | /** |
| 279 | * @type divDialog |
| 280 | */ |
| 281 | return { |
| 282 | title : editor.lang.div.title, |
| 283 | minWidth : 400, |
| 284 | minHeight : 165, |
| 285 | contents : |
| 286 | [ |
| 287 | { |
| 288 | id :'info', |
| 289 | label :editor.lang.common.generalTab, |
| 290 | title :editor.lang.common.generalTab, |
| 291 | elements : |
| 292 | [ |
| 293 | { |
| 294 | type :'hbox', |
| 295 | widths : [ '50%', '50%' ], |
| 296 | children : |
| 297 | [ |
| 298 | { |
| 299 | id :'elementStyle', |
| 300 | type :'select', |
| 301 | style :'width: 100%;', |
| 302 | label :editor.lang.div.styleSelectLabel, |
| 303 | 'default' : '', |
| 304 | items : [], |
| 305 | setup : function( element ) |
| 306 | { |
| 307 | this.setValue( element.$.style.cssText || '' ); |
| 308 | }, |
| 309 | commit: function( element ) |
| 310 | { |
| 311 | if ( this.getValue() ) |
| 312 | element.$.style.cssText = this.getValue(); |
| 313 | else |
| 314 | element.removeAttribute( 'style' ); |
| 315 | } |
| 316 | }, |
| 317 | { |
| 318 | id :'class', |
| 319 | type :'text', |
| 320 | label :editor.lang.common.cssClass, |
| 321 | 'default' : '' |
| 322 | } |
| 323 | ] |
| 324 | } |
| 325 | ] |
| 326 | }, |
| 327 | { |
| 328 | id :'advanced', |
| 329 | label :editor.lang.common.advancedTab, |
| 330 | title :editor.lang.common.advancedTab, |
| 331 | elements : |
| 332 | [ |
| 333 | { |
| 334 | type :'vbox', |
| 335 | padding :1, |
| 336 | children : |
| 337 | [ |
| 338 | { |
| 339 | type :'hbox', |
| 340 | widths : [ '50%', '50%' ], |
| 341 | children : |
| 342 | [ |
| 343 | { |
| 344 | type :'text', |
| 345 | id :'id', |
| 346 | label :editor.lang.common.id, |
| 347 | 'default' : '' |
| 348 | }, |
| 349 | { |
| 350 | type :'text', |
| 351 | id :'lang', |
| 352 | label :editor.lang.link.langCode, |
| 353 | 'default' : '' |
| 354 | } |
| 355 | ] |
| 356 | }, |
| 357 | { |
| 358 | type :'hbox', |
| 359 | children : |
| 360 | [ |
| 361 | { |
| 362 | type :'text', |
| 363 | id :'style', |
| 364 | style :'width: 100%;', |
| 365 | label :editor.lang.common.cssStyle, |
| 366 | 'default' : '' |
| 367 | } |
| 368 | ] |
| 369 | }, |
| 370 | { |
| 371 | type :'hbox', |
| 372 | children : |
| 373 | [ |
| 374 | { |
| 375 | type :'text', |
| 376 | id :'title', |
| 377 | style :'width: 100%;', |
| 378 | label :editor.lang.common.advisoryTitle, |
| 379 | 'default' : '' |
| 380 | } |
| 381 | ] |
| 382 | }, |
| 383 | { |
| 384 | type :'select', |
| 385 | id :'dir', |
| 386 | style :'width: 100%;', |
| 387 | label :editor.lang.common.langDir, |
| 388 | 'default' : '', |
| 389 | items : |
| 390 | [ |
| 391 | [ |
| 392 | editor.lang.common.langDirLtr, |
| 393 | 'ltr' |
| 394 | ], |
| 395 | [ |
| 396 | editor.lang.common.langDirRtl, |
| 397 | 'rtl' |
| 398 | ] |
| 399 | ] |
| 400 | } |
| 401 | ] |
| 402 | } |
| 403 | ] |
| 404 | } |
| 405 | ], |
| 406 | onLoad : function() |
| 407 | { |
| 408 | setupFields.call(this); |
| 409 | }, |
| 410 | onShow : function() |
| 411 | { |
| 412 | // Whether always create new container regardless of existed |
| 413 | // ones. |
| 414 | if ( command == 'editdiv' ) |
| 415 | { |
| 416 | // Try to discover the containers that already existed in |
| 417 | // ranges |
| 418 | var div = getDiv( editor ); |
| 419 | // update dialog field values |
| 420 | div && this.setupContent( this._element = div ); |
| 421 | } |
| 422 | }, |
| 423 | onOk : function() |
| 424 | { |
| 425 | if( command == 'editdiv' ) |
| 426 | containers = [ this._element ]; |
| 427 | else |
| 428 | containers = createDiv( editor, true ); |
| 429 | |
| 430 | // Update elements attributes |
| 431 | for( var i = 0 ; i < containers.length ; i++ ) |
| 432 | this.commitContent( containers[ i ] ); |
| 433 | this.hide(); |
| 434 | } |
| 435 | }; |
| 436 | } |
| 437 | |
| 438 | CKEDITOR.dialog.add( 'creatediv', function( editor ) |
| 439 | { |
| 440 | return divDialog( editor, 'creatediv' ); |
| 441 | } ); |
| 442 | CKEDITOR.dialog.add( 'editdiv', function( editor ) |
| 443 | { |
| 444 | return divDialog( editor, 'editdiv' ); |
| 445 | } ); |
| 446 | } )(); |
| 447 | |
| 448 | /* |
| 449 | * @name CKEDITOR.config.div_wrapTable |
| 450 | * Whether to wrap the whole table instead of indivisual cells when created 'div' in table cell. |
| 451 | * @type Boolean |
| 452 | * @default false |
| 453 | * @example config.div_wrapTable = true; |
| 454 | */ |