This source file includes following definitions.
- GetFormEncoding
- IsHTTPFormSubmit
- GetButtonToActivate
- IsSelectInDefaultState
- IsInDefaultState
- findSuitableSearchInputElement
- buildSearchString
#include "config.h"
#include "WebSearchableFormData.h"
#include "HTMLNames.h"
#include "WebFormElement.h"
#include "WebInputElement.h"
#include "core/dom/Document.h"
#include "core/html/FormDataList.h"
#include "core/html/HTMLFormControlElement.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLOptionElement.h"
#include "core/html/HTMLSelectElement.h"
#include "platform/network/FormDataBuilder.h"
#include "wtf/text/TextEncoding.h"
using namespace WebCore;
using namespace HTMLNames;
namespace {
void GetFormEncoding(const HTMLFormElement* form, WTF::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 = WTF::TextEncoding(*i);
if (encoding->isValid())
return;
}
if (!form->document().loader())
return;
*encoding = WTF::TextEncoding(form->document().encoding());
}
bool IsHTTPFormSubmit(const HTMLFormElement* form)
{
String action(form->action());
return form->document().completeURL(action.isNull() ? "" : action).protocolIs("http");
}
HTMLFormControlElement* GetButtonToActivate(HTMLFormElement* form)
{
HTMLFormControlElement* firstSubmitButton = 0;
const Vector<FormAssociatedElement*>& element = form->associatedElements();
for (Vector<FormAssociatedElement*>::const_iterator i(element.begin()); i != element.end(); ++i) {
if (!(*i)->isFormControlElement())
continue;
HTMLFormControlElement* control = toHTMLFormControlElement(*i);
if (control->isActivatedSubmit()) {
return 0;
}
if (!firstSubmitButton && control->isSuccessfulSubmitButton())
firstSubmitButton = control;
}
return firstSubmitButton;
}
bool IsSelectInDefaultState(HTMLSelectElement* select)
{
const Vector<HTMLElement*>& listItems = select->listItems();
if (select->multiple() || select->size() > 1) {
for (Vector<HTMLElement*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
if (!(*i)->hasLocalName(HTMLNames::optionTag))
continue;
HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
if (optionElement->selected() != optionElement->hasAttribute(selectedAttr))
return false;
}
return true;
}
HTMLOptionElement* initialSelected = 0;
for (Vector<HTMLElement*>::const_iterator i(listItems.begin()); i != listItems.end(); ++i) {
if (!(*i)->hasLocalName(HTMLNames::optionTag))
continue;
HTMLOptionElement* optionElement = toHTMLOptionElement(*i);
if (optionElement->hasAttribute(selectedAttr)) {
initialSelected = optionElement;
break;
}
if (!initialSelected)
initialSelected = optionElement;
}
return !initialSelected || initialSelected->selected();
}
bool IsInDefaultState(HTMLFormControlElement* formElement)
{
ASSERT(formElement);
if (isHTMLInputElement(*formElement)) {
const HTMLInputElement& inputElement = toHTMLInputElement(*formElement);
if (inputElement.isCheckbox() || inputElement.isRadioButton())
return inputElement.checked() == inputElement.hasAttribute(checkedAttr);
} else if (isHTMLSelectElement(*formElement)) {
return IsSelectInDefaultState(toHTMLSelectElement(formElement));
}
return true;
}
HTMLInputElement* findSuitableSearchInputElement(const HTMLFormElement* form)
{
HTMLInputElement* textElement = 0;
const Vector<FormAssociatedElement*>& element = form->associatedElements();
for (Vector<FormAssociatedElement*>::const_iterator i(element.begin()); i != element.end(); ++i) {
if (!(*i)->isFormControlElement())
continue;
HTMLFormControlElement* control = toHTMLFormControlElement(*i);
if (control->isDisabledFormControl() || control->name().isNull())
continue;
if (!IsInDefaultState(control) || isHTMLTextAreaElement(*control))
return 0;
if (isHTMLInputElement(*control) && control->willValidate()) {
const HTMLInputElement& input = toHTMLInputElement(*control);
if (input.isFileUpload() || input.isPasswordField())
return 0;
if (input.isTextField()) {
if (textElement) {
return 0;
}
textElement = toHTMLInputElement(control);
}
}
}
return textElement;
}
bool buildSearchString(const HTMLFormElement* form, Vector<char>* encodedString, WTF::TextEncoding* encoding, const HTMLInputElement* textElement)
{
bool isElementFound = false;
Vector<FormAssociatedElement*> elements = form->associatedElements();
for (Vector<FormAssociatedElement*>::const_iterator i(elements.begin()); i != elements.end(); ++i) {
if (!(*i)->isFormControlElement())
continue;
HTMLFormControlElement* control = toHTMLFormControlElement(*i);
if (control->isDisabledFormControl() || control->name().isNull())
continue;
FormDataList dataList(*encoding);
if (!control->appendFormData(dataList, false))
continue;
const WillBeHeapVector<FormDataList::Item>& items = dataList.items();
for (WillBeHeapVector<FormDataList::Item>::const_iterator j(items.begin()); j != items.end(); ++j) {
if (!encodedString->isEmpty())
encodedString->append('&');
FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
encodedString->append('=');
++j;
if (control == textElement) {
encodedString->append("{searchTerms}", 13);
isElementFound = true;
} else
FormDataBuilder::encodeStringAsFormData(*encodedString, j->data());
}
}
return isElementFound;
}
}
namespace blink {
WebSearchableFormData::WebSearchableFormData(const WebFormElement& form, const WebInputElement& selectedInputElement)
{
RefPtr<HTMLFormElement> formElement = form.operator PassRefPtr<HTMLFormElement>();
HTMLInputElement* inputElement = selectedInputElement.operator PassRefPtr<HTMLInputElement>().get();
if (equalIgnoringCase(formElement->getAttribute(methodAttr), "post")
|| (!IsHTTPFormSubmit(formElement.get()) && !inputElement))
return;
Vector<char> encodedString;
WTF::TextEncoding encoding;
GetFormEncoding(formElement.get(), &encoding);
if (!encoding.isValid()) {
return;
}
if (!inputElement) {
inputElement = findSuitableSearchInputElement(formElement.get());
if (!inputElement)
return;
}
HTMLFormControlElement* firstSubmitButton = GetButtonToActivate(formElement.get());
if (firstSubmitButton) {
firstSubmitButton->setActivatedSubmit(true);
}
bool isValidSearchString = buildSearchString(formElement.get(), &encodedString, &encoding, inputElement);
if (firstSubmitButton)
firstSubmitButton->setActivatedSubmit(false);
if (!isValidSearchString)
return;
String action(formElement->action());
KURL url(formElement->document().completeURL(action.isNull() ? "" : action));
RefPtr<FormData> formData = FormData::create(encodedString);
url.setQuery(formData->flattenToString());
m_url = url;
m_encoding = String(encoding.name());
}
}