forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsTableEditor.cpp
3382 lines (2934 loc) · 110 KB
/
nsTableEditor.cpp
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdio.h>
#include "mozilla/Assertions.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/dom/Element.h"
#include "nsAString.h"
#include "nsAlgorithm.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsEditor.h"
#include "nsEditorUtils.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsHTMLEditUtils.h"
#include "nsHTMLEditor.h"
#include "nsIAtom.h"
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsIEditor.h"
#include "nsIFrame.h"
#include "nsIHTMLEditor.h"
#include "nsINode.h"
#include "nsIPresShell.h"
#include "nsISupportsUtils.h"
#include "nsITableCellLayout.h" // For efficient access to table cell
#include "nsITableEditor.h"
#include "nsLiteralString.h"
#include "nsQueryFrame.h"
#include "nsRange.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsTableCellFrame.h"
#include "nsTableOuterFrame.h"
#include "nscore.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::dom;
/***************************************************************************
* stack based helper class for restoring selection after table edit
*/
class MOZ_STACK_CLASS nsSetSelectionAfterTableEdit
{
private:
nsCOMPtr<nsITableEditor> mEd;
nsCOMPtr<nsIDOMElement> mTable;
int32_t mCol, mRow, mDirection, mSelected;
public:
nsSetSelectionAfterTableEdit(nsITableEditor *aEd, nsIDOMElement* aTable,
int32_t aRow, int32_t aCol, int32_t aDirection,
bool aSelected) :
mEd(do_QueryInterface(aEd))
{
mTable = aTable;
mRow = aRow;
mCol = aCol;
mDirection = aDirection;
mSelected = aSelected;
}
~nsSetSelectionAfterTableEdit()
{
if (mEd)
mEd->SetSelectionAfterTableEdit(mTable, mRow, mCol, mDirection, mSelected);
}
// This is needed to abort the caret reset in the destructor
// when one method yields control to another
void CancelSetCaret() {mEd = nullptr; mTable = nullptr;}
};
// Table Editing helper utilities (not exposed in IDL)
NS_IMETHODIMP
nsHTMLEditor::InsertCell(nsIDOMElement *aCell, int32_t aRowSpan, int32_t aColSpan,
bool aAfter, bool aIsHeader, nsIDOMElement **aNewCell)
{
NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
if (aNewCell) *aNewCell = nullptr;
// And the parent and offsets needed to do an insert
nsCOMPtr<nsIDOMNode> cellParent;
nsresult res = aCell->GetParentNode(getter_AddRefs(cellParent));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(cellParent, NS_ERROR_NULL_POINTER);
int32_t cellOffset = GetChildOffset(aCell, cellParent);
nsCOMPtr<nsIDOMElement> newCell;
if (aIsHeader)
res = CreateElementWithDefaults(NS_LITERAL_STRING("th"), getter_AddRefs(newCell));
else
res = CreateElementWithDefaults(NS_LITERAL_STRING("td"), getter_AddRefs(newCell));
if(NS_FAILED(res)) return res;
if(!newCell) return NS_ERROR_FAILURE;
//Optional: return new cell created
if (aNewCell)
{
*aNewCell = newCell.get();
NS_ADDREF(*aNewCell);
}
if( aRowSpan > 1)
{
// Note: Do NOT use editor transaction for this
nsAutoString newRowSpan;
newRowSpan.AppendInt(aRowSpan, 10);
newCell->SetAttribute(NS_LITERAL_STRING("rowspan"), newRowSpan);
}
if( aColSpan > 1)
{
// Note: Do NOT use editor transaction for this
nsAutoString newColSpan;
newColSpan.AppendInt(aColSpan, 10);
newCell->SetAttribute(NS_LITERAL_STRING("colspan"), newColSpan);
}
if(aAfter) cellOffset++;
//Don't let Rules System change the selection
nsAutoTxnsConserveSelection dontChangeSelection(this);
return InsertNode(newCell, cellParent, cellOffset);
}
NS_IMETHODIMP nsHTMLEditor::SetColSpan(nsIDOMElement *aCell, int32_t aColSpan)
{
NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
nsAutoString newSpan;
newSpan.AppendInt(aColSpan, 10);
return SetAttribute(aCell, NS_LITERAL_STRING("colspan"), newSpan);
}
NS_IMETHODIMP nsHTMLEditor::SetRowSpan(nsIDOMElement *aCell, int32_t aRowSpan)
{
NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
nsAutoString newSpan;
newSpan.AppendInt(aRowSpan, 10);
return SetAttribute(aCell, NS_LITERAL_STRING("rowspan"), newSpan);
}
/****************************************************************/
// Table Editing interface methods
NS_IMETHODIMP
nsHTMLEditor::InsertTableCell(int32_t aNumber, bool aAfter)
{
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> curCell;
nsCOMPtr<nsIDOMNode> cellParent;
int32_t cellOffset, startRowIndex, startColIndex;
nsresult res = GetCellContext(nullptr,
getter_AddRefs(table),
getter_AddRefs(curCell),
getter_AddRefs(cellParent), &cellOffset,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(curCell, NS_EDITOR_ELEMENT_NOT_FOUND);
// Get more data for current cell in row we are inserting at (we need COLSPAN)
int32_t curStartRowIndex, curStartColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
bool isSelected;
res = GetCellDataAt(table, startRowIndex, startColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex, &rowSpan, &colSpan,
&actualRowSpan, &actualColSpan, &isSelected);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(curCell, NS_ERROR_FAILURE);
int32_t newCellIndex = aAfter ? (startColIndex+colSpan) : startColIndex;
//We control selection resetting after the insert...
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, newCellIndex, ePreviousColumn, false);
//...so suppress Rules System selection munging
nsAutoTxnsConserveSelection dontChangeSelection(this);
int32_t i;
for (i = 0; i < aNumber; i++)
{
nsCOMPtr<nsIDOMElement> newCell;
res = CreateElementWithDefaults(NS_LITERAL_STRING("td"), getter_AddRefs(newCell));
if (NS_SUCCEEDED(res) && newCell)
{
if (aAfter) cellOffset++;
res = InsertNode(newCell, cellParent, cellOffset);
if(NS_FAILED(res)) break;
}
}
return res;
}
NS_IMETHODIMP
nsHTMLEditor::GetFirstRow(nsIDOMElement* aTableElement, nsIDOMNode** aRowNode)
{
NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
*aRowNode = nullptr;
NS_ENSURE_TRUE(aTableElement, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMElement> tableElement;
nsresult res = GetElementOrParentByTagName(NS_LITERAL_STRING("table"), aTableElement, getter_AddRefs(tableElement));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(tableElement, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> tableChild;
res = tableElement->GetFirstChild(getter_AddRefs(tableChild));
NS_ENSURE_SUCCESS(res, res);
while (tableChild)
{
nsCOMPtr<nsIContent> content = do_QueryInterface(tableChild);
if (content)
{
if (content->IsHTMLElement(nsGkAtoms::tr)) {
// Found a row directly under <table>
*aRowNode = tableChild;
NS_ADDREF(*aRowNode);
return NS_OK;
}
// Look for row in one of the row container elements
if (content->IsAnyOfHTMLElements(nsGkAtoms::tbody,
nsGkAtoms::thead,
nsGkAtoms::tfoot)) {
nsCOMPtr<nsIDOMNode> rowNode;
res = tableChild->GetFirstChild(getter_AddRefs(rowNode));
NS_ENSURE_SUCCESS(res, res);
// We can encounter textnodes here -- must find a row
while (rowNode && !nsHTMLEditUtils::IsTableRow(rowNode))
{
nsCOMPtr<nsIDOMNode> nextNode;
res = rowNode->GetNextSibling(getter_AddRefs(nextNode));
NS_ENSURE_SUCCESS(res, res);
rowNode = nextNode;
}
if(rowNode)
{
*aRowNode = rowNode.get();
NS_ADDREF(*aRowNode);
return NS_OK;
}
}
}
// Here if table child was a CAPTION or COLGROUP
// or child of a row parent wasn't a row (bad HTML?),
// or first child was a textnode
// Look in next table child
nsCOMPtr<nsIDOMNode> nextChild;
res = tableChild->GetNextSibling(getter_AddRefs(nextChild));
NS_ENSURE_SUCCESS(res, res);
tableChild = nextChild;
}
// If here, row was not found
return NS_EDITOR_ELEMENT_NOT_FOUND;
}
NS_IMETHODIMP
nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode)
{
NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
*aRowNode = nullptr;
NS_ENSURE_TRUE(aCurrentRowNode, NS_ERROR_NULL_POINTER);
if (!nsHTMLEditUtils::IsTableRow(aCurrentRowNode))
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMNode> nextRow;
nsresult res = aCurrentRowNode->GetNextSibling(getter_AddRefs(nextRow));
NS_ENSURE_SUCCESS(res, res);
nsCOMPtr<nsIDOMNode> nextNode;
// Skip over any textnodes here
while (nextRow && !nsHTMLEditUtils::IsTableRow(nextRow))
{
res = nextRow->GetNextSibling(getter_AddRefs(nextNode));
NS_ENSURE_SUCCESS(res, res);
nextRow = nextNode;
}
if(nextRow)
{
*aRowNode = nextRow.get();
NS_ADDREF(*aRowNode);
return NS_OK;
}
// No row found, search for rows in other table sections
nsCOMPtr<nsIDOMNode> rowParent;
res = aCurrentRowNode->GetParentNode(getter_AddRefs(rowParent));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(rowParent, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> parentSibling;
res = rowParent->GetNextSibling(getter_AddRefs(parentSibling));
NS_ENSURE_SUCCESS(res, res);
while (parentSibling)
{
res = parentSibling->GetFirstChild(getter_AddRefs(nextRow));
NS_ENSURE_SUCCESS(res, res);
// We can encounter textnodes here -- must find a row
while (nextRow && !nsHTMLEditUtils::IsTableRow(nextRow))
{
res = nextRow->GetNextSibling(getter_AddRefs(nextNode));
NS_ENSURE_SUCCESS(res, res);
nextRow = nextNode;
}
if(nextRow)
{
*aRowNode = nextRow.get();
NS_ADDREF(*aRowNode);
return NS_OK;
}
// We arrive here only if a table section has no children
// or first child of section is not a row (bad HTML or more "_moz_text" nodes!)
// So look for another section sibling
res = parentSibling->GetNextSibling(getter_AddRefs(nextNode));
NS_ENSURE_SUCCESS(res, res);
parentSibling = nextNode;
}
// If here, row was not found
return NS_EDITOR_ELEMENT_NOT_FOUND;
}
nsresult
nsHTMLEditor::GetLastCellInRow(nsIDOMNode* aRowNode, nsIDOMNode** aCellNode)
{
NS_ENSURE_TRUE(aCellNode, NS_ERROR_NULL_POINTER);
*aCellNode = nullptr;
NS_ENSURE_TRUE(aRowNode, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> rowChild;
nsresult res = aRowNode->GetLastChild(getter_AddRefs(rowChild));
NS_ENSURE_SUCCESS(res, res);
while (rowChild && !nsHTMLEditUtils::IsTableCell(rowChild))
{
// Skip over textnodes
nsCOMPtr<nsIDOMNode> previousChild;
res = rowChild->GetPreviousSibling(getter_AddRefs(previousChild));
NS_ENSURE_SUCCESS(res, res);
rowChild = previousChild;
}
if (rowChild)
{
*aCellNode = rowChild.get();
NS_ADDREF(*aCellNode);
return NS_OK;
}
// If here, cell was not found
return NS_EDITOR_ELEMENT_NOT_FOUND;
}
NS_IMETHODIMP
nsHTMLEditor::InsertTableColumn(int32_t aNumber, bool aAfter)
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> curCell;
int32_t startRowIndex, startColIndex;
nsresult res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
getter_AddRefs(curCell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(curCell, NS_EDITOR_ELEMENT_NOT_FOUND);
// Get more data for current cell (we need ROWSPAN)
int32_t curStartRowIndex, curStartColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
bool isSelected;
res = GetCellDataAt(table, startRowIndex, startColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex,
&rowSpan, &colSpan,
&actualRowSpan, &actualColSpan, &isSelected);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(curCell, NS_ERROR_FAILURE);
nsAutoEditBatch beginBatching(this);
// Prevent auto insertion of BR in new cell until we're done
nsAutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
// Use column after current cell if requested
if (aAfter)
{
startColIndex += actualColSpan;
//Detect when user is adding after a COLSPAN=0 case
// Assume they want to stop the "0" behavior and
// really add a new column. Thus we set the
// colspan to its true value
if (colSpan == 0)
SetColSpan(curCell, actualColSpan);
}
int32_t rowCount, colCount, rowIndex;
res = GetTableSize(table, &rowCount, &colCount);
NS_ENSURE_SUCCESS(res, res);
//We reset caret in destructor...
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, startColIndex, ePreviousRow, false);
//.. so suppress Rules System selection munging
nsAutoTxnsConserveSelection dontChangeSelection(this);
// If we are inserting after all existing columns
// Make sure table is "well formed"
// before appending new column
if (startColIndex >= colCount)
NormalizeTable(table);
nsCOMPtr<nsIDOMNode> rowNode;
for ( rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
if (startColIndex < colCount)
{
// We are inserting before an existing column
res = GetCellDataAt(table, rowIndex, startColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex,
&rowSpan, &colSpan,
&actualRowSpan, &actualColSpan, &isSelected);
NS_ENSURE_SUCCESS(res, res);
// Don't fail entire process if we fail to find a cell
// (may fail just in particular rows with < adequate cells per row)
if (curCell)
{
if (curStartColIndex < startColIndex)
{
// We have a cell spanning this location
// Simply increase its colspan to keep table rectangular
// Note: we do nothing if colsSpan=0,
// since it should automatically span the new column
if (colSpan > 0)
SetColSpan(curCell, colSpan+aNumber);
} else {
// Simply set selection to the current cell
// so we can let InsertTableCell() do the work
// Insert a new cell before current one
selection->Collapse(curCell, 0);
res = InsertTableCell(aNumber, false);
}
}
} else {
// Get current row and append new cells after last cell in row
if(rowIndex == 0)
res = GetFirstRow(table.get(), getter_AddRefs(rowNode));
else
{
nsCOMPtr<nsIDOMNode> nextRow;
res = GetNextRow(rowNode.get(), getter_AddRefs(nextRow));
rowNode = nextRow;
}
NS_ENSURE_SUCCESS(res, res);
if (rowNode)
{
nsCOMPtr<nsIDOMNode> lastCell;
res = GetLastCellInRow(rowNode, getter_AddRefs(lastCell));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(lastCell, NS_ERROR_FAILURE);
curCell = do_QueryInterface(lastCell);
if (curCell)
{
// Simply add same number of cells to each row
// Although tempted to check cell indexes for curCell,
// the effects of COLSPAN>1 in some cells makes this futile!
// We must use NormalizeTable first to assure
// that there are cells in each cellmap location
selection->Collapse(curCell, 0);
res = InsertTableCell(aNumber, true);
}
}
}
}
return res;
}
NS_IMETHODIMP
nsHTMLEditor::InsertTableRow(int32_t aNumber, bool aAfter)
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> curCell;
int32_t startRowIndex, startColIndex;
nsresult res = GetCellContext(nullptr,
getter_AddRefs(table),
getter_AddRefs(curCell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(curCell, NS_EDITOR_ELEMENT_NOT_FOUND);
// Get more data for current cell in row we are inserting at (we need COLSPAN)
int32_t curStartRowIndex, curStartColIndex, rowSpan, colSpan, actualRowSpan, actualColSpan;
bool isSelected;
res = GetCellDataAt(table, startRowIndex, startColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex,
&rowSpan, &colSpan,
&actualRowSpan, &actualColSpan, &isSelected);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(curCell, NS_ERROR_FAILURE);
int32_t rowCount, colCount;
res = GetTableSize(table, &rowCount, &colCount);
NS_ENSURE_SUCCESS(res, res);
nsAutoEditBatch beginBatching(this);
// Prevent auto insertion of BR in new cell until we're done
nsAutoRules beginRulesSniffing(this, EditAction::insertNode, nsIEditor::eNext);
if (aAfter)
{
// Use row after current cell
startRowIndex += actualRowSpan;
//Detect when user is adding after a ROWSPAN=0 case
// Assume they want to stop the "0" behavior and
// really add a new row. Thus we set the
// rowspan to its true value
if (rowSpan == 0)
SetRowSpan(curCell, actualRowSpan);
}
//We control selection resetting after the insert...
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, startColIndex, ePreviousColumn, false);
//...so suppress Rules System selection munging
nsAutoTxnsConserveSelection dontChangeSelection(this);
nsCOMPtr<nsIDOMElement> cellForRowParent;
int32_t cellsInRow = 0;
if (startRowIndex < rowCount)
{
// We are inserting above an existing row
// Get each cell in the insert row to adjust for COLSPAN effects while we
// count how many cells are needed
int32_t colIndex = 0;
// This returns NS_TABLELAYOUT_CELL_NOT_FOUND when we run past end of row,
// which passes the NS_SUCCEEDED macro
while ( NS_OK == GetCellDataAt(table, startRowIndex, colIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex,
&rowSpan, &colSpan,
&actualRowSpan, &actualColSpan,
&isSelected) )
{
if (curCell)
{
if (curStartRowIndex < startRowIndex)
{
// We have a cell spanning this location
// Simply increase its rowspan
//Note that if rowSpan == 0, we do nothing,
// since that cell should automatically extend into the new row
if (rowSpan > 0)
SetRowSpan(curCell, rowSpan+aNumber);
} else {
// We have a cell in the insert row
// Count the number of cells we need to add to the new row
cellsInRow += actualColSpan;
// Save cell we will use below
if (!cellForRowParent)
cellForRowParent = curCell;
}
// Next cell in row
colIndex += actualColSpan;
}
else
colIndex++;
}
} else {
// We are adding a new row after all others
// If it weren't for colspan=0 effect,
// we could simply use colCount for number of new cells...
// XXX colspan=0 support has now been removed in table layout so maybe this can be cleaned up now? (bug 1243183)
cellsInRow = colCount;
// ...but we must compensate for all cells with rowSpan = 0 in the last row
int32_t lastRow = rowCount-1;
int32_t tempColIndex = 0;
while ( NS_OK == GetCellDataAt(table, lastRow, tempColIndex,
getter_AddRefs(curCell),
&curStartRowIndex, &curStartColIndex,
&rowSpan, &colSpan,
&actualRowSpan, &actualColSpan,
&isSelected) )
{
if (rowSpan == 0)
cellsInRow -= actualColSpan;
tempColIndex += actualColSpan;
// Save cell from the last row that we will use below
if (!cellForRowParent && curStartRowIndex == lastRow)
cellForRowParent = curCell;
}
}
if (cellsInRow > 0)
{
// The row parent and offset where we will insert new row
nsCOMPtr<nsIDOMNode> parentOfRow;
int32_t newRowOffset;
NS_NAMED_LITERAL_STRING(trStr, "tr");
if (cellForRowParent)
{
nsCOMPtr<nsIDOMElement> parentRow;
res = GetElementOrParentByTagName(trStr, cellForRowParent, getter_AddRefs(parentRow));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(parentRow, NS_ERROR_NULL_POINTER);
parentRow->GetParentNode(getter_AddRefs(parentOfRow));
NS_ENSURE_TRUE(parentOfRow, NS_ERROR_NULL_POINTER);
newRowOffset = GetChildOffset(parentRow, parentOfRow);
// Adjust for when adding past the end
if (aAfter && startRowIndex >= rowCount)
newRowOffset++;
}
else
return NS_ERROR_FAILURE;
for (int32_t row = 0; row < aNumber; row++)
{
// Create a new row
nsCOMPtr<nsIDOMElement> newRow;
res = CreateElementWithDefaults(trStr, getter_AddRefs(newRow));
if (NS_SUCCEEDED(res))
{
NS_ENSURE_TRUE(newRow, NS_ERROR_FAILURE);
for (int32_t i = 0; i < cellsInRow; i++)
{
nsCOMPtr<nsIDOMElement> newCell;
res = CreateElementWithDefaults(NS_LITERAL_STRING("td"), getter_AddRefs(newCell));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(newCell, NS_ERROR_FAILURE);
// Don't use transaction system yet! (not until entire row is inserted)
nsCOMPtr<nsIDOMNode>resultNode;
res = newRow->AppendChild(newCell, getter_AddRefs(resultNode));
NS_ENSURE_SUCCESS(res, res);
}
// Use transaction system to insert the entire row+cells
// (Note that rows are inserted at same childoffset each time)
res = InsertNode(newRow, parentOfRow, newRowOffset);
NS_ENSURE_SUCCESS(res, res);
}
}
}
return res;
}
// Editor helper only
// XXX Code changed for bug 217717 and now we don't need aSelection param
// TODO: Remove aSelection param
nsresult
nsHTMLEditor::DeleteTable2(nsIDOMElement* aTable, Selection* aSelection)
{
NS_ENSURE_TRUE(aTable, NS_ERROR_NULL_POINTER);
// Select the table
nsresult res = ClearSelection();
if (NS_SUCCEEDED(res))
res = AppendNodeToSelectionAsRange(aTable);
NS_ENSURE_SUCCESS(res, res);
return DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
}
NS_IMETHODIMP
nsHTMLEditor::DeleteTable()
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsresult res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
nullptr, nullptr, nullptr, nullptr, nullptr);
NS_ENSURE_SUCCESS(res, res);
nsAutoEditBatch beginBatching(this);
return DeleteTable2(table, selection);
}
NS_IMETHODIMP
nsHTMLEditor::DeleteTableCell(int32_t aNumber)
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> cell;
int32_t startRowIndex, startColIndex;
nsresult res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
getter_AddRefs(cell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if we didn't find a table or cell
NS_ENSURE_TRUE(table && cell, NS_EDITOR_ELEMENT_NOT_FOUND);
nsAutoEditBatch beginBatching(this);
// Prevent rules testing until we're done
nsAutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
nsCOMPtr<nsIDOMElement> firstCell;
nsCOMPtr<nsIDOMRange> range;
res = GetFirstSelectedCell(getter_AddRefs(range), getter_AddRefs(firstCell));
NS_ENSURE_SUCCESS(res, res);
int32_t rangeCount;
res = selection->GetRangeCount(&rangeCount);
NS_ENSURE_SUCCESS(res, res);
if (firstCell && rangeCount > 1)
{
// When > 1 selected cell,
// ignore aNumber and use selected cells
cell = firstCell;
int32_t rowCount, colCount;
res = GetTableSize(table, &rowCount, &colCount);
NS_ENSURE_SUCCESS(res, res);
// Get indexes -- may be different than original cell
res = GetCellIndexes(cell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// The setCaret object will call SetSelectionAfterTableEdit in its destructor
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, startColIndex, ePreviousColumn, false);
nsAutoTxnsConserveSelection dontChangeSelection(this);
bool checkToDeleteRow = true;
bool checkToDeleteColumn = true;
while (cell)
{
bool deleteRow = false;
bool deleteCol = false;
if (checkToDeleteRow)
{
// Optimize to delete an entire row
// Clear so we don't repeat AllCellsInRowSelected within the same row
checkToDeleteRow = false;
deleteRow = AllCellsInRowSelected(table, startRowIndex, colCount);
if (deleteRow)
{
// First, find the next cell in a different row
// to continue after we delete this row
int32_t nextRow = startRowIndex;
while (nextRow == startRowIndex)
{
res = GetNextSelectedCell(nullptr, getter_AddRefs(cell));
NS_ENSURE_SUCCESS(res, res);
if (!cell) break;
res = GetCellIndexes(cell, &nextRow, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
}
// Delete entire row
res = DeleteRow(table, startRowIndex);
NS_ENSURE_SUCCESS(res, res);
if (cell)
{
// For the next cell: Subtract 1 for row we deleted
startRowIndex = nextRow - 1;
// Set true since we know we will look at a new row next
checkToDeleteRow = true;
}
}
}
if (!deleteRow)
{
if (checkToDeleteColumn)
{
// Optimize to delete an entire column
// Clear this so we don't repeat AllCellsInColSelected within the same Col
checkToDeleteColumn = false;
deleteCol = AllCellsInColumnSelected(table, startColIndex, colCount);
if (deleteCol)
{
// First, find the next cell in a different column
// to continue after we delete this column
int32_t nextCol = startColIndex;
while (nextCol == startColIndex)
{
res = GetNextSelectedCell(nullptr, getter_AddRefs(cell));
NS_ENSURE_SUCCESS(res, res);
if (!cell) break;
res = GetCellIndexes(cell, &startRowIndex, &nextCol);
NS_ENSURE_SUCCESS(res, res);
}
// Delete entire Col
res = DeleteColumn(table, startColIndex);
NS_ENSURE_SUCCESS(res, res);
if (cell)
{
// For the next cell, subtract 1 for col. deleted
startColIndex = nextCol - 1;
// Set true since we know we will look at a new column next
checkToDeleteColumn = true;
}
}
}
if (!deleteCol)
{
// First get the next cell to delete
nsCOMPtr<nsIDOMElement> nextCell;
res = GetNextSelectedCell(getter_AddRefs(range), getter_AddRefs(nextCell));
NS_ENSURE_SUCCESS(res, res);
// Then delete the cell
res = DeleteNode(cell);
NS_ENSURE_SUCCESS(res, res);
// The next cell to delete
cell = nextCell;
if (cell)
{
res = GetCellIndexes(cell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
}
}
}
}
}
else for (int32_t i = 0; i < aNumber; i++)
{
res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
getter_AddRefs(cell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(cell, NS_EDITOR_ELEMENT_NOT_FOUND);
if (1 == GetNumberOfCellsInRow(table, startRowIndex))
{
nsCOMPtr<nsIDOMElement> parentRow;
res = GetElementOrParentByTagName(NS_LITERAL_STRING("tr"), cell, getter_AddRefs(parentRow));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(parentRow, NS_ERROR_NULL_POINTER);
// We should delete the row instead,
// but first check if its the only row left
// so we can delete the entire table
int32_t rowCount, colCount;
res = GetTableSize(table, &rowCount, &colCount);
NS_ENSURE_SUCCESS(res, res);
if (rowCount == 1)
return DeleteTable2(table, selection);
// We need to call DeleteTableRow to handle cells with rowspan
res = DeleteTableRow(1);
NS_ENSURE_SUCCESS(res, res);
}
else
{
// More than 1 cell in the row
// The setCaret object will call SetSelectionAfterTableEdit in its destructor
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, startColIndex, ePreviousColumn, false);
nsAutoTxnsConserveSelection dontChangeSelection(this);
res = DeleteNode(cell);
// If we fail, don't try to delete any more cells???
NS_ENSURE_SUCCESS(res, res);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsHTMLEditor::DeleteTableCellContents()
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> cell;
int32_t startRowIndex, startColIndex;
nsresult res;
res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
getter_AddRefs(cell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(cell, NS_EDITOR_ELEMENT_NOT_FOUND);
nsAutoEditBatch beginBatching(this);
// Prevent rules testing until we're done
nsAutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
//Don't let Rules System change the selection
nsAutoTxnsConserveSelection dontChangeSelection(this);
nsCOMPtr<nsIDOMElement> firstCell;
nsCOMPtr<nsIDOMRange> range;
res = GetFirstSelectedCell(getter_AddRefs(range), getter_AddRefs(firstCell));
NS_ENSURE_SUCCESS(res, res);
if (firstCell)
{
cell = firstCell;
res = GetCellIndexes(cell, &startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
}
nsSetSelectionAfterTableEdit setCaret(this, table, startRowIndex, startColIndex, ePreviousColumn, false);
while (cell)
{
DeleteCellContents(cell);
if (firstCell)
{
// We doing a selected cells, so do all of them
res = GetNextSelectedCell(nullptr, getter_AddRefs(cell));
NS_ENSURE_SUCCESS(res, res);
}
else
cell = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP
nsHTMLEditor::DeleteCellContents(nsIDOMElement *aCell)
{
NS_ENSURE_TRUE(aCell, NS_ERROR_NULL_POINTER);
// Prevent rules testing until we're done
nsAutoRules beginRulesSniffing(this, EditAction::deleteNode, nsIEditor::eNext);
nsCOMPtr<nsIDOMNode> child;
bool hasChild;
aCell->HasChildNodes(&hasChild);
while (hasChild)
{
aCell->GetLastChild(getter_AddRefs(child));
nsresult res = DeleteNode(child);
NS_ENSURE_SUCCESS(res, res);
aCell->HasChildNodes(&hasChild);
}
return NS_OK;
}
NS_IMETHODIMP
nsHTMLEditor::DeleteTableColumn(int32_t aNumber)
{
RefPtr<Selection> selection;
nsCOMPtr<nsIDOMElement> table;
nsCOMPtr<nsIDOMElement> cell;
int32_t startRowIndex, startColIndex, rowCount, colCount;
nsresult res = GetCellContext(getter_AddRefs(selection),
getter_AddRefs(table),
getter_AddRefs(cell),
nullptr, nullptr,
&startRowIndex, &startColIndex);
NS_ENSURE_SUCCESS(res, res);
// Don't fail if no cell found
NS_ENSURE_TRUE(table && cell, NS_EDITOR_ELEMENT_NOT_FOUND);