Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLLinkElement.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: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsIDOMHTMLLinkElement.h"
00038 #include "nsIDOMLinkStyle.h"
00039 #include "nsIDOMEventReceiver.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsILink.h"
00042 #include "nsHTMLAtoms.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsPresContext.h"
00045 #include "nsIDOMStyleSheet.h"
00046 #include "nsIStyleSheet.h"
00047 #include "nsIStyleSheetLinkingElement.h"
00048 #include "nsStyleLinkElement.h"
00049 #include "nsReadableUtils.h"
00050 #include "nsUnicharUtils.h"
00051 #include "nsIURL.h"
00052 #include "nsNetUtil.h"
00053 #include "nsIDocument.h"
00054 #include "nsIDOMEvent.h"
00055 #include "nsIPrivateDOMEvent.h"
00056 #include "nsIDOMDocumentEvent.h"
00057 #include "nsIDOMEventTarget.h"
00058 #include "nsParserUtils.h"
00059 #include "nsPIDOMWindow.h"
00060 #include "nsIScriptGlobalObject.h"
00061 
00062 class nsHTMLLinkElement : public nsGenericHTMLElement,
00063                           public nsIDOMHTMLLinkElement,
00064                           public nsILink,
00065                           public nsStyleLinkElement
00066 {
00067 public:
00068   nsHTMLLinkElement(nsINodeInfo *aNodeInfo);
00069   virtual ~nsHTMLLinkElement();
00070 
00071   // nsISupports
00072   NS_DECL_ISUPPORTS_INHERITED
00073 
00074   // nsIDOMNode
00075   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLElement::)
00076 
00077   // nsIDOMElement
00078   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLElement::)
00079 
00080   // nsIDOMHTMLElement
00081   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLElement::)
00082 
00083   // nsIDOMHTMLLinkElement
00084   NS_DECL_NSIDOMHTMLLINKELEMENT
00085 
00086   // nsILink
00087   NS_IMETHOD    GetLinkState(nsLinkState &aState);
00088   NS_IMETHOD    SetLinkState(nsLinkState aState);
00089   NS_IMETHOD    GetHrefURI(nsIURI** aURI);
00090   NS_IMETHOD    LinkAdded();
00091   NS_IMETHOD    LinkRemoved();
00092 
00093   virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00094                               nsIContent* aBindingParent,
00095                               PRBool aCompileEventHandlers);
00096   virtual void UnbindFromTree(PRBool aDeep = PR_TRUE,
00097                               PRBool aNullParent = PR_TRUE);
00098   void CreateAndDispatchEvent(nsIDocument* aDoc, const nsAString& aEventName);
00099   nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00100                    const nsAString& aValue, PRBool aNotify)
00101   {
00102     return SetAttr(aNameSpaceID, aName, nsnull, aValue, aNotify);
00103   }
00104   virtual nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00105                            nsIAtom* aPrefix, const nsAString& aValue,
00106                            PRBool aNotify);
00107   virtual nsresult UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00108                              PRBool aNotify);
00109 
00110   virtual nsresult HandleDOMEvent(nsPresContext* aPresContext,
00111                                   nsEvent* aEvent, nsIDOMEvent** aDOMEvent,
00112                                   PRUint32 aFlags,
00113                                   nsEventStatus* aEventStatus);
00114 
00115 protected:
00116   virtual void GetStyleSheetURL(PRBool* aIsInline,
00117                                 nsIURI** aURI);
00118   virtual void GetStyleSheetInfo(nsAString& aTitle,
00119                                  nsAString& aType,
00120                                  nsAString& aMedia,
00121                                  PRBool* aIsAlternate);
00122  
00123   // The cached visited state
00124   nsLinkState mLinkState;
00125 };
00126 
00127 
00128 NS_IMPL_NS_NEW_HTML_ELEMENT(Link)
00129 
00130 
00131 nsHTMLLinkElement::nsHTMLLinkElement(nsINodeInfo *aNodeInfo)
00132   : nsGenericHTMLElement(aNodeInfo),
00133     mLinkState(eLinkState_Unknown)
00134 {
00135 }
00136 
00137 nsHTMLLinkElement::~nsHTMLLinkElement()
00138 {
00139 }
00140 
00141 
00142 NS_IMPL_ADDREF_INHERITED(nsHTMLLinkElement, nsGenericElement) 
00143 NS_IMPL_RELEASE_INHERITED(nsHTMLLinkElement, nsGenericElement) 
00144 
00145 
00146 // QueryInterface implementation for nsHTMLLinkElement
00147 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLLinkElement, nsGenericHTMLElement)
00148   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLLinkElement)
00149   NS_INTERFACE_MAP_ENTRY(nsIDOMLinkStyle)
00150   NS_INTERFACE_MAP_ENTRY(nsILink)
00151   NS_INTERFACE_MAP_ENTRY(nsIStyleSheetLinkingElement)
00152   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLLinkElement)
00153 NS_HTML_CONTENT_INTERFACE_MAP_END
00154 
00155 
00156 NS_IMPL_DOM_CLONENODE(nsHTMLLinkElement)
00157 
00158 
00159 NS_IMETHODIMP
00160 nsHTMLLinkElement::GetDisabled(PRBool* aDisabled)
00161 {
00162   nsCOMPtr<nsIDOMStyleSheet> ss(do_QueryInterface(mStyleSheet));
00163   nsresult result = NS_OK;
00164 
00165   if (ss) {
00166     result = ss->GetDisabled(aDisabled);
00167   } else {
00168     *aDisabled = PR_FALSE;
00169   }
00170 
00171   return result;
00172 }
00173 
00174 NS_IMETHODIMP 
00175 nsHTMLLinkElement::SetDisabled(PRBool aDisabled)
00176 {
00177   nsCOMPtr<nsIDOMStyleSheet> ss(do_QueryInterface(mStyleSheet));
00178   nsresult result = NS_OK;
00179 
00180   if (ss) {
00181     result = ss->SetDisabled(aDisabled);
00182   }
00183 
00184   return result;
00185 }
00186 
00187 
00188 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Charset, charset)
00189 NS_IMPL_URI_ATTR(nsHTMLLinkElement, Href, href)
00190 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Hreflang, hreflang)
00191 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Media, media)
00192 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Rel, rel)
00193 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Rev, rev)
00194 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Target, target)
00195 NS_IMPL_STRING_ATTR(nsHTMLLinkElement, Type, type)
00196 
00197 nsresult
00198 nsHTMLLinkElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00199                               nsIContent* aBindingParent,
00200                               PRBool aCompileEventHandlers)
00201 {
00202   nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent,
00203                                                  aBindingParent,
00204                                                  aCompileEventHandlers);
00205   NS_ENSURE_SUCCESS(rv, rv);
00206 
00207   UpdateStyleSheet(nsnull);
00208 
00209   // XXXbz we really shouldn't fire the event until after we've finished with
00210   // the outermost BindToTree...  In particular, this can effectively cause us
00211   // to reenter this code, or for some part of the document to become unbound
00212   // inside the event!
00213   CreateAndDispatchEvent(aDocument, NS_LITERAL_STRING("DOMLinkAdded"));
00214 
00215   return rv;  
00216 }
00217 
00218 NS_IMETHODIMP
00219 nsHTMLLinkElement::LinkAdded()
00220 {
00221   CreateAndDispatchEvent(GetOwnerDoc(), NS_LITERAL_STRING("DOMLinkAdded"));
00222   return NS_OK;
00223 }
00224 
00225 NS_IMETHODIMP
00226 nsHTMLLinkElement::LinkRemoved()
00227 {
00228   CreateAndDispatchEvent(GetOwnerDoc(), NS_LITERAL_STRING("DOMLinkRemoved"));
00229   return NS_OK;
00230 }
00231 
00232 void
00233 nsHTMLLinkElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent)
00234 {
00235   nsCOMPtr<nsIDocument> oldDoc = GetCurrentDoc();
00236   if (oldDoc) {
00237     GetCurrentDoc()->ForgetLink(this);
00238     // If this link is ever reinserted into a document, it might
00239     // be under a different xml:base, so forget the cached state now
00240     mLinkState = eLinkState_Unknown;
00241   }
00242 
00243   // XXXbz we really shouldn't fire the event until after we've finished with
00244   // the outermost UnbindFromTree...  In particular, this can effectively cause
00245   // us to reenter this code, or to be bound to a different tree inside the
00246   // event!
00247   CreateAndDispatchEvent(oldDoc, NS_LITERAL_STRING("DOMLinkRemoved"));
00248   nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
00249   UpdateStyleSheet(oldDoc);
00250 }
00251 
00252 void
00253 nsHTMLLinkElement::CreateAndDispatchEvent(nsIDocument* aDoc,
00254                                           const nsAString& aEventName)
00255 {
00256   if (!aDoc)
00257     return;
00258 
00259   nsAutoString rel;
00260   nsAutoString rev;
00261   GetAttr(kNameSpaceID_None, nsHTMLAtoms::rel, rel);
00262   GetAttr(kNameSpaceID_None, nsHTMLAtoms::rev, rev);
00263 
00264   // In the unlikely case that both rev is specified *and* rel=stylesheet,
00265   // this code will cause the event to fire, on the principle that maybe the
00266   // page really does want to specify that it's author is a stylesheet. Since
00267   // this should never actually happen and the performance hit is minimal,
00268   // doing the "right" thing costs virtually nothing here, even if it doesn't
00269   // make much sense.
00270   if (rev.IsEmpty() &&
00271       (rel.IsEmpty() || rel.LowerCaseEqualsLiteral("stylesheet")))
00272     return;
00273 
00274   nsCOMPtr<nsIDOMDocumentEvent> docEvent(do_QueryInterface(aDoc));
00275   nsCOMPtr<nsIDOMEvent> event;
00276   docEvent->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
00277   if (event) {
00278     event->InitEvent(aEventName, PR_TRUE, PR_TRUE);
00279     nsCOMPtr<nsIDOMEventTarget> target =
00280       do_QueryInterface(NS_STATIC_CAST(nsIDOMNode*, this));
00281     if (target) {
00282       nsCOMPtr<nsIPrivateDOMEvent> privEvent(do_QueryInterface(event));
00283       if (privEvent) {
00284         privEvent->SetTrusted(PR_TRUE);
00285       }
00286       PRBool defaultActionEnabled;
00287       target->DispatchEvent(event, &defaultActionEnabled);
00288     }
00289   }
00290 }
00291 
00292 nsresult
00293 nsHTMLLinkElement::SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00294                            nsIAtom* aPrefix, const nsAString& aValue,
00295                            PRBool aNotify)
00296 {
00297   if (aName == nsHTMLAtoms::href && kNameSpaceID_None == aNameSpaceID) {
00298     nsIDocument* doc = GetCurrentDoc();
00299     if (doc) {
00300       doc->ForgetLink(this);
00301         // The change to 'href' will cause style reresolution which will
00302         // eventually recompute the link state and re-add this element
00303         // to the link map if necessary.
00304     }
00305     SetLinkState(eLinkState_Unknown);
00306   }
00307 
00308   nsresult rv = nsGenericHTMLElement::SetAttr(aNameSpaceID, aName, aPrefix,
00309                                               aValue, aNotify);
00310   if (NS_SUCCEEDED(rv)) {
00311     UpdateStyleSheet();
00312   }
00313 
00314   return rv;
00315 }
00316 
00317 nsresult
00318 nsHTMLLinkElement::UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00319                              PRBool aNotify)
00320 {
00321   nsresult rv = nsGenericHTMLElement::UnsetAttr(aNameSpaceID, aAttribute,
00322                                                 aNotify);
00323   if (NS_SUCCEEDED(rv)) {
00324     UpdateStyleSheet();
00325   }
00326 
00327   return rv;
00328 }
00329 
00330 nsresult
00331 nsHTMLLinkElement::HandleDOMEvent(nsPresContext* aPresContext,
00332                            nsEvent* aEvent,
00333                            nsIDOMEvent** aDOMEvent,
00334                            PRUint32 aFlags,
00335                            nsEventStatus* aEventStatus)
00336 {
00337   return HandleDOMEventForAnchors(aPresContext, aEvent, aDOMEvent,
00338                                   aFlags, aEventStatus);
00339 }
00340 
00341 NS_IMETHODIMP
00342 nsHTMLLinkElement::GetLinkState(nsLinkState &aState)
00343 {
00344   aState = mLinkState;
00345   return NS_OK;
00346 }
00347 
00348 NS_IMETHODIMP
00349 nsHTMLLinkElement::SetLinkState(nsLinkState aState)
00350 {
00351   mLinkState = aState;
00352   return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 nsHTMLLinkElement::GetHrefURI(nsIURI** aURI)
00357 {
00358   return GetHrefURIForAnchors(aURI);
00359 }
00360 
00361 void
00362 nsHTMLLinkElement::GetStyleSheetURL(PRBool* aIsInline,
00363                                     nsIURI** aURI)
00364 {
00365   *aIsInline = PR_FALSE;
00366   GetHrefURIForAnchors(aURI);
00367   return;
00368 }
00369 
00370 void
00371 nsHTMLLinkElement::GetStyleSheetInfo(nsAString& aTitle,
00372                                      nsAString& aType,
00373                                      nsAString& aMedia,
00374                                      PRBool* aIsAlternate)
00375 {
00376   aTitle.Truncate();
00377   aType.Truncate();
00378   aMedia.Truncate();
00379   *aIsAlternate = PR_FALSE;
00380 
00381   nsAutoString rel;
00382   nsStringArray linkTypes(4);
00383   GetAttr(kNameSpaceID_None, nsHTMLAtoms::rel, rel);
00384   nsStyleLinkElement::ParseLinkTypes(rel, linkTypes);
00385   // Is it a stylesheet link?
00386   if (linkTypes.IndexOf(NS_LITERAL_STRING("stylesheet")) < 0) {
00387     return;
00388   }
00389 
00390   nsAutoString title;
00391   GetAttr(kNameSpaceID_None, nsHTMLAtoms::title, title);
00392   title.CompressWhitespace();
00393   aTitle.Assign(title);
00394 
00395   // If alternate, does it have title?
00396   if (-1 != linkTypes.IndexOf(NS_LITERAL_STRING("alternate"))) {
00397     if (aTitle.IsEmpty()) { // alternates must have title
00398       return;
00399     } else {
00400       *aIsAlternate = PR_TRUE;
00401     }
00402   }
00403 
00404   GetAttr(kNameSpaceID_None, nsHTMLAtoms::media, aMedia);
00405   ToLowerCase(aMedia); // HTML4.0 spec is inconsistent, make it case INSENSITIVE
00406 
00407   nsAutoString mimeType;
00408   nsAutoString notUsed;
00409   GetAttr(kNameSpaceID_None, nsHTMLAtoms::type, aType);
00410   nsParserUtils::SplitMimeType(aType, mimeType, notUsed);
00411   if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
00412     return;
00413   }
00414 
00415   // If we get here we assume that we're loading a css file, so set the
00416   // type to 'text/css'
00417   aType.AssignLiteral("text/css");
00418 
00419   return;
00420 }