JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / tabletools / plugin.js
index 1f2224c..0019a49 100644 (file)
@@ -146,14 +146,14 @@ 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
@@ -216,7 +216,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\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
@@ -234,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
@@ -339,7 +339,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        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
@@ -400,51 +400,6 @@ 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
@@ -498,7 +453,7 @@ 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
@@ -633,7 +588,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                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
@@ -709,7 +664,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                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
@@ -759,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
@@ -772,9 +727,9 @@ 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,except body,as well.( #5416 )\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.getName() != 'body' )\r
+                                               if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) )\r
                                                        parent.remove();\r
                                                else\r
                                                        table.remove();\r
@@ -1088,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
@@ -1114,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