File: focusgroup_flags.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (392 lines) | stat: -rw-r--r-- 16,254 bytes parent folder | download | duplicates (2)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2022 The Chromium Authors
// 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/dom/focusgroup_flags.h"

#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
#include "third_party/blink/renderer/core/dom/space_split_string.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/text/strcat.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink::focusgroup {

FocusgroupFlags FindNearestFocusgroupAncestorFlags(const Element* element) {
  Element* ancestor = FlatTreeTraversal::ParentElement(*element);
  while (ancestor) {
    FocusgroupFlags ancestor_flags = ancestor->GetFocusgroupFlags();
    // When this is true, we found the focusgroup to extend.
    if (ancestor_flags != FocusgroupFlags::kNone) {
      return ancestor_flags;
    }
    ancestor = FlatTreeTraversal::ParentElement(*ancestor);
  }
  return FocusgroupFlags::kNone;
}

FocusgroupFlags ParseFocusgroup(const Element* element,
                                const AtomicString& input) {
  DCHECK(element);
  ExecutionContext* context = element->GetExecutionContext();
  DCHECK(RuntimeEnabledFeatures::FocusgroupEnabled(context));

  UseCounter::Count(context, WebFeature::kFocusgroup);

  // 1. Parse the input.
  bool has_extend = false;
  bool has_inline = false;
  bool has_block = false;
  bool has_grid = false;
  bool has_wrap = false;
  bool has_row_wrap = false;
  bool has_col_wrap = false;
  bool has_flow = false;
  bool has_row_flow = false;
  bool has_col_flow = false;
  StringBuilder invalid_tokens;

  SpaceSplitString tokens(input);
  for (unsigned i = 0; i < tokens.size(); i++) {
    AtomicString lowercase_token = tokens[i].LowerASCII();
    if (lowercase_token == "extend") {
      has_extend = true;
    } else if (lowercase_token == "inline") {
      has_inline = true;
    } else if (lowercase_token == "block") {
      has_block = true;
    } else if (lowercase_token == "grid") {
      has_grid = true;
    } else if (lowercase_token == "wrap") {
      has_wrap = true;
    } else if (lowercase_token == "row-wrap") {
      has_row_wrap = true;
    } else if (lowercase_token == "col-wrap") {
      has_col_wrap = true;
    } else if (lowercase_token == "flow") {
      has_flow = true;
    } else if (lowercase_token == "row-flow") {
      has_row_flow = true;
    } else if (lowercase_token == "col-flow") {
      has_col_flow = true;
    } else {
      if (!invalid_tokens.empty())
        invalid_tokens.Append(", ");

      // We don't use |lowercase_token| here since that string value will be
      // logged in the console and we want it to match the input.
      invalid_tokens.Append(tokens[i]);
    }
  }

  if (!invalid_tokens.empty()) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            WTF::StrCat({"Unrecognized focusgroup attribute values: ",
                         invalid_tokens.ReleaseString()})));
  }

  FocusgroupFlags flags = FocusgroupFlags::kNone;

  // 2. Apply the extend logic. A focusgroup can extend another one explicitly
  // when the author specifies "extend" or implicitly when a focusgroup has the
  // "gridcells" role.
  FocusgroupFlags ancestor_flags = FocusgroupFlags::kNone;
  if (has_extend) {
    // Focusgroups should only be allowed to extend when they have a focusgroup
    // ancestor and the focusgroup ancestor isn't a grid focusgroup.
    ancestor_flags = FindNearestFocusgroupAncestorFlags(element);
    if (ancestor_flags != FocusgroupFlags::kNone) {
      flags |= FocusgroupFlags::kExtend;
      if (ancestor_flags & FocusgroupFlags::kGrid) {
        element->GetDocument().AddConsoleMessage(
            MakeGarbageCollected<ConsoleMessage>(
                mojom::blink::ConsoleMessageSource::kOther,
                mojom::blink::ConsoleMessageLevel::kError,
                "Focusgroup attribute value 'extend' present, but grid "
                "focusgroups cannot be extended. Ignoring focusgroup."));
        return FocusgroupFlags::kNone;
      }
    } else {
      element->GetDocument().AddConsoleMessage(
          MakeGarbageCollected<ConsoleMessage>(
              mojom::blink::ConsoleMessageSource::kOther,
              mojom::blink::ConsoleMessageLevel::kError,
              "Focusgroup attribute value 'extend' present, but no parent "
              "focusgroup found. Ignoring 'extend'."));
    }
  }

  // 3. Apply the grid focusgroup logic:
  //     * 'grid' can only be set on an HTML table element.
  //     * The grid-related wrap/flown can only be set on a grid focusgroup.
  if (has_grid) {
    if (has_extend) {
      element->GetDocument().AddConsoleMessage(
          MakeGarbageCollected<ConsoleMessage>(
              mojom::blink::ConsoleMessageSource::kOther,
              mojom::blink::ConsoleMessageLevel::kError,
              "Focusgroup attribute values 'extend' and 'grid' present, but "
              "grid focusgroup cannot extend. Ignoring focusgroup."));
      return FocusgroupFlags::kNone;
    }

    flags |= FocusgroupFlags::kGrid;

    // Set the wrap/flow flags, if specified.
    if (has_wrap) {
      flags |= FocusgroupFlags::kWrapInline | FocusgroupFlags::kWrapBlock;
      if (has_row_wrap) {
        element->GetDocument().AddConsoleMessage(
            MakeGarbageCollected<ConsoleMessage>(
                mojom::blink::ConsoleMessageSource::kOther,
                mojom::blink::ConsoleMessageLevel::kWarning,
                "Focusgroup attribute value 'row-wrap' present, but can be "
                "omitted because focusgroup already wraps in both axes."));
      }
      if (has_col_wrap) {
        element->GetDocument().AddConsoleMessage(
            MakeGarbageCollected<ConsoleMessage>(
                mojom::blink::ConsoleMessageSource::kOther,
                mojom::blink::ConsoleMessageLevel::kWarning,
                "Focusgroup attribute value 'col-wrap' present, but can be "
                "omitted because focusgroup already wraps in both axes."));
      }
    } else {
      if (has_row_wrap)
        flags |= FocusgroupFlags::kWrapInline;
      if (has_col_wrap)
        flags |= FocusgroupFlags::kWrapBlock;

      if (has_row_wrap && has_col_wrap) {
        element->GetDocument().AddConsoleMessage(
            MakeGarbageCollected<ConsoleMessage>(
                mojom::blink::ConsoleMessageSource::kOther,
                mojom::blink::ConsoleMessageLevel::kWarning,
                "Focusgroup attribute values 'row-wrap col-wrap' should be "
                "replaced by 'wrap'."));
      }
    }

    if (has_flow) {
      if (flags & FocusgroupFlags::kWrapInline ||
          flags & FocusgroupFlags::kWrapBlock) {
        element->GetDocument().AddConsoleMessage(MakeGarbageCollected<
                                                 ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'flow' present, but focusgroup already "
            "set to wrap in at least one axis."));
      } else {
        flags |= FocusgroupFlags::kRowFlow | FocusgroupFlags::kColFlow;
        if (has_row_flow) {
          element->GetDocument().AddConsoleMessage(
              MakeGarbageCollected<ConsoleMessage>(
                  mojom::blink::ConsoleMessageSource::kOther,
                  mojom::blink::ConsoleMessageLevel::kWarning,
                  "Focusgroup attribute value 'row-flow' present, but can be "
                  "omitted because focusgroup already flows in both axes."));
        }
        if (has_col_flow) {
          element->GetDocument().AddConsoleMessage(
              MakeGarbageCollected<ConsoleMessage>(
                  mojom::blink::ConsoleMessageSource::kOther,
                  mojom::blink::ConsoleMessageLevel::kWarning,
                  "Focusgroup attribute value 'col-flow' present, but can be "
                  "omitted because focusgroup already flows in both axes."));
        }
      }
    } else {
      if (has_row_flow) {
        if (flags & FocusgroupFlags::kWrapInline) {
          element->GetDocument().AddConsoleMessage(
              MakeGarbageCollected<ConsoleMessage>(
                  mojom::blink::ConsoleMessageSource::kOther,
                  mojom::blink::ConsoleMessageLevel::kError,
                  "Focusgroup attribute value 'row-flow' present, but "
                  "focusgroup already wraps in the row axis."));
        } else {
          flags |= FocusgroupFlags::kRowFlow;
        }
      }
      if (has_col_flow) {
        if (flags & FocusgroupFlags::kWrapBlock) {
          element->GetDocument().AddConsoleMessage(
              MakeGarbageCollected<ConsoleMessage>(
                  mojom::blink::ConsoleMessageSource::kOther,
                  mojom::blink::ConsoleMessageLevel::kError,
                  "Focusgroup attribute value 'col-flow' present, but "
                  "focusgroup already wraps in the column axis."));
        } else {
          flags |= FocusgroupFlags::kColFlow;
        }
      }
      if (flags & FocusgroupFlags::kRowFlow &&
          flags & FocusgroupFlags::kColFlow) {
        element->GetDocument().AddConsoleMessage(
            MakeGarbageCollected<ConsoleMessage>(
                mojom::blink::ConsoleMessageSource::kOther,
                mojom::blink::ConsoleMessageLevel::kWarning,
                "Focusgroup attribute values 'row-flow col-flow' should be "
                "replaced by 'flow'."));
      }
    }

    // These values are reserved for linear focusgroups.
    if (has_inline) {
      element->GetDocument().AddConsoleMessage(
          MakeGarbageCollected<ConsoleMessage>(
              mojom::blink::ConsoleMessageSource::kOther,
              mojom::blink::ConsoleMessageLevel::kError,
              "Focusgroup attribute value 'inline' present, but has no effect "
              "on grid focusgroups."));
    }
    if (has_block) {
      element->GetDocument().AddConsoleMessage(
          MakeGarbageCollected<ConsoleMessage>(
              mojom::blink::ConsoleMessageSource::kOther,
              mojom::blink::ConsoleMessageLevel::kError,
              "Focusgroup attribute value 'block' present, but has no effect "
              "on grid focusgroups."));
    }

    return flags;
  }

  // At this point, we are necessarily in a linear focusgroup. Any grid
  // focusgroup should have returned above.

  if (has_row_wrap) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'row-wrap' present, but has no effect "
            "on linear focusgroups."));
  }
  if (has_col_wrap) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'col-wrap' present, but has no effect "
            "on linear focusgroups."));
  }
  if (has_flow) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'flow' present, but has no effect on "
            "linear focusgroups."));
  }
  if (has_row_flow) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'row-flow' present, but has no effect "
            "on linear focusgroups."));
  }
  if (has_col_flow) {
    element->GetDocument().AddConsoleMessage(
        MakeGarbageCollected<ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kError,
            "Focusgroup attribute value 'col-flow' present, but has no effect "
            "on linear focusgroups."));
  }

  // 4. Set the axis supported on that focusgroup.
  if (has_inline) {
    flags |= FocusgroupFlags::kInline;
  }
  if (has_block) {
    flags |= FocusgroupFlags::kBlock;
  }

  // When no axis is specified, it means that the focusgroup should handle
  // both.
  if (!has_inline && !has_block) {
    flags |= FocusgroupFlags::kInline | FocusgroupFlags::kBlock;
  }

  if (has_inline && has_block) {
    element->GetDocument().AddConsoleMessage(MakeGarbageCollected<
                                             ConsoleMessage>(
        mojom::blink::ConsoleMessageSource::kOther,
        mojom::blink::ConsoleMessageLevel::kWarning,
        "'inline' and 'block' focusgroup attribute values used together "
        "are redundant (this is the default behavior) and can be omitted."));
  }

  // 6. Determine in what axis a focusgroup should wrap. This needs to be
  // performed once the supported axes are final.
  if (has_wrap) {
    if (flags & FocusgroupFlags::kExtend) {
      bool extends_inline = flags & FocusgroupFlags::kInline &&
                            ancestor_flags & FocusgroupFlags::kInline;
      if (!extends_inline && flags & FocusgroupFlags::kInline) {
        flags |= FocusgroupFlags::kWrapInline;
      }
      bool extends_block = flags & FocusgroupFlags::kBlock &&
                           ancestor_flags & FocusgroupFlags::kBlock;
      if (!extends_block && flags & FocusgroupFlags::kBlock) {
        flags |= FocusgroupFlags::kWrapBlock;
      }

      if (extends_inline && extends_block) {
        element->GetDocument().AddConsoleMessage(MakeGarbageCollected<
                                                 ConsoleMessage>(
            mojom::blink::ConsoleMessageSource::kOther,
            mojom::blink::ConsoleMessageLevel::kWarning,
            "Focusgroup attribute value 'wrap' present but ignored. 'wrap' has "
            "no effect when set on a focusgroup that extends another one in "
            "both axes."));
      }
    } else {
      if (flags & FocusgroupFlags::kInline) {
        flags |= FocusgroupFlags::kWrapInline;
      }
      if (flags & FocusgroupFlags::kBlock) {
        flags |= FocusgroupFlags::kWrapBlock;
      }
    }
  }

  // When a focusgroup extends another one, inherit the ancestor's wrap behavior
  // for the descendant's supported axes.
  if (flags & FocusgroupFlags::kExtend) {
    DCHECK(ancestor_flags != FocusgroupFlags::kNone);
    if ((flags & FocusgroupFlags::kWrapInline) ==
            (ancestor_flags & FocusgroupFlags::kWrapInline) &&
        (flags & FocusgroupFlags::kWrapBlock) ==
            (ancestor_flags & FocusgroupFlags::kWrapBlock)) {
      element->GetDocument().AddConsoleMessage(MakeGarbageCollected<
                                               ConsoleMessage>(
          mojom::blink::ConsoleMessageSource::kOther,
          mojom::blink::ConsoleMessageLevel::kWarning,
          "Focusgroup attribute value 'wrap' present but ignored. 'wrap' is "
          "inherited from the extended parent focusgroup."));
    }
    if (flags & FocusgroupFlags::kInline) {
      flags |= (ancestor_flags & FocusgroupFlags::kWrapInline);
    }
    if (flags & FocusgroupFlags::kBlock) {
      flags |= (ancestor_flags & FocusgroupFlags::kWrapBlock);
    }
  }

  return flags;
}

}  // namespace blink::focusgroup