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 "core/HTMLNames.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/Text.h"
33 #include "core/events/KeyboardEvent.h"
34 #include "platform/text/PlatformLocale.h"
35 #include "wtf/text/WTFString.h"
39 using namespace HTMLNames;
41 static String emptyValueAXText()
43 return Locale::defaultLocale().queryString(blink::WebLocalizedString::AXDateTimeFieldEmptyValueText);
46 DateTimeFieldElement::FieldOwner::~FieldOwner()
50 DateTimeFieldElement::DateTimeFieldElement(Document& document, FieldOwner& fieldOwner)
51 : HTMLSpanElement(document)
52 , m_fieldOwner(&fieldOwner)
56 void DateTimeFieldElement::trace(Visitor* visitor)
58 visitor->trace(m_fieldOwner);
59 HTMLSpanElement::trace(visitor);
62 void DateTimeFieldElement::defaultEventHandler(Event* event)
64 if (event->isKeyboardEvent()) {
65 KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
66 if (!isDisabled() && !isFieldOwnerDisabled() && !isFieldOwnerReadOnly()) {
67 handleKeyboardEvent(keyboardEvent);
68 if (keyboardEvent->defaultHandled()) {
70 m_fieldOwner->fieldDidChangeValueByKeyboard();
74 defaultKeyboardEventHandler(keyboardEvent);
76 m_fieldOwner->fieldDidChangeValueByKeyboard();
77 if (keyboardEvent->defaultHandled())
81 HTMLElement::defaultEventHandler(event);
84 void DateTimeFieldElement::defaultKeyboardEventHandler(KeyboardEvent* keyboardEvent)
86 if (keyboardEvent->type() != EventTypeNames::keydown)
89 if (isDisabled() || isFieldOwnerDisabled())
92 const String& keyIdentifier = keyboardEvent->keyIdentifier();
94 if (keyIdentifier == "Left") {
97 // FIXME: We'd like to use FocusController::advanceFocus(FocusDirectionLeft, ...)
98 // but it doesn't work for shadow nodes. webkit.org/b/104650
99 if (!localeForOwner().isRTL() && m_fieldOwner->focusOnPreviousField(*this))
100 keyboardEvent->setDefaultHandled();
104 if (keyIdentifier == "Right") {
107 // FIXME: We'd like to use FocusController::advanceFocus(FocusDirectionRight, ...)
108 // but it doesn't work for shadow nodes. webkit.org/b/104650
109 if (!localeForOwner().isRTL() && m_fieldOwner->focusOnNextField(*this))
110 keyboardEvent->setDefaultHandled();
114 if (isFieldOwnerReadOnly())
117 if (keyIdentifier == "Down") {
118 if (keyboardEvent->getModifierState("Alt"))
120 keyboardEvent->setDefaultHandled();
125 if (keyIdentifier == "Up") {
126 keyboardEvent->setDefaultHandled();
131 if (keyIdentifier == "U+0008" || keyIdentifier == "U+007F") {
132 keyboardEvent->setDefaultHandled();
133 setEmptyValue(DispatchEvent);
138 void DateTimeFieldElement::setFocus(bool value)
141 value ? m_fieldOwner->didFocusOnField() : m_fieldOwner->didBlurFromField();
142 ContainerNode::setFocus(value);
145 void DateTimeFieldElement::focusOnNextField()
149 m_fieldOwner->focusOnNextField(*this);
152 void DateTimeFieldElement::initialize(const AtomicString& pseudo, const String& axHelpText, int axMinimum, int axMaximum)
154 // On accessibility, DateTimeFieldElement acts like spin button.
155 setAttribute(roleAttr, AtomicString("spinbutton", AtomicString::ConstructFromLiteral));
156 setAttribute(aria_valuetextAttr, AtomicString(emptyValueAXText()));
157 setAttribute(aria_valueminAttr, AtomicString::number(axMinimum));
158 setAttribute(aria_valuemaxAttr, AtomicString::number(axMaximum));
160 setAttribute(aria_helpAttr, AtomicString(axHelpText));
161 setShadowPseudoId(pseudo);
162 appendChild(Text::create(document(), visibleValue()));
165 bool DateTimeFieldElement::isDateTimeFieldElement() const
170 bool DateTimeFieldElement::isFieldOwnerDisabled() const
172 return m_fieldOwner && m_fieldOwner->isFieldOwnerDisabled();
175 bool DateTimeFieldElement::isFieldOwnerReadOnly() const
177 return m_fieldOwner && m_fieldOwner->isFieldOwnerReadOnly();
180 bool DateTimeFieldElement::isDisabled() const
182 return fastHasAttribute(disabledAttr);
185 Locale& DateTimeFieldElement::localeForOwner() const
187 return document().getCachedLocale(localeIdentifier());
190 AtomicString DateTimeFieldElement::localeIdentifier() const
192 return m_fieldOwner ? m_fieldOwner->localeIdentifier() : nullAtom;
195 float DateTimeFieldElement::maximumWidth(const Font&)
197 const float paddingLeftAndRight = 2; // This should match to html.css.
198 return paddingLeftAndRight;
201 void DateTimeFieldElement::setDisabled()
203 // Set HTML attribute disabled to change apperance.
204 setBooleanAttribute(disabledAttr, true);
205 setNeedsStyleRecalc(SubtreeStyleChange);
208 bool DateTimeFieldElement::supportsFocus() const
210 return !isDisabled() && !isFieldOwnerDisabled();
213 void DateTimeFieldElement::updateVisibleValue(EventBehavior eventBehavior)
215 Text* const textNode = toText(firstChild());
216 const String newVisibleValue = visibleValue();
217 ASSERT(newVisibleValue.length() > 0);
219 if (textNode->wholeText() == newVisibleValue)
222 textNode->replaceWholeText(newVisibleValue);
224 setAttribute(aria_valuetextAttr, AtomicString(newVisibleValue));
225 setAttribute(aria_valuenowAttr, AtomicString::number(valueForARIAValueNow()));
227 setAttribute(aria_valuetextAttr, AtomicString(emptyValueAXText()));
228 removeAttribute(aria_valuenowAttr);
231 if (eventBehavior == DispatchEvent && m_fieldOwner)
232 m_fieldOwner->fieldValueChanged();
235 int DateTimeFieldElement::valueForARIAValueNow() const
237 return valueAsInteger();