JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / tabletools / plugin.js
index 6266912..0019a49 100644 (file)
@@ -86,6 +86,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                return retval;\r
        }\r
 \r
+       function getFocusElementAfterDelCells( cellsToDelete ) {\r
+               var i = 0,\r
+                       last = cellsToDelete.length - 1,\r
+                       database = {},\r
+                       cell,focusedCell,\r
+                       tr;\r
+\r
+               while ( ( cell = cellsToDelete[ i++ ] ) )\r
+                       CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );\r
+\r
+               // 1.first we check left or right side focusable cell row by row;\r
+               i = 0;\r
+               while ( ( cell = cellsToDelete[ i++ ] ) )\r
+               {\r
+                       if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )\r
+                         || ( focusedCell = cell.getNext()     ) && !focusedCell.getCustomData( 'delete_cell' ) )\r
+                       {\r
+                               CKEDITOR.dom.element.clearAllMarkers( database );\r
+                               return focusedCell;\r
+                       }\r
+               }\r
+\r
+               CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+               // 2. then we check the toppest row (outside the selection area square) focusable cell\r
+               tr = cellsToDelete[ 0 ].getParent();\r
+               if ( ( tr = tr.getPrevious() ) )\r
+                       return tr.getLast();\r
+\r
+               // 3. last we check the lowerest  row focusable cell\r
+               tr = cellsToDelete[ last ].getParent();\r
+               if ( ( tr = tr.getNext() ) )\r
+                       return tr.getChild( 0 );\r
+\r
+               return null;\r
+       }\r
+\r
        function clearRow( $tr )\r
        {\r
                // Get the array of row's cells.\r
@@ -109,52 +146,77 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return;\r
 \r
                // Create a clone of the row.\r
-               var newRow = row.clone( true );\r
+               var newRow = row.clone( 1 );\r
 \r
-               // Insert the new row before of it.\r
-               newRow.insertBefore( row );\r
+               insertBefore ?\r
+                       newRow.insertBefore( row ) :\r
+                       newRow.insertAfter( row );\r
 \r
-               // Clean one of the rows to produce the illusion of inserting an empty row\r
-               // before or after.\r
-               clearRow( insertBefore ? newRow.$ : row.$ );\r
+               // Clean the new row.\r
+               clearRow( newRow.$ );\r
        }\r
 \r
        function deleteRows( selectionOrRow )\r
        {\r
                if ( selectionOrRow instanceof CKEDITOR.dom.selection )\r
                {\r
-                       var cells = getSelectedCells( selectionOrRow );\r
-                       var rowsToDelete = [];\r
+                       var cells = getSelectedCells( selectionOrRow ),\r
+                               cellsCount = cells.length,\r
+                               rowsToDelete = [],\r
+                               cursorPosition,\r
+                               previousRowIndex,\r
+                               nextRowIndex;\r
 \r
                        // Queue up the rows - it's possible and likely that we have duplicates.\r
-                       for ( var i = 0 ; i < cells.length ; i++ )\r
+                       for ( var i = 0 ; i < cellsCount ; i++ )\r
                        {\r
-                               var row = cells[ i ].getParent();\r
-                               rowsToDelete[ row.$.rowIndex ] = row;\r
+                               var row = cells[ i ].getParent(),\r
+                                               rowIndex = row.$.rowIndex;\r
+\r
+                               !i && ( previousRowIndex = rowIndex - 1 );\r
+                               rowsToDelete[ rowIndex ] = row;\r
+                               i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 );\r
                        }\r
 \r
+                       var table = row.getAscendant( 'table' ),\r
+                                       rows =  table.$.rows,\r
+                                       rowCount = rows.length;\r
+\r
+                       // Where to put the cursor after rows been deleted?\r
+                       // 1. Into next sibling row if any;\r
+                       // 2. Into previous sibling row if any;\r
+                       // 3. Into table's parent element if it's the very last row.\r
+                       cursorPosition = new CKEDITOR.dom.element(\r
+                               nextRowIndex < rowCount && table.$.rows[ nextRowIndex ] ||\r
+                               previousRowIndex > 0 && table.$.rows[ previousRowIndex ] ||\r
+                               table.$.parentNode );\r
+\r
                        for ( i = rowsToDelete.length ; i >= 0 ; i-- )\r
                        {\r
                                if ( rowsToDelete[ i ] )\r
                                        deleteRows( rowsToDelete[ i ] );\r
                        }\r
+\r
+                       return cursorPosition;\r
                }\r
                else if ( selectionOrRow instanceof CKEDITOR.dom.element )\r
                {\r
-                       var table = selectionOrRow.getAscendant( 'table' );\r
+                       table = selectionOrRow.getAscendant( 'table' );\r
 \r
                        if ( table.$.rows.length == 1 )\r
                                table.remove();\r
                        else\r
                                selectionOrRow.remove();\r
                }\r
