plugin.js 31 KB

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