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 "SearchInputType.h"
34 #include "HTMLInputElement.h"
35 #include "KeyboardEvent.h"
36 #include "RenderTextControlSingleLine.h"
37 #include "ShadowRoot.h"
38 #include "TextControlInnerElements.h"
39 #include <wtf/PassOwnPtr.h>
43 inline SearchInputType::SearchInputType(HTMLInputElement* element)
44 : BaseTextInputType(element)
45 , m_searchEventTimer(this, &SearchInputType::searchEventTimerFired)
49 PassOwnPtr<InputType> SearchInputType::create(HTMLInputElement* element)
51 return adoptPtr(new SearchInputType(element));
54 const AtomicString& SearchInputType::formControlType() const
56 return InputTypeNames::search();
59 bool SearchInputType::shouldRespectSpeechAttribute()
64 bool SearchInputType::isSearchField() const
69 bool SearchInputType::needsContainer() const
74 void SearchInputType::createShadowSubtree()
76 ASSERT(!m_resultsButton);
77 ASSERT(!m_cancelButton);
79 TextFieldInputType::createShadowSubtree();
80 HTMLElement* container = containerElement();
81 HTMLElement* textWrapper = innerBlockElement();
86 m_resultsButton = SearchFieldResultsButtonElement::create(element()->document());
87 container->insertBefore(m_resultsButton, textWrapper, ec);
89 m_cancelButton = SearchFieldCancelButtonElement::create(element()->document());
90 container->insertBefore(m_cancelButton, textWrapper->nextSibling(), ec);
93 HTMLElement* SearchInputType::resultsButtonElement() const
95 return m_resultsButton.get();
98 HTMLElement* SearchInputType::cancelButtonElement() const
100 return m_cancelButton.get();
103 void SearchInputType::handleKeydownEvent(KeyboardEvent* event)
105 if (element()->disabled() || element()->readOnly()) {
106 TextFieldInputType::handleKeydownEvent(event);
110 const String& key = event->keyIdentifier();
111 if (key == "U+001B") {
112 RefPtr<HTMLInputElement> input = element();
113 input->setValueForUser("");
115 event->setDefaultHandled();
118 TextFieldInputType::handleKeydownEvent(event);
121 void SearchInputType::destroyShadowSubtree()
123 TextFieldInputType::destroyShadowSubtree();
124 m_resultsButton.clear();
125 m_cancelButton.clear();
128 void SearchInputType::startSearchEventTimer()
130 ASSERT(element()->renderer());
131 unsigned length = element()->innerTextValue().length();
134 stopSearchEventTimer();
135 element()->onSearch();
139 // After typing the first key, we wait 0.5 seconds.
140 // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on.
141 m_searchEventTimer.startOneShot(max(0.2, 0.6 - 0.1 * length));
144 void SearchInputType::stopSearchEventTimer()
146 m_searchEventTimer.stop();
149 void SearchInputType::searchEventTimerFired(Timer<SearchInputType>*)
151 element()->onSearch();
155 } // namespace WebCore