1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=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 "ia2Accessible.h"
#include "ia2AccessibleHypertext.h"
#include "ia2AccessibleText.h"
#include "AccessibleText_i.c"
#include "mozilla/a11y/Compatibility.h"
#include "mozilla/a11y/HyperTextAccessibleBase.h"
#include "mozilla/ClearOnShutdown.h"
using namespace mozilla::a11y;
HyperTextAccessibleBase* ia2AccessibleText::sLastTextChangeAcc = nullptr;
mozilla::StaticAutoPtr<nsString> ia2AccessibleText::sLastTextChangeString;
uint32_t ia2AccessibleText::sLastTextChangeStart = 0;
uint32_t ia2AccessibleText::sLastTextChangeEnd = 0;
bool ia2AccessibleText::sLastTextChangeWasInsert = false;
HyperTextAccessibleBase* ia2AccessibleText::TextAcc() {
auto hyp = static_cast<ia2AccessibleHypertext*>(this);
Accessible* acc = hyp->Acc();
return acc ? acc->AsHyperTextBase() : nullptr;
}
// IAccessibleText
STDMETHODIMP
ia2AccessibleText::addSelection(long aStartOffset, long aEndOffset) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
return textAcc->AddToSelection(aStartOffset, aEndOffset) ? S_OK
: E_INVALIDARG;
}
STDMETHODIMP
ia2AccessibleText::get_attributes(long aOffset, long* aStartOffset,
long* aEndOffset, BSTR* aTextAttributes) {
if (!aStartOffset || !aEndOffset || !aTextAttributes) return E_INVALIDARG;
*aStartOffset = 0;
*aEndOffset = 0;
*aTextAttributes = nullptr;
int32_t startOffset = 0, endOffset = 0;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
RefPtr<AccAttributes> attributes =
textAcc->TextAttributes(true, aOffset, &startOffset, &endOffset);
HRESULT hr =
ia2Accessible::ConvertToIA2Attributes(attributes, aTextAttributes);
if (FAILED(hr)) return hr;
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_caretOffset(long* aOffset) {
if (!aOffset) return E_INVALIDARG;
*aOffset = -1;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
*aOffset = textAcc->CaretOffset();
return *aOffset != -1 ? S_OK : S_FALSE;
}
STDMETHODIMP
ia2AccessibleText::get_characterExtents(long aOffset,
enum IA2CoordinateType aCoordType,
long* aX, long* aY, long* aWidth,
long* aHeight) {
if (!aX || !aY || !aWidth || !aHeight) return E_INVALIDARG;
*aX = *aY = *aWidth = *aHeight = 0;
uint32_t geckoCoordType =
(aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE)
? nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE
: nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
LayoutDeviceIntRect rect;
auto textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
rect = textAcc->CharBounds(aOffset, geckoCoordType);
// Can't use GetRect() because of long vs. int32_t mismatch
*aX = rect.X();
*aY = rect.Y();
*aWidth = rect.Width();
*aHeight = rect.Height();
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_nSelections(long* aNSelections) {
if (!aNSelections) return E_INVALIDARG;
*aNSelections = 0;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
*aNSelections = textAcc->SelectionCount();
if (*aNSelections == 0 &&
(Compatibility::A11ySuppressionReasons() &
SuppressionReasons::Clipboard) &&
static_cast<ia2AccessibleHypertext*>(this)->Acc()->IsDoc()) {
// Bug 1798098: Windows Suggested Actions (introduced in Windows 11
// 22H2) might walk the document a11y tree using UIA whenever anything
// is copied to the clipboard. This causes an unacceptable hang. It walks
// using IAccessibleText/IAccessibleHyperText if the document reports no
// selection, so we lie here and say that there is a selection even though
// there isn't. It will subsequently call get_selection, which will fail,
// but this hack here seems to be enough to avoid further text calls.
*aNSelections = 1;
}
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_offsetAtPoint(long aX, long aY,
enum IA2CoordinateType aCoordType,
long* aOffset) {
if (!aOffset) return E_INVALIDARG;
*aOffset = 0;
uint32_t geckoCoordType =
(aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE)
? nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE
: nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
*aOffset = textAcc->OffsetAtPoint(aX, aY, geckoCoordType);
return *aOffset == -1 ? S_FALSE : S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_selection(long aSelectionIndex, long* aStartOffset,
long* aEndOffset) {
if (!aStartOffset || !aEndOffset) return E_INVALIDARG;
*aStartOffset = *aEndOffset = 0;
int32_t startOffset = 0, endOffset = 0;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->SelectionBoundsAt(aSelectionIndex, &startOffset, &endOffset)) {
return E_INVALIDARG;
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_text(long aStartOffset, long aEndOffset, BSTR* aText) {
if (!aText) return E_INVALIDARG;
*aText = nullptr;
nsAutoString text;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidRange(aStartOffset, aEndOffset)) {
return E_INVALIDARG;
}
textAcc->TextSubstring(aStartOffset, aEndOffset, text);
if (text.IsEmpty()) return S_FALSE;
*aText = ::SysAllocStringLen(text.get(), text.Length());
return *aText ? S_OK : E_OUTOFMEMORY;
}
STDMETHODIMP
ia2AccessibleText::get_textBeforeOffset(long aOffset,
enum IA2TextBoundaryType aBoundaryType,
long* aStartOffset, long* aEndOffset,
BSTR* aText) {
if (!aStartOffset || !aEndOffset || !aText) return E_INVALIDARG;
*aStartOffset = *aEndOffset = 0;
*aText = nullptr;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidOffset(aOffset)) return E_INVALIDARG;
nsAutoString text;
int32_t startOffset = 0, endOffset = 0;
if (aBoundaryType == IA2_TEXT_BOUNDARY_ALL) {
startOffset = 0;
endOffset = textAcc->CharacterCount();
textAcc->TextSubstring(startOffset, endOffset, text);
} else {
AccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
if (boundaryType == -1) return S_FALSE;
textAcc->TextBeforeOffset(aOffset, boundaryType, &startOffset, &endOffset,
text);
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
if (text.IsEmpty()) return S_FALSE;
*aText = ::SysAllocStringLen(text.get(), text.Length());
return *aText ? S_OK : E_OUTOFMEMORY;
}
STDMETHODIMP
ia2AccessibleText::get_textAfterOffset(long aOffset,
enum IA2TextBoundaryType aBoundaryType,
long* aStartOffset, long* aEndOffset,
BSTR* aText) {
if (!aStartOffset || !aEndOffset || !aText) return E_INVALIDARG;
*aStartOffset = 0;
*aEndOffset = 0;
*aText = nullptr;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidOffset(aOffset)) return E_INVALIDARG;
nsAutoString text;
int32_t startOffset = 0, endOffset = 0;
if (aBoundaryType == IA2_TEXT_BOUNDARY_ALL) {
startOffset = 0;
endOffset = textAcc->CharacterCount();
textAcc->TextSubstring(startOffset, endOffset, text);
} else {
AccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
if (boundaryType == -1) return S_FALSE;
textAcc->TextAfterOffset(aOffset, boundaryType, &startOffset, &endOffset,
text);
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
if (text.IsEmpty()) return S_FALSE;
*aText = ::SysAllocStringLen(text.get(), text.Length());
return *aText ? S_OK : E_OUTOFMEMORY;
}
STDMETHODIMP
ia2AccessibleText::get_textAtOffset(long aOffset,
enum IA2TextBoundaryType aBoundaryType,
long* aStartOffset, long* aEndOffset,
BSTR* aText) {
if (!aStartOffset || !aEndOffset || !aText) return E_INVALIDARG;
*aStartOffset = *aEndOffset = 0;
*aText = nullptr;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) return CO_E_OBJNOTCONNECTED;
if (!textAcc->IsValidOffset(aOffset)) return E_INVALIDARG;
nsAutoString text;
int32_t startOffset = 0, endOffset = 0;
if (aBoundaryType == IA2_TEXT_BOUNDARY_ALL) {
startOffset = 0;
endOffset = textAcc->CharacterCount();
textAcc->TextSubstring(startOffset, endOffset, text);
} else {
AccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
if (boundaryType == -1) return S_FALSE;
textAcc->TextAtOffset(aOffset, boundaryType, &startOffset, &endOffset,
text);
}
*aStartOffset = startOffset;
*aEndOffset = endOffset;
if (text.IsEmpty()) return S_FALSE;
*aText = ::SysAllocStringLen(text.get(), text.Length());
return *aText ? S_OK : E_OUTOFMEMORY;
}
STDMETHODIMP
ia2AccessibleText::removeSelection(long aSelectionIndex) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
return textAcc->RemoveFromSelection(aSelectionIndex) ? S_OK : E_INVALIDARG;
}
STDMETHODIMP
ia2AccessibleText::setCaretOffset(long aOffset) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidOffset(aOffset)) return E_INVALIDARG;
textAcc->SetCaretOffset(aOffset);
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::setSelection(long aSelectionIndex, long aStartOffset,
long aEndOffset) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
return textAcc->SetSelectionBoundsAt(aSelectionIndex, aStartOffset,
aEndOffset)
? S_OK
: E_INVALIDARG;
}
STDMETHODIMP
ia2AccessibleText::get_nCharacters(long* aNCharacters) {
if (!aNCharacters) return E_INVALIDARG;
*aNCharacters = 0;
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) return CO_E_OBJNOTCONNECTED;
*aNCharacters = textAcc->CharacterCount();
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::scrollSubstringTo(long aStartIndex, long aEndIndex,
enum IA2ScrollType aScrollType) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidRange(aStartIndex, aEndIndex)) return E_INVALIDARG;
textAcc->ScrollSubstringTo(aStartIndex, aEndIndex, aScrollType);
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::scrollSubstringToPoint(long aStartIndex, long aEndIndex,
enum IA2CoordinateType aCoordType,
long aX, long aY) {
HyperTextAccessibleBase* textAcc = TextAcc();
if (!textAcc) {
return CO_E_OBJNOTCONNECTED;
}
if (!textAcc->IsValidRange(aStartIndex, aEndIndex)) {
return E_INVALIDARG;
}
uint32_t geckoCoordType =
(aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE)
? nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE
: nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
textAcc->ScrollSubstringToPoint(aStartIndex, aEndIndex, geckoCoordType, aX,
aY);
return S_OK;
}
STDMETHODIMP
ia2AccessibleText::get_newText(IA2TextSegment* aNewText) {
return GetModifiedText(true, aNewText);
}
STDMETHODIMP
ia2AccessibleText::get_oldText(IA2TextSegment* aOldText) {
return GetModifiedText(false, aOldText);
}
// ia2AccessibleText
HRESULT
ia2AccessibleText::GetModifiedText(bool aGetInsertedText,
IA2TextSegment* aText) {
if (!aText) return E_INVALIDARG;
if (!sLastTextChangeAcc) return S_OK;
if (aGetInsertedText != sLastTextChangeWasInsert) return S_OK;
if (sLastTextChangeAcc != TextAcc()) return S_OK;
aText->start = sLastTextChangeStart;
aText->end = sLastTextChangeEnd;
if (sLastTextChangeString->IsEmpty()) return S_FALSE;
aText->text = ::SysAllocStringLen(sLastTextChangeString->get(),
sLastTextChangeString->Length());
return aText->text ? S_OK : E_OUTOFMEMORY;
}
AccessibleTextBoundary ia2AccessibleText::GetGeckoTextBoundary(
enum IA2TextBoundaryType aBoundaryType) {
switch (aBoundaryType) {
case IA2_TEXT_BOUNDARY_CHAR:
return nsIAccessibleText::BOUNDARY_CLUSTER;
case IA2_TEXT_BOUNDARY_WORD:
return nsIAccessibleText::BOUNDARY_WORD_START;
case IA2_TEXT_BOUNDARY_LINE:
return nsIAccessibleText::BOUNDARY_LINE_START;
case IA2_TEXT_BOUNDARY_PARAGRAPH:
return nsIAccessibleText::BOUNDARY_PARAGRAPH;
// case IA2_TEXT_BOUNDARY_SENTENCE:
// XXX: not implemented
default:
return -1;
}
}
void ia2AccessibleText::InitTextChangeData() {
ClearOnShutdown(&sLastTextChangeString);
}
void ia2AccessibleText::UpdateTextChangeData(HyperTextAccessibleBase* aAcc,
bool aInsert,
const nsAString& aStr,
int32_t aStart, uint32_t aLen) {
if (!sLastTextChangeString) sLastTextChangeString = new nsString();
sLastTextChangeAcc = aAcc;
sLastTextChangeStart = aStart;
sLastTextChangeEnd = aStart + aLen;
sLastTextChangeWasInsert = aInsert;
*sLastTextChangeString = aStr;
}
|