summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/highlight_painting_utils.cc
blob: 74b415469213b0b6b4fc6cf79d11287086505445 (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
// Copyright 2017 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 "third_party/blink/renderer/core/paint/highlight_painting_utils.h"

#include "third_party/blink/renderer/core/css/pseudo_style_request.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/v0_insertion_point.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/paint/text_paint_style.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/graphics/color.h"

namespace blink {

namespace {

bool NodeIsSelectable(const ComputedStyle& style, Node* node) {
  return !node->IsInert() && !(style.UserSelect() == EUserSelect::kNone &&
                               style.UserModify() == EUserModify::kReadOnly);
}

bool NodeIsReplaced(Node* node) {
  return node && node->GetLayoutObject() &&
         node->GetLayoutObject()->IsLayoutReplaced();
}

Color ForcedSystemForegroundColor(PseudoId pseudo_id,
                                  mojom::blink::ColorScheme color_scheme) {
  CSSValueID keyword = CSSValueID::kHighlighttext;
  switch (pseudo_id) {
    case kPseudoIdTargetText:
      // TODO(futhark): According to the spec, the UA style should use Marktext.
      keyword = CSSValueID::kHighlighttext;
      break;
    case kPseudoIdSelection:
      keyword = CSSValueID::kHighlighttext;
      break;
    default:
      NOTREACHED();
      break;
  }
  return LayoutTheme::GetTheme().SystemColor(keyword, color_scheme);
}

Color ForcedSystemBackgroundColor(PseudoId pseudo_id,
                                  mojom::blink::ColorScheme color_scheme) {
  CSSValueID keyword = CSSValueID::kHighlight;
  switch (pseudo_id) {
    case kPseudoIdTargetText:
      // TODO(futhark): According to the spec, the UA style should use Mark.
      keyword = CSSValueID::kHighlight;
      break;
    case kPseudoIdSelection:
      keyword = CSSValueID::kHighlight;
      break;
    default:
      NOTREACHED();
      break;
  }
  return LayoutTheme::GetTheme().SystemColor(keyword, color_scheme);
}

Color HighlightThemeForegroundColor(const Document& document,
                                    const ComputedStyle& style,
                                    const CSSProperty& color_property,
                                    PseudoId pseudo_id) {
  switch (pseudo_id) {
    case kPseudoIdSelection:
      if (!LayoutTheme::GetTheme().SupportsSelectionForegroundColors())
        return style.VisitedDependentColor(color_property);
      if (document.GetFrame()->Selection().FrameIsFocusedAndActive()) {
        return LayoutTheme::GetTheme().ActiveSelectionForegroundColor(
            style.UsedColorScheme());
      }
      return LayoutTheme::GetTheme().InactiveSelectionForegroundColor(
          style.UsedColorScheme());
    case kPseudoIdTargetText:
      return LayoutTheme::GetTheme().PlatformTextSearchColor(
          false /* active match */, document.InForcedColorsMode(),
          style.UsedColorScheme());
    default:
      NOTREACHED();
      return Color();
  }
}

Color HighlightThemeBackgroundColor(const Document& document,
                                    const ComputedStyle& style,
                                    PseudoId pseudo_id) {
  switch (pseudo_id) {
    case kPseudoIdSelection:
      return document.GetFrame()->Selection().FrameIsFocusedAndActive()
                 ? LayoutTheme::GetTheme().ActiveSelectionBackgroundColor(
                       style.UsedColorScheme())
                 : LayoutTheme::GetTheme().InactiveSelectionBackgroundColor(
                       style.UsedColorScheme());
    case kPseudoIdTargetText:
      return LayoutTheme::GetTheme().PlatformTextSearchHighlightColor(
          false /* active match */, document.InForcedColorsMode(),
          style.UsedColorScheme());
    default:
      NOTREACHED();
      return Color();
  }
}

scoped_refptr<const ComputedStyle> HighlightPseudoStyle(Node* node,
                                                        PseudoId pseudo) {
  if (!node)
    return nullptr;

  Element* element = nullptr;

  // In Blink, highlight pseudo style only applies to direct children of the
  // element on which the highligh pseudo is matched. In order to be able to
  // style highlight inside elements implemented with a UA shadow tree, like
  // input::selection, we calculate highlight style on the shadow host for
  // elements inside the UA shadow.
  ShadowRoot* root = node->ContainingShadowRoot();
  if (root && root->IsUserAgent())
    element = node->OwnerShadowHost();

  // If we request highlight style for LayoutText, query highlight style on the
  // parent element instead, as that is the node for which the highligh pseudo
  // matches. This should most likely have used FlatTreeTraversal, but since we
  // don't implement inheritance of highlight styles, it would probably break
  // cases where you style a shadow host with a highlight pseudo and expect
  // light tree text children to be affected by that style.
  if (!element)
    element = Traversal<Element>::FirstAncestorOrSelf(*node);

  // <content> and <shadow> elements do not have ComputedStyle, hence they will
  // return null for StyleForPseudoElement(). Return early to avoid DCHECK
  // failure for GetComputedStyle() inside StyleForPseudoElement() below.
  if (!element || element->IsPseudoElement() ||
      IsActiveV0InsertionPoint(*element)) {
    return nullptr;
  }

  PseudoElementStyleRequest request(pseudo);

  if (pseudo == kPseudoIdSelection &&
      element->GetDocument().GetStyleEngine().UsesWindowInactiveSelector() &&
      !element->GetDocument().GetPage()->GetFocusController().IsActive()) {
    // ::selection and ::selection:window-inactive styles may be different. Only
    // cache the styles for ::selection if there are no :window-inactive
    // selector, or if the page is active.
    return element->StyleForPseudoElement(request, element->GetComputedStyle());
  }

  return element->CachedStyleForPseudoElement(request);
}

Color HighlightColor(const Document& document,
                     const ComputedStyle& style,
                     Node* node,
                     PseudoId pseudo,
                     const CSSProperty& color_property,
                     const GlobalPaintFlags global_paint_flags) {
  if (pseudo == kPseudoIdSelection) {
    // If the element is unselectable, or we are only painting the selection,
    // don't override the foreground color with the selection foreground color.
    if ((node && !NodeIsSelectable(style, node)) ||
        (global_paint_flags & kGlobalPaintSelectionDragImageOnly)) {
      return style.VisitedDependentColor(color_property);
    }
  }

  scoped_refptr<const ComputedStyle> pseudo_style =
      HighlightPseudoStyle(node, pseudo);

  mojom::blink::ColorScheme color_scheme = style.UsedColorScheme();
  if (pseudo_style) {
    if (!document.InForcedColorsMode() ||
        pseudo_style->ForcedColorAdjust() == EForcedColorAdjust::kNone) {
      return pseudo_style->VisitedDependentColor(color_property);
    }
    color_scheme = pseudo_style->UsedColorScheme();
  }

  if (document.InForcedColorsMode())
    return ForcedSystemForegroundColor(pseudo, color_scheme);
  return HighlightThemeForegroundColor(document, style, color_property, pseudo);
}

}  // anonymous namespace

Color HighlightPaintingUtils::HighlightBackgroundColor(
    const Document& document,
    const ComputedStyle& style,
    Node* node,
    PseudoId pseudo) {
  if (pseudo == kPseudoIdSelection) {
    if (node && !NodeIsSelectable(style, node))
      return Color::kTransparent;
  }

  mojom::blink::ColorScheme color_scheme = style.UsedColorScheme();
  if (scoped_refptr<const ComputedStyle> pseudo_style =
          HighlightPseudoStyle(node, pseudo)) {
    if (!document.InForcedColorsMode() ||
        pseudo_style->ForcedColorAdjust() == EForcedColorAdjust::kNone) {
      Color highlight_color =
          pseudo_style->VisitedDependentColor(GetCSSPropertyBackgroundColor());
      if (pseudo == kPseudoIdSelection && NodeIsReplaced(node)) {
        // Avoid that ::selection full obscures selected replaced elements like
        // images.
        return highlight_color.BlendWithWhite();
      }
      return highlight_color;
    }
    color_scheme = pseudo_style->UsedColorScheme();
  }

  if (document.InForcedColorsMode())
    return ForcedSystemBackgroundColor(pseudo, color_scheme);
  return HighlightThemeBackgroundColor(document, style, pseudo);
}

base::Optional<AppliedTextDecoration>
HighlightPaintingUtils::HighlightTextDecoration(
    const ComputedStyle& style,
    const ComputedStyle& pseudo_style) {
  const Vector<AppliedTextDecoration>& style_decorations =
      style.AppliedTextDecorations();
  const Vector<AppliedTextDecoration>& pseudo_style_decorations =
      pseudo_style.AppliedTextDecorations();

  if (style_decorations.IsEmpty())
    return base::nullopt;

  base::Optional<AppliedTextDecoration> highlight_text_decoration =
      style_decorations.back();

  if (pseudo_style_decorations.size() &&
      style_decorations.back().Lines() ==
          pseudo_style_decorations.back().Lines()) {
    highlight_text_decoration = pseudo_style_decorations.back();
  }

  highlight_text_decoration.value().SetColor(
      pseudo_style.VisitedDependentColor(GetCSSPropertyTextDecorationColor()));

  return highlight_text_decoration;
}

Color HighlightPaintingUtils::HighlightForegroundColor(
    const Document& document,
    const ComputedStyle& style,
    Node* node,
    PseudoId pseudo,
    const GlobalPaintFlags global_paint_flags) {
  return HighlightColor(document, style, node, pseudo,
                        GetCSSPropertyWebkitTextFillColor(),
                        global_paint_flags);
}

Color HighlightPaintingUtils::HighlightEmphasisMarkColor(
    const Document& document,
    const ComputedStyle& style,
    Node* node,
    PseudoId pseudo,
    const GlobalPaintFlags global_paint_flags) {
  return HighlightColor(document, style, node, pseudo,
                        GetCSSPropertyWebkitTextEmphasisColor(),
                        global_paint_flags);
}

TextPaintStyle HighlightPaintingUtils::HighlightPaintingStyle(
    const Document& document,
    const ComputedStyle& style,
    Node* node,
    PseudoId pseudo,
    const TextPaintStyle& text_style,
    const PaintInfo& paint_info) {
  TextPaintStyle highlight_style = text_style;
  bool uses_text_as_clip = paint_info.phase == PaintPhase::kTextClip;
  const GlobalPaintFlags global_paint_flags = paint_info.GetGlobalPaintFlags();

  if (!uses_text_as_clip) {
    highlight_style.fill_color = HighlightForegroundColor(
        document, style, node, pseudo, global_paint_flags);
    highlight_style.emphasis_mark_color = HighlightEmphasisMarkColor(
        document, style, node, pseudo, global_paint_flags);
  }

  if (scoped_refptr<const ComputedStyle> pseudo_style =
          HighlightPseudoStyle(node, pseudo)) {
    highlight_style.stroke_color =
        uses_text_as_clip ? Color::kBlack
                          : pseudo_style->VisitedDependentColor(
                                GetCSSPropertyWebkitTextStrokeColor());
    highlight_style.stroke_width = pseudo_style->TextStrokeWidth();
    highlight_style.shadow =
        uses_text_as_clip ? nullptr : pseudo_style->TextShadow();
    highlight_style.selection_text_decoration =
        HighlightTextDecoration(style, *pseudo_style);
  }

  // Text shadows are disabled when printing. http://crbug.com/258321
  if (paint_info.IsPrinting())
    highlight_style.shadow = nullptr;

  return highlight_style;
}

}  // namespace blink