Merge for backout of changeset 6a2bef8d7020

This commit is contained in:
Shawn Wilsher 2010-02-18 10:08:15 -08:00
Родитель e8b6d3eb24 36ef6cbc56
Коммит c7f34322cb
6 изменённых файлов: 351 добавлений и 337 удалений

Просмотреть файл

@ -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<nsIURI> 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<nsIURI> 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<nsIURI> 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<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURL> 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<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> 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<nsIURI> 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<nsIURI> uri(GetURI());
nsCOMPtr<nsIURL> 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<nsIURI> uri(GetURI());
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> uri(GetURI());
nsCOMPtr<nsIURL> 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<nsIURI>
Link::GetURIToMutate()
{
nsCOMPtr<nsIURI> uri(GetURI());
if (!uri) {
return nsnull;
}
nsCOMPtr<nsIURI> clone;
(void)uri->Clone(getter_AddRefs(clone));
return clone.forget();
}
void
Link::SetHrefAttribute(nsIURI *aURI)
{
NS_ASSERTION(aURI, "Null URI is illegal!");
nsCOMPtr<nsIContent> 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

Просмотреть файл

@ -69,24 +69,6 @@ public:
*/
already_AddRefed<nsIURI> 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<nsIURI> GetURIToMutate();
void SetHrefAttribute(nsIURI *aURI);
mutable nsCOMPtr<nsIURI> mCachedURI;
};

Просмотреть файл

@ -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<nsIURI> 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<nsIURI> 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<nsIURI> uri;
GetHrefURIToMutate(getter_AddRefs(uri));
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> uri;
GetHrefURIToMutate(getter_AddRefs(uri));
nsCOMPtr<nsIURL> 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<nsIURI> uri;
GetHrefURIToMutate(getter_AddRefs(uri));
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> 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<nsIURI> 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<nsIURI> 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<nsIURI> uri = GetHrefURIForAnchors();
if (!uri) {
// Don't throw from these methods! Not a valid URI means return
// empty string.
return NS_OK;
}
nsCOMPtr<nsIURL> 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<nsIURI> uri = GetHrefURIForAnchors();
nsCOMPtr<nsIURL> 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<nsIURI> 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<nsIURI> uri = GetHrefURIForAnchors();
nsCOMPtr<nsIURL> 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
{

Просмотреть файл

@ -496,6 +496,24 @@ public:
*/
static PRBool InNavQuirksMode(nsIDocument* aDoc);
// Helper functions for <a> and <area>
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.
*/

Просмотреть файл

@ -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)

Просмотреть файл

@ -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)