1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "webkit/glue/form_field.h"
6
7 #include "base/string_util.h"
8 #include "base/utf_string_conversions.h"
9 #ifndef ANDROID
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h"
13 #endif
14
15 #ifndef ANDROID
16 using WebKit::WebFormControlElement;
17 using WebKit::WebElement;
18 using WebKit::WebInputElement;
19 using WebKit::WebOptionElement;
20 using WebKit::WebSelectElement;
21 using WebKit::WebVector;
22 #endif
23
24 namespace webkit_glue {
25
FormField()26 FormField::FormField()
27 : max_length(0),
28 is_autofilled(false) {
29 }
30
31 #ifndef ANDROID
32 // TODO(jhawkins): This constructor should probably be deprecated and the
33 // functionality moved to FormManager.
FormField(WebFormControlElement element)34 FormField::FormField(WebFormControlElement element)
35 : max_length(0),
36 is_autofilled(false) {
37 name = element.nameForAutofill();
38
39 // TODO(jhawkins): Extract the field label. For now we just use the field
40 // name.
41 label = name;
42
43 form_control_type = element.formControlType();
44 if (form_control_type == ASCIIToUTF16("text")) {
45 const WebInputElement& input_element = element.toConst<WebInputElement>();
46 value = input_element.value();
47 max_length = input_element.size();
48 is_autofilled = input_element.isAutofilled();
49 } else if (form_control_type == ASCIIToUTF16("select-one")) {
50 WebSelectElement select_element = element.to<WebSelectElement>();
51 value = select_element.value();
52
53 // For select-one elements copy option strings.
54 WebVector<WebElement> list_items = select_element.listItems();
55 option_strings.reserve(list_items.size());
56 for (size_t i = 0; i < list_items.size(); ++i) {
57 if (list_items[i].hasTagName("option"))
58 option_strings.push_back(list_items[i].to<WebOptionElement>().value());
59 }
60 }
61
62 TrimWhitespace(value, TRIM_LEADING, &value);
63 }
64 #endif
65
FormField(const string16 & label,const string16 & name,const string16 & value,const string16 & form_control_type,int max_length,bool is_autofilled)66 FormField::FormField(const string16& label,
67 const string16& name,
68 const string16& value,
69 const string16& form_control_type,
70 int max_length,
71 bool is_autofilled)
72 : label(label),
73 name(name),
74 value(value),
75 form_control_type(form_control_type),
76 max_length(max_length),
77 is_autofilled(is_autofilled) {
78 }
79
~FormField()80 FormField::~FormField() {
81 }
82
operator ==(const FormField & field) const83 bool FormField::operator==(const FormField& field) const {
84 // A FormField stores a value, but the value is not part of the identity of
85 // the field, so we don't want to compare the values.
86 return (label == field.label &&
87 name == field.name &&
88 form_control_type == field.form_control_type &&
89 max_length == field.max_length);
90 }
91
operator !=(const FormField & field) const92 bool FormField::operator!=(const FormField& field) const {
93 return !operator==(field);
94 }
95
StrictlyEqualsHack(const FormField & field) const96 bool FormField::StrictlyEqualsHack(const FormField& field) const {
97 return (label == field.label &&
98 name == field.name &&
99 value == field.value &&
100 form_control_type == field.form_control_type &&
101 max_length == field.max_length);
102 }
103
operator <<(std::ostream & os,const FormField & field)104 std::ostream& operator<<(std::ostream& os, const FormField& field) {
105 return os
106 << UTF16ToUTF8(field.label)
107 << " "
108 << UTF16ToUTF8(field.name)
109 << " "
110 << UTF16ToUTF8(field.value)
111 << " "
112 << UTF16ToUTF8(field.form_control_type)
113 << " "
114 << field.max_length;
115 }
116
117 } // namespace webkit_glue
118