DOM event handling should pass Event around by reference.
[WebKit-https.git] / Source / WebCore / html / BaseChooserOnlyDateAndTimeInputType.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #if ENABLE(DATE_AND_TIME_INPUT_TYPES)
28 #include "BaseChooserOnlyDateAndTimeInputType.h"
29
30 #include "Chrome.h"
31 #include "HTMLDivElement.h"
32 #include "HTMLInputElement.h"
33 #include "Page.h"
34 #include "RenderElement.h"
35 #include "ScriptController.h"
36 #include "ShadowRoot.h"
37 #include <wtf/NeverDestroyed.h>
38
39 namespace WebCore {
40
41 BaseChooserOnlyDateAndTimeInputType::~BaseChooserOnlyDateAndTimeInputType()
42 {
43     closeDateTimeChooser();
44 }
45
46 void BaseChooserOnlyDateAndTimeInputType::handleDOMActivateEvent(Event&)
47 {
48     if (element().isDisabledOrReadOnly() || !element().renderer() || !ScriptController::processingUserGesture())
49         return;
50
51     if (m_dateTimeChooser)
52         return;
53     if (!element().document().page())
54         return;
55     DateTimeChooserParameters parameters;
56     if (!element().setupDateTimeChooserParameters(parameters))
57         return;
58 }
59
60 void BaseChooserOnlyDateAndTimeInputType::createShadowSubtree()
61 {
62     static NeverDestroyed<AtomicString> valueContainerPseudo("-webkit-date-and-time-value", AtomicString::ConstructFromLiteral);
63
64     auto valueContainer = HTMLDivElement::create(element().document());
65     valueContainer->setPseudo(valueContainerPseudo);
66     element().userAgentShadowRoot()->appendChild(valueContainer);
67     updateAppearance();
68 }
69
70 void BaseChooserOnlyDateAndTimeInputType::updateAppearance()
71 {
72     Node* node = element().userAgentShadowRoot()->firstChild();
73     if (!is<HTMLElement>(node))
74         return;
75     String displayValue = visibleValue();
76     if (displayValue.isEmpty()) {
77         // Need to put something to keep text baseline.
78         displayValue = ASCIILiteral(" ");
79     }
80     downcast<HTMLElement>(*node).setInnerText(displayValue, ASSERT_NO_EXCEPTION);
81 }
82
83 void BaseChooserOnlyDateAndTimeInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
84 {
85     BaseDateAndTimeInputType::setValue(value, valueChanged, eventBehavior);
86     if (valueChanged)
87         updateAppearance();
88 }
89
90 void BaseChooserOnlyDateAndTimeInputType::detach()
91 {
92     closeDateTimeChooser();
93 }
94
95 void BaseChooserOnlyDateAndTimeInputType::didChooseValue(const String& value)
96 {
97     element().setValue(value, DispatchInputAndChangeEvent);
98 }
99
100 void BaseChooserOnlyDateAndTimeInputType::didEndChooser()
101 {
102     m_dateTimeChooser = nullptr;
103 }
104
105 void BaseChooserOnlyDateAndTimeInputType::closeDateTimeChooser()
106 {
107     if (m_dateTimeChooser)
108         m_dateTimeChooser->endChooser();
109 }
110
111 void BaseChooserOnlyDateAndTimeInputType::handleKeydownEvent(KeyboardEvent& event)
112 {
113     BaseClickableWithKeyInputType::handleKeydownEvent(element(), event);
114 }
115
116 void BaseChooserOnlyDateAndTimeInputType::handleKeypressEvent(KeyboardEvent& event)
117 {
118     BaseClickableWithKeyInputType::handleKeypressEvent(element(), event);
119 }
120
121 void BaseChooserOnlyDateAndTimeInputType::handleKeyupEvent(KeyboardEvent& event)
122 {
123     BaseClickableWithKeyInputType::handleKeyupEvent(*this, event);
124 }
125
126 void BaseChooserOnlyDateAndTimeInputType::accessKeyAction(bool sendMouseEvents)
127 {
128     BaseDateAndTimeInputType::accessKeyAction(sendMouseEvents);
129     BaseClickableWithKeyInputType::accessKeyAction(element(), sendMouseEvents);
130 }
131
132 bool BaseChooserOnlyDateAndTimeInputType::isMouseFocusable() const
133 {
134     return element().isTextFormControlFocusable();
135 }
136
137 }
138 #endif