Commit cf661ce6 authored by Nektarios Paisios's avatar Nektarios Paisios Committed by Commit Bot

Switch to using the new AXRange class for spelling markers and word boundaries

This patch begins using the new AXPosition and AXRange classes in a relatively simple part of the codebase in order to avoid regressions.
It also takes the opportunity to rename AXObject::AXRange to AXSelection in preparation of the use of the new AXSelection class in a subsequent patch.
The C++ Style Guide allows the use of initializer lists {...} when returning from or when calling functions, hence this patch switches to the new style in all the selection related functions. This will make the switch to the new AXSelection class in a subsequent patch easier since a rename at the point of construction will not be needed.
The patch makes some functions that are using the AXRange class not inline because the Style Guide dictates that virtual functions should not be inlined.

R=dmazzoni@chromium.org

Bug: 639340
Change-Id: I8e19986b0937fd6a954dfe1f5631064c5a9c42a2
Reviewed-on: https://chromium-review.googlesource.com/995976Reviewed-by: default avatarNektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarAaron Leventhal <aleventhal@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548779}
parent de322468
......@@ -32,6 +32,8 @@
#include "core/layout/LayoutText.h"
#include "core/layout/api/LineLayoutAPIShim.h"
#include "modules/accessibility/AXObjectCacheImpl.h"
#include "modules/accessibility/AXPosition.h"
#include "modules/accessibility/AXRange.h"
#include "platform/LayoutUnit.h"
namespace blink {
......@@ -115,12 +117,14 @@ void AXInlineTextBox::GetWordBoundaries(Vector<AXRange>& words) const {
if (!inline_text_box_ || inline_text_box_->GetText().ContainsOnlyWhitespace())
return;
Vector<AbstractInlineTextBox::WordBoundaries> word_boundaries;
inline_text_box_->GetWordBoundaries(word_boundaries);
words.resize(word_boundaries.size());
for (unsigned i = 0; i < word_boundaries.size(); i++)
words[i] =
AXRange(word_boundaries[i].start_index, word_boundaries[i].end_index);
Vector<AbstractInlineTextBox::WordBoundaries> boundaries;
inline_text_box_->GetWordBoundaries(boundaries);
words.ReserveCapacity(boundaries.size());
for (const auto& boundary : boundaries) {
words.emplace_back(
AXPosition::CreatePositionInTextObject(*this, boundary.start_index),
AXPosition::CreatePositionInTextObject(*this, boundary.end_index));
}
}
String AXInlineTextBox::GetName(AXNameFrom& name_from,
......
......@@ -1775,13 +1775,13 @@ Element* AXLayoutObject::AnchorElement() const {
// Functions that retrieve the current selection.
//
AXObject::AXRange AXLayoutObject::Selection() const {
AXRange text_selection = TextControlSelection();
AXObject::AXSelection AXLayoutObject::Selection() const {
AXSelection text_selection = TextControlSelection();
if (text_selection.IsValid())
return text_selection;
if (!GetLayoutObject() || !GetLayoutObject()->GetFrame())
return AXRange();
return {};
VisibleSelection selection =
GetLayoutObject()
......@@ -1789,7 +1789,7 @@ AXObject::AXRange AXLayoutObject::Selection() const {
->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated();
if (selection.IsNone())
return AXRange();
return {};
VisiblePosition visible_start = selection.VisibleStart();
Position start = visible_start.ToParentAnchoredPosition();
......@@ -1812,12 +1812,12 @@ AXObject::AXRange AXLayoutObject::Selection() const {
anchor_node = anchor_node->parentNode();
}
if (!anchor_object)
return AXRange();
return {};
int anchor_offset = anchor_object->IndexForVisiblePosition(visible_start);
DCHECK_GE(anchor_offset, 0);
if (selection.IsCaret()) {
return AXRange(anchor_object, anchor_offset, start_affinity, anchor_object,
anchor_offset, start_affinity);
return {anchor_object, anchor_offset, start_affinity,
anchor_object, anchor_offset, start_affinity};
}
VisiblePosition visible_end = selection.VisibleEnd();
......@@ -1837,23 +1837,23 @@ AXObject::AXRange AXLayoutObject::Selection() const {
focus_node = focus_node->parentNode();
}
if (!focus_object)
return AXRange();
return {};
int focus_offset = focus_object->IndexForVisiblePosition(visible_end);
DCHECK_GE(focus_offset, 0);
return AXRange(anchor_object, anchor_offset, start_affinity, focus_object,
focus_offset, end_affinity);
return {anchor_object, anchor_offset, start_affinity,
focus_object, focus_offset, end_affinity};
}
// Gets only the start and end offsets of the selection computed using the
// current object as the starting point. Returns a null selection if there is
// no selection in the subtree rooted at this object.
AXObject::AXRange AXLayoutObject::SelectionUnderObject() const {
AXRange text_selection = TextControlSelection();
AXObject::AXSelection AXLayoutObject::SelectionUnderObject() const {
AXSelection text_selection = TextControlSelection();
if (text_selection.IsValid())
return text_selection;
if (!GetNode() || !GetLayoutObject()->GetFrame())
return AXRange();
return {};
VisibleSelection selection =
GetLayoutObject()
......@@ -1870,7 +1870,7 @@ AXObject::AXRange AXLayoutObject::SelectionUnderObject() const {
IGNORE_EXCEPTION_FOR_TESTING) < 0 &&
selection_range->comparePoint(parent_node, node_index + 1,
IGNORE_EXCEPTION_FOR_TESTING) > 0)) {
return AXRange();
return {};
}
int start = IndexForVisiblePosition(selection.VisibleStart());
......@@ -1878,12 +1878,12 @@ AXObject::AXRange AXLayoutObject::SelectionUnderObject() const {
int end = IndexForVisiblePosition(selection.VisibleEnd());
DCHECK_GE(end, 0);
return AXRange(start, end);
return {start, end};
}
AXObject::AXRange AXLayoutObject::TextControlSelection() const {
AXObject::AXSelection AXLayoutObject::TextControlSelection() const {
if (!GetLayoutObject())
return AXRange();
return {};
LayoutObject* layout = nullptr;
if (GetLayoutObject()->IsTextControl()) {
......@@ -1896,11 +1896,11 @@ AXObject::AXRange AXLayoutObject::TextControlSelection() const {
}
if (!layout)
return AXRange();
return {};
AXObject* ax_object = AXObjectCache().GetOrCreate(layout);
if (!ax_object || !ax_object->IsAXLayoutObject())
return AXRange();
return {};
VisibleSelection selection =
layout->GetFrame()
......@@ -1912,8 +1912,8 @@ AXObject::AXRange AXLayoutObject::TextControlSelection() const {
int start = text_control->selectionStart();
int end = text_control->selectionEnd();
return AXRange(ax_object, start, selection.VisibleStart().Affinity(),
ax_object, end, selection.VisibleEnd().Affinity());
return {ax_object, start, selection.VisibleStart().Affinity(),
ax_object, end, selection.VisibleEnd().Affinity()};
}
int AXLayoutObject::IndexForVisiblePosition(
......@@ -2028,7 +2028,7 @@ static VisiblePosition ToVisiblePosition(AXObject* obj, int offset) {
return blink::VisiblePositionForIndex(node_index + offset, parent);
}
bool AXLayoutObject::OnNativeSetSelectionAction(const AXRange& selection) {
bool AXLayoutObject::OnNativeSetSelectionAction(const AXSelection& selection) {
if (!GetLayoutObject() || !selection.IsValid())
return false;
......
......@@ -137,12 +137,12 @@ class MODULES_EXPORT AXLayoutObject : public AXNodeObject {
NameSources*) const override;
// Modify or take an action on an object.
bool OnNativeSetSelectionAction(const AXRange&) override;
bool OnNativeSetSelectionAction(const AXSelection&) override;
bool OnNativeSetValueAction(const String&) override;
// Methods that retrieve or manipulate the current selection.
AXRange Selection() const override;
AXRange SelectionUnderObject() const override;
AXSelection Selection() const override;
AXSelection SelectionUnderObject() const override;
// Hit testing.
AXObject* AccessibilityHitTest(const IntPoint&) const override;
......@@ -203,7 +203,7 @@ class MODULES_EXPORT AXLayoutObject : public AXNodeObject {
void AddInlineTextBoxChildren(bool force);
LayoutRect ComputeElementRect() const;
AXRange TextControlSelection() const;
AXSelection TextControlSelection() const;
int IndexForVisiblePosition(const VisiblePosition&) const;
AXLayoutObject* GetUnignoredObjectFromNode(Node&) const;
......
......@@ -69,6 +69,8 @@
#include "core/layout/LayoutObject.h"
#include "core/svg/SVGElement.h"
#include "modules/accessibility/AXObjectCacheImpl.h"
#include "modules/accessibility/AXPosition.h"
#include "modules/accessibility/AXRange.h"
#include "modules/media_controls/elements/MediaControlElementsHelper.h"
#include "platform/text/PlatformLocale.h"
#include "platform/weborigin/KURL.h"
......@@ -1151,14 +1153,18 @@ void AXNodeObject::Markers(Vector<DocumentMarker::MarkerType>& marker_types,
if (!GetNode() || !GetDocument() || !GetDocument()->View())
return;
if (!GetNode()->IsTextNode())
return;
DocumentMarkerController& marker_controller = GetDocument()->Markers();
DocumentMarkerVector markers = marker_controller.MarkersFor(GetNode());
for (size_t i = 0; i < markers.size(); ++i) {
DocumentMarker* marker = markers[i];
if (MarkerTypeIsUsedForAccessibility(marker->GetType())) {
marker_types.push_back(marker->GetType());
marker_ranges.push_back(
AXRange(marker->StartOffset(), marker->EndOffset()));
marker_ranges.emplace_back(
AXPosition::CreatePositionInTextObject(*this, marker->StartOffset()),
AXPosition::CreatePositionInTextObject(*this, marker->EndOffset()));
}
}
}
......
......@@ -51,6 +51,7 @@
#include "core/layout/LayoutView.h"
#include "core/page/Page.h"
#include "modules/accessibility/AXObjectCacheImpl.h"
#include "modules/accessibility/AXRange.h"
#include "modules/accessibility/AXSparseAttributeSetter.h"
#include "platform/scroll/ScrollAlignment.h"
#include "platform/text/PlatformLocale.h"
......@@ -1475,6 +1476,13 @@ AccessibilityOrientation AXObject::Orientation() const {
return kAccessibilityOrientationUndefined;
}
void AXObject::Markers(Vector<DocumentMarker::MarkerType>&,
Vector<AXRange>&) const {}
void AXObject::TextCharacterOffsets(Vector<int>&) const {}
void AXObject::GetWordBoundaries(Vector<AXRange>&) const {}
AXDefaultActionVerb AXObject::Action() const {
Element* action_element = ActionElement();
if (!action_element)
......@@ -2334,8 +2342,8 @@ bool AXObject::RequestSetSelectedAction(bool selected) {
return OnNativeSetSelectedAction(selected);
}
bool AXObject::RequestSetSelectionAction(const AXRange& range) {
return OnNativeSetSelectionAction(range);
bool AXObject::RequestSetSelectionAction(const AXSelection& selection) {
return OnNativeSetSelectionAction(selection);
}
bool AXObject::RequestSetSequentialFocusNavigationStartingPointAction() {
......@@ -2446,7 +2454,7 @@ bool AXObject::OnNativeSetSelectedAction(bool) {
return false;
}
bool AXObject::OnNativeSetSelectionAction(const AXRange& range) {
bool AXObject::OnNativeSetSelectionAction(const AXSelection& selection) {
return false;
}
......
......@@ -53,6 +53,7 @@ namespace blink {
class AccessibleNodeList;
class AXObject;
class AXObjectCacheImpl;
class AXRange;
class IntPoint;
class LayoutObject;
class LocalFrameView;
......@@ -165,7 +166,7 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
public:
typedef HeapVector<Member<AXObject>> AXObjectVector;
struct AXRange {
struct AXSelection {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
// The deepest descendant in which the range starts.
// (nullptr means the current object.)
......@@ -189,7 +190,7 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
// than the next line.
TextAffinity focus_affinity;
AXRange()
AXSelection()
: anchor_object(nullptr),
anchor_offset(-1),
anchor_affinity(TextAffinity::kUpstream),
......@@ -197,7 +198,7 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
focus_offset(-1),
focus_affinity(TextAffinity::kDownstream) {}
AXRange(int start_offset, int end_offset)
AXSelection(int start_offset, int end_offset)
: anchor_object(nullptr),
anchor_offset(start_offset),
anchor_affinity(TextAffinity::kUpstream),
......@@ -205,12 +206,12 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
focus_offset(end_offset),
focus_affinity(TextAffinity::kDownstream) {}
AXRange(AXObject* anchor_object,
int anchor_offset,
TextAffinity anchor_affinity,
AXObject* focus_object,
int focus_offset,
TextAffinity focus_affinity)
AXSelection(AXObject* anchor_object,
int anchor_offset,
TextAffinity anchor_affinity,
AXObject* focus_object,
int focus_offset,
TextAffinity focus_affinity)
: anchor_object(anchor_object),
anchor_offset(anchor_offset),
anchor_affinity(anchor_affinity),
......@@ -524,13 +525,13 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
// For all node objects. The start and end character offset of each
// marker, such as spelling or grammar error.
virtual void Markers(Vector<DocumentMarker::MarkerType>&,
Vector<AXRange>&) const {}
Vector<AXRange>&) const;
// For an inline text box.
// The integer horizontal pixel offset of each character in the string;
// negative values for RTL.
virtual void TextCharacterOffsets(Vector<int>&) const {}
virtual void TextCharacterOffsets(Vector<int>&) const;
// The start and end character offset of each word in the object's text.
virtual void GetWordBoundaries(Vector<AXRange>&) const {}
virtual void GetWordBoundaries(Vector<AXRange>&) const;
// Properties of interactive elements.
AXDefaultActionVerb Action() const;
......@@ -692,11 +693,11 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
// Methods that retrieve or manipulate the current selection.
// Get the current selection from anywhere in the accessibility tree.
virtual AXRange Selection() const { return AXRange(); }
virtual AXSelection Selection() const { return AXSelection(); }
// Gets only the start and end offsets of the selection computed using the
// current object as the starting point. Returns a null selection if there is
// no selection in the subtree rooted at this object.
virtual AXRange SelectionUnderObject() const { return AXRange(); }
virtual AXSelection SelectionUnderObject() const { return AXSelection(); }
// Scrollable containers.
bool IsScrollableContainer() const;
......@@ -729,7 +730,7 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
bool RequestScrollToMakeVisibleAction();
bool RequestScrollToMakeVisibleWithSubFocusAction(const IntRect&);
bool RequestSetSelectedAction(bool);
bool RequestSetSelectionAction(const AXRange&);
bool RequestSetSelectionAction(const AXSelection&);
bool RequestSetSequentialFocusNavigationStartingPointAction();
bool RequestSetValueAction(const String&);
bool RequestShowContextMenuAction();
......@@ -745,7 +746,7 @@ class MODULES_EXPORT AXObject : public GarbageCollectedFinalized<AXObject> {
virtual bool OnNativeScrollToMakeVisibleWithSubFocusAction(
const IntRect&) const;
virtual bool OnNativeSetSelectedAction(bool);
virtual bool OnNativeSetSelectionAction(const AXRange&);
virtual bool OnNativeSetSelectionAction(const AXSelection&);
virtual bool OnNativeSetSequentialFocusNavigationStartingPointAction();
virtual bool OnNativeSetValueAction(const String&);
virtual bool OnNativeShowContextMenuAction();
......
......@@ -47,6 +47,7 @@
#include "core/style/ComputedStyle.h"
#include "modules/accessibility/AXObject.h"
#include "modules/accessibility/AXObjectCacheImpl.h"
#include "modules/accessibility/AXRange.h"
#include "modules/accessibility/AXTable.h"
#include "modules/accessibility/AXTableCell.h"
#include "modules/accessibility/AXTableColumn.h"
......@@ -754,7 +755,7 @@ void WebAXObject::Selection(WebAXObject& anchor_object,
return;
}
AXObject::AXRange ax_selection = private_->Selection();
AXObject::AXSelection ax_selection = private_->Selection();
anchor_object = WebAXObject(ax_selection.anchor_object);
anchor_offset = ax_selection.anchor_offset;
anchor_affinity =
......@@ -779,9 +780,9 @@ bool WebAXObject::SetSelection(const WebAXObject& anchor_object,
if (IsDetached())
return false;
AXObject::AXRange ax_selection(anchor_object, anchor_offset,
TextAffinity::kUpstream, focus_object,
focus_offset, TextAffinity::kDownstream);
AXObject::AXSelection ax_selection(anchor_object, anchor_offset,
TextAffinity::kUpstream, focus_object,
focus_offset, TextAffinity::kDownstream);
return private_->RequestSetSelectionAction(ax_selection);
}
......@@ -789,7 +790,7 @@ unsigned WebAXObject::SelectionEnd() const {
if (IsDetached())
return 0;
AXObject::AXRange ax_selection = private_->SelectionUnderObject();
AXObject::AXSelection ax_selection = private_->SelectionUnderObject();
if (ax_selection.focus_offset < 0)
return 0;
......@@ -800,7 +801,7 @@ unsigned WebAXObject::SelectionStart() const {
if (IsDetached())
return 0;
AXObject::AXRange ax_selection = private_->SelectionUnderObject();
AXObject::AXSelection ax_selection = private_->SelectionUnderObject();
if (ax_selection.anchor_offset < 0)
return 0;
......@@ -1346,7 +1347,7 @@ void WebAXObject::Markers(WebVector<WebAXMarkerType>& types,
return;
Vector<DocumentMarker::MarkerType> marker_types;
Vector<AXObject::AXRange> marker_ranges;
Vector<AXRange> marker_ranges;
private_->Markers(marker_types, marker_ranges);
DCHECK_EQ(marker_types.size(), marker_ranges.size());
......@@ -1355,9 +1356,11 @@ void WebAXObject::Markers(WebVector<WebAXMarkerType>& types,
WebVector<int> end_offsets(marker_ranges.size());
for (size_t i = 0; i < marker_types.size(); ++i) {
web_marker_types[i] = static_cast<WebAXMarkerType>(marker_types[i]);
DCHECK(marker_ranges[i].IsSimple());
start_offsets[i] = marker_ranges[i].anchor_offset;
end_offsets[i] = marker_ranges[i].focus_offset;
DCHECK(marker_ranges[i].IsValid());
DCHECK_EQ(marker_ranges[i].Start().ContainerObject(),
marker_ranges[i].End().ContainerObject());
start_offsets[i] = marker_ranges[i].Start().TextOffset();
end_offsets[i] = marker_ranges[i].End().TextOffset();
}
types.Swap(web_marker_types);
......@@ -1384,15 +1387,17 @@ void WebAXObject::GetWordBoundaries(WebVector<int>& starts,
if (IsDetached())
return;
Vector<AXObject::AXRange> word_boundaries;
Vector<AXRange> word_boundaries;
private_->GetWordBoundaries(word_boundaries);
WebVector<int> word_start_offsets(word_boundaries.size());
WebVector<int> word_end_offsets(word_boundaries.size());
for (size_t i = 0; i < word_boundaries.size(); ++i) {
DCHECK(word_boundaries[i].IsSimple());
word_start_offsets[i] = word_boundaries[i].anchor_offset;
word_end_offsets[i] = word_boundaries[i].focus_offset;
DCHECK(word_boundaries[i].IsValid());
DCHECK_EQ(word_boundaries[i].Start().ContainerObject(),
word_boundaries[i].End().ContainerObject());
word_start_offsets[i] = word_boundaries[i].Start().TextOffset();
word_end_offsets[i] = word_boundaries[i].End().TextOffset();
}
starts.Swap(word_start_offsets);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment