1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Aaron Leventhal <aleventh@us.ibm.com> (original author)
24 : * Alexander Surkov <surkov.alexander@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsHTMLLinkAccessible.h"
41 :
42 : #include "nsCoreUtils.h"
43 : #include "Role.h"
44 : #include "States.h"
45 :
46 : #include "nsDocAccessible.h"
47 : #include "nsEventStates.h"
48 : #include "mozilla/dom/Element.h"
49 :
50 : using namespace mozilla::a11y;
51 :
52 : ////////////////////////////////////////////////////////////////////////////////
53 : // nsHTMLLinkAccessible
54 : ////////////////////////////////////////////////////////////////////////////////
55 :
56 0 : nsHTMLLinkAccessible::
57 : nsHTMLLinkAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
58 0 : nsHyperTextAccessibleWrap(aContent, aDoc)
59 : {
60 0 : }
61 :
62 : // Expose nsIAccessibleHyperLink unconditionally
63 0 : NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLLinkAccessible, nsHyperTextAccessibleWrap,
64 : nsIAccessibleHyperLink)
65 :
66 : ////////////////////////////////////////////////////////////////////////////////
67 : // nsIAccessible
68 :
69 : role
70 0 : nsHTMLLinkAccessible::NativeRole()
71 : {
72 0 : return roles::LINK;
73 : }
74 :
75 : PRUint64
76 0 : nsHTMLLinkAccessible::NativeState()
77 : {
78 0 : PRUint64 states = nsHyperTextAccessibleWrap::NativeState();
79 :
80 0 : states &= ~states::READONLY;
81 :
82 0 : if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::name)) {
83 : // This is how we indicate it is a named anchor
84 : // In other words, this anchor can be selected as a location :)
85 : // There is no other better state to use to indicate this.
86 0 : states |= states::SELECTABLE;
87 : }
88 :
89 0 : nsEventStates state = mContent->AsElement()->State();
90 0 : if (state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
91 0 : NS_EVENT_STATE_UNVISITED)) {
92 0 : states |= states::LINKED;
93 :
94 0 : if (state.HasState(NS_EVENT_STATE_VISITED))
95 0 : states |= states::TRAVERSED;
96 :
97 0 : return states;
98 : }
99 :
100 : // This is a either named anchor (a link with also a name attribute) or
101 : // it doesn't have any attributes. Check if 'click' event handler is
102 : // registered, otherwise bail out.
103 0 : if (nsCoreUtils::HasClickListener(mContent))
104 0 : states |= states::LINKED;
105 :
106 0 : return states;
107 : }
108 :
109 : NS_IMETHODIMP
110 0 : nsHTMLLinkAccessible::GetValue(nsAString& aValue)
111 : {
112 0 : aValue.Truncate();
113 :
114 0 : nsresult rv = nsHyperTextAccessible::GetValue(aValue);
115 0 : NS_ENSURE_SUCCESS(rv, rv);
116 :
117 0 : if (!aValue.IsEmpty())
118 0 : return NS_OK;
119 :
120 0 : nsIPresShell* presShell(mDoc->PresShell());
121 0 : nsCOMPtr<nsIDOMNode> DOMNode(do_QueryInterface(mContent));
122 0 : return presShell->GetLinkLocation(DOMNode, aValue);
123 : }
124 :
125 : PRUint8
126 0 : nsHTMLLinkAccessible::ActionCount()
127 : {
128 0 : return IsLinked() ? 1 : nsHyperTextAccessible::ActionCount();
129 : }
130 :
131 : NS_IMETHODIMP
132 0 : nsHTMLLinkAccessible::GetActionName(PRUint8 aIndex, nsAString& aName)
133 : {
134 0 : aName.Truncate();
135 :
136 0 : if (!IsLinked())
137 0 : return nsHyperTextAccessible::GetActionName(aIndex, aName);
138 :
139 : // Action 0 (default action): Jump to link
140 0 : if (aIndex != eAction_Jump)
141 0 : return NS_ERROR_INVALID_ARG;
142 :
143 0 : aName.AssignLiteral("jump");
144 0 : return NS_OK;
145 : }
146 :
147 : NS_IMETHODIMP
148 0 : nsHTMLLinkAccessible::DoAction(PRUint8 aIndex)
149 : {
150 0 : if (!IsLinked())
151 0 : return nsHyperTextAccessible::DoAction(aIndex);
152 :
153 : // Action 0 (default action): Jump to link
154 0 : if (aIndex != eAction_Jump)
155 0 : return NS_ERROR_INVALID_ARG;
156 :
157 0 : if (IsDefunct())
158 0 : return NS_ERROR_FAILURE;
159 :
160 0 : DoCommand();
161 0 : return NS_OK;
162 : }
163 :
164 : ////////////////////////////////////////////////////////////////////////////////
165 : // HyperLinkAccessible
166 :
167 : bool
168 0 : nsHTMLLinkAccessible::IsLink()
169 : {
170 : // Expose HyperLinkAccessible unconditionally.
171 0 : return true;
172 : }
173 :
174 : already_AddRefed<nsIURI>
175 0 : nsHTMLLinkAccessible::AnchorURIAt(PRUint32 aAnchorIndex)
176 : {
177 0 : return aAnchorIndex == 0 ? mContent->GetHrefURI() : nsnull;
178 : }
179 :
180 : ////////////////////////////////////////////////////////////////////////////////
181 : // Protected members
182 :
183 : bool
184 0 : nsHTMLLinkAccessible::IsLinked()
185 : {
186 0 : if (IsDefunct())
187 0 : return false;
188 :
189 0 : nsEventStates state = mContent->AsElement()->State();
190 : return state.HasAtLeastOneOfStates(NS_EVENT_STATE_VISITED |
191 0 : NS_EVENT_STATE_UNVISITED);
192 : }
|