+\r
+               return 0;\r
        }\r
 \r
        function insertColumn( selection, insertBefore )\r
        {\r
                // Get the cell where the selection is placed in.\r
                var startElement = selection.getStartElement();\r
-               var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );\r
+               var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );\r
 \r
                if ( !cell )\r
                        return;\r
@@ -172,7 +234,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( $row.cells.length < ( cellIndex + 1 ) )\r
                                continue;\r
 \r
-                       cell = new CKEDITOR.dom.element( $row.cells[ cellIndex ].cloneNode( false ) );\r
+                       cell = ( new CKEDITOR.dom.element( $row.cells[ cellIndex ] ) ).clone( 0 );\r
 \r
                        if ( !CKEDITOR.env.ie )\r
                                cell.appendBogus();\r
@@ -186,21 +248,65 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
+       function getFocusElementAfterDelCols( cells )\r
+       {\r
+               var cellIndexList = [],\r
+                       table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),\r
+                       i, length,\r
+                       targetIndex, targetCell;\r
+\r
+               // get the cellIndex list of delete cells\r
+               for ( i = 0, length = cells.length; i < length; i++ )\r
+                       cellIndexList.push( cells[i].$.cellIndex );\r
+\r
+               // get the focusable column index\r
+               cellIndexList.sort();\r
+               for ( i = 1, length = cellIndexList.length; i < length; i++ )\r
+               {\r
+                       if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 )\r
+                       {\r
+                               targetIndex = cellIndexList[ i - 1 ] + 1;\r
+                               break;\r
+                       }\r
+               }\r
+\r
+               if ( !targetIndex )\r
+                       targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 )\r
+                                                       : ( cellIndexList[ cellIndexList.length - 1 ] + 1 );\r
+\r
+               // scan row by row to get the target cell\r
+               var rows = table.$.rows;\r
+               for ( i = 0, length = rows.length; i < length ; i++ )\r
+               {\r
+                       targetCell = rows[ i ].cells[ targetIndex ];\r
+                       if ( targetCell )\r
+                               break;\r
+               }\r
+\r
+               return targetCell ?  new CKEDITOR.dom.element( targetCell ) :  table.getPrevious();\r
+       }\r
+\r
        function deleteColumns( selectionOrCell )\r
        {\r
                if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
                {\r
-                       var colsToDelete = getSelectedCells( selectionOrCell );\r
-                       for ( var i = colsToDelete.length ; i >= 0 ; i-- )\r
+                       var colsToDelete = getSelectedCells( selectionOrCell ),\r
+                               elementToFocus = getFocusElementAfterDelCols( colsToDelete );\r
+\r
+                       for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- )\r
                        {\r
                                if ( colsToDelete[ i ] )\r
                                        deleteColumns( colsToDelete[ i ] );\r
                        }\r
+\r
+                       return elementToFocus;\r
                }\r
                else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
                {\r
                        // Get the cell's table.\r
                        var table = selectionOrCell.getAscendant( 'table' );\r
+                       if ( !table )\r
+                               return null;\r
 \r
                        // Get the cell index.\r
                        var cellIndex = selectionOrCell.$.cellIndex;\r
@@ -226,12 +332,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        row.$.removeChild( row.$.cells[ cellIndex ] );\r
                        }\r
                }\r
+\r
+               return null;\r
        }\r
 \r
        function insertCell( selection, insertBefore )\r
        {\r
                var startElement = selection.getStartElement();\r
-               var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );\r
+               var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 );\r
 \r
                if ( !cell )\r
                        return;\r
@@ -252,13 +360,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
                {\r
                        var cellsToDelete = getSelectedCells( selectionOrCell );\r
+                       var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
+                       var cellToFocus   = getFocusElementAfterDelCells( cellsToDelete );\r
+\r
                        for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
                                deleteCells( cellsToDelete[ i ] );\r
+\r
+                       if ( cellToFocus )\r
+                               placeCursorInCell( cellToFocus, true );\r
+                       else if ( table )\r
+                               table.remove();\r
                }\r
                else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
                {\r
-                       if ( selectionOrCell.getParent().getChildCount() == 1 )\r
-                               selectionOrCell.getParent().remove();\r
+                       var tr = selectionOrCell.getParent();\r
+                       if ( tr.getChildCount() == 1 )\r
+                               tr.remove();\r
                        else\r
                                selectionOrCell.remove();\r
                }\r
