File: ax_inline_text_box.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 (626 lines) | stat: -rw-r--r-- 23,760 bytes parent folder | download | duplicates (5)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
 * Copyright (C) 2013, 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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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/modules/accessibility/ax_inline_text_box.h"

#include <stdint.h>

#include <optional>
#include <utility>
#include <variant>

#include "base/notreached.h"
#include "base/numerics/clamped_math.h"
#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/markers/custom_highlight_marker.h"
#include "third_party/blink/renderer/core/editing/markers/document_marker_controller.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/highlight/highlight.h"
#include "third_party/blink/renderer/core/layout/inline/abstract_inline_text_box.h"
#include "third_party/blink/renderer/core/layout/inline/inline_cursor.h"
#include "third_party/blink/renderer/core/layout/inline/inline_node.h"
#include "third_party/blink/renderer/core/layout/inline/offset_mapping.h"
#include "third_party/blink/renderer/core/layout/layout_text.h"
#include "third_party/blink/renderer/modules/accessibility/ax_block_flow_iterator.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object-inl.h"
#include "third_party/blink/renderer/modules/accessibility/ax_object_cache_impl.h"
#include "third_party/blink/renderer/modules/accessibility/ax_position.h"
#include "third_party/blink/renderer/modules/accessibility/ax_range.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/ax_enums.mojom-blink-forward.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/gfx/geometry/transform.h"

namespace blink {
namespace {

using AXInlineTextBoxType = std::variant<Member<AbstractInlineTextBox>,
                                         AXBlockFlowIterator::FragmentIndex>;

}  // namespace
AXInlineTextBox::AXInlineTextBox(AbstractInlineTextBox* inline_text_box,
                                 AXObjectCacheImpl& ax_object_cache)
    : AXObject(ax_object_cache), data_(inline_text_box) {}

AXInlineTextBox::AXInlineTextBox(AXBlockFlowIterator::FragmentIndex index,
                                 AXObjectCacheImpl& ax_object_cache)
    : AXObject(ax_object_cache), data_(index) {}

void AXInlineTextBox::Trace(Visitor* visitor) const {
  if (auto inline_text_box =
          std::get_if<Member<AbstractInlineTextBox>>(&data_)) {
    visitor->Trace(*inline_text_box);
  }
  AXObject::Trace(visitor);
}

void AXInlineTextBox::GetRelativeBounds(AXObject** out_container,
                                        gfx::RectF& out_bounds_in_container,
                                        gfx::Transform& out_container_transform,
                                        bool* clips_children) const {
  *out_container = nullptr;
  out_bounds_in_container = gfx::RectF();
  out_container_transform.MakeIdentity();

  if (!ParentObject() || !ParentObject()->GetLayoutObject()) {
    return;
  }

  *out_container = ParentObject();

  if (auto* inline_text_box = GetInlineTextBox()) {
    out_bounds_in_container = gfx::RectF(inline_text_box->LocalBounds());
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    out_bounds_in_container = gfx::RectF(it.LocalBounds());
  }

  // Subtract the local bounding box of the parent because they're
  // both in the same coordinate system.
  gfx::RectF parent_bounding_box =
      ParentObject()->LocalBoundingBoxRectForAccessibility();
  out_bounds_in_container.Offset(-parent_bounding_box.OffsetFromOrigin());
}

bool AXInlineTextBox::ComputeIsIgnored(
    IgnoredReasons* ignored_reasons) const {
  AXObject* parent = ParentObject();
  if (!parent)
    return false;

  if (!parent->IsIgnored())
    return false;

  if (ignored_reasons)
    parent->ComputeIsIgnored(ignored_reasons);

  return true;
}

void AXInlineTextBox::TextCharacterOffsets(Vector<int>& offsets) const {
  if (IsDetached())
    return;

  if (auto* inline_text_box = GetInlineTextBox()) {
    inline_text_box->GetCharacterLayoutPixelOffsets(offsets);
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    it.GetCharacterLayoutPixelOffsets(offsets);
  }
}

void AXInlineTextBox::GetWordBoundaries(Vector<int>& word_starts,
                                        Vector<int>& word_ends) const {
  ax::mojom::blink::NameFrom name_not_used;
  if (GetName(name_not_used, /*name_objects=*/nullptr)
          .ContainsOnlyWhitespaceOrEmpty()) {
    return;
  }

  Vector<AbstractInlineTextBox::WordBoundaries> boundaries;
  if (auto* inline_text_box = GetInlineTextBox()) {
    inline_text_box->GetWordBoundaries(boundaries);
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    // Todo: refactor this method to be in some other place, so it can be used
    // by both algorithms, as it does not depend on internal class state.
    AbstractInlineTextBox::GetWordBoundariesForText(boundaries, it.GetText());
  }
  word_starts.reserve(boundaries.size());
  word_ends.reserve(boundaries.size());
  for (const auto& boundary : boundaries) {
    word_starts.push_back(boundary.start_index);
    word_ends.push_back(boundary.end_index);
  }
}

int AXInlineTextBox::TextOffsetInFormattingContext(int offset) const {
  DCHECK_GE(offset, 0);
  if (IsDetached())
    return 0;

  // Retrieve the text offset from the start of the layout block flow ancestor.
  if (auto* inline_text_box = GetInlineTextBox()) {
    return static_cast<int>(inline_text_box->TextOffsetInFormattingContext(
        static_cast<unsigned int>(offset)));
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());

    return it.TextStartOffset() + offset;
  }
  NOTREACHED();
}

