summaryrefslogtreecommitdiffstats
path: root/webkit/glue/form_field.cc
blob: 30d22ad954b4dc536d64d4831b70105be3b8c500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2010 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 "webkit/glue/form_field.h"

#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebOptionElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebSelectElement.h"

using WebKit::WebFormControlElement;
using WebKit::WebElement;
using WebKit::WebInputElement;
using WebKit::WebOptionElement;
using WebKit::WebSelectElement;
using WebKit::WebVector;

namespace webkit_glue {

FormField::FormField()
    : size_(0) {
}

// TODO(jhawkins): This constructor should probably be deprecated and the
// functionality moved to FormManager.
FormField::FormField(WebFormControlElement element)
    : size_(0) {
  name_ = element.nameForAutofill();

  // TODO(jhawkins): Extract the field label.  For now we just use the field
  // name.
  label_ = name_;

  form_control_type_ = element.formControlType();
  if (form_control_type_ == ASCIIToUTF16("text")) {
    const WebInputElement& input_element = element.toConst<WebInputElement>();
    value_ = input_element.value();
    size_ = input_element.size();
  } else if (form_control_type_ == ASCIIToUTF16("select-one")) {
    WebSelectElement select_element = element.to<WebSelectElement>();
    value_ = select_element.value();

    // For select-one elements copy option strings.
    WebVector<WebElement> 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<WebOptionElement>().value());
    }
  }

  TrimWhitespace(value_, TRIM_LEADING, &value_);
}

FormField::FormField(const string16& label,
                     const string16& name,
                     const string16& value,
                     const string16& form_control_type,
                     int size)
  : label_(label),
    name_(name),
    value_(value),
    form_control_type_(form_control_type),
    size_(size) {
}

FormField::~FormField() {
}

bool FormField::operator==(const FormField& field) const {
  // A FormField stores a value, but the value is not part of the identity of
  // the field, so we don't want to compare the values.
  return (label_ == field.label_ &&
          name_ == field.name_ &&
          form_control_type_ == field.form_control_type_ &&
          size_ == field.size_);
}

bool FormField::operator!=(const FormField& field) const {
  return !operator==(field);
}

bool FormField::StrictlyEqualsHack(const FormField& field) const {
  return (label_ == field.label_ &&
          name_ == field.name_ &&
          value_ == field.value_ &&
          form_control_type_ == field.form_control_type_ &&
          size_ == field.size_);
}

std::ostream& operator<<(std::ostream& os, const FormField& field) {
  return os
      << UTF16ToUTF8(field.label())
      << " "
      << UTF16ToUTF8(field.name())
      << " "
      << UTF16ToUTF8(field.value())
      << " "
      << UTF16ToUTF8(field.form_control_type())
      << " "
      << field.size();
}

}  // namespace webkit_glue