OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/events/InputEvent.h" | 5 #include "core/events/InputEvent.h" |
6 | 6 |
| 7 #include "core/dom/Range.h" |
7 #include "core/events/EventDispatcher.h" | 8 #include "core/events/EventDispatcher.h" |
8 #include "public/platform/WebEditingCommandType.h" | 9 #include "public/platform/WebEditingCommandType.h" |
9 | 10 |
10 namespace blink { | 11 namespace blink { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 const struct { | 15 const struct { |
15 InputEvent::InputType inputType; | 16 InputEvent::InputType inputType; |
16 const char* stringName; | 17 const char* stringName; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 : UIEvent(type, initializer) | 56 : UIEvent(type, initializer) |
56 { | 57 { |
57 // TODO(ojan): We should find a way to prevent conversion like String->enum-
>String just in order to use initializer. | 58 // TODO(ojan): We should find a way to prevent conversion like String->enum-
>String just in order to use initializer. |
58 // See InputEvent::createBeforeInput() for the first conversion. | 59 // See InputEvent::createBeforeInput() for the first conversion. |
59 if (initializer.hasInputType()) | 60 if (initializer.hasInputType()) |
60 m_inputType = convertStringToInputType(initializer.inputType()); | 61 m_inputType = convertStringToInputType(initializer.inputType()); |
61 if (initializer.hasData()) | 62 if (initializer.hasData()) |
62 m_data = initializer.data(); | 63 m_data = initializer.data(); |
63 if (initializer.hasIsComposing()) | 64 if (initializer.hasIsComposing()) |
64 m_isComposing = initializer.isComposing(); | 65 m_isComposing = initializer.isComposing(); |
| 66 if (initializer.hasRanges()) |
| 67 m_ranges = initializer.ranges(); |
65 } | 68 } |
66 | 69 |
67 /* static */ | 70 /* static */ |
68 InputEvent* InputEvent::createBeforeInput(InputType inputType, const String& dat
a, EventCancelable cancelable, EventIsComposing isComposing) | 71 InputEvent* InputEvent::createBeforeInput(InputType inputType, const String& dat
a, EventCancelable cancelable, EventIsComposing isComposing, const RangeVector*
ranges) |
69 { | 72 { |
70 InputEventInit inputEventInit; | 73 InputEventInit inputEventInit; |
71 | 74 |
72 inputEventInit.setBubbles(true); | 75 inputEventInit.setBubbles(true); |
73 inputEventInit.setCancelable(cancelable == IsCancelable); | 76 inputEventInit.setCancelable(cancelable == IsCancelable); |
74 // TODO(ojan): We should find a way to prevent conversion like String->enum-
>String just in order to use initializer. | 77 // TODO(ojan): We should find a way to prevent conversion like String->enum-
>String just in order to use initializer. |
75 // See InputEvent::InputEvent() for the second conversion. | 78 // See InputEvent::InputEvent() for the second conversion. |
76 inputEventInit.setInputType(convertInputTypeToString(inputType)); | 79 inputEventInit.setInputType(convertInputTypeToString(inputType)); |
77 inputEventInit.setData(data); | 80 inputEventInit.setData(data); |
78 inputEventInit.setIsComposing(isComposing == IsComposing); | 81 inputEventInit.setIsComposing(isComposing == IsComposing); |
| 82 if (ranges) |
| 83 inputEventInit.setRanges(*ranges); |
79 | 84 |
80 return InputEvent::create(EventTypeNames::beforeinput, inputEventInit); | 85 return InputEvent::create(EventTypeNames::beforeinput, inputEventInit); |
81 } | 86 } |
82 | 87 |
83 String InputEvent::inputType() const | 88 String InputEvent::inputType() const |
84 { | 89 { |
85 return convertInputTypeToString(m_inputType); | 90 return convertInputTypeToString(m_inputType); |
86 } | 91 } |
87 | 92 |
88 bool InputEvent::isInputEvent() const | 93 bool InputEvent::isInputEvent() const |
89 { | 94 { |
90 return true; | 95 return true; |
91 } | 96 } |
92 | 97 |
| 98 // TODO(chongz): We should get rid of this |EventDispatchMediator| pattern and i
ntroduce |
| 99 // simpler interface such as |beforeDispatchEvent()| and |afterDispatchEvent()|
virtual methods. |
| 100 EventDispatchMediator* InputEvent::createMediator() |
| 101 { |
| 102 return InputEventDispatchMediator::create(this); |
| 103 } |
| 104 |
93 DEFINE_TRACE(InputEvent) | 105 DEFINE_TRACE(InputEvent) |
94 { | 106 { |
95 UIEvent::trace(visitor); | 107 UIEvent::trace(visitor); |
| 108 visitor->trace(m_ranges); |
| 109 } |
| 110 |
| 111 InputEventDispatchMediator* InputEventDispatchMediator::create(InputEvent* input
Event) |
| 112 { |
| 113 return new InputEventDispatchMediator(inputEvent); |
| 114 } |
| 115 |
| 116 InputEventDispatchMediator::InputEventDispatchMediator(InputEvent* inputEvent) |
| 117 : EventDispatchMediator(inputEvent) |
| 118 { |
| 119 } |
| 120 |
| 121 InputEvent& InputEventDispatchMediator::event() const |
| 122 { |
| 123 return toInputEvent(EventDispatchMediator::event()); |
| 124 } |
| 125 |
| 126 DispatchEventResult InputEventDispatchMediator::dispatchEvent(EventDispatcher& d
ispatcher) const |
| 127 { |
| 128 DispatchEventResult result = dispatcher.dispatch(); |
| 129 // It's weird to hold and clear live |Range| objects internally, and only ex
pose |StaticRange| |
| 130 // through |getRanges()|. However there is no better solutions due to the fo
llowing issues: |
| 131 // 1. We don't want to expose live |Range| objects for the author to hold
as it will slow down |
| 132 // all DOM operations. So we just expose |StaticRange|. |
| 133 // 2. Event handlers in chain might modify DOM, which means we have to kee
p a copy of live |
| 134 // |Range| internally and return snapshots. |
| 135 // 3. We don't want authors to hold live |Range| indefinitely by holding |
InputEvent|, so we |
| 136 // clear them after dispatch. |
| 137 // Authors should explicitly call |getRanges()|->|toRange()| if they want to
keep a copy of |Range|. |
| 138 // See Editing TF meeting notes: |
| 139 // https://docs.google.com/document/d/1hCj6QX77NYIVY0RWrMHT1Yra6t8_Qu8PopaWL
G0AM58/edit?usp=sharing |
| 140 event().m_ranges.clear(); |
| 141 return result; |
96 } | 142 } |
97 | 143 |
98 } // namespace blink | 144 } // namespace blink |
OLD | NEW |