forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsSelection.cpp
5727 lines (4842 loc) · 173 KB
/
nsSelection.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 -*- */
/* vim: set ts=2 sw=2 et tw=78: */
/* 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/. */
/*
* Implementation of selection: nsISelection,nsISelectionPrivate and nsFrameSelection
*/
#include "mozilla/Selection.h"
#include "mozilla/Attributes.h"
#include "nsCOMPtr.h"
#include "nsWeakReference.h"
#include "nsIFactory.h"
#include "nsIEnumerator.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsFrameSelection.h"
#include "nsISelectionListener.h"
#include "nsIComponentManager.h"
#include "nsContentCID.h"
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsRange.h"
#include "nsCOMArray.h"
#include "nsGUIEvent.h"
#include "nsIDOMKeyEvent.h"
#include "nsITableLayout.h"
#include "nsITableCellLayout.h"
#include "nsIDOMNodeList.h"
#include "nsTArray.h"
#include "nsIScrollableFrame.h"
#include "nsCCUncollectableMarker.h"
#include "nsIContentIterator.h"
#include "nsIDocumentEncoder.h"
#include "nsTextFragment.h"
// for IBMBIDI
#include "nsFrameIterator.h"
#include "nsILineIterator.h"
#include "nsGkAtoms.h"
#include "nsLayoutUtils.h"
#include "nsLayoutCID.h"
#include "nsBidiPresUtils.h"
#include "nsTextFrame.h"
#include "nsIDOMText.h"
#include "nsContentUtils.h"
#include "nsThreadUtils.h"
#include "mozilla/Preferences.h"
#include "nsDOMClassInfoID.h"
//included for desired x position;
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsCaret.h"
#include "nsITimer.h"
#include "nsIServiceManager.h"
#include "nsFrameManager.h"
// notifications
#include "nsIDOMDocument.h"
#include "nsIDocument.h"
#include "nsISelectionController.h"//for the enums
#include "nsAutoCopyListener.h"
#include "nsCopySupport.h"
#include "nsIClipboard.h"
#ifdef IBMBIDI
#include "nsIBidiKeyboard.h"
#endif // IBMBIDI
#include "nsError.h"
#include "mozilla/dom/Element.h"
using namespace mozilla;
//#define DEBUG_TABLE 1
static NS_DEFINE_IID(kCContentIteratorCID, NS_CONTENTITERATOR_CID);
//PROTOTYPES
class nsFrameSelection;
class nsAutoScrollTimer;
static bool IsValidSelectionPoint(nsFrameSelection *aFrameSel, nsINode *aNode);
static nsIAtom *GetTag(nsINode *aNode);
// returns the parent
static nsINode* ParentOffset(nsINode *aNode, int32_t *aChildOffset);
static nsINode* GetCellParent(nsINode *aDomNode);
#ifdef PRINT_RANGE
static void printRange(nsRange *aDomRange);
#define DEBUG_OUT_RANGE(x) printRange(x)
#else
#define DEBUG_OUT_RANGE(x)
#endif //MOZ_DEBUG
//#define DEBUG_SELECTION // uncomment for printf describing every collapse and extend.
//#define DEBUG_NAVIGATION
//#define DEBUG_TABLE_SELECTION 1
struct CachedOffsetForFrame {
CachedOffsetForFrame()
: mCachedFrameOffset(0, 0) // nsPoint ctor
, mLastCaretFrame(nullptr)
, mLastContentOffset(0)
, mCanCacheFrameOffset(false)
{}
nsPoint mCachedFrameOffset; // cached frame offset
nsIFrame* mLastCaretFrame; // store the frame the caret was last drawn in.
int32_t mLastContentOffset; // store last content offset
bool mCanCacheFrameOffset; // cached frame offset is valid?
};
static RangeData sEmptyData(nullptr);
// Stack-class to turn on/off selection batching for table selection
class NS_STACK_CLASS nsSelectionBatcher MOZ_FINAL
{
private:
nsCOMPtr<nsISelectionPrivate> mSelection;
public:
nsSelectionBatcher(nsISelectionPrivate *aSelection) : mSelection(aSelection)
{
if (mSelection) mSelection->StartBatchChanges();
}
~nsSelectionBatcher()
{
if (mSelection) mSelection->EndBatchChanges();
}
};
class nsAutoScrollTimer : public nsITimerCallback
{
public:
NS_DECL_ISUPPORTS
nsAutoScrollTimer()
: mFrameSelection(0), mSelection(0), mPresContext(0), mPoint(0,0), mDelay(30)
{
}
virtual ~nsAutoScrollTimer()
{
if (mTimer)
mTimer->Cancel();
}
// aPoint is relative to aPresContext's root frame
nsresult Start(nsPresContext *aPresContext, nsPoint &aPoint)
{
mPoint = aPoint;
// Store the presentation context. The timer will be
// stopped by the selection if the prescontext is destroyed.
mPresContext = aPresContext;
mContent = nsIPresShell::GetCapturingContent();
if (!mTimer)
{
nsresult result;
mTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
if (NS_FAILED(result))
return result;
}
return mTimer->InitWithCallback(this, mDelay, nsITimer::TYPE_ONE_SHOT);
}
nsresult Stop()
{
if (mTimer)
{
mTimer->Cancel();
mTimer = 0;
}
mContent = nullptr;
return NS_OK;
}
nsresult Init(nsFrameSelection* aFrameSelection, Selection* aSelection)
{
mFrameSelection = aFrameSelection;
mSelection = aSelection;
return NS_OK;
}
nsresult SetDelay(uint32_t aDelay)
{
mDelay = aDelay;
return NS_OK;
}
NS_IMETHOD Notify(nsITimer *timer)
{
if (mSelection && mPresContext)
{
nsWeakFrame frame =
mContent ? mPresContext->GetPrimaryFrameFor(mContent) : nullptr;
if (!frame)
return NS_OK;
mContent = nullptr;
nsPoint pt = mPoint -
frame->GetOffsetTo(mPresContext->PresShell()->FrameManager()->GetRootFrame());
mFrameSelection->HandleDrag(frame, pt);
if (!frame.IsAlive())
return NS_OK;
NS_ASSERTION(frame->PresContext() == mPresContext, "document mismatch?");
mSelection->DoAutoScroll(frame, pt);
}
return NS_OK;
}
private:
nsFrameSelection *mFrameSelection;
Selection* mSelection;
nsPresContext *mPresContext;
// relative to mPresContext's root frame
nsPoint mPoint;
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsIContent> mContent;
uint32_t mDelay;
};
NS_IMPL_ISUPPORTS1(nsAutoScrollTimer, nsITimerCallback)
nsresult NS_NewDomSelection(nsISelection **aDomSelection)
{
Selection* rlist = new Selection;
*aDomSelection = (nsISelection *)rlist;
NS_ADDREF(rlist);
return NS_OK;
}
static int8_t
GetIndexFromSelectionType(SelectionType aType)
{
switch (aType)
{
case nsISelectionController::SELECTION_NORMAL: return 0; break;
case nsISelectionController::SELECTION_SPELLCHECK: return 1; break;
case nsISelectionController::SELECTION_IME_RAWINPUT: return 2; break;
case nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT: return 3; break;
case nsISelectionController::SELECTION_IME_CONVERTEDTEXT: return 4; break;
case nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT: return 5; break;
case nsISelectionController::SELECTION_ACCESSIBILITY: return 6; break;
case nsISelectionController::SELECTION_FIND: return 7; break;
case nsISelectionController::SELECTION_URLSECONDARY: return 8; break;
default:
return -1; break;
}
/* NOTREACHED */
return 0;
}
static SelectionType
GetSelectionTypeFromIndex(int8_t aIndex)
{
switch (aIndex)
{
case 0: return nsISelectionController::SELECTION_NORMAL; break;
case 1: return nsISelectionController::SELECTION_SPELLCHECK; break;
case 2: return nsISelectionController::SELECTION_IME_RAWINPUT; break;
case 3: return nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT; break;
case 4: return nsISelectionController::SELECTION_IME_CONVERTEDTEXT; break;
case 5: return nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT; break;
case 6: return nsISelectionController::SELECTION_ACCESSIBILITY; break;
case 7: return nsISelectionController::SELECTION_FIND; break;
case 8: return nsISelectionController::SELECTION_URLSECONDARY; break;
default:
return nsISelectionController::SELECTION_NORMAL; break;
}
/* NOTREACHED */
return 0;
}
/*
The limiter is used specifically for the text areas and textfields
In that case it is the DIV tag that is anonymously created for the text
areas/fields. Text nodes and BR nodes fall beneath it. In the case of a
BR node the limiter will be the parent and the offset will point before or
after the BR node. In the case of the text node the parent content is
the text node itself and the offset will be the exact character position.
The offset is not important to check for validity. Simply look at the
passed in content. If it equals the limiter then the selection point is valid.
If its parent it the limiter then the point is also valid. In the case of
NO limiter all points are valid since you are in a topmost iframe. (browser
or composer)
*/
bool
IsValidSelectionPoint(nsFrameSelection *aFrameSel, nsINode *aNode)
{
if (!aFrameSel || !aNode)
return false;
nsIContent *limiter = aFrameSel->GetLimiter();
if (limiter && limiter != aNode && limiter != aNode->GetParent()) {
//if newfocus == the limiter. that's ok. but if not there and not parent bad
return false; //not in the right content. tLimiter said so
}
limiter = aFrameSel->GetAncestorLimiter();
return !limiter || nsContentUtils::ContentIsDescendantOf(aNode, limiter);
}
NS_IMPL_ADDREF(nsSelectionIterator)
NS_IMPL_RELEASE(nsSelectionIterator)
NS_INTERFACE_MAP_BEGIN(nsSelectionIterator)
NS_INTERFACE_MAP_ENTRY(nsIEnumerator)
NS_INTERFACE_MAP_ENTRY(nsIBidirectionalEnumerator)
NS_INTERFACE_MAP_END_AGGREGATED(mDomSelection)
///////////BEGIN nsSelectionIterator methods
nsSelectionIterator::nsSelectionIterator(Selection* aList)
:mIndex(0)
{
if (!aList)
{
NS_NOTREACHED("nsFrameSelection");
return;
}
mDomSelection = aList;
}
nsSelectionIterator::~nsSelectionIterator()
{
}
////////////END nsSelectionIterator methods
////////////BEGIN nsSelectionIterator methods
NS_IMETHODIMP
nsSelectionIterator::Next()
{
mIndex++;
int32_t cnt = mDomSelection->mRanges.Length();
if (mIndex < cnt)
return NS_OK;
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSelectionIterator::Prev()
{
mIndex--;
if (mIndex >= 0 )
return NS_OK;
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSelectionIterator::First()
{
if (!mDomSelection)
return NS_ERROR_NULL_POINTER;
mIndex = 0;
return NS_OK;
}
NS_IMETHODIMP
nsSelectionIterator::Last()
{
if (!mDomSelection)
return NS_ERROR_NULL_POINTER;
mIndex = mDomSelection->mRanges.Length() - 1;
return NS_OK;
}
NS_IMETHODIMP
nsSelectionIterator::CurrentItem(nsISupports **aItem)
{
*aItem = static_cast<nsIDOMRange*>(CurrentItem());
if (!*aItem) {
return NS_ERROR_FAILURE;
}
NS_ADDREF(*aItem);
return NS_OK;
}
nsRange*
nsSelectionIterator::CurrentItem()
{
return mDomSelection->mRanges.SafeElementAt(mIndex, sEmptyData).mRange;
}
NS_IMETHODIMP
nsSelectionIterator::IsDone()
{
int32_t cnt = mDomSelection->mRanges.Length();
if (mIndex >= 0 && mIndex < cnt) {
// XXX This is completely incompatible with the meaning of nsresult.
// NS_ENUMERATOR_FALSE is defined to be 1. (bug 778111)
return (nsresult)NS_ENUMERATOR_FALSE;
}
return NS_OK;
}
////////////END nsSelectionIterator methods
////////////BEGIN nsFrameSelection methods
nsFrameSelection::nsFrameSelection()
{
int32_t i;
for (i = 0;i<nsISelectionController::NUM_SELECTIONTYPES;i++){
mDomSelections[i] = new Selection(this);
mDomSelections[i]->SetType(GetSelectionTypeFromIndex(i));
}
mBatching = 0;
mChangesDuringBatching = false;
mNotifyFrames = true;
mMouseDoubleDownState = false;
mHint = HINTLEFT;
#ifdef IBMBIDI
mCaretBidiLevel = BIDI_LEVEL_UNDEFINED;
#endif
mDragSelectingCells = false;
mSelectingTableCellMode = 0;
mSelectedCellIndex = 0;
// Check to see if the autocopy pref is enabled
// and add the autocopy listener if it is
if (Preferences::GetBool("clipboard.autocopy")) {
nsAutoCopyListener *autoCopy = nsAutoCopyListener::GetInstance();
if (autoCopy) {
int8_t index =
GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
if (mDomSelections[index]) {
autoCopy->Listen(mDomSelections[index]);
}
}
}
mDisplaySelection = nsISelectionController::SELECTION_OFF;
mSelectionChangeReason = nsISelectionListener::NO_REASON;
mDelayedMouseEventValid = false;
// These values are not used since they are only valid when
// mDelayedMouseEventValid is true, and setting mDelayedMouseEventValid
//alwaysoverrides these values.
mDelayedMouseEventIsShift = false;
mDelayedMouseEventClickCount = 0;
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsFrameSelection)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsFrameSelection)
int32_t i;
for (i = 0; i < nsISelectionController::NUM_SELECTIONTYPES; ++i) {
tmp->mDomSelections[i] = nullptr;
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCellParent)
tmp->mSelectingTableCellMode = 0;
tmp->mDragSelectingCells = false;
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mStartSelectedCell)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mEndSelectedCell)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mAppendStartSelectedCell)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mUnselectCellOnMouseUp)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mMaintainRange)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mLimiter)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mAncestorLimiter)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsFrameSelection)
if (tmp->mShell && tmp->mShell->GetDocument() &&
nsCCUncollectableMarker::InGeneration(cb,
tmp->mShell->GetDocument()->
GetMarkedCCGeneration())) {
return NS_SUCCESS_INTERRUPTED_TRAVERSE;
}
int32_t i;
for (i = 0; i < nsISelectionController::NUM_SELECTIONTYPES; ++i) {
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mDomSelections[i],
nsISelection)
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCellParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mStartSelectedCell)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mEndSelectedCell)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAppendStartSelectedCell)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mUnselectCellOnMouseUp)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mMaintainRange, nsIDOMRange)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mLimiter)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAncestorLimiter)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFrameSelection)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFrameSelection)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsFrameSelection)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
nsresult
nsFrameSelection::FetchDesiredX(nscoord &aDesiredX) //the x position requested by the Key Handling for up down
{
if (!mShell)
{
NS_ERROR("fetch desired X failed");
return NS_ERROR_FAILURE;
}
if (mDesiredXSet)
{
aDesiredX = mDesiredX;
return NS_OK;
}
nsRefPtr<nsCaret> caret = mShell->GetCaret();
if (!caret)
return NS_ERROR_NULL_POINTER;
int8_t index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
nsresult result = caret->SetCaretDOMSelection(mDomSelections[index]);
if (NS_FAILED(result))
return result;
nsRect coord;
nsIFrame* caretFrame = caret->GetGeometry(mDomSelections[index], &coord);
if (!caretFrame)
return NS_ERROR_FAILURE;
nsPoint viewOffset(0, 0);
nsIView* view = nullptr;
caretFrame->GetOffsetFromView(viewOffset, &view);
if (view)
coord.x += viewOffset.x;
aDesiredX = coord.x;
return NS_OK;
}
void
nsFrameSelection::InvalidateDesiredX() //do not listen to mDesiredX you must get another.
{
mDesiredXSet = false;
}
void
nsFrameSelection::SetDesiredX(nscoord aX) //set the mDesiredX
{
mDesiredX = aX;
mDesiredXSet = true;
}
nsresult
nsFrameSelection::ConstrainFrameAndPointToAnchorSubtree(nsIFrame *aFrame,
nsPoint& aPoint,
nsIFrame **aRetFrame,
nsPoint& aRetPoint)
{
//
// The whole point of this method is to return a frame and point that
// that lie within the same valid subtree as the anchor node's frame,
// for use with the method GetContentAndOffsetsFromPoint().
//
// A valid subtree is defined to be one where all the content nodes in
// the tree have a valid parent-child relationship.
//
// If the anchor frame and aFrame are in the same subtree, aFrame will
// be returned in aRetFrame. If they are in different subtrees, we
// return the frame for the root of the subtree.
//
if (!aFrame || !aRetFrame)
return NS_ERROR_NULL_POINTER;
*aRetFrame = aFrame;
aRetPoint = aPoint;
//
// Get the frame and content for the selection's anchor point!
//
nsresult result;
nsCOMPtr<nsIDOMNode> anchorNode;
int32_t anchorOffset = 0;
int8_t index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
if (!mDomSelections[index])
return NS_ERROR_NULL_POINTER;
result = mDomSelections[index]->GetAnchorNode(getter_AddRefs(anchorNode));
if (NS_FAILED(result))
return result;
if (!anchorNode)
return NS_OK;
result = mDomSelections[index]->GetAnchorOffset(&anchorOffset);
if (NS_FAILED(result))
return result;
nsCOMPtr<nsIContent> anchorContent = do_QueryInterface(anchorNode);
if (!anchorContent)
return NS_ERROR_FAILURE;
//
// Now find the root of the subtree containing the anchor's content.
//
NS_ENSURE_STATE(mShell);
nsIContent* anchorRoot = anchorContent->GetSelectionRootContent(mShell);
NS_ENSURE_TRUE(anchorRoot, NS_ERROR_UNEXPECTED);
//
// Now find the root of the subtree containing aFrame's content.
//
nsIContent* content = aFrame->GetContent();
if (content)
{
nsIContent* contentRoot = content->GetSelectionRootContent(mShell);
NS_ENSURE_TRUE(contentRoot, NS_ERROR_UNEXPECTED);
if (anchorRoot == contentRoot)
{
// If the aFrame's content isn't the capturing content, it should be
// a descendant. At this time, we can return simply.
nsIContent* capturedContent = nsIPresShell::GetCapturingContent();
if (capturedContent != content)
{
return NS_OK;
}
// Find the frame under the mouse cursor with the root frame.
// At this time, don't use the anchor's frame because it may not have
// fixed positioned frames.
nsIFrame* rootFrame = mShell->FrameManager()->GetRootFrame();
nsPoint ptInRoot = aPoint + aFrame->GetOffsetTo(rootFrame);
nsIFrame* cursorFrame =
nsLayoutUtils::GetFrameForPoint(rootFrame, ptInRoot);
// If the mouse cursor in on a frame which is descendant of same
// selection root, we can expand the selection to the frame.
if (cursorFrame && cursorFrame->PresContext()->PresShell() == mShell)
{
nsIContent* cursorContent = cursorFrame->GetContent();
NS_ENSURE_TRUE(cursorContent, NS_ERROR_FAILURE);
nsIContent* cursorContentRoot =
cursorContent->GetSelectionRootContent(mShell);
NS_ENSURE_TRUE(cursorContentRoot, NS_ERROR_UNEXPECTED);
if (cursorContentRoot == anchorRoot)
{
*aRetFrame = cursorFrame;
aRetPoint = aPoint + aFrame->GetOffsetTo(cursorFrame);
return NS_OK;
}
}
// Otherwise, e.g., the cursor isn't on any frames (e.g., the mouse
// cursor is out of the window), we should use the frame of the anchor
// root.
}
}
//
// When we can't find a frame which is under the mouse cursor and has a same
// selection root as the anchor node's, we should return the selection root
// frame.
//
*aRetFrame = anchorRoot->GetPrimaryFrame();
if (!*aRetFrame)
return NS_ERROR_FAILURE;
//
// Now make sure that aRetPoint is converted to the same coordinate
// system used by aRetFrame.
//
aRetPoint = aPoint + aFrame->GetOffsetTo(*aRetFrame);
return NS_OK;
}
#ifdef IBMBIDI
void
nsFrameSelection::SetCaretBidiLevel(uint8_t aLevel)
{
// If the current level is undefined, we have just inserted new text.
// In this case, we don't want to reset the keyboard language
bool afterInsert = !!(mCaretBidiLevel & BIDI_LEVEL_UNDEFINED);
mCaretBidiLevel = aLevel;
nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
if (bidiKeyboard && !afterInsert)
bidiKeyboard->SetLangFromBidiLevel(aLevel);
return;
}
uint8_t
nsFrameSelection::GetCaretBidiLevel() const
{
return mCaretBidiLevel;
}
void
nsFrameSelection::UndefineCaretBidiLevel()
{
mCaretBidiLevel |= BIDI_LEVEL_UNDEFINED;
}
#endif
#ifdef PRINT_RANGE
void printRange(nsRange *aDomRange)
{
if (!aDomRange)
{
printf("NULL nsIDOMRange\n");
}
nsINode* startNode = aDomRange->GetStartParent();
nsINode* endNode = aDomRange->GetEndParent();
int32_t startOffset = aDomRange->StartOffset();
int32_t endOffset = aDomRange->EndOffset();
printf("range: 0x%lx\t start: 0x%lx %ld, \t end: 0x%lx,%ld\n",
(unsigned long)aDomRange,
(unsigned long)startNode, (long)startOffset,
(unsigned long)endNode, (long)endOffset);
}
#endif /* PRINT_RANGE */
static
nsIAtom *GetTag(nsINode *aNode)
{
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
if (!content)
{
NS_NOTREACHED("bad node passed to GetTag()");
return nullptr;
}
return content->Tag();
}
// Returns the parent
nsINode*
ParentOffset(nsINode *aNode, int32_t *aChildOffset)
{
if (!aNode || !aChildOffset)
return nullptr;
nsIContent* parent = aNode->GetParent();
if (parent)
{
*aChildOffset = parent->IndexOf(aNode);
return parent;
}
return nullptr;
}
static nsINode*
GetCellParent(nsINode *aDomNode)
{
if (!aDomNode)
return nullptr;
nsINode* current = aDomNode;
// Start with current node and look for a table cell
while (current)
{
nsIAtom* tag = GetTag(current);
if (tag == nsGkAtoms::td || tag == nsGkAtoms::th)
return current;
current = current->GetParent();
}
return nullptr;
}
void
nsFrameSelection::Init(nsIPresShell *aShell, nsIContent *aLimiter)
{
mShell = aShell;
mMouseDownState = false;
mDesiredXSet = false;
mLimiter = aLimiter;
mCaretMovementStyle =
Preferences::GetInt("bidi.edit.caret_movement_style", 2);
}
nsresult
nsFrameSelection::MoveCaret(uint32_t aKeycode,
bool aContinueSelection,
nsSelectionAmount aAmount)
{
bool visualMovement =
(aKeycode == nsIDOMKeyEvent::DOM_VK_BACK_SPACE ||
aKeycode == nsIDOMKeyEvent::DOM_VK_DELETE ||
aKeycode == nsIDOMKeyEvent::DOM_VK_HOME ||
aKeycode == nsIDOMKeyEvent::DOM_VK_END) ?
false : // Delete operations and home/end are always logical
mCaretMovementStyle == 1 ||
(mCaretMovementStyle == 2 && !aContinueSelection);
return MoveCaret(aKeycode, aContinueSelection, aAmount, visualMovement);
}
nsresult
nsFrameSelection::MoveCaret(uint32_t aKeycode,
bool aContinueSelection,
nsSelectionAmount aAmount,
bool aVisualMovement)
{
NS_ENSURE_STATE(mShell);
// Flush out layout, since we need it to be up to date to do caret
// positioning.
mShell->FlushPendingNotifications(Flush_Layout);
if (!mShell) {
return NS_OK;
}
nsPresContext *context = mShell->GetPresContext();
if (!context)
return NS_ERROR_FAILURE;
bool isCollapsed;
nscoord desiredX = 0; //we must keep this around and revalidate it when its just UP/DOWN
int8_t index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
nsRefPtr<Selection> sel = mDomSelections[index];
if (!sel)
return NS_ERROR_NULL_POINTER;
int32_t scrollFlags = 0;
nsINode* focusNode = sel->GetFocusNode();
if (focusNode &&
(focusNode->IsEditable() ||
(focusNode->IsElement() &&
focusNode->AsElement()->State().
HasState(NS_EVENT_STATE_MOZ_READWRITE)))) {
// If caret moves in editor, it should cause scrolling even if it's in
// overflow: hidden;.
scrollFlags |= Selection::SCROLL_OVERFLOW_HIDDEN;
}
nsresult result = sel->GetIsCollapsed(&isCollapsed);
if (NS_FAILED(result))
return result;
if (aKeycode == nsIDOMKeyEvent::DOM_VK_UP ||
aKeycode == nsIDOMKeyEvent::DOM_VK_DOWN)
{
result = FetchDesiredX(desiredX);
if (NS_FAILED(result))
return result;
SetDesiredX(desiredX);
}
int32_t caretStyle =
Preferences::GetInt("layout.selection.caret_style", 0);
#ifdef XP_MACOSX
if (caretStyle == 0) {
caretStyle = 2; // put caret at the selection edge in the |aKeycode| direction
}
#endif
if (!isCollapsed && !aContinueSelection && caretStyle == 2) {
switch (aKeycode){
case nsIDOMKeyEvent::DOM_VK_LEFT :
case nsIDOMKeyEvent::DOM_VK_UP :
{
const nsRange* anchorFocusRange = sel->GetAnchorFocusRange();
if (anchorFocusRange) {
sel->Collapse(anchorFocusRange->GetStartParent(),
anchorFocusRange->StartOffset());
}
mHint = HINTRIGHT;
sel->ScrollIntoView(nsISelectionController::SELECTION_FOCUS_REGION,
nsIPresShell::ScrollAxis(),
nsIPresShell::ScrollAxis(), scrollFlags);
return NS_OK;
}
case nsIDOMKeyEvent::DOM_VK_RIGHT :
case nsIDOMKeyEvent::DOM_VK_DOWN :
{
const nsRange* anchorFocusRange = sel->GetAnchorFocusRange();
if (anchorFocusRange) {
sel->Collapse(anchorFocusRange->GetEndParent(),
anchorFocusRange->EndOffset());
}
mHint = HINTLEFT;
sel->ScrollIntoView(nsISelectionController::SELECTION_FOCUS_REGION,
nsIPresShell::ScrollAxis(),
nsIPresShell::ScrollAxis(), scrollFlags);
return NS_OK;
}
}
}
nsIFrame *frame;
int32_t offsetused = 0;
result = sel->GetPrimaryFrameForFocusNode(&frame, &offsetused,
aVisualMovement);
if (NS_FAILED(result) || !frame)
return NS_FAILED(result) ? result : NS_ERROR_FAILURE;
//set data using mLimiter to stop on scroll views. If we have a limiter then we stop peeking
//when we hit scrollable views. If no limiter then just let it go ahead
nsPeekOffsetStruct pos(aAmount, eDirPrevious, offsetused, desiredX,
true, mLimiter != nullptr, true, aVisualMovement);
nsBidiLevel baseLevel = nsBidiPresUtils::GetFrameBaseLevel(frame);
HINT tHint(mHint); //temporary variable so we dont set mHint until it is necessary
switch (aKeycode){
case nsIDOMKeyEvent::DOM_VK_RIGHT :
InvalidateDesiredX();
pos.mDirection = (baseLevel & 1) ? eDirPrevious : eDirNext;
break;
case nsIDOMKeyEvent::DOM_VK_LEFT :
InvalidateDesiredX();
pos.mDirection = (baseLevel & 1) ? eDirNext : eDirPrevious;
break;
case nsIDOMKeyEvent::DOM_VK_DELETE :
InvalidateDesiredX();
pos.mDirection = eDirNext;
break;
case nsIDOMKeyEvent::DOM_VK_BACK_SPACE :
InvalidateDesiredX();
pos.mDirection = eDirPrevious;
break;
case nsIDOMKeyEvent::DOM_VK_DOWN :
pos.mAmount = eSelectLine;
pos.mDirection = eDirNext;
break;
case nsIDOMKeyEvent::DOM_VK_UP :
pos.mAmount = eSelectLine;
pos.mDirection = eDirPrevious;
break;
case nsIDOMKeyEvent::DOM_VK_HOME :
InvalidateDesiredX();
pos.mAmount = eSelectBeginLine;
break;
case nsIDOMKeyEvent::DOM_VK_END :
InvalidateDesiredX();
pos.mAmount = eSelectEndLine;
break;
default :return NS_ERROR_FAILURE;
}
PostReason(nsISelectionListener::KEYPRESS_REASON);
if (NS_SUCCEEDED(result = frame->PeekOffset(&pos)) && pos.mResultContent)