Bug 1753131 - Dispatch devicechange events even without an actively capturing MediaSt...
[gecko.git] / accessible / html / HTMLLinkAccessible.cpp
blob9e447244d96fd40d94496b04cb233ebb1daf5e2c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "HTMLLinkAccessible.h"
8 #include "CacheConstants.h"
9 #include "nsCoreUtils.h"
10 #include "DocAccessible.h"
11 #include "Role.h"
12 #include "States.h"
14 #include "nsContentUtils.h"
15 #include "mozilla/EventStates.h"
16 #include "mozilla/dom/Element.h"
17 #include "mozilla/dom/MutationEventBinding.h"
19 using namespace mozilla;
20 using namespace mozilla::a11y;
22 ////////////////////////////////////////////////////////////////////////////////
23 // HTMLLinkAccessible
24 ////////////////////////////////////////////////////////////////////////////////
26 HTMLLinkAccessible::HTMLLinkAccessible(nsIContent* aContent,
27 DocAccessible* aDoc)
28 : HyperTextAccessibleWrap(aContent, aDoc) {
29 mType = eHTMLLinkType;
32 ////////////////////////////////////////////////////////////////////////////////
33 // nsIAccessible
35 role HTMLLinkAccessible::NativeRole() const { return roles::LINK; }
37 uint64_t HTMLLinkAccessible::NativeState() const {
38 return HyperTextAccessibleWrap::NativeState() & ~states::READONLY;
41 uint64_t HTMLLinkAccessible::NativeLinkState() const {
42 EventStates eventState = mContent->AsElement()->State();
43 if (eventState.HasState(NS_EVENT_STATE_UNVISITED)) return states::LINKED;
45 if (eventState.HasState(NS_EVENT_STATE_VISITED)) {
46 return states::LINKED | states::TRAVERSED;
49 // This is a either named anchor (a link with also a name attribute) or
50 // it doesn't have any attributes. Check if 'click' event handler is
51 // registered, otherwise bail out.
52 return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0;
55 uint64_t HTMLLinkAccessible::NativeInteractiveState() const {
56 uint64_t state = HyperTextAccessibleWrap::NativeInteractiveState();
58 // This is how we indicate it is a named anchor. In other words, this anchor
59 // can be selected as a location :) There is no other better state to use to
60 // indicate this.
61 if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::name)) {
62 state |= states::SELECTABLE;
65 return state;
68 void HTMLLinkAccessible::Value(nsString& aValue) const {
69 aValue.Truncate();
71 HyperTextAccessible::Value(aValue);
72 if (aValue.IsEmpty()) {
73 nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue);
77 uint8_t HTMLLinkAccessible::ActionCount() const {
78 return IsLinked() ? 1 : HyperTextAccessible::ActionCount();
81 void HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
82 aName.Truncate();
84 if (!IsLinked()) {
85 HyperTextAccessible::ActionNameAt(aIndex, aName);
86 return;
89 // Action 0 (default action): Jump to link
90 if (aIndex == eAction_Jump) aName.AssignLiteral("jump");
93 bool HTMLLinkAccessible::DoAction(uint8_t aIndex) const {
94 if (!IsLinked()) return HyperTextAccessible::DoAction(aIndex);
96 // Action 0 (default action): Jump to link
97 if (aIndex != eAction_Jump) return false;
99 DoCommand();
100 return true;
103 bool HTMLLinkAccessible::AttributeChangesState(nsAtom* aAttribute) {
104 return aAttribute == nsGkAtoms::href ||
105 HyperTextAccessibleWrap::AttributeChangesState(aAttribute);
108 void HTMLLinkAccessible::DOMAttributeChanged(int32_t aNameSpaceID,
109 nsAtom* aAttribute,
110 int32_t aModType,
111 const nsAttrValue* aOldValue,
112 uint64_t aOldState) {
113 HyperTextAccessibleWrap::DOMAttributeChanged(aNameSpaceID, aAttribute,
114 aModType, aOldValue, aOldState);
116 if (aAttribute == nsGkAtoms::href &&
117 (aModType == dom::MutationEvent_Binding::ADDITION ||
118 aModType == dom::MutationEvent_Binding::REMOVAL)) {
119 SendCache(CacheDomain::Actions, CacheUpdateType::Update);
123 ////////////////////////////////////////////////////////////////////////////////
124 // HyperLinkAccessible
126 bool HTMLLinkAccessible::IsLink() const {
127 // Expose HyperLinkAccessible unconditionally.
128 return true;
131 already_AddRefed<nsIURI> HTMLLinkAccessible::AnchorURIAt(
132 uint32_t aAnchorIndex) const {
133 return aAnchorIndex == 0 ? mContent->GetHrefURI() : nullptr;
136 ////////////////////////////////////////////////////////////////////////////////
137 // HTMLLinkAccessible
139 bool HTMLLinkAccessible::IsLinked() const {
140 EventStates state = mContent->AsElement()->State();
141 return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
142 NS_EVENT_STATE_UNVISITED);