| | 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 | // This could introduce some duplicated elements in array. |
| | 198 | blockGroups[ i ][ j ] = currentNode; |
| | 199 | } |
| | 200 | |
| | 201 | // Wrapped blocks counting |
| | 202 | var fixedBlock = null; |
| | 203 | for ( var j = 0 ; j < blockGroups[ i ].length ; j++ ) |
| | 204 | { |
| | 205 | currentNode = blockGroups[ i ][ j ]; |
| | 206 | |
| | 207 | // Avoid DUP elements introduced by grouping. |
| | 208 | if ( !( currentNode.getCustomData && currentNode.getCustomData( 'block_processed' ) ) ) |
| | 209 | { |
| | 210 | currentNode.is && CKEDITOR.dom.element.setMarker( database, currentNode, 'block_processed', true ); |
| | 211 | |
| | 212 | // Establish new container, wrapping all elements in this group. |
| | 213 | if ( j == 0 ) |
| | 214 | divElement.insertBefore( currentNode ); |
| | 215 | |
| | 216 | divElement.append( currentNode ); |
| | 217 | } |
| | 218 | } |
| | 219 | |
| | 220 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| | 221 | containers.push( divElement ); |
| | 222 | } |
| | 223 | |
| | 224 | selection.selectBookmarks( bookmarks ); |
| | 225 | return containers; |
| | 226 | } |
| | 227 | |
| | 228 | function getDiv( editor ) |
| | 229 | { |
| | 230 | var path = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() ), |
| | 231 | blockLimit = path.blockLimit, |
| | 232 | div = blockLimit && blockLimit.getAscendant( 'div', true ); |
| | 233 | return div; |
| | 234 | } |
| | 235 | /** |
| | 236 | * Divide a set of nodes to different groups by their path's blocklimit element. |
| | 237 | * Note: the specified nodes should be in source order naturally, which mean they are supposed to producea by following class: |
| | 238 | * * CKEDITOR.dom.range.Iterator |
| | 239 | * * CKEDITOR.dom.domWalker |
| | 240 | * @return {Array []} the grouped nodes |
| | 241 | */ |
| | 242 | function groupByDivLimit( nodes ) |
| | 243 | { |
| | 244 | var groups = [], |
| | 245 | lastDivLimit = null, |
| | 246 | path, block; |
| | 247 | for ( var i = 0 ; i < nodes.length ; i++ ) |
| | 248 | { |
| | 249 | block = nodes[i]; |
| | 250 | var limit = getDivLimitElement( block ); |
| | 251 | if ( !limit.equals( lastDivLimit ) ) |
| | 252 | { |
| | 253 | lastDivLimit = limit ; |
| | 254 | groups.push( [] ) ; |
| | 255 | } |
| | 256 | groups[ groups.length - 1 ].push( block ) ; |
| | 257 | } |
| | 258 | return groups; |
| | 259 | } |
| | 260 | |
| | 261 | /** |
| | 262 | * Hold a collection of created block container elements. |
| | 263 | */ |
| | 264 | var containers = []; |
| | 265 | /** |
| | 266 | * @type divDialog |
| | 267 | */ |
| | 268 | return { |
| | 269 | title : editor.lang.div.title, |
| | 270 | minWidth : 400, |
| | 271 | minHeight : 165, |
| | 272 | contents : |
| | 273 | [ |
| | 274 | { |
| | 275 | id :'info', |
| | 276 | label :editor.lang.common.generalTab, |
| | 277 | title :editor.lang.common.generalTab, |
| | 278 | elements : |
| | 279 | [ |
| | 280 | { |
| | 281 | type :'hbox', |
| | 282 | widths : [ '50%', '50%' ], |
| | 283 | children : |
| | 284 | [ |
| | 285 | { |
| | 286 | id :'elementStyle', |
| | 287 | type :'select', |
| | 288 | style :'width: 100%;', |
| | 289 | label :editor.lang.div.styleSelectLabel, |
| | 290 | 'default' : '', |
| | 291 | items : [], |
| | 292 | setup : function( element ) |
| | 293 | { |
| | 294 | this.setValue( element.$.style.cssText || '' ); |
| | 295 | }, |
| | 296 | commit: function( element ) |
| | 297 | { |
| | 298 | if ( this.getValue() ) |
| | 299 | element.$.style.cssText = this.getValue(); |
| | 300 | else |
| | 301 | element.removeAttribute( 'style' ); |
| | 302 | } |
| | 303 | }, |
| | 304 | { |
| | 305 | id :'class', |
| | 306 | type :'text', |
| | 307 | label :editor.lang.common.cssClass, |
| | 308 | 'default' : '' |
| | 309 | } |
| | 310 | ] |
| | 311 | } |
| | 312 | ] |
| | 313 | }, |
| | 314 | { |
| | 315 | id :'advanced', |
| | 316 | label :editor.lang.common.advancedTab, |
| | 317 | title :editor.lang.common.advancedTab, |
| | 318 | elements : |
| | 319 | [ |
| | 320 | { |
| | 321 | type :'vbox', |
| | 322 | padding :1, |
| | 323 | children : |
| | 324 | [ |
| | 325 | { |
| | 326 | type :'hbox', |
| | 327 | widths : [ '50%', '50%' ], |
| | 328 | children : |
| | 329 | [ |
| | 330 | { |
| | 331 | type :'text', |
| | 332 | id :'id', |
| | 333 | label :editor.lang.common.id, |
| | 334 | 'default' : '' |
| | 335 | }, |
| | 336 | { |
| | 337 | type :'text', |
| | 338 | id :'lang', |
| | 339 | label :editor.lang.link.langCode, |
| | 340 | 'default' : '' |
| | 341 | } |
| | 342 | ] |
| | 343 | }, |
| | 344 | { |
| | 345 | type :'hbox', |
| | 346 | children : |
| | 347 | [ |
| | 348 | { |
| | 349 | type :'text', |
| | 350 | id :'style', |
| | 351 | style :'width: 100%;', |
| | 352 | label :editor.lang.common.cssStyle, |
| | 353 | 'default' : '' |
| | 354 | } |
| | 355 | ] |
| | 356 | }, |
| | 357 | { |
| | 358 | type :'hbox', |
| | 359 | children : |
| | 360 | [ |
| | 361 | { |
| | 362 | type :'text', |
| | 363 | id :'title', |
| | 364 | style :'width: 100%;', |
| | 365 | label :editor.lang.common.advisoryTitle, |
| | 366 | 'default' : '' |
| | 367 | } |
| | 368 | ] |
| | 369 | }, |
| | 370 | { |
| | 371 | type :'select', |
| | 372 | id :'dir', |
| | 373 | style :'width: 100%;', |
| | 374 | label :editor.lang.common.langDir, |
| | 375 | 'default' : '', |
| | 376 | items : |
| | 377 | [ |
| | 378 | [ |
| | 379 | editor.lang.common.langDirLtr, |
| | 380 | 'ltr' |
| | 381 | ], |
| | 382 | [ |
| | 383 | editor.lang.common.langDirRtl, |
| | 384 | 'rtl' |
| | 385 | ] |
| | 386 | ] |
| | 387 | } |
| | 388 | ] |
| | 389 | } |
| | 390 | ] |
| | 391 | } |
| | 392 | ], |
| | 393 | onLoad : function() |
| | 394 | { |
| | 395 | setupFields.call(this); |
| | 396 | }, |
| | 397 | onShow : function() |
| | 398 | { |
| | 399 | // Whether always create new container regardless of existed |
| | 400 | // ones. |
| | 401 | if ( command == 'editdiv' ) |
| | 402 | { |
| | 403 | // Try to discover the containers that already existed in |
| | 404 | // ranges |
| | 405 | var div = getDiv( editor ); |
| | 406 | // update dialog field values |
| | 407 | div && this.setupContent( this._element = div ); |
| | 408 | } |
| | 409 | }, |
| | 410 | onOk : function() |
| | 411 | { |
| | 412 | if( command == 'editdiv' ) |
| | 413 | containers = [ this._element ]; |
| | 414 | else |
| | 415 | containers = createDiv( editor, true ); |
| | 416 | |
| | 417 | // Update elements attributes |
| | 418 | for( var i = 0 ; i < containers.length ; i++ ) |
| | 419 | this.commitContent( containers[ i ] ); |
| | 420 | this.hide(); |
| | 421 | } |
| | 422 | }; |
| | 423 | } |
| | 424 | |
| | 425 | CKEDITOR.dialog.add( 'creatediv', function( editor ) |
| | 426 | { |
| | 427 | return divDialog( editor, 'creatediv' ); |
| | 428 | } ); |
| | 429 | CKEDITOR.dialog.add( 'editdiv', function( editor ) |
| | 430 | { |
| | 431 | return divDialog( editor, 'editdiv' ); |
| | 432 | } ); |
| | 433 | } )(); |
| | 434 | |
| | 435 | /* |
| | 436 | * @name CKEDITOR.config.div_wrapTable |
| | 437 | * Whether to wrap the whole table instead of indivisual cells when created 'div' in table cell. |
| | 438 | * @type Boolean |
| | 439 | * @default false |
| | 440 | * @example config.div_wrapTable = true; |
| | 441 | */ |