1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #include "TextFieldInputType.h"
34
35 #include "Frame.h"
36 #include "HTMLInputElement.h"
37 #include "KeyboardEvent.h"
38 #include "RenderTextControlSingleLine.h"
39 #include "TextEvent.h"
40 #include "WheelEvent.h"
41 #include <wtf/text/WTFString.h>
42
43 namespace WebCore {
44
isTextField() const45 bool TextFieldInputType::isTextField() const
46 {
47 return true;
48 }
49
valueMissing(const String & value) const50 bool TextFieldInputType::valueMissing(const String& value) const
51 {
52 return value.isEmpty();
53 }
54
canSetSuggestedValue()55 bool TextFieldInputType::canSetSuggestedValue()
56 {
57 return true;
58 }
59
handleKeydownEvent(KeyboardEvent * event)60 void TextFieldInputType::handleKeydownEvent(KeyboardEvent* event)
61 {
62 if (!element()->focused())
63 return;
64 Frame* frame = element()->document()->frame();
65 if (!frame || !frame->editor()->doTextFieldCommandFromEvent(element(), event))
66 return;
67 event->setDefaultHandled();
68 }
69
handleKeydownEventForSpinButton(KeyboardEvent * event)70 void TextFieldInputType::handleKeydownEventForSpinButton(KeyboardEvent* event)
71 {
72 if (element()->disabled() || element()->readOnly())
73 return;
74 const String& key = event->keyIdentifier();
75 int step = 0;
76 if (key == "Up")
77 step = 1;
78 else if (key == "Down")
79 step = -1;
80 else
81 return;
82 element()->stepUpFromRenderer(step);
83 event->setDefaultHandled();
84 }
85
handleWheelEventForSpinButton(WheelEvent * event)86 void TextFieldInputType::handleWheelEventForSpinButton(WheelEvent* event)
87 {
88 if (element()->disabled() || element()->readOnly() || !element()->focused())
89 return;
90 int step = 0;
91 if (event->wheelDeltaY() > 0)
92 step = 1;
93 else if (event->wheelDeltaY() < 0)
94 step = -1;
95 else
96 return;
97 element()->stepUpFromRenderer(step);
98 event->setDefaultHandled();
99 }
100
forwardEvent(Event * event)101 void TextFieldInputType::forwardEvent(Event* event)
102 {
103 if (element()->renderer() && (event->isMouseEvent() || event->isDragEvent() || event->isWheelEvent() || event->type() == eventNames().blurEvent || event->type() == eventNames().focusEvent))
104 toRenderTextControlSingleLine(element()->renderer())->forwardEvent(event);
105 }
106
shouldSubmitImplicitly(Event * event)107 bool TextFieldInputType::shouldSubmitImplicitly(Event* event)
108 {
109 return (event->type() == eventNames().textInputEvent && event->isTextEvent() && static_cast<TextEvent*>(event)->data() == "\n") || InputType::shouldSubmitImplicitly(event);
110 }
111
createRenderer(RenderArena * arena,RenderStyle *) const112 RenderObject* TextFieldInputType::createRenderer(RenderArena* arena, RenderStyle*) const
113 {
114 return new (arena) RenderTextControlSingleLine(element(), element()->placeholderShouldBeVisible());
115 }
116
shouldUseInputMethod() const117 bool TextFieldInputType::shouldUseInputMethod() const
118 {
119 return true;
120 }
121
sanitizeValue(const String & proposedValue)122 String TextFieldInputType::sanitizeValue(const String& proposedValue)
123 {
124 return InputElement::sanitizeValueForTextField(element(), proposedValue);
125 }
126
shouldRespectListAttribute()127 bool TextFieldInputType::shouldRespectListAttribute()
128 {
129 return true;
130 }
131
132 } // namespace WebCore
133