int AXInlineTextBox::TextOffsetInContainer(int offset) const {
  DCHECK_GE(offset, 0);
  if (IsDetached())
    return 0;

  // Retrieve the text offset from the start of the layout block flow ancestor.
  int offset_in_block_flow_container = TextOffsetInFormattingContext(offset);
  const AXObject* parent = ParentObject();
  if (!parent)
    return offset_in_block_flow_container;

  // If the parent object in the accessibility tree exists, then it is either
  // a static text object or a line break. In the static text case, it is an
  // AXNodeObject associated with an inline text object. Hence the container
  // is another inline object, not a layout block flow. We need to subtract the
  // text start offset of the static text parent from the text start offset of
  // this inline text box.
  int offset_in_inline_parent = parent->TextOffsetInFormattingContext(0);
  // TODO(nektar) Figure out why this asserts in marker-hyphens.html.
  // To see error, comment out below early return and run command similar to:
  // run_web_tests.py --driver-logging -t linux-debug
  //   --additional-driver-flag=--force-renderer-accessibility
  //   external/wpt/css/css-pseudo/marker-hyphens.html
  // DCHECK_LE(offset_in_inline_parent, offset_in_block_flow_container);
  return offset_in_block_flow_container - offset_in_inline_parent;
}

String AXInlineTextBox::GetName(ax::mojom::blink::NameFrom& name_from,
                                AXObject::AXObjectVector* name_objects) const {
  if (IsDetached())
    return String();

  name_from = ax::mojom::blink::NameFrom::kContents;
  if (auto* inline_text_box = GetInlineTextBox()) {
    return inline_text_box->GetText();
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());

    return it.GetText();
  }
  NOTREACHED();
}

// In addition to LTR and RTL direction, edit fields also support
// top to bottom and bottom to top via the CSS writing-mode property.
ax::mojom::blink::WritingDirection AXInlineTextBox::GetTextDirection() const {
  if (IsDetached())
    return AXObject::GetTextDirection();

  PhysicalDirection direction;
  if (auto* inline_text_box = GetInlineTextBox()) {
    direction = inline_text_box->GetDirection();
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    direction = it.GetDirection();
  }

  switch (direction) {
    case PhysicalDirection::kRight:
      return ax::mojom::blink::WritingDirection::kLtr;
    case PhysicalDirection::kLeft:
      return ax::mojom::blink::WritingDirection::kRtl;
    case PhysicalDirection::kDown:
      return ax::mojom::blink::WritingDirection::kTtb;
    case PhysicalDirection::kUp:
      return ax::mojom::blink::WritingDirection::kBtt;
  }

  return AXObject::GetTextDirection();
}

Document* AXInlineTextBox::GetDocument() const {
  return ParentObject() ? ParentObject()->GetDocument() : nullptr;
}

AbstractInlineTextBox* AXInlineTextBox::GetInlineTextBox() const {
  if (auto inline_text_box =
          std::get_if<Member<AbstractInlineTextBox>>(&data_)) {
    return (*inline_text_box).Get();
  }
  return nullptr;
}

AXObject* AXInlineTextBox::NextOnLine() const {
  if (IsDetached())
    return nullptr;

  // TODO(crbug.com/399206210): Investigate whether
  // AXInlineTextBox::NextOnLine of list markers should point to the list item
  // content.
  if (GetInlineTextBox()) {
    return NeighboringOnLine(Direction::kNext);
  } else {
    return NeighboringOnLineWithAXBlockFlowIterator(Direction::kNext);
  }

  NOTREACHED();
}

