Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLLinkAccessibleWrap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public
00006  * License Version 1.1 (the "License"); you may not use this file
00007  * except in compliance with the License. You may obtain a copy of
00008  * the License at http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS
00011  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00012  * implied. See the License for the specific language governing
00013  * rights and limitations under the License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is Sun Microsystems, Inc.
00018  * Portions created by Sun Microsystems are Copyright (C) 2002 Sun
00019  * Microsystems, Inc. All Rights Reserved.
00020  *
00021  * Original Author: Gilbert Fang (gilbert.fang@sun.com)
00022  *
00023  * Contributor(s):
00024  *   Kyle Yuan (kyle.yuan@sun.com)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the NPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the NPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsHTMLLinkAccessibleWrap.h"
00041 #include "nsIDOMHTMLCollection.h"
00042 #include "nsIDocument.h"
00043 #include "nsILink.h"
00044 #include "nsIDOMText.h"
00045 #include "nsIURI.h"
00046 #include "nsNetUtil.h"
00047 
00048 // --------------------------------------------------------
00049 // nsHTMLLinkAccessibleWrap Accessible
00050 // --------------------------------------------------------
00051 NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLLinkAccessibleWrap, nsHTMLLinkAccessible, nsIAccessibleHyperLink)
00052 
00053 nsHTMLLinkAccessibleWrap::nsHTMLLinkAccessibleWrap(nsIDOMNode* aDomNode, nsIArray* aTextNodes, nsIWeakReference* aShell, nsIFrame *aFrame):
00054 nsHTMLLinkAccessible(aDomNode, aShell, aFrame)
00055 { 
00056   mTextNodes = aTextNodes;
00057 }
00058 
00059 //-------------------------- nsIAccessibleHyperLink -------------------------
00060 /* readonly attribute long anchors; */
00061 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetAnchors(PRInt32 *aAnchors)
00062 {
00063   if (!mIsLink)
00064     return NS_ERROR_FAILURE;
00065   
00066   *aAnchors = 1;
00067   return NS_OK;
00068 }
00069 
00070 /* readonly attribute long startIndex; */
00071 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetStartIndex(PRInt32 *aStartIndex)
00072 {
00073   PRInt32 endIndex;
00074   return GetLinkOffset(aStartIndex, &endIndex);
00075 }
00076 
00077 /* readonly attribute long endIndex; */
00078 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetEndIndex(PRInt32 *aEndIndex)
00079 {
00080   PRInt32 startIndex;
00081   return GetLinkOffset(&startIndex, aEndIndex);
00082 }
00083 
00084 /* nsIURI getURI (in long i); */
00085 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetURI(PRInt32 i, nsIURI **aURI)
00086 {
00087   //I do not know why we have to return a nsIURI instead of
00088   //nsILink or just a string of url. Anyway, maybe nsIURI is
00089   //more powerful for the future.
00090   *aURI = nsnull;
00091 
00092   if (!mIsLink)
00093     return NS_ERROR_FAILURE;
00094 
00095   nsCOMPtr<nsILink> link(do_QueryInterface(mActionContent));
00096   if (link) {
00097     return link->GetHrefURI(aURI);
00098   }
00099 
00100   return NS_ERROR_FAILURE;
00101 }
00102 
00103 /* nsIAccessible getObject (in long i); */
00104 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetObject(PRInt32 aIndex,
00105                                               nsIAccessible **aAccessible)
00106 {
00107   if (0 != aIndex)
00108     return NS_ERROR_FAILURE;
00109   
00110   return QueryInterface(NS_GET_IID(nsIAccessible), (void **)aAccessible);
00111 }
00112 
00113 /* boolean isValid (); */
00114 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::IsValid(PRBool *aIsValid)
00115 {
00116   // I have not found the cause which makes this attribute false.
00117   *aIsValid = PR_TRUE;
00118   return NS_OK;
00119 }
00120 
00121 /* boolean isSelected (); */
00122 NS_IMETHODIMP nsHTMLLinkAccessibleWrap::IsSelected(PRBool *aIsSelected)
00123 {
00124   *aIsSelected = (gLastFocusedNode == mDOMNode);
00125   return NS_OK;
00126 }
00127 
00128 nsresult nsHTMLLinkAccessibleWrap::GetLinkOffset(PRInt32* aStartOffset, PRInt32* aEndOffset)
00129 {
00130   NS_ENSURE_TRUE(mTextNodes, NS_ERROR_FAILURE);
00131 
00132   if (!mIsLink)
00133     return NS_ERROR_FAILURE;
00134 
00135   nsCOMPtr<nsILink> currentLink(do_QueryInterface(mActionContent));
00136   NS_ENSURE_TRUE(currentLink, NS_ERROR_FAILURE);
00137 
00138   PRUint32 index, count = 0;
00139   PRUint32 totalLength = 0, textLength = 0;
00140 
00141   mTextNodes->GetLength(&count);
00142   for (index = 0; index < count; index++) {
00143     nsCOMPtr<nsIDOMNode> domNode(do_QueryElementAt(mTextNodes, index));
00144     nsCOMPtr<nsIDOMText> domText(do_QueryInterface(domNode));
00145     if (domText) {
00146       domText->GetLength(&textLength);
00147       totalLength += textLength;
00148     }
00149 
00150     // text node maybe a child (or grandchild, ...) of a link node
00151     nsCOMPtr<nsIDOMNode> parentNode;
00152     nsCOMPtr<nsILink> link = nsnull;
00153     domNode->GetParentNode(getter_AddRefs(parentNode));
00154     while (parentNode) {
00155       link = do_QueryInterface(parentNode);
00156       if (link)
00157         break;
00158       nsCOMPtr<nsIDOMNode> temp = parentNode;
00159       temp->GetParentNode(getter_AddRefs(parentNode));
00160     }
00161 
00162     if (link == currentLink) {
00163       *aEndOffset = totalLength;
00164       *aStartOffset = totalLength - textLength;
00165       return NS_OK;
00166     }
00167   }
00168   return NS_ERROR_FAILURE;
00169 }
00170 
00171 // --------------------------------------------------------
00172 // nsHTMLImageMapAccessible Accessible
00173 // --------------------------------------------------------
00174 NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLImageMapAccessible, nsHTMLImageAccessible, nsIAccessibleHyperLink)
00175 
00176 nsHTMLImageMapAccessible::nsHTMLImageMapAccessible(nsIDOMNode* aDomNode, nsIWeakReference* aShell):
00177 nsHTMLImageAccessible(aDomNode, aShell)
00178 {
00179 }
00180 
00181 /* readonly attribute long anchors; */
00182 NS_IMETHODIMP nsHTMLImageMapAccessible::GetAnchors(PRInt32 *aAnchors)
00183 {
00184   return GetChildCount(aAnchors);
00185 }
00186 
00187 /* readonly attribute long startIndex; */
00188 NS_IMETHODIMP nsHTMLImageMapAccessible::GetStartIndex(PRInt32 *aStartIndex)
00189 {
00190   //should not be supported in image map hyperlink
00191   return NS_ERROR_NOT_IMPLEMENTED;
00192 }
00193 
00194 /* readonly attribute long endIndex; */
00195 NS_IMETHODIMP nsHTMLImageMapAccessible::GetEndIndex(PRInt32 *aEndIndex)
00196 {
00197   //should not be supported in image map hyperlink
00198   return NS_ERROR_NOT_IMPLEMENTED;
00199 }
00200 
00201 /* nsIURI getURI (in long i); */
00202 NS_IMETHODIMP nsHTMLImageMapAccessible::GetURI(PRInt32 aIndex, nsIURI **aURI)
00203 {
00204   *aURI = nsnull;
00205 
00206   nsCOMPtr<nsIDOMHTMLCollection> mapAreas;
00207   mMapElement->GetAreas(getter_AddRefs(mapAreas));
00208   if (!mapAreas)
00209     return NS_ERROR_FAILURE;
00210 
00211   nsCOMPtr<nsIDOMNode> domNode;
00212   mapAreas->Item(aIndex,getter_AddRefs(domNode));
00213   if (!domNode)
00214     return NS_ERROR_FAILURE;
00215 
00216   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00217   if (content) {
00218     nsCOMPtr<nsIURI> baseURI = content->GetBaseURI();
00219 
00220     nsCOMPtr<nsIDOMElement> area(do_QueryInterface(domNode));
00221     nsAutoString hrefValue;
00222     if (NS_SUCCEEDED(area->GetAttribute(NS_LITERAL_STRING("href"), hrefValue))) {
00223       return NS_NewURI(aURI, hrefValue, nsnull, baseURI);
00224     }
00225   }
00226 
00227   return NS_ERROR_FAILURE;
00228 }
00229 
00230 /* nsIAccessible getObject (in long i); */
00231 NS_IMETHODIMP nsHTMLImageMapAccessible::GetObject(PRInt32 aIndex,
00232                                                   nsIAccessible **aAccessible)
00233 {
00234   *aAccessible = nsnull;
00235   nsCOMPtr<nsIAccessible> areaAccessible;
00236   nsresult rv = GetChildAt(aIndex, getter_AddRefs(areaAccessible));
00237   areaAccessible.swap(*aAccessible);
00238   return rv;
00239 }
00240 
00241 /* boolean isValid (); */
00242 NS_IMETHODIMP nsHTMLImageMapAccessible::IsValid(PRBool *aIsValid)
00243 {
00244   *aIsValid = PR_TRUE;
00245   return NS_OK;
00246 }
00247 
00248 /* boolean isSelected (); */
00249 NS_IMETHODIMP nsHTMLImageMapAccessible::IsSelected(PRBool *aIsSelected)
00250 {
00251   *aIsSelected = PR_FALSE;
00252   return NS_OK;
00253 }