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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/* -*- 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 "nsCoreUtils.h"
#include "nsAttrValue.h"
#include "nsIAccessibleTypes.h"
#include "mozilla/dom/Document.h"
#include "nsAccUtils.h"
#include "nsRange.h"
#include "nsXULElement.h"
#include "nsIDocShell.h"
#include "nsIObserverService.h"
#include "nsPresContext.h"
#include "nsISelectionController.h"
#include "nsISimpleEnumerator.h"
#include "mozilla/dom/TouchEvent.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/EventListenerManager.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/ScrollContainerFrame.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/TouchEvents.h"
#include "nsGkAtoms.h"
#include "AnchorPositioningUtils.h"
#include "nsComponentManagerUtils.h"
#include "XULTreeElement.h"
#include "nsIContentInlines.h"
#include "nsTreeColumns.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ElementInternals.h"
#include "mozilla/dom/HTMLLabelElement.h"
#include "mozilla/dom/MouseEventBinding.h"
#include "mozilla/dom/Selection.h"
using namespace mozilla;
using mozilla::dom::DOMRect;
using mozilla::dom::Element;
using mozilla::dom::Selection;
using mozilla::dom::XULTreeElement;
using mozilla::a11y::nsAccUtils;
////////////////////////////////////////////////////////////////////////////////
// nsCoreUtils
////////////////////////////////////////////////////////////////////////////////
bool nsCoreUtils::IsLabelWithControl(nsIContent* aContent) {
dom::HTMLLabelElement* label = dom::HTMLLabelElement::FromNode(aContent);
if (label && label->GetControl()) return true;
return false;
}
bool nsCoreUtils::HasClickListener(nsIContent* aContent) {
NS_ENSURE_TRUE(aContent, false);
EventListenerManager* listenerManager =
aContent->GetExistingListenerManager();
return listenerManager &&
(listenerManager->HasListenersFor(nsGkAtoms::onclick) ||
listenerManager->HasListenersFor(nsGkAtoms::onmousedown) ||
listenerManager->HasListenersFor(nsGkAtoms::onmouseup));
}
void nsCoreUtils::DispatchClickEvent(XULTreeElement* aTree, int32_t aRowIndex,
nsTreeColumn* aColumn,
const nsAString& aPseudoElt) {
RefPtr<dom::Element> tcElm = aTree->GetTreeBody();
if (!tcElm) return;
Document* document = tcElm->GetUncomposedDoc();
if (!document) return;
RefPtr<PresShell> presShell = document->GetPresShell();
if (!presShell) {
return;
}
// Ensure row is visible.
aTree->EnsureRowIsVisible(aRowIndex);
// Calculate x and y coordinates.
nsresult rv;
nsIntRect rect =
aTree->GetCoordsForCellItem(aRowIndex, aColumn, aPseudoElt, rv);
if (NS_FAILED(rv)) {
return;
}
RefPtr<DOMRect> treeBodyRect = tcElm->GetBoundingClientRect();
int32_t tcX = (int32_t)treeBodyRect->X();
int32_t tcY = (int32_t)treeBodyRect->Y();
// Dispatch mouse events.
AutoWeakFrame tcFrame = tcElm->GetPrimaryFrame();
nsIFrame* rootFrame = presShell->GetRootFrame();
nsPoint offset;
nsCOMPtr<nsIWidget> rootWidget = rootFrame->GetNearestWidget(offset);
RefPtr<nsPresContext> presContext = presShell->GetPresContext();
int32_t cnvdX = presContext->CSSPixelsToDevPixels(tcX + int32_t(rect.x) + 1) +
presContext->AppUnitsToDevPixels(offset.x);
int32_t cnvdY = presContext->CSSPixelsToDevPixels(tcY + int32_t(rect.y) + 1) +
presContext->AppUnitsToDevPixels(offset.y);
// This isn't needed once bug 1924790 is fixed.
tcElm->OwnerDoc()->NotifyUserGestureActivation();
// XUL is just desktop, so there is no real reason for senfing touch events.
DispatchMouseEvent(eMouseDown, cnvdX, cnvdY, tcElm, tcFrame, presShell,
rootWidget);
DispatchMouseEvent(eMouseUp, cnvdX, cnvdY, tcElm, tcFrame, presShell,
rootWidget);
}
void nsCoreUtils::DispatchMouseEvent(EventMessage aMessage, int32_t aX,
int32_t aY, nsIContent* aContent,
nsIFrame* aFrame, PresShell* aPresShell,
nsIWidget* aRootWidget) {
MOZ_ASSERT(!IsPointerEventMessage(aMessage));
WidgetMouseEvent event(true, aMessage, aRootWidget, WidgetMouseEvent::eReal);
event.mRefPoint = LayoutDeviceIntPoint(aX, aY);
event.mClickCount = 1;
event.mButton = MouseButton::ePrimary;
event.mInputSource = dom::MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
nsEventStatus status = nsEventStatus_eIgnore;
aPresShell->HandleEventWithTarget(&event, aFrame, aContent, &status);
}
void nsCoreUtils::DispatchTouchEvent(EventMessage aMessage, int32_t aX,
int32_t aY, nsIContent* aContent,
nsIFrame* aFrame, PresShell* aPresShell,
nsIWidget* aRootWidget) {
nsIDocShell* docShell = nullptr;
if (aPresShell->GetDocument()) {
docShell = aPresShell->GetDocument()->GetDocShell();
}
if (!dom::TouchEvent::PrefEnabled(docShell)) {
return;
}
WidgetTouchEvent event(true, aMessage, aRootWidget);
// XXX: Touch has an identifier of -1 to hint that it is synthesized.
RefPtr<dom::Touch> t = new dom::Touch(-1, LayoutDeviceIntPoint(aX, aY),
LayoutDeviceIntPoint(1, 1), 0.0f, 1.0f);
t->SetTouchTarget(aContent);
event.mTouches.AppendElement(t);
nsEventStatus status = nsEventStatus_eIgnore;
aPresShell->HandleEventWithTarget(&event, aFrame, aContent, &status);
}
uint32_t nsCoreUtils::GetAccessKeyFor(nsIContent* aContent) {
// Accesskeys are registered by @accesskey attribute only. At first check
// whether it is presented on the given element to avoid the slow
// EventStateManager::GetRegisteredAccessKey() method.
if (!aContent->IsElement() || !aContent->AsElement()->HasAttr(
kNameSpaceID_None, nsGkAtoms::accesskey)) {
return 0;
}
nsPresContext* presContext = aContent->OwnerDoc()->GetPresContext();
if (!presContext) return 0;
EventStateManager* esm = presContext->EventStateManager();
if (!esm) return 0;
return esm->GetRegisteredAccessKey(aContent->AsElement());
}
nsIContent* nsCoreUtils::GetDOMElementFor(nsIContent* aContent) {
if (aContent->IsElement()) return aContent;
if (aContent->IsText()) return aContent->GetFlattenedTreeParent();
return nullptr;
}
nsINode* nsCoreUtils::GetDOMNodeFromDOMPoint(nsINode* aNode, uint32_t aOffset) {
if (aNode && aNode->IsElement()) {
uint32_t childCount = aNode->GetChildCount();
NS_ASSERTION(aOffset <= childCount, "Wrong offset of the DOM point!");
// The offset can be after last child of container node that means DOM point
// is placed immediately after the last child. In this case use the DOM node
// from the given DOM point is used as result node.
if (aOffset != childCount) return aNode->GetChildAt_Deprecated(aOffset);
}
return aNode;
}
bool nsCoreUtils::IsAncestorOf(nsINode* aPossibleAncestorNode,
nsINode* aPossibleDescendantNode,
nsINode* aRootNode) {
NS_ENSURE_TRUE(aPossibleAncestorNode && aPossibleDescendantNode, false);
nsINode* parentNode = aPossibleDescendantNode;
while ((parentNode = parentNode->GetParentNode()) &&
parentNode != aRootNode) {
if (parentNode == aPossibleAncestorNode) return true;
}
return false;
}
nsresult nsCoreUtils::ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange,
uint32_t aScrollType) {
ScrollAxis vertical, horizontal;
ConvertScrollTypeToPercents(aScrollType, &vertical, &horizontal);
return ScrollSubstringTo(aFrame, aRange, vertical, horizontal);
}
nsresult nsCoreUtils::ScrollSubstringTo(nsIFrame* aFrame, nsRange* aRange,
ScrollAxis aVertical,
ScrollAxis aHorizontal) {
if (!aFrame || !aRange) {
return NS_ERROR_FAILURE;
}
const RefPtr<dom::Selection> selection = [&]() -> dom::Selection* {
nsISelectionController* const selCon = aFrame->GetSelectionController();
NS_ENSURE_TRUE(selCon, nullptr);
return selCon->GetSelection(
nsISelectionController::SELECTION_ACCESSIBILITY);
}();
if (MOZ_UNLIKELY(!selection)) {
return NS_ERROR_FAILURE;
}
selection->RemoveAllRanges(IgnoreErrors());
selection->AddRangeAndSelectFramesAndNotifyListeners(*aRange, IgnoreErrors());
selection->ScrollIntoView(nsISelectionController::SELECTION_ANCHOR_REGION,
aVertical, aHorizontal, ScrollFlags::None,
SelectionScrollMode::SyncNoFlush);
selection->CollapseToStart(IgnoreErrors());
return NS_OK;
}
void nsCoreUtils::ScrollFrameToPoint(nsIFrame* aScrollContainerFrame,
nsIFrame* aFrame,
const LayoutDeviceIntPoint& aPoint) {
ScrollContainerFrame* scrollContainerFrame =
do_QueryFrame(aScrollContainerFrame);
if (!scrollContainerFrame) {
return;
}
nsPoint point = LayoutDeviceIntPoint::ToAppUnits(
aPoint, aFrame->PresContext()->AppUnitsPerDevPixel());
nsRect frameRect = aFrame->GetScreenRectInAppUnits();
nsPoint deltaPoint = point - frameRect.TopLeft();
nsPoint scrollPoint = scrollContainerFrame->GetScrollPosition();
scrollPoint -= deltaPoint;
scrollContainerFrame->ScrollTo(scrollPoint, ScrollMode::Instant);
}
void nsCoreUtils::ConvertScrollTypeToPercents(uint32_t aScrollType,
ScrollAxis* aVertical,
ScrollAxis* aHorizontal) {
WhereToScroll whereY, whereX;
WhenToScroll whenY, whenX;
switch (aScrollType) {
case nsIAccessibleScrollType::SCROLL_TYPE_TOP_LEFT:
whereY = WhereToScroll::Start;
whenY = WhenToScroll::Always;
whereX = WhereToScroll::Start;
whenX = WhenToScroll::Always;
break;
case nsIAccessibleScrollType::SCROLL_TYPE_BOTTOM_RIGHT:
whereY = WhereToScroll::End;
whenY = WhenToScroll::Always;
whereX = WhereToScroll::End;
whenX = WhenToScroll::Always;
break;
case nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE:
whereY = WhereToScroll::Start;
whenY = WhenToScroll::Always;
whereX = WhereToScroll::Nearest;
whenX = WhenToScroll::IfNotFullyVisible;
break;
case nsIAccessibleScrollType::SCROLL_TYPE_BOTTOM_EDGE:
whereY = WhereToScroll::End;
whenY = WhenToScroll::Always;
whereX = WhereToScroll::Nearest;
whenX = WhenToScroll::IfNotFullyVisible;
break;
case nsIAccessibleScrollType::SCROLL_TYPE_LEFT_EDGE:
whereY = WhereToScroll::Nearest;
whenY = WhenToScroll::IfNotFullyVisible;
whereX = WhereToScroll::Start;
whenX = WhenToScroll::Always;
break;
case nsIAccessibleScrollType::SCROLL_TYPE_RIGHT_EDGE:
whereY = WhereToScroll::Nearest;
whenY = WhenToScroll::IfNotFullyVisible;
whereX = WhereToScroll::End;
whenX = WhenToScroll::Always;
break;
default:
whereY = WhereToScroll::Center;
whenY = WhenToScroll::IfNotFullyVisible;
whereX = WhereToScroll::Center;
whenX = WhenToScroll::IfNotFullyVisible;
}
*aVertical = ScrollAxis(whereY, whenY);
*aHorizontal = ScrollAxis(whereX, whenX);
}
already_AddRefed<nsIDocShell> nsCoreUtils::GetDocShellFor(nsINode* aNode) {
if (!aNode) return nullptr;
nsCOMPtr<nsIDocShell> docShell = aNode->OwnerDoc()->GetDocShell();
return docShell.forget();
}
bool nsCoreUtils::IsRootDocument(Document* aDocument) {
nsCOMPtr<nsIDocShellTreeItem> docShellTreeItem = aDocument->GetDocShell();
NS_ASSERTION(docShellTreeItem, "No document shell for document!");
nsCOMPtr<nsIDocShellTreeItem> parentTreeItem;
docShellTreeItem->GetInProcessParent(getter_AddRefs(parentTreeItem));
return !parentTreeItem;
}
bool nsCoreUtils::IsTopLevelContentDocInProcess(Document* aDocumentNode) {
mozilla::dom::BrowsingContext* bc = aDocumentNode->GetBrowsingContext();
return bc->IsContent() && (
// Tab document.
bc->IsTop() ||
// Out-of-process iframe.
!bc->GetParent()->IsInProcess());
}
bool nsCoreUtils::IsErrorPage(Document* aDocument) {
nsIURI* uri = aDocument->GetDocumentURI();
if (!uri->SchemeIs("about")) {
return false;
}
nsAutoCString path;
uri->GetPathQueryRef(path);
constexpr auto neterror = "neterror"_ns;
constexpr auto certerror = "certerror"_ns;
return StringBeginsWith(path, neterror) || StringBeginsWith(path, certerror);
}
PresShell* nsCoreUtils::GetPresShellFor(nsINode* aNode) {
return aNode->OwnerDoc()->GetPresShell();
}
bool nsCoreUtils::GetID(nsIContent* aContent, nsAString& aID) {
return aContent->IsElement() &&
aContent->AsElement()->GetAttr(nsGkAtoms::id, aID);
}
bool nsCoreUtils::GetUIntAttr(nsIContent* aContent, nsAtom* aAttr,
int32_t* aUInt) {
if (!aContent->IsElement()) {
return false;
}
return GetUIntAttrValue(nsAccUtils::GetARIAAttr(aContent->AsElement(), aAttr),
aUInt);
}
bool nsCoreUtils::GetUIntAttrValue(const nsAttrValue* aVal, int32_t* aUInt) {
if (!aVal) {
return false;
}
nsAutoString value;
aVal->ToString(value);
if (!value.IsEmpty()) {
nsresult error = NS_OK;
int32_t integer = value.ToInteger(&error);
if (NS_SUCCEEDED(error) && integer > 0) {
*aUInt = integer;
return true;
}
}
return false;
}
void nsCoreUtils::GetLanguageFor(nsIContent* aContent, nsIContent* aRootContent,
nsAString& aLanguage) {
aLanguage.Truncate();
nsIContent* walkUp = aContent;
while (walkUp && walkUp != aRootContent &&
(!walkUp->IsElement() ||
!walkUp->AsElement()->GetAttr(nsGkAtoms::lang, aLanguage))) {
walkUp = walkUp->GetParent();
}
}
XULTreeElement* nsCoreUtils::GetTree(nsIContent* aContent) {
// Find DOMNode's parents recursively until reach the <tree> tag
nsIContent* currentContent = aContent;
while (currentContent) {
if (currentContent->NodeInfo()->Equals(nsGkAtoms::tree, kNameSpaceID_XUL)) {
return XULTreeElement::FromNode(currentContent);
}
currentContent = currentContent->GetFlattenedTreeParent();
}
return nullptr;
}
already_AddRefed<nsTreeColumn> nsCoreUtils::GetFirstSensibleColumn(
XULTreeElement* aTree, FlushType aFlushType) {
if (!aTree) {
return nullptr;
}
RefPtr<nsTreeColumns> cols = aTree->GetColumns(aFlushType);
if (!cols) {
return nullptr;
}
RefPtr<nsTreeColumn> column = cols->GetFirstColumn();
if (column && IsColumnHidden(column)) return GetNextSensibleColumn(column);
return column.forget();
}
uint32_t nsCoreUtils::GetSensibleColumnCount(XULTreeElement* aTree) {
uint32_t count = 0;
if (!aTree) {
return count;
}
RefPtr<nsTreeColumns> cols = aTree->GetColumns();
if (!cols) {
return count;
}
nsTreeColumn* column = cols->GetFirstColumn();
while (column) {
if (!IsColumnHidden(column)) count++;
column = column->GetNext();
}
return count;
}
already_AddRefed<nsTreeColumn> nsCoreUtils::GetSensibleColumnAt(
XULTreeElement* aTree, uint32_t aIndex) {
if (!aTree) {
return nullptr;
}
uint32_t idx = aIndex;
nsCOMPtr<nsTreeColumn> column = GetFirstSensibleColumn(aTree);
while (column) {
if (idx == 0) return column.forget();
idx--;
column = GetNextSensibleColumn(column);
}
return nullptr;
}
already_AddRefed<nsTreeColumn> nsCoreUtils::GetNextSensibleColumn(
nsTreeColumn* aColumn) {
if (!aColumn) {
return nullptr;
}
RefPtr<nsTreeColumn> nextColumn = aColumn->GetNext();
while (nextColumn && IsColumnHidden(nextColumn)) {
nextColumn = nextColumn->GetNext();
}
return nextColumn.forget();
}
already_AddRefed<nsTreeColumn> nsCoreUtils::GetPreviousSensibleColumn(
nsTreeColumn* aColumn) {
if (!aColumn) {
return nullptr;
}
RefPtr<nsTreeColumn> prevColumn = aColumn->GetPrevious();
while (prevColumn && IsColumnHidden(prevColumn)) {
prevColumn = prevColumn->GetPrevious();
}
return prevColumn.forget();
}
bool nsCoreUtils::IsColumnHidden(nsTreeColumn* aColumn) {
if (!aColumn) {
return false;
}
Element* element = aColumn->Element();
return element->GetBoolAttr(nsGkAtoms::hidden);
}
void nsCoreUtils::ScrollTo(PresShell* aPresShell, nsIContent* aContent,
uint32_t aScrollType) {
ScrollAxis vertical, horizontal;
ConvertScrollTypeToPercents(aScrollType, &vertical, &horizontal);
aPresShell->ScrollContentIntoView(aContent, vertical, horizontal,
ScrollFlags::ScrollOverflowHidden);
}
bool nsCoreUtils::IsHTMLTableHeader(nsIContent* aContent) {
return aContent->NodeInfo()->Equals(nsGkAtoms::th) ||
(aContent->IsElement() &&
aContent->AsElement()->HasAttr(nsGkAtoms::scope));
}
bool nsCoreUtils::IsWhitespaceString(const nsAString& aString) {
nsAString::const_char_iterator iterBegin, iterEnd;
aString.BeginReading(iterBegin);
aString.EndReading(iterEnd);
while (iterBegin != iterEnd && IsWhitespace(*iterBegin)) ++iterBegin;
return iterBegin == iterEnd;
}
bool nsCoreUtils::AccEventObserversExist() {
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
NS_ENSURE_TRUE(obsService, false);
nsCOMPtr<nsISimpleEnumerator> observers;
obsService->EnumerateObservers(NS_ACCESSIBLE_EVENT_TOPIC,
getter_AddRefs(observers));
NS_ENSURE_TRUE(observers, false);
bool hasObservers = false;
observers->HasMoreElements(&hasObservers);
return hasObservers;
}
void nsCoreUtils::DispatchAccEvent(RefPtr<nsIAccessibleEvent> event) {
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
NS_ENSURE_TRUE_VOID(obsService);
obsService->NotifyObservers(event, NS_ACCESSIBLE_EVENT_TOPIC, nullptr);
}
bool nsCoreUtils::IsDisplayContents(nsIContent* aContent) {
auto* element = Element::FromNodeOrNull(aContent);
return element && element->IsDisplayContents();
}
bool nsCoreUtils::CanCreateAccessibleWithoutFrame(nsIContent* aContent) {
auto* element = Element::FromNodeOrNull(aContent);
if (!element) {
return false;
}
if (!element->HasServoData() || Servo_Element_IsDisplayNone(element)) {
// Out of the flat tree or in a display: none subtree.
return false;
}
// If we aren't display: contents or option/optgroup we can't create an
// accessible without frame. Our select combobox code relies on the latter.
if (!element->IsDisplayContents() &&
!element->IsAnyOfHTMLElements(nsGkAtoms::option, nsGkAtoms::optgroup)) {
return false;
}
// Even if we're display: contents or optgroups, we might not be able to
// create an accessible if we're in a content-visibility: hidden subtree.
//
// To check that, find the closest ancestor element with a frame.
for (nsINode* ancestor = element->GetFlattenedTreeParentNode();
ancestor && ancestor->IsContent();
ancestor = ancestor->GetFlattenedTreeParentNode()) {
if (nsIFrame* f = ancestor->AsContent()->GetPrimaryFrame()) {
if (f->HidesContent(nsIFrame::IncludeContentVisibility::Hidden) ||
f->IsHiddenByContentVisibilityOnAnyAncestor(
nsIFrame::IncludeContentVisibility::Hidden)) {
return false;
}
break;
}
}
return true;
}
bool nsCoreUtils::IsDocumentVisibleConsideringInProcessAncestors(
const Document* aDocument) {
const Document* parent = aDocument;
do {
if (!parent->IsVisible()) {
return false;
}
} while ((parent = parent->GetInProcessParentDocument()));
return true;
}
bool nsCoreUtils::IsDescendantOfAnyShadowIncludingAncestor(
nsINode* aDescendant, nsINode* aStartAncestor) {
const nsINode* descRoot = aDescendant->SubtreeRoot();
nsINode* ancRoot = aStartAncestor->SubtreeRoot();
for (;;) {
if (ancRoot == descRoot) {
return true;
}
auto* shadow = mozilla::dom::ShadowRoot::FromNode(ancRoot);
if (!shadow || !shadow->GetHost()) {
break;
}
ancRoot = shadow->GetHost()->SubtreeRoot();
}
return false;
}
Element* nsCoreUtils::GetAriaActiveDescendantElement(Element* aElement) {
if (Element* activeDescendant = aElement->GetAriaActiveDescendantElement()) {
return activeDescendant;
}
if (auto* element = nsGenericHTMLElement::FromNode(aElement)) {
if (auto* internals = element->GetInternals()) {
return internals->GetAriaActiveDescendantElement();
}
}
return nullptr;
}
bool nsCoreUtils::IsTrimmedWhitespaceBeforeHardLineBreak(nsIFrame* aFrame) {
if (!aFrame->GetRect().IsEmpty() ||
!aFrame->HasAnyStateBits(TEXT_END_OF_LINE)) {
return false;
}
// Normally, accessibility calls nsIFrame::GetRenderedText with
// TrailingWhitespace::NoTrim. Using TrailingWhitespace::Trim instead trims 0
// width whitespace before a hard line break, resulting in an empty string if
// that is all the frame contains. Note that TrailingWhitespace::Trim does
// *not* trim whitespace before a soft line break (wrapped line).
nsIFrame::RenderedText text = aFrame->GetRenderedText(
0, UINT32_MAX, nsIFrame::TextOffsetType::OffsetsInContentText,
nsIFrame::TrailingWhitespace::Trim);
return text.mString.IsEmpty();
}
const nsIFrame* nsCoreUtils::GetAnchorForPositionedFrame(
const PresShell* aPresShell, const nsIFrame* aPositionedFrame) {
if (!aPositionedFrame ||
!aPositionedFrame->Style()->HasAnchorPosReference()) {
return nullptr;
}
const nsAtom* anchorName = nullptr;
AnchorPosReferenceData* referencedAnchors =
aPositionedFrame->GetProperty(nsIFrame::AnchorPosReferences());
if (!referencedAnchors) {
return nullptr;
}
for (const auto& entry : *referencedAnchors) {
if (entry.GetData().isNothing()) {
continue;
}
if (anchorName && entry.GetKey() != anchorName) {
// Multiple anchors referenced.
return nullptr;
}
anchorName = entry.GetKey();
}
return anchorName
? aPresShell->GetAnchorPosAnchor(anchorName, aPositionedFrame)
: nullptr;
}
nsIFrame* nsCoreUtils::GetPositionedFrameForAnchor(
const PresShell* aPresShell, const nsIFrame* aAnchorFrame) {
if (!aAnchorFrame) {
return nullptr;
}
nsIFrame* positionedFrame = nullptr;
const auto* styleDisp = aAnchorFrame->StyleDisplay();
if (styleDisp->HasAnchorName()) {
for (auto& name : styleDisp->mAnchorName.AsSpan()) {
for (nsIFrame* frame : aPresShell->GetAnchorPosPositioned()) {
// Bug 1990069: We need to iterate over all positioned frames in doc and
// check their referenced anchors because we don't store reverse mapping
// from anchor to positioned frame.
const auto* referencedAnchors =
frame->GetProperty(nsIFrame::AnchorPosReferences());
if (!referencedAnchors) {
// Depending on where we are in the reflow, this property may or may
// not be set. If it isn't set, a future reflow will set it, so we can
// just skip this frame for now.
continue;
}
const auto* data = referencedAnchors->Lookup(name.AsAtom());
if (data && *data && data->ref().mOffsetData) {
if (aAnchorFrame ==
aPresShell->GetAnchorPosAnchor(name.AsAtom(), frame)) {
if (positionedFrame) {
// Multiple positioned frames reference this anchor.
return nullptr;
}
positionedFrame = frame;
}
}
}
}
}
return positionedFrame;
}
|