AXObject* AXInlineTextBox::PreviousOnLine() const {
  if (IsDetached())
    return nullptr;

  if (IsPartOfAListItem()) {
    return ParentObject()->PreviousOnLine();
  }

  if (GetInlineTextBox()) {
    return NeighboringOnLine(Direction::kPrevious);
  } else {
    return NeighboringOnLineWithAXBlockFlowIterator(Direction::kPrevious);
  }
  NOTREACHED();
}

std::optional<AXBlockFlowIterator::FragmentIndex>
AXInlineTextBox::GetFragmentIndex() const {
  if (auto* index = std::get_if<AXBlockFlowIterator::FragmentIndex>(&data_)) {
    return *index;
  }

  return std::nullopt;
}

void AXInlineTextBox::SerializeMarkerAttributes(
    ui::AXNodeData* node_data) const {
  // TODO(nektar) Address 20% performance degredation and restore code.
  // It may be necessary to add document markers as part of tree data instead
  // of computing for every node. To measure current performance, create a
  // release build without DCHECKs, and then run command similar to:
  // tools/perf/run_benchmark blink_perf.accessibility   --browser=exact \
  //   --browser-executable=path/to/chrome --story-filter="accessibility.*"
  //   --results-label="[my-branch-name]"
  // Pay attention only to rows with  ProcessDeferredAccessibilityEvents
  // and RenderAccessibilityImpl::SendPendingAccessibilityEvents.
  if (!RuntimeEnabledFeatures::
          AccessibilityUseAXPositionForDocumentMarkersEnabled())
    return;

  if (IsDetached())
    return;
  if (!GetDocument() || GetDocument()->IsSlotAssignmentDirty()) {
    // In order to retrieve the document markers we need access to the flat
    // tree. If the slot assignments in a shadow DOM subtree are dirty,
    // accessing the flat tree will cause them to be updated, which could in
    // turn cause an update to the accessibility tree, potentially causing this
    // method to be called repeatedly.
    return;  // Wait until distribution for flat tree traversal has been
             // updated.
  }

  int text_length = TextLength();
  if (!text_length)
    return;
  const auto ax_range = AXRange::RangeOfContents(*this);

  std::vector<int32_t> marker_types;
  std::vector<int32_t> highlight_types;
  std::vector<int32_t> marker_starts;
  std::vector<int32_t> marker_ends;

  // First use ARIA markers for spelling/grammar if available.
  std::optional<DocumentMarker::MarkerType> aria_marker_type =
      GetAriaSpellingOrGrammarMarker();
  if (aria_marker_type) {
    marker_types.push_back(ToAXMarkerType(aria_marker_type.value()));
    marker_starts.push_back(ax_range.Start().TextOffset());
    marker_ends.push_back(ax_range.End().TextOffset());
  }

  DocumentMarkerController& marker_controller = GetDocument()->Markers();
  const Position dom_range_start =
      ax_range.Start().ToPosition(AXPositionAdjustmentBehavior::kMoveLeft);
  const Position dom_range_end =
      ax_range.End().ToPosition(AXPositionAdjustmentBehavior::kMoveRight);
  if (dom_range_start.IsNull() || dom_range_end.IsNull())
    return;

  // TODO(nektar) Figure out why the start > end sometimes.
  // To see error, comment out below early return and run command similar to:
  // run_web_tests.py --driver-logging -t linux-debug
  //   --additional-driver-flag=--force-renderer-accessibility
  //   external/wpt/css/css-ui/text-overflow-006.html
  if (dom_range_start > dom_range_end)
    return;  // Temporary until above TODO is resolved.
  DCHECK_LE(dom_range_start, dom_range_end);
  const EphemeralRangeInFlatTree dom_range(
      ToPositionInFlatTree(dom_range_start),
      ToPositionInFlatTree(dom_range_end));
  DCHECK(dom_range.IsNotNull());
  const DocumentMarker::MarkerTypes markers_used_by_accessibility(
      DocumentMarker::kSpelling | DocumentMarker::kGrammar |
      DocumentMarker::kTextMatch | DocumentMarker::kActiveSuggestion |
      DocumentMarker::kSuggestion | DocumentMarker::kTextFragment |
      DocumentMarker::kCustomHighlight);
  // "MarkersIntersectingRange" performs a binary search through the document
  // markers list for markers in the given range and of the given types. It
  // should be of a logarithmic complexity.
  const VectorOfPairs<const Text, DocumentMarker> node_marker_pairs =
      marker_controller.MarkersIntersectingRange(dom_range,
                                                 markers_used_by_accessibility);
  const int start_text_offset_in_parent = TextOffsetInContainer(0);
  for (const auto& node_marker_pair : node_marker_pairs) {
    DCHECK_EQ(ParentObject()->GetNode(), node_marker_pair.first);
    const DocumentMarker* marker = node_marker_pair.second;

    if (aria_marker_type == marker->GetType())
      continue;

    // The document markers are represented by DOM offsets in this object's
    // static text parent. We need to translate to text offsets in the
    // accessibility tree, first in this object's parent and then to local text
    // offsets.
    const auto start_position = AXPosition::FromPosition(
        Position(*ParentObject()->GetNode(), marker->StartOffset()),
        AXObjectCache(), TextAffinity::kDownstream,
        AXPositionAdjustmentBehavior::kMoveLeft);
    const auto end_position = AXPosition::FromPosition(
        Position(*ParentObject()->GetNode(), marker->EndOffset()),
        AXObjectCache(), TextAffinity::kDownstream,
        AXPositionAdjustmentBehavior::kMoveRight);
    if (!start_position.IsValid() || !end_position.IsValid())
      continue;

    const int local_start_offset = base::ClampMax(
        start_position.TextOffset() - start_text_offset_in_parent, 0);
    DCHECK_LE(local_start_offset, text_length);
    const int local_end_offset = base::ClampMin(
        end_position.TextOffset() - start_text_offset_in_parent, text_length);
    DCHECK_GE(local_end_offset, 0);

    int32_t highlight_type =
        static_cast<int32_t>(ax::mojom::blink::HighlightType::kNone);
    if (marker->GetType() == DocumentMarker::kCustomHighlight) {
      const auto& highlight_marker = To<CustomHighlightMarker>(*marker);
      highlight_type =
          ToAXHighlightType(highlight_marker.GetHighlight()->type());
    }

    marker_types.push_back(int32_t{ToAXMarkerType(marker->GetType())});
    highlight_types.push_back(static_cast<int32_t>(highlight_type));
    marker_starts.push_back(local_start_offset);
    marker_ends.push_back(local_end_offset);
  }

  DCHECK_EQ(marker_types.size(), marker_starts.size());
  DCHECK_EQ(marker_types.size(), marker_ends.size());

  if (marker_types.empty())
    return;

  node_data->AddIntListAttribute(
      ax::mojom::blink::IntListAttribute::kMarkerTypes, marker_types);
  node_data->AddIntListAttribute(
      ax::mojom::blink::IntListAttribute::kHighlightTypes, highlight_types);
  node_data->AddIntListAttribute(
      ax::mojom::blink::IntListAttribute::kMarkerStarts, marker_starts);
  node_data->AddIntListAttribute(
      ax::mojom::blink::IntListAttribute::kMarkerEnds, marker_ends);
}

