diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /accessible/html | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'accessible/html')
-rw-r--r-- | accessible/html/HTMLCanvasAccessible.cpp | 16 | ||||
-rw-r--r-- | accessible/html/HTMLCanvasAccessible.h | 35 | ||||
-rw-r--r-- | accessible/html/HTMLElementAccessibles.cpp | 231 | ||||
-rw-r--r-- | accessible/html/HTMLElementAccessibles.h | 159 | ||||
-rw-r--r-- | accessible/html/HTMLFormControlAccessible.cpp | 979 | ||||
-rw-r--r-- | accessible/html/HTMLFormControlAccessible.h | 387 | ||||
-rw-r--r-- | accessible/html/HTMLImageMapAccessible.cpp | 200 | ||||
-rw-r--r-- | accessible/html/HTMLImageMapAccessible.h | 82 | ||||
-rw-r--r-- | accessible/html/HTMLLinkAccessible.cpp | 129 | ||||
-rw-r--r-- | accessible/html/HTMLLinkAccessible.h | 59 | ||||
-rw-r--r-- | accessible/html/HTMLListAccessible.cpp | 112 | ||||
-rw-r--r-- | accessible/html/HTMLListAccessible.h | 85 | ||||
-rw-r--r-- | accessible/html/HTMLSelectAccessible.cpp | 472 | ||||
-rw-r--r-- | accessible/html/HTMLSelectAccessible.h | 216 | ||||
-rw-r--r-- | accessible/html/HTMLTableAccessible.cpp | 712 | ||||
-rw-r--r-- | accessible/html/HTMLTableAccessible.h | 177 | ||||
-rw-r--r-- | accessible/html/moz.build | 52 |
17 files changed, 4103 insertions, 0 deletions
diff --git a/accessible/html/HTMLCanvasAccessible.cpp b/accessible/html/HTMLCanvasAccessible.cpp new file mode 100644 index 0000000000..d6b02ce61c --- /dev/null +++ b/accessible/html/HTMLCanvasAccessible.cpp @@ -0,0 +1,16 @@ +/* -*- 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 "HTMLCanvasAccessible.h" + +#include "mozilla/a11y/Role.h" + +using namespace mozilla::a11y; + +HTMLCanvasAccessible::HTMLCanvasAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +role HTMLCanvasAccessible::NativeRole() const { return roles::CANVAS; } diff --git a/accessible/html/HTMLCanvasAccessible.h b/accessible/html/HTMLCanvasAccessible.h new file mode 100644 index 0000000000..49d443bce1 --- /dev/null +++ b/accessible/html/HTMLCanvasAccessible.h @@ -0,0 +1,35 @@ +/* -*- 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/. */ + +#ifndef mozilla_a11y_HTMLCanvasAccessible_h__ +#define mozilla_a11y_HTMLCanvasAccessible_h__ + +#include "HyperTextAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * HTML canvas accessible (html:canvas). + */ +class HTMLCanvasAccessible : public HyperTextAccessible { + public: + HTMLCanvasAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // nsISupports + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLCanvasAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + + protected: + virtual ~HTMLCanvasAccessible() {} +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLElementAccessibles.cpp b/accessible/html/HTMLElementAccessibles.cpp new file mode 100644 index 0000000000..e01de31ff2 --- /dev/null +++ b/accessible/html/HTMLElementAccessibles.cpp @@ -0,0 +1,231 @@ +/* -*- 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 "HTMLElementAccessibles.h" + +#include "CacheConstants.h" +#include "nsCoreUtils.h" +#include "nsTextEquivUtils.h" +#include "Relation.h" +#include "mozilla/a11y/Role.h" +#include "States.h" + +#include "mozilla/dom/HTMLLabelElement.h" +#include "mozilla/dom/HTMLDetailsElement.h" +#include "mozilla/dom/HTMLSummaryElement.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLHRAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLHRAccessible::NativeRole() const { return roles::SEPARATOR; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLBRAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLBRAccessible::NativeRole() const { return roles::WHITESPACE; } + +uint64_t HTMLBRAccessible::NativeState() const { return states::READONLY; } + +ENameValueFlag HTMLBRAccessible::NativeName(nsString& aName) const { + aName = static_cast<char16_t>('\n'); // Newline char + return eNameOK; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLLabelAccessible +//////////////////////////////////////////////////////////////////////////////// + +ENameValueFlag HTMLLabelAccessible::NativeName(nsString& aName) const { + nsTextEquivUtils::GetNameFromSubtree(this, aName); + return aName.IsEmpty() ? eNameOK : eNameFromSubtree; +} + +Relation HTMLLabelAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + if (aType == RelationType::LABEL_FOR) { + dom::HTMLLabelElement* label = dom::HTMLLabelElement::FromNode(mContent); + rel.AppendTarget(mDoc, label->GetControl()); + } + + return rel; +} + +void HTMLLabelAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::_for) { + mDoc->QueueCacheUpdate(this, CacheDomain::Relations | CacheDomain::Actions); + } +} + +bool HTMLLabelAccessible::HasPrimaryAction() const { + return nsCoreUtils::IsLabelWithControl(mContent); +} + +void HTMLLabelAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == 0) { + if (HasPrimaryAction()) { + aName.AssignLiteral("click"); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// nsHTMLOuputAccessible +//////////////////////////////////////////////////////////////////////////////// + +Relation HTMLOutputAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + if (aType == RelationType::CONTROLLED_BY) { + rel.AppendIter(new IDRefsIterator(mDoc, mContent, nsGkAtoms::_for)); + } + + return rel; +} + +void HTMLOutputAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::_for) { + mDoc->QueueCacheUpdate(this, CacheDomain::Relations); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSummaryAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLSummaryAccessible::HTMLSummaryAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mGenericTypes |= eButton; +} + +bool HTMLSummaryAccessible::HasPrimaryAction() const { return true; } + +void HTMLSummaryAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex != eAction_Click) { + return; + } + + dom::HTMLSummaryElement* summary = + dom::HTMLSummaryElement::FromNode(mContent); + if (!summary) { + return; + } + + dom::HTMLDetailsElement* details = summary->GetDetails(); + if (!details) { + return; + } + + if (details->Open()) { + aName.AssignLiteral("collapse"); + } else { + aName.AssignLiteral("expand"); + } +} + +uint64_t HTMLSummaryAccessible::NativeState() const { + uint64_t state = HyperTextAccessible::NativeState(); + + dom::HTMLSummaryElement* summary = + dom::HTMLSummaryElement::FromNode(mContent); + if (!summary) { + return state; + } + + dom::HTMLDetailsElement* details = summary->GetDetails(); + if (!details) { + return state; + } + + if (details->Open()) { + state |= states::EXPANDED; + } else { + state |= states::COLLAPSED; + } + + return state; +} + +HTMLSummaryAccessible* HTMLSummaryAccessible::FromDetails( + LocalAccessible* details) { + if (!dom::HTMLDetailsElement::FromNodeOrNull(details->GetContent())) { + return nullptr; + } + + HTMLSummaryAccessible* summaryAccessible = nullptr; + for (uint32_t i = 0; i < details->ChildCount(); i++) { + // Iterate through the children of our details accessible to locate main + // summary. This iteration includes the anonymous summary if the details + // element was not explicitly created with one. + LocalAccessible* child = details->LocalChildAt(i); + auto* summary = + mozilla::dom::HTMLSummaryElement::FromNodeOrNull(child->GetContent()); + if (summary && summary->IsMainSummary()) { + summaryAccessible = static_cast<HTMLSummaryAccessible*>(child); + break; + } + } + + return summaryAccessible; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSummaryAccessible: Widgets + +bool HTMLSummaryAccessible::IsWidget() const { return true; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLHeaderOrFooterAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLHeaderOrFooterAccessible::NativeRole() const { + // Only map header and footer if they are direct descendants of the body tag. + // If other sectioning or sectioning root elements, they become sections. + nsIContent* parent = mContent->GetParent(); + while (parent) { + if (parent->IsAnyOfHTMLElements( + nsGkAtoms::article, nsGkAtoms::aside, nsGkAtoms::nav, + nsGkAtoms::section, nsGkAtoms::main, nsGkAtoms::blockquote, + nsGkAtoms::details, nsGkAtoms::dialog, nsGkAtoms::fieldset, + nsGkAtoms::figure, nsGkAtoms::td)) { + break; + } + parent = parent->GetParent(); + } + + // No sectioning or sectioning root elements found. + if (!parent) { + return roles::LANDMARK; + } + + return roles::SECTION; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSectionAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLSectionAccessible::NativeRole() const { + nsAutoString name; + const_cast<HTMLSectionAccessible*>(this)->Name(name); + return name.IsEmpty() ? roles::SECTION : roles::REGION; +} diff --git a/accessible/html/HTMLElementAccessibles.h b/accessible/html/HTMLElementAccessibles.h new file mode 100644 index 0000000000..5a3ec6cef8 --- /dev/null +++ b/accessible/html/HTMLElementAccessibles.h @@ -0,0 +1,159 @@ +/* -*- 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/. */ + +#ifndef mozilla_a11y_HTMLElementAccessibles_h__ +#define mozilla_a11y_HTMLElementAccessibles_h__ + +#include "BaseAccessibles.h" + +namespace mozilla { +namespace a11y { + +/** + * Used for HTML hr element. + */ +class HTMLHRAccessible : public LeafAccessible { + public: + HTMLHRAccessible(nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) {} + + // LocalAccessible + virtual a11y::role NativeRole() const override; +}; + +/** + * Used for HTML br element. + */ +class HTMLBRAccessible : public LeafAccessible { + public: + HTMLBRAccessible(nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + mType = eHTMLBRType; + mGenericTypes |= eText; + } + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for HTML label element. + */ +class HTMLLabelAccessible : public HyperTextAccessible { + public: + HTMLLabelAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLLabelAccessible, HyperTextAccessible) + + // LocalAccessible + virtual Relation RelationByType(RelationType aType) const override; + + // ActionAccessible + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool HasPrimaryAction() const override; + + protected: + virtual ~HTMLLabelAccessible() {} + virtual ENameValueFlag NativeName(nsString& aName) const override; + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; +}; + +/** + * Used for HTML output element. + */ +class HTMLOutputAccessible : public HyperTextAccessible { + public: + HTMLOutputAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLOutputAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual Relation RelationByType(RelationType aType) const override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + protected: + virtual ~HTMLOutputAccessible() {} +}; + +/** + * Accessible for the HTML summary element. + */ +class HTMLSummaryAccessible : public HyperTextAccessible { + public: + enum { eAction_Click = 0 }; + + HTMLSummaryAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Check that the given LocalAccessible belongs to a details frame. + // If so, find and return the accessible for the detail frame's + // main summary. + static HTMLSummaryAccessible* FromDetails(LocalAccessible* aDetails); + + // LocalAccessible + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool HasPrimaryAction() const override; + + // Widgets + virtual bool IsWidget() const override; +}; + +/** + * Used for HTML header and footer elements. + */ +class HTMLHeaderOrFooterAccessible : public HyperTextAccessible { + public: + HTMLHeaderOrFooterAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLHeaderOrFooterAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + + protected: + virtual ~HTMLHeaderOrFooterAccessible() {} +}; + +/** + * Used for HTML section element. + */ +class HTMLSectionAccessible : public HyperTextAccessible { + public: + HTMLSectionAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLSectionAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + + protected: + virtual ~HTMLSectionAccessible() = default; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLFormControlAccessible.cpp b/accessible/html/HTMLFormControlAccessible.cpp new file mode 100644 index 0000000000..95d6fed7b3 --- /dev/null +++ b/accessible/html/HTMLFormControlAccessible.cpp @@ -0,0 +1,979 @@ +/* -*- 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 "HTMLFormControlAccessible.h" + +#include "CacheConstants.h" +#include "DocAccessible-inl.h" +#include "LocalAccessible-inl.h" +#include "nsAccUtils.h" +#include "nsEventShell.h" +#include "nsTextEquivUtils.h" +#include "Relation.h" +#include "mozilla/a11y/Role.h" +#include "States.h" +#include "TextLeafAccessible.h" + +#include "nsContentList.h" +#include "mozilla/dom/HTMLInputElement.h" +#include "mozilla/dom/HTMLTextAreaElement.h" +#include "mozilla/dom/HTMLFormControlsCollection.h" +#include "nsIFormControl.h" + +#include "mozilla/FloatingPoint.h" +#include "mozilla/Preferences.h" +#include "mozilla/TextEditor.h" + +using namespace mozilla; +using namespace mozilla::dom; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLFormAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLFormAccessible::NativeRole() const { + nsAutoString name; + const_cast<HTMLFormAccessible*>(this)->Name(name); + return name.IsEmpty() ? roles::FORM : roles::FORM_LANDMARK; +} + +void HTMLFormAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + if (aAttribute == nsGkAtoms::autocomplete) { + dom::HTMLFormElement* formEl = dom::HTMLFormElement::FromNode(mContent); + + HTMLFormControlsCollection* controls = formEl->Elements(); + uint32_t length = controls->Length(); + for (uint32_t i = 0; i < length; i++) { + if (LocalAccessible* acc = mDoc->GetAccessible(controls->Item(i))) { + if (acc->IsTextField() && !acc->IsPassword()) { + if (!acc->Elm()->HasAttr(nsGkAtoms::list_) && + !acc->Elm()->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::autocomplete, nsGkAtoms::OFF, + eIgnoreCase)) { + RefPtr<AccEvent> stateChangeEvent = + new AccStateChangeEvent(acc, states::SUPPORTS_AUTOCOMPLETION); + mDoc->FireDelayedEvent(stateChangeEvent); + } + } + } + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLRadioButtonAccessible +//////////////////////////////////////////////////////////////////////////////// + +uint64_t HTMLRadioButtonAccessible::NativeState() const { + uint64_t state = AccessibleWrap::NativeState(); + + state |= states::CHECKABLE; + + HTMLInputElement* input = HTMLInputElement::FromNode(mContent); + if (input && input->Checked()) state |= states::CHECKED; + + return state; +} + +void HTMLRadioButtonAccessible::GetPositionAndSetSize(int32_t* aPosInSet, + int32_t* aSetSize) { + Unused << ComputeGroupAttributes(aPosInSet, aSetSize); +} + +void HTMLRadioButtonAccessible::DOMAttributeChanged( + int32_t aNameSpaceID, nsAtom* aAttribute, int32_t aModType, + const nsAttrValue* aOldValue, uint64_t aOldState) { + if (aAttribute == nsGkAtoms::name) { + // If our name changed, it's possible our MEMBER_OF relation + // also changed. Push a cache update for Relations. + mDoc->QueueCacheUpdate(this, CacheDomain::Relations); + } else { + // Otherwise, handle this attribute change the way our parent + // class wants us to handle it. + RadioButtonAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, + aModType, aOldValue, aOldState); + } +} + +Relation HTMLRadioButtonAccessible::ComputeGroupAttributes( + int32_t* aPosInSet, int32_t* aSetSize) const { + Relation rel = Relation(); + int32_t namespaceId = mContent->NodeInfo()->NamespaceID(); + nsAutoString tagName; + mContent->NodeInfo()->GetName(tagName); + + nsAutoString type; + mContent->AsElement()->GetAttr(nsGkAtoms::type, type); + nsAutoString name; + mContent->AsElement()->GetAttr(nsGkAtoms::name, name); + + RefPtr<nsContentList> inputElms; + + nsCOMPtr<nsIFormControl> formControlNode(do_QueryInterface(mContent)); + if (dom::Element* formElm = formControlNode->GetForm()) { + inputElms = NS_GetContentList(formElm, namespaceId, tagName); + } else { + inputElms = NS_GetContentList(mContent->OwnerDoc(), namespaceId, tagName); + } + NS_ENSURE_TRUE(inputElms, rel); + + uint32_t inputCount = inputElms->Length(false); + + // Compute posinset and setsize. + int32_t indexOf = 0; + int32_t count = 0; + + for (uint32_t index = 0; index < inputCount; index++) { + nsIContent* inputElm = inputElms->Item(index, false); + if (inputElm->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + type, eCaseMatters) && + inputElm->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, + name, eCaseMatters) && + mDoc->HasAccessible(inputElm)) { + count++; + rel.AppendTarget(mDoc->GetAccessible(inputElm)); + if (inputElm == mContent) indexOf = count; + } + } + + *aPosInSet = indexOf; + *aSetSize = count; + return rel; +} + +Relation HTMLRadioButtonAccessible::RelationByType(RelationType aType) const { + if (aType == RelationType::MEMBER_OF) { + int32_t unusedPos, unusedSetSize; + return ComputeGroupAttributes(&unusedPos, &unusedSetSize); + } + + return LocalAccessible::RelationByType(aType); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLButtonAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLButtonAccessible::HTMLButtonAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mGenericTypes |= eButton; +} + +bool HTMLButtonAccessible::HasPrimaryAction() const { return true; } + +void HTMLButtonAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) aName.AssignLiteral("press"); +} + +uint64_t HTMLButtonAccessible::NativeState() const { + uint64_t state = HyperTextAccessible::NativeState(); + + dom::Element* elm = Elm(); + if (auto* popover = elm->GetEffectivePopoverTargetElement()) { + LocalAccessible* popoverAcc = mDoc->GetAccessible(popover); + if (!popoverAcc || !popoverAcc->IsAncestorOf(this)) { + if (popover->IsPopoverOpen()) { + state |= states::EXPANDED; + } else { + state |= states::COLLAPSED; + } + } + } + + ElementState elmState = mContent->AsElement()->State(); + if (elmState.HasState(ElementState::DEFAULT)) state |= states::DEFAULT; + + return state; +} + +role HTMLButtonAccessible::NativeRole() const { return roles::PUSHBUTTON; } + +ENameValueFlag HTMLButtonAccessible::NativeName(nsString& aName) const { + // No need to check @value attribute for buttons since this attribute results + // in native anonymous text node and the name is calculated from subtree. + // The same magic works for @alt and @value attributes in case of type="image" + // element that has no valid @src (note if input@type="image" has an image + // then neither @alt nor @value attributes are used to generate a visual label + // and thus we need to obtain the accessible name directly from attribute + // value). Also the same algorithm works in case of default labels for + // type="submit"/"reset"/"image" elements. + + ENameValueFlag nameFlag = LocalAccessible::NativeName(aName); + if (!aName.IsEmpty() || !mContent->IsHTMLElement(nsGkAtoms::input) || + !mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::image, eCaseMatters)) { + return nameFlag; + } + + if (!mContent->AsElement()->GetAttr(nsGkAtoms::alt, aName)) { + mContent->AsElement()->GetAttr(nsGkAtoms::value, aName); + } + + aName.CompressWhitespace(); + return eNameOK; +} + +void HTMLButtonAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::value) { + dom::Element* elm = Elm(); + if (elm->IsHTMLElement(nsGkAtoms::input) || + (elm->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, nsGkAtoms::image, + eCaseMatters) && + !elm->HasAttr(nsGkAtoms::alt))) { + if (!nsAccUtils::HasARIAAttr(elm, nsGkAtoms::aria_labelledby) && + !nsAccUtils::HasARIAAttr(elm, nsGkAtoms::aria_label)) { + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); + } + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLButtonAccessible: Widgets + +bool HTMLButtonAccessible::IsWidget() const { return true; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTextFieldAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLTextFieldAccessible::HTMLTextFieldAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::password, eIgnoreCase) + ? eHTMLTextPasswordFieldType + : eHTMLTextFieldType; +} + +role HTMLTextFieldAccessible::NativeRole() const { + if (mType == eHTMLTextPasswordFieldType) { + return roles::PASSWORD_TEXT; + } + if (mContent->AsElement()->HasAttr(nsGkAtoms::list_)) { + return roles::EDITCOMBOBOX; + } + return roles::ENTRY; +} + +already_AddRefed<AccAttributes> HTMLTextFieldAccessible::NativeAttributes() { + RefPtr<AccAttributes> attributes = HyperTextAccessible::NativeAttributes(); + + // Expose type for text input elements as it gives some useful context, + // especially for mobile. + if (const nsAttrValue* attr = + mContent->AsElement()->GetParsedAttr(nsGkAtoms::type)) { + RefPtr<nsAtom> inputType = attr->GetAsAtom(); + if (inputType) { + if (!ARIARoleMap() && inputType == nsGkAtoms::search) { + attributes->SetAttribute(nsGkAtoms::xmlroles, nsGkAtoms::searchbox); + } + attributes->SetAttribute(nsGkAtoms::textInputType, inputType); + } + } + // If this element has the placeholder attribute set, + // and if that is not identical to the name, expose it as an object attribute. + nsString placeholderText; + if (mContent->AsElement()->GetAttr(nsGkAtoms::placeholder, placeholderText)) { + nsAutoString name; + const_cast<HTMLTextFieldAccessible*>(this)->Name(name); + if (!name.Equals(placeholderText)) { + attributes->SetAttribute(nsGkAtoms::placeholder, + std::move(placeholderText)); + } + } + + return attributes.forget(); +} + +ENameValueFlag HTMLTextFieldAccessible::Name(nsString& aName) const { + ENameValueFlag nameFlag = LocalAccessible::Name(aName); + if (!aName.IsEmpty()) return nameFlag; + + // text inputs and textareas might have useful placeholder text + mContent->AsElement()->GetAttr(nsGkAtoms::placeholder, aName); + return eNameOK; +} + +void HTMLTextFieldAccessible::Value(nsString& aValue) const { + aValue.Truncate(); + if (NativeState() & states::PROTECTED) { // Don't return password text! + return; + } + + HTMLTextAreaElement* textArea = HTMLTextAreaElement::FromNode(mContent); + if (textArea) { + textArea->GetValue(aValue); + return; + } + + HTMLInputElement* input = HTMLInputElement::FromNode(mContent); + if (input) { + // Pass NonSystem as the caller type, to be safe. We don't expect to have a + // file input here. + input->GetValue(aValue, CallerType::NonSystem); + } +} + +bool HTMLTextFieldAccessible::AttributeChangesState(nsAtom* aAttribute) { + if (aAttribute == nsGkAtoms::readonly || aAttribute == nsGkAtoms::list_ || + aAttribute == nsGkAtoms::autocomplete) { + return true; + } + + return LocalAccessible::AttributeChangesState(aAttribute); +} + +void HTMLTextFieldAccessible::ApplyARIAState(uint64_t* aState) const { + HyperTextAccessible::ApplyARIAState(aState); + aria::MapToState(aria::eARIAAutoComplete, mContent->AsElement(), aState); +} + +uint64_t HTMLTextFieldAccessible::NativeState() const { + uint64_t state = HyperTextAccessible::NativeState(); + + // Text fields are always editable, even if they are also read only or + // disabled. + state |= states::EDITABLE; + + // can be focusable, focused, protected. readonly, unavailable, selected + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::password, eIgnoreCase)) { + state |= states::PROTECTED; + } + + if (mContent->AsElement()->HasAttr(nsGkAtoms::readonly)) { + state |= states::READONLY; + } + + // Is it an <input> or a <textarea> ? + HTMLInputElement* input = HTMLInputElement::FromNode(mContent); + state |= input && input->IsSingleLineTextControl() ? states::SINGLE_LINE + : states::MULTI_LINE; + + if (state & (states::PROTECTED | states::MULTI_LINE | states::READONLY | + states::UNAVAILABLE)) { + return state; + } + + // Expose autocomplete state if it has associated autocomplete list. + if (mContent->AsElement()->HasAttr(nsGkAtoms::list_)) { + return state | states::SUPPORTS_AUTOCOMPLETION | states::HASPOPUP; + } + + if (Preferences::GetBool("browser.formfill.enable")) { + // Check to see if autocompletion is allowed on this input. We don't expose + // it for password fields even though the entire password can be remembered + // for a page if the user asks it to be. However, the kind of autocomplete + // we're talking here is based on what the user types, where a popup of + // possible choices comes up. + nsAutoString autocomplete; + mContent->AsElement()->GetAttr(nsGkAtoms::autocomplete, autocomplete); + + if (!autocomplete.LowerCaseEqualsLiteral("off")) { + Element* formElement = input->GetForm(); + if (formElement) { + formElement->GetAttr(nsGkAtoms::autocomplete, autocomplete); + } + + if (!formElement || !autocomplete.LowerCaseEqualsLiteral("off")) { + state |= states::SUPPORTS_AUTOCOMPLETION; + } + } + } + + return state; +} + +bool HTMLTextFieldAccessible::HasPrimaryAction() const { return true; } + +void HTMLTextFieldAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) aName.AssignLiteral("activate"); +} + +bool HTMLTextFieldAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != 0) return false; + + if (FocusMgr()->IsFocused(this)) { + // This already has focus, so TakeFocus()will do nothing. However, the user + // might be activating this element because they dismissed a touch keyboard + // and want to bring it back. + DoCommand(); + } else { + TakeFocus(); + } + return true; +} + +already_AddRefed<EditorBase> HTMLTextFieldAccessible::GetEditor() const { + RefPtr<TextControlElement> textControlElement = + TextControlElement::FromNodeOrNull(mContent); + if (!textControlElement) { + return nullptr; + } + RefPtr<TextEditor> textEditor = textControlElement->GetTextEditor(); + return textEditor.forget(); +} + +void HTMLTextFieldAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + if (aAttribute == nsGkAtoms::placeholder) { + mDoc->QueueCacheUpdate(this, CacheDomain::NameAndDescription); + return; + } + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTextFieldAccessible: Widgets + +bool HTMLTextFieldAccessible::IsWidget() const { return true; } + +LocalAccessible* HTMLTextFieldAccessible::ContainerWidget() const { + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLFileInputAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLFileInputAccessible::HTMLFileInputAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLFileInputType; + mGenericTypes |= eButton; +} + +role HTMLFileInputAccessible::NativeRole() const { return roles::PUSHBUTTON; } + +bool HTMLFileInputAccessible::IsAcceptableChild(nsIContent* aEl) const { + // File inputs are rendered using native anonymous children. However, we + // want to expose this as a button Accessible so that clients can pick up the + // name and description from the button they activate, rather than a + // container. We still expose the text leaf descendants so we can get the + // name of the Browse button and the file name. + return aEl->IsText(); +} + +ENameValueFlag HTMLFileInputAccessible::Name(nsString& aName) const { + ENameValueFlag flag = HyperTextAccessible::Name(aName); + if (flag == eNameFromSubtree) { + // The author didn't provide a name. We'll compute the name from our subtree + // below. + aName.Truncate(); + } else { + // The author provided a name. We do use that, but we also append our + // subtree text so the user knows this is a file chooser button and what + // file has been chosen. + if (aName.IsEmpty()) { + // Name computation is recursing, perhaps due to a wrapping <label>. Don't + // append the subtree text. Return " " to prevent + // nsTextEquivUtils::AppendFromAccessible walking the subtree itself. + aName += ' '; + return flag; + } + } + // Unfortunately, GetNameFromSubtree doesn't separate the button text from the + // file name text. Compute the text ourselves. + uint32_t count = ChildCount(); + for (uint32_t c = 0; c < count; ++c) { + TextLeafAccessible* leaf = LocalChildAt(c)->AsTextLeaf(); + MOZ_ASSERT(leaf); + if (!aName.IsEmpty()) { + aName += ' '; + } + aName += leaf->Text(); + } + return flag; +} + +bool HTMLFileInputAccessible::HasPrimaryAction() const { return true; } + +void HTMLFileInputAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == 0) { + aName.AssignLiteral("press"); + } +} + +bool HTMLFileInputAccessible::IsWidget() const { return true; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSpinnerAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLSpinnerAccessible::NativeRole() const { return roles::SPINBUTTON; } + +void HTMLSpinnerAccessible::Value(nsString& aValue) const { + HTMLTextFieldAccessible::Value(aValue); + if (!aValue.IsEmpty()) return; + + // Pass NonSystem as the caller type, to be safe. We don't expect to have a + // file input here. + HTMLInputElement::FromNode(mContent)->GetValue(aValue, CallerType::NonSystem); +} + +double HTMLSpinnerAccessible::MaxValue() const { + double value = HTMLTextFieldAccessible::MaxValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetMaximum().toDouble(); +} + +double HTMLSpinnerAccessible::MinValue() const { + double value = HTMLTextFieldAccessible::MinValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetMinimum().toDouble(); +} + +double HTMLSpinnerAccessible::Step() const { + double value = HTMLTextFieldAccessible::Step(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetStep().toDouble(); +} + +double HTMLSpinnerAccessible::CurValue() const { + double value = HTMLTextFieldAccessible::CurValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetValueAsDecimal().toDouble(); +} + +bool HTMLSpinnerAccessible::SetCurValue(double aValue) { + ErrorResult er; + HTMLInputElement::FromNode(mContent)->SetValueAsNumber(aValue, er); + return !er.Failed(); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLRangeAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLRangeAccessible::NativeRole() const { return roles::SLIDER; } + +bool HTMLRangeAccessible::IsWidget() const { return true; } + +void HTMLRangeAccessible::Value(nsString& aValue) const { + LeafAccessible::Value(aValue); + if (!aValue.IsEmpty()) return; + + // Pass NonSystem as the caller type, to be safe. We don't expect to have a + // file input here. + HTMLInputElement::FromNode(mContent)->GetValue(aValue, CallerType::NonSystem); +} + +double HTMLRangeAccessible::MaxValue() const { + double value = LeafAccessible::MaxValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetMaximum().toDouble(); +} + +double HTMLRangeAccessible::MinValue() const { + double value = LeafAccessible::MinValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetMinimum().toDouble(); +} + +double HTMLRangeAccessible::Step() const { + double value = LeafAccessible::Step(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetStep().toDouble(); +} + +double HTMLRangeAccessible::CurValue() const { + double value = LeafAccessible::CurValue(); + if (!std::isnan(value)) return value; + + return HTMLInputElement::FromNode(mContent)->GetValueAsDecimal().toDouble(); +} + +bool HTMLRangeAccessible::SetCurValue(double aValue) { + nsAutoString strValue; + strValue.AppendFloat(aValue); + HTMLInputElement::FromNode(mContent)->SetUserInput( + strValue, *nsContentUtils::GetSystemPrincipal()); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLGroupboxAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLGroupboxAccessible::HTMLGroupboxAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +role HTMLGroupboxAccessible::NativeRole() const { return roles::GROUPING; } + +nsIContent* HTMLGroupboxAccessible::GetLegend() const { + for (nsIContent* legendContent = mContent->GetFirstChild(); legendContent; + legendContent = legendContent->GetNextSibling()) { + if (legendContent->NodeInfo()->Equals(nsGkAtoms::legend, + mContent->GetNameSpaceID())) { + // Either XHTML namespace or no namespace + return legendContent; + } + } + + return nullptr; +} + +ENameValueFlag HTMLGroupboxAccessible::NativeName(nsString& aName) const { + ENameValueFlag nameFlag = LocalAccessible::NativeName(aName); + if (!aName.IsEmpty()) return nameFlag; + + nsIContent* legendContent = GetLegend(); + if (legendContent) { + nsTextEquivUtils::AppendTextEquivFromContent(this, legendContent, &aName); + } + + aName.CompressWhitespace(); + return eNameOK; +} + +Relation HTMLGroupboxAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessible::RelationByType(aType); + // No override for label, so use <legend> for this <fieldset> + if (aType == RelationType::LABELLED_BY) rel.AppendTarget(mDoc, GetLegend()); + + return rel; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLLegendAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLLegendAccessible::HTMLLegendAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +Relation HTMLLegendAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessible::RelationByType(aType); + if (aType != RelationType::LABEL_FOR) return rel; + + LocalAccessible* groupbox = LocalParent(); + if (groupbox && groupbox->Role() == roles::GROUPING) { + rel.AppendTarget(groupbox); + } + + return rel; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLFigureAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLFigureAccessible::HTMLFigureAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +ENameValueFlag HTMLFigureAccessible::NativeName(nsString& aName) const { + ENameValueFlag nameFlag = HyperTextAccessible::NativeName(aName); + if (!aName.IsEmpty()) return nameFlag; + + nsIContent* captionContent = Caption(); + if (captionContent) { + nsTextEquivUtils::AppendTextEquivFromContent(this, captionContent, &aName); + } + + aName.CompressWhitespace(); + return eNameOK; +} + +Relation HTMLFigureAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessible::RelationByType(aType); + if (aType == RelationType::LABELLED_BY) rel.AppendTarget(mDoc, Caption()); + + return rel; +} + +nsIContent* HTMLFigureAccessible::Caption() const { + for (nsIContent* childContent = mContent->GetFirstChild(); childContent; + childContent = childContent->GetNextSibling()) { + if (childContent->NodeInfo()->Equals(nsGkAtoms::figcaption, + mContent->GetNameSpaceID())) { + return childContent; + } + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLFigcaptionAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLFigcaptionAccessible::HTMLFigcaptionAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +Relation HTMLFigcaptionAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessible::RelationByType(aType); + if (aType != RelationType::LABEL_FOR) return rel; + + LocalAccessible* figure = LocalParent(); + if (figure && figure->GetContent()->NodeInfo()->Equals( + nsGkAtoms::figure, mContent->GetNameSpaceID())) { + rel.AppendTarget(figure); + } + + return rel; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLProgressAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLProgressAccessible::NativeRole() const { return roles::PROGRESSBAR; } + +uint64_t HTMLProgressAccessible::NativeState() const { + uint64_t state = LeafAccessible::NativeState(); + + // An undetermined progressbar (i.e. without a value) has a mixed state. + nsAutoString attrValue; + mContent->AsElement()->GetAttr(nsGkAtoms::value, attrValue); + if (attrValue.IsEmpty()) { + state |= states::MIXED; + } + + return state; +} + +bool HTMLProgressAccessible::IsWidget() const { return true; } + +void HTMLProgressAccessible::Value(nsString& aValue) const { + LeafAccessible::Value(aValue); + if (!aValue.IsEmpty()) { + return; + } + + double maxValue = MaxValue(); + if (std::isnan(maxValue) || maxValue == 0) { + return; + } + + double curValue = CurValue(); + if (std::isnan(curValue)) { + return; + } + + // Treat the current value bigger than maximum as 100%. + double percentValue = + (curValue < maxValue) ? (curValue / maxValue) * 100 : 100; + + aValue.AppendFloat(percentValue); + aValue.Append('%'); +} + +double HTMLProgressAccessible::MaxValue() const { + double value = LeafAccessible::MaxValue(); + if (!std::isnan(value)) { + return value; + } + + nsAutoString strValue; + if (mContent->AsElement()->GetAttr(nsGkAtoms::max, strValue)) { + nsresult result = NS_OK; + value = strValue.ToDouble(&result); + if (NS_SUCCEEDED(result)) { + return value; + } + } + + return 1; +} + +double HTMLProgressAccessible::MinValue() const { + double value = LeafAccessible::MinValue(); + return std::isnan(value) ? 0 : value; +} + +double HTMLProgressAccessible::Step() const { + double value = LeafAccessible::Step(); + return std::isnan(value) ? 0 : value; +} + +double HTMLProgressAccessible::CurValue() const { + double value = LeafAccessible::CurValue(); + if (!std::isnan(value)) { + return value; + } + + nsAutoString attrValue; + if (!mContent->AsElement()->GetAttr(nsGkAtoms::value, attrValue)) { + return UnspecifiedNaN<double>(); + } + + nsresult error = NS_OK; + value = attrValue.ToDouble(&error); + return NS_FAILED(error) ? UnspecifiedNaN<double>() : value; +} + +bool HTMLProgressAccessible::SetCurValue(double aValue) { + return false; // progress meters are readonly. +} + +void HTMLProgressAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + LeafAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::value) { + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_VALUE_CHANGE, this); + + uint64_t currState = NativeState(); + if ((aOldState ^ currState) & states::MIXED) { + RefPtr<AccEvent> stateChangeEvent = new AccStateChangeEvent( + this, states::MIXED, (currState & states::MIXED)); + mDoc->FireDelayedEvent(stateChangeEvent); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLMeterAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLMeterAccessible::NativeRole() const { return roles::METER; } + +bool HTMLMeterAccessible::IsWidget() const { return true; } + +void HTMLMeterAccessible::Value(nsString& aValue) const { + LeafAccessible::Value(aValue); + if (!aValue.IsEmpty()) { + return; + } + + // If we did not get a value from the above LeafAccessible call, + // we should check to see if the meter has inner text. + // If it does, we'll use that as our value. + nsTextEquivUtils::AppendFromDOMChildren(mContent, &aValue); + aValue.CompressWhitespace(); + if (!aValue.IsEmpty()) { + return; + } + + // If no inner text is found, use curValue + double curValue = CurValue(); + if (std::isnan(curValue)) { + return; + } + + aValue.AppendFloat(curValue); +} + +double HTMLMeterAccessible::MaxValue() const { + double max = LeafAccessible::MaxValue(); + double min = MinValue(); + + if (!std::isnan(max)) { + return max > min ? max : min; + } + + // If we didn't find a max value, check for the max attribute + nsAutoString strValue; + if (mContent->AsElement()->GetAttr(nsGkAtoms::max, strValue)) { + nsresult result = NS_OK; + max = strValue.ToDouble(&result); + if (NS_SUCCEEDED(result)) { + return max > min ? max : min; + } + } + + return 1 > min ? 1 : min; +} + +double HTMLMeterAccessible::MinValue() const { + double min = LeafAccessible::MinValue(); + if (!std::isnan(min)) { + return min; + } + + nsAutoString strValue; + if (mContent->AsElement()->GetAttr(nsGkAtoms::min, strValue)) { + nsresult result = NS_OK; + min = strValue.ToDouble(&result); + if (NS_SUCCEEDED(result)) { + return min; + } + } + + return 0; +} + +double HTMLMeterAccessible::CurValue() const { + double value = LeafAccessible::CurValue(); + double minValue = MinValue(); + + if (std::isnan(value)) { + /* If we didn't find a value from the LeafAccessible call above, check + * for a value attribute */ + nsAutoString attrValue; + if (!mContent->AsElement()->GetAttr(nsGkAtoms::value, attrValue)) { + return minValue; + } + + // If we find a value attribute, attempt to convert it to a double + nsresult error = NS_OK; + value = attrValue.ToDouble(&error); + if (NS_FAILED(error)) { + return minValue; + } + } + + /* If we end up with a defined value, verify it falls between + * our established min/max. Otherwise, snap it to the nearest boundary. */ + double maxValue = MaxValue(); + if (value > maxValue) { + value = maxValue; + } else if (value < minValue) { + value = minValue; + } + + return value; +} + +bool HTMLMeterAccessible::SetCurValue(double aValue) { + return false; // meters are readonly. +} + +void HTMLMeterAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + LeafAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::value) { + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_VALUE_CHANGE, this); + } +} diff --git a/accessible/html/HTMLFormControlAccessible.h b/accessible/html/HTMLFormControlAccessible.h new file mode 100644 index 0000000000..1e00bd4e40 --- /dev/null +++ b/accessible/html/HTMLFormControlAccessible.h @@ -0,0 +1,387 @@ +/* -*- 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/. */ + +#ifndef MOZILLA_A11Y_HTMLFormControlAccessible_H_ +#define MOZILLA_A11Y_HTMLFormControlAccessible_H_ + +#include "FormControlAccessible.h" +#include "HyperTextAccessible.h" +#include "mozilla/a11y/AccTypes.h" +#include "mozilla/dom/Element.h" +#include "AccAttributes.h" +#include "nsAccUtils.h" +#include "Relation.h" + +namespace mozilla { +class EditorBase; +namespace a11y { + +/** + * Accessible for HTML input@type="radio" element. + */ +class HTMLRadioButtonAccessible : public RadioButtonAccessible { + public: + HTMLRadioButtonAccessible(nsIContent* aContent, DocAccessible* aDoc) + : RadioButtonAccessible(aContent, aDoc) { + // Ignore "RadioStateChange" DOM event in lieu of document observer + // state change notification. + mStateFlags |= eIgnoreDOMUIEvent; + mType = eHTMLRadioButtonType; + } + + // LocalAccessible + virtual uint64_t NativeState() const override; + virtual Relation RelationByType(RelationType aType) const override; + + protected: + virtual void GetPositionAndSetSize(int32_t* aPosInSet, + int32_t* aSetSize) override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + private: + Relation ComputeGroupAttributes(int32_t* aPosInSet, int32_t* aSetSize) const; +}; + +/** + * Accessible for HTML input@type="button", @type="submit", @type="image" + * and HTML button elements. + */ +class HTMLButtonAccessible : public HyperTextAccessible { + public: + enum { eAction_Click = 0 }; + + HTMLButtonAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + + // Widgets + virtual bool IsWidget() const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; +}; + +/** + * Accessible for HTML input@type="text", input@type="password", textarea + * and other HTML text controls. + */ +class HTMLTextFieldAccessible : public HyperTextAccessible { + public: + enum { eAction_Click = 0 }; + + HTMLTextFieldAccessible(nsIContent* aContent, DocAccessible* aDoc); + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLTextFieldAccessible, + HyperTextAccessible) + + // HyperTextAccessible + MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual already_AddRefed<EditorBase> GetEditor() + const override; + + // LocalAccessible + virtual void Value(nsString& aValue) const override; + virtual void ApplyARIAState(uint64_t* aState) const override; + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual already_AddRefed<AccAttributes> NativeAttributes() override; + virtual bool AttributeChangesState(nsAtom* aAttribute) override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // Widgets + virtual bool IsWidget() const override; + virtual LocalAccessible* ContainerWidget() const override; + + protected: + virtual ~HTMLTextFieldAccessible() {} + + // LocalAccessible + virtual ENameValueFlag Name(nsString& aName) const override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; +}; + +/** + * Accessible for input@type="file" element. + */ +class HTMLFileInputAccessible : public HyperTextAccessible { + public: + HTMLFileInputAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual mozilla::a11y::role NativeRole() const override; + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + virtual ENameValueFlag Name(nsString& aName) const override; + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool IsWidget() const override; +}; + +/** + * Used for HTML input@type="number". + */ +class HTMLSpinnerAccessible final : public HTMLTextFieldAccessible { + public: + HTMLSpinnerAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HTMLTextFieldAccessible(aContent, aDoc) { + mGenericTypes |= eNumericValue; + } + + // LocalAccessible + virtual mozilla::a11y::role NativeRole() const override; + virtual void Value(nsString& aValue) const override; + + virtual double MaxValue() const override; + virtual double MinValue() const override; + virtual double CurValue() const override; + virtual double Step() const override; + virtual bool SetCurValue(double aValue) override; +}; + +/** + * Used for input@type="range" element. + */ +class HTMLRangeAccessible : public LeafAccessible { + public: + HTMLRangeAccessible(nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + mGenericTypes |= eNumericValue; + } + + // LocalAccessible + virtual void Value(nsString& aValue) const override; + virtual mozilla::a11y::role NativeRole() const override; + + // Value + virtual double MaxValue() const override; + virtual double MinValue() const override; + virtual double CurValue() const override; + virtual double Step() const override; + virtual bool SetCurValue(double aValue) override; + + // Widgets + virtual bool IsWidget() const override; +}; + +/** + * Accessible for HTML fieldset element. + */ +class HTMLGroupboxAccessible : public HyperTextAccessible { + public: + HTMLGroupboxAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual mozilla::a11y::role NativeRole() const override; + virtual Relation RelationByType(RelationType aType) const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + // HTMLGroupboxAccessible + nsIContent* GetLegend() const; +}; + +/** + * Accessible for HTML legend element. + */ +class HTMLLegendAccessible : public HyperTextAccessible { + public: + HTMLLegendAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual Relation RelationByType(RelationType aType) const override; +}; + +/** + * Accessible for HTML5 figure element. + */ +class HTMLFigureAccessible : public HyperTextAccessible { + public: + HTMLFigureAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual Relation RelationByType(RelationType aType) const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + // HTMLLegendAccessible + nsIContent* Caption() const; +}; + +/** + * Accessible for HTML5 figcaption element. + */ +class HTMLFigcaptionAccessible : public HyperTextAccessible { + public: + HTMLFigcaptionAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual Relation RelationByType(RelationType aType) const override; +}; + +/** + * Used for HTML form element. + */ +class HTMLFormAccessible : public HyperTextAccessible { + public: + HTMLFormAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLFormAccessible, HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + + protected: + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + virtual ~HTMLFormAccessible() = default; +}; + +/** + * Accessible for HTML progress element. + */ + +class HTMLProgressAccessible : public LeafAccessible { + public: + HTMLProgressAccessible(nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + // Ignore 'ValueChange' DOM event in lieu of @value attribute change + // notifications. + mStateFlags |= eIgnoreDOMUIEvent; + mGenericTypes |= eNumericValue; + mType = eProgressType; + } + + // LocalAccessible + virtual void Value(nsString& aValue) const override; + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // Value + virtual double MaxValue() const override; + virtual double MinValue() const override; + virtual double CurValue() const override; + virtual double Step() const override; + virtual bool SetCurValue(double aValue) override; + + // Widgets + virtual bool IsWidget() const override; + + protected: + virtual ~HTMLProgressAccessible() {} + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; +}; + +/** + * Accessible for HTML meter element. + */ + +class HTMLMeterAccessible : public LeafAccessible { + public: + HTMLMeterAccessible(nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + // Ignore 'ValueChange' DOM event in lieu of @value attribute change + // notifications. + mStateFlags |= eIgnoreDOMUIEvent; + mGenericTypes |= eNumericValue; + mType = eProgressType; + } + + // LocalAccessible + virtual void Value(nsString& aValue) const override; + virtual mozilla::a11y::role NativeRole() const override; + + // Value + virtual double MaxValue() const override; + virtual double MinValue() const override; + virtual double CurValue() const override; + virtual bool SetCurValue(double aValue) override; + + // Widgets + virtual bool IsWidget() const override; + + protected: + virtual ~HTMLMeterAccessible() {} + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; +}; + +/** + * Accessible for HTML date/time inputs. + */ +template <a11y::role R> +class HTMLDateTimeAccessible : public HyperTextAccessible { + public: + HTMLDateTimeAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLDateTimeFieldType; + } + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLDateTimeAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual mozilla::a11y::role NativeRole() const override { return R; } + virtual already_AddRefed<AccAttributes> NativeAttributes() override { + RefPtr<AccAttributes> attributes = HyperTextAccessible::NativeAttributes(); + // Unfortunately, an nsStaticAtom can't be passed as a + // template argument, so fetch the type from the DOM. + if (const nsAttrValue* attr = + mContent->AsElement()->GetParsedAttr(nsGkAtoms::type)) { + RefPtr<nsAtom> inputType = attr->GetAsAtom(); + if (inputType) { + attributes->SetAttribute(nsGkAtoms::textInputType, inputType); + } + } + return attributes.forget(); + } + + // Widgets + virtual bool IsWidget() const override { return true; } + + protected: + virtual ~HTMLDateTimeAccessible() {} +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLImageMapAccessible.cpp b/accessible/html/HTMLImageMapAccessible.cpp new file mode 100644 index 0000000000..14dd485875 --- /dev/null +++ b/accessible/html/HTMLImageMapAccessible.cpp @@ -0,0 +1,200 @@ +/* -*- 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 "HTMLImageMapAccessible.h" + +#include "ARIAMap.h" +#include "EventTree.h" +#include "mozilla/a11y/Role.h" + +#include "nsCoreUtils.h" +#include "nsIFrame.h" +#include "nsImageFrame.h" +#include "nsImageMap.h" +#include "nsLayoutUtils.h" +#include "mozilla/dom/HTMLAreaElement.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLImageMapAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLImageMapAccessible::HTMLImageMapAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : ImageAccessible(aContent, aDoc) { + mType = eImageMapType; + + UpdateChildAreas(false); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLImageMapAccessible: LocalAccessible public + +role HTMLImageMapAccessible::NativeRole() const { return roles::IMAGE_MAP; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLImageMapAccessible: public + +void HTMLImageMapAccessible::UpdateChildAreas(bool aDoFireEvents) { + if (!mContent || !mContent->GetPrimaryFrame()) { + return; + } + nsImageFrame* imageFrame = do_QueryFrame(mContent->GetPrimaryFrame()); + + // If image map is not initialized yet then we trigger one time more later. + nsImageMap* imageMapObj = imageFrame->GetExistingImageMap(); + if (!imageMapObj) return; + + TreeMutation mt(this, TreeMutation::kNoEvents & !aDoFireEvents); + + // Remove areas that are not a valid part of the image map anymore. + for (int32_t childIdx = mChildren.Length() - 1; childIdx >= 0; childIdx--) { + LocalAccessible* area = mChildren.ElementAt(childIdx); + if (area->GetContent()->GetPrimaryFrame()) continue; + + mt.BeforeRemoval(area); + RemoveChild(area); + } + + // Insert new areas into the tree. + uint32_t areaElmCount = imageMapObj->AreaCount(); + for (uint32_t idx = 0; idx < areaElmCount; idx++) { + nsIContent* areaContent = imageMapObj->GetAreaAt(idx); + LocalAccessible* area = mChildren.SafeElementAt(idx); + if (!area || area->GetContent() != areaContent) { + RefPtr<LocalAccessible> area = new HTMLAreaAccessible(areaContent, mDoc); + mDoc->BindToDocument(area, aria::GetRoleMap(areaContent->AsElement())); + + if (!InsertChildAt(idx, area)) { + mDoc->UnbindFromDocument(area); + break; + } + + mt.AfterInsertion(area); + } + } + + mt.Done(); +} + +LocalAccessible* HTMLImageMapAccessible::GetChildAccessibleFor( + const nsINode* aNode) const { + uint32_t length = mChildren.Length(); + for (uint32_t i = 0; i < length; i++) { + LocalAccessible* area = mChildren[i]; + if (area->GetContent() == aNode) return area; + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLAreaAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLAreaAccessible::HTMLAreaAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HTMLLinkAccessible(aContent, aDoc) { + // Make HTML area DOM element not accessible. HTML image map accessible + // manages its tree itself. + mStateFlags |= eNotNodeMapEntry; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLAreaAccessible: LocalAccessible + +role HTMLAreaAccessible::NativeRole() const { + // A link element without an href attribute and without a click listener + // should be reported as a generic. + if (mContent->IsElement()) { + dom::Element* element = mContent->AsElement(); + if (!element->HasAttr(nsGkAtoms::href) && + !nsCoreUtils::HasClickListener(element)) { + return roles::TEXT; + } + } + return HTMLLinkAccessible::NativeRole(); +} + +ENameValueFlag HTMLAreaAccessible::NativeName(nsString& aName) const { + ENameValueFlag nameFlag = LocalAccessible::NativeName(aName); + if (!aName.IsEmpty()) return nameFlag; + + if (!mContent->AsElement()->GetAttr(nsGkAtoms::alt, aName)) { + Value(aName); + } + + return eNameOK; +} + +void HTMLAreaAccessible::Description(nsString& aDescription) const { + aDescription.Truncate(); + + // Still to do - follow IE's standard here + RefPtr<dom::HTMLAreaElement> area = + dom::HTMLAreaElement::FromNodeOrNull(mContent); + if (area) area->GetShape(aDescription); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLAreaAccessible: LocalAccessible public + +LocalAccessible* HTMLAreaAccessible::LocalChildAtPoint( + int32_t aX, int32_t aY, EWhichChildAtPoint aWhichChild) { + // Don't walk into area accessibles. + return this; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLImageMapAccessible: HyperLinkAccessible + +uint32_t HTMLAreaAccessible::StartOffset() { + // Image map accessible is not hypertext accessible therefore + // StartOffset/EndOffset implementations of LocalAccessible doesn't work here. + // We return index in parent because image map contains area links only which + // are embedded objects. + // XXX: image map should be a hypertext accessible. + return IndexInParent(); +} + +uint32_t HTMLAreaAccessible::EndOffset() { return IndexInParent() + 1; } + +nsRect HTMLAreaAccessible::RelativeBounds(nsIFrame** aBoundingFrame) const { + nsIFrame* frame = GetFrame(); + if (!frame) return nsRect(); + + nsImageFrame* imageFrame = do_QueryFrame(frame); + nsImageMap* map = imageFrame->GetImageMap(); + + nsRect bounds; + nsresult rv = map->GetBoundsForAreaContent(mContent, bounds); + + if (NS_FAILED(rv)) return nsRect(); + + // XXX Areas are screwy; they return their rects as a pair of points, one pair + // stored into the width and height. + *aBoundingFrame = frame; + bounds.SizeTo(bounds.Width() - bounds.X(), bounds.Height() - bounds.Y()); + return bounds; +} + +nsRect HTMLAreaAccessible::ParentRelativeBounds() { + nsIFrame* boundingFrame = nullptr; + nsRect relativeBoundsRect = RelativeBounds(&boundingFrame); + if (MOZ_UNLIKELY(!boundingFrame)) { + // Area is not attached to an image map? + return nsRect(); + } + + // The relative bounds returned above are relative to this area's + // image map, which is technically already "parent relative". + // Because area elements are `display:none` to layout, they can't + // have transforms or other styling applied directly, and so we + // don't apply any additional transforms here. Any transform + // at the image map layer will be taken care of when computing bounds + // in the parent process. + return relativeBoundsRect; +} diff --git a/accessible/html/HTMLImageMapAccessible.h b/accessible/html/HTMLImageMapAccessible.h new file mode 100644 index 0000000000..15c56b44de --- /dev/null +++ b/accessible/html/HTMLImageMapAccessible.h @@ -0,0 +1,82 @@ +/* -*- 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/. */ + +#ifndef mozilla_a11y_HTMLImageMapAccessible_h__ +#define mozilla_a11y_HTMLImageMapAccessible_h__ + +#include "HTMLLinkAccessible.h" +#include "ImageAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * Used for HTML image maps. + */ +class HTMLImageMapAccessible final : public ImageAccessible { + public: + HTMLImageMapAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // nsISupports and cycle collector + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLImageMapAccessible, ImageAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + + /** + * Update area children of the image map. + */ + void UpdateChildAreas(bool aDoFireEvents = true); + + /** + * Return accessible of child node. + */ + LocalAccessible* GetChildAccessibleFor(const nsINode* aNode) const; + + protected: + virtual ~HTMLImageMapAccessible() {} +}; + +/** + * Accessible for image map areas - must be child of image. + */ +class HTMLAreaAccessible final : public HTMLLinkAccessible { + public: + HTMLAreaAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual void Description(nsString& aDescription) const override; + virtual LocalAccessible* LocalChildAtPoint( + int32_t aX, int32_t aY, EWhichChildAtPoint aWhichChild) override; + virtual nsRect RelativeBounds(nsIFrame** aBoundingFrame) const override; + virtual nsRect ParentRelativeBounds() override; + + // HyperLinkAccessible + virtual uint32_t StartOffset() override; + virtual uint32_t EndOffset() override; + + virtual bool IsAcceptableChild(nsIContent* aEl) const override { + return false; + } + + // LocalAccessible + virtual role NativeRole() const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +//////////////////////////////////////////////////////////////////////////////// +// LocalAccessible downcasting method + +inline HTMLImageMapAccessible* LocalAccessible::AsImageMap() { + return IsImageMap() ? static_cast<HTMLImageMapAccessible*>(this) : nullptr; +} + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLLinkAccessible.cpp b/accessible/html/HTMLLinkAccessible.cpp new file mode 100644 index 0000000000..b3549996fc --- /dev/null +++ b/accessible/html/HTMLLinkAccessible.cpp @@ -0,0 +1,129 @@ +/* -*- 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 "HTMLLinkAccessible.h" + +#include "CacheConstants.h" +#include "nsCoreUtils.h" +#include "mozilla/a11y/Role.h" +#include "States.h" + +#include "nsContentUtils.h" +#include "mozilla/a11y/DocAccessible.h" +#include "mozilla/dom/Element.h" +#include "mozilla/dom/MutationEventBinding.h" + +using namespace mozilla; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLLinkAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLLinkAccessible::HTMLLinkAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLLinkType; +} + +//////////////////////////////////////////////////////////////////////////////// +// nsIAccessible + +role HTMLLinkAccessible::NativeRole() const { return roles::LINK; } + +uint64_t HTMLLinkAccessible::NativeState() const { + return HyperTextAccessible::NativeState() & ~states::READONLY; +} + +uint64_t HTMLLinkAccessible::NativeLinkState() const { + dom::ElementState state = mContent->AsElement()->State(); + if (state.HasState(dom::ElementState::UNVISITED)) { + return states::LINKED; + } + + if (state.HasState(dom::ElementState::VISITED)) { + return states::LINKED | states::TRAVERSED; + } + + // This is a either named anchor (a link with also a name attribute) or + // it doesn't have any attributes. Check if 'click' event handler is + // registered, otherwise bail out. + return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0; +} + +uint64_t HTMLLinkAccessible::NativeInteractiveState() const { + uint64_t state = HyperTextAccessible::NativeInteractiveState(); + + // This is how we indicate it is a named anchor. In other words, this anchor + // can be selected as a location :) There is no other better state to use to + // indicate this. + if (mContent->AsElement()->HasAttr(nsGkAtoms::name)) { + state |= states::SELECTABLE; + } + + return state; +} + +void HTMLLinkAccessible::Value(nsString& aValue) const { + aValue.Truncate(); + + HyperTextAccessible::Value(aValue); + if (aValue.IsEmpty()) { + nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue); + } +} + +bool HTMLLinkAccessible::HasPrimaryAction() const { + return IsLinked() || HyperTextAccessible::HasPrimaryAction(); + ; +} + +void HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + aName.Truncate(); + + if (!IsLinked()) { + HyperTextAccessible::ActionNameAt(aIndex, aName); + return; + } + + // Action 0 (default action): Jump to link + if (aIndex == eAction_Jump) aName.AssignLiteral("jump"); +} + +bool HTMLLinkAccessible::AttributeChangesState(nsAtom* aAttribute) { + return aAttribute == nsGkAtoms::href || + HyperTextAccessible::AttributeChangesState(aAttribute); +} + +void HTMLLinkAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::href && + (aModType == dom::MutationEvent_Binding::ADDITION || + aModType == dom::MutationEvent_Binding::REMOVAL)) { + mDoc->QueueCacheUpdate(this, CacheDomain::Actions); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HyperLinkAccessible + +bool HTMLLinkAccessible::IsLink() const { + // Expose HyperLinkAccessible unconditionally. + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLLinkAccessible + +bool HTMLLinkAccessible::IsLinked() const { + dom::ElementState state = mContent->AsElement()->State(); + return state.HasAtLeastOneOfStates(dom::ElementState::VISITED_OR_UNVISITED); +} diff --git a/accessible/html/HTMLLinkAccessible.h b/accessible/html/HTMLLinkAccessible.h new file mode 100644 index 0000000000..de5f903a3d --- /dev/null +++ b/accessible/html/HTMLLinkAccessible.h @@ -0,0 +1,59 @@ +/* -*- 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/. */ + +#ifndef mozilla_a11y_HTMLLinkAccessible_h__ +#define mozilla_a11y_HTMLLinkAccessible_h__ + +#include "HyperTextAccessible.h" + +namespace mozilla { +namespace a11y { + +class HTMLLinkAccessible : public HyperTextAccessible { + public: + HTMLLinkAccessible(nsIContent* aContent, DocAccessible* aDoc); + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLLinkAccessible, HyperTextAccessible) + + // LocalAccessible + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeLinkState() const override; + virtual uint64_t NativeInteractiveState() const override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + + // HyperLinkAccessible + virtual bool IsLink() const override; + + /** + * Returns true if the link has href attribute. + */ + bool IsLinked() const; + + protected: + virtual ~HTMLLinkAccessible() {} + + virtual bool AttributeChangesState(nsAtom* aAttribute) override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + enum { eAction_Jump = 0 }; +}; + +inline HTMLLinkAccessible* LocalAccessible::AsHTMLLink() { + return IsHTMLLink() ? static_cast<HTMLLinkAccessible*>(this) : nullptr; +} + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLListAccessible.cpp b/accessible/html/HTMLListAccessible.cpp new file mode 100644 index 0000000000..d7a2fc23ae --- /dev/null +++ b/accessible/html/HTMLListAccessible.cpp @@ -0,0 +1,112 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* 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 "HTMLListAccessible.h" + +#include "AccAttributes.h" +#include "nsAccessibilityService.h" +#include "mozilla/a11y/Role.h" +#include "States.h" + +#include "nsLayoutUtils.h" + +using namespace mozilla; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLListAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLListAccessible::NativeRole() const { + a11y::role r = GetAccService()->MarkupRole(mContent); + return r != roles::NOTHING ? r : roles::LIST; +} + +uint64_t HTMLListAccessible::NativeState() const { + return HyperTextAccessible::NativeState() | states::READONLY; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLLIAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLLIAccessible::HTMLLIAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLLiType; +} + +role HTMLLIAccessible::NativeRole() const { + a11y::role r = GetAccService()->MarkupRole(mContent); + return r != roles::NOTHING ? r : roles::LISTITEM; +} + +uint64_t HTMLLIAccessible::NativeState() const { + return HyperTextAccessible::NativeState() | states::READONLY; +} + +nsRect HTMLLIAccessible::BoundsInAppUnits() const { + nsRect rect = AccessibleWrap::BoundsInAppUnits(); + + LocalAccessible* bullet = Bullet(); + nsIFrame* frame = GetFrame(); + MOZ_ASSERT(!(bullet && !frame), "Cannot have a bullet if there is no frame"); + + if (bullet && frame && + frame->StyleList()->mListStylePosition != + StyleListStylePosition::Inside) { + nsRect bulletRect = bullet->BoundsInAppUnits(); + return rect.Union(bulletRect); + } + + return rect; +} + +LocalAccessible* HTMLLIAccessible::Bullet() const { + LocalAccessible* firstChild = LocalFirstChild(); + if (firstChild && firstChild->NativeRole() == roles::LISTITEM_MARKER) { + return firstChild; + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLListBulletAccessible +//////////////////////////////////////////////////////////////////////////////// +HTMLListBulletAccessible::HTMLListBulletAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + mGenericTypes |= eText; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLListBulletAccessible: LocalAccessible + +ENameValueFlag HTMLListBulletAccessible::Name(nsString& aName) const { + nsLayoutUtils::GetMarkerSpokenText(mContent, aName); + return eNameOK; +} + +role HTMLListBulletAccessible::NativeRole() const { + return roles::LISTITEM_MARKER; +} + +uint64_t HTMLListBulletAccessible::NativeState() const { + return LeafAccessible::NativeState() | states::READONLY; +} + +already_AddRefed<AccAttributes> HTMLListBulletAccessible::NativeAttributes() { + RefPtr<AccAttributes> attributes = new AccAttributes(); + return attributes.forget(); +} + +void HTMLListBulletAccessible::AppendTextTo(nsAString& aText, + uint32_t aStartOffset, + uint32_t aLength) { + nsAutoString bulletText; + Name(bulletText); + aText.Append(Substring(bulletText, aStartOffset, aLength)); +} diff --git a/accessible/html/HTMLListAccessible.h b/accessible/html/HTMLListAccessible.h new file mode 100644 index 0000000000..ac48ad2380 --- /dev/null +++ b/accessible/html/HTMLListAccessible.h @@ -0,0 +1,85 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef mozilla_a11y_HTMLListAccessible_h__ +#define mozilla_a11y_HTMLListAccessible_h__ + +#include "BaseAccessibles.h" +#include "HyperTextAccessible.h" + +namespace mozilla { +namespace a11y { + +class HTMLListBulletAccessible; + +/** + * Used for HTML list (like HTML ul). + */ +class HTMLListAccessible : public HyperTextAccessible { + public: + HTMLListAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mGenericTypes |= eList; + } + + // nsISupports + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLListAccessible, HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + protected: + virtual ~HTMLListAccessible() {} +}; + +/** + * Used for HTML list item (e.g. HTML li). + */ +class HTMLLIAccessible : public HyperTextAccessible { + public: + HTMLLIAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // nsISupports + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLLIAccessible, HyperTextAccessible) + + // LocalAccessible + virtual nsRect BoundsInAppUnits() const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // HTMLLIAccessible + LocalAccessible* Bullet() const; + + protected: + virtual ~HTMLLIAccessible() {} +}; + +/** + * Used for bullet of HTML list item element (for example, HTML li). + */ +class HTMLListBulletAccessible : public LeafAccessible { + public: + HTMLListBulletAccessible(nsIContent* aContent, DocAccessible* aDoc); + virtual ~HTMLListBulletAccessible() {} + + // LocalAccessible + virtual ENameValueFlag Name(nsString& aName) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual already_AddRefed<AccAttributes> NativeAttributes() override; + virtual void AppendTextTo(nsAString& aText, uint32_t aStartOffset = 0, + uint32_t aLength = UINT32_MAX) override; +}; + +inline HTMLLIAccessible* LocalAccessible::AsHTMLListItem() { + return IsHTMLListItem() ? static_cast<HTMLLIAccessible*>(this) : nullptr; +} + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLSelectAccessible.cpp b/accessible/html/HTMLSelectAccessible.cpp new file mode 100644 index 0000000000..f8fb4180c7 --- /dev/null +++ b/accessible/html/HTMLSelectAccessible.cpp @@ -0,0 +1,472 @@ +/* -*- Mode: C++; tab-width: 4; 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 "HTMLSelectAccessible.h" + +#include "LocalAccessible-inl.h" +#include "nsAccessibilityService.h" +#include "nsAccUtils.h" +#include "DocAccessible.h" +#include "mozilla/a11y/Role.h" +#include "States.h" + +#include "nsCOMPtr.h" +#include "mozilla/dom/HTMLOptionElement.h" +#include "mozilla/dom/HTMLOptGroupElement.h" +#include "mozilla/dom/HTMLSelectElement.h" +#include "nsComboboxControlFrame.h" +#include "nsContainerFrame.h" +#include "nsListControlFrame.h" + +using namespace mozilla::a11y; +using namespace mozilla::dom; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectListAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLSelectListAccessible::HTMLSelectListAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mGenericTypes |= eListControl | eSelect; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectListAccessible: LocalAccessible public + +uint64_t HTMLSelectListAccessible::NativeState() const { + uint64_t state = AccessibleWrap::NativeState(); + if (mContent->AsElement()->HasAttr(nsGkAtoms::multiple)) { + state |= states::MULTISELECTABLE | states::EXTSELECTABLE; + } + + return state; +} + +role HTMLSelectListAccessible::NativeRole() const { return roles::LISTBOX; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectListAccessible: SelectAccessible + +bool HTMLSelectListAccessible::SelectAll() { + return mContent->AsElement()->HasAttr(nsGkAtoms::multiple) + ? AccessibleWrap::SelectAll() + : false; +} + +bool HTMLSelectListAccessible::UnselectAll() { + return mContent->AsElement()->HasAttr(nsGkAtoms::multiple) + ? AccessibleWrap::UnselectAll() + : false; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectListAccessible: Widgets + +bool HTMLSelectListAccessible::IsWidget() const { return true; } + +bool HTMLSelectListAccessible::IsActiveWidget() const { + return FocusMgr()->HasDOMFocus(mContent); +} + +bool HTMLSelectListAccessible::AreItemsOperable() const { return true; } + +LocalAccessible* HTMLSelectListAccessible::CurrentItem() const { + nsListControlFrame* listControlFrame = do_QueryFrame(GetFrame()); + if (listControlFrame) { + nsCOMPtr<nsIContent> activeOptionNode = + listControlFrame->GetCurrentOption(); + if (activeOptionNode) { + DocAccessible* document = Document(); + if (document) return document->GetAccessible(activeOptionNode); + } + } + return nullptr; +} + +void HTMLSelectListAccessible::SetCurrentItem(const LocalAccessible* aItem) { + if (!aItem->GetContent()->IsElement()) return; + + aItem->GetContent()->AsElement()->SetAttr( + kNameSpaceID_None, nsGkAtoms::selected, u"true"_ns, true); +} + +bool HTMLSelectListAccessible::IsAcceptableChild(nsIContent* aEl) const { + return aEl->IsAnyOfHTMLElements(nsGkAtoms::option, nsGkAtoms::optgroup); +} + +bool HTMLSelectListAccessible::AttributeChangesState(nsAtom* aAttribute) { + return aAttribute == nsGkAtoms::multiple || + LocalAccessible::AttributeChangesState(aAttribute); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectOptionAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLSelectOptionAccessible::HTMLSelectOptionAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) {} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectOptionAccessible: LocalAccessible public + +role HTMLSelectOptionAccessible::NativeRole() const { + if (GetCombobox()) return roles::COMBOBOX_OPTION; + + return roles::OPTION; +} + +ENameValueFlag HTMLSelectOptionAccessible::NativeName(nsString& aName) const { + if (auto* option = dom::HTMLOptionElement::FromNode(mContent)) { + option->GetAttr(nsGkAtoms::label, aName); + if (!aName.IsEmpty()) { + return eNameOK; + } + option->GetText(aName); + return eNameFromSubtree; + } + if (auto* group = dom::HTMLOptGroupElement::FromNode(mContent)) { + group->GetLabel(aName); + return aName.IsEmpty() ? eNameOK : eNameFromSubtree; + } + MOZ_ASSERT_UNREACHABLE("What content do we have?"); + return eNameFromSubtree; +} + +void HTMLSelectOptionAccessible::DOMAttributeChanged( + int32_t aNameSpaceID, nsAtom* aAttribute, int32_t aModType, + const nsAttrValue* aOldValue, uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::label) { + dom::Element* elm = Elm(); + if (!nsAccUtils::HasARIAAttr(elm, nsGkAtoms::aria_labelledby) && + !nsAccUtils::HasARIAAttr(elm, nsGkAtoms::aria_label)) { + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); + } + } +} + +uint64_t HTMLSelectOptionAccessible::NativeState() const { + // As a HTMLSelectOptionAccessible we can have the following states: + // SELECTABLE, SELECTED, FOCUSED, FOCUSABLE, OFFSCREEN + // Upcall to LocalAccessible, but skip HyperTextAccessible impl + // because we don't want EDITABLE or SELECTABLE_TEXT + uint64_t state = LocalAccessible::NativeState(); + + LocalAccessible* select = GetSelect(); + if (!select) return state; + + uint64_t selectState = select->State(); + if (selectState & states::INVISIBLE) return state; + + // Are we selected? + HTMLOptionElement* option = HTMLOptionElement::FromNode(mContent); + bool selected = option && option->Selected(); + if (selected) state |= states::SELECTED; + + if (selectState & states::OFFSCREEN) { + state |= states::OFFSCREEN; + } else if (selectState & states::COLLAPSED) { + // <select> is COLLAPSED: add OFFSCREEN, if not the currently + // visible option + if (!selected) { + state |= states::OFFSCREEN; + // Ensure the invisible state is removed. Otherwise, group info will skip + // this option. Furthermore, this gets cached and this doesn't get + // invalidated even once the select is expanded. + state &= ~states::INVISIBLE; + } else { + // Clear offscreen and invisible for currently showing option + state &= ~(states::OFFSCREEN | states::INVISIBLE); + state |= selectState & states::OPAQUE1; + } + } else { + // XXX list frames are weird, don't rely on LocalAccessible's general + // visibility implementation unless they get reimplemented in layout + state &= ~states::OFFSCREEN; + // <select> is not collapsed: compare bounds to calculate OFFSCREEN + LocalAccessible* listAcc = LocalParent(); + if (listAcc) { + LayoutDeviceIntRect optionRect = Bounds(); + LayoutDeviceIntRect listRect = listAcc->Bounds(); + if (optionRect.Y() < listRect.Y() || + optionRect.YMost() > listRect.YMost()) { + state |= states::OFFSCREEN; + } + } + } + + return state; +} + +uint64_t HTMLSelectOptionAccessible::NativeInteractiveState() const { + return NativelyUnavailable() ? states::UNAVAILABLE + : states::FOCUSABLE | states::SELECTABLE; +} + +nsRect HTMLSelectOptionAccessible::RelativeBounds( + nsIFrame** aBoundingFrame) const { + LocalAccessible* combobox = GetCombobox(); + if (combobox && (combobox->State() & states::COLLAPSED)) { + return combobox->RelativeBounds(aBoundingFrame); + } + + return HyperTextAccessible::RelativeBounds(aBoundingFrame); +} + +void HTMLSelectOptionAccessible::ActionNameAt(uint8_t aIndex, + nsAString& aName) { + if (aIndex == eAction_Select) aName.AssignLiteral("select"); +} + +bool HTMLSelectOptionAccessible::HasPrimaryAction() const { return true; } + +void HTMLSelectOptionAccessible::SetSelected(bool aSelect) { + HTMLOptionElement* option = HTMLOptionElement::FromNode(mContent); + if (option) option->SetSelected(aSelect); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectOptionAccessible: Widgets + +LocalAccessible* HTMLSelectOptionAccessible::ContainerWidget() const { + LocalAccessible* parent = LocalParent(); + if (parent && parent->IsHTMLOptGroup()) { + parent = parent->LocalParent(); + } + + return parent && parent->IsListControl() ? parent : nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLSelectOptGroupAccessible +//////////////////////////////////////////////////////////////////////////////// + +role HTMLSelectOptGroupAccessible::NativeRole() const { + return roles::GROUPING; +} + +uint64_t HTMLSelectOptGroupAccessible::NativeInteractiveState() const { + return NativelyUnavailable() ? states::UNAVAILABLE : 0; +} + +bool HTMLSelectOptGroupAccessible::IsAcceptableChild(nsIContent* aEl) const { + return aEl->IsCharacterData() || aEl->IsHTMLElement(nsGkAtoms::option); +} + +bool HTMLSelectOptGroupAccessible::HasPrimaryAction() const { return false; } + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLComboboxAccessible::HTMLComboboxAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mType = eHTMLComboboxType; + mGenericTypes |= eCombobox; + mStateFlags |= eNoKidsFromDOM; + + if ((nsComboboxControlFrame*)do_QueryFrame(GetFrame())) { + mListAccessible = new HTMLComboboxListAccessible(mParent, mContent, mDoc); + Document()->BindToDocument(mListAccessible, nullptr); + AppendChild(mListAccessible); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxAccessible: LocalAccessible + +role HTMLComboboxAccessible::NativeRole() const { return roles::COMBOBOX; } + +bool HTMLComboboxAccessible::RemoveChild(LocalAccessible* aChild) { + MOZ_ASSERT(aChild == mListAccessible); + if (AccessibleWrap::RemoveChild(aChild)) { + mListAccessible = nullptr; + return true; + } + return false; +} + +void HTMLComboboxAccessible::Shutdown() { + MOZ_ASSERT(!mDoc || mDoc->IsDefunct() || !mListAccessible); + if (mListAccessible) { + mListAccessible->Shutdown(); + mListAccessible = nullptr; + } + + AccessibleWrap::Shutdown(); +} + +uint64_t HTMLComboboxAccessible::NativeState() const { + // As a HTMLComboboxAccessible we can have the following states: + // FOCUSED, FOCUSABLE, HASPOPUP, EXPANDED, COLLAPSED + // Get focus status from base class + uint64_t state = LocalAccessible::NativeState(); + + nsComboboxControlFrame* comboFrame = do_QueryFrame(GetFrame()); + if (comboFrame && comboFrame->IsDroppedDown()) { + state |= states::EXPANDED; + } else { + state |= states::COLLAPSED; + } + + state |= states::HASPOPUP; + return state; +} + +void HTMLComboboxAccessible::Description(nsString& aDescription) const { + aDescription.Truncate(); + // First check to see if combo box itself has a description, perhaps through + // tooltip (title attribute) or via aria-describedby + LocalAccessible::Description(aDescription); + if (!aDescription.IsEmpty()) return; + + // Otherwise use description of selected option. + LocalAccessible* option = SelectedOption(); + if (option) option->Description(aDescription); +} + +void HTMLComboboxAccessible::Value(nsString& aValue) const { + // Use accessible name of selected option. + LocalAccessible* option = SelectedOption(); + if (option) option->Name(aValue); +} + +bool HTMLComboboxAccessible::HasPrimaryAction() const { return true; } + +void HTMLComboboxAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex != HTMLComboboxAccessible::eAction_Click) return; + + nsComboboxControlFrame* comboFrame = do_QueryFrame(GetFrame()); + if (!comboFrame) return; + + if (comboFrame->IsDroppedDown()) { + aName.AssignLiteral("close"); + } else { + aName.AssignLiteral("open"); + } +} + +bool HTMLComboboxAccessible::IsAcceptableChild(nsIContent* aEl) const { + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxAccessible: Widgets + +bool HTMLComboboxAccessible::IsWidget() const { return true; } + +bool HTMLComboboxAccessible::IsActiveWidget() const { + return FocusMgr()->HasDOMFocus(mContent); +} + +bool HTMLComboboxAccessible::AreItemsOperable() const { + nsComboboxControlFrame* comboboxFrame = do_QueryFrame(GetFrame()); + return comboboxFrame && comboboxFrame->IsDroppedDown(); +} + +LocalAccessible* HTMLComboboxAccessible::CurrentItem() const { + return AreItemsOperable() ? mListAccessible->CurrentItem() : nullptr; +} + +void HTMLComboboxAccessible::SetCurrentItem(const LocalAccessible* aItem) { + if (AreItemsOperable()) mListAccessible->SetCurrentItem(aItem); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxAccessible: protected + +LocalAccessible* HTMLComboboxAccessible::SelectedOption() const { + HTMLSelectElement* select = HTMLSelectElement::FromNode(mContent); + int32_t selectedIndex = select->SelectedIndex(); + + if (selectedIndex >= 0) { + HTMLOptionElement* option = select->Item(selectedIndex); + if (option) { + DocAccessible* document = Document(); + if (document) return document->GetAccessible(option); + } + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxListAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLComboboxListAccessible::HTMLComboboxListAccessible(LocalAccessible* aParent, + nsIContent* aContent, + DocAccessible* aDoc) + : HTMLSelectListAccessible(aContent, aDoc) { + mStateFlags |= eSharedNode; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxAccessible: LocalAccessible + +role HTMLComboboxListAccessible::NativeRole() const { + return roles::COMBOBOX_LIST; +} + +uint64_t HTMLComboboxListAccessible::NativeState() const { + // As a HTMLComboboxListAccessible we can have the following states: + // FOCUSED, FOCUSABLE, FLOATING, INVISIBLE + // Get focus status from base class + uint64_t state = LocalAccessible::NativeState(); + + nsComboboxControlFrame* comboFrame = do_QueryFrame(mParent->GetFrame()); + if (comboFrame && comboFrame->IsDroppedDown()) { + state |= states::FLOATING; + } else { + state |= states::INVISIBLE; + } + + return state; +} + +nsRect HTMLComboboxListAccessible::RelativeBounds( + nsIFrame** aBoundingFrame) const { + *aBoundingFrame = nullptr; + + LocalAccessible* comboAcc = LocalParent(); + if (!comboAcc) return nsRect(); + + if (0 == (comboAcc->State() & states::COLLAPSED)) { + return HTMLSelectListAccessible::RelativeBounds(aBoundingFrame); + } + + // Get the first option. + nsIContent* content = mContent->GetFirstChild(); + if (!content) return nsRect(); + + nsIFrame* frame = content->GetPrimaryFrame(); + if (!frame) { + *aBoundingFrame = nullptr; + return nsRect(); + } + + *aBoundingFrame = frame->GetParent(); + return (*aBoundingFrame)->GetRect(); +} + +bool HTMLComboboxListAccessible::IsAcceptableChild(nsIContent* aEl) const { + return aEl->IsAnyOfHTMLElements(nsGkAtoms::option, nsGkAtoms::optgroup); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLComboboxListAccessible: Widgets + +bool HTMLComboboxListAccessible::IsActiveWidget() const { + return mParent && mParent->IsActiveWidget(); +} + +bool HTMLComboboxListAccessible::AreItemsOperable() const { + return mParent && mParent->AreItemsOperable(); +} diff --git a/accessible/html/HTMLSelectAccessible.h b/accessible/html/HTMLSelectAccessible.h new file mode 100644 index 0000000000..c496490e06 --- /dev/null +++ b/accessible/html/HTMLSelectAccessible.h @@ -0,0 +1,216 @@ +/* -*- Mode: C++; tab-width: 4; 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/. */ + +#ifndef mozilla_a11y_HTMLSelectAccessible_h__ +#define mozilla_a11y_HTMLSelectAccessible_h__ + +#include "HTMLFormControlAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * Selects, Listboxes and Comboboxes, are made up of a number of different + * widgets, some of which are shared between the two. This file contains + * all of the widgets for both of the Selects, for HTML only. + * + * Listbox: + * - HTMLSelectListAccessible + * - HTMLSelectOptionAccessible + * + * Comboboxes: + * - HTMLComboboxAccessible + * - HTMLComboboxListAccessible [ inserted in accessible tree ] + * - HTMLSelectOptionAccessible(s) + */ + +/* + * The list that contains all the options in the select. + */ +class HTMLSelectListAccessible : public AccessibleWrap { + public: + HTMLSelectListAccessible(nsIContent* aContent, DocAccessible* aDoc); + virtual ~HTMLSelectListAccessible() {} + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + virtual bool AttributeChangesState(nsAtom* aAttribute) override; + + // SelectAccessible + virtual bool SelectAll() override; + virtual bool UnselectAll() override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual LocalAccessible* CurrentItem() const override; + virtual void SetCurrentItem(const LocalAccessible* aItem) override; +}; + +/* + * Options inside the select, contained within the list + */ +class HTMLSelectOptionAccessible : public HyperTextAccessible { + public: + enum { eAction_Select = 0 }; + + HTMLSelectOptionAccessible(nsIContent* aContent, DocAccessible* aDoc); + virtual ~HTMLSelectOptionAccessible() {} + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + + virtual nsRect RelativeBounds(nsIFrame** aBoundingFrame) const override; + virtual void SetSelected(bool aSelect) override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + + // Widgets + virtual LocalAccessible* ContainerWidget() const override; + + protected: + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + private: + /** + * Return a select accessible the option belongs to if any. + */ + LocalAccessible* GetSelect() const { + LocalAccessible* parent = mParent; + if (parent && parent->IsHTMLOptGroup()) { + parent = parent->LocalParent(); + } + + if (parent && parent->IsListControl()) { + LocalAccessible* combobox = parent->LocalParent(); + return combobox && combobox->IsCombobox() ? combobox : mParent; + } + + return nullptr; + } + + /** + * Return a combobox accessible the option belongs to if any. + */ + LocalAccessible* GetCombobox() const { + LocalAccessible* parent = mParent; + if (parent && parent->IsHTMLOptGroup()) { + parent = parent->LocalParent(); + } + + if (parent && parent->IsListControl()) { + LocalAccessible* combobox = parent->LocalParent(); + return combobox && combobox->IsCombobox() ? combobox : nullptr; + } + + return nullptr; + } +}; + +/* + * Opt Groups inside the select, contained within the list + */ +class HTMLSelectOptGroupAccessible : public HTMLSelectOptionAccessible { + public: + HTMLSelectOptGroupAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HTMLSelectOptionAccessible(aContent, aDoc) { + mType = eHTMLOptGroupType; + } + virtual ~HTMLSelectOptGroupAccessible() {} + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeInteractiveState() const override; + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; +}; + +/** ------------------------------------------------------ */ +/** Finally, the Combobox widgets */ +/** ------------------------------------------------------ */ + +class HTMLComboboxListAccessible; + +/* + * A class the represents the HTML Combobox widget. + */ +class HTMLComboboxAccessible final : public AccessibleWrap { + public: + enum { eAction_Click = 0 }; + + HTMLComboboxAccessible(nsIContent* aContent, DocAccessible* aDoc); + virtual ~HTMLComboboxAccessible() {} + + // LocalAccessible + virtual void Shutdown() override; + virtual void Description(nsString& aDescription) const override; + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual bool RemoveChild(LocalAccessible* aChild) override; + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + + // ActionAccessible + virtual bool HasPrimaryAction() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual LocalAccessible* CurrentItem() const override; + virtual void SetCurrentItem(const LocalAccessible* aItem) override; + + HTMLComboboxListAccessible* List() const { return mListAccessible; } + + /** + * Return selected option. + */ + LocalAccessible* SelectedOption() const; + + private: + RefPtr<HTMLComboboxListAccessible> mListAccessible; +}; + +/* + * A class that represents the window that lives to the right + * of the drop down button inside the Select. This is the window + * that is made visible when the button is pressed. + */ +class HTMLComboboxListAccessible : public HTMLSelectListAccessible { + public: + HTMLComboboxListAccessible(LocalAccessible* aParent, nsIContent* aContent, + DocAccessible* aDoc); + virtual ~HTMLComboboxListAccessible() {} + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual nsRect RelativeBounds(nsIFrame** aBoundingFrame) const override; + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + + // Widgets + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/HTMLTableAccessible.cpp b/accessible/html/HTMLTableAccessible.cpp new file mode 100644 index 0000000000..2c3dc6b82d --- /dev/null +++ b/accessible/html/HTMLTableAccessible.cpp @@ -0,0 +1,712 @@ +/* -*- 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 "HTMLTableAccessible.h" + +#include <stdint.h> + +#include "nsAccessibilityService.h" +#include "AccAttributes.h" +#include "ARIAMap.h" +#include "CacheConstants.h" +#include "LocalAccessible-inl.h" +#include "nsTextEquivUtils.h" +#include "Relation.h" +#include "mozilla/a11y/Role.h" +#include "States.h" + +#include "mozilla/a11y/TableAccessible.h" +#include "mozilla/a11y/TableCellAccessible.h" +#include "mozilla/Assertions.h" +#include "mozilla/dom/Element.h" +#include "mozilla/dom/NameSpaceConstants.h" +#include "nsCaseTreatment.h" +#include "nsColor.h" +#include "nsCOMPtr.h" +#include "nsCoreUtils.h" +#include "nsDebug.h" +#include "nsIHTMLCollection.h" +#include "nsError.h" +#include "nsGkAtoms.h" +#include "nsLiteralString.h" +#include "nsMargin.h" +#include "nsQueryFrame.h" +#include "nsSize.h" +#include "nsStringFwd.h" +#include "nsTableCellFrame.h" +#include "nsTableWrapperFrame.h" + +using namespace mozilla; +using namespace mozilla::dom; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableCellAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLTableCellAccessible::HTMLTableCellAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLTableCellType; + mGenericTypes |= eTableCell; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableCellAccessible: LocalAccessible implementation + +role HTMLTableCellAccessible::NativeRole() const { + // We implement this rather than using the markup maps because we only want + // this role to be returned if this is a valid cell. An invalid cell (e.g. if + // the table has role="none") won't use this class, so it will get a generic + // role, since the markup map doesn't specify a role. + if (mContent->IsMathMLElement(nsGkAtoms::mtd_)) { + return roles::MATHML_CELL; + } + return roles::CELL; +} + +uint64_t HTMLTableCellAccessible::NativeState() const { + uint64_t state = HyperTextAccessible::NativeState(); + + nsIFrame* frame = mContent->GetPrimaryFrame(); + NS_ASSERTION(frame, "No frame for valid cell accessible!"); + + if (frame && frame->IsSelected()) { + state |= states::SELECTED; + } + + return state; +} + +uint64_t HTMLTableCellAccessible::NativeInteractiveState() const { + return HyperTextAccessible::NativeInteractiveState() | states::SELECTABLE; +} + +already_AddRefed<AccAttributes> HTMLTableCellAccessible::NativeAttributes() { + RefPtr<AccAttributes> attributes = HyperTextAccessible::NativeAttributes(); + + // We only need to expose table-cell-index to clients. If we're in the content + // process, we don't need this, so building a CachedTableAccessible is very + // wasteful. This will be exposed by RemoteAccessible in the parent process + // instead. + if (!IPCAccessibilityActive()) { + if (const TableCellAccessible* cell = AsTableCell()) { + TableAccessible* table = cell->Table(); + const uint32_t row = cell->RowIdx(); + const uint32_t col = cell->ColIdx(); + const int32_t cellIdx = table->CellIndexAt(row, col); + if (cellIdx != -1) { + attributes->SetAttribute(nsGkAtoms::tableCellIndex, cellIdx); + } + } + } + + // abbr attribute + + // Pick up object attribute from abbr DOM element (a child of the cell) or + // from abbr DOM attribute. + nsString abbrText; + if (ChildCount() == 1) { + LocalAccessible* abbr = LocalFirstChild(); + if (abbr->IsAbbreviation()) { + nsIContent* firstChildNode = abbr->GetContent()->GetFirstChild(); + if (firstChildNode) { + nsTextEquivUtils::AppendTextEquivFromTextContent(firstChildNode, + &abbrText); + } + } + } + if (abbrText.IsEmpty()) { + mContent->AsElement()->GetAttr(nsGkAtoms::abbr, abbrText); + } + + if (!abbrText.IsEmpty()) { + attributes->SetAttribute(nsGkAtoms::abbr, std::move(abbrText)); + } + + // axis attribute + nsString axisText; + mContent->AsElement()->GetAttr(nsGkAtoms::axis, axisText); + if (!axisText.IsEmpty()) { + attributes->SetAttribute(nsGkAtoms::axis, std::move(axisText)); + } + + return attributes.forget(); +} + +void HTMLTableCellAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::headers || aAttribute == nsGkAtoms::abbr || + aAttribute == nsGkAtoms::scope) { + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_OBJECT_ATTRIBUTE_CHANGED, + this); + if (HTMLTableAccessible* table = Table()) { + // Modifying these attributes can also modify our table's classification + // as either a layout or data table. Queue an update on the table itself + // to re-compute our "layout guess" + mDoc->QueueCacheUpdate(table, CacheDomain::Table); + } + mDoc->QueueCacheUpdate(this, CacheDomain::Table); + } else if (aAttribute == nsGkAtoms::rowspan || + aAttribute == nsGkAtoms::colspan) { + if (HTMLTableAccessible* table = Table()) { + // Modifying these attributes can also modify our table's classification + // as either a layout or data table. Queue an update on the table itself + // to re-compute our "layout guess" + mDoc->QueueCacheUpdate(table, CacheDomain::Table); + } + mDoc->QueueCacheUpdate(this, CacheDomain::Table); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableCellAccessible implementation + +HTMLTableAccessible* HTMLTableCellAccessible::Table() const { + LocalAccessible* parent = const_cast<HTMLTableCellAccessible*>(this); + while ((parent = parent->LocalParent())) { + if (parent->IsHTMLTable()) { + return HTMLTableAccessible::GetFrom(parent); + } + } + + return nullptr; +} + +uint32_t HTMLTableCellAccessible::ColExtent() const { + nsTableCellFrame* cell = do_QueryFrame(GetFrame()); + if (!cell) { + // This probably isn't a table according to the layout engine; e.g. it has + // display: block. + return 1; + } + nsTableFrame* table = cell->GetTableFrame(); + MOZ_ASSERT(table); + return table->GetEffectiveColSpan(*cell); +} + +uint32_t HTMLTableCellAccessible::RowExtent() const { + nsTableCellFrame* cell = do_QueryFrame(GetFrame()); + if (!cell) { + // This probably isn't a table according to the layout engine; e.g. it has + // display: block. + return 1; + } + nsTableFrame* table = cell->GetTableFrame(); + MOZ_ASSERT(table); + return table->GetEffectiveRowSpan(*cell); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableHeaderCellAccessible +//////////////////////////////////////////////////////////////////////////////// + +HTMLTableHeaderCellAccessible::HTMLTableHeaderCellAccessible( + nsIContent* aContent, DocAccessible* aDoc) + : HTMLTableCellAccessible(aContent, aDoc) {} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableHeaderCellAccessible: LocalAccessible implementation + +role HTMLTableHeaderCellAccessible::NativeRole() const { + dom::Element* el = Elm(); + if (!el) { + return roles::NOTHING; + } + + // Check value of @scope attribute. + static mozilla::dom::Element::AttrValuesArray scopeValues[] = { + nsGkAtoms::col, nsGkAtoms::colgroup, nsGkAtoms::row, nsGkAtoms::rowgroup, + nullptr}; + int32_t valueIdx = el->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::scope, + scopeValues, eCaseMatters); + + switch (valueIdx) { + case 0: + case 1: + return roles::COLUMNHEADER; + case 2: + case 3: + return roles::ROWHEADER; + } + + dom::Element* nextEl = el->GetNextElementSibling(); + dom::Element* prevEl = el->GetPreviousElementSibling(); + // If this is the only cell in its row, it's a column header. + if (!nextEl && !prevEl) { + return roles::COLUMNHEADER; + } + const bool nextIsHeader = nextEl && nsCoreUtils::IsHTMLTableHeader(nextEl); + const bool prevIsHeader = prevEl && nsCoreUtils::IsHTMLTableHeader(prevEl); + // If this has a header on both sides, it is a column header. + if (prevIsHeader && nextIsHeader) { + return roles::COLUMNHEADER; + } + // If this has a header on one side and only a single normal cell on the + // other, it's a column header. + if (nextIsHeader && prevEl && !prevEl->GetPreviousElementSibling()) { + return roles::COLUMNHEADER; + } + if (prevIsHeader && nextEl && !nextEl->GetNextElementSibling()) { + return roles::COLUMNHEADER; + } + // If this has a normal cell next to it, it 's a row header. + if ((nextEl && !nextIsHeader) || (prevEl && !prevIsHeader)) { + return roles::ROWHEADER; + } + // If this has a row span, it could be a row header. + if (RowExtent() > 1) { + // It isn't a row header if it has 1 or more consecutive headers next to it. + if (prevIsHeader && + (!prevEl->GetPreviousElementSibling() || + nsCoreUtils::IsHTMLTableHeader(prevEl->GetPreviousElementSibling()))) { + return roles::COLUMNHEADER; + } + if (nextIsHeader && + (!nextEl->GetNextElementSibling() || + nsCoreUtils::IsHTMLTableHeader(nextEl->GetNextElementSibling()))) { + return roles::COLUMNHEADER; + } + return roles::ROWHEADER; + } + // Otherwise, assume it's a column header. + return roles::COLUMNHEADER; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableRowAccessible +//////////////////////////////////////////////////////////////////////////////// + +// LocalAccessible protected +ENameValueFlag HTMLTableRowAccessible::NativeName(nsString& aName) const { + // For table row accessibles, we only want to calculate the name from the + // sub tree if an ARIA role is present. + if (HasStrongARIARole()) { + return AccessibleWrap::NativeName(aName); + } + + return eNameOK; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableAccessible +//////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableAccessible: LocalAccessible + +bool HTMLTableAccessible::InsertChildAt(uint32_t aIndex, + LocalAccessible* aChild) { + // Move caption accessible so that it's the first child. Check for the first + // caption only, because nsAccessibilityService ensures we don't create + // accessibles for the other captions, since only the first is actually + // visible. + return HyperTextAccessible::InsertChildAt( + aChild->IsHTMLCaption() ? 0 : aIndex, aChild); +} + +uint64_t HTMLTableAccessible::NativeState() const { + return LocalAccessible::NativeState() | states::READONLY; +} + +ENameValueFlag HTMLTableAccessible::NativeName(nsString& aName) const { + ENameValueFlag nameFlag = LocalAccessible::NativeName(aName); + if (!aName.IsEmpty()) { + return nameFlag; + } + + // Use table caption as a name. + LocalAccessible* caption = Caption(); + if (caption) { + nsIContent* captionContent = caption->GetContent(); + if (captionContent) { + nsTextEquivUtils::AppendTextEquivFromContent(this, captionContent, + &aName); + if (!aName.IsEmpty()) { + return eNameOK; + } + } + } + + // If no caption then use summary as a name. + mContent->AsElement()->GetAttr(nsGkAtoms::summary, aName); + return eNameOK; +} + +void HTMLTableAccessible::DOMAttributeChanged(int32_t aNameSpaceID, + nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) { + HyperTextAccessible::DOMAttributeChanged(aNameSpaceID, aAttribute, aModType, + aOldValue, aOldState); + + if (aAttribute == nsGkAtoms::summary) { + nsAutoString name; + ARIAName(name); + if (name.IsEmpty()) { + if (!Caption()) { + // XXX: Should really be checking if caption provides a name. + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); + } + } + + mDoc->FireDelayedEvent(nsIAccessibleEvent::EVENT_OBJECT_ATTRIBUTE_CHANGED, + this); + mDoc->QueueCacheUpdate(this, CacheDomain::Table); + } +} + +already_AddRefed<AccAttributes> HTMLTableAccessible::NativeAttributes() { + RefPtr<AccAttributes> attributes = AccessibleWrap::NativeAttributes(); + + if (mContent->IsMathMLElement(nsGkAtoms::mtable_)) { + GetAccService()->MarkupAttributes(this, attributes); + } + + if (IsProbablyLayoutTable()) { + attributes->SetAttribute(nsGkAtoms::layout_guess, true); + } + + return attributes.forget(); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableAccessible: LocalAccessible + +Relation HTMLTableAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + if (aType == RelationType::LABELLED_BY) { + rel.AppendTarget(Caption()); + } + + return rel; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableAccessible: Table + +LocalAccessible* HTMLTableAccessible::Caption() const { + LocalAccessible* child = mChildren.SafeElementAt(0, nullptr); + // Since this is an HTML table the caption needs to be a caption + // element with no ARIA role (except for a reduntant role='caption'). + // If we did a full Role() calculation here we risk getting into an infinite + // loop where the parent role would depend on its name which would need to be + // calculated by retrieving the caption (bug 1420773.) + return child && child->NativeRole() == roles::CAPTION && + (!child->HasStrongARIARole() || + child->IsARIARole(nsGkAtoms::caption)) + ? child + : nullptr; +} + +uint32_t HTMLTableAccessible::ColCount() const { + nsTableWrapperFrame* tableFrame = GetTableWrapperFrame(); + return tableFrame ? tableFrame->GetColCount() : 0; +} + +uint32_t HTMLTableAccessible::RowCount() { + nsTableWrapperFrame* tableFrame = GetTableWrapperFrame(); + return tableFrame ? tableFrame->GetRowCount() : 0; +} + +bool HTMLTableAccessible::IsProbablyLayoutTable() { + // Implement a heuristic to determine if table is most likely used for layout. + + // XXX do we want to look for rowspan or colspan, especialy that span all but + // a couple cells at the beginning or end of a row/col, and especially when + // they occur at the edge of a table? + + // XXX For now debugging descriptions are always on via SHOW_LAYOUT_HEURISTIC + // This will allow release trunk builds to be used by testers to refine + // the algorithm. Integrate it into Logging. + // Change to |#define SHOW_LAYOUT_HEURISTIC DEBUG| before final release +#ifdef SHOW_LAYOUT_HEURISTIC +# define RETURN_LAYOUT_ANSWER(isLayout, heuristic) \ + { \ + mLayoutHeuristic = isLayout \ + ? nsLiteralString(u"layout table: " heuristic) \ + : nsLiteralString(u"data table: " heuristic); \ + return isLayout; \ + } +#else +# define RETURN_LAYOUT_ANSWER(isLayout, heuristic) \ + { return isLayout; } +#endif + + MOZ_ASSERT(!IsDefunct(), "Table accessible should not be defunct"); + + // Need to see all elements while document is being edited. + if (Document()->State() & states::EDITABLE) { + RETURN_LAYOUT_ANSWER(false, "In editable document"); + } + + // Check to see if an ARIA role overrides the role from native markup, + // but for which we still expose table semantics (treegrid, for example). + if (HasARIARole()) { + RETURN_LAYOUT_ANSWER(false, "Has role attribute"); + } + + dom::Element* el = Elm(); + if (el->IsMathMLElement(nsGkAtoms::mtable_)) { + RETURN_LAYOUT_ANSWER(false, "MathML matrix"); + } + + MOZ_ASSERT(el->IsHTMLElement(nsGkAtoms::table), + "Table should not be built by CSS display:table style"); + + // Check if datatable attribute has "0" value. + if (el->AttrValueIs(kNameSpaceID_None, nsGkAtoms::datatable, u"0"_ns, + eCaseMatters)) { + RETURN_LAYOUT_ANSWER(true, "Has datatable = 0 attribute, it's for layout"); + } + + // Check for legitimate data table attributes. + if (el->Element::HasNonEmptyAttr(nsGkAtoms::summary)) { + RETURN_LAYOUT_ANSWER(false, "Has summary -- legitimate table structures"); + } + + // Check for legitimate data table elements. + LocalAccessible* caption = LocalFirstChild(); + if (caption && caption->IsHTMLCaption() && caption->HasChildren()) { + RETURN_LAYOUT_ANSWER(false, + "Not empty caption -- legitimate table structures"); + } + + for (nsIContent* childElm = el->GetFirstChild(); childElm; + childElm = childElm->GetNextSibling()) { + if (!childElm->IsHTMLElement()) continue; + + if (childElm->IsAnyOfHTMLElements(nsGkAtoms::col, nsGkAtoms::colgroup, + nsGkAtoms::tfoot, nsGkAtoms::thead)) { + RETURN_LAYOUT_ANSWER( + false, + "Has col, colgroup, tfoot or thead -- legitimate table structures"); + } + + if (childElm->IsHTMLElement(nsGkAtoms::tbody)) { + for (nsIContent* rowElm = childElm->GetFirstChild(); rowElm; + rowElm = rowElm->GetNextSibling()) { + if (rowElm->IsHTMLElement(nsGkAtoms::tr)) { + if (LocalAccessible* row = Document()->GetAccessible(rowElm)) { + if (const nsRoleMapEntry* roleMapEntry = row->ARIARoleMap()) { + if (roleMapEntry->role != roles::ROW) { + RETURN_LAYOUT_ANSWER(true, "Repurposed tr with different role"); + } + } + } + + for (nsIContent* cellElm = rowElm->GetFirstChild(); cellElm; + cellElm = cellElm->GetNextSibling()) { + if (cellElm->IsHTMLElement()) { + if (cellElm->NodeInfo()->Equals(nsGkAtoms::th)) { + RETURN_LAYOUT_ANSWER(false, + "Has th -- legitimate table structures"); + } + + if (cellElm->AsElement()->HasAttr(nsGkAtoms::headers) || + cellElm->AsElement()->HasAttr(nsGkAtoms::scope) || + cellElm->AsElement()->HasAttr(nsGkAtoms::abbr)) { + RETURN_LAYOUT_ANSWER(false, + "Has headers, scope, or abbr attribute -- " + "legitimate table structures"); + } + + if (LocalAccessible* cell = Document()->GetAccessible(cellElm)) { + if (const nsRoleMapEntry* roleMapEntry = cell->ARIARoleMap()) { + if (roleMapEntry->role != roles::CELL && + roleMapEntry->role != roles::COLUMNHEADER && + roleMapEntry->role != roles::ROWHEADER && + roleMapEntry->role != roles::GRID_CELL) { + RETURN_LAYOUT_ANSWER(true, + "Repurposed cell with different role"); + } + } + if (cell->ChildCount() == 1 && + cell->LocalFirstChild()->IsAbbreviation()) { + RETURN_LAYOUT_ANSWER( + false, "has abbr -- legitimate table structures"); + } + } + } + } + } + } + } + } + + // If only 1 column or only 1 row, it's for layout. + auto colCount = ColCount(); + if (colCount <= 1) { + RETURN_LAYOUT_ANSWER(true, "Has only 1 column"); + } + auto rowCount = RowCount(); + if (rowCount <= 1) { + RETURN_LAYOUT_ANSWER(true, "Has only 1 row"); + } + + // Check for many columns. + if (colCount >= 5) { + RETURN_LAYOUT_ANSWER(false, ">=5 columns"); + } + + // Now we know there are 2-4 columns and 2 or more rows. Check to see if + // there are visible borders on the cells. + // XXX currently, we just check the first cell -- do we really need to do + // more? + nsTableWrapperFrame* tableFrame = do_QueryFrame(el->GetPrimaryFrame()); + if (!tableFrame) { + RETURN_LAYOUT_ANSWER(false, "table with no frame!"); + } + + nsIFrame* cellFrame = tableFrame->GetCellFrameAt(0, 0); + if (!cellFrame) { + RETURN_LAYOUT_ANSWER(false, "table's first cell has no frame!"); + } + + nsMargin border = cellFrame->StyleBorder()->GetComputedBorder(); + if (border.top || border.bottom || border.left || border.right) { + RETURN_LAYOUT_ANSWER(false, "Has nonzero border-width on table cell"); + } + + // Check for nested tables. + nsCOMPtr<nsIHTMLCollection> nestedTables = + el->GetElementsByTagName(u"table"_ns); + if (nestedTables->Length() > 0) { + RETURN_LAYOUT_ANSWER(true, "Has a nested table within it"); + } + + // Rules for non-bordered tables with 2-4 columns and 2+ rows from here on + // forward. + + // Check for styled background color across rows (alternating background + // color is a common feature for data tables). + auto childCount = ChildCount(); + nscolor rowColor = 0; + nscolor prevRowColor; + for (auto childIdx = 0U; childIdx < childCount; childIdx++) { + LocalAccessible* child = LocalChildAt(childIdx); + if (child->IsHTMLTableRow()) { + prevRowColor = rowColor; + nsIFrame* rowFrame = child->GetFrame(); + MOZ_ASSERT(rowFrame, "Table hierarchy got screwed up"); + if (!rowFrame) { + RETURN_LAYOUT_ANSWER(false, "Unexpected table hierarchy"); + } + + rowColor = rowFrame->StyleBackground()->BackgroundColor(rowFrame); + + if (childIdx > 0 && prevRowColor != rowColor) { + RETURN_LAYOUT_ANSWER(false, + "2 styles of row background color, non-bordered"); + } + } + } + + // Check for many rows. + const uint32_t kMaxLayoutRows = 20; + if (rowCount > kMaxLayoutRows) { // A ton of rows, this is probably for data + RETURN_LAYOUT_ANSWER(false, ">= kMaxLayoutRows (20) and non-bordered"); + } + + // Check for very wide table. + nsIFrame* documentFrame = Document()->GetFrame(); + nsSize documentSize = documentFrame->GetSize(); + if (documentSize.width > 0) { + nsSize tableSize = GetFrame()->GetSize(); + int32_t percentageOfDocWidth = (100 * tableSize.width) / documentSize.width; + if (percentageOfDocWidth > 95) { + // 3-4 columns, no borders, not a lot of rows, and 95% of the doc's width + // Probably for layout + RETURN_LAYOUT_ANSWER( + true, "<= 4 columns, table width is 95% of document width"); + } + } + + // Two column rules. + if (rowCount * colCount <= 10) { + RETURN_LAYOUT_ANSWER(true, "2-4 columns, 10 cells or less, non-bordered"); + } + + static const nsLiteralString tags[] = {u"embed"_ns, u"object"_ns, + u"iframe"_ns}; + for (const auto& tag : tags) { + nsCOMPtr<nsIHTMLCollection> descendants = el->GetElementsByTagName(tag); + if (descendants->Length() > 0) { + RETURN_LAYOUT_ANSWER(true, + "Has no borders, and has iframe, object or embed, " + "typical of advertisements"); + } + } + + RETURN_LAYOUT_ANSWER(false, + "No layout factor strong enough, so will guess data"); +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLTableAccessible: protected implementation + +void HTMLTableAccessible::Description(nsString& aDescription) const { + // Helpful for debugging layout vs. data tables + aDescription.Truncate(); + LocalAccessible::Description(aDescription); + if (!aDescription.IsEmpty()) { + return; + } + + // Use summary as description if it weren't used as a name. + // XXX: get rid code duplication with NameInternal(). + LocalAccessible* caption = Caption(); + if (caption) { + nsIContent* captionContent = caption->GetContent(); + if (captionContent) { + nsAutoString captionText; + nsTextEquivUtils::AppendTextEquivFromContent(this, captionContent, + &captionText); + + if (!captionText.IsEmpty()) { // summary isn't used as a name. + mContent->AsElement()->GetAttr(nsGkAtoms::summary, aDescription); + } + } + } + +#ifdef SHOW_LAYOUT_HEURISTIC + if (aDescription.IsEmpty()) { + bool isProbablyForLayout = IsProbablyLayoutTable(); + aDescription = mLayoutHeuristic; + } + printf("\nTABLE: %s\n", NS_ConvertUTF16toUTF8(mLayoutHeuristic).get()); +#endif +} + +nsTableWrapperFrame* HTMLTableAccessible::GetTableWrapperFrame() const { + nsTableWrapperFrame* tableFrame = do_QueryFrame(mContent->GetPrimaryFrame()); + if (tableFrame && tableFrame->PrincipalChildList().FirstChild()) { + return tableFrame; + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// HTMLCaptionAccessible +//////////////////////////////////////////////////////////////////////////////// + +Relation HTMLCaptionAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessible::RelationByType(aType); + if (aType == RelationType::LABEL_FOR) { + rel.AppendTarget(LocalParent()); + } + + return rel; +} + +role HTMLCaptionAccessible::NativeRole() const { return roles::CAPTION; } diff --git a/accessible/html/HTMLTableAccessible.h b/accessible/html/HTMLTableAccessible.h new file mode 100644 index 0000000000..7dbad68cba --- /dev/null +++ b/accessible/html/HTMLTableAccessible.h @@ -0,0 +1,177 @@ +/* -*- 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/. */ + +#ifndef mozilla_a11y_HTMLTableAccessible_h__ +#define mozilla_a11y_HTMLTableAccessible_h__ + +#include "HyperTextAccessible.h" + +class nsITableCellLayout; +class nsTableCellFrame; +class nsTableWrapperFrame; + +namespace mozilla { + +namespace a11y { + +class HTMLTableAccessible; + +/** + * HTML table cell accessible (html:td). + */ +class HTMLTableCellAccessible : public HyperTextAccessible { + public: + HTMLTableCellAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // nsISupports + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLTableCellAccessible, + HyperTextAccessible) + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + virtual already_AddRefed<AccAttributes> NativeAttributes() override; + + protected: + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + // HTMLTableCellAccessible + public: + HTMLTableAccessible* Table() const; + uint32_t ColExtent() const; + uint32_t RowExtent() const; + + static HTMLTableCellAccessible* GetFrom(LocalAccessible* aAcc) { + if (aAcc->IsHTMLTableCell()) { + return static_cast<HTMLTableCellAccessible*>(aAcc); + } + return nullptr; + } + + protected: + virtual ~HTMLTableCellAccessible() {} +}; + +/** + * HTML table row/column header accessible (html:th or html:td@scope). + */ +class HTMLTableHeaderCellAccessible : public HTMLTableCellAccessible { + public: + HTMLTableHeaderCellAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // LocalAccessible + virtual a11y::role NativeRole() const override; +}; + +/** + * HTML table row accessible (html:tr). + */ +class HTMLTableRowAccessible : public HyperTextAccessible { + public: + HTMLTableRowAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLTableRowType; + mGenericTypes |= eTableRow; + } + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLTableRowAccessible, + HyperTextAccessible) + + protected: + virtual ~HTMLTableRowAccessible() {} + + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * HTML table accessible (html:table). + */ + +// To turn on table debugging descriptions define SHOW_LAYOUT_HEURISTIC +// This allow release trunk builds to be used by testers to refine the +// data vs. layout heuristic +// #define SHOW_LAYOUT_HEURISTIC + +class HTMLTableAccessible : public HyperTextAccessible { + public: + HTMLTableAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLTableType; + mGenericTypes |= eTable; + } + + NS_INLINE_DECL_REFCOUNTING_INHERITED(HTMLTableAccessible, HyperTextAccessible) + + // HTMLTableAccessible + LocalAccessible* Caption() const; + uint32_t ColCount() const; + uint32_t RowCount(); + bool IsProbablyLayoutTable(); + + static HTMLTableAccessible* GetFrom(LocalAccessible* aAcc) { + if (aAcc->IsHTMLTable()) { + return static_cast<HTMLTableAccessible*>(aAcc); + } + return nullptr; + } + + // LocalAccessible + virtual void Description(nsString& aDescription) const override; + virtual uint64_t NativeState() const override; + virtual already_AddRefed<AccAttributes> NativeAttributes() override; + virtual Relation RelationByType(RelationType aRelationType) const override; + + virtual bool InsertChildAt(uint32_t aIndex, LocalAccessible* aChild) override; + + protected: + virtual ~HTMLTableAccessible() {} + + // LocalAccessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + virtual void DOMAttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue, + uint64_t aOldState) override; + + // HTMLTableAccessible + +#ifdef SHOW_LAYOUT_HEURISTIC + nsString mLayoutHeuristic; +#endif + + private: + /** + * Get table wrapper frame, or return null if there is no inner table. + */ + nsTableWrapperFrame* GetTableWrapperFrame() const; +}; + +/** + * HTML caption accessible (html:caption). + */ +class HTMLCaptionAccessible : public HyperTextAccessible { + public: + HTMLCaptionAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessible(aContent, aDoc) { + mType = eHTMLCaptionType; + } + + // LocalAccessible + virtual a11y::role NativeRole() const override; + virtual Relation RelationByType(RelationType aRelationType) const override; + + protected: + virtual ~HTMLCaptionAccessible() {} +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/html/moz.build b/accessible/html/moz.build new file mode 100644 index 0000000000..3a246373da --- /dev/null +++ b/accessible/html/moz.build @@ -0,0 +1,52 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +UNIFIED_SOURCES += [ + "HTMLCanvasAccessible.cpp", + "HTMLElementAccessibles.cpp", + "HTMLFormControlAccessible.cpp", + "HTMLImageMapAccessible.cpp", + "HTMLLinkAccessible.cpp", + "HTMLListAccessible.cpp", + "HTMLSelectAccessible.cpp", + "HTMLTableAccessible.cpp", +] + +LOCAL_INCLUDES += [ + "/accessible/base", + "/accessible/generic", + "/accessible/xpcom", + "/layout/forms", + "/layout/generic", + "/layout/tables", + "/layout/xul", +] + +if CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk": + LOCAL_INCLUDES += [ + "/accessible/atk", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows": + LOCAL_INCLUDES += [ + "/accessible/windows/ia2", + "/accessible/windows/msaa", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa": + LOCAL_INCLUDES += [ + "/accessible/mac", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + LOCAL_INCLUDES += [ + "/accessible/android", + ] +else: + LOCAL_INCLUDES += [ + "/accessible/other", + ] + +include("/ipc/chromium/chromium-config.mozbuild") + +FINAL_LIBRARY = "xul" |