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