1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/*
Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
(function()
{
function removeRawAttribute( $node, attr )
{
if ( CKEDITOR.env.ie )
$node.removeAttribute( attr );
else
delete $node[ attr ];
}
var cellNodeRegex = /^(?:td|th)$/;
function getSelectedCells( selection )
{
// Walker will try to split text nodes, which will make the current selection
// invalid. So save bookmarks before doing anything.
var bookmarks = selection.createBookmarks();
var ranges = selection.getRanges();
var retval = [];
var database = {};
function moveOutOfCellGuard( node )
{
// Apply to the first cell only.
if ( retval.length > 0 )
return;
// If we are exiting from the first </td>, then the td should definitely be
// included.
if ( node.type == CKEDITOR.NODE_ELEMENT && cellNodeRegex.test( node.getName() )
&& !node.getCustomData( 'selected_cell' ) )
{
CKEDITOR.dom.element.setMarker( database, node, 'selected_cell', true );
retval.push( node );
}
}
for ( var i = 0 ; i < ranges.length ; i++ )
{
var range = ranges[ i ];
if ( range.collapsed )
{
// Walker does not handle collapsed ranges yet - fall back to old API.
var startNode = range.getCommonAncestor();
var nearestCell = startNode.getAscendant( 'td', true ) || startNode.getAscendant( 'th', true );
if ( nearestCell )
retval.push( nearestCell );
}
else
{
var walker = new CKEDITOR.dom.walker( range );
var node;
walker.guard = moveOutOfCellGuard;
while ( ( node = walker.next() ) )
{
// If may be possible for us to have a range like this:
// <td>^1</td><td>^2</td>
// The 2nd td shouldn't be included.
//
// So we have to take care to include a td we've entered only when we've
// walked into its children.
var parent = node.getParent();
if ( parent && cellNodeRegex.test( parent.getName() ) && !parent.getCustomData( 'selected_cell' ) )
{
CKEDITOR.dom.element.setMarker( database, parent, 'selected_cell', true );
retval.push( parent );
}
}
}
}
CKEDITOR.dom.element.clearAllMarkers( database );
// Restore selection position.
selection.selectBookmarks( bookmarks );
return retval;
}
function createTableMap( $refCell )
{
var refCell = new CKEDITOR.dom.element( $refCell );
var $table = ( refCell.getName() == 'table' ? $refCell : refCell.getAscendant( 'table' ) ).$;
var $rows = $table.rows;
// Row and column counters.
var r = -1;
var map = [];
for ( var i = 0 ; i < $rows.length ; i++ )
{
r++;
if ( !map[ r ] )
map[ r ] = [];
var c = -1;
for ( var j = 0 ; j < $rows[ i ].cells.length ; j++ )
{
var $cell = $rows[ i ].cells[ j ];
c++;
while ( map[ r ][ c ] )
c++;
var colSpan = isNaN( $cell.colSpan ) ? 1 : $cell.colSpan;
var rowSpan = isNaN( $cell.rowSpan ) ? 1 : $cell.rowSpan;
for ( var rs = 0 ; rs < rowSpan ; rs++ )
{
if ( !map[ r + rs ] )
map[ r + rs ] = [];
for ( var cs = 0 ; cs < colSpan ; cs++ )
map [ r + rs ][ c + cs ] = $rows[ i ].cells[ j ];
}
c += colSpan - 1;
}
}
return map;
}
function installTableMap( tableMap, $table )
{
/*
* IE BUG: rowSpan is always 1 in IE if the cell isn't attached to a row. So
* store is separately in another attribute. (#1917)
*/
var rowSpanAttr = CKEDITOR.env.ie ? '_cke_rowspan' : 'rowSpan';
/*
* Disconnect all the cells in tableMap from their parents, set all colSpan
* and rowSpan attributes to 1.
*/
for ( var i = 0 ; i < tableMap.length ; i++ )
{
for ( var j = 0 ; j < tableMap[ i ].length ; j++ )
{
var $cell = tableMap[ i ][ j ];
if ( $cell.parentNode )
$cell.parentNode.removeChild( $cell );
$cell.colSpan = $cell[ rowSpanAttr ] = 1;
}
}
// Scan by rows and set colSpan.
var maxCol = 0;
for ( i = 0 ; i < tableMap.length ; i++ )
{
for ( j = 0 ; j < tableMap[ i ].length ; j++ )
{
$cell = tableMap[ i ][ j ];
if ( !$cell )
continue;
if ( j > maxCol )
maxCol = j;
if ( $cell[ '_cke_colScanned' ] )
continue;
if ( tableMap[ i ][ j - 1 ] == $cell )
$cell.colSpan++;
if ( tableMap[ i ][ j + 1 ] != $cell )
$cell[ '_cke_colScanned' ] = 1;
}
}
// Scan by columns and set rowSpan.
for ( i = 0 ; i <= maxCol ; i++ )
{
for ( j = 0 ; j < tableMap.length ; j++ )
{
if ( !tableMap[ j ] )
continue;
$cell = tableMap[ j ][ i ];
if ( !$cell || $cell[ '_cke_rowScanned' ] )
continue;
if ( tableMap[ j - 1 ] && tableMap[ j - 1 ][ i ] == $cell )
$cell[ rowSpanAttr ]++;
if ( !tableMap[ j + 1 ] || tableMap[ j + 1 ][ i ] != $cell )
$cell[ '_cke_rowScanned' ] = 1;
}
}
// Clear all temporary flags.
for ( i = 0 ; i < tableMap.length ; i++ )
{
for ( j = 0 ; j < tableMap[ i ].length ; j++ )
{
$cell = tableMap[ i ][ j ];
removeRawAttribute( $cell, '_cke_colScanned' );
removeRawAttribute( $cell, '_cke_rowScanned' );
}
}
// Insert physical rows and columns to table.
for ( i = 0 ; i < tableMap.length ; i++ )
{
var $row = $table.ownerDocument.createElement( 'tr' );
for ( j = 0 ; j < tableMap[ i ].length ; )
{
$cell = tableMap[ i ][ j ];
if ( tableMap[ i - 1 ] && tableMap[ i - 1 ][ j ] == $cell )
{
j += $cell.colSpan;
continue;
}
$row.appendChild( $cell );
if ( rowSpanAttr != 'rowSpan' )
{
$cell.rowSpan = $cell[ rowSpanAttr ];
$cell.removeAttribute( rowSpanAttr );
}
j += $cell.colSpan;
if ( $cell.colSpan == 1 )
$cell.removeAttribute( 'colSpan' );
if ( $cell.rowSpan == 1 )
$cell.removeAttribute( 'rowSpan' );
}
if ( CKEDITOR.env.ie )
$table.rows[ i ].replaceNode( $row );
else
{
var dest = new CKEDITOR.dom.element( $table.rows[ i ] );
var src = new CKEDITOR.dom.element( $row );
dest.setHtml( '' );
src.moveChildren( dest );
}
}
}
function clearRow( $tr )
{
// Get the array of row's cells.
var $cells = $tr.cells;
// Empty all cells.
for ( var i = 0 ; i < $cells.length ; i++ )
{
$cells[ i ].innerHTML = '';
if ( !CKEDITOR.env.ie )
( new CKEDITOR.dom.element( $cells[ i ] ) ).appendBogus();
}
}
function insertRow( selection, insertBefore )
{
// Get the row where the selection is placed in.
var row = selection.getStartElement().getAscendant( 'tr' );
if ( !row )
return;
// Create a clone of the row.
var newRow = row.clone( true );
// Insert the new row before of it.
newRow.insertBefore( row );
// Clean one of the rows to produce the illusion of inserting an empty row
// before or after.
clearRow( insertBefore ? newRow.$ : row.$ );
}
function deleteRows( selectionOrRow )
{
if ( selectionOrRow instanceof CKEDITOR.dom.selection )
{
var cells = getSelectedCells( selectionOrRow );
var rowsToDelete = [];
// Queue up the rows - it's possible and likely that we have duplicates.
for ( var i = 0 ; i < cells.length ; i++ )
{
var row = cells[ i ].getParent();
rowsToDelete[ row.$.rowIndex ] = row;
}
for ( i = rowsToDelete.length ; i >= 0 ; i-- )
{
if ( rowsToDelete[ i ] )
deleteRows( rowsToDelete[ i ] );
}
}
else if ( selectionOrRow instanceof CKEDITOR.dom.element )
{
var table = selectionOrRow.getAscendant( 'table' );
if ( table.$.rows.length == 1 )
table.remove();
else
selectionOrRow.remove();
}
}
function insertColumn( selection, insertBefore )
{
// Get the cell where the selection is placed in.
var startElement = selection.getStartElement();
var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );
if ( !cell )
return;
// Get the cell's table.
var table = cell.getAscendant( 'table' );
var cellIndex = cell.$.cellIndex;
// Loop through all rows available in the table.
for ( var i = 0 ; i < table.$.rows.length ; i++ )
{
var $row = table.$.rows[ i ];
// If the row doesn't have enough cells, ignore it.
if ( $row.cells.length < ( cellIndex + 1 ) )
continue;
cell = new CKEDITOR.dom.element( $row.cells[ cellIndex ].cloneNode( false ) );
if ( !CKEDITOR.env.ie )
cell.appendBogus();
// Get back the currently selected cell.
var baseCell = new CKEDITOR.dom.element( $row.cells[ cellIndex ] );
if ( insertBefore )
cell.insertBefore( baseCell );
else
cell.insertAfter( baseCell );
}
}
function deleteColumns( selectionOrCell )
{
if ( selectionOrCell instanceof CKEDITOR.dom.selection )
{
var colsToDelete = getSelectedCells( selectionOrCell );
for ( var i = colsToDelete.length ; i >= 0 ; i-- )
{
if ( colsToDelete[ i ] )
deleteColumns( colsToDelete[ i ] );
}
}
else if ( selectionOrCell instanceof CKEDITOR.dom.element )
{
// Get the cell's table.
var table = selectionOrCell.getAscendant( 'table' );
// Get the cell index.
var cellIndex = selectionOrCell.$.cellIndex;
/*
* Loop through all rows from down to up, coz it's possible that some rows
* will be deleted.
*/
for ( i = table.$.rows.length - 1 ; i >= 0 ; i-- )
{
// Get the row.
var row = new CKEDITOR.dom.element( table.$.rows[ i ] );
// If the cell to be removed is the first one and the row has just one cell.
if ( !cellIndex && row.$.cells.length == 1 )
{
deleteRows( row );
continue;
}
// Else, just delete the cell.
if ( row.$.cells[ cellIndex ] )
row.$.removeChild( row.$.cells[ cellIndex ] );
}
}
}
function insertCell( selection, insertBefore )
{
var startElement = selection.getStartElement();
var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true );
if ( !cell )
return;
// Create the new cell element to be added.
var newCell = cell.clone();
if ( !CKEDITOR.env.ie )
newCell.appendBogus();
if ( insertBefore )
newCell.insertBefore( cell );
else
newCell.insertAfter( cell );
}
function deleteCells( selectionOrCell )
{
if ( selectionOrCell instanceof CKEDITOR.dom.selection )
{
var cellsToDelete = getSelectedCells( selectionOrCell );
for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )
deleteCells( cellsToDelete[ i ] );
}
else if ( selectionOrCell instanceof CKEDITOR.dom.element )
{
if ( selectionOrCell.getParent().getChildCount() == 1 )
selectionOrCell.getParent().remove();
else
selectionOrCell.remove();
}
}
// Context menu on table caption incorrect (#3834)
var contextMenuTags = { thead : 1, tbody : 1, tfoot : 1, td : 1, tr : 1, th : 1 };
CKEDITOR.plugins.tabletools =
{
init : function( editor )
{
var lang = editor.lang.table;
editor.addCommand( 'cellProperties', new CKEDITOR.dialogCommand( 'cellProperties' ) );
CKEDITOR.dialog.add( 'cellProperties', this.path + 'dialogs/tableCell.js' );
editor.addCommand( 'tableDelete',
{
exec : function( editor )
{
var selection = editor.getSelection();
var startElement = selection && selection.getStartElement();
var table = startElement && startElement.getAscendant( 'table', true );
if ( !table )
return;
// Maintain the selection point at where the table was deleted.
selection.selectElement( table );
var range = selection.getRanges()[0];
range.collapse();
selection.selectRanges( [ range ] );
// If the table's parent has only one child, remove it as well.
if ( table.getParent().getChildCount() == 1 )
table.getParent().remove();
else
table.remove();
}
} );
editor.addCommand( 'rowDelete',
{
exec : function( editor )
{
var selection = editor.getSelection();
deleteRows( selection );
}
} );
editor.addCommand( 'rowInsertBefore',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertRow( selection, true );
}
} );
editor.addCommand( 'rowInsertAfter',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertRow( selection );
}
} );
editor.addCommand( 'columnDelete',
{
exec : function( editor )
{
var selection = editor.getSelection();
deleteColumns( selection );
}
} );
editor.addCommand( 'columnInsertBefore',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertColumn( selection, true );
}
} );
editor.addCommand( 'columnInsertAfter',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertColumn( selection );
}
} );
editor.addCommand( 'cellDelete',
{
exec : function( editor )
{
var selection = editor.getSelection();
deleteCells( selection );
}
} );
editor.addCommand( 'cellInsertBefore',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertCell( selection, true );
}
} );
editor.addCommand( 'cellInsertAfter',
{
exec : function( editor )
{
var selection = editor.getSelection();
insertCell( selection );
}
} );
// If the "menu" plugin is loaded, register the menu items.
if ( editor.addMenuItems )
{
editor.addMenuItems(
{
tablecell :
{
label : lang.cell.menu,
group : 'tablecell',
order : 1,
getItems : function()
{
var cells = getSelectedCells( editor.getSelection() );
return {
tablecell_insertBefore : CKEDITOR.TRISTATE_OFF,
tablecell_insertAfter : CKEDITOR.TRISTATE_OFF,
tablecell_delete : CKEDITOR.TRISTATE_OFF,
tablecell_properties : cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
};
}
},
tablecell_insertBefore :
{
label : lang.cell.insertBefore,
group : 'tablecell',
command : 'cellInsertBefore',
order : 5
},
tablecell_insertAfter :
{
label : lang.cell.insertAfter,
group : 'tablecell',
command : 'cellInsertAfter',
order : 10
},
tablecell_delete :
{
label : lang.cell.deleteCell,
group : 'tablecell',
command : 'cellDelete',
order : 15
},
tablecell_properties :
{
label : lang.cell.title,
group : 'tablecellproperties',
command : 'cellProperties',
order : 20
},
tablerow :
{
label : lang.row.menu,
group : 'tablerow',
order : 1,
getItems : function()
{
return {
tablerow_insertBefore : CKEDITOR.TRISTATE_OFF,
tablerow_insertAfter : CKEDITOR.TRISTATE_OFF,
tablerow_delete : CKEDITOR.TRISTATE_OFF
};
}
},
tablerow_insertBefore :
{
label : lang.row.insertBefore,
group : 'tablerow',
command : 'rowInsertBefore',
order : 5
},
tablerow_insertAfter :
{
label : lang.row.insertAfter,
group : 'tablerow',
command : 'rowInsertAfter',
order : 10
},
tablerow_delete :
{
label : lang.row.deleteRow,
group : 'tablerow',
command : 'rowDelete',
order : 15
},
tablecolumn :
{
label : lang.column.menu,
group : 'tablecolumn',
order : 1,
getItems : function()
{
return {
tablecolumn_insertBefore : CKEDITOR.TRISTATE_OFF,
tablecolumn_insertAfter : CKEDITOR.TRISTATE_OFF,
tablecolumn_delete : CKEDITOR.TRISTATE_OFF
};
}
},
tablecolumn_insertBefore :
{
label : lang.column.insertBefore,
group : 'tablecolumn',
command : 'columnInsertBefore',
order : 5
},
tablecolumn_insertAfter :
{
label : lang.column.insertAfter,
group : 'tablecolumn',
command : 'columnInsertAfter',
order : 10
},
tablecolumn_delete :
{
label : lang.column.deleteColumn,
group : 'tablecolumn',
command : 'columnDelete',
order : 15
}
});
}
// If the "contextmenu" plugin is laoded, register the listeners.
if ( editor.contextMenu )
{
editor.contextMenu.addListener( function( element, selection )
{
if ( !element )
return null;
while ( element )
{
if ( element.getName() in contextMenuTags )
{
return {
tablecell : CKEDITOR.TRISTATE_OFF,
tablerow : CKEDITOR.TRISTATE_OFF,
tablecolumn : CKEDITOR.TRISTATE_OFF
};
}
element = element.getParent();
}
return null;
} );
}
},
getSelectedCells : getSelectedCells
};
CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools );
})();
|