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

Unified Diff: Source/web/WebSearchableFormData.cpp

Issue 1305063008: Clean WebSearchableFormData.cpp up. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 3 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/web/WebSearchableFormData.cpp
diff --git a/Source/web/WebSearchableFormData.cpp b/Source/web/WebSearchableFormData.cpp
index 57a248177aefff2d0447ecac0f2e926dde98a48f..f128f0dfb0934924c8d5995794de127de6d6f1a8 100644
--- a/Source/web/WebSearchableFormData.cpp
+++ b/Source/web/WebSearchableFormData.cpp
@@ -52,43 +52,45 @@ using namespace HTMLNames;
namespace {
// Gets the encoding for the form.
-void GetFormEncoding(const HTMLFormElement* form, WTF::TextEncoding* encoding)
+// TODO(tkent): Use FormDataBuilder::encodingFromAcceptCharset().
+void getFormEncoding(const HTMLFormElement& form, WTF::TextEncoding* encoding)
{
- String str(form->getAttribute(HTMLNames::accept_charsetAttr));
+ String str(form.fastGetAttribute(HTMLNames::accept_charsetAttr));
str.replace(',', ' ');
Vector<String> charsets;
str.split(' ', charsets);
- for (Vector<String>::const_iterator i(charsets.begin()); i != charsets.end(); ++i) {
- *encoding = WTF::TextEncoding(*i);
+ for (const String& charset : charsets) {
+ *encoding = WTF::TextEncoding(charset);
if (encoding->isValid())
return;
}
- if (!form->document().loader())
- return;
- *encoding = WTF::TextEncoding(form->document().encoding());
+ if (form.document().loader())
+ *encoding = WTF::TextEncoding(form.document().encoding());
}
// Returns true if the submit request results in an HTTP URL.
-bool IsHTTPFormSubmit(const HTMLFormElement* form)
+bool isHTTPFormSubmit(const HTMLFormElement& form)
{
- // FIXME: This function is insane. This is an overly complicated way to get this information.
- String action(form->action());
- // The isNull() check is trying to avoid completeURL returning KURL() when passed a null string.
- return form->document().completeURL(action.isNull() ? "" : action).protocolIs("http");
+ // FIXME: This function is insane. This is an overly complicated way to get
+ // this information.
+ String action(form.action());
+ // The isNull() check is trying to avoid completeURL returning KURL() when
+ // passed a null string.
+ return form.document().completeURL(action.isNull() ? "" : action).protocolIs("http");
}
// If the form does not have an activated submit button, the first submit
// button is returned.
-HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form)
+HTMLFormControlElement* buttonToActivate(const HTMLFormElement& form)
{
HTMLFormControlElement* firstSubmitButton = nullptr;
- const FormAssociatedElement::List& element = form->associatedElements();
- for (FormAssociatedElement::List::const_iterator i(element.begin()); i != element.end(); ++i) {
- if (!(*i)->isFormControlElement())
+ for (auto& element : form.associatedElements()) {
+ if (!element->isFormControlElement())
continue;
- HTMLFormControlElement* control = toHTMLFormControlElement(*i);
+ HTMLFormControlElement* control = toHTMLFormControlElement(element);
if (control->isActivatedSubmit()) {
- // There's a button that is already activated for submit, return nullptr.
+ // There's a button that is already activated for submit, return
+ // nullptr.
return nullptr;
}
if (!firstSubmitButton && control->isSuccessfulSubmitButton())
@@ -99,15 +101,15 @@ HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form)
// Returns true if the selected state of all the options matches the default
// selected state.
-bool IsSelectInDefaultState(HTMLSelectElement* select)
+bool isSelectInDefaultState(const HTMLSelectElement& select)
{
- const WillBeHeapVector<RawPtrWillBeMember<HTMLElement>>& listItems = select->listItems();
- if (select->multiple() || select->size() > 1) {
- for (WillBeHeapVector<RawPtrWillBeMember<HTMLElement>>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
- if (!isHTMLOptionElement(*i))
+ const WillBeHeapVector<RawPtrWillBeMember<HTMLElement>>& listItems = select.listItems();
+ if (select.multiple() || select.size() > 1) {
+ for (const auto& item : listItems) {
+ if (!isHTMLOptionElement(*item))
continue;
- HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
- if (optionElement->selected() != optionElement->hasAttribute(selectedAttr))
+ HTMLOptionElement* optionElement = toHTMLOptionElement(item);
+ if (optionElement->selected() != optionElement->fastHasAttribute(selectedAttr))
return false;
}
return true;
@@ -116,11 +118,11 @@ bool IsSelectInDefaultState(HTMLSelectElement* select)
// The select is rendered as a combobox (called menulist in WebKit). At
// least one item is selected, determine which one.
HTMLOptionElement* initialSelected = nullptr;
- for (WillBeHeapVector<RawPtrWillBeMember<HTMLElement>>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
- if (!isHTMLOptionElement(*i))
+ for (const auto& item : listItems) {
+ if (!isHTMLOptionElement(*item))
continue;
- HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
- if (optionElement->hasAttribute(selectedAttr)) {
+ HTMLOptionElement* optionElement = toHTMLOptionElement(item);
+ if (optionElement->fastHasAttribute(selectedAttr)) {
// The page specified the option to select.
initialSelected = optionElement;
break;
@@ -134,16 +136,16 @@ bool IsSelectInDefaultState(HTMLSelectElement* select)
// 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 state of the checked attribute.
-bool IsInDefaultState(HTMLFormControlElement* formElement)
+// in its default state if the checked state matches the state of the checked
+// attribute.
+bool isInDefaultState(const HTMLFormControlElement& formElement)
{
- ASSERT(formElement);
- if (isHTMLInputElement(*formElement)) {
- const HTMLInputElement& inputElement = toHTMLInputElement(*formElement);
+ if (isHTMLInputElement(formElement)) {
+ const HTMLInputElement& inputElement = toHTMLInputElement(formElement);
if (inputElement.type() == InputTypeNames::checkbox || inputElement.type() == InputTypeNames::radio)
- return inputElement.checked() == inputElement.hasAttribute(checkedAttr);
- } else if (isHTMLSelectElement(*formElement)) {
- return IsSelectInDefaultState(toHTMLSelectElement(formElement));
+ return inputElement.checked() == inputElement.fastHasAttribute(checkedAttr);
+ } else if (isHTMLSelectElement(formElement)) {
+ return isSelectInDefaultState(toHTMLSelectElement(formElement));
}
return true;
}
@@ -154,36 +156,37 @@ bool IsInDefaultState(HTMLFormControlElement* formElement)
// - A file upload field
// - A Password field
// - More than one text field
-HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form)
+HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement& form)
{
HTMLInputElement* textElement = nullptr;
- const FormAssociatedElement::List& element = form->associatedElements();
- for (FormAssociatedElement::List::const_iterator i(element.begin()); i != element.end(); ++i) {
- if (!(*i)->isFormControlElement())
+ for (const auto& item : form.associatedElements()) {
+ if (!item->isFormControlElement())
continue;
- HTMLFormControlElement* control = toHTMLFormControlElement(*i);
+ HTMLFormControlElement& control = toHTMLFormControlElement(*item);
- if (control->isDisabledFormControl() || control->name().isNull())
+ if (control.isDisabledFormControl() || control.name().isNull())
continue;
- if (!IsInDefaultState(control) || isHTMLTextAreaElement(*control))
+ if (!isInDefaultState(control) || isHTMLTextAreaElement(control))
return nullptr;
- if (isHTMLInputElement(*control) && control->willValidate()) {
- const HTMLInputElement& input = toHTMLInputElement(*control);
+ if (isHTMLInputElement(control) && control.willValidate()) {
+ const HTMLInputElement& input = toHTMLInputElement(control);
- // Return nothing if a file upload field or a password field are found.
+ // Return nothing if a file upload field or a password field are
+ // found.
if (input.type() == InputTypeNames::file || input.type() == InputTypeNames::password)
return nullptr;
if (input.isTextField()) {
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.
+ // The auto-complete bar only knows how to fill in one
+ // value. This form has multiple fields; don't treat it as
+ // searchable.
return nullptr;
}
- textElement = toHTMLInputElement(control);
+ textElement = toHTMLInputElement(&control);
}
}
}
@@ -196,22 +199,19 @@ HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form)
// "hl=en&source=hp&biw=1085&bih=854&q={searchTerms}&btnG=Google+Search&aq=f&aqi=&aql=&oq="
//
// Return false if the provided HTMLInputElement is not found in the form
-bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString, WTF::TextEncoding* encoding, const HTMLInputElement* textElement)
+bool buildSearchString(const HTMLFormElement& form, Vector<char>* encodedString, const WTF::TextEncoding& encoding, const HTMLInputElement* textElement)
{
bool isElementFound = false;
-
- const FormAssociatedElement::List& elements = form->associatedElements();
- for (FormAssociatedElement::List::const_iterator i(elements.begin()); i != elements.end(); ++i) {
- if (!(*i)->isFormControlElement())
+ for (const auto& item : form.associatedElements()) {
+ if (!item->isFormControlElement())
continue;
- HTMLFormControlElement* control = toHTMLFormControlElement(*i);
-
- if (control->isDisabledFormControl() || control->name().isNull())
+ HTMLFormControlElement& control = toHTMLFormControlElement(*item);
+ if (control.isDisabledFormControl() || control.name().isNull())
continue;
- DOMFormData* formData = DOMFormData::create(*encoding);
- if (!control->appendFormData(*formData, false))
+ DOMFormData* formData = DOMFormData::create(encoding);
+ if (!control.appendFormData(*formData, false))
continue;
for (const FormDataList::Item& item : formData->items()) {
@@ -219,7 +219,7 @@ bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString,
encodedString->append('&');
FormDataBuilder::encodeStringAsFormData(*encodedString, item.key());
encodedString->append('=');
- if (control == textElement) {
+ if (&control == textElement) {
encodedString->append("{searchTerms}", 13);
isElementFound = true;
} else {
@@ -229,6 +229,7 @@ bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString,
}
return isElementFound;
}
+
} // namespace
WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const WebInputElement& selectedInputElement)
@@ -239,13 +240,11 @@ WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const W
// Only consider forms that GET data.
// Allow HTTPS only when an input element is provided.
if (equalIgnoringCase(formElement->getAttribute(methodAttr), "post")
- || (!IsHTTPFormSubmit(formElement.get()) && !inputElement))
+ || (!isHTTPFormSubmit(*formElement) && !inputElement))
return;
- Vector<char> encodedString;
WTF::TextEncoding encoding;
-
- GetFormEncoding(formElement.get(), &encoding);
+ getFormEncoding(*formElement, &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
@@ -256,14 +255,14 @@ WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const W
// Look for a suitable search text field in the form when a
// selectedInputElement is not provided.
if (!inputElement) {
- inputElement = findSuitableSearchInputElement(formElement.get());
+ inputElement = findSuitableSearchInputElement(*formElement);
// Return if no suitable text element has been found.
if (!inputElement)
return;
}
- HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get());
+ HTMLFormControlElement* firstSubmitButton = buttonToActivate(*formElement);
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
@@ -271,7 +270,8 @@ WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const W
firstSubmitButton->setActivatedSubmit(true);
}
- bool isValidSearchString = buildSearchString(formElement.get(), &encodedString, &encoding, inputElement);
+ Vector<char> encodedString;
+ bool isValidSearchString = buildSearchString(*formElement, &encodedString, encoding, inputElement);
if (firstSubmitButton)
firstSubmitButton->setActivatedSubmit(false);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698