void AXInlineTextBox::Init(AXObject* parent) {
  CHECK(!AXObjectCache().IsFrozen());
  role_ = ax::mojom::blink::Role::kInlineTextBox;
  DCHECK(parent);
  DCHECK(ui::CanHaveInlineTextBoxChildren(parent->RoleValue()))
      << "Unexpected parent of inline text box: " << parent->RoleValue();
  DCHECK(parent->CanHaveChildren())
      << "Parent cannot have children: " << parent;
  // Don't call SetParent(), which calls SetAncestorsHaveDirtyDescendants(),
  // because once inline textboxes are loaded for the parent text, it's never
  // necessary to again recompute this part of the tree.
  parent_ = parent;
  UpdateCachedAttributeValuesIfNeeded(false);
}

void AXInlineTextBox::Detach() {
  AXObject::Detach();
  if (GetInlineTextBox()) {
    data_ = nullptr;
  } else {
    data_ = 0u;
  }
}

bool AXInlineTextBox::IsAXInlineTextBox() const {
  return true;
}

bool AXInlineTextBox::IsLineBreakingObject() const {
  if (IsDetached())
    return AXObject::IsLineBreakingObject();

  // If this object is a forced line break, or the parent is a <br>
  // element, then this object is line breaking.
  const AXObject* parent = ParentObject();
  bool is_line_break = false;
  if (auto* inline_text_box = GetInlineTextBox()) {
    is_line_break = inline_text_box->IsLineBreak();
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    is_line_break = it.IsLineBreak();
  }
  return is_line_break ||
         (parent && parent->RoleValue() == ax::mojom::blink::Role::kLineBreak);
}

