From 41e342f3745b27ba8eeaa87187e3465de484c2a4 Mon Sep 17 00:00:00 2001 From: "jhawkins@chromium.org" Date: Wed, 21 Jul 2010 19:53:23 +0000 Subject: Fix for: State in small letters should be auto-filled from the profile. BUG=38222 TEST=in the bug Patch by George Yakovleg Review URL: http://codereview.chromium.org/2832064 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53234 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/autofill/autofill_manager.cc | 30 ++++++++++++++++++++++++++++- chrome/browser/autofill/autofill_manager.h | 7 +++++++ chrome/common/render_messages.h | 4 ++++ chrome/renderer/form_manager.cc | 25 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) (limited to 'chrome') diff --git a/chrome/browser/autofill/autofill_manager.cc b/chrome/browser/autofill/autofill_manager.cc index d1c2950..127cc56 100644 --- a/chrome/browser/autofill/autofill_manager.cc +++ b/chrome/browser/autofill/autofill_manager.cc @@ -643,10 +643,38 @@ void AutoFillManager::FillFormField(const AutoFillProfile* profile, if (type.subgroup() == AutoFillType::PHONE_NUMBER) { FillPhoneNumberField(profile, field); } else { - field->set_value(profile->GetFieldText(type)); + if (field->form_control_type() == ASCIIToUTF16("select-one")) + FillSelectOneField(profile, type, field); + else + field->set_value(profile->GetFieldText(type)); + } +} + +void AutoFillManager::FillSelectOneField(const AutoFillProfile* profile, + AutoFillType type, + webkit_glue::FormField* field) { + DCHECK(profile); + DCHECK(field); + DCHECK(field->form_control_type() == ASCIIToUTF16("select-one")); + string16 selected_string = profile->GetFieldText(type); + std::string ascii_value = UTF16ToASCII(selected_string); + for (size_t i = 0; i < field->option_strings().size(); ++i) { + if (profile->GetFieldText(type) == field->option_strings()[i]) { + // An exact match - use it. + selected_string = profile->GetFieldText(type); + break; + } + if (!base::strcasecmp(UTF16ToASCII(field->option_strings()[i]).c_str(), + ascii_value.c_str())) { + // A match, but not in the same case - save it for the case we won't + // find an exact match. + selected_string = field->option_strings()[i]; + } } + field->set_value(selected_string); } + void AutoFillManager::FillPhoneNumberField(const AutoFillProfile* profile, webkit_glue::FormField* field) { // If we are filling a phone number, check to see if the size field diff --git a/chrome/browser/autofill/autofill_manager.h b/chrome/browser/autofill/autofill_manager.h index fe4d7bd..f6574d1 100644 --- a/chrome/browser/autofill/autofill_manager.h +++ b/chrome/browser/autofill/autofill_manager.h @@ -141,6 +141,13 @@ class AutoFillManager : public RenderViewHostDelegate::AutoFill, AutoFillType type, webkit_glue::FormField* field); + // Select matching data in the |field|. For now only fixes the cases of the + // wrong case, in the future should do extended matching (for example, + // Hawaii -> HI). + void FillSelectOneField(const AutoFillProfile* profile, + AutoFillType type, + webkit_glue::FormField* field); + // Set |field| argument's value for phone number based on contents of the // |profile|. void FillPhoneNumberField(const AutoFillProfile* profile, diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 4889965..e1ec6b3 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -957,15 +957,18 @@ struct ParamTraits { WriteParam(m, p.value()); WriteParam(m, p.form_control_type()); WriteParam(m, p.size()); + WriteParam(m, p.option_strings()); } static bool Read(const Message* m, void** iter, param_type* p) { string16 label, name, value, form_control_type; int size = 0; + std::vector options; bool result = ReadParam(m, iter, &label); result = result && ReadParam(m, iter, &name); result = result && ReadParam(m, iter, &value); result = result && ReadParam(m, iter, &form_control_type); result = result && ReadParam(m, iter, &size); + result = result && ReadParam(m, iter, &options); if (!result) return false; @@ -974,6 +977,7 @@ struct ParamTraits { p->set_value(value); p->set_form_control_type(form_control_type); p->set_size(size); + p->set_option_strings(options); return true; } static void Log(const param_type& p, std::wstring* l) { diff --git a/chrome/renderer/form_manager.cc b/chrome/renderer/form_manager.cc index 0af0f4a..bfb9fe9 100644 --- a/chrome/renderer/form_manager.cc +++ b/chrome/renderer/form_manager.cc @@ -16,6 +16,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebLabelElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" #include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h" +#include "third_party/WebKit/WebKit/chromium/public/WebOptionElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebSelectElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebString.h" #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" @@ -33,6 +34,7 @@ using WebKit::WebInputElement; using WebKit::WebLabelElement; using WebKit::WebNode; using WebKit::WebNodeList; +using WebKit::WebOptionElement; using WebKit::WebSelectElement; using WebKit::WebString; using WebKit::WebVector; @@ -213,6 +215,24 @@ string16 InferLabelFromDefinitionList( return inferred_label; } +void GetOptionStringsFromElement(WebKit::WebFormControlElement element, + std::vector* option_strings) { + DCHECK(!element.isNull()); + DCHECK(option_strings); + option_strings->clear(); + if (element.formControlType() == ASCIIToUTF16("select-one")) { + WebSelectElement select_element = element.to(); + + // For select-one elements copy option strings. + WebVector list_items = select_element.listItems(); + option_strings->reserve(list_items.size()); + for (size_t i = 0; i < list_items.size(); ++i) { + if (list_items[i].hasTagName("option")) + option_strings->push_back(list_items[i].to().value()); + } + } +} + } // namespace FormManager::FormManager() { @@ -233,6 +253,11 @@ void FormManager::WebFormControlElementToFormField( field->set_name(element.nameForAutofill()); field->set_form_control_type(element.formControlType()); + // Set option strings on the field if available. + std::vector option_strings; + GetOptionStringsFromElement(element, &option_strings); + field->set_option_strings(option_strings); + if (element.formControlType() == WebString::fromUTF8("text")) { const WebInputElement& input_element = element.toConst(); field->set_size(input_element.size()); -- cgit v1.1