JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / tabletools / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         var cellNodeRegex = /^(?:td|th)$/;\r
9 \r
10         function getSelectedCells( selection )\r
11         {\r
12                 // Walker will try to split text nodes, which will make the current selection\r
13                 // invalid. So save bookmarks before doing anything.\r
14                 var bookmarks = selection.createBookmarks();\r
15 \r
16                 var ranges = selection.getRanges();\r
17                 var retval = [];\r
18                 var database = {};\r
19 \r
20                 function moveOutOfCellGuard( node )\r
21                 {\r
22                         // Apply to the first cell only.\r
23                         if ( retval.length > 0 )\r
24                                 return;\r
25 \r
26                         // If we are exiting from the first </td>, then the td should definitely be\r
27                         // included.\r
28                         if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )\r
29                                         && !node.getCustomData( 'selected_cell' ) )\r
30                         {\r
31                                 CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );\r
32                                 retval.push( node );\r
33                         }\r
34                 }\r
35 \r
36                 for ( var i = 0 ; i < ranges.length ; i++ )\r
37                 {\r
38                         var range = ranges[ i ];\r
39 \r
40                         if ( range.collapsed )\r
41                         {\r
42                                 // Walker does not handle collapsed ranges yet - fall back to old API.\r
43                                 var startNode = range.getCommonAncestor();\r
44                                 var nearestCell = startNode.getAscendant( 'td', true ) || startNode.getAscendant( 'th', true );\r
45                                 if ( nearestCell )\r
46                                         retval.push( nearestCell );\r
47                         }\r
48                         else\r
49                         {\r
50                                 var walker = new CKEDITOR.dom.walker( range );\r
51                                 var node;\r
52                                 walker.guard = moveOutOfCellGuard;\r
53 \r
54                                 while ( ( node = walker.next() ) )\r
55                                 {\r
56                                         // If may be possible for us to have a range like this:\r
57                                         // <td>^1</td><td>^2</td>\r
58                                         // The 2nd td shouldn't be included.\r
59                                         //\r
60                                         // So we have to take care to include a td we've entered only when we've\r
61                                         // walked into its children.\r
62 \r
63                                         var parent = node.getParent();\r
64                                         if ( parent && cellNodeRegex.test( parent.getName() ) && !parent.getCustomData( 'selected_cell' ) )\r
65                                         {\r
66                                                 CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );\r
67                                                 retval.push( parent );\r
68                                         }\r
69                                 }\r
70                         }\r
71                 }\r
72 \r
73                 CKEDITOR.dom.element.clearAllMarkers( database );\r
74 \r
75                 // Restore selection position.\r
76                 selection.selectBookmarks( bookmarks );\r
77 \r
78                 return retval;\r
79         }\r
80 \r
81         function getFocusElementAfterDelCells( cellsToDelete ) {\r
82                 var i = 0,\r
83                         last = cellsToDelete.length - 1,\r
84                         database = {},\r
85                         cell,focusedCell,\r
86                         tr;\r
87 \r
88                 while ( ( cell = cellsToDelete[ i++ ] ) )\r
89                         CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );\r
90 \r
91                 // 1.first we check left or right side focusable cell row by row;\r
92                 i = 0;\r
93                 while ( ( cell = cellsToDelete[ i++ ] ) )\r
94                 {\r
95                         if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )\r
96                           || ( focusedCell = cell.getNext()     ) && !focusedCell.getCustomData( 'delete_cell' ) )\r
97                         {\r
98                                 CKEDITOR.dom.element.clearAllMarkers( database );\r
99                                 return focusedCell;\r
100                         }\r
101                 }\r
102 \r
103                 CKEDITOR.dom.element.clearAllMarkers( database );\r
104 \r
105                 // 2. then we check the toppest row (outside the selection area square) focusable cell\r
106                 tr = cellsToDelete[ 0 ].getParent();\r
107                 if ( ( tr = tr.getPrevious() ) )\r
108                         return tr.getLast();\r
109 \r
110                 // 3. last we check the lowerest  row focusable cell\r
111                 tr = cellsToDelete[ last ].getParent();\r
112                 if ( ( tr = tr.getNext() ) )\r
113                         return tr.getChild( 0 );\r
114 \r
115                 return null;\r
116         }\r
117 \r
118         function insertRow( selection, insertBefore )\r
119         {\r
120                 var cells = getSelectedCells( selection ),\r
121                                 firstCell = cells[ 0 ],\r
122                                 table = firstCell.getAscendant( 'table' ),\r
123                                 doc = firstCell.getDocument(),\r
124                                 startRow = cells[ 0 ].getParent(),\r
125                                 startRowIndex = startRow.$.rowIndex,\r
126                                 lastCell = cells[ cells.length - 1 ],\r
127                                 endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,\r
128                                 endRow = new CKEDITOR.dom.element( table.$.rows[ endRowIndex ] ),\r
129                                 rowIndex = insertBefore ? startRowIndex : endRowIndex,\r
130                                 row = insertBefore ? startRow : endRow;\r
131 \r
132                 var map = CKEDITOR.tools.buildTableMap( table ),\r
133                                 cloneRow = map[ rowIndex ],\r
134                                 nextRow = insertBefore ? map[ rowIndex - 1 ] : map[ rowIndex + 1 ],\r
135                                 width = map[0].length;\r
136 \r
137                 var newRow = doc.createElement( 'tr' );\r
138                 for ( var i = 0; i < width; i++ )\r
139                 {\r
140                         var cell;\r
141                         // Check whether there's a spanning row here, do not break it.\r
142                         if ( cloneRow[ i ].rowSpan > 1 && nextRow && cloneRow[ i ] == nextRow[ i ] )\r
143                         {\r
144                                 cell = cloneRow[ i ];\r
145                                 cell.rowSpan += 1;\r
146                         }\r
147                         else\r
148                         {\r
149                                 cell = new CKEDITOR.dom.element( cloneRow[ i ] ).clone();\r
150                                 cell.removeAttribute( 'rowSpan' );\r
151                                 !CKEDITOR.env.ie && cell.appendBogus();\r
152                                 newRow.append( cell );\r
153                                 cell = cell.$;\r
154                         }\r
155 \r
156                         i += cell.colSpan - 1;\r
157                 }\r
158 \r
159                 insertBefore ?\r
160                 newRow.insertBefore( row ) :\r
161                 newRow.insertAfter( row );\r
162         }\r
163 \r
164         function deleteRows( selectionOrRow )\r
165         {\r
166                 if ( selectionOrRow instanceof CKEDITOR.dom.selection )\r
167                 {\r
168                         var cells = getSelectedCells( selectionOrRow ),\r
169                                         firstCell = cells[ 0 ],\r
170                                         table = firstCell.getAscendant( 'table' ),\r
171                                         map = CKEDITOR.tools.buildTableMap( table ),\r
172                                         startRow = cells[ 0 ].getParent(),\r
173                                         startRowIndex = startRow.$.rowIndex,\r
174                                         lastCell = cells[ cells.length - 1 ],\r
175                                         endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1,\r
176                                         rowsToDelete = [];\r
177 \r
178                         // Delete cell or reduce cell spans by checking through the table map.\r
179                         for ( var i = startRowIndex; i <= endRowIndex; i++ )\r
180                         {\r
181                                 var mapRow = map[ i ],\r
182                                                 row = new CKEDITOR.dom.element( table.$.rows[ i ] );\r
183 \r
184                                 for ( var j = 0; j < mapRow.length; j++ )\r
185                                 {\r
186                                         var cell = new CKEDITOR.dom.element( mapRow[ j ] ),\r
187                                                         cellRowIndex = cell.getParent().$.rowIndex;\r
188 \r
189                                         if ( cell.$.rowSpan == 1 )\r
190                                                 cell.remove();\r
191                                         // Row spanned cell.\r
192                                         else\r
193                                         {\r
194                                                 // Span row of the cell, reduce spanning.\r
195                                                 cell.$.rowSpan -= 1;\r
196                                                 // Root row of the cell, root cell to next row.\r
197                                                 if ( cellRowIndex == i )\r
198                                                 {\r
199                                                         var nextMapRow = map[ i + 1 ];\r
200                                                         nextMapRow[ j - 1 ] ?\r
201                                                         cell.insertAfter( new CKEDITOR.dom.element( nextMapRow[ j - 1 ] ) )\r
202                                                                         : new CKEDITOR.dom.element( table.$.rows[ i + 1 ] ).append( cell, 1 );\r
203                                                 }\r
204                                         }\r
205 \r
206                                         j += cell.$.colSpan - 1;\r
207                                 }\r
208 \r
209                                 rowsToDelete.push( row );\r
210                         }\r
211 \r
212                         var rows = table.$.rows;\r
213 \r
214                         // Where to put the cursor after rows been deleted?\r
215                         // 1. Into next sibling row if any;\r
216                         // 2. Into previous sibling row if any;\r
217                         // 3. Into table's parent element if it's the very last row.\r
218                         var cursorPosition =  new CKEDITOR.dom.element( rows[ startRowIndex ] || rows[ startRowIndex - 1 ] || table.$.parentNode );\r
219 \r
220                         for ( i = rowsToDelete.length ; i >= 0 ; i-- )\r
221                                 deleteRows( rowsToDelete[ i ] );\r
222 \r
223                         return cursorPosition;\r
224                 }\r
225                 else if ( selectionOrRow instanceof CKEDITOR.dom.element )\r
226                 {\r
227                         table = selectionOrRow.getAscendant( 'table' );\r
228 \r
229                         if ( table.$.rows.length == 1 )\r
230                                 table.remove();\r
231                         else\r
232                                 selectionOrRow.remove();\r
233                 }\r
234 \r
235                 return null;\r
236         }\r
237 \r
238         function getCellColIndex( cell, isStart )\r
239         {\r
240                 var row = cell.getParent(),\r
241                         rowCells = row.$.cells;\r
242 \r
243                 var colIndex = 0;\r
244                 for ( var i = 0; i < rowCells.length; i++ )\r
245                 {\r
246                         var mapCell = rowCells[ i ];\r
247                         colIndex += isStart ? 1 : mapCell.colSpan;\r
248                         if ( mapCell == cell.$ )\r
249                                 break;\r
250                 }\r
251 \r
252                 return colIndex -1;\r
253         }\r
254 \r
255         function getColumnsIndices( cells, isStart )\r
256         {\r
257                 var retval = isStart ? Infinity : 0;\r
258                 for ( var i = 0; i < cells.length; i++ )\r
259                 {\r
260                         var colIndex = getCellColIndex( cells[ i ], isStart );\r
261                         if ( isStart ? colIndex < retval  : colIndex > retval )\r
262                                 retval = colIndex;\r
263                 }\r
264                 return retval;\r
265         }\r
266 \r
267         function insertColumn( selection, insertBefore )\r
268         {\r
269                 var cells = getSelectedCells( selection ),\r
270                         firstCell = cells[ 0 ],\r
271                         table = firstCell.getAscendant( 'table' ),\r
272                         startCol =  getColumnsIndices( cells, 1 ),\r
273                         lastCol =  getColumnsIndices( cells ),\r
274                         colIndex = insertBefore? startCol : lastCol;\r
275 \r
276                 var map = CKEDITOR.tools.buildTableMap( table ),\r
277                         cloneCol = [],\r
278                         nextCol = [],\r
279                         height = map.length;\r
280 \r
281                 for ( var i = 0; i < height; i++ )\r
282                 {\r
283                         cloneCol.push( map[ i ][ colIndex ] );\r
284                         var nextCell = insertBefore ? map[ i ][ colIndex - 1 ] : map[ i ][ colIndex + 1 ];\r
285                         nextCell && nextCol.push( nextCell );\r
286                 }\r
287 \r
288                 for ( i = 0; i < height; i++ )\r
289                 {\r
290                         var cell;\r
291                         // Check whether there's a spanning column here, do not break it.\r
292                         if ( cloneCol[ i ].colSpan > 1\r
293                                 && nextCol.length\r
294                                 && nextCol[ i ] == cloneCol[ i ] )\r
295                         {\r
296                                 cell = cloneCol[ i ];\r
297                                 cell.colSpan += 1;\r
298                         }\r
299                         else\r
300                         {\r
301                                 cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone();\r
302                                 cell.removeAttribute( 'colSpan' );\r
303                                 !CKEDITOR.env.ie && cell.appendBogus();\r
304                                 cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) );\r
305                                 cell = cell.$;\r
306                         }\r
307 \r
308                         i += cell.rowSpan - 1;\r
309                 }\r
310         }\r
311 \r
312         function deleteColumns( selectionOrCell )\r
313         {\r
314                 var cells = getSelectedCells( selectionOrCell ),\r
315                                 firstCell = cells[ 0 ],\r
316                                 lastCell = cells[ cells.length - 1 ],\r
317                                 table = firstCell.getAscendant( 'table' ),\r
318                                 map = CKEDITOR.tools.buildTableMap( table ),\r
319                                 startColIndex,\r
320                                 endColIndex,\r
321                                 rowsToDelete = [];\r
322 \r
323                 // Figure out selected cells' column indices.\r
324                 for ( var i = 0, rows = map.length; i < rows; i++ )\r
325                 {\r
326                         for ( var j = 0, cols = map[ i ].length; j < cols; j++ )\r
327                         {\r
328                                 if ( map[ i ][ j ] == firstCell.$ )\r
329                                         startColIndex = j;\r
330                                 if ( map[ i ][ j ] == lastCell.$ )\r
331                                         endColIndex = j;\r
332                         }\r
333                 }\r
334 \r
335                 // Delete cell or reduce cell spans by checking through the table map.\r
336                 for ( i = startColIndex; i <= endColIndex; i++ )\r
337                 {\r
338                         for ( j = 0; j < map.length; j++ )\r
339                         {\r
340                                 var mapRow = map[ j ],\r
341                                         row = new CKEDITOR.dom.element( table.$.rows[ j ] ),\r
342                                         cell = new CKEDITOR.dom.element( mapRow[ i ] );\r
343 \r
344                                 if ( cell.$.colSpan == 1 )\r
345                                         cell.remove();\r
346                                 // Reduce the col spans.\r
347                                 else\r
348                                         cell.$.colSpan -= 1;\r
349 \r
350                                 j += cell.$.rowSpan - 1;\r
351 \r
352                                 if ( !row.$.cells.length )\r
353                                         rowsToDelete.push( row );\r
354                         }\r
355                 }\r
356 \r
357                 var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells;\r
358 \r
359                 // Where to put the cursor after columns been deleted?\r
360                 // 1. Into next cell of the first row if any;\r
361                 // 2. Into previous cell of the first row if any;\r
362                 // 3. Into table's parent element;\r
363                 var cursorPosition =  new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) );\r
364 \r
365                 // Delete table rows only if all columns are gone (do not remove empty row).\r
366                 if ( rowsToDelete.length == rows )\r
367                         table.remove();\r
368 \r
369                 return cursorPosition;\r
370         }\r
371 \r
372         function getFocusElementAfterDelCols( cells )\r
373         {\r
374                 var cellIndexList = [],\r
375                         table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),\r
376                         i, length,\r
377                         targetIndex, targetCell;\r
378 \r
379                 // get the cellIndex list of delete cells\r
380                 for ( i = 0, length = cells.length; i < length; i++ )\r
381                         cellIndexList.push( cells[i].$.cellIndex );\r
382 \r
383                 // get the focusable column index\r
384                 cellIndexList.sort();\r
385                 for ( i = 1, length = cellIndexList.length; i < length; i++ )\r
386                 {\r
387                         if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )\r
388                         {\r
389                                 targetIndex = cellIndexList[ i - 1 ] + 1;\r
390                                 break;\r
391                         }\r
392                 }\r
393 \r
394                 if ( !targetIndex )\r
395                         targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )\r
396                                                         : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );\r
397 \r
398                 // scan row by row to get the target cell\r
399                 var rows = table.$.rows;\r
400                 for ( i = 0, length = rows.length; i < length ; i++ )\r
401                 {\r
402                         targetCell = rows[ i ].cells[ targetIndex ];\r
403                         if ( targetCell )\r
404                                 break;\r
405                 }\r
406 \r
407                 return targetCell ?  new CKEDITOR.dom.element( targetCell ) :  table.getPrevious();\r
408         }\r
409 \r
410         function insertCell( selection, insertBefore )\r
411         {\r
412                 var startElement = selection.getStartElement();\r
413                 var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );\r
414 \r
415                 if ( !cell )\r
416                         return;\r
417 \r
418                 // Create the new cell element to be added.\r
419                 var newCell = cell.clone();\r
420                 if ( !CKEDITOR.env.ie )\r
421                         newCell.appendBogus();\r
422 \r
423                 if ( insertBefore )\r
424                         newCell.insertBefore( cell );\r
425                 else\r
426                         newCell.insertAfter( cell );\r
427         }\r
428 \r
429         function deleteCells( selectionOrCell )\r
430         {\r
431                 if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
432                 {\r
433                         var cellsToDelete = getSelectedCells( selectionOrCell );\r
434                         var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
435                         var cellToFocus   = getFocusElementAfterDelCells( cellsToDelete );\r
436 \r
437                         for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
438                                 deleteCells( cellsToDelete[ i ] );\r
439 \r
440                         if ( cellToFocus )\r
441                                 placeCursorInCell( cellToFocus, true );\r
442                         else if ( table )\r
443                                 table.remove();\r
444                 }\r
445                 else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
446                 {\r
447                         var tr = selectionOrCell.getParent();\r
448                         if ( tr.getChildCount() == 1 )\r
449                                 tr.remove();\r
450                         else\r
451                                 selectionOrCell.remove();\r
452                 }\r
453         }\r
454 \r
455         // Remove filler at end and empty spaces around the cell content.\r
456         function trimCell( cell )\r
457         {\r
458                 var bogus = cell.getBogus();\r
459                 bogus && bogus.remove();\r
460                 cell.trim();\r
461         }\r
462 \r
463         function placeCursorInCell( cell, placeAtEnd )\r
464         {\r
465                 var range = new CKEDITOR.dom.range( cell.getDocument() );\r
466                 if ( !range[ 'moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' ) ]( cell ) )\r
467                 {\r
468                         range.selectNodeContents( cell );\r
469                         range.collapse( placeAtEnd ? false : true );\r
470                 }\r
471                 range.select( true );\r
472         }\r
473 \r
474         function cellInRow( tableMap, rowIndex, cell )\r
475         {\r
476                 var oRow = tableMap[ rowIndex ];\r
477                 if ( typeof cell == 'undefined' )\r
478                         return oRow;\r
479 \r
480                 for ( var c = 0 ; oRow && c < oRow.length ; c++ )\r
481                 {\r
482                         if ( cell.is && oRow[c] == cell.$ )\r
483                                 return c;\r
484                         else if ( c == cell )\r
485                                 return new CKEDITOR.dom.element( oRow[ c ] );\r
486                 }\r
487                 return cell.is ? -1 : null;\r
488         }\r
489 \r
490         function cellInCol( tableMap, colIndex, cell )\r
491         {\r
492                 var oCol = [];\r
493                 for ( var r = 0; r < tableMap.length; r++ )\r
494                 {\r
495                         var row = tableMap[ r ];\r
496                         if ( typeof cell == 'undefined' )\r
497                                 oCol.push( row[ colIndex ] );\r
498                         else if ( cell.is && row[ colIndex ] == cell.$ )\r
499                                 return r;\r
500                         else if ( r == cell )\r
501                                 return new CKEDITOR.dom.element( row[ colIndex ] );\r
502                 }\r
503 \r
504                 return ( typeof cell == 'undefined' )? oCol : cell.is ? -1 :  null;\r
505         }\r
506 \r
507         function mergeCells( selection, mergeDirection, isDetect )\r
508         {\r
509                 var cells = getSelectedCells( selection );\r
510 \r
511                 // Invalid merge request if:\r
512                 // 1. In batch mode despite that less than two selected.\r
513                 // 2. In solo mode while not exactly only one selected.\r
514                 // 3. Cells distributed in different table groups (e.g. from both thead and tbody).\r
515                 var commonAncestor;\r
516                 if ( ( mergeDirection ? cells.length != 1 : cells.length < 2 )\r
517                                 || ( commonAncestor = selection.getCommonAncestor() )\r
518                                 && commonAncestor.type == CKEDITOR.NODE_ELEMENT\r
519                                 && commonAncestor.is( 'table' ) )\r
520                 {\r
521                         return false;\r
522                 }\r
523 \r
524                 var     cell,\r
525                         firstCell = cells[ 0 ],\r
526                         table = firstCell.getAscendant( 'table' ),\r
527                         map = CKEDITOR.tools.buildTableMap( table ),\r
528                         mapHeight = map.length,\r
529                         mapWidth = map[ 0 ].length,\r
530                         startRow = firstCell.getParent().$.rowIndex,\r
531                         startColumn = cellInRow( map, startRow, firstCell );\r
532 \r
533                 if ( mergeDirection )\r
534                 {\r
535                         var targetCell;\r
536                         try\r
537                         {\r
538                                 var rowspan = parseInt( firstCell.getAttribute( 'rowspan' ), 10 ) || 1;\r
539                                 var colspan = parseInt( firstCell.getAttribute( 'colspan' ), 10 ) || 1;\r
540 \r
541                                 targetCell =\r
542                                         map[ mergeDirection == 'up' ?\r
543                                                         ( startRow - rowspan ):\r
544                                                         mergeDirection == 'down' ? ( startRow + rowspan ) : startRow  ] [\r
545                                                 mergeDirection == 'left' ?\r
546                                                         ( startColumn - colspan ):\r
547                                                 mergeDirection == 'right' ?  ( startColumn + colspan ) : startColumn ];\r
548 \r
549                         }\r
550                         catch( er )\r
551                         {\r
552                                 return false;\r
553                         }\r
554 \r
555                         // 1. No cell could be merged.\r
556                         // 2. Same cell actually.\r
557                         if ( !targetCell || firstCell.$ == targetCell  )\r
558                                 return false;\r
559 \r
560                         // Sort in map order regardless of the DOM sequence.\r
561                         cells[ ( mergeDirection == 'up' || mergeDirection == 'left' ) ?\r
562                                  'unshift' : 'push' ]( new CKEDITOR.dom.element( targetCell ) );\r
563                 }\r
564 \r
565                 // Start from here are merging way ignorance (merge up/right, batch merge).\r
566                 var     doc = firstCell.getDocument(),\r
567                         lastRowIndex = startRow,\r
568                         totalRowSpan = 0,\r
569                         totalColSpan = 0,\r
570                         // Use a documentFragment as buffer when appending cell contents.\r
571                         frag = !isDetect && new CKEDITOR.dom.documentFragment( doc ),\r
572                         dimension = 0;\r
573 \r
574                 for ( var i = 0; i < cells.length; i++ )\r
575                 {\r
576                         cell = cells[ i ];\r
577 \r
578                         var tr = cell.getParent(),\r
579                                 cellFirstChild = cell.getFirst(),\r
580                                 colSpan = cell.$.colSpan,\r
581                                 rowSpan = cell.$.rowSpan,\r
582                                 rowIndex = tr.$.rowIndex,\r
583                                 colIndex = cellInRow( map, rowIndex, cell );\r
584 \r
585                         // Accumulated the actual places taken by all selected cells.\r
586                         dimension += colSpan * rowSpan;\r
587                         // Accumulated the maximum virtual spans from column and row.\r
588                         totalColSpan = Math.max( totalColSpan, colIndex - startColumn + colSpan ) ;\r
589                         totalRowSpan = Math.max( totalRowSpan, rowIndex - startRow + rowSpan );\r
590 \r
591                         if ( !isDetect )\r
592                         {\r
593                                 // Trim all cell fillers and check to remove empty cells.\r
594                                 if ( trimCell( cell ), cell.getChildren().count() )\r
595                                 {\r
596                                         // Merge vertically cells as two separated paragraphs.\r
597                                         if ( rowIndex != lastRowIndex\r
598                                                 && cellFirstChild\r
599                                                 && !( cellFirstChild.isBlockBoundary\r
600                                                           && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )\r
601                                         {\r
602                                                 var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
603                                                 if ( last && !( last.is && last.is( 'br' ) ) )\r
604                                                         frag.append( 'br' );\r
605                                         }\r
606 \r
607                                         cell.moveChildren( frag );\r
608                                 }\r
609                                 i ? cell.remove() : cell.setHtml( '' );\r
610                         }\r
611                         lastRowIndex = rowIndex;\r
612                 }\r
613 \r
614                 if ( !isDetect )\r
615                 {\r
616                         frag.moveChildren( firstCell );\r
617 \r
618                         if ( !CKEDITOR.env.ie )\r
619                                 firstCell.appendBogus();\r
620 \r
621                         if ( totalColSpan >= mapWidth )\r
622                                 firstCell.removeAttribute( 'rowSpan' );\r
623                         else\r
624                                 firstCell.$.rowSpan = totalRowSpan;\r
625 \r
626                         if ( totalRowSpan >= mapHeight )\r
627                                 firstCell.removeAttribute( 'colSpan' );\r
628                         else\r
629                                 firstCell.$.colSpan = totalColSpan;\r
630 \r
631                         // Swip empty <tr> left at the end of table due to the merging.\r
632                         var trs = new CKEDITOR.dom.nodeList( table.$.rows ),\r
633                                 count = trs.count();\r
634 \r
635                         for ( i = count - 1; i >= 0; i-- )\r
636                         {\r
637                                 var tailTr = trs.getItem( i );\r
638                                 if ( !tailTr.$.cells.length )\r
639                                 {\r
640                                         tailTr.remove();\r
641                                         count++;\r
642                                         continue;\r
643                                 }\r
644                         }\r
645 \r
646                         return firstCell;\r
647                 }\r
648                 // Be able to merge cells only if actual dimension of selected\r
649                 // cells equals to the caculated rectangle.\r
650                 else\r
651                         return ( totalRowSpan * totalColSpan ) == dimension;\r
652         }\r
653 \r
654         function verticalSplitCell ( selection, isDetect )\r
655         {\r
656                 var cells = getSelectedCells( selection );\r
657                 if ( cells.length > 1 )\r
658                         return false;\r
659                 else if ( isDetect )\r
660                         return true;\r
661 \r
662                 var cell = cells[ 0 ],\r
663                         tr = cell.getParent(),\r
664                         table = tr.getAscendant( 'table' ),\r
665                         map = CKEDITOR.tools.buildTableMap( table ),\r
666                         rowIndex = tr.$.rowIndex,\r
667                         colIndex = cellInRow( map, rowIndex, cell ),\r
668                         rowSpan = cell.$.rowSpan,\r
669                         newCell,\r
670                         newRowSpan,\r
671                         newCellRowSpan,\r
672                         newRowIndex;\r
673 \r
674                 if ( rowSpan > 1 )\r
675                 {\r
676                         newRowSpan = Math.ceil( rowSpan / 2 );\r
677                         newCellRowSpan = Math.floor( rowSpan / 2 );\r
678                         newRowIndex = rowIndex + newRowSpan;\r
679                         var newCellTr = new CKEDITOR.dom.element( table.$.rows[ newRowIndex ] ),\r
680                                 newCellRow = cellInRow( map, newRowIndex ),\r
681                                 candidateCell;\r
682 \r
683                         newCell = cell.clone();\r
684 \r
685                         // Figure out where to insert the new cell by checking the vitual row.\r
686                         for ( var c = 0; c < newCellRow.length; c++ )\r
687                         {\r
688                                 candidateCell = newCellRow[ c ];\r
689                                 // Catch first cell actually following the column.\r
690                                 if ( candidateCell.parentNode == newCellTr.$\r
691                                         && c > colIndex )\r
692                                 {\r
693                                         newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );\r
694                                         break;\r
695                                 }\r
696                                 else\r
697                                         candidateCell = null;\r
698                         }\r
699 \r
700                         // The destination row is empty, append at will.\r
701                         if ( !candidateCell )\r
702                                 newCellTr.append( newCell, true );\r
703                 }\r
704                 else\r
705                 {\r
706                         newCellRowSpan = newRowSpan = 1;\r
707 \r
708                         newCellTr = tr.clone();\r
709                         newCellTr.insertAfter( tr );\r
710                         newCellTr.append( newCell = cell.clone() );\r
711 \r
712                         var cellsInSameRow = cellInRow( map, rowIndex );\r
713                         for ( var i = 0; i < cellsInSameRow.length; i++ )\r
714                                 cellsInSameRow[ i ].rowSpan++;\r
715                 }\r
716 \r
717                 if ( !CKEDITOR.env.ie )\r
718                         newCell.appendBogus();\r
719 \r
720                 cell.$.rowSpan = newRowSpan;\r
721                 newCell.$.rowSpan = newCellRowSpan;\r
722                 if ( newRowSpan == 1 )\r
723                         cell.removeAttribute( 'rowSpan' );\r
724                 if ( newCellRowSpan == 1 )\r
725                         newCell.removeAttribute( 'rowSpan' );\r
726 \r
727                 return newCell;\r
728         }\r
729 \r
730         function horizontalSplitCell( selection, isDetect )\r
731         {\r
732                 var cells = getSelectedCells( selection );\r
733                 if ( cells.length > 1 )\r
734                         return false;\r
735                 else if ( isDetect )\r
736                         return true;\r
737 \r
738                 var cell = cells[ 0 ],\r
739                         tr = cell.getParent(),\r
740                         table = tr.getAscendant( 'table' ),\r
741                         map = CKEDITOR.tools.buildTableMap( table ),\r
742                         rowIndex = tr.$.rowIndex,\r
743                         colIndex = cellInRow( map, rowIndex, cell ),\r
744                         colSpan = cell.$.colSpan,\r
745                         newCell,\r
746                         newColSpan,\r
747                         newCellColSpan;\r
748 \r
749                 if ( colSpan > 1 )\r
750                 {\r
751                         newColSpan = Math.ceil( colSpan / 2 );\r
752                         newCellColSpan = Math.floor( colSpan / 2 );\r
753                 }\r
754                 else\r
755                 {\r
756                         newCellColSpan = newColSpan = 1;\r
757                         var cellsInSameCol = cellInCol( map, colIndex );\r
758                         for ( var i = 0; i < cellsInSameCol.length; i++ )\r
759                                 cellsInSameCol[ i ].colSpan++;\r
760                 }\r
761                 newCell = cell.clone();\r
762                 newCell.insertAfter( cell );\r
763                 if ( !CKEDITOR.env.ie )\r
764                         newCell.appendBogus();\r
765 \r
766                 cell.$.colSpan = newColSpan;\r
767                 newCell.$.colSpan = newCellColSpan;\r
768                 if ( newColSpan == 1 )\r
769                         cell.removeAttribute( 'colSpan' );\r
770                 if ( newCellColSpan == 1 )\r
771                         newCell.removeAttribute( 'colSpan' );\r
772 \r
773                 return newCell;\r
774         }\r
775         // Context menu on table caption incorrect (#3834)\r
776         var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };\r
777 \r
778         CKEDITOR.plugins.tabletools =\r
779         {\r
780                 init : function( editor )\r
781                 {\r
782                         var lang = editor.lang.table;\r
783 \r
784                         editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );\r
785                         CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );\r
786 \r
787                         editor.addCommand( 'tableDelete',\r
788                                 {\r
789                                         exec : function( editor )\r
790                                         {\r
791                                                 var selection = editor.getSelection(),\r
792                                                         startElement = selection && selection.getStartElement(),\r
793                                                         table = startElement && startElement.getAscendant( 'table', 1 );\r
794 \r
795                                                 if ( !table )\r
796                                                         return;\r
797 \r
798                                                 // If the table's parent has only one child remove it as well (unless it's the body or a table cell) (#5416, #6289)\r
799                                                 var parent = table.getParent();\r
800                                                 if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )\r
801                                                         table = parent;\r
802 \r
803                                                 var range = new CKEDITOR.dom.range( editor.document );\r
804                                                 range.moveToPosition( table, CKEDITOR.POSITION_BEFORE_START );\r
805                                                 table.remove();\r
806                                                 range.select();\r
807                                         }\r
808                                 } );\r
809 \r
810                         editor.addCommand( 'rowDelete',\r
811                                 {\r
812                                         exec : function( editor )\r
813                                         {\r
814                                                 var selection = editor.getSelection();\r
815                                                 placeCursorInCell( deleteRows( selection ) );\r
816                                         }\r
817                                 } );\r
818 \r
819                         editor.addCommand( 'rowInsertBefore',\r
820                                 {\r
821                                         exec : function( editor )\r
822                                         {\r
823                                                 var selection = editor.getSelection();\r
824                                                 insertRow( selection, true );\r
825                                         }\r
826                                 } );\r
827 \r
828                         editor.addCommand( 'rowInsertAfter',\r
829                                 {\r
830                                         exec : function( editor )\r
831                                         {\r
832                                                 var selection = editor.getSelection();\r
833                                                 insertRow( selection );\r
834                                         }\r
835                                 } );\r
836 \r
837                         editor.addCommand( 'columnDelete',\r
838                                 {\r
839                                         exec : function( editor )\r
840                                         {\r
841                                                 var selection = editor.getSelection();\r
842                                                 var element = deleteColumns( selection );\r
843                                                 element &&  placeCursorInCell( element, true );\r
844                                         }\r
845                                 } );\r
846 \r
847                         editor.addCommand( 'columnInsertBefore',\r
848                                 {\r
849                                         exec : function( editor )\r
850                                         {\r
851                                                 var selection = editor.getSelection();\r
852                                                 insertColumn( selection, true );\r
853                                         }\r
854                                 } );\r
855 \r
856                         editor.addCommand( 'columnInsertAfter',\r
857                                 {\r
858                                         exec : function( editor )\r
859                                         {\r
860                                                 var selection = editor.getSelection();\r
861                                                 insertColumn( selection );\r
862                                         }\r
863                                 } );\r
864 \r
865                         editor.addCommand( 'cellDelete',\r
866                                 {\r
867                                         exec : function( editor )\r
868                                         {\r
869                                                 var selection = editor.getSelection();\r
870                                                 deleteCells( selection );\r
871                                         }\r
872                                 } );\r
873 \r
874                         editor.addCommand( 'cellMerge',\r
875                                 {\r
876                                         exec : function( editor )\r
877                                         {\r
878                                                 placeCursorInCell( mergeCells( editor.getSelection() ), true );\r
879                                         }\r
880                                 } );\r
881 \r
882                         editor.addCommand( 'cellMergeRight',\r
883                                 {\r
884                                         exec : function( editor )\r
885                                         {\r
886                                                 placeCursorInCell( mergeCells( editor.getSelection(), 'right' ), true );\r
887                                         }\r
888                                 } );\r
889 \r
890                         editor.addCommand( 'cellMergeDown',\r
891                                 {\r
892                                         exec : function( editor )\r
893                                         {\r
894                                                 placeCursorInCell( mergeCells( editor.getSelection(), 'down' ), true );\r
895                                         }\r
896                                 } );\r
897 \r
898                         editor.addCommand( 'cellVerticalSplit',\r
899                                 {\r
900                                         exec : function( editor )\r
901                                         {\r
902                                                 placeCursorInCell( verticalSplitCell( editor.getSelection() ) );\r
903                                         }\r
904                                 } );\r
905 \r
906                         editor.addCommand( 'cellHorizontalSplit',\r
907                                 {\r
908                                         exec : function( editor )\r
909                                         {\r
910                                                 placeCursorInCell( horizontalSplitCell( editor.getSelection() ) );\r
911                                         }\r
912                                 } );\r
913 \r
914                         editor.addCommand( 'cellInsertBefore',\r
915                                 {\r
916                                         exec : function( editor )\r
917                                         {\r
918                                                 var selection = editor.getSelection();\r
919                                                 insertCell( selection, true );\r
920                                         }\r
921                                 } );\r
922 \r
923                         editor.addCommand( 'cellInsertAfter',\r
924                                 {\r
925                                         exec : function( editor )\r
926                                         {\r
927                                                 var selection = editor.getSelection();\r
928                                                 insertCell( selection );\r
929                                         }\r
930                                 } );\r
931 \r
932                         // If the "menu" plugin is loaded, register the menu items.\r
933                         if ( editor.addMenuItems )\r
934                         {\r
935                                 editor.addMenuItems(\r
936                                         {\r
937                                                 tablecell :\r
938                                                 {\r
939                                                         label : lang.cell.menu,\r
940                                                         group : 'tablecell',\r
941                                                         order : 1,\r
942                                                         getItems : function()\r
943                                                         {\r
944                                                                 var selection = editor.getSelection(),\r
945                                                                         cells = getSelectedCells( selection );\r
946                                                                 return {\r
947                                                                         tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,\r
948                                                                         tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,\r
949                                                                         tablecell_delete : CKEDITOR.TRISTATE_OFF,\r
950                                                                         tablecell_merge : mergeCells( selection, null, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
951                                                                         tablecell_merge_right : mergeCells( selection, 'right', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
952                                                                         tablecell_merge_down : mergeCells( selection, 'down', true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
953                                                                         tablecell_split_vertical : verticalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
954                                                                         tablecell_split_horizontal : horizontalSplitCell( selection, true ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
955                                                                         tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
956                                                                 };\r
957                                                         }\r
958                                                 },\r
959 \r
960                                                 tablecell_insertBefore :\r
961                                                 {\r
962                                                         label : lang.cell.insertBefore,\r
963                                                         group : 'tablecell',\r
964                                                         command : 'cellInsertBefore',\r
965                                                         order : 5\r
966                                                 },\r
967 \r
968                                                 tablecell_insertAfter :\r
969                                                 {\r
970                                                         label : lang.cell.insertAfter,\r
971                                                         group : 'tablecell',\r
972                                                         command : 'cellInsertAfter',\r
973                                                         order : 10\r
974                                                 },\r
975 \r
976                                                 tablecell_delete :\r
977                                                 {\r
978                                                         label : lang.cell.deleteCell,\r
979                                                         group : 'tablecell',\r
980                                                         command : 'cellDelete',\r
981                                                         order : 15\r
982                                                 },\r
983 \r
984                                                 tablecell_merge :\r
985                                                 {\r
986                                                         label : lang.cell.merge,\r
987                                                         group : 'tablecell',\r
988                                                         command : 'cellMerge',\r
989                                                         order : 16\r
990                                                 },\r
991 \r
992                                                 tablecell_merge_right :\r
993                                                 {\r
994                                                         label : lang.cell.mergeRight,\r
995                                                         group : 'tablecell',\r
996                                                         command : 'cellMergeRight',\r
997                                                         order : 17\r
998                                                 },\r
999 \r
1000                                                 tablecell_merge_down :\r
1001                                                 {\r
1002                                                         label : lang.cell.mergeDown,\r
1003                                                         group : 'tablecell',\r
1004                                                         command : 'cellMergeDown',\r
1005                                                         order : 18\r
1006                                                 },\r
1007 \r
1008                                                 tablecell_split_horizontal :\r
1009                                                 {\r
1010                                                         label : lang.cell.splitHorizontal,\r
1011                                                         group : 'tablecell',\r
1012                                                         command : 'cellHorizontalSplit',\r
1013                                                         order : 19\r
1014                                                 },\r
1015 \r
1016                                                 tablecell_split_vertical :\r
1017                                                 {\r
1018                                                         label : lang.cell.splitVertical,\r
1019                                                         group : 'tablecell',\r
1020                                                         command : 'cellVerticalSplit',\r
1021                                                         order : 20\r
1022                                                 },\r
1023 \r
1024                                                 tablecell_properties :\r
1025                                                 {\r
1026                                                         label : lang.cell.title,\r
1027                                                         group : 'tablecellproperties',\r
1028                                                         command : 'cellProperties',\r
1029                                                         order : 21\r
1030                                                 },\r
1031 \r
1032                                                 tablerow :\r
1033                                                 {\r
1034                                                         label : lang.row.menu,\r
1035                                                         group : 'tablerow',\r
1036                                                         order : 1,\r
1037                                                         getItems : function()\r
1038                                                         {\r
1039                                                                 return {\r
1040                                                                         tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,\r
1041                                                                         tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,\r
1042                                                                         tablerow_delete : CKEDITOR.TRISTATE_OFF\r
1043                                                                 };\r
1044                                                         }\r
1045                                                 },\r
1046 \r
1047                                                 tablerow_insertBefore :\r
1048                                                 {\r
1049                                                         label : lang.row.insertBefore,\r
1050                                                         group : 'tablerow',\r
1051                                                         command : 'rowInsertBefore',\r
1052                                                         order : 5\r
1053                                                 },\r
1054 \r
1055                                                 tablerow_insertAfter :\r
1056                                                 {\r
1057                                                         label : lang.row.insertAfter,\r
1058                                                         group : 'tablerow',\r
1059                                                         command : 'rowInsertAfter',\r
1060                                                         order : 10\r
1061                                                 },\r
1062 \r
1063                                                 tablerow_delete :\r
1064                                                 {\r
1065                                                         label : lang.row.deleteRow,\r
1066                                                         group : 'tablerow',\r
1067                                                         command : 'rowDelete',\r
1068                                                         order : 15\r
1069                                                 },\r
1070 \r
1071                                                 tablecolumn :\r
1072                                                 {\r
1073                                                         label : lang.column.menu,\r
1074                                                         group : 'tablecolumn',\r
1075                                                         order : 1,\r
1076                                                         getItems : function()\r
1077                                                         {\r
1078                                                                 return {\r
1079                                                                         tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,\r
1080                                                                         tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,\r
1081                                                                         tablecolumn_delete : CKEDITOR.TRISTATE_OFF\r
1082                                                                 };\r
1083                                                         }\r
1084                                                 },\r
1085 \r
1086                                                 tablecolumn_insertBefore :\r
1087                                                 {\r
1088                                                         label : lang.column.insertBefore,\r
1089                                                         group : 'tablecolumn',\r
1090                                                         command : 'columnInsertBefore',\r
1091                                                         order : 5\r
1092                                                 },\r
1093 \r
1094                                                 tablecolumn_insertAfter :\r
1095                                                 {\r
1096                                                         label : lang.column.insertAfter,\r
1097                                                         group : 'tablecolumn',\r
1098                                                         command : 'columnInsertAfter',\r
1099                                                         order : 10\r
1100                                                 },\r
1101 \r
1102                                                 tablecolumn_delete :\r
1103                                                 {\r
1104                                                         label : lang.column.deleteColumn,\r
1105                                                         group : 'tablecolumn',\r
1106                                                         command : 'columnDelete',\r
1107                                                         order : 15\r
1108                                                 }\r
1109                                         });\r
1110                         }\r
1111 \r
1112                         // If the "contextmenu" plugin is laoded, register the listeners.\r
1113                         if ( editor.contextMenu )\r
1114                         {\r
1115                                 editor.contextMenu.addListener( function( element, selection )\r
1116                                         {\r
1117                                                 if ( !element || element.isReadOnly() )\r
1118                                                         return null;\r
1119 \r
1120                                                 while ( element )\r
1121                                                 {\r
1122                                                         if ( element.getName() in contextMenuTags )\r
1123                                                         {\r
1124                                                                 return {\r
1125                                                                         tablecell : CKEDITOR.TRISTATE_OFF,\r
1126                                                                         tablerow : CKEDITOR.TRISTATE_OFF,\r
1127                                                                         tablecolumn : CKEDITOR.TRISTATE_OFF\r
1128                                                                 };\r
1129                                                         }\r
1130                                                         element = element.getParent();\r
1131                                                 }\r
1132 \r
1133                                                 return null;\r
1134                                         } );\r
1135                         }\r
1136                 },\r
1137 \r
1138                 getSelectedCells : getSelectedCells\r
1139 \r
1140         };\r
1141         CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );\r
1142 })();\r
1143 \r
1144 /**\r
1145  * Create a two-dimension array that reflects the actual layout of table cells,\r
1146  * with cell spans, with mappings to the original td elements.\r
1147  * @param table {CKEDITOR.dom.element}\r
1148  */\r
1149 CKEDITOR.tools.buildTableMap = function ( table )\r
1150 {\r
1151         var aRows = table.$.rows ;\r
1152 \r
1153         // Row and Column counters.\r
1154         var r = -1 ;\r
1155 \r
1156         var aMap = [];\r
1157 \r
1158         for ( var i = 0 ; i < aRows.length ; i++ )\r
1159         {\r
1160                 r++ ;\r
1161                 !aMap[r] && ( aMap[r] = [] );\r
1162 \r
1163                 var c = -1 ;\r
1164 \r
1165                 for ( var j = 0 ; j < aRows[i].cells.length ; j++ )\r
1166                 {\r
1167                         var oCell = aRows[i].cells[j] ;\r
1168 \r
1169                         c++ ;\r
1170                         while ( aMap[r][c] )\r
1171                                 c++ ;\r
1172 \r
1173                         var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;\r
1174                         var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;\r
1175 \r
1176                         for ( var rs = 0 ; rs < iRowSpan ; rs++ )\r
1177                         {\r
1178                                 if ( !aMap[r + rs] )\r
1179                                         aMap[r + rs] = [];\r
1180 \r
1181                                 for ( var cs = 0 ; cs < iColSpan ; cs++ )\r
1182                                 {\r
1183                                         aMap[r + rs][c + cs] = aRows[i].cells[j] ;\r
1184                                 }\r
1185                         }\r
1186 \r
1187                         c += iColSpan - 1 ;\r
1188                 }\r
1189         }\r
1190         return aMap ;\r
1191 };\r