summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/events/input_event.cc
blob: 77342f8f21354e466039d0f8a6bd6a694bdf54a5 (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
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/events/input_event.h"

#include "third_party/blink/public/platform/web_editing_command_type.h"
#include "third_party/blink/renderer/core/dom/events/event_dispatcher.h"
#include "third_party/blink/renderer/core/dom/range.h"

namespace blink {

namespace {

const struct {
  InputEvent::InputType input_type;
  const char* string_name;
} kInputTypeStringNameMap[] = {
    {InputEvent::InputType::kNone, ""},
    {InputEvent::InputType::kInsertText, "insertText"},
    {InputEvent::InputType::kInsertLineBreak, "insertLineBreak"},
    {InputEvent::InputType::kInsertParagraph, "insertParagraph"},
    {InputEvent::InputType::kInsertOrderedList, "insertOrderedList"},
    {InputEvent::InputType::kInsertUnorderedList, "insertUnorderedList"},
    {InputEvent::InputType::kInsertHorizontalRule, "insertHorizontalRule"},
    {InputEvent::InputType::kInsertFromPaste, "insertFromPaste"},
    {InputEvent::InputType::kInsertFromDrop, "insertFromDrop"},
    {InputEvent::InputType::kInsertFromYank, "insertFromYank"},
    {InputEvent::InputType::kInsertTranspose, "insertTranspose"},
    {InputEvent::InputType::kInsertReplacementText, "insertReplacementText"},
    {InputEvent::InputType::kInsertCompositionText, "insertCompositionText"},
    {InputEvent::InputType::kDeleteWordBackward, "deleteWordBackward"},
    {InputEvent::InputType::kDeleteWordForward, "deleteWordForward"},
    {InputEvent::InputType::kDeleteSoftLineBackward, "deleteSoftLineBackward"},
    {InputEvent::InputType::kDeleteSoftLineForward, "deleteSoftLineForward"},
    {InputEvent::InputType::kDeleteHardLineBackward, "deleteHardLineBackward"},
    {InputEvent::InputType::kDeleteHardLineForward, "deleteHardLineForward"},
    {InputEvent::InputType::kDeleteContentBackward, "deleteContentBackward"},
    {InputEvent::InputType::kDeleteContentForward, "deleteContentForward"},
    {InputEvent::InputType::kDeleteByCut, "deleteByCut"},
    {InputEvent::InputType::kDeleteByDrag, "deleteByDrag"},
    {InputEvent::InputType::kHistoryUndo, "historyUndo"},
    {InputEvent::InputType::kHistoryRedo, "historyRedo"},
    {InputEvent::InputType::kFormatBold, "formatBold"},
    {InputEvent::InputType::kFormatItalic, "formatItalic"},
    {InputEvent::InputType::kFormatUnderline, "formatUnderline"},
    {InputEvent::InputType::kFormatStrikeThrough, "formatStrikeThrough"},
    {InputEvent::InputType::kFormatSuperscript, "formatSuperscript"},
    {InputEvent::InputType::kFormatSubscript, "formatSubscript"},
    {InputEvent::InputType::kFormatJustifyCenter, "formatJustifyCenter"},
    {InputEvent::InputType::kFormatJustifyFull, "formatJustifyFull"},
    {InputEvent::InputType::kFormatJustifyRight, "formatJustifyRight"},
    {InputEvent::InputType::kFormatJustifyLeft, "formatJustifyLeft"},
    {InputEvent::InputType::kFormatIndent, "formatIndent"},
    {InputEvent::InputType::kFormatOutdent, "formatOutdent"},
    {InputEvent::InputType::kFormatRemove, "formatRemove"},
    {InputEvent::InputType::kFormatSetBlockTextDirection,
     "formatSetBlockTextDirection"},
};

static_assert(
    arraysize(kInputTypeStringNameMap) ==
        static_cast<size_t>(InputEvent::InputType::kNumberOfInputTypes),
    "must handle all InputEvent::InputType");

String ConvertInputTypeToString(InputEvent::InputType input_type) {
  auto* const it =
      std::begin(kInputTypeStringNameMap) + static_cast<size_t>(input_type);
  if (it >= std::begin(kInputTypeStringNameMap) &&
      it < std::end(kInputTypeStringNameMap))
    return AtomicString(it->string_name);
  return g_empty_string;
}

InputEvent::InputType ConvertStringToInputType(const String& string_name) {
  // TODO(input-dev): Use binary search if the map goes larger.
  for (const auto& entry : kInputTypeStringNameMap) {
    if (string_name == entry.string_name)
      return entry.input_type;
  }
  return InputEvent::InputType::kNone;
}

}  // anonymous namespace

InputEvent::InputEvent(const AtomicString& type,
                       const InputEventInit& initializer)
    : UIEvent(type, initializer) {
  // TODO(ojan): We should find a way to prevent conversion like
  // String->enum->String just in order to use initializer.
  // See InputEvent::createBeforeInput() for the first conversion.
  if (initializer.hasInputType())
    input_type_ = ConvertStringToInputType(initializer.inputType());
  if (initializer.hasData())
    data_ = initializer.data();
  if (initializer.hasDataTransfer())
    data_transfer_ = initializer.dataTransfer();
  if (initializer.hasIsComposing())
    is_composing_ = initializer.isComposing();
  if (!initializer.hasTargetRanges())
    return;
  for (const auto& range : initializer.targetRanges())
    ranges_.push_back(range->toRange());
}

/* static */
InputEvent* InputEvent::CreateBeforeInput(InputType input_type,
                                          const String& data,
                                          EventCancelable cancelable,
                                          EventIsComposing is_composing,
                                          const StaticRangeVector* ranges) {
  InputEventInit input_event_init;

  input_event_init.setBubbles(true);
  input_event_init.setCancelable(cancelable == kIsCancelable);
  // TODO(ojan): We should find a way to prevent conversion like
  // String->enum->String just in order to use initializer.
  // See InputEvent::InputEvent() for the second conversion.
  input_event_init.setInputType(ConvertInputTypeToString(input_type));
  input_event_init.setData(data);
  input_event_init.setIsComposing(is_composing == kIsComposing);
  if (ranges)
    input_event_init.setTargetRanges(*ranges);
  input_event_init.setComposed(true);
  return InputEvent::Create(EventTypeNames::beforeinput, input_event_init);
}

/* static */
InputEvent* InputEvent::CreateBeforeInput(InputType input_type,
                                          DataTransfer* data_transfer,
                                          EventCancelable cancelable,
                                          EventIsComposing is_composing,
                                          const StaticRangeVector* ranges) {
  InputEventInit input_event_init;

  input_event_init.setBubbles(true);
  input_event_init.setCancelable(cancelable == kIsCancelable);
  input_event_init.setInputType(ConvertInputTypeToString(input_type));
  input_event_init.setDataTransfer(data_transfer);
  input_event_init.setIsComposing(is_composing == kIsComposing);
  if (ranges)
    input_event_init.setTargetRanges(*ranges);
  input_event_init.setComposed(true);
  return InputEvent::Create(EventTypeNames::beforeinput, input_event_init);
}

/* static */
InputEvent* InputEvent::CreateInput(InputType input_type,
                                    const String& data,
                                    EventIsComposing is_composing,
                                    const StaticRangeVector* ranges) {
  InputEventInit input_event_init;

  input_event_init.setBubbles(true);
  input_event_init.setCancelable(false);
  // TODO(ojan): We should find a way to prevent conversion like
  // String->enum->String just in order to use initializer.
  // See InputEvent::InputEvent() for the second conversion.
  input_event_init.setInputType(ConvertInputTypeToString(input_type));
  input_event_init.setData(data);
  input_event_init.setIsComposing(is_composing == kIsComposing);
  if (ranges)
    input_event_init.setTargetRanges(*ranges);
  input_event_init.setComposed(true);
  return InputEvent::Create(EventTypeNames::input, input_event_init);
}

String InputEvent::inputType() const {
  return ConvertInputTypeToString(input_type_);
}

StaticRangeVector InputEvent::getTargetRanges() const {
  StaticRangeVector static_ranges;
  for (const auto& range : ranges_)
    static_ranges.push_back(StaticRange::Create(range));
  return static_ranges;
}

bool InputEvent::IsInputEvent() const {
  return true;
}

void InputEvent::Trace(blink::Visitor* visitor) {
  UIEvent::Trace(visitor);
  visitor->Trace(data_transfer_);
  visitor->Trace(ranges_);
}

DispatchEventResult InputEvent::DispatchEvent(EventDispatcher& dispatcher) {
  DispatchEventResult result = dispatcher.Dispatch();
  // It's weird to hold and clear live |Range| objects internally, and only
  // expose |StaticRange| through |getTargetRanges()|. However there is no
  // better solutions due to the following issues:
  //   1. We don't want to expose live |Range| objects for the author to hold as
  //      it will slow down all DOM operations. So we just expose |StaticRange|.
  //   2. Event handlers in chain might modify DOM, which means we have to keep
  //      a copy of live |Range| internally and return snapshots.
  //   3. We don't want authors to hold live |Range| indefinitely by holding
  //      |InputEvent|, so we clear them after dispatch.
  // Authors should explicitly call |getTargetRanges()|->|toRange()| if they
  // want to keep a copy of |Range|.  See Editing TF meeting notes:
  // https://docs.google.com/document/d/1hCj6QX77NYIVY0RWrMHT1Yra6t8_Qu8PopaWLG0AM58/edit?usp=sharing
  //
  // This is the only Event::DispatchEvent() that modifies the event.
  ranges_.clear();
  return result;
}

}  // namespace blink