Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 2a6f993

Browse files
committed
Bug 1901464 part 2: Refactor UiaText code to convert an array of TextLeafRanges to a SAFEARRAY of UiaTextRanges into a utility function. r=nlapre
This is currently only used by GetSelection, but it will be used by GetVisibleRanges in a subsequent patch. There is a slight functionality change here: if there are no ranges, this returns an empty array. GetSelection is unclear about what should be done here, but the documentation for GetVisibleRanges explicitly states an empty array should be returned, so that's what we do. GetSelection previously left the SAFEARRAY pointer uninitialised in this case, which is definitely nasty and unsafe. Differential Revision: https://phabricator.services.mozilla.com/D236089
1 parent 5b1b8e0 commit 2a6f993

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

accessible/windows/uia/UiaText.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,28 @@
1111
#include "TextLeafRange.h"
1212
#include "UiaTextRange.h"
1313

14-
using namespace mozilla;
15-
using namespace mozilla::a11y;
14+
namespace mozilla::a11y {
15+
16+
// Helpers
17+
18+
static SAFEARRAY* TextLeafRangesToUiaRanges(
19+
const nsTArray<TextLeafRange>& aRanges) {
20+
// The documentation for GetSelection doesn't specify whether we should return
21+
// an empty array or null if there are no ranges to return. However,
22+
// GetVisibleRanges says that we should return an empty array, never null, so
23+
// that's what we do.
24+
// https://learn.microsoft.com/en-us/windows/win32/api/uiautomationcore/nf-uiautomationcore-itextprovider-getvisibleranges
25+
SAFEARRAY* uiaRanges = SafeArrayCreateVector(VT_UNKNOWN, 0, aRanges.Length());
26+
LONG indices[1] = {0};
27+
for (const TextLeafRange& range : aRanges) {
28+
// SafeArrayPutElement calls AddRef on the element, so we use a raw
29+
// pointer here.
30+
UiaTextRange* uiaRange = new UiaTextRange(range);
31+
SafeArrayPutElement(uiaRanges, indices, uiaRange);
32+
++indices[0];
33+
}
34+
return uiaRanges;
35+
}
1636

1737
// UiaText
1838

@@ -40,17 +60,7 @@ UiaText::GetSelection(__RPC__deref_out_opt SAFEARRAY** aRetVal) {
4060
ranges.EmplaceBack(caret, caret);
4161
}
4262
}
43-
if (!ranges.IsEmpty()) {
44-
*aRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, ranges.Length());
45-
LONG indices[1] = {0};
46-
for (TextLeafRange& range : ranges) {
47-
// SafeArrayPutElement calls AddRef on the element, so we use a raw
48-
// pointer here.
49-
UiaTextRange* uiaRange = new UiaTextRange(range);
50-
SafeArrayPutElement(*aRetVal, indices, uiaRange);
51-
++indices[0];
52-
}
53-
}
63+
*aRetVal = TextLeafRangesToUiaRanges(ranges);
5464
return S_OK;
5565
}
5666

@@ -146,3 +156,5 @@ UiaText::get_SupportedTextSelection(
146156
}
147157
return S_OK;
148158
}
159+
160+
} // namespace mozilla::a11y

0 commit comments

Comments
 (0)