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/. */
5 
6 #include "HTMLLinkAccessible.h"
7 
8 #include "nsCoreUtils.h"
9 #include "DocAccessible.h"
10 #include "Role.h"
11 #include "States.h"
12 
13 #include "nsContentUtils.h"
14 #include "mozilla/EventStates.h"
15 #include "mozilla/dom/Element.h"
16 
17 using namespace mozilla;
18 using namespace mozilla::a11y;
19 
20 ////////////////////////////////////////////////////////////////////////////////
21 // HTMLLinkAccessible
22 ////////////////////////////////////////////////////////////////////////////////
23 
HTMLLinkAccessible(nsIContent * aContent,DocAccessible * aDoc)24 HTMLLinkAccessible::HTMLLinkAccessible(nsIContent* aContent,
25                                        DocAccessible* aDoc)
26     : HyperTextAccessibleWrap(aContent, aDoc) {}
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 // nsIAccessible
30 
NativeRole()31 role HTMLLinkAccessible::NativeRole() { return roles::LINK; }
32 
NativeState()33 uint64_t HTMLLinkAccessible::NativeState() {
34   return HyperTextAccessibleWrap::NativeState() & ~states::READONLY;
35 }
36 
NativeLinkState() const37 uint64_t HTMLLinkAccessible::NativeLinkState() const {
38   EventStates eventState = mContent->AsElement()->State();
39   if (eventState.HasState(NS_EVENT_STATE_UNVISITED)) return states::LINKED;
40 
41   if (eventState.HasState(NS_EVENT_STATE_VISITED))
42     return states::LINKED | states::TRAVERSED;
43 
44   // This is a either named anchor (a link with also a name attribute) or
45   // it doesn't have any attributes. Check if 'click' event handler is
46   // registered, otherwise bail out.
47   return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0;
48 }
49 
NativeInteractiveState() const50 uint64_t HTMLLinkAccessible::NativeInteractiveState() const {
51   uint64_t state = HyperTextAccessibleWrap::NativeInteractiveState();
52 
53   // This is how we indicate it is a named anchor. In other words, this anchor
54   // can be selected as a location :) There is no other better state to use to
55   // indicate this.
56   if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::name))
57     state |= states::SELECTABLE;
58 
59   return state;
60 }
61 
Value(nsString & aValue)62 void HTMLLinkAccessible::Value(nsString& aValue) {
63   aValue.Truncate();
64 
65   HyperTextAccessible::Value(aValue);
66   if (aValue.IsEmpty())
67     nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue);
68 }
69 
ActionCount()70 uint8_t HTMLLinkAccessible::ActionCount() {
71   return IsLinked() ? 1 : HyperTextAccessible::ActionCount();
72 }
73 
ActionNameAt(uint8_t aIndex,nsAString & aName)74 void HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
75   aName.Truncate();
76 
77   if (!IsLinked()) {
78     HyperTextAccessible::ActionNameAt(aIndex, aName);
79     return;
80   }
81 
82   // Action 0 (default action): Jump to link
83   if (aIndex == eAction_Jump) aName.AssignLiteral("jump");
84 }
85 
DoAction(uint8_t aIndex)86 bool HTMLLinkAccessible::DoAction(uint8_t aIndex) {
87   if (!IsLinked()) return HyperTextAccessible::DoAction(aIndex);
88 
89   // Action 0 (default action): Jump to link
90   if (aIndex != eAction_Jump) return false;
91 
92   DoCommand();
93   return true;
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 // HyperLinkAccessible
98 
IsLink()99 bool HTMLLinkAccessible::IsLink() {
100   // Expose HyperLinkAccessible unconditionally.
101   return true;
102 }
103 
AnchorURIAt(uint32_t aAnchorIndex)104 already_AddRefed<nsIURI> HTMLLinkAccessible::AnchorURIAt(
105     uint32_t aAnchorIndex) {
106   return aAnchorIndex == 0 ? mContent->GetHrefURI() : nullptr;
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 // Protected members
111 
IsLinked() const112 bool HTMLLinkAccessible::IsLinked() const {
113   EventStates state = mContent->AsElement()->State();
114   return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
115                                      NS_EVENT_STATE_UNVISITED);
116 }
117