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 "WebSearchableFormData.h"
     33 
     34 #include "HTMLNames.h"
     35 #include "WebFormElement.h"
     36 #include "WebInputElement.h"
     37 #include "core/dom/Document.h"
     38 #include "core/html/FormDataList.h"
     39 #include "core/html/HTMLFormControlElement.h"
     40 #include "core/html/HTMLFormElement.h"
     41 #include "core/html/HTMLInputElement.h"
     42 #include "core/html/HTMLOptionElement.h"
     43 #include "core/html/HTMLSelectElement.h"
     44 #include "core/html/HTMLTextAreaElement.h"
     45 #include "platform/network/FormDataBuilder.h"
     46 #include "wtf/text/TextEncoding.h"
     47 
     48 using namespace WebCore;
     49 using namespace HTMLNames;
     50 
     51 namespace {
     52 
     53 // Gets the encoding for the form.
     54 void GetFormEncoding(const HTMLFormElement* form, WTF::TextEncoding* encoding)
     55 {
     56     String str(form->getAttribute(HTMLNames::accept_charsetAttr));
     57     str.replace(',', ' ');
     58     Vector<String> charsets;
     59     str.split(' ', charsets);
     60     for (Vector<String>::const_iterator i(charsets.begin()); i != charsets.end(); ++i) {
     61         *encoding = WTF::TextEncoding(*i);
     62         if (encoding->isValid())
     63             return;
     64     }
     65     if (!form->document().loader())
     66          return;
     67     *encoding = WTF::TextEncoding(form->document().encoding());
     68 }
     69 
     70 // Returns true if the submit request results in an HTTP URL.
     71 bool IsHTTPFormSubmit(const HTMLFormElement* form)
     72 {
     73     // FIXME: This function is insane. This is an overly complicated way to get this information.
     74     String action(form->action());
     75     // The isNull() check is trying to avoid completeURL returning KURL() when passed a null string.
     76     return form->document().completeURL(action.isNull() ? "" : action).protocolIs("http");
     77 }
     78 
     79 // If the form does not have an activated submit button, the first submit
     80 // button is returned.
     81 HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form)
     82 {
     83     HTMLFormControlElement* firstSubmitButton = 0;
     84     const Vector<FormAssociatedElement*>& element = form->associatedElements();
     85     for (Vector<FormAssociatedElement*>::const_iterator i(element.begin()); i != element.end(); ++i) {
     86         if (!(*i)->isFormControlElement())
     87             continue;
     88         HTMLFormControlElement* control = toHTMLFormControlElement(*i);
     89         if (control->isActivatedSubmit()) {
     90             // There's a button that is already activated for submit, return 0.
     91             return 0;
     92         }
     93         if (!firstSubmitButton && control->isSuccessfulSubmitButton())
     94             firstSubmitButton = control;
     95     }
     96     return firstSubmitButton;
     97 }
     98 
     99 // Returns true if the selected state of all the options matches the default
    100 // selected state.
    101 bool IsSelectInDefaultState(HTMLSelectElement* select)
    102 {
    103     const Vector<HTMLElement*>& listItems = select->listItems();
    104     if (select->multiple() || select->size() > 1) {
    105         for (Vector<HTMLElement*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
    106             if (!(*i)->hasLocalName(HTMLNames::optionTag))
    107                 continue;
    108             HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
    109             if (optionElement->selected() != optionElement->hasAttribute(selectedAttr))
    110                 return false;
    111         }
    112         return true;
    113     }
    114 
    115     // The select is rendered as a combobox (called menulist in WebKit). At
    116     // least one item is selected, determine which one.
    117     HTMLOptionElement* initialSelected = 0;
    118     for (Vector<HTMLElement*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
    119         if (!(*i)->hasLocalName(HTMLNames::optionTag))
    120             continue;
    121         HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
    122         if (optionElement->hasAttribute(selectedAttr)) {
    123             // The page specified the option to select.
    124             initialSelected = optionElement;
    125             break;
    126         }
    127         if (!initialSelected)
    128             initialSelected = optionElement;
    129     }
    130     return !initialSelected || initialSelected->selected();
    131 }
    132 
    133 // Returns true if the form element is in its default state, false otherwise.
    134 // The default state is the state of the form element on initial load of the
    135 // page, and varies depending upon the form element. For example, a checkbox is
    136 // in its default state if the checked state matches the state of the checked attribute.
    137 bool IsInDefaultState(HTMLFormControlElement* formElement)
    138 {
    139     if (formElement->hasTagName(HTMLNames::inputTag)) {
    140         const HTMLInputElement* inputElement = toHTMLInputElement(formElement);
    141         if (inputElement->isCheckbox() || inputElement->isRadioButton())
    142             return inputElement->checked() == inputElement->hasAttribute(checkedAttr);
    143     } else if (formElement->hasTagName(HTMLNames::selectTag)) {
    144         return IsSelectInDefaultState(toHTMLSelectElement(formElement));
    145     }
    146     return true;
    147 }
    148 
    149 // Look for a suitable search text field in a given HTMLFormElement
    150 // Return nothing if one of those items are found:
    151 //  - A text area field
    152 //  - A file upload field
    153 //  - A Password field
    154 //  - More than one text field
    155 HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form)
    156 {
    157     HTMLInputElement* textElement = 0;
    158     const Vector<FormAssociatedElement*>& element = form->associatedElements();
    159     for (Vector<FormAssociatedElement*>::const_iterator i(element.begin()); i != element.end(); ++i) {
    160         if (!(*i)->isFormControlElement())
    161             continue;
    162 
    163         HTMLFormControlElement* control = toHTMLFormControlElement(*i);
    164 
    165         if (control->isDisabledFormControl() || control->name().isNull())
    166             continue;
    167 
    168         if (!IsInDefaultState(control) || isHTMLTextAreaElement(control))
    169             return 0;
    170 
    171         if (control->hasTagName(HTMLNames::inputTag) && control->willValidate()) {
    172             const HTMLInputElement* input = toHTMLInputElement(control);
    173 
    174             // Return nothing if a file upload field or a password field are found.
    175             if (input->isFileUpload() || input->isPasswordField())
    176                 return 0;
    177 
    178             if (input->isTextField()) {
    179                 if (textElement) {
    180                     // The auto-complete bar only knows how to fill in one value.
    181                     // This form has multiple fields; don't treat it as searchable.
    182                     return 0;
    183                 }
    184                 textElement = toHTMLInputElement(control);
    185             }
    186         }
    187     }
    188     return textElement;
    189 }
    190 
    191 // Build a search string based on a given HTMLFormElement and HTMLInputElement
    192 //
    193 // Search string output example from www.google.com:
    194 // "hl=en&source=hp&biw=1085&bih=854&q={searchTerms}&btnG=Google+Search&aq=f&aqi=&aql=&oq="
    195 //
    196 // Return false if the provided HTMLInputElement is not found in the form
    197 bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString, WTF::TextEncoding* encoding, const HTMLInputElement* textElement)
    198 {
    199     bool isElementFound = false;
    200 
    201     Vector<FormAssociatedElement*> elements = form->associatedElements();
    202     for (Vector<FormAssociatedElement*>::const_iterator i(elements.begin()); i != elements.end(); ++i) {
    203         if (!(*i)->isFormControlElement())
    204             continue;
    205 
    206         HTMLFormControlElement* control = toHTMLFormControlElement(*i);
    207 
    208         if (control->isDisabledFormControl() || control->name().isNull())
    209             continue;
    210 
    211         FormDataList dataList(*encoding);
    212         if (!control->appendFormData(dataList, false))
    213             continue;
    214 
    215         const Vector<FormDataList::Item>& items = dataList.items();
    216 
    217         for (Vector<FormDataList::Item>::const_iterator j(items.begin()); j != items.end(); ++j) {
    218             // Handle ISINDEX / <input name=isindex> specially, but only if it's
    219             // the first entry.
    220             if (!encodedString->isEmpty() || j->data() != "isindex") {
    221                 if (!encodedString->isEmpty())
    222                     encodedString->append('&');
    223                 FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
    224                 encodedString->append('=');
    225             }
    226             ++j;
    227             if (control == textElement) {
    228                 encodedString->append("{searchTerms}", 13);
    229                 isElementFound = true;
    230             } else
    231                 FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
    232         }
    233     }
    234     return isElementFound;
    235 }
    236 } // namespace
    237 
    238 namespace blink {
    239 
    240 WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const WebInputElement& selectedInputElement)
    241 {
    242     RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormElement>();
    243     HTMLInputElement* inputElement = selectedInputElement.operator PassRefPtr<HTMLInputElement>().get();
    244 
    245     // Only consider forms that GET data.
    246     // Allow HTTPS only when an input element is provided.
    247     if (equalIgnoringCase(formElement->getAttribute(methodAttr), "post")
    248         || (!IsHTTPFormSubmit(formElement.get()) && !inputElement))
    249         return;
    250 
    251     Vector<char> encodedString;
    252     WTF::TextEncoding encoding;
    253 
    254     GetFormEncoding(formElement.get(), &encoding);
    255     if (!encoding.isValid()) {
    256         // Need a valid encoding to encode the form elements.
    257         // If the encoding isn't found webkit ends up replacing the params with
    258         // empty strings. So, we don't try to do anything here.
    259         return;
    260     }
    261 
    262     // Look for a suitable search text field in the form when a
    263     // selectedInputElement is not provided.
    264     if (!inputElement) {
    265         inputElement = findSuitableSearchInputElement(formElement.get());
    266 
    267         // Return if no suitable text element has been found.
    268         if (!inputElement)
    269             return;
    270     }
    271 
    272     HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get());
    273     if (firstSubmitButton) {
    274         // The form does not have an active submit button, make the first button
    275         // active. We need to do this, otherwise the URL will not contain the
    276         // name of the submit button.
    277         firstSubmitButton->setActivatedSubmit(true);
    278     }
    279 
    280     bool isValidSearchString = buildSearchString(formElement.get(), &encodedString, &encoding, inputElement);
    281 
    282     if (firstSubmitButton)
    283         firstSubmitButton->setActivatedSubmit(false);
    284 
    285     // Return if the search string is not valid.
    286     if (!isValidSearchString)
    287         return;
    288 
    289     String action(formElement->action());
    290     KURL url(formElement->document().completeURL(action.isNull() ? "" : action));
    291     RefPtr<FormData> formData = FormData::create(encodedString);
    292     url.setQuery(formData->flattenToString());
    293     m_url = url;
    294     m_encoding = String(encoding.name());
    295 }
    296 
    297 } // namespace blink
    298