Bug 1820069: Fix handling of magic offsets in HyperTextAccessible*::Char/TextBounds. r=morgan

1. HyperTextAccessibleBase::CharBounds didn't have code to support magic offsets (like TEXT_OFFSET_CARET) at all, resulting in a crash.
2. HyperTextAccessibleBase::TextBounds handled the end offset being a magic offset, but not the start offset.
3. HyperTextAccessible::CharBounds assumed it could just add 1 to the start offset to pass to TextBounds, which resulted in returning the rect from the caret to the end of the container, since length (-1) is caret (-2) + 1.

This patch addresses all three of these issues.

Differential Revision: https://phabricator.services.mozilla.com/D171545
This commit is contained in:
James Teh committed 2023-03-06 08:58:42 +00:00
1 parent a0b8153ba6
commit 13011243c4
3 files changed
+87 -8

No files matched your search

@@ -210,7 +210,11 @@ bool HyperTextAccessibleBase::CharAt(int32_t aOffset, nsAString& aChar,
LayoutDeviceIntRect HyperTextAccessibleBase::CharBounds(int32_t aOffset,
uint32_t aCoordType) {
TextLeafPoint point = ToTextLeafPoint(aOffset, false);
index_t offset = ConvertMagicOffset(aOffset);
if (!offset.IsValid() || offset > CharacterCount()) {
return LayoutDeviceIntRect();
}
TextLeafPoint point = ToTextLeafPoint(static_cast<int32_t>(offset), false);
if (!point.mAcc || !point.mAcc->IsRemote() ||
!point.mAcc->AsRemote()->mCachedFields) {
return LayoutDeviceIntRect();
@@ -231,7 +235,9 @@ LayoutDeviceIntRect HyperTextAccessibleBase::TextBounds(int32_t aStartOffset,
return result;
}
if (aEndOffset > -1 && aStartOffset >= aEndOffset) {
index_t startOffset = ConvertMagicOffset(aStartOffset);
index_t endOffset = ConvertMagicOffset(aEndOffset);
if (!startOffset.IsValid() || startOffset >= endOffset) {
return LayoutDeviceIntRect();
}
@@ -240,9 +246,10 @@ LayoutDeviceIntRect HyperTextAccessibleBase::TextBounds(int32_t aStartOffset,
// lines, and a simple union may yield an incorrect width. We
// should use the length of the longest spanned line for our width.
TextLeafPoint startPoint = ToTextLeafPoint(aStartOffset, false);
TextLeafPoint startPoint =
ToTextLeafPoint(static_cast<int32_t>(startOffset), false);
TextLeafPoint endPoint =
ToTextLeafPoint(ConvertMagicOffset(aEndOffset), true);
ToTextLeafPoint(static_cast<int32_t>(endOffset), true);
if (!endPoint) {
// The caller provided an invalid offset.
return LayoutDeviceIntRect();
+5 -4
View File
@@ -183,10 +183,11 @@ class HyperTextAccessible : public AccessibleWrap,
LayoutDeviceIntRect CharBounds(int32_t aOffset,
uint32_t aCoordType) override {
int32_t endOffset = aOffset == static_cast<int32_t>(CharacterCount())
? aOffset
: aOffset + 1;
return TextBounds(aOffset, endOffset, aCoordType);
index_t startOffset = ConvertMagicOffset(aOffset);
int32_t endOffset = startOffset == CharacterCount()
? static_cast<int32_t>(startOffset)
: startOffset + 1;
return TextBounds(startOffset, endOffset, aCoordType);
}
/**
@@ -553,3 +553,74 @@ c</textarea>
},
{ chrome: true, topLevel: !isWinNoCache, iframe: !isWinNoCache }
);
/**
* Test magic offsets with GetCharacter/RangeExtents.
*/
addAccessibleTask(
`<input id="input" value="abc">`,
async function(browser, docAcc) {
const input = findAccessibleChildByID(docAcc, "input", [nsIAccessibleText]);
info("Setting caret and focusing input");
let focused = waitForEvent(EVENT_FOCUS, input);
await invokeContentTask(browser, [], () => {
const inputDom = content.document.getElementById("input");
inputDom.selectionStart = inputDom.selectionEnd = 1;
inputDom.focus();
});
await focused;
let expectedX = {};
let expectedY = {};
let expectedW = {};
let expectedH = {};
let magicX = {};
let magicY = {};
let magicW = {};
let magicH = {};
input.getCharacterExtents(
1,
expectedX,
expectedY,
expectedW,
expectedH,
COORDTYPE_SCREEN_RELATIVE
);
input.getCharacterExtents(
nsIAccessibleText.TEXT_OFFSET_CARET,
magicX,
magicY,
magicW,
magicH,
COORDTYPE_SCREEN_RELATIVE
);
Assert.deepEqual(
[magicX.value, magicY.value, magicW.value, magicH.value],
[expectedX.value, expectedY.value, expectedW.value, expectedH.value],
"GetCharacterExtents correct with TEXT_OFFSET_CARET"
);
input.getRangeExtents(
1,
3,
expectedX,
expectedY,
expectedW,
expectedH,
COORDTYPE_SCREEN_RELATIVE
);
input.getRangeExtents(
nsIAccessibleText.TEXT_OFFSET_CARET,
nsIAccessibleText.TEXT_OFFSET_END_OF_TEXT,
magicX,
magicY,
magicW,
magicH,
COORDTYPE_SCREEN_RELATIVE
);
Assert.deepEqual(
[magicX.value, magicY.value, magicW.value, magicH.value],
[expectedX.value, expectedY.value, expectedW.value, expectedH.value],
"GetRangeExtents correct with TEXT_OFFSET_CARET/END_OF_TEXT"
);
},
{ chrome: true, topLevel: !isWinNoCache, remoteIframe: !isWinNoCache }
);