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   mType = eHTMLLinkType;
28 }
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 // nsIAccessible
32 
NativeRole() const33 role HTMLLinkAccessible::NativeRole() const { return roles::LINK; }
34 
NativeState() const35 uint64_t HTMLLinkAccessible::NativeState() const {
36   return HyperTextAccessibleWrap::NativeState() & ~states::READONLY;
37 }
38 
NativeLinkState() const39 uint64_t HTMLLinkAccessible::NativeLinkState() const {
40   EventStates eventState = mContent->AsElement()->State();
41   if (eventState.HasState(NS_EVENT_STATE_UNVISITED)) return states::LINKED;
42 
43   if (eventState.HasState(NS_EVENT_STATE_VISITED)) {
44     return states::LINKED | states::TRAVERSED;
45   }
46 
47   // This is a either named anchor (a link with also a name attribute) or
48   // it doesn't have any attributes. Check if 'click' event handler is
49   // registered, otherwise bail out.
50   return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0;
51 }
52 
NativeInteractiveState() const53 uint64_t HTMLLinkAccessible::NativeInteractiveState() const {
54   uint64_t state = HyperTextAccessibleWrap::NativeInteractiveState();
55 
56   // This is how we indicate it is a named anchor. In other words, this anchor
57   // can be selected as a location :) There is no other better state to use to
58   // indicate this.
59   if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::name)) {
60     state |= states::SELECTABLE;
61   }
62 
63   return state;
64 }
65 
Value(nsString & aValue) const66 void HTMLLinkAccessible::Value(nsString& aValue) const {
67   aValue.Truncate();
68 
69   HyperTextAccessible::Value(aValue);
70   if (aValue.IsEmpty()) {
71     nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue);
72   }
73 }
74 
ActionCount() const75 uint8_t HTMLLinkAccessible::ActionCount() const {
76   return IsLinked() ? 1 : HyperTextAccessible::ActionCount();
77 }
78 
ActionNameAt(uint8_t aIndex,nsAString & aName)79 void HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
80   aName.Truncate();
81 
82   if (!IsLinked()) {
83     HyperTextAccessible::ActionNameAt(aIndex, aName);
84     return;
85   }
86 
87   // Action 0 (default action): Jump to link
88   if (aIndex == eAction_Jump) aName.AssignLiteral("jump");
89 }
90 
DoAction(uint8_t aIndex) const91 bool HTMLLinkAccessible::DoAction(uint8_t aIndex) const {
92   if (!IsLinked()) return HyperTextAccessible::DoAction(aIndex);
93 
94   // Action 0 (default action): Jump to link
95   if (aIndex != eAction_Jump) return false;
96 
97   DoCommand();
98   return true;
99 }
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 // HyperLinkAccessible
103 
IsLink() const104 bool HTMLLinkAccessible::IsLink() const {
105   // Expose HyperLinkAccessible unconditionally.
106   return true;
107 }
108 
AnchorURIAt(uint32_t aAnchorIndex) const109 already_AddRefed<nsIURI> HTMLLinkAccessible::AnchorURIAt(
110     uint32_t aAnchorIndex) const {
111   return aAnchorIndex == 0 ? mContent->GetHrefURI() : nullptr;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 // HTMLLinkAccessible
116 
IsLinked() const117 bool HTMLLinkAccessible::IsLinked() const {
118   EventStates state = mContent->AsElement()->State();
119   return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
120                                      NS_EVENT_STATE_UNVISITED);
121 }
122