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