diff --git a/accessible/src/atk/nsHTMLLinkAccessibleWrap.cpp b/accessible/src/atk/nsHTMLLinkAccessibleWrap.cpp index 4c1d365ab4a..c4ff646291c 100644 --- a/accessible/src/atk/nsHTMLLinkAccessibleWrap.cpp +++ b/accessible/src/atk/nsHTMLLinkAccessibleWrap.cpp @@ -90,7 +90,7 @@ NS_IMETHODIMP nsHTMLLinkAccessibleWrap::GetURI(PRInt32 i, nsIURI **aURI) nsCOMPtr link(do_QueryInterface(mLinkContent)); if (link) { nsXPIDLCString hrefValue; - if (NS_SUCCEEDED(link->GetHrefCString(*getter_Copies(hrefValue)))) { + if (NS_SUCCEEDED(link->GetHrefUTF8(getter_Copies(hrefValue)))) { return NS_NewURI(aURI, hrefValue, nsnull, nsnull); } } diff --git a/content/html/content/public/nsILink.h b/content/html/content/public/nsILink.h index d058c1d4aa9..eaebb9ac45d 100644 --- a/content/html/content/public/nsILink.h +++ b/content/html/content/public/nsILink.h @@ -76,10 +76,10 @@ public: NS_IMETHOD SetLinkState(nsLinkState aState) = 0; /** - * Get a pointer to the canonical URL (i.e., fully resolved to the - * base URL) that this linkable element points to. The buffer - * returned has been allocated for and should be deleted by the - * caller. + * Get a pointer to the UTF-8 encoded canonical URL (i.e., fully + * resolved to the base URL) that this link element points to. The + * buffer returned has been allocated for and should be deleted by + * the caller. * * @param aBuf [out] A pointer to be filled in with a pointer to a * null-terminated string containing the canonical URL. @@ -87,7 +87,7 @@ public: * nsnull. * @return NS_OK if the out pointer is filled in */ - NS_IMETHOD GetHrefCString(char* &aBuf) = 0; + NS_IMETHOD GetHrefUTF8(char** aBuf) = 0; }; diff --git a/content/html/content/src/nsGenericHTMLElement.cpp b/content/html/content/src/nsGenericHTMLElement.cpp index d0532a568e8..69b34bbaaa4 100644 --- a/content/html/content/src/nsGenericHTMLElement.cpp +++ b/content/html/content/src/nsGenericHTMLElement.cpp @@ -1403,14 +1403,15 @@ nsGenericHTMLElement::FindAndSetForm(nsIFormControl *aFormControl) } nsresult -nsGenericHTMLElement::HandleDOMEventForAnchors(nsIContent* aOuter, - nsIPresContext* aPresContext, +nsGenericHTMLElement::HandleDOMEventForAnchors(nsIPresContext* aPresContext, nsEvent* aEvent, nsIDOMEvent** aDOMEvent, PRUint32 aFlags, nsEventStatus* aEventStatus) { NS_ENSURE_ARG_POINTER(aEventStatus); + NS_PRECONDITION(nsCOMPtr(do_QueryInterface(this)), + "should be called only when |this| implements |nsILink|"); // Try script event handlers first nsresult ret = nsGenericHTMLElement::HandleDOMEvent(aPresContext, aEvent, @@ -1462,16 +1463,12 @@ nsGenericHTMLElement::HandleDOMEventForAnchors(nsIContent* aOuter, aEvent->message == NS_MOUSE_EXIT_SYNTH))) && !(aFlags & NS_EVENT_FLAG_CAPTURE) && !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT)) { - // If we're here, then aOuter should be an nsILink. We'll use the + // We'll use the equivalent of |GetHrefUTF8| on the // nsILink interface to get a canonified URL that has been // correctly escaped and URL-encoded for the document's charset. - nsCOMPtr link = do_QueryInterface(aOuter); - if (!link) - return NS_ERROR_UNEXPECTED; - nsXPIDLCString hrefCStr; - link->GetHrefCString(*getter_Copies(hrefCStr)); + GetHrefUTF8ForAnchors(getter_Copies(hrefCStr)); // Only bother to handle the mouse event if there was an href // specified. @@ -1600,6 +1597,45 @@ nsGenericHTMLElement::HandleDOMEventForAnchors(nsIContent* aOuter, return ret; } +nsresult +nsGenericHTMLElement::GetHrefUTF8ForAnchors(char** aHref) +{ + // This is used by the three nsILink implementations and + // nsHTMLStyleElement. + + // Get href= attribute (relative URL). + nsAutoString relURLSpec; + + if (NS_CONTENT_ATTR_HAS_VALUE == + GetAttr(kNameSpaceID_None, nsHTMLAtoms::href, relURLSpec)) { + // Clean up any leading or trailing whitespace + relURLSpec.Trim(" \t\n\r"); + + // Get base URL. + nsCOMPtr baseURL; + GetBaseURL(getter_AddRefs(baseURL)); + + if (baseURL) { + // Get absolute URL. + nsCAutoString buf; + NS_MakeAbsoluteURIWithCharset(buf, relURLSpec, mDocument, baseURL, + nsHTMLUtils::IOService, + nsHTMLUtils::CharsetMgr); + *aHref = ToNewCString(buf); + } + else { + // Absolute URL is same as relative URL. + *aHref = ToNewUTF8String(relURLSpec); + } + } + else { + // Absolute URL is null to say we have no HREF. + *aHref = nsnull; + } + + return NS_OK; +} + NS_IMETHODIMP nsGenericHTMLElement::GetNameSpaceID(PRInt32* aID) const { diff --git a/content/html/content/src/nsGenericHTMLElement.h b/content/html/content/src/nsGenericHTMLElement.h index fe8f78f186c..67d052baa28 100644 --- a/content/html/content/src/nsGenericHTMLElement.h +++ b/content/html/content/src/nsGenericHTMLElement.h @@ -206,14 +206,19 @@ public: /** * Standard anchor HandleDOMEvent, used by A, AREA and LINK (parameters * are the same as HandleDOMEvent) + * + * Callers must hold a reference to nsHTMLUtils's global reference count. */ - nsresult HandleDOMEventForAnchors(nsIContent* aOuter, - nsIPresContext* aPresContext, + nsresult HandleDOMEventForAnchors(nsIPresContext* aPresContext, nsEvent* aEvent, nsIDOMEvent** aDOMEvent, PRUint32 aFlags, nsEventStatus* aEventStatus); + // Used by A, AREA, LINK, and STYLE. + // Callers must hold a reference to nsHTMLUtils's global reference count. + nsresult GetHrefUTF8ForAnchors(char** aHref); + // Implementation for nsIHTMLContent NS_IMETHOD SetHTMLAttribute(nsIAtom* aAttribute, const nsHTMLValue& aValue, PRBool aNotify); diff --git a/content/html/content/src/nsHTMLAnchorElement.cpp b/content/html/content/src/nsHTMLAnchorElement.cpp index a333b2e1890..e6e27f4d975 100644 --- a/content/html/content/src/nsHTMLAnchorElement.cpp +++ b/content/html/content/src/nsHTMLAnchorElement.cpp @@ -106,7 +106,7 @@ public: // nsILink NS_IMETHOD GetLinkState(nsLinkState &aState); NS_IMETHOD SetLinkState(nsLinkState aState); - NS_IMETHOD GetHrefCString(char* &aBuf); + NS_IMETHOD GetHrefUTF8(char** aBuf); NS_IMETHOD SetDocument(nsIDocument* aDocument, PRBool aDeep, PRBool aCompileEventHandlers); @@ -164,12 +164,12 @@ NS_NewHTMLAnchorElement(nsIHTMLContent** aInstancePtrResult, nsHTMLAnchorElement::nsHTMLAnchorElement() : mLinkState(eLinkState_Unknown) { - nsHTMLUtils::AddRef(); // for GetHrefCString + nsHTMLUtils::AddRef(); // for GetHrefUTF8 } nsHTMLAnchorElement::~nsHTMLAnchorElement() { - nsHTMLUtils::Release(); // for GetHrefCString + nsHTMLUtils::Release(); // for GetHrefUTF8 } @@ -350,7 +350,7 @@ nsHTMLAnchorElement::HandleDOMEvent(nsIPresContext* aPresContext, PRUint32 aFlags, nsEventStatus* aEventStatus) { - return HandleDOMEventForAnchors(this, aPresContext, aEvent, aDOMEvent, + return HandleDOMEventForAnchors(aPresContext, aEvent, aDOMEvent, aFlags, aEventStatus); } @@ -358,7 +358,7 @@ NS_IMETHODIMP nsHTMLAnchorElement::GetHref(nsAString& aValue) { char *buf; - nsresult rv = GetHrefCString(buf); + nsresult rv = GetHrefUTF8(&buf); if (NS_FAILED(rv)) return rv; if (buf) { aValue.Assign(NS_ConvertUTF8toUCS2(buf)); @@ -661,39 +661,9 @@ nsHTMLAnchorElement::SetLinkState(nsLinkState aState) } NS_IMETHODIMP -nsHTMLAnchorElement::GetHrefCString(char* &aBuf) +nsHTMLAnchorElement::GetHrefUTF8(char** aBuf) { - // Get href= attribute (relative URL). - nsAutoString relURLSpec; - - if (NS_CONTENT_ATTR_HAS_VALUE == - GetAttr(kNameSpaceID_None, nsHTMLAtoms::href, relURLSpec)) { - // Clean up any leading or trailing whitespace - relURLSpec.Trim(" \t\n\r"); - - // Get base URL. - nsCOMPtr baseURL; - GetBaseURL(getter_AddRefs(baseURL)); - - if (baseURL) { - // Get absolute URL. - nsCAutoString buf; - NS_MakeAbsoluteURIWithCharset(buf, relURLSpec, mDocument, baseURL, - nsHTMLUtils::IOService, - nsHTMLUtils::CharsetMgr); - aBuf = ToNewCString(buf); - } - else { - // Absolute URL is same as relative URL. - aBuf = ToNewUTF8String(relURLSpec); - } - } - else { - // Absolute URL is null to say we have no HREF. - aBuf = nsnull; - } - - return NS_OK; + return GetHrefUTF8ForAnchors(aBuf); } NS_IMETHODIMP diff --git a/content/html/content/src/nsHTMLAreaElement.cpp b/content/html/content/src/nsHTMLAreaElement.cpp index 7cf96de4668..fd3dfd81214 100644 --- a/content/html/content/src/nsHTMLAreaElement.cpp +++ b/content/html/content/src/nsHTMLAreaElement.cpp @@ -83,7 +83,7 @@ public: // nsILink NS_IMETHOD GetLinkState(nsLinkState &aState); NS_IMETHOD SetLinkState(nsLinkState aState); - NS_IMETHOD GetHrefCString(char* &aBuf); + NS_IMETHOD GetHrefUTF8(char** aBuf); NS_IMETHOD StringToAttribute(nsIAtom* aAttribute, const nsAString& aValue, @@ -140,12 +140,12 @@ NS_NewHTMLAreaElement(nsIHTMLContent** aInstancePtrResult, nsHTMLAreaElement::nsHTMLAreaElement() : mLinkState(eLinkState_Unknown) { - nsHTMLUtils::AddRef(); // for GetHrefCString + nsHTMLUtils::AddRef(); // for GetHrefUTF8 } nsHTMLAreaElement::~nsHTMLAreaElement() { - nsHTMLUtils::Release(); // for GetHrefCString + nsHTMLUtils::Release(); // for GetHrefUTF8 } NS_IMPL_ADDREF_INHERITED(nsHTMLAreaElement, nsGenericElement) @@ -225,7 +225,7 @@ nsHTMLAreaElement::HandleDOMEvent(nsIPresContext* aPresContext, PRUint32 aFlags, nsEventStatus* aEventStatus) { - return HandleDOMEventForAnchors(this, aPresContext, aEvent, aDOMEvent, + return HandleDOMEventForAnchors(aPresContext, aEvent, aDOMEvent, aFlags, aEventStatus); } @@ -274,7 +274,7 @@ NS_IMETHODIMP nsHTMLAreaElement::GetHref(nsAString& aValue) { char *buf; - nsresult rv = GetHrefCString(buf); + nsresult rv = GetHrefUTF8(&buf); if (NS_FAILED(rv)) return rv; if (buf) { aValue.Assign(NS_ConvertASCIItoUCS2(buf)); @@ -571,36 +571,7 @@ nsHTMLAreaElement::SetLinkState(nsLinkState aState) } NS_IMETHODIMP -nsHTMLAreaElement::GetHrefCString(char* &aBuf) +nsHTMLAreaElement::GetHrefUTF8(char** aBuf) { - // Get href= attribute (relative URL). - nsAutoString relURLSpec; - - if (GetAttr(kNameSpaceID_None, nsHTMLAtoms::href, relURLSpec) == - NS_CONTENT_ATTR_HAS_VALUE) { - // Clean up any leading or trailing whitespace - relURLSpec.Trim(" \t\n\r"); - - // Get base URL. - nsCOMPtr baseURL; - GetBaseURL(getter_AddRefs(baseURL)); - - if (baseURL) { - // Get absolute URL. - nsCAutoString buf; - NS_MakeAbsoluteURIWithCharset(buf, relURLSpec, mDocument, baseURL, - nsHTMLUtils::IOService, nsHTMLUtils::CharsetMgr); - aBuf = ToNewCString(buf); - } - else { - // Absolute URL is same as relative URL. - aBuf = ToNewUTF8String(relURLSpec); - } - } - else { - // Absolute URL is empty because we have no HREF. - aBuf = nsnull; - } - - return NS_OK; + return GetHrefUTF8ForAnchors(aBuf); } diff --git a/content/html/content/src/nsHTMLLinkElement.cpp b/content/html/content/src/nsHTMLLinkElement.cpp index a166734b0c8..4730df7f76e 100644 --- a/content/html/content/src/nsHTMLLinkElement.cpp +++ b/content/html/content/src/nsHTMLLinkElement.cpp @@ -86,7 +86,7 @@ public: // nsILink NS_IMETHOD GetLinkState(nsLinkState &aState); NS_IMETHOD SetLinkState(nsLinkState aState); - NS_IMETHOD GetHrefCString(char* &aBuf); + NS_IMETHOD GetHrefUTF8(char** aBuf); NS_IMETHOD SetDocument(nsIDocument* aDocument, PRBool aDeep, PRBool aCompileEventHandlers) { @@ -226,12 +226,12 @@ NS_NewHTMLLinkElement(nsIHTMLContent** aInstancePtrResult, nsHTMLLinkElement::nsHTMLLinkElement() : mLinkState(eLinkState_Unknown) { - nsHTMLUtils::AddRef(); // for GetHrefCString + nsHTMLUtils::AddRef(); // for GetHrefUTF8 } nsHTMLLinkElement::~nsHTMLLinkElement() { - nsHTMLUtils::Release(); // for GetHrefCString + nsHTMLUtils::Release(); // for GetHrefUTF8 } @@ -321,10 +321,10 @@ NS_IMETHODIMP nsHTMLLinkElement::GetHref(nsAString& aValue) { char *buf; - nsresult rv = GetHrefCString(buf); + nsresult rv = GetHrefUTF8(&buf); if (NS_FAILED(rv)) return rv; if (buf) { - aValue.Assign(NS_ConvertASCIItoUCS2(buf)); + aValue.Assign(NS_ConvertUTF8toUCS2(buf)); nsCRT::free(buf); } @@ -356,7 +356,7 @@ nsHTMLLinkElement::HandleDOMEvent(nsIPresContext* aPresContext, PRUint32 aFlags, nsEventStatus* aEventStatus) { - return HandleDOMEventForAnchors(this, aPresContext, aEvent, aDOMEvent, + return HandleDOMEventForAnchors(aPresContext, aEvent, aDOMEvent, aFlags, aEventStatus); } @@ -375,40 +375,9 @@ nsHTMLLinkElement::SetLinkState(nsLinkState aState) } NS_IMETHODIMP -nsHTMLLinkElement::GetHrefCString(char* &aBuf) +nsHTMLLinkElement::GetHrefUTF8(char** aBuf) { - // Get href= attribute (relative URL). - nsAutoString relURLSpec; - - if (NS_CONTENT_ATTR_HAS_VALUE == - nsGenericHTMLLeafElement::GetAttr(kNameSpaceID_None, - nsHTMLAtoms::href, relURLSpec)) { - // Clean up any leading or trailing whitespace - relURLSpec.Trim(" \t\n\r"); - - // Get base URL. - nsCOMPtr baseURL; - GetBaseURL(getter_AddRefs(baseURL)); - - if (baseURL) { - // Get absolute URL. - nsCAutoString buf; - NS_MakeAbsoluteURIWithCharset(buf, relURLSpec, mDocument, baseURL, - nsHTMLUtils::IOService, - nsHTMLUtils::CharsetMgr); - aBuf = ToNewCString(buf); - } - else { - // Absolute URL is same as relative URL. - aBuf = ToNewUTF8String(relURLSpec); - } - } - else { - // Absolute URL is empty because we have no HREF. - aBuf = nsnull; - } - - return NS_OK; + return GetHrefUTF8ForAnchors(aBuf); } void diff --git a/content/html/content/src/nsHTMLStyleElement.cpp b/content/html/content/src/nsHTMLStyleElement.cpp index a239ef1a7a5..f00368dbcf5 100644 --- a/content/html/content/src/nsHTMLStyleElement.cpp +++ b/content/html/content/src/nsHTMLStyleElement.cpp @@ -167,7 +167,6 @@ public: nsresult SetInnerHTML(const nsAString& aInnerHTML); protected: - nsresult GetHrefCString(char* &aBuf); void GetStyleSheetURL(PRBool* aIsInline, nsAString& aUrl); void GetStyleSheetInfo(nsAString& aTitle, @@ -205,12 +204,12 @@ NS_NewHTMLStyleElement(nsIHTMLContent** aInstancePtrResult, nsHTMLStyleElement::nsHTMLStyleElement() { - nsHTMLUtils::AddRef(); // for GetHrefCString + nsHTMLUtils::AddRef(); // for GetHrefUTF8ForAnchors } nsHTMLStyleElement::~nsHTMLStyleElement() { - nsHTMLUtils::Release(); // for GetHrefCString + nsHTMLUtils::Release(); // for GetHrefUTF8ForAnchors } @@ -313,43 +312,6 @@ nsHTMLStyleElement::SetInnerHTML(const nsAString& aInnerHTML) return rv; } -nsresult -nsHTMLStyleElement::GetHrefCString(char* &aBuf) -{ - // Get src= attribute (relative URL). - nsAutoString relURLSpec; - - if (NS_CONTENT_ATTR_HAS_VALUE == - nsGenericHTMLContainerElement::GetAttr(kNameSpaceID_None, - nsHTMLAtoms::src, relURLSpec)) { - // Clean up any leading or trailing whitespace - relURLSpec.Trim(" \t\n\r"); - - // Get base URL. - nsCOMPtr baseURL; - GetBaseURL(getter_AddRefs(baseURL)); - - if (baseURL) { - // Get absolute URL. - nsCAutoString buf; - NS_MakeAbsoluteURIWithCharset(buf, relURLSpec, mDocument, baseURL, - nsHTMLUtils::IOService, - nsHTMLUtils::CharsetMgr); - aBuf = ToNewCString(buf); - } - else { - // Absolute URL is same as relative URL. - aBuf = ToNewUTF8String(relURLSpec); - } - } - else { - // Absolute URL is empty because we have no HREF. - aBuf = nsnull; - } - - return NS_OK; -} - void nsHTMLStyleElement::GetStyleSheetURL(PRBool* aIsInline, nsAString& aUrl) @@ -367,9 +329,9 @@ nsHTMLStyleElement::GetStyleSheetURL(PRBool* aIsInline, } char *buf; - GetHrefCString(buf); + GetHrefUTF8ForAnchors(&buf); if (buf) { - aUrl.Assign(NS_ConvertASCIItoUCS2(buf)); + aUrl.Assign(NS_ConvertUTF8toUCS2(buf)); nsCRT::free(buf); } return; diff --git a/content/html/style/src/nsStyleUtil.cpp b/content/html/style/src/nsStyleUtil.cpp index 072173ea1b9..903db3e8750 100644 --- a/content/html/style/src/nsStyleUtil.cpp +++ b/content/html/style/src/nsStyleUtil.cpp @@ -596,7 +596,7 @@ PRBool nsStyleUtil::IsHTMLLink(nsIContent *aContent, nsIAtom *aTag, nsIPresConte // bug=23209 nsXPIDLCString href; - link->GetHrefCString(*getter_Copies(href)); + link->GetHrefUTF8(getter_Copies(href)); if (href) { nsILinkHandler *linkHandler = nsnull; diff --git a/layout/style/nsStyleUtil.cpp b/layout/style/nsStyleUtil.cpp index 072173ea1b9..903db3e8750 100644 --- a/layout/style/nsStyleUtil.cpp +++ b/layout/style/nsStyleUtil.cpp @@ -596,7 +596,7 @@ PRBool nsStyleUtil::IsHTMLLink(nsIContent *aContent, nsIAtom *aTag, nsIPresConte // bug=23209 nsXPIDLCString href; - link->GetHrefCString(*getter_Copies(href)); + link->GetHrefUTF8(getter_Copies(href)); if (href) { nsILinkHandler *linkHandler = nsnull;