File: HTMLEditHelpers.h

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (1450 lines) | stat: -rw-r--r-- 56,575 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
/* -*- 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/. */

#ifndef HTMLEditHelpers_h
#define HTMLEditHelpers_h

/**
 * This header declares/defines trivial helper classes which are used by
 * HTMLEditor.  If you want to create or look for static utility methods,
 * see HTMLEditUtils.h.
 */

#include "EditorDOMPoint.h"
#include "EditorForwards.h"
#include "EditorUtils.h"  // for CaretPoint

#include "mozilla/Attributes.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/Maybe.h"
#include "mozilla/RangeBoundary.h"
#include "mozilla/Result.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/StaticRange.h"

#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsRange.h"
#include "nsString.h"

class nsISimpleEnumerator;

namespace mozilla {

enum class BlockInlineCheck : uint8_t {
  // BlockInlineCheck is not expected by the root caller.
  Unused,
  // Refer only the HTML default style at considering whether block or inline.
  // All non-HTML elements are treated as inline.
  UseHTMLDefaultStyle,
  // Refer the element's computed style of display-outside at considering
  // whether block or inline.
  UseComputedDisplayOutsideStyle,
  // Refer the element's computed style of display at considering whether block
  // or inline.  I.e., this is a good value to look for any block boundary.
  // E.g., this is proper value when:
  // * Checking visibility of collapsible white-spaces or <br>
  // * Looking for whether a padding <br> is required
  // * Looking for a caret position
  UseComputedDisplayStyle,
  // UseComputedDisplayOutsideStyle if referring siblings or children.
  // UseComputedDisplayStyle if referring ancestors.
  Auto,
};

[[nodiscard]] inline BlockInlineCheck PreferDisplayOutsideIfUsingDisplay(
    BlockInlineCheck aBlockInlineCheck) {
  return aBlockInlineCheck == BlockInlineCheck::UseComputedDisplayStyle
             ? BlockInlineCheck::UseComputedDisplayOutsideStyle
             : aBlockInlineCheck;
}

[[nodiscard]] inline BlockInlineCheck PreferDisplayIfUsingDisplayOutside(
    BlockInlineCheck aBlockInlineCheck) {
  return aBlockInlineCheck == BlockInlineCheck::UseComputedDisplayOutsideStyle
             ? BlockInlineCheck::UseComputedDisplayStyle
             : aBlockInlineCheck;
}

[[nodiscard]] inline BlockInlineCheck UseComputedDisplayStyleIfAuto(
    BlockInlineCheck aBlockInlineCheck) {
  return aBlockInlineCheck == BlockInlineCheck::Auto
             // Treat flow-root as a block such as inline-block.
             ? BlockInlineCheck::UseComputedDisplayStyle
             : aBlockInlineCheck;
}

[[nodiscard]] inline BlockInlineCheck UseComputedDisplayOutsideStyleIfAuto(
    BlockInlineCheck aBlockInlineCheck) {
  return aBlockInlineCheck == BlockInlineCheck::Auto
             // Use display-outside for checking a sibling or child element as a
             // block.
             ? BlockInlineCheck::UseComputedDisplayOutsideStyle
             : aBlockInlineCheck;
}

enum class WithTransaction { No, Yes };
inline std::ostream& operator<<(std::ostream& aStream,
                                WithTransaction aWithTransaction) {
  aStream << "WithTransaction::"
          << (aWithTransaction == WithTransaction::Yes ? "Yes" : "No");
  return aStream;
}

/*****************************************************************************
 * MoveNodeResult is a simple class for MoveSomething() methods.
 * This stores whether it's handled or not, and next insertion point and a
 * suggestion for new caret position and the moved content range which contains
 * all content which are moved by the moves.
 *****************************************************************************/
class MOZ_STACK_CLASS MoveNodeResult final : public CaretPoint,
                                             public EditActionResult {
 public:
  constexpr const EditorDOMPoint& NextInsertionPointRef() const {
    return mNextInsertionPoint;
  }
  constexpr EditorDOMPoint&& UnwrapNextInsertionPoint() {
    return std::move(mNextInsertionPoint);
  }
  constexpr const EditorDOMRange& MovedContentRangeRef() const {
    return mMovedContentRange;
  }
  constexpr EditorDOMRange&& UnwrapMovedContentRange() {
    return std::move(mMovedContentRange);
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType NextInsertionPoint() const {
    return mNextInsertionPoint.To<EditorDOMPointType>();
  }

  MoveNodeResult(const MoveNodeResult& aOther) = delete;
  MoveNodeResult& operator=(const MoveNodeResult& aOther) = delete;
  MoveNodeResult(MoveNodeResult&& aOther) = default;
  MoveNodeResult& operator=(MoveNodeResult&& aOther) = default;

  MoveNodeResult& operator|=(const MoveNodeResult& aOther) {
    MOZ_ASSERT(this != &aOther);
    // aOther is merged with this instance so that its caret suggestion
    // shouldn't be handled anymore.
    aOther.IgnoreCaretPointSuggestion();
    // Should be handled again even if it's already handled
    UnmarkAsHandledCaretPoint();

    if (aOther.Canceled()) {
      MOZ_ASSERT_UNREACHABLE("How was aOther canceled?");
      MarkAsCanceled();
    } else if (aOther.Handled()) {
      MarkAsHandled();
      UnmarkAsCanceled();
      if (!mMovedContentRange.IsPositioned() && mNextInsertionPoint.IsSet()) {
        MOZ_ASSERT(mNextInsertionPoint.IsSetAndValid());
        mMovedContentRange.SetStartAndEnd(mNextInsertionPoint,
                                          mNextInsertionPoint);
      }
      if (aOther.mMovedContentRange.IsPositioned()) {
        mMovedContentRange.MergeWith(aOther.mMovedContentRange);
      } else if (aOther.mNextInsertionPoint.IsSet()) {
        MOZ_ASSERT(aOther.mNextInsertionPoint.IsSetAndValid());
        mMovedContentRange.MergeWith(aOther.mNextInsertionPoint);
      }
    }

    // Take the new one for the next insertion point.
    mNextInsertionPoint = aOther.mNextInsertionPoint;

    // Take the new caret point if and only if it's suggested.
    if (aOther.HasCaretPointSuggestion()) {
      SetCaretPoint(aOther.CaretPointRef());
    }
    return *this;
  }

  void ForceToMarkAsHandled() {
    if (Handled()) {
      return;
    }
    MarkAsHandled();
    if (!mMovedContentRange.IsPositioned()) {
      MOZ_ASSERT(mNextInsertionPoint.IsSetAndValidInComposedDoc());
      mMovedContentRange.SetStartAndEnd(mNextInsertionPoint,
                                        mNextInsertionPoint);
    }
  }

#ifdef DEBUG
  ~MoveNodeResult() {
    MOZ_ASSERT_IF(Handled(), !HasCaretPointSuggestion() || CaretPointHandled());
  }
#endif

  /*****************************************************************************
   * When a move node handler (or its helper) does nothing,
   * the result of these factory methods should be returned.
   * aNextInsertionPoint Must be set and valid.
   *****************************************************************************/
  static MoveNodeResult IgnoredResult(
      const EditorDOMPoint& aNextInsertionPoint) {
    return MoveNodeResult(aNextInsertionPoint, false);
  }
  static MoveNodeResult IgnoredResult(EditorDOMPoint&& aNextInsertionPoint) {
    return MoveNodeResult(std::forward<EditorDOMPoint>(aNextInsertionPoint),
                          false);
  }

  /*****************************************************************************
   * When a move node handler (or its helper) handled and not canceled,
   * the result of these factory methods should be returned.
   * aNextInsertionPoint Must be set and valid.
   *****************************************************************************/
  static MoveNodeResult HandledResult(
      const EditorDOMPoint& aNextInsertionPoint) {
    return MoveNodeResult(aNextInsertionPoint, true);
  }

  static MoveNodeResult HandledResult(EditorDOMPoint&& aNextInsertionPoint) {
    return MoveNodeResult(std::forward<EditorDOMPoint>(aNextInsertionPoint),
                          true);
  }

  static MoveNodeResult HandledResult(const EditorDOMPoint& aNextInsertionPoint,
                                      const EditorDOMPoint& aPointToPutCaret) {
    return MoveNodeResult(aNextInsertionPoint, aPointToPutCaret);
  }

  static MoveNodeResult HandledResult(EditorDOMPoint&& aNextInsertionPoint,
                                      const EditorDOMPoint& aPointToPutCaret) {
    return MoveNodeResult(std::forward<EditorDOMPoint>(aNextInsertionPoint),
                          aPointToPutCaret);
  }

  static MoveNodeResult HandledResult(const EditorDOMPoint& aNextInsertionPoint,
                                      EditorDOMPoint&& aPointToPutCaret) {
    return MoveNodeResult(aNextInsertionPoint,
                          std::forward<EditorDOMPoint>(aPointToPutCaret));
  }

  static MoveNodeResult HandledResult(EditorDOMPoint&& aNextInsertionPoint,
                                      EditorDOMPoint&& aPointToPutCaret) {
    return MoveNodeResult(std::forward<EditorDOMPoint>(aNextInsertionPoint),
                          std::forward<EditorDOMPoint>(aPointToPutCaret));
  }
  // A factory method when consecutive siblings are moved once.
  static MoveNodeResult HandledResult(const nsIContent& aFirstMovedContent,
                                      EditorDOMPoint&& aNextInsertionPoint) {
    return MoveNodeResult(aFirstMovedContent,
                          std::forward<EditorDOMPoint>(aNextInsertionPoint));
  }
  // A factory method when consecutive siblings are moved once.
  static MoveNodeResult HandledResult(const nsIContent& aFirstMovedContent,
                                      EditorDOMPoint&& aNextInsertionPoint,
                                      EditorDOMPoint&& aPointToPutCaret) {
    return MoveNodeResult(aFirstMovedContent,
                          std::forward<EditorDOMPoint>(aNextInsertionPoint),
                          std::forward<EditorDOMPoint>(aPointToPutCaret));
  }

 private:
  explicit MoveNodeResult(const EditorDOMPoint& aNextInsertionPoint,
                          bool aHandled)
      : EditActionResult(false, aHandled && aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(aNextInsertionPoint) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(EditorDOMPoint&& aNextInsertionPoint, bool aHandled)
      : EditActionResult(false, aHandled && aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(std::move(aNextInsertionPoint)) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(const EditorDOMPoint& aNextInsertionPoint,
                          const EditorDOMPoint& aPointToPutCaret)
      : CaretPoint(aPointToPutCaret),
        EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(aNextInsertionPoint) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(EditorDOMPoint&& aNextInsertionPoint,
                          const EditorDOMPoint& aPointToPutCaret)
      : CaretPoint(aPointToPutCaret),
        EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(std::move(aNextInsertionPoint)) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(const EditorDOMPoint& aNextInsertionPoint,
                          EditorDOMPoint&& aPointToPutCaret)
      : CaretPoint(std::forward<EditorDOMPoint>(aPointToPutCaret)),
        EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(aNextInsertionPoint) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(EditorDOMPoint&& aNextInsertionPoint,
                          EditorDOMPoint&& aPointToPutCaret)
      : CaretPoint(std::forward<EditorDOMPoint>(aPointToPutCaret)),
        EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(std::forward<EditorDOMPoint>(aNextInsertionPoint)) {
    if (Handled()) {
      mMovedContentRange = EditorDOMRange(mNextInsertionPoint);
    }
  }
  explicit MoveNodeResult(const nsIContent& aFirstMovedContent,
                          EditorDOMPoint&& aNextInsertionPoint)
      : EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(std::forward<EditorDOMPoint>(aNextInsertionPoint)) {
    if (Handled()) {
      EditorDOMPoint pointAfterFirstMovedContent =
          EditorDOMPoint::After(aFirstMovedContent);
      if (MOZ_LIKELY(pointAfterFirstMovedContent.EqualsOrIsBefore(
              mNextInsertionPoint))) {
        mMovedContentRange = EditorDOMRange(
            std::move(pointAfterFirstMovedContent), mNextInsertionPoint);
      } else {
        mMovedContentRange = EditorDOMRange(
            mNextInsertionPoint, std::move(pointAfterFirstMovedContent));
      }
    }
  }
  explicit MoveNodeResult(const nsIContent& aFirstMovedContent,
                          EditorDOMPoint&& aNextInsertionPoint,
                          EditorDOMPoint&& aPointToPutCaret)
      : CaretPoint(std::forward<EditorDOMPoint>(aPointToPutCaret)),
        EditActionResult(false, aNextInsertionPoint.IsSet()),
        mNextInsertionPoint(std::forward<EditorDOMPoint>(aNextInsertionPoint)) {
    if (Handled()) {
      EditorDOMPoint pointAfterFirstMovedContent =
          EditorDOMPoint::After(aFirstMovedContent);
      if (MOZ_LIKELY(pointAfterFirstMovedContent.EqualsOrIsBefore(
              mNextInsertionPoint))) {
        mMovedContentRange = EditorDOMRange(
            std::move(pointAfterFirstMovedContent), mNextInsertionPoint);
      } else {
        mMovedContentRange = EditorDOMRange(
            mNextInsertionPoint, std::move(pointAfterFirstMovedContent));
      }
    }
  }

  using EditActionResult::CanceledResult;
  using EditActionResult::MarkAsCanceled;
  using EditActionResult::MarkAsHandled;

  EditorDOMPoint mNextInsertionPoint;
  EditorDOMRange mMovedContentRange;

  friend class AutoTrackDOMMoveNodeResult;
};

/*****************************************************************************
 * DeleteRangeResult is a simple class for various delete handlers of
 * HTMLEditor.
 *****************************************************************************/
class MOZ_STACK_CLASS DeleteRangeResult final : public CaretPoint,
                                                public EditActionResult {
 public:
  DeleteRangeResult() : CaretPoint(EditorDOMPoint()) {};
  DeleteRangeResult(const EditorDOMPoint& aDeletePoint,
                    const EditorDOMPoint& aCaretPoint)
      : CaretPoint(aCaretPoint),
        EditActionResult(false, true),
        mDeleteRange(aDeletePoint) {
    MOZ_ASSERT(aDeletePoint.IsSetAndValidInComposedDoc());
    MOZ_ASSERT_IF(aCaretPoint.IsSet(),
                  aCaretPoint.IsSetAndValidInComposedDoc());
  }
  DeleteRangeResult(const EditorDOMPoint& aDeletePoint,
                    EditorDOMPoint&& aCaretPoint)
      : CaretPoint(std::move(aCaretPoint)),
        EditActionResult(false, true),
        mDeleteRange(aDeletePoint) {
    MOZ_ASSERT(aDeletePoint.IsSetAndValidInComposedDoc());
    MOZ_ASSERT_IF(HasCaretPointSuggestion(),
                  CaretPointRef().IsSetAndValidInComposedDoc());
  }
  DeleteRangeResult(const EditorDOMRange& aDeleteRange,
                    const EditorDOMPoint& aCaretPoint)
      : CaretPoint(aCaretPoint),
        EditActionResult(false, true),
        mDeleteRange(aDeleteRange) {
    MOZ_ASSERT(aDeleteRange.IsPositionedAndValid());
    MOZ_ASSERT_IF(aCaretPoint.IsSet(),
                  aCaretPoint.IsSetAndValidInComposedDoc());
  }
  DeleteRangeResult(EditorDOMRange&& aDeleteRange,
                    const EditorDOMPoint& aCaretPoint)
      : CaretPoint(aCaretPoint),
        EditActionResult(false, true),
        mDeleteRange(std::move(aDeleteRange)) {
    MOZ_ASSERT(mDeleteRange.IsPositionedAndValid());
    MOZ_ASSERT_IF(aCaretPoint.IsSet(),
                  aCaretPoint.IsSetAndValidInComposedDoc());
  }
  DeleteRangeResult(const EditorDOMRange& aDeleteRange,
                    EditorDOMPoint&& aCaretPoint)
      : CaretPoint(std::move(aCaretPoint)),
        EditActionResult(false, true),
        mDeleteRange(aDeleteRange) {
    MOZ_ASSERT(aDeleteRange.IsPositionedAndValidInComposedDoc());
    MOZ_ASSERT_IF(HasCaretPointSuggestion(),
                  CaretPointRef().IsSetAndValidInComposedDoc());
  }
  DeleteRangeResult(EditorDOMRange&& aDeleteRange, EditorDOMPoint&& aCaretPoint)
      : CaretPoint(std::move(aCaretPoint)),
        EditActionResult(false, true),
        mDeleteRange(std::move(aDeleteRange)) {
    MOZ_ASSERT(mDeleteRange.IsPositionedAndValid());
    MOZ_ASSERT_IF(HasCaretPointSuggestion(),
                  CaretPointRef().IsSetAndValidInComposedDoc());
  }

  [[nodiscard]] static DeleteRangeResult IgnoredResult() {
    return DeleteRangeResult(EditActionResult::IgnoredResult());
  }
  [[nodiscard]] static DeleteRangeResult CanceledResult() {
    return DeleteRangeResult(EditActionResult::CanceledResult());
  }

  [[nodiscard]] EditorDOMRange&& UnwrapDeleteRange() {
    return std::move(mDeleteRange);
  }
  [[nodiscard]] const EditorDOMRange& DeleteRangeRef() const {
    return mDeleteRange;
  }

  template <typename EditorDOMPointType>
  void SetDeleteRangeStart(const EditorDOMPointType& aPoint) {
    MOZ_ASSERT(aPoint.IsSetAndValidInComposedDoc());
    if (mDeleteRange.IsPositioned()) {
      mDeleteRange.SetStart(aPoint);
    } else {
      mDeleteRange.SetStartAndEnd(aPoint, aPoint);
    }
  }

  template <typename EditorDOMPointType>
  void SetDeleteRangeEnd(const EditorDOMPointType& aPoint) {
    MOZ_ASSERT(aPoint.IsSetAndValidInComposedDoc());
    if (mDeleteRange.IsPositioned()) {
      mDeleteRange.SetEnd(aPoint);
    } else {
      mDeleteRange.SetStartAndEnd(aPoint, aPoint);
    }
  }

  DeleteRangeResult& operator|=(const DeleteRangeResult& aOtherResult) {
    if (aOtherResult.Ignored() || aOtherResult.Canceled()) {
      return *this;
    }
    MarkAsHandled();
    UnmarkAsCanceled();
    if (aOtherResult.mDeleteRange.IsPositioned()) {
      mDeleteRange.MergeWith(aOtherResult.mDeleteRange);
    }
    return operator|=(static_cast<const CaretPoint&>(aOtherResult));
  }

  DeleteRangeResult& operator|=(const CaretPoint& aCaretPoint) {
    if (MOZ_UNLIKELY(!aCaretPoint.HasCaretPointSuggestion())) {
      return *this;
    }
    SetCaretPoint(aCaretPoint.CaretPointRef());
    aCaretPoint.IgnoreCaretPointSuggestion();
    return *this;
  }

 private:
  explicit DeleteRangeResult(EditActionResult&& aEditActionResult)
      : CaretPoint(EditorDOMPoint()),
        EditActionResult(std::move(aEditActionResult)) {}

  using EditActionResult::MarkAsCanceled;
  using EditActionResult::MarkAsHandled;

  EditorDOMRange mDeleteRange;

  friend class AutoTrackDOMDeleteRangeResult;
};

/*****************************************************************************
 * SplitNodeResult is a simple class for
 * HTMLEditor::SplitNodeDeepWithTransaction().
 * This makes the callers' code easier to read.
 *****************************************************************************/
class MOZ_STACK_CLASS SplitNodeResult final : public CaretPoint {
 public:
  bool Handled() const { return mPreviousNode || mNextNode; }

  /**
   * DidSplit() returns true if a node was actually split.
   */
  bool DidSplit() const { return mPreviousNode && mNextNode; }

  /**
   * GetPreviousContent() returns previous content node at the split point.
   */
  MOZ_KNOWN_LIVE nsIContent* GetPreviousContent() const {
    if (mGivenSplitPoint.IsSet()) {
      return mGivenSplitPoint.IsEndOfContainer() ? mGivenSplitPoint.GetChild()
                                                 : nullptr;
    }
    return mPreviousNode;
  }
  template <typename NodeType>
  MOZ_KNOWN_LIVE NodeType* GetPreviousContentAs() const {
    return NodeType::FromNodeOrNull(GetPreviousContent());
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtPreviousContent() const {
    if (nsIContent* previousContent = GetPreviousContent()) {
      return EditorDOMPointType(previousContent);
    }
    return EditorDOMPointType();
  }

  /**
   * GetNextContent() returns next content node at the split point.
   */
  MOZ_KNOWN_LIVE nsIContent* GetNextContent() const {
    if (mGivenSplitPoint.IsSet()) {
      return !mGivenSplitPoint.IsEndOfContainer() ? mGivenSplitPoint.GetChild()
                                                  : nullptr;
    }
    return mNextNode;
  }
  template <typename NodeType>
  MOZ_KNOWN_LIVE NodeType* GetNextContentAs() const {
    return NodeType::FromNodeOrNull(GetNextContent());
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtNextContent() const {
    if (nsIContent* nextContent = GetNextContent()) {
      return EditorDOMPointType(nextContent);
    }
    return EditorDOMPointType();
  }

  /**
   * Returns new content node which is created at splitting a node.  I.e., this
   * returns nullptr if no node was split.
   */
  MOZ_KNOWN_LIVE nsIContent* GetNewContent() const {
    if (!DidSplit()) {
      return nullptr;
    }
    return mNextNode;
  }
  template <typename NodeType>
  MOZ_KNOWN_LIVE NodeType* GetNewContentAs() const {
    return NodeType::FromNodeOrNull(GetNewContent());
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtNewContent() const {
    if (nsIContent* newContent = GetNewContent()) {
      return EditorDOMPointType(newContent);
    }
    return EditorDOMPointType();
  }

  /**
   * Returns original content node which is (or is just tried to be) split.
   */
  MOZ_KNOWN_LIVE nsIContent* GetOriginalContent() const {
    if (mGivenSplitPoint.IsSet()) {
      // Different from previous/next content, if the creator didn't split a
      // node, the container of the split point is the original node.
      return mGivenSplitPoint.GetContainerAs<nsIContent>();
    }
    return mPreviousNode ? mPreviousNode : mNextNode;
  }
  template <typename NodeType>
  MOZ_KNOWN_LIVE NodeType* GetOriginalContentAs() const {
    return NodeType::FromNodeOrNull(GetOriginalContent());
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtOriginalContent() const {
    if (nsIContent* originalContent = GetOriginalContent()) {
      return EditorDOMPointType(originalContent);
    }
    return EditorDOMPointType();
  }

  /**
   * AtSplitPoint() returns the split point in the container.
   * HTMLEditor::CreateAndInsertElement() or something similar methods.
   */
  template <typename EditorDOMPointType>
  EditorDOMPointType AtSplitPoint() const {
    if (mGivenSplitPoint.IsSet()) {
      return mGivenSplitPoint.To<EditorDOMPointType>();
    }
    if (!mPreviousNode) {
      return EditorDOMPointType(mNextNode);
    }
    return EditorDOMPointType::After(mPreviousNode);
  }

  SplitNodeResult(const SplitNodeResult&) = delete;
  SplitNodeResult& operator=(const SplitNodeResult&) = delete;
  SplitNodeResult(SplitNodeResult&&) = default;
  SplitNodeResult& operator=(SplitNodeResult&&) = default;

  /**
   * This constructor should be used for setting specific caret point instead of
   * aSplitResult's one.
   */
  SplitNodeResult(SplitNodeResult&& aSplitResult,
                  const EditorDOMPoint& aNewCaretPoint)
      : SplitNodeResult(std::move(aSplitResult)) {
    SetCaretPoint(aNewCaretPoint);
  }
  SplitNodeResult(SplitNodeResult&& aSplitResult,
                  EditorDOMPoint&& aNewCaretPoint)
      : SplitNodeResult(std::move(aSplitResult)) {
    SetCaretPoint(std::move(aNewCaretPoint));
  }

  /**
   * This constructor shouldn't be used by anybody except methods which
   * use this as result when it succeeds.
   *
   * @param aNewNode    The node which is newly created.
   * @param aSplitNode  The node which was split.
   * @param aNewCaretPoint
   *                    An optional new caret position.  If this is omitted,
   *                    the point between new node and split node will be
   *                    suggested.
   */
  SplitNodeResult(nsIContent& aNewNode, nsIContent& aSplitNode,
                  const Maybe<EditorDOMPoint>& aNewCaretPoint = Nothing())
      : CaretPoint(aNewCaretPoint.isSome()
                       ? aNewCaretPoint.ref()
                       : EditorDOMPoint::AtEndOf(aSplitNode)),
        mPreviousNode(&aSplitNode),
        mNextNode(&aNewNode) {}

  SplitNodeResult ToHandledResult() const {
    CaretPointHandled();
    SplitNodeResult result;
    result.mPreviousNode = GetPreviousContent();
    result.mNextNode = GetNextContent();
    MOZ_DIAGNOSTIC_ASSERT(result.Handled());
    // Don't recompute the caret position because in this case, split has not
    // occurred yet.  In the case,  the caller shouldn't need to update
    // selection.
    result.SetCaretPoint(CaretPointRef());
    return result;
  }

  /**
   * The following factory methods creates a SplitNodeResult instance for the
   * special cases.
   *
   * @param aDeeperSplitNodeResult
   *                    If the splitter has already split a child or a
   *                    descendant of the latest split node, the split node
   *                    result should be specified.
   */
  static inline SplitNodeResult HandledButDidNotSplitDueToEndOfContainer(
      nsIContent& aNotSplitNode,
      const SplitNodeResult* aDeeperSplitNodeResult = nullptr) {
    SplitNodeResult result;
    result.mPreviousNode = &aNotSplitNode;
    // Caret should be put at the last split point instead of current node.
    if (aDeeperSplitNodeResult) {
      result.SetCaretPoint(aDeeperSplitNodeResult->CaretPointRef());
      aDeeperSplitNodeResult->IgnoreCaretPointSuggestion();
    }
    return result;
  }

  static inline SplitNodeResult HandledButDidNotSplitDueToStartOfContainer(
      nsIContent& aNotSplitNode,
      const SplitNodeResult* aDeeperSplitNodeResult = nullptr) {
    SplitNodeResult result;
    result.mNextNode = &aNotSplitNode;
    // Caret should be put at the last split point instead of current node.
    if (aDeeperSplitNodeResult) {
      result.SetCaretPoint(aDeeperSplitNodeResult->CaretPointRef());
      aDeeperSplitNodeResult->IgnoreCaretPointSuggestion();
    }
    return result;
  }

  template <typename PT, typename CT>
  static inline SplitNodeResult NotHandled(
      const EditorDOMPointBase<PT, CT>& aGivenSplitPoint,
      const SplitNodeResult* aDeeperSplitNodeResult = nullptr) {
    SplitNodeResult result;
    result.mGivenSplitPoint = aGivenSplitPoint;
    // Caret should be put at the last split point instead of current node.
    if (aDeeperSplitNodeResult) {
      result.SetCaretPoint(aDeeperSplitNodeResult->CaretPointRef());
      aDeeperSplitNodeResult->IgnoreCaretPointSuggestion();
    }
    return result;
  }

  /**
   * Returns aSplitNodeResult as-is unless it didn't split a node but
   * aDeeperSplitNodeResult has already split a child or a descendant and has a
   * valid point to put caret around there.  In the case, this return
   * aSplitNodeResult which suggests a caret position around the last split
   * point.
   */
  static inline SplitNodeResult MergeWithDeeperSplitNodeResult(
      SplitNodeResult&& aSplitNodeResult,
      const SplitNodeResult& aDeeperSplitNodeResult) {
    aSplitNodeResult.UnmarkAsHandledCaretPoint();
    aDeeperSplitNodeResult.IgnoreCaretPointSuggestion();
    if (aSplitNodeResult.DidSplit() ||
        !aDeeperSplitNodeResult.HasCaretPointSuggestion()) {
      return std::move(aSplitNodeResult);
    }
    SplitNodeResult result(std::move(aSplitNodeResult));
    result.SetCaretPoint(aDeeperSplitNodeResult.CaretPointRef());
    return result;
  }

#ifdef DEBUG
  ~SplitNodeResult() {
    MOZ_ASSERT(!HasCaretPointSuggestion() || CaretPointHandled());
  }
#endif

 private:
  SplitNodeResult() = default;

  // When methods which return this class split some nodes actually, they
  // need to set a set of left node and right node to this class.  However,
  // one or both of them may be moved or removed by mutation observer.
  // In such case, we cannot represent the point with EditorDOMPoint since
  // it requires current container node.  Therefore, we need to use
  // nsCOMPtr<nsIContent> here instead.
  nsCOMPtr<nsIContent> mPreviousNode;
  nsCOMPtr<nsIContent> mNextNode;

  // Methods which return this class may not split any nodes actually.  Then,
  // they may want to return given split point as is since such behavior makes
  // their callers simpler.  In this case, the point may be in a text node
  // which cannot be represented as a node.  Therefore, we need EditorDOMPoint
  // for representing the point.
  EditorDOMPoint mGivenSplitPoint;
};

/*****************************************************************************
 * JoinNodesResult is a simple class for HTMLEditor::JoinNodesWithTransaction().
 * This makes the callers' code easier to read.
 *****************************************************************************/
class MOZ_STACK_CLASS JoinNodesResult final {
 public:
  MOZ_KNOWN_LIVE nsIContent* ExistingContent() const {
    return mJoinedPoint.ContainerAs<nsIContent>();
  }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtExistingContent() const {
    return EditorDOMPointType(mJoinedPoint.ContainerAs<nsIContent>());
  }

  MOZ_KNOWN_LIVE nsIContent* RemovedContent() const { return mRemovedContent; }
  template <typename EditorDOMPointType>
  EditorDOMPointType AtRemovedContent() const {
    if (mRemovedContent) {
      return EditorDOMPointType(mRemovedContent);
    }
    return EditorDOMPointType();
  }

  template <typename EditorDOMPointType>
  EditorDOMPointType AtJoinedPoint() const {
    return mJoinedPoint.To<EditorDOMPointType>();
  }

  JoinNodesResult() = delete;

  /**
   * This constructor shouldn't be used by anybody except methods which
   * use this as result when it succeeds.
   *
   * @param aJoinedPoint        First child of right node or first character.
   * @param aRemovedContent     The node which was removed from the parent.
   */
  JoinNodesResult(const EditorDOMPoint& aJoinedPoint,
                  nsIContent& aRemovedContent)
      : mJoinedPoint(aJoinedPoint), mRemovedContent(&aRemovedContent) {
    MOZ_DIAGNOSTIC_ASSERT(aJoinedPoint.IsInContentNode());
  }

  JoinNodesResult(const JoinNodesResult& aOther) = delete;
  JoinNodesResult& operator=(const JoinNodesResult& aOther) = delete;
  JoinNodesResult(JoinNodesResult&& aOther) = default;
  JoinNodesResult& operator=(JoinNodesResult&& aOther) = default;

 private:
  EditorDOMPoint mJoinedPoint;
  MOZ_KNOWN_LIVE nsCOMPtr<nsIContent> mRemovedContent;
};

/*****************************************************************************
 * SplitRangeOffFromNodeResult class is a simple class for methods which split a
 * node at 2 points for making part of the node split off from the node.
 *****************************************************************************/
class MOZ_STACK_CLASS SplitRangeOffFromNodeResult final : public CaretPoint {
 public:
  /**
   * GetLeftContent() returns new created node before the part of quarried out.
   * This may return nullptr if the method didn't split at start edge of
   * the node.
   */
  MOZ_KNOWN_LIVE nsIContent* GetLeftContent() const { return mLeftContent; }
  template <typename ContentNodeType>
  MOZ_KNOWN_LIVE ContentNodeType* GetLeftContentAs() const {
    return ContentNodeType::FromNodeOrNull(GetLeftContent());
  }
  constexpr nsCOMPtr<nsIContent>&& UnwrapLeftContent() {
    mMovedContent = true;
    return std::move(mLeftContent);
  }

  /**
   * GetMiddleContent() returns new created node between left node and right
   * node.  I.e., this is quarried out from the node.  This may return nullptr
   * if the method unwrapped the middle node.
   */
  MOZ_KNOWN_LIVE nsIContent* GetMiddleContent() const { return mMiddleContent; }
  template <typename ContentNodeType>
  MOZ_KNOWN_LIVE ContentNodeType* GetMiddleContentAs() const {
    return ContentNodeType::FromNodeOrNull(GetMiddleContent());
  }
  constexpr nsCOMPtr<nsIContent>&& UnwrapMiddleContent() {
    mMovedContent = true;
    return std::move(mMiddleContent);
  }

  /**
   * GetRightContent() returns the right node after the part of quarried out.
   * This may return nullptr it the method didn't split at end edge of the
   * node.
   */
  MOZ_KNOWN_LIVE nsIContent* GetRightContent() const { return mRightContent; }
  template <typename ContentNodeType>
  MOZ_KNOWN_LIVE ContentNodeType* GetRightContentAs() const {
    return ContentNodeType::FromNodeOrNull(GetRightContent());
  }
  constexpr nsCOMPtr<nsIContent>&& UnwrapRightContent() {
    mMovedContent = true;
    return std::move(mRightContent);
  }

  /**
   * GetLeftmostContent() returns the leftmost content after trying to
   * split twice.  If the node was not split, this returns the original node.
   */
  MOZ_KNOWN_LIVE nsIContent* GetLeftmostContent() const {
    MOZ_ASSERT(!mMovedContent);
    return mLeftContent ? mLeftContent
                        : (mMiddleContent ? mMiddleContent : mRightContent);
  }
  template <typename ContentNodeType>
  MOZ_KNOWN_LIVE ContentNodeType* GetLeftmostContentAs() const {
    return ContentNodeType::FromNodeOrNull(GetLeftmostContent());
  }

  /**
   * GetRightmostContent() returns the rightmost content after trying to
   * split twice.  If the node was not split, this returns the original node.
   */
  MOZ_KNOWN_LIVE nsIContent* GetRightmostContent() const {
    MOZ_ASSERT(!mMovedContent);
    return mRightContent ? mRightContent
                         : (mMiddleContent ? mMiddleContent : mLeftContent);
  }
  template <typename ContentNodeType>
  MOZ_KNOWN_LIVE ContentNodeType* GetRightmostContentAs() const {
    return ContentNodeType::FromNodeOrNull(GetRightmostContent());
  }

  [[nodiscard]] bool DidSplit() const { return mLeftContent || mRightContent; }

  SplitRangeOffFromNodeResult() = delete;

  SplitRangeOffFromNodeResult(nsIContent* aLeftContent,
                              nsIContent* aMiddleContent,
                              nsIContent* aRightContent)
      : mLeftContent(aLeftContent),
        mMiddleContent(aMiddleContent),
        mRightContent(aRightContent) {}

  SplitRangeOffFromNodeResult(nsIContent* aLeftContent,
                              nsIContent* aMiddleContent,
                              nsIContent* aRightContent,
                              EditorDOMPoint&& aPointToPutCaret)
      : CaretPoint(std::move(aPointToPutCaret)),
        mLeftContent(aLeftContent),
        mMiddleContent(aMiddleContent),
        mRightContent(aRightContent) {}

  SplitRangeOffFromNodeResult(const SplitRangeOffFromNodeResult& aOther) =
      delete;
  SplitRangeOffFromNodeResult& operator=(
      const SplitRangeOffFromNodeResult& aOther) = delete;
  SplitRangeOffFromNodeResult(SplitRangeOffFromNodeResult&& aOther) noexcept
      : CaretPoint(aOther.UnwrapCaretPoint()),
        mLeftContent(std::move(aOther.mLeftContent)),
        mMiddleContent(std::move(aOther.mMiddleContent)),
        mRightContent(std::move(aOther.mRightContent)) {
    MOZ_ASSERT(!aOther.mMovedContent);
  }
  SplitRangeOffFromNodeResult& operator=(SplitRangeOffFromNodeResult&& aOther) =
      delete;  // due to bug 1792638

#ifdef DEBUG
  ~SplitRangeOffFromNodeResult() {
    MOZ_ASSERT(!HasCaretPointSuggestion() || CaretPointHandled());
  }
#endif

 private:
  MOZ_KNOWN_LIVE nsCOMPtr<nsIContent> mLeftContent;
  MOZ_KNOWN_LIVE nsCOMPtr<nsIContent> mMiddleContent;
  MOZ_KNOWN_LIVE nsCOMPtr<nsIContent> mRightContent;

  bool mutable mMovedContent = false;
};

/*****************************************************************************
 * SplitRangeOffResult class is a simple class for methods which splits
 * specific ancestor elements at 2 DOM points.
 *****************************************************************************/
class MOZ_STACK_CLASS SplitRangeOffResult final : public CaretPoint {
 public:
  constexpr bool Handled() const { return mHandled; }

  /**
   * The start boundary is at the right of split at split point.  The end
   * boundary is at right node of split at end point, i.e., the end boundary
   * points out of the range to have been split off.
   */
  constexpr const EditorDOMRange& RangeRef() const { return mRange; }

  SplitRangeOffResult() = delete;

  /**
   * Constructor for success case.
   *
   * @param aTrackedRangeStart          The range whose start is at topmost
   *                                    right node child at start point if
   *                                    actually split there, or at the point
   *                                    to be tried to split, and whose end is
   *                                    at topmost right node child at end point
   *                                    if actually split there, or at the point
   *                                    to be tried to split.  Note that if the
   *                                    method allows to run script after
   *                                    splitting the range boundaries, they
   *                                    should be tracked with
   *                                    AutoTrackDOMRange.
   * @param aSplitNodeResultAtStart     Raw split node result at start point.
   * @param aSplitNodeResultAtEnd       Raw split node result at start point.
   */
  SplitRangeOffResult(EditorDOMRange&& aTrackedRange,
                      SplitNodeResult&& aSplitNodeResultAtStart,
                      SplitNodeResult&& aSplitNodeResultAtEnd)
      : mRange(std::move(aTrackedRange)),
        mHandled(aSplitNodeResultAtStart.Handled() ||
                 aSplitNodeResultAtEnd.Handled()) {
    MOZ_ASSERT(mRange.StartRef().IsSet());
    MOZ_ASSERT(mRange.EndRef().IsSet());
    // The given results are created for creating this instance so that the
    // caller may not need to handle with them.  For making who taking the
    // responsible clearer, we should move them into this constructor.
    EditorDOMPoint pointToPutCaret;
    SplitNodeResult splitNodeResultAtStart(std::move(aSplitNodeResultAtStart));
    SplitNodeResult splitNodeResultAtEnd(std::move(aSplitNodeResultAtEnd));
    splitNodeResultAtStart.MoveCaretPointTo(
        pointToPutCaret, {SuggestCaret::OnlyIfHasSuggestion});
    splitNodeResultAtEnd.MoveCaretPointTo(pointToPutCaret,
                                          {SuggestCaret::OnlyIfHasSuggestion});
    SetCaretPoint(std::move(pointToPutCaret));
  }

  SplitRangeOffResult(const SplitRangeOffResult& aOther) = delete;
  SplitRangeOffResult& operator=(const SplitRangeOffResult& aOther) = delete;
  SplitRangeOffResult(SplitRangeOffResult&& aOther) = default;
  SplitRangeOffResult& operator=(SplitRangeOffResult&& aOther) = default;

 private:
  EditorDOMRange mRange;

  // If you need to store previous and/or next node at start/end point,
  // you might be able to use `SplitNodeResult::GetPreviousNode()` etc in the
  // constructor only when `SplitNodeResult::Handled()` returns true.  But
  // the node might have gone with another DOM tree mutation.  So, be careful
  // if you do it.

  bool mHandled;
};

/******************************************************************************
 * DOM tree iterators
 *****************************************************************************/

class MOZ_RAII DOMIterator {
 public:
  explicit DOMIterator();
  explicit DOMIterator(nsINode& aNode);
  virtual ~DOMIterator() = default;

  nsresult Init(nsRange& aRange);
  nsresult Init(const RawRangeBoundary& aStartRef,
                const RawRangeBoundary& aEndRef);

  template <class NodeClass>
  void AppendAllNodesToArray(
      nsTArray<OwningNonNull<NodeClass>>& aArrayOfNodes) const;

  /**
   * AppendNodesToArray() calls aFunctor before appending found node to
   * aArrayOfNodes.  If aFunctor returns false, the node will be ignored.
   * You can use aClosure instead of capturing something with lambda.
   * Note that aNode is guaranteed that it's an instance of NodeClass
   * or its sub-class.
   * XXX If we can make type of aNode templated without std::function,
   *     it'd be better, though.
   */
  using BoolFunctor = bool (*)(nsINode& aNode, void* aClosure);
  template <class NodeClass>
  void AppendNodesToArray(BoolFunctor aFunctor,
                          nsTArray<OwningNonNull<NodeClass>>& aArrayOfNodes,
                          void* aClosure = nullptr) const;

 protected:
  SafeContentIteratorBase* mIter;
  PostContentIterator mPostOrderIter;
};

class MOZ_RAII DOMSubtreeIterator final : public DOMIterator {
 public:
  explicit DOMSubtreeIterator();
  virtual ~DOMSubtreeIterator() = default;

  nsresult Init(nsRange& aRange);

 private:
  ContentSubtreeIterator mSubtreeIter;
  explicit DOMSubtreeIterator(nsINode& aNode) = delete;
};

/******************************************************************************
 * ReplaceRangeData
 *
 * This represents range to be replaced and replacing string.
 *****************************************************************************/

template <typename EditorDOMPointType>
class MOZ_STACK_CLASS ReplaceRangeDataBase final {
 public:
  ReplaceRangeDataBase() = default;
  template <typename OtherEditorDOMRangeType>
  ReplaceRangeDataBase(const OtherEditorDOMRangeType& aRange,
                       const nsAString& aReplaceString)
      : mRange(aRange), mReplaceString(aReplaceString) {}
  template <typename StartPointType, typename EndPointType>
  ReplaceRangeDataBase(const StartPointType& aStart, const EndPointType& aEnd,
                       const nsAString& aReplaceString)
      : mRange(aStart, aEnd), mReplaceString(aReplaceString) {}

  bool IsSet() const { return mRange.IsPositioned(); }
  bool IsSetAndValid() const { return mRange.IsPositionedAndValid(); }
  bool Collapsed() const { return mRange.Collapsed(); }
  bool HasReplaceString() const { return !mReplaceString.IsEmpty(); }
  const EditorDOMPointType& StartRef() const { return mRange.StartRef(); }
  const EditorDOMPointType& EndRef() const { return mRange.EndRef(); }
  const EditorDOMRangeBase<EditorDOMPointType>& RangeRef() const {
    return mRange;
  }
  const nsString& ReplaceStringRef() const { return mReplaceString; }

  template <typename PointType>
  MOZ_NEVER_INLINE_DEBUG void SetStart(const PointType& aStart) {
    mRange.SetStart(aStart);
  }
  template <typename PointType>
  MOZ_NEVER_INLINE_DEBUG void SetEnd(const PointType& aEnd) {
    mRange.SetEnd(aEnd);
  }
  template <typename StartPointType, typename EndPointType>
  MOZ_NEVER_INLINE_DEBUG void SetStartAndEnd(const StartPointType& aStart,
                                             const EndPointType& aEnd) {
    mRange.SetRange(aStart, aEnd);
  }
  template <typename OtherEditorDOMRangeType>
  MOZ_NEVER_INLINE_DEBUG void SetRange(const OtherEditorDOMRangeType& aRange) {
    mRange = aRange;
  }
  void SetReplaceString(const nsAString& aReplaceString) {
    mReplaceString = aReplaceString;
  }
  template <typename StartPointType, typename EndPointType>
  MOZ_NEVER_INLINE_DEBUG void SetStartAndEnd(const StartPointType& aStart,
                                             const EndPointType& aEnd,
                                             const nsAString& aReplaceString) {
    SetStartAndEnd(aStart, aEnd);
    SetReplaceString(aReplaceString);
  }
  template <typename OtherEditorDOMRangeType>
  MOZ_NEVER_INLINE_DEBUG void Set(const OtherEditorDOMRangeType& aRange,
                                  const nsAString& aReplaceString) {
    SetRange(aRange);
    SetReplaceString(aReplaceString);
  }

 private:
  EditorDOMRangeBase<EditorDOMPointType> mRange;
  // This string may be used with ReplaceTextTransaction.  Therefore, for
  // avoiding memory copy, we should store it with nsString rather than
  // nsAutoString.
  nsString mReplaceString;
};

/******************************************************************************
 * EditorElementStyle represents a generic style of element
 ******************************************************************************/

class MOZ_STACK_CLASS EditorElementStyle {
 public:
#define DEFINE_FACTORY(aName, aAttr)            \
  constexpr static EditorElementStyle aName() { \
    return EditorElementStyle(*(aAttr));        \
  }

  // text-align, caption-side, a pair of margin-left and margin-right
  DEFINE_FACTORY(Align, nsGkAtoms::align)
  // background-color
  DEFINE_FACTORY(BGColor, nsGkAtoms::bgcolor)
  // background-image
  DEFINE_FACTORY(Background, nsGkAtoms::background)
  // border
  DEFINE_FACTORY(Border, nsGkAtoms::border)
  // height
  DEFINE_FACTORY(Height, nsGkAtoms::height)
  // color
  DEFINE_FACTORY(Text, nsGkAtoms::text)
  // list-style-type
  DEFINE_FACTORY(Type, nsGkAtoms::type)
  // vertical-align
  DEFINE_FACTORY(VAlign, nsGkAtoms::valign)
  // width
  DEFINE_FACTORY(Width, nsGkAtoms::width)

  static EditorElementStyle Create(const nsAtom& aAttribute) {
    MOZ_DIAGNOSTIC_ASSERT(IsHTMLStyle(&aAttribute));
    return EditorElementStyle(*aAttribute.AsStatic());
  }

  [[nodiscard]] static bool IsHTMLStyle(const nsAtom* aAttribute) {
    return aAttribute == nsGkAtoms::align || aAttribute == nsGkAtoms::bgcolor ||
           aAttribute == nsGkAtoms::background ||
           aAttribute == nsGkAtoms::border || aAttribute == nsGkAtoms::height ||
           aAttribute == nsGkAtoms::text || aAttribute == nsGkAtoms::type ||
           aAttribute == nsGkAtoms::valign || aAttribute == nsGkAtoms::width;
  }

  /**
   * Returns true if the style can be represented by CSS and it's possible to
   * apply the style with CSS.
   */
  [[nodiscard]] bool IsCSSSettable(const nsStaticAtom& aTagName) const;
  [[nodiscard]] bool IsCSSSettable(const dom::Element& aElement) const;

  /**
   * Returns true if the style can be represented by CSS and it's possible to
   * remove the style with CSS.
   */
  [[nodiscard]] bool IsCSSRemovable(const nsStaticAtom& aTagName) const;
  [[nodiscard]] bool IsCSSRemovable(const dom::Element& aElement) const;

  nsStaticAtom* Style() const { return mStyle; }

  [[nodiscard]] bool IsInlineStyle() const { return !mStyle; }
  inline EditorInlineStyle& AsInlineStyle();
  inline const EditorInlineStyle& AsInlineStyle() const;

 protected:
  MOZ_KNOWN_LIVE nsStaticAtom* mStyle = nullptr;
  EditorElementStyle() = default;

 private:
  constexpr explicit EditorElementStyle(const nsStaticAtom& aStyle)
      // Needs const_cast hack here because the this class users may want
      // non-const nsStaticAtom pointer due to bug 1794954
      : mStyle(const_cast<nsStaticAtom*>(&aStyle)) {}
};

/******************************************************************************
 * EditorInlineStyle represents an inline style.
 ******************************************************************************/

struct MOZ_STACK_CLASS EditorInlineStyle : public EditorElementStyle {
  // nullptr if you want to remove all inline styles.
  // Otherwise, one of the presentation tag names which we support in style
  // editor, and there special cases: nsGkAtoms::href means <a href="...">,
  // and nsGkAtoms::name means <a name="...">.
  MOZ_KNOWN_LIVE nsStaticAtom* const mHTMLProperty = nullptr;
  // For some mHTMLProperty values, need to be set to its attribute name.
  // E.g., nsGkAtoms::size and nsGkAtoms::face for nsGkAtoms::font.
  // Otherwise, nullptr.
  // TODO: Once we stop using these structure to wrap selected content nodes
  //       with <a href> elements, we can make this nsStaticAtom*.
  MOZ_KNOWN_LIVE const RefPtr<nsAtom> mAttribute;

  /**
   * Returns true if the style means that all inline styles should be removed.
   */
  [[nodiscard]] bool IsStyleToClearAllInlineStyles() const {
    return !mHTMLProperty;
  }

  /**
   * Returns true if the style is about <a>.
   */
  [[nodiscard]] bool IsStyleOfAnchorElement() const {
    return mHTMLProperty == nsGkAtoms::a || mHTMLProperty == nsGkAtoms::href ||
           mHTMLProperty == nsGkAtoms::name;
  }

  /**
   * Returns true if the style is invertible with CSS.
   */
  [[nodiscard]] bool IsInvertibleWithCSS() const {
    return mHTMLProperty == nsGkAtoms::b;
  }

  /**
   * Returns true if the style can be specified with text-decoration.
   */
  enum class IgnoreSElement { No, Yes };
  [[nodiscard]] bool IsStyleOfTextDecoration(
      IgnoreSElement aIgnoreSElement) const {
    return mHTMLProperty == nsGkAtoms::u ||
           mHTMLProperty == nsGkAtoms::strike ||
           (aIgnoreSElement == IgnoreSElement::No &&
            mHTMLProperty == nsGkAtoms::s);
  }

  /**
   * Returns true if the style can be represented with <font>.
   */
  [[nodiscard]] bool IsStyleOfFontElement() const {
    MOZ_ASSERT_IF(
        mHTMLProperty == nsGkAtoms::font,
        mAttribute == nsGkAtoms::bgcolor || mAttribute == nsGkAtoms::color ||
            mAttribute == nsGkAtoms::face || mAttribute == nsGkAtoms::size);
    return mHTMLProperty == nsGkAtoms::font && mAttribute != nsGkAtoms::bgcolor;
  }

  /**
   * Returns true if the style is font-size or <font size="...">.
   */
  [[nodiscard]] bool IsStyleOfFontSize() const {
    return mHTMLProperty == nsGkAtoms::font && mAttribute == nsGkAtoms::size;
  }

  /**
   * Returns true if the style is conflict with vertical-align even though
   * they are not mapped to vertical-align in the CSS mode.
   */
  [[nodiscard]] bool IsStyleConflictingWithVerticalAlign() const {
    return mHTMLProperty == nsGkAtoms::sup || mHTMLProperty == nsGkAtoms::sub;
  }

  /**
   * If the style has a similar element  which should be removed when applying
   * the style, this retuns an element name.  Otherwise, returns nullptr.
   */
  [[nodiscard]] nsStaticAtom* GetSimilarElementNameAtom() const {
    if (mHTMLProperty == nsGkAtoms::b) {
      return nsGkAtoms::strong;
    }
    if (mHTMLProperty == nsGkAtoms::i) {
      return nsGkAtoms::em;
    }
    if (mHTMLProperty == nsGkAtoms::strike) {
      return nsGkAtoms::s;
    }
    return nullptr;
  }

  /**
   * Returns true if aContent is an HTML element and represents the style.
   */
  [[nodiscard]] bool IsRepresentedBy(const nsIContent& aContent) const;

  /**
   * Returns true if aElement has style attribute and specifies this style.
   *
   * TODO: Make aElement be constant, but it needs to touch CSSEditUtils a lot.
   */
  [[nodiscard]] Result<bool, nsresult> IsSpecifiedBy(
      const HTMLEditor& aHTMLEditor, dom::Element& aElement) const;

  explicit EditorInlineStyle(const nsStaticAtom& aHTMLProperty,
                             nsAtom* aAttribute = nullptr)
      : EditorInlineStyle(aHTMLProperty, aAttribute, HasValue::No) {}
  EditorInlineStyle(const nsStaticAtom& aHTMLProperty,
                    RefPtr<nsAtom>&& aAttribute)
      : EditorInlineStyle(aHTMLProperty, aAttribute, HasValue::No) {}

  /**
   * Returns the instance which means remove all inline styles.
   */
  static EditorInlineStyle RemoveAllStyles() { return EditorInlineStyle(); }

  PendingStyleCache ToPendingStyleCache(nsAString&& aValue) const;

  bool operator==(const EditorInlineStyle& aOther) const {
    return mHTMLProperty == aOther.mHTMLProperty &&
           mAttribute == aOther.mAttribute;
  }

  bool MaybeHasValue() const { return mMaybeHasValue; }
  inline EditorInlineStyleAndValue& AsInlineStyleAndValue();
  inline const EditorInlineStyleAndValue& AsInlineStyleAndValue() const;

 protected:
  const bool mMaybeHasValue = false;

  enum class HasValue { No, Yes };
  EditorInlineStyle(const nsStaticAtom& aHTMLProperty, nsAtom* aAttribute,
                    HasValue aHasValue)
      // Needs const_cast hack here because the struct users may want
      // non-const nsStaticAtom pointer due to bug 1794954
      : mHTMLProperty(const_cast<nsStaticAtom*>(&aHTMLProperty)),
        mAttribute(aAttribute),
        mMaybeHasValue(aHasValue == HasValue::Yes) {}
  EditorInlineStyle(const nsStaticAtom& aHTMLProperty,
                    RefPtr<nsAtom>&& aAttribute, HasValue aHasValue)
      // Needs const_cast hack here because the struct users may want
      // non-const nsStaticAtom pointer due to bug 1794954
      : mHTMLProperty(const_cast<nsStaticAtom*>(&aHTMLProperty)),
        mAttribute(std::move(aAttribute)),
        mMaybeHasValue(aHasValue == HasValue::Yes) {}
  EditorInlineStyle(const EditorInlineStyle& aStyle, HasValue aHasValue)
      : mHTMLProperty(aStyle.mHTMLProperty),
        mAttribute(aStyle.mAttribute),
        mMaybeHasValue(aHasValue == HasValue::Yes) {}

 private:
  EditorInlineStyle() = default;

  using EditorElementStyle::AsInlineStyle;
  using EditorElementStyle::IsInlineStyle;
  using EditorElementStyle::Style;
};

inline EditorInlineStyle& EditorElementStyle::AsInlineStyle() {
  return reinterpret_cast<EditorInlineStyle&>(*this);
}

inline const EditorInlineStyle& EditorElementStyle::AsInlineStyle() const {
  return reinterpret_cast<const EditorInlineStyle&>(*this);
}

/******************************************************************************
 * EditorInlineStyleAndValue represents an inline style and stores its value.
 ******************************************************************************/

struct MOZ_STACK_CLASS EditorInlineStyleAndValue : public EditorInlineStyle {
  // Stores the value of mAttribute.
  nsString const mAttributeValue;

  bool IsStyleToClearAllInlineStyles() const = delete;
  EditorInlineStyleAndValue() = delete;

  explicit EditorInlineStyleAndValue(nsStaticAtom& aHTMLProperty)
      : EditorInlineStyle(aHTMLProperty, nullptr, HasValue::No) {}
  EditorInlineStyleAndValue(nsStaticAtom& aHTMLProperty, nsAtom& aAttribute,
                            const nsAString& aValue)
      : EditorInlineStyle(aHTMLProperty, &aAttribute, HasValue::Yes),
        mAttributeValue(aValue) {}
  EditorInlineStyleAndValue(nsStaticAtom& aHTMLProperty,
                            RefPtr<nsAtom>&& aAttribute,
                            const nsAString& aValue)
      : EditorInlineStyle(aHTMLProperty, std::move(aAttribute), HasValue::Yes),
        mAttributeValue(aValue) {
    MOZ_ASSERT(mAttribute);
  }
  EditorInlineStyleAndValue(nsStaticAtom& aHTMLProperty, nsAtom& aAttribute,
                            nsString&& aValue)
      : EditorInlineStyle(aHTMLProperty, &aAttribute, HasValue::Yes),
        mAttributeValue(std::move(aValue)) {}
  EditorInlineStyleAndValue(nsStaticAtom& aHTMLProperty,
                            RefPtr<nsAtom>&& aAttribute, nsString&& aValue)
      : EditorInlineStyle(aHTMLProperty, std::move(aAttribute), HasValue::Yes),
        mAttributeValue(aValue) {}

  [[nodiscard]] static EditorInlineStyleAndValue ToInvert(
      const EditorInlineStyle& aStyle) {
    MOZ_ASSERT(aStyle.IsInvertibleWithCSS());
    return EditorInlineStyleAndValue(aStyle, u"-moz-editor-invert-value"_ns);
  }

  // mHTMLProperty is never nullptr since all constructors guarantee it.
  // Therefore, hide it and expose its reference instead.
  MOZ_KNOWN_LIVE nsStaticAtom& HTMLPropertyRef() const {
    MOZ_DIAGNOSTIC_ASSERT(mHTMLProperty);
    return *mHTMLProperty;
  }

  [[nodiscard]] bool IsStyleToInvert() const {
    return mAttributeValue.EqualsLiteral(u"-moz-editor-invert-value");
  }

  /**
   * Returns true if this style is representable with HTML.
   */
  [[nodiscard]] bool IsRepresentableWithHTML() const {
    // Use background-color in any elements
    if (mAttribute == nsGkAtoms::bgcolor) {
      return false;
    }
    // Inverting the style means that it's invertible with CSS
    if (IsStyleToInvert()) {
      return false;
    }
    return true;
  }

 private:
  using EditorInlineStyle::mHTMLProperty;

  EditorInlineStyleAndValue(const EditorInlineStyle& aStyle,
                            const nsAString& aValue)
      : EditorInlineStyle(aStyle, HasValue::Yes), mAttributeValue(aValue) {}

  using EditorInlineStyle::AsInlineStyleAndValue;
  using EditorInlineStyle::HasValue;
};

inline EditorInlineStyleAndValue& EditorInlineStyle::AsInlineStyleAndValue() {
  return reinterpret_cast<EditorInlineStyleAndValue&>(*this);
}

inline const EditorInlineStyleAndValue&
EditorInlineStyle::AsInlineStyleAndValue() const {
  return reinterpret_cast<const EditorInlineStyleAndValue&>(*this);
}

}  // namespace mozilla

#endif  // #ifndef HTMLEditHelpers_h