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/forms/SearchInputType.h" 33 34 #include "bindings/v8/ExceptionStatePlaceholder.h" 35 #include "core/HTMLNames.h" 36 #include "core/InputTypeNames.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" 44 45 namespace WebCore { 46 47 using namespace HTMLNames; 48 49 inline SearchInputType::SearchInputType(HTMLInputElement& element) 50 : BaseTextInputType(element) 51 , m_searchEventTimer(this, &SearchInputType::searchEventTimerFired) 52 { 53 } 54 55 PassRefPtrWillBeRawPtr<InputType> SearchInputType::create(HTMLInputElement& element) 56 { 57 return adoptRefWillBeNoop(new SearchInputType(element)); 58 } 59 60 void SearchInputType::countUsage() 61 { 62 countUsageIfVisible(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 Element* container = containerElement(); 94 Element* viewPort = element().userAgentShadowRoot()->getElementById(ShadowElementNames::editingViewPort()); 95 ASSERT(container); 96 ASSERT(viewPort); 97 98 container->insertBefore(SearchFieldDecorationElement::create(element().document()), viewPort); 99 container->insertBefore(SearchFieldCancelButtonElement::create(element().document()), viewPort->nextSibling()); 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 RefPtrWillBeRawPtr<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().innerEditorValue().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), FROM_HERE); 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 updateCancelButtonVisibility(); 154 155 // If the incremental attribute is set, then dispatch the search event 156 if (searchEventsShouldBeDispatched()) 157 startSearchEventTimer(); 158 159 TextFieldInputType::didSetValueByUserEdit(state); 160 } 161 162 void SearchInputType::updateView() 163 { 164 BaseTextInputType::updateView(); 165 updateCancelButtonVisibility(); 166 } 167 168 void SearchInputType::updateCancelButtonVisibility() 169 { 170 Element* button = element().userAgentShadowRoot()->getElementById(ShadowElementNames::clearButton()); 171 if (!button) 172 return; 173 if (element().value().isEmpty()) { 174 button->setInlineStyleProperty(CSSPropertyOpacity, 0.0, CSSPrimitiveValue::CSS_NUMBER); 175 button->setInlineStyleProperty(CSSPropertyPointerEvents, CSSValueNone); 176 } else { 177 button->removeInlineStyleProperty(CSSPropertyOpacity); 178 button->removeInlineStyleProperty(CSSPropertyPointerEvents); 179 } 180 } 181 182 bool SearchInputType::supportsInputModeAttribute() const 183 { 184 return true; 185 } 186 187 } // namespace WebCore 188