Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/html/HTMLLinkAccessible.cpp
Line
Count
Source (jump to first uncovered line)
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
24
HTMLLinkAccessible::
25
  HTMLLinkAccessible(nsIContent* aContent, DocAccessible* aDoc) :
26
  HyperTextAccessibleWrap(aContent, aDoc)
27
0
{
28
0
}
29
30
////////////////////////////////////////////////////////////////////////////////
31
// nsIAccessible
32
33
role
34
HTMLLinkAccessible::NativeRole() const
35
0
{
36
0
  return roles::LINK;
37
0
}
38
39
uint64_t
40
HTMLLinkAccessible::NativeState() const
41
0
{
42
0
  return HyperTextAccessibleWrap::NativeState() & ~states::READONLY;
43
0
}
44
45
uint64_t
46
HTMLLinkAccessible::NativeLinkState() const
47
0
{
48
0
  EventStates eventState = mContent->AsElement()->State();
49
0
  if (eventState.HasState(NS_EVENT_STATE_UNVISITED))
50
0
    return states::LINKED;
51
0
52
0
  if (eventState.HasState(NS_EVENT_STATE_VISITED))
53
0
    return states::LINKED | states::TRAVERSED;
54
0
55
0
  // This is a either named anchor (a link with also a name attribute) or
56
0
  // it doesn't have any attributes. Check if 'click' event handler is
57
0
  // registered, otherwise bail out.
58
0
  return nsCoreUtils::HasClickListener(mContent) ? states::LINKED : 0;
59
0
}
60
61
uint64_t
62
HTMLLinkAccessible::NativeInteractiveState() const
63
0
{
64
0
  uint64_t state = HyperTextAccessibleWrap::NativeInteractiveState();
65
0
66
0
  // This is how we indicate it is a named anchor. In other words, this anchor
67
0
  // can be selected as a location :) There is no other better state to use to
68
0
  // indicate this.
69
0
  if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::name))
70
0
    state |= states::SELECTABLE;
71
0
72
0
  return state;
73
0
}
74
75
void
76
HTMLLinkAccessible::Value(nsString& aValue) const
77
0
{
78
0
  aValue.Truncate();
79
0
80
0
  HyperTextAccessible::Value(aValue);
81
0
  if (aValue.IsEmpty())
82
0
    nsContentUtils::GetLinkLocation(mContent->AsElement(), aValue);
83
0
}
84
85
uint8_t
86
HTMLLinkAccessible::ActionCount() const
87
0
{
88
0
  return IsLinked() ? 1 : HyperTextAccessible::ActionCount();
89
0
}
90
91
void
92
HTMLLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
93
0
{
94
0
  aName.Truncate();
95
0
96
0
  if (!IsLinked()) {
97
0
    HyperTextAccessible::ActionNameAt(aIndex, aName);
98
0
    return;
99
0
  }
100
0
101
0
  // Action 0 (default action): Jump to link
102
0
  if (aIndex == eAction_Jump)
103
0
    aName.AssignLiteral("jump");
104
0
}
105
106
bool
107
HTMLLinkAccessible::DoAction(uint8_t aIndex) const
108
0
{
109
0
  if (!IsLinked())
110
0
    return HyperTextAccessible::DoAction(aIndex);
111
0
112
0
  // Action 0 (default action): Jump to link
113
0
  if (aIndex != eAction_Jump)
114
0
    return false;
115
0
116
0
  DoCommand();
117
0
  return true;
118
0
}
119
120
////////////////////////////////////////////////////////////////////////////////
121
// HyperLinkAccessible
122
123
bool
124
HTMLLinkAccessible::IsLink() const
125
0
{
126
0
  // Expose HyperLinkAccessible unconditionally.
127
0
  return true;
128
0
}
129
130
already_AddRefed<nsIURI>
131
HTMLLinkAccessible::AnchorURIAt(uint32_t aAnchorIndex) const
132
0
{
133
0
  return aAnchorIndex == 0 ? mContent->GetHrefURI() : nullptr;
134
0
}
135
136
////////////////////////////////////////////////////////////////////////////////
137
// Protected members
138
139
bool
140
HTMLLinkAccessible::IsLinked() const
141
0
{
142
0
  EventStates state = mContent->AsElement()->State();
143
0
  return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
144
0
                                     NS_EVENT_STATE_UNVISITED);
145
0
}