2009-10-13 15:13:41 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et :
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-10-13 15:13:41 -07:00
|
|
|
|
|
|
|
#include "Link.h"
|
|
|
|
|
2014-04-02 21:18:36 -07:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-11-14 14:10:07 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2009-11-09 10:00:54 -08:00
|
|
|
#include "nsIURL.h"
|
2012-02-19 19:51:48 -08:00
|
|
|
#include "nsISizeOf.h"
|
2009-11-09 10:00:54 -08:00
|
|
|
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsString.h"
|
2009-12-15 16:04:07 -08:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2009-11-09 10:00:53 -08:00
|
|
|
|
2010-08-05 10:07:46 -07:00
|
|
|
#include "mozilla/Services.h"
|
2009-11-23 10:48:52 -08:00
|
|
|
|
2009-10-13 15:13:41 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2011-05-31 18:46:57 -07:00
|
|
|
Link::Link(Element *aElement)
|
2012-04-20 14:48:54 -07:00
|
|
|
: mElement(aElement)
|
2010-08-05 10:07:46 -07:00
|
|
|
, mHistory(services::GetHistoryService())
|
2012-12-07 06:35:14 -08:00
|
|
|
, mLinkState(eLinkState_NotLink)
|
|
|
|
, mNeedsRegistration(false)
|
2012-04-20 14:48:54 -07:00
|
|
|
, mRegistered(false)
|
2009-10-13 15:13:41 -07:00
|
|
|
{
|
2011-05-31 18:46:57 -07:00
|
|
|
NS_ABORT_IF_FALSE(mElement, "Must have an element");
|
2009-10-13 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:01:53 -08:00
|
|
|
Link::~Link()
|
|
|
|
{
|
|
|
|
UnregisterFromHistory();
|
|
|
|
}
|
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
bool
|
|
|
|
Link::ElementHasHref() const
|
|
|
|
{
|
|
|
|
return ((!mElement->IsSVG() && mElement->HasAttr(kNameSpaceID_None, nsGkAtoms::href))
|
|
|
|
|| (!mElement->IsHTML() && mElement->HasAttr(kNameSpaceID_XLink, nsGkAtoms::href)));
|
|
|
|
}
|
|
|
|
|
2009-10-13 15:13:41 -07:00
|
|
|
void
|
|
|
|
Link::SetLinkState(nsLinkState aState)
|
|
|
|
{
|
2009-11-23 10:48:52 -08:00
|
|
|
NS_ASSERTION(mRegistered,
|
|
|
|
"Setting the link state of an unregistered Link!");
|
2009-12-15 16:04:07 -08:00
|
|
|
NS_ASSERTION(mLinkState != aState,
|
|
|
|
"Setting state to the currently set state!");
|
|
|
|
|
|
|
|
// Set our current state as appropriate.
|
2009-10-13 15:13:41 -07:00
|
|
|
mLinkState = aState;
|
2009-11-23 10:48:52 -08:00
|
|
|
|
|
|
|
// Per IHistory interface documentation, we are no longer registered.
|
|
|
|
mRegistered = false;
|
2009-12-15 16:04:07 -08:00
|
|
|
|
2011-05-31 18:46:57 -07:00
|
|
|
NS_ABORT_IF_FALSE(LinkState() == NS_EVENT_STATE_VISITED ||
|
|
|
|
LinkState() == NS_EVENT_STATE_UNVISITED,
|
|
|
|
"Unexpected state obtained from LinkState()!");
|
|
|
|
|
|
|
|
// Tell the element to update its visited state
|
|
|
|
mElement->UpdateState(true);
|
2009-10-13 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
2014-04-02 21:18:36 -07:00
|
|
|
EventStates
|
2009-11-09 10:00:53 -08:00
|
|
|
Link::LinkState() const
|
|
|
|
{
|
2009-11-23 10:48:52 -08:00
|
|
|
// We are a constant method, but we are just lazily doing things and have to
|
|
|
|
// track that state. Cast away that constness!
|
|
|
|
Link *self = const_cast<Link *>(this);
|
|
|
|
|
2011-05-31 18:46:57 -07:00
|
|
|
Element *element = self->mElement;
|
2009-11-23 10:48:52 -08:00
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
// If we have not yet registered for notifications and need to,
|
|
|
|
// due to our href changing, register now!
|
|
|
|
if (!mRegistered && mNeedsRegistration && element->IsInDoc()) {
|
|
|
|
// Only try and register once.
|
|
|
|
self->mNeedsRegistration = false;
|
|
|
|
|
2009-11-23 10:48:52 -08:00
|
|
|
nsCOMPtr<nsIURI> hrefURI(GetURI());
|
|
|
|
|
2012-11-29 21:41:40 -08:00
|
|
|
// Assume that we are not visited until we are told otherwise.
|
|
|
|
self->mLinkState = eLinkState_Unvisited;
|
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
// Make sure the href attribute has a valid link (bug 23209).
|
|
|
|
// If we have a good href, register with History if available.
|
|
|
|
if (mHistory && hrefURI) {
|
2012-11-09 01:55:54 -08:00
|
|
|
nsresult rv = mHistory->RegisterVisitedCallback(hrefURI, self);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
self->mRegistered = true;
|
2009-11-23 10:48:52 -08:00
|
|
|
|
2012-11-09 01:55:54 -08:00
|
|
|
// And make sure we are in the document's link map.
|
2012-12-07 13:01:59 -08:00
|
|
|
element->GetCurrentDoc()->AddStyleRelevantLink(self);
|
2010-02-24 08:37:03 -08:00
|
|
|
}
|
2009-11-23 10:48:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return our known state.
|
2009-11-09 10:00:53 -08:00
|
|
|
if (mLinkState == eLinkState_Visited) {
|
|
|
|
return NS_EVENT_STATE_VISITED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
return NS_EVENT_STATE_UNVISITED;
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:18:36 -07:00
|
|
|
return EventStates();
|
2009-11-09 10:00:53 -08:00
|
|
|
}
|
|
|
|
|
2013-08-23 19:42:39 -07:00
|
|
|
nsIURI*
|
2009-11-09 10:00:54 -08:00
|
|
|
Link::GetURI() const
|
|
|
|
{
|
|
|
|
// If we have this URI cached, use it.
|
2013-08-23 19:42:39 -07:00
|
|
|
if (mCachedURI) {
|
|
|
|
return mCachedURI;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise obtain it.
|
|
|
|
Link *self = const_cast<Link *>(this);
|
2011-05-31 18:46:57 -07:00
|
|
|
Element *element = self->mElement;
|
2013-08-23 19:42:39 -07:00
|
|
|
mCachedURI = element->GetHrefURI();
|
2009-11-09 10:00:54 -08:00
|
|
|
|
2013-08-23 19:42:39 -07:00
|
|
|
return mCachedURI;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetProtocol(const nsAString &aProtocol, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:05:10 -07:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetPassword(const nsAString &aPassword, ErrorResult& aError)
|
2013-09-04 10:05:10 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetUsername(const nsAString &aUsername, ErrorResult& aError)
|
2013-09-04 10:05:10 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetHost(const nsAString &aHost, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2013-09-04 10:07:21 -07:00
|
|
|
(void)uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
2009-11-09 10:00:54 -08:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetHostname(const nsAString &aHostname, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetPathname(const nsAString &aPathname, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetSearch(const nsAString& aSearch, ErrorResult& aError)
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
|
|
|
SetSearchInternal(aSearch);
|
2014-02-03 08:48:38 -08:00
|
|
|
UpdateURLSearchParams();
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::SetSearchInternal(const nsAString& aSearch)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetPort(const nsAString &aPort, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString portStr(aPort);
|
2013-12-05 13:05:51 -08:00
|
|
|
|
|
|
|
// nsIURI uses -1 as default value.
|
|
|
|
int32_t port = -1;
|
|
|
|
if (!aPort.IsEmpty()) {
|
|
|
|
port = portStr.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)uri->SetPort(port);
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::SetHash(const nsAString &aHash, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
2011-05-21 18:12:46 -07:00
|
|
|
if (!uri) {
|
2009-11-09 10:00:54 -08:00
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
(void)uri->SetRef(NS_ConvertUTF16toUTF8(aHash));
|
2009-11-09 10:00:54 -08:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:05:10 -07:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetOrigin(nsAString &aOrigin, ErrorResult& aError)
|
2013-09-04 10:05:10 -07:00
|
|
|
{
|
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString origin;
|
2013-09-04 10:07:21 -07:00
|
|
|
nsContentUtils::GetUTFNonNullOrigin(uri, origin);
|
|
|
|
aOrigin.Assign(origin);
|
2013-09-04 10:05:10 -07:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetProtocol(nsAString &_protocol, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
_protocol.AssignLiteral("http");
|
|
|
|
}
|
|
|
|
else {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString scheme;
|
2009-11-09 10:00:54 -08:00
|
|
|
(void)uri->GetScheme(scheme);
|
|
|
|
CopyASCIItoUTF16(scheme, _protocol);
|
|
|
|
}
|
2014-01-04 07:02:17 -08:00
|
|
|
_protocol.Append(char16_t(':'));
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2013-09-04 10:05:10 -07:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetUsername(nsAString& aUsername, ErrorResult& aError)
|
2013-09-04 10:05:10 -07:00
|
|
|
{
|
|
|
|
aUsername.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString username;
|
|
|
|
uri->GetUsername(username);
|
|
|
|
CopyASCIItoUTF16(username, aUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetPassword(nsAString &aPassword, ErrorResult& aError)
|
2013-09-04 10:05:10 -07:00
|
|
|
{
|
|
|
|
aPassword.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString password;
|
|
|
|
uri->GetPassword(password);
|
|
|
|
CopyASCIItoUTF16(password, aPassword);
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetHost(nsAString &_host, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_host.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostport;
|
2009-11-09 10:00:54 -08:00
|
|
|
nsresult rv = uri->GetHostPort(hostport);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(hostport, _host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetHostname(nsAString &_hostname, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_hostname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2009-11-09 10:00:54 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetPathname(nsAString &_pathname, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_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.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString file;
|
2009-11-09 10:00:54 -08:00
|
|
|
nsresult rv = url->GetFilePath(file);
|
2013-01-04 09:02:13 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(file, _pathname);
|
|
|
|
}
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetSearch(nsAString &_search, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_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.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString search;
|
2009-11-09 10:00:54 -08:00
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_SUCCEEDED(rv) && !search.IsEmpty()) {
|
|
|
|
CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, _search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetPort(nsAString &_port, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_port.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port;
|
2009-11-09 10:00:54 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:02:13 -08:00
|
|
|
void
|
2014-07-11 16:30:27 -07:00
|
|
|
Link::GetHash(nsAString &_hash, ErrorResult& aError)
|
2009-11-09 10:00:54 -08:00
|
|
|
{
|
|
|
|
_hash.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2011-05-21 18:12:46 -07:00
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty
|
2009-11-09 10:00:54 -08:00
|
|
|
// string.
|
2013-01-04 09:02:13 -08:00
|
|
|
return;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString ref;
|
2011-05-21 18:12:46 -07:00
|
|
|
nsresult rv = uri->GetRef(ref);
|
2009-11-09 10:00:54 -08:00
|
|
|
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
|
|
|
|
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
|
2014-01-04 07:02:17 -08:00
|
|
|
_hash.Assign(char16_t('#'));
|
2009-11-09 10:00:54 -08:00
|
|
|
AppendUTF8toUTF16(ref, _hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 15:13:41 -07:00
|
|
|
void
|
2012-12-07 06:35:14 -08:00
|
|
|
Link::ResetLinkState(bool aNotify, bool aHasHref)
|
2009-10-13 15:13:41 -07:00
|
|
|
{
|
2012-12-07 06:35:14 -08:00
|
|
|
nsLinkState defaultState;
|
|
|
|
|
|
|
|
// The default state for links with an href is unvisited.
|
|
|
|
if (aHasHref) {
|
|
|
|
defaultState = eLinkState_Unvisited;
|
|
|
|
} else {
|
|
|
|
defaultState = eLinkState_NotLink;
|
2010-02-24 08:37:03 -08:00
|
|
|
}
|
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
// If !mNeedsRegstration, then either we've never registered, or we're
|
|
|
|
// currently registered; in either case, we should remove ourself
|
|
|
|
// from the doc and the history.
|
|
|
|
if (!mNeedsRegistration && mLinkState != eLinkState_NotLink) {
|
|
|
|
nsIDocument *doc = mElement->GetCurrentDoc();
|
|
|
|
if (doc && (mRegistered || mLinkState == eLinkState_Visited)) {
|
|
|
|
// Tell the document to forget about this link if we've registered
|
|
|
|
// with it before.
|
|
|
|
doc->ForgetLink(this);
|
|
|
|
}
|
2009-10-13 15:13:41 -07:00
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
UnregisterFromHistory();
|
2009-10-13 15:13:41 -07:00
|
|
|
}
|
2009-11-09 10:00:54 -08:00
|
|
|
|
2012-12-07 06:35:14 -08:00
|
|
|
// If we have an href, we should register with the history.
|
|
|
|
mNeedsRegistration = aHasHref;
|
|
|
|
|
|
|
|
// If we've cached the URI, reset always invalidates it.
|
|
|
|
mCachedURI = nullptr;
|
2014-02-03 08:48:38 -08:00
|
|
|
UpdateURLSearchParams();
|
2009-11-23 10:48:52 -08:00
|
|
|
|
2009-11-09 10:00:54 -08:00
|
|
|
// Update our state back to the default.
|
2009-10-13 15:13:41 -07:00
|
|
|
mLinkState = defaultState;
|
2009-11-09 10:00:54 -08:00
|
|
|
|
2011-05-31 18:46:57 -07:00
|
|
|
// We have to be very careful here: if aNotify is false we do NOT
|
|
|
|
// want to call UpdateState, because that will call into LinkState()
|
|
|
|
// and try to start off loads, etc. But ResetLinkState is called
|
|
|
|
// with aNotify false when things are in inconsistent states, so
|
|
|
|
// we'll get confused in that situation. Instead, just silently
|
2012-12-07 06:35:14 -08:00
|
|
|
// update the link state on mElement. Since we might have set the
|
|
|
|
// link state to unvisited, make sure to update with that state if
|
|
|
|
// required.
|
2011-05-31 18:46:57 -07:00
|
|
|
if (aNotify) {
|
|
|
|
mElement->UpdateState(aNotify);
|
|
|
|
} else {
|
2012-12-07 06:35:14 -08:00
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
mElement->UpdateLinkState(NS_EVENT_STATE_UNVISITED);
|
|
|
|
} else {
|
2014-04-02 21:18:36 -07:00
|
|
|
mElement->UpdateLinkState(EventStates());
|
2012-12-07 06:35:14 -08:00
|
|
|
}
|
2010-02-24 08:37:03 -08:00
|
|
|
}
|
2009-10-13 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
2009-11-23 10:48:52 -08:00
|
|
|
void
|
|
|
|
Link::UnregisterFromHistory()
|
|
|
|
{
|
|
|
|
// If we are not registered, we have nothing to do.
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:01:53 -08:00
|
|
|
NS_ASSERTION(mCachedURI, "mRegistered is true, but we have no cached URI?!");
|
2009-11-23 10:48:52 -08:00
|
|
|
|
|
|
|
// And tell History to stop tracking us.
|
2012-11-09 01:55:54 -08:00
|
|
|
if (mHistory) {
|
|
|
|
nsresult rv = mHistory->UnregisterVisitedCallback(mCachedURI, this);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "This should only fail if we misuse the API!");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mRegistered = false;
|
|
|
|
}
|
2009-11-23 10:48:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-09 10:00:54 -08:00
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
Link::GetURIToMutate()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> clone;
|
|
|
|
(void)uri->Clone(getter_AddRefs(clone));
|
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::SetHrefAttribute(nsIURI *aURI)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Null URI is illegal!");
|
|
|
|
|
2014-06-09 09:20:19 -07:00
|
|
|
// if we change this code to not reserialize we need to do something smarter
|
|
|
|
// in SetProtocol because changing the protocol of an URI can change the
|
|
|
|
// "nature" of the nsIURL/nsIURI implementation.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString href;
|
2009-11-09 10:00:54 -08:00
|
|
|
(void)aURI->GetSpec(href);
|
2011-05-31 18:46:57 -07:00
|
|
|
(void)mElement->SetAttr(kNameSpaceID_None, nsGkAtoms::href,
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ConvertUTF8toUTF16(href), true);
|
2009-11-09 10:00:54 -08:00
|
|
|
}
|
|
|
|
|
2012-02-19 19:51:48 -08:00
|
|
|
size_t
|
2013-06-23 05:03:39 -07:00
|
|
|
Link::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-19 19:51:48 -08:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
if (mCachedURI) {
|
|
|
|
nsCOMPtr<nsISizeOf> iface = do_QueryInterface(mCachedURI);
|
|
|
|
if (iface) {
|
|
|
|
n += iface->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following members don't need to be measured:
|
|
|
|
// - mElement, because it is a pointer-to-self used to avoid QIs
|
|
|
|
// - mHistory, because it is non-owning
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
URLSearchParams*
|
2014-03-19 08:36:46 -07:00
|
|
|
Link::SearchParams()
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
|
|
|
CreateSearchParamsIfNeeded();
|
|
|
|
return mSearchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-19 08:36:46 -07:00
|
|
|
Link::SetSearchParams(URLSearchParams& aSearchParams)
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
2014-02-21 13:54:42 -08:00
|
|
|
}
|
2014-02-03 08:48:38 -08:00
|
|
|
|
2014-03-19 08:36:46 -07:00
|
|
|
mSearchParams = &aSearchParams;
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-07 17:45:21 -07:00
|
|
|
Link::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
MOZ_ASSERT(mSearchParams);
|
2014-08-07 17:45:21 -07:00
|
|
|
MOZ_ASSERT(mSearchParams == aSearchParams);
|
2013-12-12 11:30:27 -08:00
|
|
|
|
|
|
|
nsString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 08:48:38 -08:00
|
|
|
Link::UpdateURLSearchParams()
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
if (!mSearchParams) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-12 11:30:27 -08:00
|
|
|
|
|
|
|
nsAutoCString search;
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url) {
|
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the query from a nsIURL.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->ParseInput(search, this);
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::CreateSearchParamsIfNeeded()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
mSearchParams = new URLSearchParams();
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
UpdateURLSearchParams();
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::Unlink()
|
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
|
|
|
mSearchParams = nullptr;
|
|
|
|
}
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::Traverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
Link* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSearchParams);
|
|
|
|
}
|
|
|
|
|
2009-10-13 15:13:41 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|