summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/layout_menu_list.cc
blob: b94bd3119a9ca01788a2fcc14b28838cebf11b82 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * This file is part of the select element layoutObject in WebCore.
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc.
 *               All rights reserved.
 *           (C) 2009 Torch Mobile Inc. All rights reserved.
 *               (http://www.torchmobile.com/)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "third_party/blink/renderer/core/layout/layout_menu_list.h"

#include <math.h>
#include "third_party/blink/renderer/core/accessibility/ax_object_cache.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/html/forms/html_option_element.h"
#include "third_party/blink/renderer/core/html/forms/html_select_element.h"
#include "third_party/blink/renderer/core/layout/layout_block_flow.h"
#include "third_party/blink/renderer/core/layout/layout_text.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/platform/text/platform_locale.h"

namespace blink {

LayoutMenuList::LayoutMenuList(Element* element)
    : LayoutFlexibleBox(element),
      button_text_(nullptr),
      inner_block_(nullptr),
      is_empty_(false),
      has_updated_active_option_(false),
      inner_block_height_(LayoutUnit()),
      options_width_(0),
      last_active_index_(-1) {
  DCHECK(IsHTMLSelectElement(element));
}

LayoutMenuList::~LayoutMenuList() = default;

// FIXME: Instead of this hack we should add a ShadowRoot to <select> with no
// insertion point to prevent children from rendering.
bool LayoutMenuList::IsChildAllowed(LayoutObject* object,
                                    const ComputedStyle&) const {
  return object->IsAnonymous();
}

scoped_refptr<ComputedStyle> LayoutMenuList::CreateInnerStyle() {
  scoped_refptr<ComputedStyle> inner_style =
      ComputedStyle::CreateAnonymousStyleWithDisplay(StyleRef(),
                                                     EDisplay::kBlock);

  AdjustInnerStyle(*inner_style);
  return inner_style;
}

void LayoutMenuList::UpdateInnerStyle() {
  DCHECK(inner_block_);
  scoped_refptr<ComputedStyle> inner_style =
      ComputedStyle::Clone(inner_block_->StyleRef());
  AdjustInnerStyle(*inner_style);
  inner_block_->SetStyleInternal(std::move(inner_style));
  // LayoutMenuList::ControlClipRect() depends on inner_block_->ContentsSize().
  SetNeedsPaintPropertyUpdate();
  if (Layer())
    Layer()->SetNeedsCompositingInputsUpdate();
}

void LayoutMenuList::CreateInnerBlock() {
  if (inner_block_) {
    DCHECK_EQ(FirstChild(), inner_block_);
    DCHECK(!inner_block_->NextSibling());
    return;
  }

  // Create an anonymous block.
  DCHECK(!FirstChild());
  inner_block_ =
      LayoutBlockFlow::CreateAnonymous(&GetDocument(), CreateInnerStyle());

  button_text_ =
      LayoutText::CreateEmptyAnonymous(GetDocument(), MutableStyle());
  // We need to set the text explicitly though it was specified in the
  // constructor because LayoutText doesn't refer to the text
  // specified in the constructor in a case of re-transforming.
  inner_block_->AddChild(button_text_);
  LayoutFlexibleBox::AddChild(inner_block_);

  // LayoutMenuList::ControlClipRect() depends on inner_block_->ContentsSize().
  SetNeedsPaintPropertyUpdate();
  if (Layer())
    Layer()->SetNeedsCompositingInputsUpdate();
}

bool LayoutMenuList::HasOptionStyleChanged(
    const ComputedStyle& inner_style) const {
  return option_style_ &&
         ((option_style_->Direction() != inner_style.Direction() ||
           option_style_->GetUnicodeBidi() != inner_style.GetUnicodeBidi()));
}

void LayoutMenuList::AdjustInnerStyle(ComputedStyle& inner_style) const {
  inner_style.SetFlexGrow(1);
  inner_style.SetFlexShrink(1);
  // min-width: 0; is needed for correct shrinking.
  inner_style.SetMinWidth(Length(0, kFixed));

  // Use margin:auto instead of align-items:center to get safe centering, i.e.
  // when the content overflows, treat it the same as align-items: flex-start.
  // But we only do that for the cases where html.css would otherwise use
  // center.
  if (StyleRef().AlignItemsPosition() == ItemPosition::kCenter) {
    inner_style.SetMarginTop(Length());
    inner_style.SetMarginBottom(Length());
    inner_style.SetAlignSelfPosition(ItemPosition::kFlexStart);
  }

  Length padding_start = Length(
      LayoutTheme::GetTheme().PopupInternalPaddingStart(StyleRef()), kFixed);
  Length padding_end =
      Length(LayoutTheme::GetTheme().PopupInternalPaddingEnd(
                 GetFrameView()->GetChromeClient(), StyleRef()),
             kFixed);
  inner_style.SetPaddingLeft(StyleRef().Direction() == TextDirection::kLtr
                                 ? padding_start
                                 : padding_end);
  inner_style.SetPaddingRight(StyleRef().Direction() == TextDirection::kLtr
                                  ? padding_end
                                  : padding_start);
  inner_style.SetPaddingTop(Length(
      LayoutTheme::GetTheme().PopupInternalPaddingTop(StyleRef()), kFixed));
  inner_style.SetPaddingBottom(Length(
      LayoutTheme::GetTheme().PopupInternalPaddingBottom(StyleRef()), kFixed));
  inner_style.SetTextAlign(StyleRef().IsLeftToRightDirection()
                               ? ETextAlign::kLeft
                               : ETextAlign::kRight);

  if (HasOptionStyleChanged(inner_style)) {
    inner_block_->SetNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation(
        LayoutInvalidationReason::kStyleChange);
    inner_style.SetDirection(option_style_->Direction());
    inner_style.SetUnicodeBidi(option_style_->GetUnicodeBidi());
  }
}

HTMLSelectElement* LayoutMenuList::SelectElement() const {
  return ToHTMLSelectElement(GetNode());
}

void LayoutMenuList::AddChild(LayoutObject* new_child,
                              LayoutObject* before_child) {
  inner_block_->AddChild(new_child, before_child);
  DCHECK_EQ(inner_block_, FirstChild());

  if (AXObjectCache* cache = GetDocument().ExistingAXObjectCache())
    cache->ChildrenChanged(this);

  // LayoutMenuList::ControlClipRect() depends on inner_block_->ContentsSize().
  SetNeedsPaintPropertyUpdate();
  if (Layer())
    Layer()->SetNeedsCompositingInputsUpdate();
}

void LayoutMenuList::RemoveChild(LayoutObject* old_child) {
  if (old_child == inner_block_ || !inner_block_) {
    LayoutFlexibleBox::RemoveChild(old_child);
    inner_block_ = nullptr;
  } else {
    inner_block_->RemoveChild(old_child);
  }
}

void LayoutMenuList::StyleDidChange(StyleDifference diff,
                                    const ComputedStyle* old_style) {
  LayoutBlock::StyleDidChange(diff, old_style);

  if (!inner_block_)
    CreateInnerBlock();

  button_text_->SetStyle(MutableStyle());
  UpdateInnerStyle();
  UpdateInnerBlockHeight();
}

void LayoutMenuList::UpdateInnerBlockHeight() {
  const SimpleFontData* font_data = StyleRef().GetFont().PrimaryFont();
  DCHECK(font_data);
  inner_block_height_ = (font_data ? font_data->GetFontMetrics().Height() : 0) +
                        inner_block_->BorderAndPaddingHeight();
}

void LayoutMenuList::UpdateOptionsWidth() const {
  float max_option_width = 0;

  for (auto* const option : SelectElement()->GetOptionList()) {
    String text = option->TextIndentedToRespectGroupLabel();
    const ComputedStyle* item_style =
        option->GetComputedStyle() ? option->GetComputedStyle() : Style();
    item_style->ApplyTextTransform(&text);
    // We apply SELECT's style, not OPTION's style because m_optionsWidth is
    // used to determine intrinsic width of the menulist box.
    TextRun text_run = ConstructTextRun(StyleRef().GetFont(), text, *Style());
    max_option_width =
        std::max(max_option_width, StyleRef().GetFont().Width(text_run));
  }
  options_width_ = static_cast<int>(ceilf(max_option_width));
}

void LayoutMenuList::UpdateFromElement() {
  HTMLSelectElement* select = SelectElement();
  HTMLOptionElement* option = select->OptionToBeShown();
  String text = g_empty_string;
  option_style_ = nullptr;

  if (select->IsMultiple()) {
    unsigned selected_count = 0;
    HTMLOptionElement* selected_option_element = nullptr;
    for (auto* const option : select->GetOptionList()) {
      if (option->Selected()) {
        if (++selected_count == 1)
          selected_option_element = option;
      }
    }

    if (selected_count == 1) {
      text = selected_option_element->TextIndentedToRespectGroupLabel();
      option_style_ = selected_option_element->GetComputedStyle();
    } else {
      Locale& locale = select->GetLocale();
      String localized_number_string =
          locale.ConvertToLocalizedNumber(String::Number(selected_count));
      text = locale.QueryString(WebLocalizedString::kSelectMenuListText,
                                localized_number_string);
      DCHECK(!option_style_);
    }
  } else {
    if (option) {
      text = option->TextIndentedToRespectGroupLabel();
      option_style_ = option->GetComputedStyle();
    }
  }

  SetText(text.StripWhiteSpace());

  DidUpdateActiveOption(option);

  DCHECK(inner_block_);
  if (HasOptionStyleChanged(inner_block_->StyleRef()))
    UpdateInnerStyle();
}

void LayoutMenuList::SetText(const String& s) {
  if (s.IsEmpty()) {
    // FIXME: This is a hack. We need the select to have the same baseline
    // positioning as any surrounding text. Wihtout any content, we align the
    // bottom of the select to the bottom of the text. With content (In this
    // case the faked " ") we correctly align the middle of the select to the
    // middle of the text. It should be possible to remove this, just set
    // s.impl() into the text and have things align correctly...
    // crbug.com/485982
    is_empty_ = true;
    button_text_->SetText(StringImpl::Create(" ", 1), true);
  } else {
    is_empty_ = false;
    button_text_->SetText(s.Impl(), true);
  }
  // LayoutMenuList::ControlClipRect() depends on inner_block_->ContentsSize().
  SetNeedsPaintPropertyUpdate();
  if (Layer())
    Layer()->SetNeedsCompositingInputsUpdate();
}

String LayoutMenuList::GetText() const {
  return button_text_ && !is_empty_ ? button_text_->GetText() : String();
}

LayoutRect LayoutMenuList::ControlClipRect(
    const LayoutPoint& additional_offset) const {
  // Clip to the intersection of the content box and the content box for the
  // inner box. This will leave room for the arrows which sit in the inner box
  // padding, and if the inner box ever spills out of the outer box, that will
  // get clipped too.
  LayoutRect outer_box = PhysicalContentBoxRect();
  outer_box.MoveBy(additional_offset);

  LayoutRect inner_box(
      additional_offset + inner_block_->Location() +
          LayoutSize(inner_block_->PaddingLeft(), inner_block_->PaddingTop()),
      inner_block_->ContentSize());

  return Intersection(outer_box, inner_box);
}

void LayoutMenuList::ComputeIntrinsicLogicalWidths(
    LayoutUnit& min_logical_width,
    LayoutUnit& max_logical_width) const {
  UpdateOptionsWidth();

  max_logical_width =
      std::max(options_width_,
               LayoutTheme::GetTheme().MinimumMenuListSize(StyleRef())) +
      inner_block_->PaddingLeft() + inner_block_->PaddingRight();
  if (!StyleRef().Width().IsPercentOrCalc())
    min_logical_width = max_logical_width;
  else
    min_logical_width = LayoutUnit();
}

void LayoutMenuList::ComputeLogicalHeight(
    LayoutUnit logical_height,
    LayoutUnit logical_top,
    LogicalExtentComputedValues& computed_values) const {
  if (StyleRef().HasAppearance())
    logical_height = inner_block_height_ + BorderAndPaddingHeight();
  LayoutBox::ComputeLogicalHeight(logical_height, logical_top, computed_values);
}

void LayoutMenuList::DidSelectOption(HTMLOptionElement* option) {
  DidUpdateActiveOption(option);
}

void LayoutMenuList::DidUpdateActiveOption(HTMLOptionElement* option) {
  if (!GetDocument().ExistingAXObjectCache())
    return;

  int option_index = option ? option->index() : -1;
  if (last_active_index_ == option_index)
    return;
  last_active_index_ = option_index;

  // We skip sending accessiblity notifications for the very first option,
  // otherwise we get extra focus and select events that are undesired.
  if (!has_updated_active_option_) {
    has_updated_active_option_ = true;
    return;
  }

  GetDocument().ExistingAXObjectCache()->HandleUpdateActiveMenuOption(
      this, option_index);
}

LayoutUnit LayoutMenuList::ClientPaddingLeft() const {
  return PaddingLeft() + inner_block_->PaddingLeft();
}

LayoutUnit LayoutMenuList::ClientPaddingRight() const {
  return PaddingRight() + inner_block_->PaddingRight();
}

}  // namespace blink