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