Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 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 "WebInputElement.h"
     33 
     34 #include "HTMLNames.h"
     35 #include "RuntimeEnabledFeatures.h"
     36 #include "WebNodeCollection.h"
     37 #include "core/dom/NodeTraversal.h"
     38 #include "core/dom/shadow/ElementShadow.h"
     39 #include "core/dom/shadow/ShadowRoot.h"
     40 #include "core/html/HTMLDataListElement.h"
     41 #include "core/html/HTMLInputElement.h"
     42 #include "core/html/shadow/TextControlInnerElements.h"
     43 #include "public/platform/WebString.h"
     44 #include "wtf/PassRefPtr.h"
     45 
     46 using namespace WebCore;
     47 
     48 namespace WebKit {
     49 
     50 bool WebInputElement::isTextField() const
     51 {
     52     return constUnwrap<HTMLInputElement>()->isTextField();
     53 }
     54 
     55 bool WebInputElement::isText() const
     56 {
     57     return constUnwrap<HTMLInputElement>()->isText();
     58 }
     59 
     60 bool WebInputElement::isPasswordField() const
     61 {
     62     return constUnwrap<HTMLInputElement>()->isPasswordField();
     63 }
     64 
     65 bool WebInputElement::isImageButton() const
     66 {
     67     return constUnwrap<HTMLInputElement>()->isImageButton();
     68 }
     69 
     70 bool WebInputElement::isRadioButton() const
     71 {
     72     return constUnwrap<HTMLInputElement>()->isRadioButton();
     73 }
     74 
     75 bool WebInputElement::isCheckbox() const
     76 {
     77     return constUnwrap<HTMLInputElement>()->isCheckbox();
     78 }
     79 
     80 bool WebInputElement::autoComplete() const
     81 {
     82     return constUnwrap<HTMLInputElement>()->shouldAutocomplete();
     83 }
     84 
     85 int WebInputElement::maxLength() const
     86 {
     87     return constUnwrap<HTMLInputElement>()->maxLength();
     88 }
     89 
     90 bool WebInputElement::isActivatedSubmit() const
     91 {
     92     return constUnwrap<HTMLInputElement>()->isActivatedSubmit();
     93 }
     94 
     95 void WebInputElement::setActivatedSubmit(bool activated)
     96 {
     97     unwrap<HTMLInputElement>()->setActivatedSubmit(activated);
     98 }
     99 
    100 int WebInputElement::size() const
    101 {
    102     return constUnwrap<HTMLInputElement>()->size();
    103 }
    104 
    105 void WebInputElement::setValue(const WebString& value, bool sendChangeEvent)
    106 {
    107     unwrap<HTMLInputElement>()->setValue(value, sendChangeEvent ? DispatchChangeEvent : DispatchNoEvent);
    108 }
    109 
    110 WebString WebInputElement::value() const
    111 {
    112     return constUnwrap<HTMLInputElement>()->value();
    113 }
    114 
    115 WebString WebInputElement::editingValue() const
    116 {
    117     return constUnwrap<HTMLInputElement>()->innerTextValue();
    118 }
    119 
    120 void WebInputElement::setEditingValue(const WebString& value)
    121 {
    122     unwrap<HTMLInputElement>()->setEditingValue(value);
    123 }
    124 
    125 void WebInputElement::setSuggestedValue(const WebString& value)
    126 {
    127     unwrap<HTMLInputElement>()->setSuggestedValue(value);
    128 }
    129 
    130 WebString WebInputElement::suggestedValue() const
    131 {
    132     return constUnwrap<HTMLInputElement>()->suggestedValue();
    133 }
    134 
    135 void WebInputElement::setPlaceholder(const WebString& value)
    136 {
    137     unwrap<HTMLInputElement>()->setAttribute(HTMLNames::placeholderAttr, value);
    138 }
    139 
    140 WebString WebInputElement::placeholder() const
    141 {
    142     return constUnwrap<HTMLInputElement>()->fastGetAttribute(HTMLNames::placeholderAttr);
    143 }
    144 
    145 bool WebInputElement::isAutofilled() const
    146 {
    147     return constUnwrap<HTMLInputElement>()->isAutofilled();
    148 }
    149 
    150 void WebInputElement::setAutofilled(bool autofilled)
    151 {
    152     unwrap<HTMLInputElement>()->setAutofilled(autofilled);
    153 }
    154 
    155 void WebInputElement::setSelectionRange(int start, int end)
    156 {
    157     unwrap<HTMLInputElement>()->setSelectionRange(start, end);
    158 }
    159 
    160 int WebInputElement::selectionStart() const
    161 {
    162     return constUnwrap<HTMLInputElement>()->selectionStart();
    163 }
    164 
    165 int WebInputElement::selectionEnd() const
    166 {
    167     return constUnwrap<HTMLInputElement>()->selectionEnd();
    168 }
    169 
    170 bool WebInputElement::isValidValue(const WebString& value) const
    171 {
    172     return constUnwrap<HTMLInputElement>()->isValidValue(value);
    173 }
    174 
    175 void WebInputElement::setChecked(bool nowChecked, bool sendChangeEvent)
    176 {
    177     unwrap<HTMLInputElement>()->setChecked(nowChecked, sendChangeEvent ? DispatchChangeEvent : DispatchNoEvent);
    178 }
    179 
    180 bool WebInputElement::isChecked() const
    181 {
    182     return constUnwrap<HTMLInputElement>()->checked();
    183 }
    184 
    185 bool WebInputElement::isMultiple() const
    186 {
    187     return constUnwrap<HTMLInputElement>()->multiple();
    188 }
    189 
    190 WebNodeCollection WebInputElement::dataListOptions() const
    191 {
    192     if (RuntimeEnabledFeatures::dataListElementEnabled()) {
    193         HTMLDataListElement* dataList = static_cast<HTMLDataListElement*>(constUnwrap<HTMLInputElement>()->list());
    194         if (dataList)
    195             return WebNodeCollection(dataList->options());
    196     }
    197     return WebNodeCollection();
    198 }
    199 
    200 WebString WebInputElement::localizeValue(const WebString& proposedValue) const
    201 {
    202     return constUnwrap<HTMLInputElement>()->localizeValue(proposedValue);
    203 }
    204 
    205 bool WebInputElement::isSpeechInputEnabled() const
    206 {
    207 #if ENABLE(INPUT_SPEECH)
    208     return constUnwrap<HTMLInputElement>()->isSpeechEnabled();
    209 #else
    210     return false;
    211 #endif
    212 }
    213 
    214 WebInputElement::SpeechInputState WebInputElement::getSpeechInputState() const
    215 {
    216 #if ENABLE(INPUT_SPEECH)
    217     InputFieldSpeechButtonElement* speechButton = toInputFieldSpeechButtonElement(constUnwrap<HTMLInputElement>()->speechButtonElement());
    218     if (speechButton)
    219         return static_cast<WebInputElement::SpeechInputState>(speechButton->state());
    220 #endif
    221 
    222     return Idle;
    223 }
    224 
    225 void WebInputElement::startSpeechInput()
    226 {
    227 #if ENABLE(INPUT_SPEECH)
    228     InputFieldSpeechButtonElement* speechButton = toInputFieldSpeechButtonElement(constUnwrap<HTMLInputElement>()->speechButtonElement());
    229     if (speechButton)
    230         speechButton->startSpeechInput();
    231 #endif
    232 }
    233 
    234 void WebInputElement::stopSpeechInput()
    235 {
    236 #if ENABLE(INPUT_SPEECH)
    237     InputFieldSpeechButtonElement* speechButton = toInputFieldSpeechButtonElement(constUnwrap<HTMLInputElement>()->speechButtonElement());
    238     if (speechButton)
    239         speechButton->stopSpeechInput();
    240 #endif
    241 }
    242 
    243 int WebInputElement::defaultMaxLength()
    244 {
    245     return HTMLInputElement::maximumLength;
    246 }
    247 
    248 WebString WebInputElement::directionForFormData() const
    249 {
    250     return constUnwrap<HTMLInputElement>()->directionForFormData();
    251 }
    252 
    253 // FIXME: Remove this once password_generation_manager.h stops using it.
    254 WebElement WebInputElement::decorationElementFor(void*)
    255 {
    256     return passwordGeneratorButtonElement();
    257 }
    258 
    259 WebElement WebInputElement::passwordGeneratorButtonElement() const
    260 {
    261     return WebElement(constUnwrap<HTMLInputElement>()->passwordGeneratorButtonElement());
    262 }
    263 
    264 WebInputElement::WebInputElement(const PassRefPtr<HTMLInputElement>& elem)
    265     : WebFormControlElement(elem)
    266 {
    267 }
    268 
    269 WebInputElement& WebInputElement::operator=(const PassRefPtr<HTMLInputElement>& elem)
    270 {
    271     m_private = elem;
    272     return *this;
    273 }
    274 
    275 WebInputElement::operator PassRefPtr<HTMLInputElement>() const
    276 {
    277     return toHTMLInputElement(m_private.get());
    278 }
    279 
    280 WebInputElement* toWebInputElement(WebElement* webElement)
    281 {
    282     if (!webElement->unwrap<Element>()->hasTagName(HTMLNames::inputTag))
    283         return 0;
    284 
    285     return static_cast<WebInputElement*>(webElement);
    286 }
    287 } // namespace WebKit
    288