|
11 | 11 | #include "TextLeafRange.h"
|
12 | 12 | #include "UiaTextRange.h"
|
13 | 13 |
|
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 | +} |
16 | 36 |
|
17 | 37 | // UiaText
|
18 | 38 |
|
@@ -40,17 +60,7 @@ UiaText::GetSelection(__RPC__deref_out_opt SAFEARRAY** aRetVal) {
|
40 | 60 | ranges.EmplaceBack(caret, caret);
|
41 | 61 | }
|
42 | 62 | }
|
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); |
54 | 64 | return S_OK;
|
55 | 65 | }
|
56 | 66 |
|
@@ -146,3 +156,5 @@ UiaText::get_SupportedTextSelection(
|
146 | 156 | }
|
147 | 157 | return S_OK;
|
148 | 158 | }
|
| 159 | + |
| 160 | +} // namespace mozilla::a11y |
0 commit comments