2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
27 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
28 #include "core/html/shadow/DateTimeFieldElement.h"
30 #include "HTMLNames.h"
31 #include "core/dom/Text.h"
32 #include "core/events/KeyboardEvent.h"
33 #include "platform/text/PlatformLocale.h"
34 #include "wtf/text/WTFString.h"
38 using namespace HTMLNames;
40 static String emptyValueAXText()
42 return Locale::defaultLocale().queryString(blink::WebLocalizedString::AXDateTimeFieldEmptyValueText);
45 DateTimeFieldElement::FieldOwner::~FieldOwner()
49 DateTimeFieldElement::DateTimeFieldElement(Document& document, FieldOwner& fieldOwner)
50 : HTMLSpanElement(document)
51 , m_fieldOwner(&fieldOwner)
55 void DateTimeFieldElement::trace(Visitor* visitor)
57 visitor->trace(m_fieldOwner);
58 HTMLSpanElement::trace(visitor);
61 void DateTimeFieldElement::defaultEventHandler(Event* event)
63 if (event->isKeyboardEvent()) {
64 KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
65 if (!isDisabled() && !isFieldOwnerDisabled() && !isFieldOwnerReadOnly()) {
66 handleKeyboardEvent(keyboardEvent);
67 if (keyboardEvent->defaultHandled()) {
69 m_fieldOwner->fieldDidChangeValueByKeyboard();
73 defaultKeyboardEventHandler(keyboardEvent);
75 m_fieldOwner->fieldDidChangeValueByKeyboard();
76 if (keyboardEvent->defaultHandled())
80 HTMLElement::defaultEventHandler(event);
83 void DateTimeFieldElement::defaultKeyboardEventHandler(KeyboardEvent* keyboardEvent)
85 if (keyboardEvent->type() != EventTypeNames::keydown)
88 if (isDisabled() || isFieldOwnerDisabled())
91 const String& keyIdentifier = keyboardEvent->keyIdentifier();
93 if (keyIdentifier == "Left") {
96 // FIXME: We'd like to use FocusController::advanceFocus(FocusDirectionLeft, ...)
97 // but it doesn't work for shadow nodes. webkit.org/b/104650
98 if (!localeForOwner().isRTL() && m_fieldOwner->focusOnPreviousField(*this))
99 keyboardEvent->setDefaultHandled();
103 if (keyIdentifier == "Right") {
106 // FIXME: We'd like to use FocusController::advanceFocus(FocusDirectionRight, ...)
107 // but it doesn't work for shadow nodes. webkit.org/b/104650
108 if (!localeForOwner().isRTL() && m_fieldOwner->focusOnNextField(*this))
109 keyboardEvent->setDefaultHandled();
113 if (isFieldOwnerReadOnly())
116 if (keyIdentifier == "Down") {
117 if (keyboardEvent->getModifierState("Alt"))
119 keyboardEvent->setDefaultHandled();
124 if (keyIdentifier == "Up") {
125 keyboardEvent->setDefaultHandled();
130 if (keyIdentifier == "U+0008" || keyIdentifier == "U+007F") {
131 keyboardEvent->setDefaultHandled();
132 setEmptyValue(DispatchEvent);
137 void DateTimeFieldElement::setFocus(bool value)
140 value ? m_fieldOwner->didFocusOnField() : m_fieldOwner->didBlurFromField();
141 ContainerNode::setFocus(value);
144 void DateTimeFieldElement::focusOnNextField()
148 m_fieldOwner->focusOnNextField(*this);
151 void DateTimeFieldElement::initialize(const AtomicString& pseudo, const String& axHelpText, int axMinimum, int axMaximum)
153 // On accessibility, DateTimeFieldElement acts like spin button.
154 setAttribute(roleAttr, AtomicString("spinbutton", AtomicString::ConstructFromLiteral));
155 setAttribute(aria_valuetextAttr, AtomicString(emptyValueAXText()));
156 setAttribute(aria_valueminAttr, AtomicString::number(axMinimum));
157 setAttribute(aria_valuemaxAttr, AtomicString::number(axMaximum));
159 setAttribute(aria_helpAttr, AtomicString(axHelpText));
160 setShadowPseudoId(pseudo);
161 appendChild(Text::create(document(), visibleValue()));
164 bool DateTimeFieldElement::isDateTimeFieldElement() const
169 bool DateTimeFieldElement::isFieldOwnerDisabled() const
171 return m_fieldOwner && m_fieldOwner->isFieldOwnerDisabled();
174 bool DateTimeFieldElement::isFieldOwnerReadOnly() const
176 return m_fieldOwner && m_fieldOwner->isFieldOwnerReadOnly();
179 bool DateTimeFieldElement::isDisabled() const
181 return fastHasAttribute(disabledAttr);
184 Locale& DateTimeFieldElement::localeForOwner() const
186 return document().getCachedLocale(localeIdentifier());
189 AtomicString DateTimeFieldElement::localeIdentifier() const
191 return m_fieldOwner ? m_fieldOwner->localeIdentifier() : nullAtom;
194 float DateTimeFieldElement::maximumWidth(const Font&)
196 const float paddingLeftAndRight = 2; // This should match to html.css.
197 return paddingLeftAndRight;
200 void DateTimeFieldElement::setDisabled()
202 // Set HTML attribute disabled to change apperance.
203 setBooleanAttribute(disabledAttr, true);
204 setNeedsStyleRecalc(SubtreeStyleChange);
207 bool DateTimeFieldElement::supportsFocus() const
209 return !isDisabled() && !isFieldOwnerDisabled();
212 void DateTimeFieldElement::updateVisibleValue(EventBehavior eventBehavior)
214 Text* const textNode = toText(firstChild());
215 const String newVisibleValue = visibleValue();
216 ASSERT(newVisibleValue.length() > 0);
218 if (textNode->wholeText() == newVisibleValue)
221 textNode->replaceWholeText(newVisibleValue);
223 setAttribute(aria_valuetextAttr, AtomicString(newVisibleValue));
224 setAttribute(aria_valuenowAttr, AtomicString::number(valueForARIAValueNow()));
226 setAttribute(aria_valuetextAttr, AtomicString(emptyValueAXText()));
227 removeAttribute(aria_valuenowAttr);
230 if (eventBehavior == DispatchEvent && m_fieldOwner)
231 m_fieldOwner->fieldValueChanged();
234 int DateTimeFieldElement::valueForARIAValueNow() const
236 return valueAsInteger();
239 } // namespace WebCore