int AXInlineTextBox::TextLength() const {
  if (IsDetached())
    return 0;

  int len = 0;
  if (auto* inline_text_box = GetInlineTextBox()) {
    len = inline_text_box->Len();
  } else {
    AXBlockFlowIterator it(ParentObject());
    it.MoveToIndex(GetFragmentIndex().value());
    len = it.GetText().length();
  }

  return len;
}

void AXInlineTextBox::ClearChildren() {
  // An AXInlineTextBox has no children to clear.
}

bool AXInlineTextBox::IsPartOfAListItem() const {
  // An AXInlineTextBox that is part of a list item needs special handling, as
  // the list marker content is rendered in a separate box than the list item
  // content, but accessibility still wants to connect the two in the same line.
  if (ParentObject()->ParentObject()->RoleValue() ==
      ax::mojom::blink::Role::kListItem) {
    return true;
  }
  return false;
}

AXObject* AXInlineTextBox::NeighboringOnLine(const Direction direction) const {
  InlineCursor cursor = GetInlineTextBox()->GetCursorOnLine();
  AbstractInlineTextBox* candidate = nullptr;
  while (cursor && !candidate) {
    direction == Direction::kNext ? cursor.MoveToNext()
                                  : cursor.MoveToPrevious();
    if (!cursor) {
      return nullptr;
    }
    switch (cursor.Current().Item()->Type()) {
      case FragmentItem::kText:
      case FragmentItem::kGeneratedText:
        if (cursor.Current().GetLayoutObject()->IsText()) {
          candidate = AbstractInlineTextBox::GetOrCreate(cursor);
        }
        break;

      case FragmentItem::kLine:
        return nullptr;

      case FragmentItem::kBox:
        if (cursor.Current().Item()->BoxFragment()) {
          // TODO(crbug.com/399204651): Implement navigating into separate
          // PhysicalBox
          // fragments.
          return ::features::
                         IsAccessibilityPruneRedundantInlineConnectivityEnabled()
                     ? nullptr
                 : direction == Direction::kNext
                     ? ParentObject()->NextOnLine()
                     : ParentObject()->PreviousOnLine();
        }
        // Inline-box continues on to the next/previous item.
        break;

      case FragmentItem::kInvalid:
        NOTREACHED();
    }
  }

  if (!candidate) {
    return nullptr;
  }

  if (AXObject* obj = AXObjectCache().Get(candidate)) {
    return obj;
  }

  // If an AbstractInlineTextBox exists, but no AXObject is associated with
  // it, this means that the text is associated with an inert or aria-hiden
  // node. Delegate to the parent the decision.
  return ::features::IsAccessibilityPruneRedundantInlineConnectivityEnabled()
             ? nullptr
         : direction == Direction::kNext ? ParentObject()->NextOnLine()
                                         : ParentObject()->PreviousOnLine();
}

AXObject* AXInlineTextBox::NeighboringOnLineWithAXBlockFlowIterator(
    const Direction direction) const {
  AXBlockFlowIterator it(ParentObject());
  it.MoveToIndex(GetFragmentIndex().value());
  AXBlockFlowData::Neighbor result_or_status;
  switch (direction) {
    case Direction::kNext:
      result_or_status = it.NextOnLineAsIndex();
      break;
    case Direction::kPrevious:
      result_or_status = it.PreviousOnLineAsIndex();
      break;
  }

  auto* status = std::get_if<AXBlockFlowData::FailureReason>(&result_or_status);
  if (status) {
    switch (*status) {
      case AXBlockFlowData::FailureReason::kAtLineBoundary:
        return nullptr;
      case AXBlockFlowData::FailureReason::kAtBoxFragment:
        return ::features::
                       IsAccessibilityPruneRedundantInlineConnectivityEnabled()
                   ? nullptr
               : direction == Direction::kNext
                   ? ParentObject()->NextOnLine()
                   : ParentObject()->PreviousOnLine();
    }
  }

  AXBlockFlowData::TextFragmentKey* key =
      std::get_if<AXBlockFlowData::TextFragmentKey>(&result_or_status);
  CHECK(key);
  AXObject* obj = AXObjectCache().Get(key->first, key->second);
  if (obj) {
    return obj;
  }

  return ::features::IsAccessibilityPruneRedundantInlineConnectivityEnabled()
             ? nullptr
         : direction == Direction::kNext ? ParentObject()->NextOnLine()
                                         : ParentObject()->PreviousOnLine();
}

}  // namespace blink