Bug 1730088 part 2: HyperTextAccessibleBase: Add methods for child index/text offset conversion. r=eeejay

Although HyperTextAccessibleBase implements these, we leave the HyperTextAccessible implementations (overriding the base) because they do caching.
Depending on performance, we may eventually want to move the caching into the base implementation.
I decided not to do this initially because it will use extra memory in the parent process and it may be a premature optimisation.

Differential Revision: https://phabricator.services.mozilla.com/D127207
This commit is contained in:
James Teh 2021-10-07 04:22:44 +00:00
Родитель f46a0aaf44
Коммит 68d5471fbc
5 изменённых файлов: 121 добавлений и 32 удалений

Просмотреть файл

@ -0,0 +1,67 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "HyperTextAccessibleBase.h"
#include "mozilla/a11y/Accessible.h"
#include "nsAccUtils.h"
namespace mozilla::a11y {
int32_t HyperTextAccessibleBase::GetChildIndexAtOffset(uint32_t aOffset) const {
const Accessible* thisAcc = Acc();
uint32_t childCount = thisAcc->ChildCount();
uint32_t lastTextOffset = 0;
for (uint32_t childIndex = 0; childIndex < childCount; ++childIndex) {
Accessible* child = thisAcc->ChildAt(childIndex);
lastTextOffset += nsAccUtils::TextLength(child);
if (aOffset < lastTextOffset) {
return childIndex;
}
}
if (aOffset == lastTextOffset) {
return childCount - 1;
}
return -1;
}
Accessible* HyperTextAccessibleBase::GetChildAtOffset(uint32_t aOffset) const {
const Accessible* thisAcc = Acc();
return thisAcc->ChildAt(GetChildIndexAtOffset(aOffset));
}
int32_t HyperTextAccessibleBase::GetChildOffset(const Accessible* aChild,
bool aInvalidateAfter) const {
const Accessible* thisAcc = Acc();
if (aChild->Parent() != thisAcc) {
return -1;
}
int32_t index = aChild->IndexInParent();
if (index == -1) {
return -1;
}
return GetChildOffset(index, aInvalidateAfter);
}
int32_t HyperTextAccessibleBase::GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter) const {
if (aChildIndex == 0) {
return 0;
}
const Accessible* thisAcc = Acc();
MOZ_ASSERT(aChildIndex <= thisAcc->ChildCount());
uint32_t lastTextOffset = 0;
for (uint32_t childIndex = 0; childIndex <= aChildIndex; ++childIndex) {
if (childIndex == aChildIndex) {
return lastTextOffset;
}
Accessible* child = thisAcc->ChildAt(childIndex);
lastTextOffset += nsAccUtils::TextLength(child);
}
MOZ_ASSERT_UNREACHABLE();
return lastTextOffset;
}
} // namespace mozilla::a11y

Просмотреть файл

@ -7,6 +7,7 @@
#define _HyperTextAccessibleBase_H_
namespace mozilla::a11y {
class Accessible;
// This character marks where in the text returned via Text interface,
// that embedded object characters exist
@ -14,7 +15,47 @@ const char16_t kEmbeddedObjectChar = 0xfffc;
const char16_t kImaginaryEmbeddedObjectChar = ' ';
const char16_t kForcedNewLineChar = '\n';
class HyperTextAccessibleBase {};
class HyperTextAccessibleBase {
public:
/**
* Return child accessible at the given text offset.
*
* @param aOffset [in] the given text offset
*/
virtual int32_t GetChildIndexAtOffset(uint32_t aOffset) const;
/**
* Return child accessible at the given text offset.
*
* @param aOffset [in] the given text offset
*/
virtual Accessible* GetChildAtOffset(uint32_t aOffset) const;
/**
* Return text offset of the given child accessible within hypertext
* accessible.
*
* @param aChild [in] accessible child to get text offset for
* @param aInvalidateAfter [in, optional] indicates whether invalidate
* cached offsets for next siblings of the child
*/
int32_t GetChildOffset(const Accessible* aChild,
bool aInvalidateAfter = false) const;
/**
* Return text offset for the child accessible index.
*/
virtual int32_t GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter = false) const;
protected:
virtual const Accessible* Acc() const = 0;
Accessible* Acc() {
const Accessible* acc =
const_cast<const HyperTextAccessibleBase*>(this)->Acc();
return const_cast<Accessible*>(acc);
}
};
} // namespace mozilla::a11y

Просмотреть файл

@ -11,6 +11,7 @@ EXPORTS.mozilla.a11y += [
UNIFIED_SOURCES += [
"Accessible.cpp",
"HyperTextAccessibleBase.cpp",
]
LOCAL_INCLUDES += [

Просмотреть файл

@ -221,39 +221,14 @@ class HyperTextAccessible : public AccessibleWrap,
*/
already_AddRefed<AccAttributes> DefaultTextAttributes();
/**
* Return text offset of the given child accessible within hypertext
* accessible.
*
* @param aChild [in] accessible child to get text offset for
* @param aInvalidateAfter [in, optional] indicates whether invalidate
* cached offsets for next siblings of the child
*/
int32_t GetChildOffset(const LocalAccessible* aChild,
bool aInvalidateAfter = false) const {
int32_t index = GetIndexOf(aChild);
return index == -1 ? -1 : GetChildOffset(index, aInvalidateAfter);
}
// HyperTextAccessibleBase provides an overload which takes an Accessible.
using HyperTextAccessibleBase::GetChildOffset;
virtual int32_t GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter = false) const override;
/**
* Return text offset for the child accessible index.
*/
int32_t GetChildOffset(uint32_t aChildIndex,
bool aInvalidateAfter = false) const;
virtual int32_t GetChildIndexAtOffset(uint32_t aOffset) const override;
/**
* Return child accessible at the given text offset.
*
* @param aOffset [in] the given text offset
*/
int32_t GetChildIndexAtOffset(uint32_t aOffset) const;
/**
* Return child accessible at the given text offset.
*
* @param aOffset [in] the given text offset
*/
LocalAccessible* GetChildAtOffset(uint32_t aOffset) const {
virtual LocalAccessible* GetChildAtOffset(uint32_t aOffset) const override {
return LocalChildAt(GetChildIndexAtOffset(aOffset));
}
@ -543,6 +518,9 @@ class HyperTextAccessible : public AccessibleWrap,
*/
void SetMathMLXMLRoles(AccAttributes* aAttributes);
// HyperTextAccessibleBase
virtual const Accessible* Acc() const override { return this; }
private:
/**
* End text offsets array.

Просмотреть файл

@ -262,6 +262,8 @@ class RemoteAccessibleBase : public Accessible, public HyperTextAccessibleBase {
uint64_t mID;
protected:
virtual const Accessible* Acc() const override { return this; }
RefPtr<AccAttributes> mCachedFields;
// XXX DocAccessibleParent gets to change this to change the role of