summaryrefslogtreecommitdiffstats
path: root/dom/base/SelectionChangeEventDispatcher.cpp
blob: a669c45d53eb9d54e43fc21a57087620beb047f6 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 mozilla::SelectionChangeEventDispatcher
 */

#include "SelectionChangeEventDispatcher.h"

#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Selection.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsFrameSelection.h"
#include "nsRange.h"

namespace mozilla {

using namespace dom;

SelectionChangeEventDispatcher::RawRangeData::RawRangeData(
    const nsRange* aRange) {
  if (aRange->IsPositioned()) {
    mStartContainer = aRange->GetStartContainer();
    mEndContainer = aRange->GetEndContainer();
    mStartOffset = aRange->StartOffset();
    mEndOffset = aRange->EndOffset();
  } else {
    mStartContainer = nullptr;
    mEndContainer = nullptr;
    mStartOffset = 0;
    mEndOffset = 0;
  }
}

bool SelectionChangeEventDispatcher::RawRangeData::Equals(
    const nsRange* aRange) {
  if (!aRange->IsPositioned()) {
    return !mStartContainer;
  }
  return mStartContainer == aRange->GetStartContainer() &&
         mEndContainer == aRange->GetEndContainer() &&
         mStartOffset == aRange->StartOffset() &&
         mEndOffset == aRange->EndOffset();
}

inline void ImplCycleCollectionTraverse(
    nsCycleCollectionTraversalCallback& aCallback,
    SelectionChangeEventDispatcher::RawRangeData& aField, const char* aName,
    uint32_t aFlags = 0) {
  ImplCycleCollectionTraverse(aCallback, aField.mStartContainer,
                              "mStartContainer", aFlags);
  ImplCycleCollectionTraverse(aCallback, aField.mEndContainer, "mEndContainer",
                              aFlags);
}

NS_IMPL_CYCLE_COLLECTION_CLASS(SelectionChangeEventDispatcher)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SelectionChangeEventDispatcher)
  tmp->mOldRanges.Clear();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SelectionChangeEventDispatcher)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOldRanges);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

void SelectionChangeEventDispatcher::OnSelectionChange(Document* aDoc,
                                                       Selection* aSel,
                                                       int16_t aReason) {
  // Check if the ranges have actually changed
  // Don't bother checking this if we are hiding changes.
  if (mOldRanges.Length() == aSel->RangeCount() &&
      !aSel->IsBlockingSelectionChangeEvents()) {
    bool changed = mOldDirection != aSel->GetDirection();
    if (!changed) {
      for (const uint32_t i : IntegerRange(mOldRanges.Length())) {
        if (!mOldRanges[i].Equals(aSel->GetRangeAt(i))) {
          changed = true;
          break;
        }
      }
    }

    if (!changed) {
      return;
    }
  }

  // The ranges have actually changed, update the mOldRanges array
  mOldRanges.ClearAndRetainStorage();
  for (const uint32_t i : IntegerRange(aSel->RangeCount())) {
    mOldRanges.AppendElement(RawRangeData(aSel->GetRangeAt(i)));
  }
  mOldDirection = aSel->GetDirection();

  // If we are hiding changes, then don't do anything else. We do this after we
  // update mOldRanges so that changes after the changes stop being hidden don't
  // incorrectly trigger a change, even though they didn't change anything
  if (aSel->IsBlockingSelectionChangeEvents()) {
    return;
  }

  const Document* doc = aSel->GetParentObject();
  if (MOZ_UNLIKELY(!doc)) {
    return;
  }
  const nsPIDOMWindowInner* inner = doc->GetInnerWindow();
  if (MOZ_UNLIKELY(!inner)) {
    return;
  }
  const bool maybeHasSelectionChangeEventListeners =
      !inner || inner->HasSelectionChangeEventListeners();
  const bool maybeHasFormSelectEventListeners =
      !inner || inner->HasFormSelectEventListeners();
  if (!maybeHasSelectionChangeEventListeners &&
      !maybeHasFormSelectEventListeners) {
    return;
  }

  // Be aware, don't call GetTextControlFromSelectionLimiter once you might
  // run script because selection limit may have already been changed by it.
  nsCOMPtr<nsIContent> textControl;
  if ((maybeHasFormSelectEventListeners &&
       (aReason & nsISelectionListener::JS_REASON)) ||
      maybeHasSelectionChangeEventListeners) {
    if (const nsFrameSelection* fs = aSel->GetFrameSelection()) {
      if (nsCOMPtr<nsIContent> root = fs->GetLimiter()) {
        textControl = root->GetClosestNativeAnonymousSubtreeRootParentOrHost();
        MOZ_ASSERT_IF(textControl,
                      textControl->IsTextControlElement() &&
                          !textControl->IsInNativeAnonymousSubtree());
      }
    }
  };

  // Selection changes with non-JS reason only cares about whether the new
  // selection is collapsed or not. See TextInputListener::OnSelectionChange.
  if (textControl && maybeHasFormSelectEventListeners &&
      (aReason & nsISelectionListener::JS_REASON)) {
    RefPtr<AsyncEventDispatcher> asyncDispatcher =
        new AsyncEventDispatcher(textControl, eFormSelect, CanBubble::eYes);
    asyncDispatcher->PostDOMEvent();
  }

  if (!maybeHasSelectionChangeEventListeners) {
    return;
  }

  // The spec currently doesn't say that we should dispatch this event on text
  // controls, so for now we only support doing that under a pref, disabled by
  // default.
  // See https://github.com/w3c/selection-api/issues/53.
  if (textControl &&
      !StaticPrefs::dom_select_events_textcontrols_selectionchange_enabled()) {
    return;
  }

  nsINode* target =
      textControl ? static_cast<nsINode*>(textControl.get()) : aDoc;
  if (target) {
    CanBubble canBubble = textControl ? CanBubble::eYes : CanBubble::eNo;
    RefPtr<AsyncEventDispatcher> asyncDispatcher =
        new AsyncEventDispatcher(target, eSelectionChange, canBubble);
    asyncDispatcher->PostDOMEvent();
  }
}

}  // namespace mozilla