1 /* 2 * Copyright (C) 2010 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 are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #include "config.h" 32 #include "core/html/SearchInputType.h" 33 34 #include "HTMLNames.h" 35 #include "bindings/v8/ExceptionStatePlaceholder.h" 36 #include "core/dom/KeyboardEvent.h" 37 #include "core/html/HTMLInputElement.h" 38 #include "core/html/InputTypeNames.h" 39 #include "core/html/shadow/ShadowElementNames.h" 40 #include "core/html/shadow/TextControlInnerElements.h" 41 #include "core/rendering/RenderSearchField.h" 42 #include "wtf/PassOwnPtr.h" 43 44 namespace WebCore { 45 46 using namespace HTMLNames; 47 48 inline SearchInputType::SearchInputType(HTMLInputElement* element) 49 : BaseTextInputType(element) 50 , m_searchEventTimer(this, &SearchInputType::searchEventTimerFired) 51 { 52 } 53 54 PassOwnPtr<InputType> SearchInputType::create(HTMLInputElement* element) 55 { 56 return adoptPtr(new SearchInputType(element)); 57 } 58 59 void SearchInputType::attach() 60 { 61 TextFieldInputType::attach(); 62 observeFeatureIfVisible(UseCounter::InputTypeSearch); 63 } 64 65 RenderObject* SearchInputType::createRenderer(RenderStyle*) const 66 { 67 return new RenderSearchField(element()); 68 } 69 70 const AtomicString& SearchInputType::formControlType() const 71 { 72 return InputTypeNames::search(); 73 } 74 75 bool SearchInputType::shouldRespectSpeechAttribute() 76 { 77 return true; 78 } 79 80 bool SearchInputType::isSearchField() const 81 { 82 return true; 83 } 84 85 bool SearchInputType::needsContainer() const 86 { 87 return true; 88 } 89 90 void SearchInputType::createShadowSubtree() 91 { 92 TextFieldInputType::createShadowSubtree(); 93 HTMLElement* container = containerElement(); 94 HTMLElement* textWrapper = innerBlockElement(); 95 ASSERT(container); 96 ASSERT(textWrapper); 97 98 container->insertBefore(SearchFieldDecorationElement::create(element()->document()), textWrapper, IGNORE_EXCEPTION); 99 container->insertBefore(SearchFieldCancelButtonElement::create(element()->document()), textWrapper->nextSibling(), IGNORE_EXCEPTION); 100 } 101 102 void SearchInputType::handleKeydownEvent(KeyboardEvent* event) 103 { 104 if (element()->isDisabledOrReadOnly()) { 105 TextFieldInputType::handleKeydownEvent(event); 106 return; 107 } 108 109 const String& key = event->keyIdentifier(); 110 if (key == "U+001B") { 111 RefPtr<HTMLInputElement> input = element(); 112 input->setValueForUser(""); 113 input->onSearch(); 114 event->setDefaultHandled(); 115 return; 116 } 117 TextFieldInputType::handleKeydownEvent(event); 118 } 119 120 void SearchInputType::startSearchEventTimer() 121 { 122 ASSERT(element()->renderer()); 123 unsigned length = element()->innerTextValue().length(); 124 125 if (!length) { 126 stopSearchEventTimer(); 127 element()->onSearch(); 128 return; 129 } 130 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)); 134 } 135 136 void SearchInputType::stopSearchEventTimer() 137 { 138 m_searchEventTimer.stop(); 139 } 140 141 void SearchInputType::searchEventTimerFired(Timer<SearchInputType>*) 142 { 143 element()->onSearch(); 144 } 145 146 bool SearchInputType::searchEventsShouldBeDispatched() const 147 { 148 return element()->hasAttribute(incrementalAttr); 149 } 150 151 void SearchInputType::didSetValueByUserEdit(ValueChangeState state) 152 { 153 if (element()->uaShadowElementById(ShadowElementNames::clearButton())) 154 toRenderSearchField(element()->renderer())->updateCancelButtonVisibility(); 155 156 // If the incremental attribute is set, then dispatch the search event 157 if (searchEventsShouldBeDispatched()) 158 startSearchEventTimer(); 159 160 TextFieldInputType::didSetValueByUserEdit(state); 161 } 162 163 bool SearchInputType::supportsInputModeAttribute() const 164 { 165 return true; 166 } 167 168 } // namespace WebCore 169