@@ -283,62 +400,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                range.select( true );\r
        }\r
 \r
-       function buildTableMap( table )\r
-       {\r
-\r
-               var aRows = table.$.rows ;\r
-\r
-               // Row and Column counters.\r
-               var r = -1 ;\r
-\r
-               var aMap = [];\r
-\r
-               for ( var i = 0 ; i < aRows.length ; i++ )\r
-               {\r
-                       r++ ;\r
-                       !aMap[r] && ( aMap[r] = [] );\r
-\r
-                       var c = -1 ;\r
-\r
-                       for ( var j = 0 ; j < aRows[i].cells.length ; j++ )\r
-                       {\r
-                               var oCell = aRows[i].cells[j] ;\r
-\r
-                               c++ ;\r
-                               while ( aMap[r][c] )\r
-                                       c++ ;\r
-\r
-                               var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;\r
-                               var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;\r
-\r
-                               for ( var rs = 0 ; rs < iRowSpan ; rs++ )\r
-                               {\r
-                                       if ( !aMap[r + rs] )\r
-                                               aMap[r + rs] = new Array() ;\r
-\r
-                                       for ( var cs = 0 ; cs < iColSpan ; cs++ )\r
-                                       {\r
-                                               aMap[r + rs][c + cs] = aRows[i].cells[j] ;\r
-                                       }\r
-                               }\r
-\r
-                               c += iColSpan - 1 ;\r
-                       }\r
-               }\r
-               return aMap ;\r
-       }\r
-\r
        function cellInRow( tableMap, rowIndex, cell )\r
        {\r
                var oRow = tableMap[ rowIndex ];\r
-               if( typeof cell == 'undefined' )\r
+               if ( typeof cell == 'undefined' )\r
                        return oRow;\r
 \r
                for ( var c = 0 ; oRow && c < oRow.length ; c++ )\r
                {\r
                        if ( cell.is && oRow[c] == cell.$ )\r
                                return c;\r
-                       else if( c == cell )\r
+                       else if ( c == cell )\r
                                return new CKEDITOR.dom.element( oRow[ c ] );\r
                }\r
                return cell.is ? -1 : null;\r
@@ -350,11 +422,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( var r = 0; r < tableMap.length; r++ )\r
                {\r
                        var row = tableMap[ r ];\r
-                       if( typeof cell == 'undefined' )\r
+                       if ( typeof cell == 'undefined' )\r
                                oCol.push( row[ colIndex ] );\r
-                       else if( cell.is && row[ colIndex ] == cell.$ )\r
+                       else if ( cell.is && row[ colIndex ] == cell.$ )\r
                                return r;\r
-                       else if( r == cell )\r
+                       else if ( r == cell )\r
                                return new CKEDITOR.dom.element( row[ colIndex ] );\r
                }\r
 \r
@@ -381,13 +453,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var     cell,\r
                        firstCell = cells[ 0 ],\r
                        table = firstCell.getAscendant( 'table' ),\r
-                       map = buildTableMap( table ),\r
+                       map = CKEDITOR.tools.buildTableMap( table ),\r
                        mapHeight = map.length,\r
                        mapWidth = map[ 0 ].length,\r
                        startRow = firstCell.getParent().$.rowIndex,\r
                        startColumn = cellInRow( map, startRow, firstCell );\r
 \r
-               if( mergeDirection )\r
+               if ( mergeDirection )\r
                {\r
                        var targetCell;\r
                        try\r
@@ -408,7 +480,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        // 1. No cell could be merged.\r
                        // 2. Same cell actually.\r
-                       if( !targetCell || firstCell.$ == targetCell  )\r
+                       if ( !targetCell || firstCell.$ == targetCell  )\r
                                return false;\r
 \r
                        // Sort in map order regardless of the DOM sequence.\r
@@ -445,16 +517,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( !isDetect )\r
                        {\r
                                // Trim all cell fillers and check to remove empty cells.\r
-                               if( trimCell( cell ), cell.getChildren().count() )\r
+                               if ( trimCell( cell ), cell.getChildren().count() )\r
                                {\r
                                        // Merge vertically cells as two separated paragraphs.\r
-                                       if( rowIndex != lastRowIndex\r
+                                       if ( rowIndex != lastRowIndex\r
                                                && cellFirstChild\r
                                                && !( cellFirstChild.isBlockBoundary\r
                                                          && cellFirstChild.isBlockBoundary( { br : 1 } ) ) )\r
                                        {\r
                                                var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
-                                               if( last && !( last.is && last.is( 'br' ) ) )\r
+                                               if ( last && !( last.is && last.is( 'br' ) ) )\r
                                                        frag.append( new CKEDITOR.dom.element( 'br' ) );\r
                                        }\r
 \r
@@ -469,15 +541,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        frag.moveChildren( firstCell );\r
 \r
-                       if( !CKEDITOR.env.ie )\r
+                       if ( !CKEDITOR.env.ie )\r
                                firstCell.appendBogus();\r
 \r
-                       if( totalColSpan >= mapWidth )\r
+                       if ( totalColSpan >= mapWidth )\r
                                firstCell.removeAttribute( 'rowSpan' );\r
                        else\r
                                firstCell.$.rowSpan = totalRowSpan;\r
 \r
-                       if( totalRowSpan >= mapHeight )\r
+                       if ( totalRowSpan >= mapHeight )\r
                                firstCell.removeAttribute( 'colSpan' );\r
                        else\r
                                firstCell.$.colSpan = totalColSpan;\r
@@ -489,7 +561,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        for ( i = count - 1; i >= 0; i-- )\r
                        {\r
                                var tailTr = trs.getItem( i );\r
-                               if( !tailTr.$.cells.length )\r
+                               if ( !tailTr.$.cells.length )\r
                                {\r
                                        tailTr.remove();\r
                                        count++;\r
@@ -508,15 +580,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function verticalSplitCell ( selection, isDetect )\r
        {\r
                var cells = getSelectedCells( selection );\r
-               if( cells.length > 1 )\r
+               if ( cells.length > 1 )\r
                        return false;\r
-               else if( isDetect )\r
+               else if ( isDetect )\r
                        return true;\r
 \r
                var cell = cells[ 0 ],\r
                        tr = cell.getParent(),\r
                        table = tr.getAscendant( 'table' ),\r
-                       map = buildTableMap( table ),\r
+                       map = CKEDITOR.tools.buildTableMap( table ),\r
                        rowIndex = tr.$.rowIndex,\r
                        colIndex = cellInRow( map, rowIndex, cell ),\r
                        rowSpan = cell.$.rowSpan,\r
@@ -525,7 +597,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        newCellRowSpan,\r
                        newRowIndex;\r
 \r
-               if( rowSpan > 1 )\r
+               if ( rowSpan > 1 )\r
                {\r
                        newRowSpan = Math.ceil( rowSpan / 2 );\r
                        newCellRowSpan = Math.floor( rowSpan / 2 );\r
@@ -541,7 +613,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                candidateCell = newCellRow[ c ];\r
                                // Catch first cell actually following the column.\r
-                               if( candidateCell.parentNode == newCellTr.$\r
+                               if ( candidateCell.parentNode == newCellTr.$\r
                                        && c > colIndex )\r
                                {\r
                                        newCell.insertBefore( new CKEDITOR.dom.element( candidateCell ) );\r
@@ -552,7 +624,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
 \r
                        // The destination row is empty, append at will.\r
-                       if( !candidateCell )\r
+                       if ( !candidateCell )\r
                                newCellTr.append( newCell, true );\r
                }\r
                else\r
@@ -568,14 +640,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                cellsInSameRow[ i ].rowSpan++;\r
                }\r
 \r
-               if( !CKEDITOR.env.ie )\r
+               if ( !CKEDITOR.env.ie )\r
                        newCell.appendBogus();\r
 \r
                cell.$.rowSpan = newRowSpan;\r
                newCell.$.rowSpan = newCellRowSpan;\r
-               if( newRowSpan == 1 )\r
+               if ( newRowSpan == 1 )\r
                        cell.removeAttribute( 'rowSpan' );\r
-               if( newCellRowSpan == 1 )\r
+               if ( newCellRowSpan == 1 )\r
                        newCell.removeAttribute( 'rowSpan' );\r
 \r
                return newCell;\r
@@ -584,15 +656,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function horizontalSplitCell( selection, isDetect )\r
        {\r
                var cells = getSelectedCells( selection );\r
-               if( cells.length > 1 )\r
+               if ( cells.length > 1 )\r
                        return false;\r
-               else if( isDetect )\r
+               else if ( isDetect )\r
                        return true;\r
 \r
                var cell = cells[ 0 ],\r
                        tr = cell.getParent(),\r
                        table = tr.getAscendant( 'table' ),\r
-                       map = buildTableMap( table ),\r
+                       map = CKEDITOR.tools.buildTableMap( table ),\r
                        rowIndex = tr.$.rowIndex,\r
                        colIndex = cellInRow( map, rowIndex, cell ),\r
                        colSpan = cell.$.colSpan,\r
@@ -600,7 +672,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        newColSpan,\r
                        newCellColSpan;\r
 \r
-               if( colSpan > 1 )\r
+               if ( colSpan > 1 )\r
                {\r
                        newColSpan = Math.ceil( colSpan / 2 );\r
                        newCellColSpan = Math.floor( colSpan / 2 );\r
@@ -614,14 +686,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
                newCell = cell.clone();\r
                newCell.insertAfter( cell );\r
-               if( !CKEDITOR.env.ie )\r
+               if ( !CKEDITOR.env.ie )\r
                        newCell.appendBogus();\r
 \r
                cell.$.colSpan = newColSpan;\r
                newCell.$.colSpan = newCellColSpan;\r
-               if( newColSpan == 1 )\r
+               if ( newColSpan == 1 )\r
                        cell.removeAttribute( 'colSpan' );\r
-               if( newCellColSpan == 1 )\r
+               if ( newCellColSpan == 1 )\r
                        newCell.removeAttribute( 'colSpan' );\r
 \r
                return newCell;\r
@@ -642,9 +714,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                {\r
                                        exec : function( editor )\r
                                        {\r
-                                               var selection = editor.getSelection();\r
-                                               var startElement = selection && selection.getStartElement();\r
-                                               var table = startElement && startElement.getAscendant( 'table', true );\r
+                                               var selection = editor.getSelection(),\r
+                                                       startElement = selection && selection.getStartElement(),\r
+                                                       table = startElement && startElement.getAscendant( 'table', 1 );\r
 \r
                                                if ( !table )\r
                                                        return;\r
@@ -655,9 +727,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                range.collapse();\r
                                                selection.selectRanges( [ range ] );\r
 \r
-                                               // If the table's parent has only one child, remove it as well.\r
-                                               if ( table.getParent().getChildCount() == 1 )\r
-                                                       table.getParent().remove();\r
+                                               // 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
+                                               var parent = table.getParent();\r
+                                               if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )\r
+                                                       parent.remove();\r
                                                else\r
                                                        table.remove();\r
                                        }\r
@@ -668,7 +741,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        exec : function( editor )\r
                                        {\r
                                                var selection = editor.getSelection();\r
-                                               deleteRows( selection );\r
+                                               placeCursorInCell( deleteRows( selection ) );\r
                                        }\r
                                } );\r
 \r
@@ -695,7 +768,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        exec : function( editor )\r
                                        {\r
                                                var selection = editor.getSelection();\r
-                                               deleteColumns( selection );\r
+                                               var element = deleteColumns( selection );\r
+                                               element &&  placeCursorInCell( element, true );\r
                                        }\r
                                } );\r
 \r
@@ -969,7 +1043,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                editor.contextMenu.addListener( function( element, selection )\r
                                        {\r
-                                               if ( !element )\r
+                                               if ( !element || element.isReadOnly() )\r
                                                        return null;\r
 \r
                                                while ( element )\r
@@ -995,3 +1069,52 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        };\r
        CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );\r
 })();\r
+\r
+/**\r
+ * Create a two-dimension array that reflects the actual layout of table cells,\r
+ * with cell spans, with mappings to the original td elements.\r
+ * @param table {CKEDITOR.dom.element}\r
+ */\r
+CKEDITOR.tools.buildTableMap = function ( table )\r
+{\r
+       var aRows = table.$.rows ;\r
+\r
+       // Row and Column counters.\r
+       var r = -1 ;\r
+\r
+       var aMap = [];\r
+\r
+       for ( var i = 0 ; i < aRows.length ; i++ )\r
+       {\r
+               r++ ;\r
+               !aMap[r] && ( aMap[r] = [] );\r
+\r
+               var c = -1 ;\r
+\r
+               for ( var j = 0 ; j < aRows[i].cells.length ; j++ )\r
+               {\r
+                       var oCell = aRows[i].cells[j] ;\r
+\r
+                       c++ ;\r
+                       while ( aMap[r][c] )\r
+                               c++ ;\r
+\r
+                       var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;\r
+                       var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;\r
+\r
+                       for ( var rs = 0 ; rs < iRowSpan ; rs++ )\r
+                       {\r
+                               if ( !aMap[r + rs] )\r
+                                       aMap[r + rs] = [];\r
+\r
+                               for ( var cs = 0 ; cs < iColSpan ; cs++ )\r
+                               {\r
+                                       aMap[r + rs][c + cs] = aRows[i].cells[j] ;\r
+                               }\r
+                       }\r
+\r
+                       c += iColSpan - 1 ;\r
+               }\r
+       }\r
+       return aMap ;\r
+};\r