summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/forms/text_control_inner_elements.cc
blob: c1f64e3421cad39ee7d2cc73be280a5658b976a3 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2010 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 COMPUTER, INC. ``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 COMPUTER, INC. OR
 * 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/text_control_inner_elements.h"

#include "third_party/blink/renderer/core/css/resolver/style_adjuster.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_token_list.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/events/mouse_event.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/core/html/shadow/shadow_element_names.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/layout_text_control_single_line.h"

namespace blink {

EditingViewPortElement::EditingViewPortElement(Document& document)
    : HTMLDivElement(document) {
  SetHasCustomStyleCallbacks();
  setAttribute(html_names::kIdAttr, shadow_element_names::EditingViewPort());
}

scoped_refptr<ComputedStyle>
EditingViewPortElement::CustomStyleForLayoutObject() {
  // FXIME: Move these styles to html.css.

  scoped_refptr<ComputedStyle> style = ComputedStyle::Create();
  style->InheritFrom(OwnerShadowHost()->ComputedStyleRef());

  style->SetFlexGrow(1);
  style->SetMinWidth(Length::Fixed(0));
  style->SetDisplay(EDisplay::kBlock);
  style->SetDirection(TextDirection::kLtr);

  // We don't want the shadow dom to be editable, so we set this block to
  // read-only in case the input itself is editable.
  style->SetUserModify(EUserModify::kReadOnly);

  return style;
}

// ---------------------------

TextControlInnerEditorElement::TextControlInnerEditorElement(Document& document)
    : HTMLDivElement(document) {
  SetHasCustomStyleCallbacks();
}

void TextControlInnerEditorElement::DefaultEventHandler(Event& event) {
  // FIXME: In the future, we should add a way to have default event listeners.
  // Then we would add one to the text field's inner div, and we wouldn't need
  // this subclass.
  // Or possibly we could just use a normal event listener.
  if (event.IsBeforeTextInsertedEvent() ||
      event.type() == event_type_names::kWebkitEditableContentChanged) {
    Element* shadow_ancestor = OwnerShadowHost();
    // A TextControlInnerTextElement can have no host if its been detached,
    // but kept alive by an EditCommand. In this case, an undo/redo can
    // cause events to be sent to the TextControlInnerTextElement. To
    // prevent an infinite loop, we must check for this case before sending
    // the event up the chain.
    if (shadow_ancestor)
      shadow_ancestor->DefaultEventHandler(event);
  }

  if (event.type() == event_type_names::kScroll) {
    // The scroller for a text control is inside of a shadow tree but the
    // scroll event won't bubble past the shadow root and authors cannot add
    // an event listener to it. Fire the scroll event at the shadow host so
    // that the page can hear about the scroll.
    Element* shadow_ancestor = OwnerShadowHost();
    if (shadow_ancestor)
      shadow_ancestor->DispatchEvent(event);
  }

  if (!event.DefaultHandled())
    HTMLDivElement::DefaultEventHandler(event);
}

void TextControlInnerEditorElement::SetVisibility(bool is_visible) {
  if (is_visible_ != is_visible) {
    is_visible_ = is_visible;
    SetNeedsStyleRecalc(kLocalStyleChange,
                        StyleChangeReasonForTracing::Create(
                            style_change_reason::kControlValue));
  }
}

LayoutObject* TextControlInnerEditorElement::CreateLayoutObject(
    const ComputedStyle&,
    LegacyLayout) {
  return new LayoutTextControlInnerEditor(this);
}

scoped_refptr<ComputedStyle>
TextControlInnerEditorElement::CustomStyleForLayoutObject() {
  scoped_refptr<ComputedStyle> inner_editor_style = CreateInnerEditorStyle();
  // Using StyleAdjuster::adjustComputedStyle updates unwanted style. We'd like
  // to apply only editing-related and alignment-related.
  StyleAdjuster::AdjustStyleForEditing(*inner_editor_style);
  if (!is_visible_)
    inner_editor_style->SetOpacity(0);
  return inner_editor_style;
}

scoped_refptr<ComputedStyle>
TextControlInnerEditorElement::CreateInnerEditorStyle() const {
  Element* host = OwnerShadowHost();
  DCHECK(host);
  const ComputedStyle& start_style = host->ComputedStyleRef();
  scoped_refptr<ComputedStyle> text_block_style = ComputedStyle::Create();
  text_block_style->InheritFrom(start_style);
  // The inner block, if present, always has its direction set to LTR,
  // so we need to inherit the direction and unicode-bidi style from the
  // element.
  text_block_style->SetDirection(start_style.Direction());
  text_block_style->SetUnicodeBidi(start_style.GetUnicodeBidi());
  text_block_style->SetUserSelect(EUserSelect::kText);
  text_block_style->SetUserModify(
      To<HTMLFormControlElement>(host)->IsDisabledOrReadOnly()
          ? EUserModify::kReadOnly
          : EUserModify::kReadWritePlaintextOnly);
  text_block_style->SetDisplay(EDisplay::kBlock);
  text_block_style->SetHasLineIfEmpty(true);
  text_block_style->SetShouldIgnoreOverflowPropertyForInlineBlockBaseline();

  if (!IsA<HTMLTextAreaElement>(host)) {
    text_block_style->SetWhiteSpace(EWhiteSpace::kPre);
    text_block_style->SetOverflowWrap(EOverflowWrap::kNormal);
    text_block_style->SetTextOverflow(
        ToTextControl(host)->ValueForTextOverflow());
    int computed_line_height = start_style.ComputedLineHeight();
    // Do not allow line-height to be smaller than our default.
    if (text_block_style->FontSize() >= computed_line_height) {
      text_block_style->SetLineHeight(
          ComputedStyleInitialValues::InitialLineHeight());
    }

    // We'd like to remove line-height if it's unnecessary because
    // overflow:scroll clips editing text by line-height.
    const Length& logical_height = start_style.LogicalHeight();
    // Here, we remove line-height if the INPUT fixed height is taller than the
    // line-height.  It's not the precise condition because logicalHeight
    // includes border and padding if box-sizing:border-box, and there are cases
    // in which we don't want to remove line-height with percent or calculated
    // length.
    // TODO(tkent): This should be done during layout.
    if (logical_height.IsPercentOrCalc() ||
        (logical_height.IsFixed() &&
         logical_height.GetFloatValue() > computed_line_height)) {
      text_block_style->SetLineHeight(
          ComputedStyleInitialValues::InitialLineHeight());
    }

    if (To<HTMLInputElement>(host)->ShouldRevealPassword())
      text_block_style->SetTextSecurity(ETextSecurity::kNone);

    text_block_style->SetOverflowX(EOverflow::kScroll);
    // overflow-y:visible doesn't work because overflow-x:scroll makes a layer.
    text_block_style->SetOverflowY(EOverflow::kScroll);
    scoped_refptr<ComputedStyle> no_scrollbar_style = ComputedStyle::Create();
    no_scrollbar_style->SetStyleType(kPseudoIdScrollbar);
    no_scrollbar_style->SetDisplay(EDisplay::kNone);
    text_block_style->AddCachedPseudoElementStyle(no_scrollbar_style);
    text_block_style->SetHasPseudoElementStyle(kPseudoIdScrollbar);
  }

  return text_block_style;
}

// ----------------------------

SearchFieldCancelButtonElement::SearchFieldCancelButtonElement(
    Document& document)
    : HTMLDivElement(document) {
  SetShadowPseudoId(AtomicString("-webkit-search-cancel-button"));
  setAttribute(html_names::kIdAttr, shadow_element_names::SearchClearButton());
}

void SearchFieldCancelButtonElement::DefaultEventHandler(Event& event) {
  // If the element is visible, on mouseup, clear the value, and set selection
  auto* mouse_event = DynamicTo<MouseEvent>(event);
  auto* input = To<HTMLInputElement>(OwnerShadowHost());
  if (!input || input->IsDisabledOrReadOnly()) {
    if (!event.DefaultHandled())
      HTMLDivElement::DefaultEventHandler(event);
    return;
  }

  if (event.type() == event_type_names::kClick && mouse_event &&
      mouse_event->button() ==
          static_cast<int16_t>(WebPointerProperties::Button::kLeft)) {
    input->SetValueForUser("");
    input->SetAutofillState(WebAutofillState::kNotFilled);
    input->OnSearch();
    event.SetDefaultHandled();
  }

  if (!event.DefaultHandled())
    HTMLDivElement::DefaultEventHandler(event);
}

bool SearchFieldCancelButtonElement::WillRespondToMouseClickEvents() {
  auto* input = To<HTMLInputElement>(OwnerShadowHost());
  if (input && !input->IsDisabledOrReadOnly())
    return true;

  return HTMLDivElement::WillRespondToMouseClickEvents();
}

// ----------------------------

PasswordRevealButtonElement::PasswordRevealButtonElement(Document& document)
    : HTMLDivElement(document) {
  SetShadowPseudoId(AtomicString("-internal-reveal"));
  setAttribute(html_names::kIdAttr,
               shadow_element_names::PasswordRevealButton());
}

void PasswordRevealButtonElement::DefaultEventHandler(Event& event) {
  auto* input = To<HTMLInputElement>(OwnerShadowHost());
  if (!input || input->IsDisabledOrReadOnly()) {
    if (!event.DefaultHandled())
      HTMLDivElement::DefaultEventHandler(event);
    return;
  }

  // Toggle the should-reveal-password state when clicked.
  if (event.type() == event_type_names::kClick && IsA<MouseEvent>(event)) {
    bool shouldRevealPassword = !input->ShouldRevealPassword();

    input->SetShouldRevealPassword(shouldRevealPassword);
    input->UpdateView();

    event.SetDefaultHandled();
  }

  if (!event.DefaultHandled())
    HTMLDivElement::DefaultEventHandler(event);
}

bool PasswordRevealButtonElement::WillRespondToMouseClickEvents() {
  auto* input = To<HTMLInputElement>(OwnerShadowHost());
  if (input && !input->IsDisabledOrReadOnly())
    return true;

  return HTMLDivElement::WillRespondToMouseClickEvents();
}
}  // namespace blink