+ editor.container.focusPrevious( true, editor.tabIndex );\r
+ }\r
+ };\r
+\r
+ function selectNextCellCommand( backward )\r
+ {\r
+ return {\r
+ editorFocus : false,\r
+ canUndo : false,\r
+ modes : { wysiwyg : 1 },\r
+ exec : function( editor )\r
+ {\r
+ if ( editor.focusManager.hasFocus )\r
+ {\r
+ var sel = editor.getSelection(),\r
+ ancestor = sel.getCommonAncestor(),\r
+ cell;\r
+\r
+ if ( ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) ) )\r
+ {\r
+ var resultRange = new CKEDITOR.dom.range( editor.document ),\r
+ next = CKEDITOR.tools.tryThese( function()\r
+ {\r
+ var row = cell.getParent(),\r
+ next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ];\r
+\r
+ // Invalid any empty value.\r
+ next.parentNode.parentNode;\r
+ return next;\r
+ },\r
+ function()\r
+ {\r
+ var row = cell.getParent(),\r
+ table = row.getAscendant( 'table' ),\r
+ nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ];\r
+\r
+ return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ];\r
+ });\r
+\r
+ // Clone one more row at the end of table and select the first newly established cell.\r
+ if ( ! ( next || backward ) )\r
+ {\r
+ var table = cell.getAscendant( 'table' ).$,\r
+ cells = cell.getParent().$.cells;\r
+\r
+ var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );\r
+\r
+ for ( var i = 0, count = cells.length ; i < count; i++ )\r
+ {\r
+ var newCell = newRow.append( new CKEDITOR.dom.element(\r
+ cells[ i ], editor.document ).clone( false, false ) );\r
+ !CKEDITOR.env.ie && newCell.appendBogus();\r
+ }\r
+\r
+ resultRange.moveToElementEditStart( newRow );\r
+ }\r
+ else if ( next )\r
+ {\r
+ next = new CKEDITOR.dom.element( next );\r
+ resultRange.moveToElementEditStart( next );\r
+ // Avoid selecting empty block makes the cursor blind.\r
+ if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )\r
+ resultRange.selectNodeContents( next );\r
+ }\r
+ else\r
+ return true;\r
+\r
+ resultRange.select( true );\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r