2 * Copyright (C) 2010 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/html/forms/SearchInputType.h"
34 #include "HTMLNames.h"
35 #include "InputTypeNames.h"
36 #include "bindings/v8/ExceptionStatePlaceholder.h"
37 #include "core/events/KeyboardEvent.h"
38 #include "core/dom/shadow/ShadowRoot.h"
39 #include "core/html/HTMLInputElement.h"
40 #include "core/html/shadow/ShadowElementNames.h"
41 #include "core/html/shadow/TextControlInnerElements.h"
42 #include "core/rendering/RenderSearchField.h"
43 #include "wtf/PassOwnPtr.h"
47 using namespace HTMLNames;
49 inline SearchInputType::SearchInputType(HTMLInputElement& element)
50 : BaseTextInputType(element)
51 , m_searchEventTimer(this, &SearchInputType::searchEventTimerFired)
55 PassRefPtrWillBeRawPtr<InputType> SearchInputType::create(HTMLInputElement& element)
57 return adoptRefWillBeNoop(new SearchInputType(element));
60 void SearchInputType::countUsage()
62 countUsageIfVisible(UseCounter::InputTypeSearch);
65 RenderObject* SearchInputType::createRenderer(RenderStyle*) const
67 return new RenderSearchField(&element());
70 const AtomicString& SearchInputType::formControlType() const
72 return InputTypeNames::search;
75 bool SearchInputType::shouldRespectSpeechAttribute()
80 bool SearchInputType::isSearchField() const
85 bool SearchInputType::needsContainer() const
90 void SearchInputType::createShadowSubtree()
92 TextFieldInputType::createShadowSubtree();
93 Element* container = containerElement();
94 Element* viewPort = element().userAgentShadowRoot()->getElementById(ShadowElementNames::editingViewPort());
98 container->insertBefore(SearchFieldDecorationElement::create(element().document()), viewPort);
99 container->insertBefore(SearchFieldCancelButtonElement::create(element().document()), viewPort->nextSibling());
102 void SearchInputType::handleKeydownEvent(KeyboardEvent* event)
104 if (element().isDisabledOrReadOnly()) {
105 TextFieldInputType::handleKeydownEvent(event);
109 const String& key = event->keyIdentifier();
110 if (key == "U+001B") {
111 RefPtr<HTMLInputElement> input(element());
112 input->setValueForUser("");
114 event->setDefaultHandled();
117 TextFieldInputType::handleKeydownEvent(event);
120 void SearchInputType::startSearchEventTimer()
122 ASSERT(element().renderer());
123 unsigned length = element().innerTextValue().length();
126 stopSearchEventTimer();
127 element().onSearch();
131 // After typing the first key, we wait 0.5 seconds.
132 // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on.
133 m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length), FROM_HERE);
136 void SearchInputType::stopSearchEventTimer()
138 m_searchEventTimer.stop();
141 void SearchInputType::searchEventTimerFired(Timer<SearchInputType>*)
143 element().onSearch();
146 bool SearchInputType::searchEventsShouldBeDispatched() const
148 return element().hasAttribute(incrementalAttr);
151 void SearchInputType::didSetValueByUserEdit(ValueChangeState state)
153 updateCancelButtonVisibility();
155 // If the incremental attribute is set, then dispatch the search event
156 if (searchEventsShouldBeDispatched())
157 startSearchEventTimer();
159 TextFieldInputType::didSetValueByUserEdit(state);
162 void SearchInputType::updateView()
164 BaseTextInputType::updateView();
165 updateCancelButtonVisibility();
168 void SearchInputType::updateCancelButtonVisibility()
170 Element* button = element().userAgentShadowRoot()->getElementById(ShadowElementNames::clearButton());
173 if (element().value().isEmpty()) {
174 button->setInlineStyleProperty(CSSPropertyOpacity, 0.0, CSSPrimitiveValue::CSS_NUMBER);
175 button->setInlineStyleProperty(CSSPropertyPointerEvents, CSSValueNone);
177 button->removeInlineStyleProperty(CSSPropertyOpacity);
178 button->removeInlineStyleProperty(CSSPropertyPointerEvents);
182 bool SearchInputType::supportsInputModeAttribute() const
187 } // namespace WebCore