summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/forms/date_time_symbolic_field_element.cc
blob: 5039df9f369ba2f5070ea29b1a705e30d448a8d2 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/html/forms/date_time_symbolic_field_element.h"

#include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/text/text_break_iterator.h"
#include "third_party/blink/renderer/platform/text/text_run.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/unicode.h"

namespace blink {

static AtomicString MakeVisibleEmptyValue(const Vector<String>& symbols) {
  unsigned maximum_length = 0;
  for (unsigned index = 0; index < symbols.size(); ++index)
    maximum_length =
        std::max(maximum_length, NumGraphemeClusters(symbols[index]));
  StringBuilder builder;
  builder.ReserveCapacity(maximum_length);
  for (unsigned length = 0; length < maximum_length; ++length)
    builder.Append('-');
  return builder.ToAtomicString();
}

DateTimeSymbolicFieldElement::DateTimeSymbolicFieldElement(
    Document& document,
    FieldOwner& field_owner,
    const Vector<String>& symbols,
    int minimum,
    int maximum)
    : DateTimeFieldElement(document, field_owner),
      symbols_(symbols),
      visible_empty_value_(MakeVisibleEmptyValue(symbols)),
      selected_index_(-1),
      type_ahead_(this),
      minimum_index_(minimum),
      maximum_index_(maximum) {
  DCHECK(!symbols.IsEmpty());
  DCHECK_GE(minimum_index_, 0);
  SECURITY_DCHECK(maximum_index_ < static_cast<int>(symbols_.size()));
  DCHECK_LE(minimum_index_, maximum_index_);
}

float DateTimeSymbolicFieldElement::MaximumWidth(const ComputedStyle& style) {
  float maximum_width = ComputeTextWidth(style, VisibleEmptyValue());
  for (unsigned index = 0; index < symbols_.size(); ++index)
    maximum_width =
        std::max(maximum_width, ComputeTextWidth(style, symbols_[index]));
  return maximum_width + DateTimeFieldElement::MaximumWidth(style);
}

void DateTimeSymbolicFieldElement::HandleKeyboardEvent(
    KeyboardEvent* keyboard_event) {
  if (keyboard_event->type() != EventTypeNames::keypress)
    return;

  const UChar char_code = WTF::Unicode::ToLower(keyboard_event->charCode());
  if (char_code < ' ')
    return;

  keyboard_event->SetDefaultHandled();

  int index = type_ahead_.HandleEvent(
      keyboard_event, TypeAhead::kMatchPrefix | TypeAhead::kCycleFirstChar |
                          TypeAhead::kMatchIndex);
  if (index < 0)
    return;
  SetValueAsInteger(index, kDispatchEvent);
}

bool DateTimeSymbolicFieldElement::HasValue() const {
  return selected_index_ >= 0;
}

void DateTimeSymbolicFieldElement::Initialize(const AtomicString& pseudo,
                                              const String& ax_help_text) {
  // The minimum and maximum below are exposed to users, and 1-based numbers
  // are natural for symbolic fields. For example, the minimum value of a
  // month field should be 1, not 0.
  DateTimeFieldElement::Initialize(pseudo, ax_help_text, minimum_index_ + 1,
                                   maximum_index_ + 1);
}

void DateTimeSymbolicFieldElement::SetEmptyValue(EventBehavior event_behavior) {
  if (IsDisabled())
    return;
  selected_index_ = kInvalidIndex;
  UpdateVisibleValue(event_behavior);
}

void DateTimeSymbolicFieldElement::SetValueAsInteger(
    int new_selected_index,
    EventBehavior event_behavior) {
  selected_index_ = std::max(
      0, std::min(new_selected_index, static_cast<int>(symbols_.size() - 1)));
  UpdateVisibleValue(event_behavior);
}

void DateTimeSymbolicFieldElement::StepDown() {
  if (HasValue()) {
    if (!IndexIsInRange(--selected_index_))
      selected_index_ = maximum_index_;
  } else {
    selected_index_ = maximum_index_;
  }
  UpdateVisibleValue(kDispatchEvent);
}

void DateTimeSymbolicFieldElement::StepUp() {
  if (HasValue()) {
    if (!IndexIsInRange(++selected_index_))
      selected_index_ = minimum_index_;
  } else {
    selected_index_ = minimum_index_;
  }
  UpdateVisibleValue(kDispatchEvent);
}

String DateTimeSymbolicFieldElement::Value() const {
  return HasValue() ? symbols_[selected_index_] : g_empty_string;
}

int DateTimeSymbolicFieldElement::ValueAsInteger() const {
  return selected_index_;
}

int DateTimeSymbolicFieldElement::ValueForARIAValueNow() const {
  // Synchronize with minimum/maximum adjustment in initialize().
  return selected_index_ + 1;
}

String DateTimeSymbolicFieldElement::VisibleEmptyValue() const {
  return visible_empty_value_;
}

String DateTimeSymbolicFieldElement::VisibleValue() const {
  return HasValue() ? symbols_[selected_index_] : VisibleEmptyValue();
}

int DateTimeSymbolicFieldElement::IndexOfSelectedOption() const {
  return selected_index_;
}

int DateTimeSymbolicFieldElement::OptionCount() const {
  return symbols_.size();
}

String DateTimeSymbolicFieldElement::OptionAtIndex(int index) const {
  return symbols_[index];
}

}  // namespace blink