summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-11 00:18:25 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-11 00:18:25 +0000
commitcfb7731f32d6e69bbaaf78a19731ac4dfc8463ce (patch)
tree7a9552ae9ac9c4f1ebdc3bac7724980908e55efc /chrome
parent58b8903b7e8382824cfdb986a4ce5aad6f40c221 (diff)
downloadchromium_src-cfb7731f32d6e69bbaaf78a19731ac4dfc8463ce.zip
chromium_src-cfb7731f32d6e69bbaaf78a19731ac4dfc8463ce.tar.gz
chromium_src-cfb7731f32d6e69bbaaf78a19731ac4dfc8463ce.tar.bz2
Implement PhoneField, a FormField that matches a phone number field in a form.
BUG=none TEST=none Review URL: http://codereview.chromium.org/488010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34311 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/autofill/form_field.cc4
-rw-r--r--chrome/browser/autofill/phone_field.cc114
-rw-r--r--chrome/browser/autofill/phone_field.h42
-rwxr-xr-xchrome/chrome_browser.gypi2
4 files changed, 162 insertions, 0 deletions
diff --git a/chrome/browser/autofill/form_field.cc b/chrome/browser/autofill/form_field.cc
index dca943f..da74448 100644
--- a/chrome/browser/autofill/form_field.cc
+++ b/chrome/browser/autofill/form_field.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/autofill/form_field.h"
#include "chrome/browser/autofill/autofill_field.h"
+#include "chrome/browser/autofill/phone_field.h"
#include "third_party/WebKit/WebKit/chromium/public/WebRegularExpression.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
@@ -64,6 +65,9 @@ FormField* FormField::ParseFormField(
field = EmailField::Parse(iter, is_ecml);
if (field != NULL)
return field;
+ field = PhoneField::Parse(iter, is_ecml);
+ if (field != NULL)
+ return field;
// TODO(jhawkins):
// - AddressField
diff --git a/chrome/browser/autofill/phone_field.cc b/chrome/browser/autofill/phone_field.cc
new file mode 100644
index 0000000..d0963408
--- /dev/null
+++ b/chrome/browser/autofill/phone_field.cc
@@ -0,0 +1,114 @@
+// 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.
+
+#include "chrome/browser/autofill/phone_field.h"
+
+#include "base/logging.h"
+#include "base/string16.h"
+#include "base/string_util.h"
+#include "chrome/browser/autofill/autofill_field.h"
+
+// static
+PhoneField* PhoneField::Parse(std::vector<AutoFillField*>::const_iterator* iter,
+ bool is_ecml) {
+ if (is_ecml)
+ return ParseECML(iter);
+
+ std::vector<AutoFillField*>::const_iterator q = *iter;
+ AutoFillField* phone = NULL;
+ AutoFillField* phone2 = NULL;
+ AutoFillField* phone3 = NULL;
+ bool area_code; // true if we've parsed an area code field.
+
+ // Some pages, such as BloomingdalesShipping.html, have a field labeled
+ // "Area Code and Phone"; we want to parse this as a phone number field so
+ // we look for "phone" before we look for "area code".
+ if (ParseText(&q, ASCIIToUTF16("phone"), &phone)) {
+ area_code = false;
+ } else {
+ if (!ParseText(&q, ASCIIToUTF16("area code"), &phone))
+ return NULL;
+ area_code = true;
+ ParseText(&q, ASCIIToUTF16("phone"), &phone2);
+ }
+
+ // Sometimes phone number fields are separated by "-" (e.g. test page
+ // Crate and Barrel Check Out.html). Also, area codes are sometimes
+ // surrounded by parentheses, so a ")" may appear after the area code field.
+ //
+ // We used to match "tel" here, which we've seen in field names (e.g. on
+ // Newegg2.html), but that's too general: some pages (e.g.
+ // uk/Furniture123-1.html) have several phone numbers in succession and we
+ // don't want those to be parsed as components of a single phone number.
+ if (phone2 == NULL)
+ ParseText(&q, ASCIIToUTF16("^-|)|"), &phone2);
+
+ // Look for a third text box.
+ if (phone2)
+ ParseText(&q, ASCIIToUTF16("^-|"), &phone3);
+
+ // Now we have one, two, or three phone number text fields. Package them
+ // up into a PhoneField object.
+
+ PhoneField phone_field;
+ if (phone2 == NULL) { // only one field
+ if (area_code) // it's an area code
+ return NULL; // doesn't make sense
+ phone_field.phone_ = phone;
+ } else {
+ phone_field.area_code_ = phone;
+ if (phone3 == NULL) { // two fields
+ phone_field.phone_ = phone2;
+ } else { // three boxes: area code, prefix and suffix
+ phone_field.prefix_ = phone2;
+ phone_field.phone_ = phone3;
+ }
+ }
+
+ // Now look for an extension.
+ ParseText(&q, ASCIIToUTF16("ext"), &phone_field.extension_);
+
+ *iter = q;
+ return new PhoneField(phone_field);
+}
+
+// static
+PhoneField* PhoneField::ParseECML(
+ std::vector<AutoFillField*>::const_iterator* iter) {
+ string16 pattern(GetEcmlPattern(kEcmlShipToPhone, kEcmlBillToPhone, '|'));
+
+ AutoFillField* field;
+ if (ParseText(iter, pattern, &field)) {
+ PhoneField* phone_field = new PhoneField();
+ phone_field->phone_ = field;
+ return phone_field;
+ }
+
+ return NULL;
+}
+
+bool PhoneField::GetFieldInfo(FieldTypeMap* field_type_map) const {
+ bool ok;
+
+ if (area_code_ != NULL) {
+ ok = Add(field_type_map, area_code_, AutoFillType(PHONE_HOME_CITY_CODE));
+ DCHECK(ok);
+
+ // NOTE: we ignore the prefix/suffix thing here.
+ ok = ok && Add(field_type_map, phone_, AutoFillType(PHONE_HOME_NUMBER));
+ DCHECK(ok);
+ } else {
+ ok = Add(field_type_map, phone_, AutoFillType(PHONE_HOME_WHOLE_NUMBER));
+ DCHECK(ok);
+ }
+
+ return ok;
+}
+
+PhoneField::PhoneField()
+ : phone_(NULL),
+ area_code_(NULL),
+ prefix_(NULL),
+ extension_(NULL) {
+}
diff --git a/chrome/browser/autofill/phone_field.h b/chrome/browser/autofill/phone_field.h
new file mode 100644
index 0000000..18dabe3
--- /dev/null
+++ b/chrome/browser/autofill/phone_field.h
@@ -0,0 +1,42 @@
+// 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.
+
+#ifndef CHROME_BROWSER_AUTOFILL_PHONE_FIELD_H_
+#define CHROME_BROWSER_AUTOFILL_PHONE_FIELD_H_
+
+#include <vector>
+
+#include "chrome/browser/autofill/autofill_type.h"
+#include "chrome/browser/autofill/form_field.h"
+
+class AutoFillField;
+
+// A phone number in one of the following formats:
+// - area code, prefix, suffix
+// - area code, number
+// - number
+class PhoneField : public FormField {
+ public:
+ static PhoneField* Parse(std::vector<AutoFillField*>::const_iterator* iter,
+ bool is_ecml);
+ static PhoneField* ParseECML(
+ std::vector<AutoFillField*>::const_iterator* iter);
+
+ virtual bool GetFieldInfo(FieldTypeMap* field_type_map) const;
+
+ virtual int priority() const { return 2; }
+
+ protected:
+ PhoneField();
+
+ private:
+ // Always present; holds suffix if prefix is present.
+ AutoFillField* phone_;
+
+ AutoFillField* area_code_; // optional
+ AutoFillField* prefix_; // optional
+ AutoFillField* extension_; // optional
+};
+
+#endif // CHROME_BROWSER_AUTOFILL_PHONE_FIELD_H_
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index bd8bc81..d54b044 100755
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -93,6 +93,8 @@
'browser/autofill/form_structure.h',
'browser/autofill/personal_data_manager.cc',
'browser/autofill/personal_data_manager.h',
+ 'browser/autofill/phone_field.cc',
+ 'browser/autofill/phone_field.h',
'browser/automation/automation_autocomplete_edit_tracker.h',
'browser/automation/automation_browser_tracker.h',
'browser/automation/extension_automation_constants.h',