Pass in IsComposed flag to Event constructors
[WebKit-https.git] / Source / WebCore / dom / KeyboardEvent.cpp
1 /*
2  * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3  * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5  * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "config.h"
24 #include "KeyboardEvent.h"
25
26 #include "DOMWindow.h"
27 #include "Document.h"
28 #include "Editor.h"
29 #include "EventHandler.h"
30 #include "EventNames.h"
31 #include "Frame.h"
32 #include "PlatformKeyboardEvent.h"
33 #include "WindowsKeyboardCodes.h"
34
35 namespace WebCore {
36
37 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type type)
38 {
39     switch (type) {
40         case PlatformEvent::KeyUp:
41             return eventNames().keyupEvent;
42         case PlatformEvent::RawKeyDown:
43             return eventNames().keydownEvent;
44         case PlatformEvent::Char:
45             return eventNames().keypressEvent;
46         case PlatformEvent::KeyDown:
47             // The caller should disambiguate the combined event into RawKeyDown or Char events.
48             break;
49         default:
50             break;
51     }
52     ASSERT_NOT_REACHED();
53     return eventNames().keydownEvent;
54 }
55
56 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode)
57 {
58     switch (keycode) {
59     case VK_LCONTROL:
60     case VK_RCONTROL:
61         return VK_CONTROL;
62     case VK_LSHIFT:
63     case VK_RSHIFT:
64         return VK_SHIFT;
65     case VK_LMENU:
66     case VK_RMENU:
67         return VK_MENU;
68     default:
69         return keycode;
70     }
71 }
72
73 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key)
74 {
75     if (key.isKeypad())
76         return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD;
77
78     switch (key.windowsVirtualKeyCode()) {
79     case VK_LCONTROL:
80     case VK_LSHIFT:
81     case VK_LMENU:
82     case VK_LWIN:
83         return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
84     case VK_RCONTROL:
85     case VK_RSHIFT:
86     case VK_RMENU:
87     case VK_RWIN:
88         return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
89     default:
90         return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
91     }
92 }
93
94 inline KeyboardEvent::KeyboardEvent() = default;
95
96 inline KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, RefPtr<WindowProxy>&& view)
97     : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()), CanBubble::Yes, IsCancelable::Yes, IsComposed::Yes,
98         key.timestamp().approximateMonotonicTime(), view.copyRef(), 0, key.modifiers())
99     , m_underlyingPlatformEvent(std::make_unique<PlatformKeyboardEvent>(key))
100 #if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
101     , m_key(key.key())
102 #endif
103 #if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
104     , m_code(key.code())
105 #endif
106     , m_keyIdentifier(key.keyIdentifier())
107     , m_location(keyLocationCode(key))
108     , m_repeat(key.isAutoRepeat())
109     , m_isComposing(view && is<DOMWindow>(view->window()) && downcast<DOMWindow>(*view->window()).frame() && downcast<DOMWindow>(*view->window()).frame()->editor().hasComposition())
110 #if USE(APPKIT)
111     , m_handledByInputMethod(key.handledByInputMethod())
112     , m_keypressCommands(key.commands())
113 #endif
114 {
115 }
116
117 inline KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const Init& initializer)
118     : UIEventWithKeyState(eventType, initializer)
119 #if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
120     , m_key(initializer.key)
121 #endif
122 #if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
123     , m_code(initializer.code)
124 #endif
125     , m_keyIdentifier(initializer.keyIdentifier)
126     , m_location(initializer.keyLocation ? *initializer.keyLocation : initializer.location)
127     , m_repeat(initializer.repeat)
128     , m_isComposing(initializer.isComposing)
129     , m_charCode(initializer.charCode)
130     , m_keyCode(initializer.keyCode)
131     , m_which(initializer.which)
132 {
133 }
134
135 KeyboardEvent::~KeyboardEvent() = default;
136
137 Ref<KeyboardEvent> KeyboardEvent::create(const PlatformKeyboardEvent& platformEvent, RefPtr<WindowProxy>&& view)
138 {
139     return adoptRef(*new KeyboardEvent(platformEvent, WTFMove(view)));
140 }
141
142 Ref<KeyboardEvent> KeyboardEvent::createForBindings()
143 {
144     return adoptRef(*new KeyboardEvent);
145 }
146
147 Ref<KeyboardEvent> KeyboardEvent::create(const AtomicString& type, const Init& initializer)
148 {
149     return adoptRef(*new KeyboardEvent(type, initializer));
150 }
151
152 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, RefPtr<WindowProxy>&& view,
153     const String& keyIdentifier, unsigned location, bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
154 {
155     if (isBeingDispatched())
156         return;
157
158     initUIEvent(type, canBubble, cancelable, WTFMove(view), 0);
159
160     m_keyIdentifier = keyIdentifier;
161     m_location = location;
162
163     setModifierKeys(ctrlKey, altKey, shiftKey, metaKey, altGraphKey);
164
165     m_charCode = std::nullopt;
166     m_isComposing = false;
167     m_keyCode = std::nullopt;
168     m_repeat = false;
169     m_underlyingPlatformEvent = nullptr;
170     m_which = std::nullopt;
171
172 #if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
173     m_code = { };
174 #endif
175
176 #if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
177     m_key = { };
178 #endif
179
180 #if PLATFORM(COCOA)
181     m_handledByInputMethod = false;
182     m_keypressCommands = { };
183 #endif
184 }
185
186 int KeyboardEvent::keyCode() const
187 {
188     if (m_keyCode)
189         return m_keyCode.value();
190
191     // IE: virtual key code for keyup/keydown, character code for keypress
192     // Firefox: virtual key code for keyup/keydown, zero for keypress
193     // We match IE.
194     if (!m_underlyingPlatformEvent)
195         return 0;
196     if (type() == eventNames().keydownEvent || type() == eventNames().keyupEvent)
197         return windowsVirtualKeyCodeWithoutLocation(m_underlyingPlatformEvent->windowsVirtualKeyCode());
198
199     return charCode();
200 }
201
202 int KeyboardEvent::charCode() const
203 {
204     if (m_charCode)
205         return m_charCode.value();
206
207     // IE: not supported
208     // Firefox: 0 for keydown/keyup events, character code for keypress
209     // We match Firefox, unless in backward compatibility mode, where we always return the character code.
210     bool backwardCompatibilityMode = false;
211     auto* window = view() ? view()->window() : nullptr;
212     if (is<DOMWindow>(window) && downcast<DOMWindow>(*window).frame())
213         backwardCompatibilityMode = downcast<DOMWindow>(*window).frame()->eventHandler().needsKeyboardEventDisambiguationQuirks();
214
215     if (!m_underlyingPlatformEvent || (type() != eventNames().keypressEvent && !backwardCompatibilityMode))
216         return 0;
217     return m_underlyingPlatformEvent->text().characterStartingAt(0);
218 }
219
220 EventInterface KeyboardEvent::eventInterface() const
221 {
222     return KeyboardEventInterfaceType;
223 }
224
225 bool KeyboardEvent::isKeyboardEvent() const
226 {
227     return true;
228 }
229
230 int KeyboardEvent::which() const
231 {
232     // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
233     // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
234     if (m_which)
235         return m_which.value();
236     return keyCode();
237 }
238
239 } // namespace WebCore