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
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Aaron Leventhal <aleventh@us.ibm.com> (original author)
24 * Alexander Surkov <surkov.alexander@gmail.com>
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsHTMLLinkAccessible.h"
44 ////////////////////////////////////////////////////////////////////////////////
45 // nsHTMLLinkAccessible
47 nsHTMLLinkAccessible::nsHTMLLinkAccessible(nsIDOMNode
* aDomNode
,
48 nsIWeakReference
* aShell
):
49 nsHyperTextAccessibleWrap(aDomNode
, aShell
)
53 // Expose nsIAccessibleHyperLink unconditionally
54 NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLLinkAccessible
, nsHyperTextAccessibleWrap
,
55 nsIAccessibleHyperLink
)
57 ////////////////////////////////////////////////////////////////////////////////
61 nsHTMLLinkAccessible::GetName(nsAString
& aName
)
66 return NS_ERROR_FAILURE
;
68 nsCOMPtr
<nsIContent
> content(do_QueryInterface(mDOMNode
));
69 nsresult rv
= AppendFlatStringFromSubtree(content
, &aName
);
70 NS_ENSURE_SUCCESS(rv
, rv
);
72 if (aName
.IsEmpty()) {
73 // Probably an image without alt or title inside, try to get the name on
74 // the link by usual way.
75 return GetHTMLName(aName
, PR_FALSE
);
82 nsHTMLLinkAccessible::GetRole(PRUint32
*aRole
)
84 NS_ENSURE_ARG_POINTER(aRole
);
86 *aRole
= nsIAccessibleRole::ROLE_LINK
;
91 nsHTMLLinkAccessible::GetState(PRUint32
*aState
, PRUint32
*aExtraState
)
93 nsresult rv
= nsHyperTextAccessibleWrap::GetState(aState
, aExtraState
);
94 NS_ENSURE_SUCCESS(rv
, rv
);
98 *aState
&= ~nsIAccessibleStates::STATE_READONLY
;
100 nsCOMPtr
<nsIContent
> content(do_QueryInterface(mDOMNode
));
101 if (content
&& content
->HasAttr(kNameSpaceID_None
,
102 nsAccessibilityAtoms::name
)) {
103 // This is how we indicate it is a named anchor
104 // In other words, this anchor can be selected as a location :)
105 // There is no other better state to use to indicate this.
106 *aState
|= nsIAccessibleStates::STATE_SELECTABLE
;
109 nsCOMPtr
<nsILink
> link
= do_QueryInterface(mDOMNode
);
110 NS_ENSURE_STATE(link
);
112 nsLinkState linkState
;
113 link
->GetLinkState(linkState
);
114 if (linkState
== eLinkState_NotLink
|| linkState
== eLinkState_Unknown
) {
115 // This is a either named anchor (a link with also a name attribute) or
116 // it doesn't have any attributes. Check if 'click' event handler is
117 // registered, otherwise bail out.
118 PRBool isOnclick
= nsAccUtils::HasListener(content
,
119 NS_LITERAL_STRING("click"));
124 *aState
|= nsIAccessibleStates::STATE_LINKED
;
126 if (linkState
== eLinkState_Visited
)
127 *aState
|= nsIAccessibleStates::STATE_TRAVERSED
;
133 nsHTMLLinkAccessible::GetValue(nsAString
& aValue
)
137 nsresult rv
= nsHyperTextAccessible::GetValue(aValue
);
138 NS_ENSURE_SUCCESS(rv
, rv
);
140 if (!aValue
.IsEmpty())
143 nsCOMPtr
<nsIPresShell
> presShell(do_QueryReferent(mWeakShell
));
144 if (mDOMNode
&& presShell
)
145 return presShell
->GetLinkLocation(mDOMNode
, aValue
);
151 nsHTMLLinkAccessible::GetNumActions(PRUint8
*aNumActions
)
153 NS_ENSURE_ARG_POINTER(aNumActions
);
156 return nsHyperTextAccessible::GetNumActions(aNumActions
);
163 nsHTMLLinkAccessible::GetActionName(PRUint8 aIndex
, nsAString
& aName
)
168 return nsHyperTextAccessible::GetActionName(aIndex
, aName
);
170 // Action 0 (default action): Jump to link
171 if (aIndex
!= eAction_Jump
)
172 return NS_ERROR_INVALID_ARG
;
174 aName
.AssignLiteral("jump");
179 nsHTMLLinkAccessible::DoAction(PRUint8 aIndex
)
182 return nsHyperTextAccessible::DoAction(aIndex
);
184 // Action 0 (default action): Jump to link
185 if (aIndex
!= eAction_Jump
)
186 return NS_ERROR_INVALID_ARG
;
189 return NS_ERROR_FAILURE
;
191 nsCOMPtr
<nsIContent
> content(do_QueryInterface(mDOMNode
));
192 return DoCommand(content
);
195 ////////////////////////////////////////////////////////////////////////////////
196 // nsIAccessibleHyperLink
199 nsHTMLLinkAccessible::GetURI(PRInt32 aIndex
, nsIURI
**aURI
)
201 NS_ENSURE_ARG_POINTER(aURI
);
205 return NS_ERROR_INVALID_ARG
;
207 nsCOMPtr
<nsILink
> link(do_QueryInterface(mDOMNode
));
208 NS_ENSURE_STATE(link
);
210 return link
->GetHrefURI(aURI
);
213 ////////////////////////////////////////////////////////////////////////////////
217 nsHTMLLinkAccessible::IsLinked()
219 nsCOMPtr
<nsILink
> link(do_QueryInterface(mDOMNode
));
223 nsLinkState linkState
;
224 nsresult rv
= link
->GetLinkState(linkState
);
226 return NS_SUCCEEDED(rv
) && linkState
!= eLinkState_NotLink
&&
227 linkState
!= eLinkState_Unknown
;