OLD | NEW |
| (Empty) |
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 "core/html/shadow/DateTimeFieldElement.h" | |
27 | |
28 #include "core/HTMLNames.h" | |
29 #include "core/dom/Document.h" | |
30 #include "core/dom/StyleChangeReason.h" | |
31 #include "core/dom/Text.h" | |
32 #include "core/events/KeyboardEvent.h" | |
33 #include "core/layout/TextRunConstructor.h" | |
34 #include "core/style/ComputedStyle.h" | |
35 #include "platform/text/PlatformLocale.h" | |
36 #include "platform/wtf/text/WTFString.h" | |
37 | |
38 namespace blink { | |
39 | |
40 using namespace HTMLNames; | |
41 | |
42 DateTimeFieldElement::FieldOwner::~FieldOwner() {} | |
43 | |
44 DateTimeFieldElement::DateTimeFieldElement(Document& document, | |
45 FieldOwner& field_owner) | |
46 : HTMLSpanElement(document), field_owner_(&field_owner) {} | |
47 | |
48 DEFINE_TRACE(DateTimeFieldElement) { | |
49 visitor->Trace(field_owner_); | |
50 HTMLSpanElement::Trace(visitor); | |
51 } | |
52 | |
53 float DateTimeFieldElement::ComputeTextWidth(const ComputedStyle& style, | |
54 const String& text) { | |
55 return style.GetFont().Width(ConstructTextRun(style.GetFont(), text, style)); | |
56 } | |
57 | |
58 void DateTimeFieldElement::DefaultEventHandler(Event* event) { | |
59 if (event->IsKeyboardEvent()) { | |
60 KeyboardEvent* keyboard_event = ToKeyboardEvent(event); | |
61 if (!IsDisabled() && !IsFieldOwnerDisabled() && !IsFieldOwnerReadOnly()) { | |
62 HandleKeyboardEvent(keyboard_event); | |
63 if (keyboard_event->DefaultHandled()) { | |
64 if (field_owner_) | |
65 field_owner_->FieldDidChangeValueByKeyboard(); | |
66 return; | |
67 } | |
68 } | |
69 DefaultKeyboardEventHandler(keyboard_event); | |
70 if (field_owner_) | |
71 field_owner_->FieldDidChangeValueByKeyboard(); | |
72 if (keyboard_event->DefaultHandled()) | |
73 return; | |
74 } | |
75 | |
76 HTMLElement::DefaultEventHandler(event); | |
77 } | |
78 | |
79 void DateTimeFieldElement::DefaultKeyboardEventHandler( | |
80 KeyboardEvent* keyboard_event) { | |
81 if (keyboard_event->type() != EventTypeNames::keydown) | |
82 return; | |
83 | |
84 if (IsDisabled() || IsFieldOwnerDisabled()) | |
85 return; | |
86 | |
87 const String& key = keyboard_event->key(); | |
88 | |
89 if (key == "ArrowLeft") { | |
90 if (!field_owner_) | |
91 return; | |
92 // FIXME: We'd like to use FocusController::advanceFocus(FocusDirectionLeft, | |
93 // ...) but it doesn't work for shadow nodes. webkit.org/b/104650 | |
94 if (!LocaleForOwner().IsRTL() && field_owner_->FocusOnPreviousField(*this)) | |
95 keyboard_event->SetDefaultHandled(); | |
96 return; | |
97 } | |
98 | |
99 if (key == "ArrowRight") { | |
100 if (!field_owner_) | |
101 return; | |
102 // FIXME: We'd like to use | |
103 // FocusController::advanceFocus(FocusDirectionRight, ...) | |
104 // but it doesn't work for shadow nodes. webkit.org/b/104650 | |
105 if (!LocaleForOwner().IsRTL() && field_owner_->FocusOnNextField(*this)) | |
106 keyboard_event->SetDefaultHandled(); | |
107 return; | |
108 } | |
109 | |
110 if (IsFieldOwnerReadOnly()) | |
111 return; | |
112 | |
113 if (key == "ArrowDown") { | |
114 if (keyboard_event->getModifierState("Alt")) | |
115 return; | |
116 keyboard_event->SetDefaultHandled(); | |
117 StepDown(); | |
118 return; | |
119 } | |
120 | |
121 if (key == "ArrowUp") { | |
122 keyboard_event->SetDefaultHandled(); | |
123 StepUp(); | |
124 return; | |
125 } | |
126 | |
127 if (key == "Backspace" || key == "Delete") { | |
128 keyboard_event->SetDefaultHandled(); | |
129 SetEmptyValue(kDispatchEvent); | |
130 return; | |
131 } | |
132 } | |
133 | |
134 void DateTimeFieldElement::SetFocused(bool value, WebFocusType focus_type) { | |
135 if (field_owner_) { | |
136 if (value) { | |
137 field_owner_->DidFocusOnField(focus_type); | |
138 } else { | |
139 field_owner_->DidBlurFromField(focus_type); | |
140 } | |
141 } | |
142 | |
143 ContainerNode::SetFocused(value, focus_type); | |
144 } | |
145 | |
146 void DateTimeFieldElement::FocusOnNextField() { | |
147 if (!field_owner_) | |
148 return; | |
149 field_owner_->FocusOnNextField(*this); | |
150 } | |
151 | |
152 void DateTimeFieldElement::Initialize(const AtomicString& pseudo, | |
153 const String& ax_help_text, | |
154 int ax_minimum, | |
155 int ax_maximum) { | |
156 // On accessibility, DateTimeFieldElement acts like spin button. | |
157 setAttribute(roleAttr, AtomicString("spinbutton")); | |
158 setAttribute(aria_valuetextAttr, AtomicString(VisibleValue())); | |
159 setAttribute(aria_valueminAttr, AtomicString::Number(ax_minimum)); | |
160 setAttribute(aria_valuemaxAttr, AtomicString::Number(ax_maximum)); | |
161 | |
162 setAttribute(aria_helpAttr, AtomicString(ax_help_text)); | |
163 SetShadowPseudoId(pseudo); | |
164 AppendChild(Text::Create(GetDocument(), VisibleValue())); | |
165 } | |
166 | |
167 bool DateTimeFieldElement::IsDateTimeFieldElement() const { | |
168 return true; | |
169 } | |
170 | |
171 bool DateTimeFieldElement::IsFieldOwnerDisabled() const { | |
172 return field_owner_ && field_owner_->IsFieldOwnerDisabled(); | |
173 } | |
174 | |
175 bool DateTimeFieldElement::IsFieldOwnerReadOnly() const { | |
176 return field_owner_ && field_owner_->IsFieldOwnerReadOnly(); | |
177 } | |
178 | |
179 bool DateTimeFieldElement::IsDisabled() const { | |
180 return FastHasAttribute(disabledAttr); | |
181 } | |
182 | |
183 Locale& DateTimeFieldElement::LocaleForOwner() const { | |
184 return GetDocument().GetCachedLocale(LocaleIdentifier()); | |
185 } | |
186 | |
187 AtomicString DateTimeFieldElement::LocaleIdentifier() const { | |
188 return field_owner_ ? field_owner_->LocaleIdentifier() : g_null_atom; | |
189 } | |
190 | |
191 float DateTimeFieldElement::MaximumWidth(const ComputedStyle&) { | |
192 const float kPaddingLeftAndRight = 2; // This should match to html.css. | |
193 return kPaddingLeftAndRight; | |
194 } | |
195 | |
196 void DateTimeFieldElement::SetDisabled() { | |
197 // Set HTML attribute disabled to change apperance. | |
198 SetBooleanAttribute(disabledAttr, true); | |
199 SetNeedsStyleRecalc( | |
200 kSubtreeStyleChange, | |
201 StyleChangeReasonForTracing::CreateWithExtraData( | |
202 StyleChangeReason::kPseudoClass, StyleChangeExtraData::g_disabled)); | |
203 } | |
204 | |
205 bool DateTimeFieldElement::SupportsFocus() const { | |
206 return !IsDisabled() && !IsFieldOwnerDisabled(); | |
207 } | |
208 | |
209 void DateTimeFieldElement::UpdateVisibleValue(EventBehavior event_behavior) { | |
210 Text* const text_node = ToText(FirstChild()); | |
211 const String new_visible_value = VisibleValue(); | |
212 DCHECK_GT(new_visible_value.length(), 0u); | |
213 | |
214 if (text_node->wholeText() == new_visible_value) | |
215 return; | |
216 | |
217 text_node->ReplaceWholeText(new_visible_value); | |
218 if (HasValue()) { | |
219 setAttribute(aria_valuenowAttr, | |
220 AtomicString::Number(ValueForARIAValueNow())); | |
221 } else { | |
222 removeAttribute(aria_valuenowAttr); | |
223 } | |
224 setAttribute(aria_valuetextAttr, AtomicString(new_visible_value)); | |
225 | |
226 if (event_behavior == kDispatchEvent && field_owner_) | |
227 field_owner_->FieldValueChanged(); | |
228 } | |
229 | |
230 int DateTimeFieldElement::ValueForARIAValueNow() const { | |
231 return ValueAsInteger(); | |
232 } | |
233 | |
234 } // namespace blink | |
OLD | NEW |