Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Unified Diff: webkit/api/src/WebSearchableFormData.cpp

Issue 306057: Move SearchableFormData over to the WebKit API. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webkit/api/public/WebSearchableFormData.h ('k') | webkit/glue/searchable_form_data.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webkit/api/src/WebSearchableFormData.cpp
===================================================================
--- webkit/api/src/WebSearchableFormData.cpp (revision 29420)
+++ webkit/api/src/WebSearchableFormData.cpp (working copy)
@@ -1,12 +1,36 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
+/*
+ * Copyright (C) 2009 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
#include "config.h"
+#include "WebSearchableFormData.h"
-#include "base/compiler_specific.h"
-
-MSVC_PUSH_WARNING_LEVEL(0);
#include "Document.h"
#include "FormDataBuilder.h"
#include "FormDataList.h"
@@ -19,236 +43,214 @@
#include "HTMLOptionsCollection.h"
#include "HTMLSelectElement.h"
#include "TextEncoding.h"
-MSVC_POP_WARNING();
+#include "WebForm.h"
-#undef LOG
-
-#include "webkit/glue/glue_util.h"
-#include "webkit/glue/searchable_form_data.h"
-
using namespace WebCore;
-namespace webkit_glue {
-
namespace {
// Gets the encoding for the form.
-void GetFormEncoding(HTMLFormElement* form, TextEncoding* encoding) {
- String str(form->getAttribute(HTMLNames::accept_charsetAttr));
- str.replace(',', ' ');
- Vector<String> charsets;
- str.split(' ', charsets);
- for (Vector<String>::const_iterator i(charsets.begin()); i != charsets.end();
- ++i) {
- *encoding = TextEncoding(*i);
- if (encoding->isValid())
- return;
- }
- const Frame* frame = form->document()->frame();
- *encoding = frame ?
- TextEncoding(frame->loader()->encoding()) : Latin1Encoding();
+void GetFormEncoding(const HTMLFormElement* form, TextEncoding* encoding)
+{
+ String str(form->getAttribute(HTMLNames::accept_charsetAttr));
+ str.replace(',', ' ');
+ Vector<String> charsets;
+ str.split(' ', charsets);
+ for (Vector<String>::const_iterator i(charsets.begin()); i != charsets.end(); ++i) {
+ *encoding = TextEncoding(*i);
+ if (encoding->isValid())
+ return;
+ }
+ const Frame* frame = form->document()->frame();
+ *encoding = frame ? TextEncoding(frame->loader()->encoding()) : Latin1Encoding();
}
// Returns true if the submit request results in an HTTP URL.
-bool IsHTTPFormSubmit(HTMLFormElement* form) {
- String action(form->action());
- return form->document()->frame()->loader()->
- completeURL(action.isNull() ? "" : action).protocol() == "http";
+bool IsHTTPFormSubmit(const HTMLFormElement* form)
+{
+ String action(form->action());
+ return form->document()->frame()->loader()->completeURL(action.isNull() ? "" : action).protocol() == "http";
}
// If the form does not have an activated submit button, the first submit
// button is returned.
-HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form) {
- HTMLFormControlElement* first_submit_button = NULL;
- for (Vector<HTMLFormControlElement*>::const_iterator
- i(form->formElements.begin()); i != form->formElements.end(); ++i) {
- HTMLFormControlElement* form_element = *i;
- if (form_element->isActivatedSubmit()) {
- // There's a button that is already activated for submit, return NULL.
- return NULL;
- } else if (first_submit_button == NULL &&
- form_element->isSuccessfulSubmitButton()) {
- first_submit_button = form_element;
+HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form)
+{
+ HTMLFormControlElement* firstSubmitButton = 0;
+ for (Vector<HTMLFormControlElement*>::const_iterator i(form->formElements.begin()); i != form->formElements.end(); ++i) {
+ HTMLFormControlElement* formElement = *i;
+ if (formElement->isActivatedSubmit()) {
+ // There's a button that is already activated for submit, return 0.
+ return 0;
+ } else if (!firstSubmitButton && formElement->isSuccessfulSubmitButton())
+ firstSubmitButton = formElement;
}
- }
- return first_submit_button;
+ return firstSubmitButton;
}
// Returns true if the selected state of all the options matches the default
// selected state.
-bool IsSelectInDefaultState(HTMLSelectElement* select) {
- const Vector<Element*>& list_items = select->listItems();
- if (select->multiple() || select->size() > 1) {
- for (Vector<Element*>::const_iterator i(list_items.begin());
- i != list_items.end(); ++i) {
- if (!(*i)->hasLocalName(HTMLNames::optionTag))
- continue;
- const HTMLOptionElement* option_element =
- static_cast<const HTMLOptionElement*>(*i);
- if (option_element->selected() != option_element->defaultSelected())
- return false;
+bool IsSelectInDefaultState(const HTMLSelectElement* select)
+{
+ const Vector<Element*>& listItems = select->listItems();
+ if (select->multiple() || select->size() > 1) {
+ for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
+ if (!(*i)->hasLocalName(HTMLNames::optionTag))
+ continue;
+ const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i);
+ if (optionElement->selected() != optionElement->defaultSelected())
+ return false;
+ }
+ return true;
}
- return true;
- }
- // The select is rendered as a combobox (called menulist in WebKit). At
- // least one item is selected, determine which one.
- const HTMLOptionElement* initial_selected = NULL;
- for (Vector<Element*>::const_iterator i(list_items.begin());
- i != list_items.end(); ++i) {
- if (!(*i)->hasLocalName(HTMLNames::optionTag))
- continue;
- const HTMLOptionElement* option_element =
- static_cast<const HTMLOptionElement*>(*i);
- if (option_element->defaultSelected()) {
- // The page specified the option to select.
- initial_selected = option_element;
- break;
- } else if (!initial_selected) {
- initial_selected = option_element;
+ // The select is rendered as a combobox (called menulist in WebKit). At
+ // least one item is selected, determine which one.
+ const HTMLOptionElement* initialSelected = 0;
+ for (Vector<Element*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
+ if (!(*i)->hasLocalName(HTMLNames::optionTag))
+ continue;
+ const HTMLOptionElement* optionElement = static_cast<const HTMLOptionElement*>(*i);
+ if (optionElement->defaultSelected()) {
+ // The page specified the option to select.
+ initialSelected = optionElement;
+ break;
+ } else if (!initialSelected)
+ initialSelected = optionElement;
}
- }
- return initial_selected ? initial_selected->selected() : true;
+ return initialSelected ? initialSelected->selected() : true;
}
// Returns true if the form element is in its default state, false otherwise.
// The default state is the state of the form element on initial load of the
// page, and varies depending upon the form element. For example, a checkbox is
// in its default state if the checked state matches the defaultChecked state.
-bool IsInDefaultState(HTMLFormControlElement* form_element) {
- if (form_element->hasTagName(HTMLNames::inputTag)) {
- const HTMLInputElement* input_element =
- static_cast<const HTMLInputElement*>(form_element);
- if (input_element->inputType() == HTMLInputElement::CHECKBOX ||
- input_element->inputType() == HTMLInputElement::RADIO) {
- return input_element->checked() == input_element->defaultChecked();
- }
- } else if (form_element->hasTagName(HTMLNames::selectTag)) {
- return IsSelectInDefaultState(
- static_cast<HTMLSelectElement*>(form_element));
- }
- return true;
+bool IsInDefaultState(const HTMLFormControlElement* formElement)
+{
+ if (formElement->hasTagName(HTMLNames::inputTag)) {
+ const HTMLInputElement* inputElement = static_cast<const HTMLInputElement*>(formElement);
+ if (inputElement->inputType() == HTMLInputElement::CHECKBOX || inputElement->inputType() == HTMLInputElement::RADIO)
+ return inputElement->checked() == inputElement->defaultChecked();
+ } else if (formElement->hasTagName(HTMLNames::selectTag))
+ return IsSelectInDefaultState(static_cast<const HTMLSelectElement*>(formElement));
+ return true;
}
// If form has only one text input element, return true. If a valid input
// element is not found, return false. Additionally, the form data for all
// elements is added to enc_string and the encoding used is set in
// encoding_name.
-bool HasSuitableTextElement(HTMLFormElement* form,
- Vector<char>* enc_string,
- std::string* encoding_name) {
- TextEncoding encoding;
- GetFormEncoding(form, &encoding);
- if (!encoding.isValid()) {
- // Need a valid encoding to encode the form elements.
- // If the encoding isn't found webkit ends up replacing the params with
- // empty strings. So, we don't try to do anything here.
- return NULL;
- }
- *encoding_name = encoding.name();
+bool HasSuitableTextElement(const HTMLFormElement* form, Vector<char>* encodedString, String* encodingName)
+{
+ TextEncoding encoding;
+ GetFormEncoding(form, &encoding);
+ if (!encoding.isValid()) {
+ // Need a valid encoding to encode the form elements.
+ // If the encoding isn't found webkit ends up replacing the params with
+ // empty strings. So, we don't try to do anything here.
+ return 0;
+ }
+ *encodingName = encoding.name();
- HTMLInputElement* text_element = NULL;
- for (Vector<HTMLFormControlElement*>::const_iterator
- i(form->formElements.begin()); i != form->formElements.end(); ++i) {
- HTMLFormControlElement* form_element = *i;
- if (form_element->disabled() || form_element->name().isNull())
- continue;
+ HTMLInputElement* textElement = 0;
+ for (Vector<HTMLFormControlElement*>::const_iterator i(form->formElements.begin()); i != form->formElements.end(); ++i) {
+ HTMLFormControlElement* formElement = *i;
+ if (formElement->disabled() || formElement->name().isNull())
+ continue;
- if (!IsInDefaultState(form_element) ||
- form_element->hasTagName(HTMLNames::textareaTag))
- return NULL;
+ if (!IsInDefaultState(formElement) || formElement->hasTagName(HTMLNames::textareaTag))
+ return 0;
- bool is_text_element = false;
- if (form_element->hasTagName(HTMLNames::inputTag)) {
- switch (static_cast<const HTMLInputElement*>(form_element)->inputType()) {
- case HTMLInputElement::TEXT:
- case HTMLInputElement::ISINDEX:
- is_text_element = true;
- break;
- case HTMLInputElement::PASSWORD:
- // Don't store passwords! This is most likely an https anyway.
- // Fall through.
- case HTMLInputElement::FILE:
- // Too big, don't try to index this.
- return NULL;
- default:
- // All other input types are indexable.
- break;
+ bool isTextElement = false;
+ if (formElement->hasTagName(HTMLNames::inputTag)) {
+ switch (static_cast<const HTMLInputElement*>(formElement)->inputType()) {
+ case HTMLInputElement::TEXT:
+ case HTMLInputElement::ISINDEX:
+ isTextElement = true;
+ break;
+ case HTMLInputElement::PASSWORD:
+ // Don't store passwords! This is most likely an https anyway.
+ // Fall through.
+ case HTMLInputElement::FILE:
+ // Too big, don't try to index this.
+ return 0;
+ default:
+ // All other input types are indexable.
+ break;
+ }
}
- }
- FormDataList data_list(encoding);
- if (!form_element->appendFormData(data_list, false))
- continue;
+ FormDataList dataList(encoding);
+ if (!formElement->appendFormData(dataList, false))
+ continue;
- const Vector<FormDataList::Item>& item_list = data_list.list();
- if (is_text_element && !item_list.isEmpty()) {
- if (text_element != NULL) {
- // The auto-complete bar only knows how to fill in one value.
- // This form has multiple fields; don't treat it as searchable.
- return false;
+ const Vector<FormDataList::Item>& itemList = dataList.list();
+ if (isTextElement && !itemList.isEmpty()) {
+ if (textElement) {
+ // The auto-complete bar only knows how to fill in one value.
+ // This form has multiple fields; don't treat it as searchable.
+ return false;
+ }
+ textElement = static_cast<HTMLInputElement*>(formElement);
}
- text_element = static_cast<HTMLInputElement*>(form_element);
- }
- for (Vector<FormDataList::Item>::const_iterator j(item_list.begin());
- j != item_list.end(); ++j) {
- // Handle ISINDEX / <input name=isindex> specially, but only if it's
- // the first entry.
- if (!enc_string->isEmpty() || j->data() != "isindex") {
- if (!enc_string->isEmpty())
- enc_string->append('&');
- FormDataBuilder::encodeStringAsFormData(*enc_string, j->data());
- enc_string->append('=');
+ for (Vector<FormDataList::Item>::const_iterator j(itemList.begin()); j != itemList.end(); ++j) {
+ // Handle ISINDEX / <input name=isindex> specially, but only if it's
+ // the first entry.
+ if (!encodedString->isEmpty() || j->data() != "isindex") {
+ if (!encodedString->isEmpty())
+ encodedString->append('&');
+ FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
+ encodedString->append('=');
+ }
+ ++j;
+ if (formElement == textElement)
+ encodedString->append("{searchTerms}", 13);
+ else
+ FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
}
- ++j;
- if (form_element == text_element)
- enc_string->append("{searchTerms}", 13);
- else
- FormDataBuilder::encodeStringAsFormData(*enc_string, j->data());
}
- }
- return text_element != NULL;
+ return textElement;
}
} // namespace
-SearchableFormData* SearchableFormData::Create(const WebKit::WebForm& webform) {
- RefPtr<HTMLFormElement> form = WebFormToHTMLFormElement(webform);
- const Frame* frame = form->document()->frame();
- if (frame == NULL)
- return NULL;
+namespace WebKit {
- // Only consider forms that GET data and the action targets an http page.
- if (equalIgnoringCase(form->getAttribute(HTMLNames::methodAttr), "post") ||
- !IsHTTPFormSubmit(form.get()))
- return NULL;
+WebSearchableFormData::WebSearchableFormData(const WebForm& form)
+{
+ RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormElement>();
+ const Frame* frame = formElement->document()->frame();
+ if (!frame)
+ return;
- HTMLFormControlElement* first_submit_button = GetButtonToActivate(form.get());
- if (first_submit_button) {
- // The form does not have an active submit button, make the first button
- // active. We need to do this, otherwise the URL will not contain the
- // name of the submit button.
- first_submit_button->setActivatedSubmit(true);
- }
- Vector<char> enc_string;
- std::string encoding;
- bool has_element = HasSuitableTextElement(form.get(), &enc_string, &encoding);
- if (first_submit_button)
- first_submit_button->setActivatedSubmit(false);
- if (!has_element) {
- // Not a searchable form.
- return NULL;
- }
+ // Only consider forms that GET data and the action targets an http page.
+ if (equalIgnoringCase(formElement->getAttribute(HTMLNames::methodAttr), "post") || !IsHTTPFormSubmit(formElement.get()))
+ return;
- // It's a valid form.
- // Generate the URL and create a new SearchableFormData.
- RefPtr<FormData> form_data = FormData::create(enc_string);
- String action(form->action());
- KURL url = frame->loader()->completeURL(action.isNull() ? "" : action);
- url.setQuery(form_data->flattenToString());
- GURL gurl(KURLToGURL(url));
- return new SearchableFormData(gurl, encoding);
+ HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get());
+ if (firstSubmitButton) {
+ // The form does not have an active submit button, make the first button
+ // active. We need to do this, otherwise the URL will not contain the
+ // name of the submit button.
+ firstSubmitButton->setActivatedSubmit(true);
+ }
+ Vector<char> encodedString;
+ String encoding;
+ bool hasElement = HasSuitableTextElement(formElement.get(), &encodedString, &encoding);
+ if (firstSubmitButton)
+ firstSubmitButton->setActivatedSubmit(false);
+ if (!hasElement) {
+ // Not a searchable form.
+ return;
+ }
+
+ String action(formElement->action());
+ KURL url(frame->loader()->completeURL(action.isNull() ? "" : action));
+ RefPtr<FormData> formData = FormData::create(encodedString);
+ url.setQuery(formData->flattenToString());
+ m_url = url;
+ m_encoding = encoding;
}
-} // namespace webkit_glue
+} // namespace WebKit
« no previous file with comments | « webkit/api/public/WebSearchableFormData.h ('k') | webkit/glue/searchable_form_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698