From 36ef6cbc56e627c65614cd50f1a75ae7a4854876 Mon Sep 17 00:00:00 2001 From: Shawn Wilsher Date: Thu, 18 Feb 2010 10:08:00 -0800 Subject: [PATCH] Backed out changeset 6a2bef8d7020 --- content/base/src/Link.cpp | 312 ----------------- content/base/src/Link.h | 21 -- .../html/content/src/nsGenericHTMLElement.cpp | 329 ++++++++++++++++++ .../html/content/src/nsGenericHTMLElement.h | 18 + .../html/content/src/nsHTMLAnchorElement.cpp | 4 +- .../html/content/src/nsHTMLAreaElement.cpp | 4 +- 6 files changed, 351 insertions(+), 337 deletions(-) diff --git a/content/base/src/Link.cpp b/content/base/src/Link.cpp index 198f0b4722e2..ce1c638997b9 100644 --- a/content/base/src/Link.cpp +++ b/content/base/src/Link.cpp @@ -40,11 +40,6 @@ #include "Link.h" #include "nsIEventStateManager.h" -#include "nsIURL.h" - -#include "nsEscape.h" -#include "nsGkAtoms.h" -#include "nsString.h" namespace mozilla { namespace dom { @@ -104,288 +99,6 @@ Link::GetURI() const return uri.forget(); } -nsresult -Link::SetProtocol(const nsAString &aProtocol) -{ - nsCOMPtr uri(GetURIToMutate()); - if (!uri) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - nsAString::const_iterator start, end; - aProtocol.BeginReading(start); - aProtocol.EndReading(end); - nsAString::const_iterator iter(start); - (void)FindCharInReadable(':', iter, end); - (void)uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))); - - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetHost(const nsAString &aHost) -{ - nsCOMPtr uri(GetURIToMutate()); - if (!uri) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - // We cannot simply call nsIURI::SetHost because that would treat the name as - // an IPv6 address (like http:://[server:443]/). We also cannot call - // nsIURI::SetHostPort because that isn't implemented. Sadfaces. - - // First set the hostname. - nsAString::const_iterator start, end; - aHost.BeginReading(start); - aHost.EndReading(end); - nsAString::const_iterator iter(start); - (void)FindCharInReadable(':', iter, end); - NS_ConvertUTF16toUTF8 host(Substring(start, iter)); - (void)uri->SetHost(host); - - // Also set the port if needed. - if (iter != end) { - iter++; - if (iter != end) { - nsAutoString portStr(Substring(iter, end)); - nsresult rv; - PRInt32 port = portStr.ToInteger((PRInt32 *)&rv); - if (NS_SUCCEEDED(rv)) { - (void)uri->SetPort(port); - } - } - }; - - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetHostname(const nsAString &aHostname) -{ - nsCOMPtr uri(GetURIToMutate()); - if (!uri) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - (void)uri->SetHost(NS_ConvertUTF16toUTF8(aHostname)); - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetPathname(const nsAString &aPathname) -{ - nsCOMPtr uri(GetURIToMutate()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - (void)url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname)); - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetSearch(const nsAString &aSearch) -{ - nsCOMPtr uri(GetURIToMutate()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - (void)url->SetQuery(NS_ConvertUTF16toUTF8(aSearch)); - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetPort(const nsAString &aPort) -{ - nsCOMPtr uri(GetURIToMutate()); - if (!uri) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - nsresult rv; - nsAutoString portStr(aPort); - PRInt32 port = portStr.ToInteger((PRInt32 *)&rv); - if (NS_FAILED(rv)) { - return NS_OK; - } - - (void)uri->SetPort(port); - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::SetHash(const nsAString &aHash) -{ - nsCOMPtr uri(GetURIToMutate()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Ignore failures to be compatible with NS4. - return NS_OK; - } - - (void)url->SetRef(NS_ConvertUTF16toUTF8(aHash)); - SetHrefAttribute(uri); - return NS_OK; -} - -nsresult -Link::GetProtocol(nsAString &_protocol) -{ - nsCOMPtr uri(GetURI()); - if (!uri) { - _protocol.AssignLiteral("http"); - } - else { - nsCAutoString scheme; - (void)uri->GetScheme(scheme); - CopyASCIItoUTF16(scheme, _protocol); - } - _protocol.Append(PRUnichar(':')); - return NS_OK; -} - -nsresult -Link::GetHost(nsAString &_host) -{ - _host.Truncate(); - - nsCOMPtr uri(GetURI()); - if (!uri) { - // Do not throw! Not having a valid URI should result in an empty string. - return NS_OK; - } - - nsCAutoString hostport; - nsresult rv = uri->GetHostPort(hostport); - if (NS_SUCCEEDED(rv)) { - CopyUTF8toUTF16(hostport, _host); - } - return NS_OK; -} - -nsresult -Link::GetHostname(nsAString &_hostname) -{ - _hostname.Truncate(); - - nsCOMPtr uri(GetURI()); - if (!uri) { - // Do not throw! Not having a valid URI should result in an empty string. - return NS_OK; - } - - nsCAutoString host; - nsresult rv = uri->GetHost(host); - // Note that failure to get the host from the URI is not necessarily a bad - // thing. Some URIs do not have a host. - if (NS_SUCCEEDED(rv)) { - CopyUTF8toUTF16(host, _hostname); - } - return NS_OK; -} - -nsresult -Link::GetPathname(nsAString &_pathname) -{ - _pathname.Truncate(); - - nsCOMPtr uri(GetURI()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Do not throw! Not having a valid URI or URL should result in an empty - // string. - return NS_OK; - } - - nsCAutoString file; - nsresult rv = url->GetFilePath(file); - NS_ENSURE_SUCCESS(rv, rv); - CopyUTF8toUTF16(file, _pathname); - return NS_OK; -} - -nsresult -Link::GetSearch(nsAString &_search) -{ - _search.Truncate(); - - nsCOMPtr uri(GetURI()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Do not throw! Not having a valid URI or URL should result in an empty - // string. - return NS_OK; - } - - nsCAutoString search; - nsresult rv = url->GetQuery(search); - if (NS_SUCCEEDED(rv) && !search.IsEmpty()) { - CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, _search); - } - return NS_OK; -} - -nsresult -Link::GetPort(nsAString &_port) -{ - _port.Truncate(); - - nsCOMPtr uri(GetURI()); - if (!uri) { - // Do not throw! Not having a valid URI should result in an empty string. - return NS_OK; - } - - PRInt32 port; - nsresult rv = uri->GetPort(&port); - // Note that failure to get the port from the URI is not necessarily a bad - // thing. Some URIs do not have a port. - if (NS_SUCCEEDED(rv) && port != -1) { - nsAutoString portStr; - portStr.AppendInt(port, 10); - _port.Assign(portStr); - } - return NS_OK; -} - -nsresult -Link::GetHash(nsAString &_hash) -{ - _hash.Truncate(); - - nsCOMPtr uri(GetURI()); - nsCOMPtr url(do_QueryInterface(uri)); - if (!url) { - // Do not throw! Not having a valid URI or URL should result in an empty - // string. - return NS_OK; - } - - nsCAutoString ref; - nsresult rv = url->GetRef(ref); - if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) { - NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes! - _hash.Assign(PRUnichar('#')); - AppendUTF8toUTF16(ref, _hash); - } - return NS_OK; -} - void Link::ResetLinkState() { @@ -405,30 +118,5 @@ Link::ResetLinkState() mCachedURI = nsnull; } -already_AddRefed -Link::GetURIToMutate() -{ - nsCOMPtr uri(GetURI()); - if (!uri) { - return nsnull; - } - nsCOMPtr clone; - (void)uri->Clone(getter_AddRefs(clone)); - return clone.forget(); -} - -void -Link::SetHrefAttribute(nsIURI *aURI) -{ - NS_ASSERTION(aURI, "Null URI is illegal!"); - nsCOMPtr content(do_QueryInterface(this)); - NS_ASSERTION(content, "Why isn't this an nsIContent node?!"); - - nsCAutoString href; - (void)aURI->GetSpec(href); - (void)content->SetAttr(kNameSpaceID_None, nsGkAtoms::href, - NS_ConvertUTF8toUTF16(href), PR_TRUE); -} - } // namespace dom } // namespace mozilla diff --git a/content/base/src/Link.h b/content/base/src/Link.h index 4940ed877947..d9dde365bb0a 100644 --- a/content/base/src/Link.h +++ b/content/base/src/Link.h @@ -69,24 +69,6 @@ public: */ already_AddRefed GetURI() const; - /** - * Helper methods for modifying and obtaining parts of the URI of the Link. - */ - nsresult SetProtocol(const nsAString &aProtocol); - nsresult SetHost(const nsAString &aHost); - nsresult SetHostname(const nsAString &aHostname); - nsresult SetPathname(const nsAString &aPathname); - nsresult SetSearch(const nsAString &aSearch); - nsresult SetPort(const nsAString &aPort); - nsresult SetHash(const nsAString &aHash); - nsresult GetProtocol(nsAString &_protocol); - nsresult GetHost(nsAString &_host); - nsresult GetHostname(nsAString &_hostname); - nsresult GetPathname(nsAString &_pathname); - nsresult GetSearch(nsAString &_search); - nsresult GetPort(nsAString &_port); - nsresult GetHash(nsAString &_hash); - protected: /** * Invalidates any link caching, and resets the state to the default. @@ -96,9 +78,6 @@ protected: nsLinkState mLinkState; private: - already_AddRefed GetURIToMutate(); - void SetHrefAttribute(nsIURI *aURI); - mutable nsCOMPtr mCachedURI; }; diff --git a/content/html/content/src/nsGenericHTMLElement.cpp b/content/html/content/src/nsGenericHTMLElement.cpp index 72e7a92c995c..171fb3aa86c0 100644 --- a/content/html/content/src/nsGenericHTMLElement.cpp +++ b/content/html/content/src/nsGenericHTMLElement.cpp @@ -2991,6 +2991,335 @@ nsGenericHTMLElement::PerformAccesskey(PRBool aKeyCausesActivation, } } +void +nsGenericHTMLElement::SetHrefToURI(nsIURI* aURI) +{ + nsCAutoString newHref; + aURI->GetSpec(newHref); + SetAttrHelper(nsGkAtoms::href, NS_ConvertUTF8toUTF16(newHref)); +} + +nsresult +nsGenericHTMLElement::SetProtocolInHrefURI(const nsAString &aProtocol) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + if (!uri) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + nsAString::const_iterator start, end; + aProtocol.BeginReading(start); + aProtocol.EndReading(end); + nsAString::const_iterator iter(start); + FindCharInReadable(':', iter, end); + uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))); + + SetHrefToURI(uri); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetHostnameInHrefURI(const nsAString &aHostname) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + if (!uri) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + uri->SetHost(NS_ConvertUTF16toUTF8(aHostname)); + + SetHrefToURI(uri); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetPathnameInHrefURI(const nsAString &aPathname) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + nsCOMPtr url = do_QueryInterface(uri); + if (!url) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname)); + + SetHrefToURI(uri); + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetHostInHrefURI(const nsAString &aHost) +{ + // Can't simply call nsURI::SetHost, because that would treat the name as an + // IPv6 address (like http://[server:443]/) + // And can't call SetHostPort, because that's not implemented. Very sad. + + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + if (!uri) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + nsAString::const_iterator start, end; + aHost.BeginReading(start); + aHost.EndReading(end); + nsAString::const_iterator iter(start); + FindCharInReadable(':', iter, end); + uri->SetHost(NS_ConvertUTF16toUTF8(Substring(start, iter))); + if (iter != end) { + ++iter; + if (iter != end) { + nsAutoString portStr(Substring(iter, end)); + nsresult rv; + PRInt32 port; + port = portStr.ToInteger((PRInt32*)&rv); + if (NS_SUCCEEDED(rv)) { + uri->SetPort(port); + } + } + } + + SetHrefToURI(uri); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetSearchInHrefURI(const nsAString &aSearch) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + nsCOMPtr url = do_QueryInterface(uri); + if (!url) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + url->SetQuery(NS_ConvertUTF16toUTF8(aSearch)); + + SetHrefToURI(uri); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetHashInHrefURI(const nsAString &aHash) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + nsCOMPtr url = do_QueryInterface(uri); + if (!url) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + url->SetRef(NS_ConvertUTF16toUTF8(aHash)); + + SetHrefToURI(uri); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::SetPortInHrefURI(const nsAString &aPort) +{ + nsCOMPtr uri; + GetHrefURIToMutate(getter_AddRefs(uri)); + if (!uri) { + // Ignore failures to be compatible with NS4 + return NS_OK; + } + + nsresult rv; + PRInt32 port = nsString(aPort).ToInteger((PRInt32*)&rv); + if (NS_FAILED(rv)) + return NS_OK; + + uri->SetPort(port); + SetHrefToURI(uri); + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetProtocolFromHrefURI(nsAString& aProtocol) +{ + nsCOMPtr uri = GetHrefURIForAnchors(); + + if (!uri) { + aProtocol.AssignLiteral("http"); + } else { + nsCAutoString scheme; + uri->GetScheme(scheme); + CopyASCIItoUTF16(scheme, aProtocol); + } + aProtocol.Append(PRUnichar(':')); + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetHostFromHrefURI(nsAString& aHost) +{ + aHost.Truncate(); + + nsCOMPtr uri = GetHrefURIForAnchors(); + if (!uri) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + nsCAutoString hostport; + nsresult rv = uri->GetHostPort(hostport); + + // Failure to get the hostport from the URI isn't necessarily an + // error. Some URI's just don't have a hostport. + + if (NS_SUCCEEDED(rv)) { + CopyUTF8toUTF16(hostport, aHost); + } + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetHostnameFromHrefURI(nsAString& aHostname) +{ + aHostname.Truncate(); + nsCOMPtr uri = GetHrefURIForAnchors(); + if (!uri) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + nsCAutoString host; + nsresult rv = uri->GetHost(host); + + if (NS_SUCCEEDED(rv)) { + // Failure to get the host from the URI isn't necessarily an + // error. Some URI's just don't have a host. + + CopyUTF8toUTF16(host, aHostname); + } + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetPathnameFromHrefURI(nsAString& aPathname) +{ + aPathname.Truncate(); + + nsCOMPtr uri = GetHrefURIForAnchors(); + if (!uri) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + nsCOMPtr url(do_QueryInterface(uri)); + + if (!url) { + // If this is not a URL, we can't get the pathname from the URI + + return NS_OK; + } + + nsCAutoString file; + nsresult rv = url->GetFilePath(file); + if (NS_FAILED(rv)) + return rv; + + CopyUTF8toUTF16(file, aPathname); + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetSearchFromHrefURI(nsAString& aSearch) +{ + aSearch.Truncate(); + nsCOMPtr uri = GetHrefURIForAnchors(); + nsCOMPtr url(do_QueryInterface(uri)); + if (!url) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + nsCAutoString search; + nsresult rv = url->GetQuery(search); + if (NS_FAILED(rv)) + return NS_OK; + + if (!search.IsEmpty()) { + CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, aSearch); + } + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetPortFromHrefURI(nsAString& aPort) +{ + aPort.Truncate(); + nsCOMPtr uri = GetHrefURIForAnchors(); + if (!uri) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + PRInt32 port; + nsresult rv = uri->GetPort(&port); + + if (NS_SUCCEEDED(rv)) { + // Failure to get the port from the URI isn't necessarily an + // error. Some URI's just don't have a port. + + if (port == -1) { + return NS_OK; + } + + nsAutoString portStr; + portStr.AppendInt(port, 10); + aPort.Assign(portStr); + } + + return NS_OK; +} + +nsresult +nsGenericHTMLElement::GetHashFromHrefURI(nsAString& aHash) +{ + aHash.Truncate(); + nsCOMPtr uri = GetHrefURIForAnchors(); + nsCOMPtr url(do_QueryInterface(uri)); + if (!url) { + // Don't throw from these methods! Not a valid URI means return + // empty string. + return NS_OK; + } + + nsCAutoString ref; + nsresult rv = url->GetRef(ref); + if (NS_FAILED(rv)) + return NS_OK; + NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes! + + if (!ref.IsEmpty()) { + aHash.Assign(PRUnichar('#')); + AppendUTF8toUTF16(ref, aHash); + } + return NS_OK; +} + const nsAttrName* nsGenericHTMLElement::InternalGetExistingAttrNameFromQName(const nsAString& aStr) const { diff --git a/content/html/content/src/nsGenericHTMLElement.h b/content/html/content/src/nsGenericHTMLElement.h index 3a4d7ca28122..d89eb3fd7c94 100644 --- a/content/html/content/src/nsGenericHTMLElement.h +++ b/content/html/content/src/nsGenericHTMLElement.h @@ -496,6 +496,24 @@ public: */ static PRBool InNavQuirksMode(nsIDocument* aDoc); + // Helper functions for and + void SetHrefToURI(nsIURI* aURI); + nsresult SetProtocolInHrefURI(const nsAString &aProtocol); + nsresult SetHostInHrefURI(const nsAString &aHost); + nsresult SetHostnameInHrefURI(const nsAString &aHostname); + nsresult SetPathnameInHrefURI(const nsAString &aPathname); + nsresult SetSearchInHrefURI(const nsAString &aSearch); + nsresult SetPortInHrefURI(const nsAString &aPort); + nsresult SetHashInHrefURI(const nsAString &aHash); + + nsresult GetProtocolFromHrefURI(nsAString& aProtocol); + nsresult GetHostFromHrefURI(nsAString& aHost); + nsresult GetHostnameFromHrefURI(nsAString& aHostname); + nsresult GetPathnameFromHrefURI(nsAString& aPathname); + nsresult GetSearchFromHrefURI(nsAString& aSearch); + nsresult GetPortFromHrefURI(nsAString& aPort); + nsresult GetHashFromHrefURI(nsAString& aHash); + /** * Locate an nsIEditor rooted at this content node, if there is one. */ diff --git a/content/html/content/src/nsHTMLAnchorElement.cpp b/content/html/content/src/nsHTMLAnchorElement.cpp index 7f8d2c5e084b..a8fb2666ff18 100644 --- a/content/html/content/src/nsHTMLAnchorElement.cpp +++ b/content/html/content/src/nsHTMLAnchorElement.cpp @@ -338,12 +338,12 @@ nsHTMLAnchorElement::SetTarget(const nsAString& aValue) NS_IMETHODIMP \ nsHTMLAnchorElement::Get##_part(nsAString& a##_part) \ { \ - return Link::Get##_part(a##_part); \ + return Get##_part##FromHrefURI(a##_part); \ } \ NS_IMETHODIMP \ nsHTMLAnchorElement::Set##_part(const nsAString& a##_part) \ { \ - return Link::Set##_part(a##_part); \ + return Set##_part##InHrefURI(a##_part); \ } IMPL_URI_PART(Protocol) diff --git a/content/html/content/src/nsHTMLAreaElement.cpp b/content/html/content/src/nsHTMLAreaElement.cpp index 37a2ac15caed..3b0a13403f13 100644 --- a/content/html/content/src/nsHTMLAreaElement.cpp +++ b/content/html/content/src/nsHTMLAreaElement.cpp @@ -273,12 +273,12 @@ nsHTMLAreaElement::UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute, NS_IMETHODIMP \ nsHTMLAreaElement::Get##_part(nsAString& a##_part) \ { \ - return Link::Get##_part(a##_part); \ + return Get##_part##FromHrefURI(a##_part); \ } \ NS_IMETHODIMP \ nsHTMLAreaElement::Set##_part(const nsAString& a##_part) \ { \ - return Link::Set##_part(a##_part); \ + return Set##_part##InHrefURI(a##_part); \ } IMPL_URI_PART(Protocol)