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/shadow/ElementShadow.h"
     38 #include "core/dom/shadow/ShadowRoot.h"
     39 #include "core/html/HTMLDataListElement.h"
     40 #include "core/html/HTMLInputElement.h"
     41 #include "core/html/shadow/ShadowElementNames.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 blink {
     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::setSelectionRange(int start, int end)
    136 {
    137     unwrap<HTMLInputElement>()->setSelectionRange(start, end);
    138 }
    139 
    140 int WebInputElement::selectionStart() const
    141 {
    142     return constUnwrap<HTMLInputElement>()->selectionStart();
    143 }
    144 
    145 int WebInputElement::selectionEnd() const
    146 {
    147     return constUnwrap<HTMLInputElement>()->selectionEnd();
    148 }
    149 
    150 bool WebInputElement::isValidValue(const WebString& value) const
    151 {
    152     return constUnwrap<HTMLInputElement>()->isValidValue(value);
    153 }
    154 
    155 void WebInputElement::setChecked(bool nowChecked, bool sendChangeEvent)
    156 {
    157     unwrap<HTMLInputElement>()->setChecked(nowChecked, sendChangeEvent ? DispatchChangeEvent : DispatchNoEvent);
    158 }
    159 
    160 bool WebInputElement::isChecked() const
    161 {
    162     return constUnwrap<HTMLInputElement>()->checked();
    163 }
    164 
    165 bool WebInputElement::isMultiple() const
    166 {
    167     return constUnwrap<HTMLInputElement>()->multiple();
    168 }
    169 
    170 WebNodeCollection WebInputElement::dataListOptions() const
    171 {
    172     if (RuntimeEnabledFeatures::dataListElementEnabled()) {
    173         if (HTMLDataListElement* dataList = toHTMLDataListElement(constUnwrap<HTMLInputElement>()->list()))
    174             return WebNodeCollection(dataList->options());
    175     }
    176     return WebNodeCollection();
    177 }
    178 
    179 WebString WebInputElement::localizeValue(const WebString& proposedValue) const
    180 {
    181     return constUnwrap<HTMLInputElement>()->localizeValue(proposedValue);
    182 }
    183 
    184 bool WebInputElement::isSpeechInputEnabled() const
    185 {
    186 #if ENABLE(INPUT_SPEECH)
    187     return constUnwrap<HTMLInputElement>()->isSpeechEnabled();
    188 #else
    189     return false;
    190 #endif
    191 }
    192 
    193 #if ENABLE(INPUT_SPEECH)
    194 static inline InputFieldSpeechButtonElement* speechButtonElement(const WebInputElement* webInput)
    195 {
    196     return toInputFieldSpeechButtonElement(webInput->constUnwrap<HTMLInputElement>()->userAgentShadowRoot()->getElementById(ShadowElementNames::speechButton()));
    197 }
    198 #endif
    199 
    200 WebInputElement::SpeechInputState WebInputElement::getSpeechInputState() const
    201 {
    202 #if ENABLE(INPUT_SPEECH)
    203     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
    204         return static_cast<WebInputElement::SpeechInputState>(speechButton->state());
    205 #endif
    206 
    207     return Idle;
    208 }
    209 
    210 void WebInputElement::startSpeechInput()
    211 {
    212 #if ENABLE(INPUT_SPEECH)
    213     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
    214         speechButton->startSpeechInput();
    215 #endif
    216 }
    217 
    218 void WebInputElement::stopSpeechInput()
    219 {
    220 #if ENABLE(INPUT_SPEECH)
    221     if (InputFieldSpeechButtonElement* speechButton = speechButtonElement(this))
    222         speechButton->stopSpeechInput();
    223 #endif
    224 }
    225 
    226 int WebInputElement::defaultMaxLength()
    227 {
    228     return HTMLInputElement::maximumLength;
    229 }
    230 
    231 WebString WebInputElement::directionForFormData() const
    232 {
    233     return constUnwrap<HTMLInputElement>()->directionForFormData();
    234 }
    235 
    236 // FIXME: Remove this once password_generation_manager.h stops using it.
    237 WebElement WebInputElement::decorationElementFor(void*)
    238 {
    239     return passwordGeneratorButtonElement();
    240 }
    241 
    242 WebElement WebInputElement::passwordGeneratorButtonElement() const
    243 {
    244     return WebElement(constUnwrap<HTMLInputElement>()->passwordGeneratorButtonElement());
    245 }
    246 
    247 WebInputElement::WebInputElement(const PassRefPtr<HTMLInputElement>& elem)
    248     : WebFormControlElement(elem)
    249 {
    250 }
    251 
    252 WebInputElement& WebInputElement::operator=(const PassRefPtr<HTMLInputElement>& elem)
    253 {
    254     m_private = elem;
    255     return *this;
    256 }
    257 
    258 WebInputElement::operator PassRefPtr<HTMLInputElement>() const
    259 {
    260     return toHTMLInputElement(m_private.get());
    261 }
    262 
    263 WebInputElement* toWebInputElement(WebElement* webElement)
    264 {
    265     if (!webElement->unwrap<Element>()->hasTagName(HTMLNames::inputTag))
    266         return 0;
    267 
    268     return static_cast<WebInputElement*>(webElement);
    269 }
    270 } // namespace blink
    271