michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAccUtils.h" michael@0: michael@0: #include "Accessible-inl.h" michael@0: #include "ARIAMap.h" michael@0: #include "nsAccessibilityService.h" michael@0: #include "nsCoreUtils.h" michael@0: #include "DocAccessible.h" michael@0: #include "HyperTextAccessible.h" michael@0: #include "nsIAccessibleTypes.h" michael@0: #include "Role.h" michael@0: #include "States.h" michael@0: #include "TextLeafAccessible.h" michael@0: michael@0: #include "nsIDOMXULContainerElement.h" michael@0: #include "nsIPersistentProperties2.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::a11y; michael@0: michael@0: void michael@0: nsAccUtils::GetAccAttr(nsIPersistentProperties *aAttributes, michael@0: nsIAtom *aAttrName, nsAString& aAttrValue) michael@0: { michael@0: aAttrValue.Truncate(); michael@0: michael@0: aAttributes->GetStringProperty(nsAtomCString(aAttrName), aAttrValue); michael@0: } michael@0: michael@0: void michael@0: nsAccUtils::SetAccAttr(nsIPersistentProperties *aAttributes, michael@0: nsIAtom *aAttrName, const nsAString& aAttrValue) michael@0: { michael@0: nsAutoString oldValue; michael@0: nsAutoCString attrName; michael@0: michael@0: aAttributes->SetStringProperty(nsAtomCString(aAttrName), aAttrValue, oldValue); michael@0: } michael@0: michael@0: void michael@0: nsAccUtils::SetAccGroupAttrs(nsIPersistentProperties *aAttributes, michael@0: int32_t aLevel, int32_t aSetSize, michael@0: int32_t aPosInSet) michael@0: { michael@0: nsAutoString value; michael@0: michael@0: if (aLevel) { michael@0: value.AppendInt(aLevel); michael@0: SetAccAttr(aAttributes, nsGkAtoms::level, value); michael@0: } michael@0: michael@0: if (aSetSize && aPosInSet) { michael@0: value.Truncate(); michael@0: value.AppendInt(aPosInSet); michael@0: SetAccAttr(aAttributes, nsGkAtoms::posinset, value); michael@0: michael@0: value.Truncate(); michael@0: value.AppendInt(aSetSize); michael@0: SetAccAttr(aAttributes, nsGkAtoms::setsize, value); michael@0: } michael@0: } michael@0: michael@0: int32_t michael@0: nsAccUtils::GetDefaultLevel(Accessible* aAccessible) michael@0: { michael@0: roles::Role role = aAccessible->Role(); michael@0: michael@0: if (role == roles::OUTLINEITEM) michael@0: return 1; michael@0: michael@0: if (role == roles::ROW) { michael@0: Accessible* parent = aAccessible->Parent(); michael@0: // It is a row inside flatten treegrid. Group level is always 1 until it michael@0: // is overriden by aria-level attribute. michael@0: if (parent && parent->Role() == roles::TREE_TABLE) michael@0: return 1; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int32_t michael@0: nsAccUtils::GetARIAOrDefaultLevel(Accessible* aAccessible) michael@0: { michael@0: int32_t level = 0; michael@0: nsCoreUtils::GetUIntAttr(aAccessible->GetContent(), michael@0: nsGkAtoms::aria_level, &level); michael@0: michael@0: if (level != 0) michael@0: return level; michael@0: michael@0: return GetDefaultLevel(aAccessible); michael@0: } michael@0: michael@0: int32_t michael@0: nsAccUtils::GetLevelForXULContainerItem(nsIContent *aContent) michael@0: { michael@0: nsCOMPtr item(do_QueryInterface(aContent)); michael@0: if (!item) michael@0: return 0; michael@0: michael@0: nsCOMPtr container; michael@0: item->GetParentContainer(getter_AddRefs(container)); michael@0: if (!container) michael@0: return 0; michael@0: michael@0: // Get level of the item. michael@0: int32_t level = -1; michael@0: while (container) { michael@0: level++; michael@0: michael@0: nsCOMPtr parentContainer; michael@0: container->GetParentContainer(getter_AddRefs(parentContainer)); michael@0: parentContainer.swap(container); michael@0: } michael@0: michael@0: return level; michael@0: } michael@0: michael@0: void michael@0: nsAccUtils::SetLiveContainerAttributes(nsIPersistentProperties *aAttributes, michael@0: nsIContent *aStartContent, michael@0: nsIContent *aTopContent) michael@0: { michael@0: nsAutoString live, relevant, busy; michael@0: nsIContent *ancestor = aStartContent; michael@0: while (ancestor) { michael@0: michael@0: // container-relevant attribute michael@0: if (relevant.IsEmpty() && michael@0: HasDefinedARIAToken(ancestor, nsGkAtoms::aria_relevant) && michael@0: ancestor->GetAttr(kNameSpaceID_None, nsGkAtoms::aria_relevant, relevant)) michael@0: SetAccAttr(aAttributes, nsGkAtoms::containerRelevant, relevant); michael@0: michael@0: // container-live, and container-live-role attributes michael@0: if (live.IsEmpty()) { michael@0: nsRoleMapEntry* role = aria::GetRoleMap(ancestor); michael@0: if (HasDefinedARIAToken(ancestor, nsGkAtoms::aria_live)) { michael@0: ancestor->GetAttr(kNameSpaceID_None, nsGkAtoms::aria_live, michael@0: live); michael@0: } else if (role) { michael@0: GetLiveAttrValue(role->liveAttRule, live); michael@0: } michael@0: if (!live.IsEmpty()) { michael@0: SetAccAttr(aAttributes, nsGkAtoms::containerLive, live); michael@0: if (role) { michael@0: SetAccAttr(aAttributes, nsGkAtoms::containerLiveRole, michael@0: role->ARIARoleString()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // container-atomic attribute michael@0: if (ancestor->AttrValueIs(kNameSpaceID_None, nsGkAtoms::aria_atomic, michael@0: nsGkAtoms::_true, eCaseMatters)) { michael@0: SetAccAttr(aAttributes, nsGkAtoms::containerAtomic, michael@0: NS_LITERAL_STRING("true")); michael@0: } michael@0: michael@0: // container-busy attribute michael@0: if (busy.IsEmpty() && michael@0: HasDefinedARIAToken(ancestor, nsGkAtoms::aria_busy) && michael@0: ancestor->GetAttr(kNameSpaceID_None, nsGkAtoms::aria_busy, busy)) michael@0: SetAccAttr(aAttributes, nsGkAtoms::containerBusy, busy); michael@0: michael@0: if (ancestor == aTopContent) michael@0: break; michael@0: michael@0: ancestor = ancestor->GetParent(); michael@0: if (!ancestor) michael@0: ancestor = aTopContent; // Use / michael@0: } michael@0: } michael@0: michael@0: bool michael@0: nsAccUtils::HasDefinedARIAToken(nsIContent *aContent, nsIAtom *aAtom) michael@0: { michael@0: NS_ASSERTION(aContent, "aContent is null in call to HasDefinedARIAToken!"); michael@0: michael@0: if (!aContent->HasAttr(kNameSpaceID_None, aAtom) || michael@0: aContent->AttrValueIs(kNameSpaceID_None, aAtom, michael@0: nsGkAtoms::_empty, eCaseMatters) || michael@0: aContent->AttrValueIs(kNameSpaceID_None, aAtom, michael@0: nsGkAtoms::_undefined, eCaseMatters)) { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: nsIAtom* michael@0: nsAccUtils::GetARIAToken(dom::Element* aElement, nsIAtom* aAttr) michael@0: { michael@0: if (!HasDefinedARIAToken(aElement, aAttr)) michael@0: return nsGkAtoms::_empty; michael@0: michael@0: static nsIContent::AttrValuesArray tokens[] = michael@0: { &nsGkAtoms::_false, &nsGkAtoms::_true, michael@0: &nsGkAtoms::mixed, nullptr}; michael@0: michael@0: int32_t idx = aElement->FindAttrValueIn(kNameSpaceID_None, michael@0: aAttr, tokens, eCaseMatters); michael@0: if (idx >= 0) michael@0: return *(tokens[idx]); michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: Accessible* michael@0: nsAccUtils::GetSelectableContainer(Accessible* aAccessible, uint64_t aState) michael@0: { michael@0: if (!aAccessible) michael@0: return nullptr; michael@0: michael@0: if (!(aState & states::SELECTABLE)) michael@0: return nullptr; michael@0: michael@0: Accessible* parent = aAccessible; michael@0: while ((parent = parent->Parent()) && !parent->IsSelect()) { michael@0: if (parent->Role() == roles::PANE) michael@0: return nullptr; michael@0: } michael@0: return parent; michael@0: } michael@0: michael@0: bool michael@0: nsAccUtils::IsARIASelected(Accessible* aAccessible) michael@0: { michael@0: return aAccessible->GetContent()-> michael@0: AttrValueIs(kNameSpaceID_None, nsGkAtoms::aria_selected, michael@0: nsGkAtoms::_true, eCaseMatters); michael@0: } michael@0: michael@0: HyperTextAccessible* michael@0: nsAccUtils::GetTextContainer(nsINode* aNode) michael@0: { michael@0: // Get text accessible containing the result node. michael@0: DocAccessible* doc = michael@0: GetAccService()->GetDocAccessible(aNode->OwnerDoc()); michael@0: Accessible* accessible = michael@0: doc ? doc->GetAccessibleOrContainer(aNode) : nullptr; michael@0: if (!accessible) michael@0: return nullptr; michael@0: michael@0: do { michael@0: HyperTextAccessible* textAcc = accessible->AsHyperText(); michael@0: if (textAcc) michael@0: return textAcc; michael@0: michael@0: accessible = accessible->Parent(); michael@0: } while (accessible); michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIntPoint michael@0: nsAccUtils::ConvertToScreenCoords(int32_t aX, int32_t aY, michael@0: uint32_t aCoordinateType, michael@0: Accessible* aAccessible) michael@0: { michael@0: nsIntPoint coords(aX, aY); michael@0: michael@0: switch (aCoordinateType) { michael@0: case nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE: michael@0: break; michael@0: michael@0: case nsIAccessibleCoordinateType::COORDTYPE_WINDOW_RELATIVE: michael@0: { michael@0: coords += nsCoreUtils::GetScreenCoordsForWindow(aAccessible->GetNode()); michael@0: break; michael@0: } michael@0: michael@0: case nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE: michael@0: { michael@0: coords += GetScreenCoordsForParent(aAccessible); michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: NS_NOTREACHED("invalid coord type!"); michael@0: } michael@0: michael@0: return coords; michael@0: } michael@0: michael@0: void michael@0: nsAccUtils::ConvertScreenCoordsTo(int32_t *aX, int32_t *aY, michael@0: uint32_t aCoordinateType, michael@0: Accessible* aAccessible) michael@0: { michael@0: switch (aCoordinateType) { michael@0: case nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE: michael@0: break; michael@0: michael@0: case nsIAccessibleCoordinateType::COORDTYPE_WINDOW_RELATIVE: michael@0: { michael@0: nsIntPoint coords = nsCoreUtils::GetScreenCoordsForWindow(aAccessible->GetNode()); michael@0: *aX -= coords.x; michael@0: *aY -= coords.y; michael@0: break; michael@0: } michael@0: michael@0: case nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE: michael@0: { michael@0: nsIntPoint coords = GetScreenCoordsForParent(aAccessible); michael@0: *aX -= coords.x; michael@0: *aY -= coords.y; michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: NS_NOTREACHED("invalid coord type!"); michael@0: } michael@0: } michael@0: michael@0: nsIntPoint michael@0: nsAccUtils::GetScreenCoordsForParent(Accessible* aAccessible) michael@0: { michael@0: Accessible* parent = aAccessible->Parent(); michael@0: if (!parent) michael@0: return nsIntPoint(0, 0); michael@0: michael@0: nsIFrame *parentFrame = parent->GetFrame(); michael@0: if (!parentFrame) michael@0: return nsIntPoint(0, 0); michael@0: michael@0: nsRect rect = parentFrame->GetScreenRectInAppUnits(); michael@0: return nsPoint(rect.x, rect.y). michael@0: ToNearestPixels(parentFrame->PresContext()->AppUnitsPerDevPixel()); michael@0: } michael@0: michael@0: bool michael@0: nsAccUtils::GetLiveAttrValue(uint32_t aRule, nsAString& aValue) michael@0: { michael@0: switch (aRule) { michael@0: case eOffLiveAttr: michael@0: aValue = NS_LITERAL_STRING("off"); michael@0: return true; michael@0: case ePoliteLiveAttr: michael@0: aValue = NS_LITERAL_STRING("polite"); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: michael@0: bool michael@0: nsAccUtils::IsTextInterfaceSupportCorrect(Accessible* aAccessible) michael@0: { michael@0: // Don't test for accessible docs, it makes us create accessibles too michael@0: // early and fire mutation events before we need to michael@0: if (aAccessible->IsDoc()) michael@0: return true; michael@0: michael@0: bool foundText = false; michael@0: uint32_t childCount = aAccessible->ChildCount(); michael@0: for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) { michael@0: Accessible* child = aAccessible->GetChildAt(childIdx); michael@0: if (!IsEmbeddedObject(child)) { michael@0: foundText = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (foundText) { michael@0: // found text child node michael@0: nsCOMPtr text = do_QueryObject(aAccessible); michael@0: if (!text) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: uint32_t michael@0: nsAccUtils::TextLength(Accessible* aAccessible) michael@0: { michael@0: if (IsEmbeddedObject(aAccessible)) michael@0: return 1; michael@0: michael@0: TextLeafAccessible* textLeaf = aAccessible->AsTextLeaf(); michael@0: if (textLeaf) michael@0: return textLeaf->Text().Length(); michael@0: michael@0: // For list bullets (or anything other accessible which would compute its own michael@0: // text. They don't have their own frame. michael@0: // XXX In the future, list bullets may have frame and anon content, so michael@0: // we should be able to remove this at that point michael@0: nsAutoString text; michael@0: aAccessible->AppendTextTo(text); // Get all the text michael@0: return text.Length(); michael@0: } michael@0: michael@0: bool michael@0: nsAccUtils::MustPrune(Accessible* aAccessible) michael@0: { michael@0: roles::Role role = aAccessible->Role(); michael@0: michael@0: // Don't prune the tree for certain roles if the tree is more complex than michael@0: // a single text leaf. michael@0: return michael@0: (role == roles::MENUITEM || michael@0: role == roles::COMBOBOX_OPTION || michael@0: role == roles::OPTION || michael@0: role == roles::ENTRY || michael@0: role == roles::FLAT_EQUATION || michael@0: role == roles::PASSWORD_TEXT || michael@0: role == roles::PUSHBUTTON || michael@0: role == roles::TOGGLE_BUTTON || michael@0: role == roles::GRAPHIC || michael@0: role == roles::SLIDER || michael@0: role == roles::PROGRESSBAR || michael@0: role == roles::SEPARATOR) && michael@0: aAccessible->ContentChildCount() == 1 && michael@0: aAccessible->ContentChildAt(0)->IsTextLeaf(); michael@0: }