2009-10-13 22:13:41 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et :
|
2012-05-21 11:12:37 +00: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 22:13:41 +00:00
|
|
|
|
|
|
|
#include "Link.h"
|
|
|
|
|
2014-04-03 04:18:36 +00:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-11-14 22:10:07 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2009-11-09 18:00:54 +00:00
|
|
|
#include "nsIURL.h"
|
2012-02-20 03:51:48 +00:00
|
|
|
#include "nsISizeOf.h"
|
2009-11-09 18:00:54 +00:00
|
|
|
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsString.h"
|
2009-12-16 00:04:07 +00:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2009-11-09 18:00:53 +00:00
|
|
|
|
2010-08-05 17:07:46 +00:00
|
|
|
#include "mozilla/Services.h"
|
2009-11-23 18:48:52 +00:00
|
|
|
|
2009-10-13 22:13:41 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2011-06-01 01:46:57 +00:00
|
|
|
Link::Link(Element *aElement)
|
2012-04-20 21:48:54 +00:00
|
|
|
: mElement(aElement)
|
2010-08-05 17:07:46 +00:00
|
|
|
, mHistory(services::GetHistoryService())
|
2012-12-07 14:35:14 +00:00
|
|
|
, mLinkState(eLinkState_NotLink)
|
|
|
|
, mNeedsRegistration(false)
|
2012-04-20 21:48:54 +00:00
|
|
|
, mRegistered(false)
|
2009-10-13 22:13:41 +00:00
|
|
|
{
|
2011-06-01 01:46:57 +00:00
|
|
|
NS_ABORT_IF_FALSE(mElement, "Must have an element");
|
2009-10-13 22:13:41 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 00:01:53 +00:00
|
|
|
Link::~Link()
|
|
|
|
{
|
|
|
|
UnregisterFromHistory();
|
|
|
|
}
|
|
|
|
|
2012-12-07 14:35:14 +00: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 22:13:41 +00:00
|
|
|
void
|
|
|
|
Link::SetLinkState(nsLinkState aState)
|
|
|
|
{
|
2009-11-23 18:48:52 +00:00
|
|
|
NS_ASSERTION(mRegistered,
|
|
|
|
"Setting the link state of an unregistered Link!");
|
2009-12-16 00:04:07 +00:00
|
|
|
NS_ASSERTION(mLinkState != aState,
|
|
|
|
"Setting state to the currently set state!");
|
|
|
|
|
|
|
|
// Set our current state as appropriate.
|
2009-10-13 22:13:41 +00:00
|
|
|
mLinkState = aState;
|
2009-11-23 18:48:52 +00:00
|
|
|
|
|
|
|
// Per IHistory interface documentation, we are no longer registered.
|
|
|
|
mRegistered = false;
|
2009-12-16 00:04:07 +00:00
|
|
|
|
2011-06-01 01:46:57 +00: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 22:13:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 04:18:36 +00:00
|
|
|
EventStates
|
2009-11-09 18:00:53 +00:00
|
|
|
Link::LinkState() const
|
|
|
|
{
|
2009-11-23 18:48:52 +00: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-06-01 01:46:57 +00:00
|
|
|
Element *element = self->mElement;
|
2009-11-23 18:48:52 +00:00
|
|
|
|
2012-12-07 14:35:14 +00:00
|
|
|
// If we have not yet registered for notifications and need to,
|
|
|
|
// due to our href changing, register now!
|
2014-10-02 18:45:44 +00:00
|
|
|
if (!mRegistered && mNeedsRegistration && element->IsInComposedDoc()) {
|
2012-12-07 14:35:14 +00:00
|
|
|
// Only try and register once.
|
|
|
|
self->mNeedsRegistration = false;
|
|
|
|
|
2009-11-23 18:48:52 +00:00
|
|
|
nsCOMPtr<nsIURI> hrefURI(GetURI());
|
|
|
|
|
2012-11-30 05:41:40 +00:00
|
|
|
// Assume that we are not visited until we are told otherwise.
|
|
|
|
self->mLinkState = eLinkState_Unvisited;
|
|
|
|
|
2012-12-07 14:35:14 +00: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 09:55:54 +00:00
|
|
|
nsresult rv = mHistory->RegisterVisitedCallback(hrefURI, self);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
self->mRegistered = true;
|
2009-11-23 18:48:52 +00:00
|
|
|
|
2012-11-09 09:55:54 +00:00
|
|
|
// And make sure we are in the document's link map.
|
2014-10-02 18:45:44 +00:00
|
|
|
element->GetComposedDoc()->AddStyleRelevantLink(self);
|
2010-02-24 16:37:03 +00:00
|
|
|
}
|
2009-11-23 18:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return our known state.
|
2009-11-09 18:00:53 +00:00
|
|
|
if (mLinkState == eLinkState_Visited) {
|
|
|
|
return NS_EVENT_STATE_VISITED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
return NS_EVENT_STATE_UNVISITED;
|
|
|
|
}
|
|
|
|
|
2014-04-03 04:18:36 +00:00
|
|
|
return EventStates();
|
2009-11-09 18:00:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 02:42:39 +00:00
|
|
|
nsIURI*
|
2009-11-09 18:00:54 +00:00
|
|
|
Link::GetURI() const
|
|
|
|
{
|
|
|
|
// If we have this URI cached, use it.
|
2013-08-24 02:42:39 +00:00
|
|
|
if (mCachedURI) {
|
|
|
|
return mCachedURI;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise obtain it.
|
|
|
|
Link *self = const_cast<Link *>(this);
|
2011-06-01 01:46:57 +00:00
|
|
|
Element *element = self->mElement;
|
2013-08-24 02:42:39 +00:00
|
|
|
mCachedURI = element->GetHrefURI();
|
2009-11-09 18:00:54 +00:00
|
|
|
|
2013-08-24 02:42:39 +00:00
|
|
|
return mCachedURI;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetProtocol(const nsAString &aProtocol, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00: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 17:05:10 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetPassword(const nsAString &aPassword, ErrorResult& aError)
|
2013-09-04 17:05:10 +00: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 23:30:27 +00:00
|
|
|
Link::SetUsername(const nsAString &aUsername, ErrorResult& aError)
|
2013-09-04 17:05:10 +00: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 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetHost(const nsAString &aHost, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 17:07:21 +00:00
|
|
|
(void)uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
2009-11-09 18:00:54 +00:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetHostname(const nsAString &aHostname, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetPathname(const nsAString &aPathname, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetSearch(const nsAString& aSearch, ErrorResult& aError)
|
2014-10-23 15:32:35 +00:00
|
|
|
{
|
|
|
|
SetSearchInternal(aSearch);
|
|
|
|
UpdateURLSearchParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::SetSearchInternal(const nsAString& aSearch)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetPort(const nsAString &aPort, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString portStr(aPort);
|
2013-12-05 21:05:51 +00: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 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)uri->SetPort(port);
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::SetHash(const nsAString &aHash, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURIToMutate());
|
2011-05-22 01:12:46 +00:00
|
|
|
if (!uri) {
|
2009-11-09 18:00:54 +00:00
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
(void)uri->SetRef(NS_ConvertUTF16toUTF8(aHash));
|
2009-11-09 18:00:54 +00:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:05:10 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetOrigin(nsAString &aOrigin, ErrorResult& aError)
|
2013-09-04 17:05:10 +00:00
|
|
|
{
|
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString origin;
|
2014-08-31 10:40:11 +00:00
|
|
|
nsContentUtils::GetUTFOrigin(uri, origin);
|
2013-09-04 17:07:21 +00:00
|
|
|
aOrigin.Assign(origin);
|
2013-09-04 17:05:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetProtocol(nsAString &_protocol, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
_protocol.AssignLiteral("http");
|
|
|
|
}
|
|
|
|
else {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString scheme;
|
2009-11-09 18:00:54 +00:00
|
|
|
(void)uri->GetScheme(scheme);
|
|
|
|
CopyASCIItoUTF16(scheme, _protocol);
|
|
|
|
}
|
2014-01-04 15:02:17 +00:00
|
|
|
_protocol.Append(char16_t(':'));
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 17:05:10 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetUsername(nsAString& aUsername, ErrorResult& aError)
|
2013-09-04 17:05:10 +00:00
|
|
|
{
|
|
|
|
aUsername.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString username;
|
|
|
|
uri->GetUsername(username);
|
|
|
|
CopyASCIItoUTF16(username, aUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetPassword(nsAString &aPassword, ErrorResult& aError)
|
2013-09-04 17:05:10 +00:00
|
|
|
{
|
|
|
|
aPassword.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString password;
|
|
|
|
uri->GetPassword(password);
|
|
|
|
CopyASCIItoUTF16(password, aPassword);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetHost(nsAString &_host, ErrorResult& aError)
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString hostport;
|
2009-11-09 18:00:54 +00:00
|
|
|
nsresult rv = uri->GetHostPort(hostport);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(hostport, _host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetHostname(nsAString &_hostname, ErrorResult& aError)
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:46:20 +00:00
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(uri, _hostname);
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetPathname(nsAString &_pathname, ErrorResult& aError)
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString file;
|
2009-11-09 18:00:54 +00:00
|
|
|
nsresult rv = url->GetFilePath(file);
|
2013-01-04 17:02:13 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(file, _pathname);
|
|
|
|
}
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetSearch(nsAString &_search, ErrorResult& aError)
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString search;
|
2009-11-09 18:00:54 +00:00
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_SUCCEEDED(rv) && !search.IsEmpty()) {
|
|
|
|
CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, _search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetPort(nsAString &_port, ErrorResult& aError)
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2009-11-09 18:00:54 +00: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 17:02:13 +00:00
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
Link::GetHash(nsAString &_hash, ErrorResult& aError)
|
2009-11-09 18:00:54 +00:00
|
|
|
{
|
|
|
|
_hash.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2011-05-22 01:12:46 +00:00
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty
|
2009-11-09 18:00:54 +00:00
|
|
|
// string.
|
2013-01-04 17:02:13 +00:00
|
|
|
return;
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString ref;
|
2011-05-22 01:12:46 +00:00
|
|
|
nsresult rv = uri->GetRef(ref);
|
2009-11-09 18:00:54 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
|
2014-01-04 15:02:17 +00:00
|
|
|
_hash.Assign(char16_t('#'));
|
2014-11-12 01:14:04 +00:00
|
|
|
if (!nsContentUtils::ShouldEncodeURLHash()) {
|
|
|
|
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
|
|
|
|
}
|
2009-11-09 18:00:54 +00:00
|
|
|
AppendUTF8toUTF16(ref, _hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 22:13:41 +00:00
|
|
|
void
|
2012-12-07 14:35:14 +00:00
|
|
|
Link::ResetLinkState(bool aNotify, bool aHasHref)
|
2009-10-13 22:13:41 +00:00
|
|
|
{
|
2012-12-07 14:35:14 +00: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 16:37:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 14:35:14 +00: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) {
|
2014-10-02 18:45:44 +00:00
|
|
|
nsIDocument *doc = mElement->GetComposedDoc();
|
2012-12-07 14:35:14 +00:00
|
|
|
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 22:13:41 +00:00
|
|
|
|
2012-12-07 14:35:14 +00:00
|
|
|
UnregisterFromHistory();
|
2009-10-13 22:13:41 +00:00
|
|
|
}
|
2009-11-09 18:00:54 +00:00
|
|
|
|
2012-12-07 14:35:14 +00: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-10-23 15:32:35 +00:00
|
|
|
UpdateURLSearchParams();
|
2009-11-23 18:48:52 +00:00
|
|
|
|
2009-11-09 18:00:54 +00:00
|
|
|
// Update our state back to the default.
|
2009-10-13 22:13:41 +00:00
|
|
|
mLinkState = defaultState;
|
2009-11-09 18:00:54 +00:00
|
|
|
|
2011-06-01 01:46:57 +00: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 14:35:14 +00: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-06-01 01:46:57 +00:00
|
|
|
if (aNotify) {
|
|
|
|
mElement->UpdateState(aNotify);
|
|
|
|
} else {
|
2012-12-07 14:35:14 +00:00
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
mElement->UpdateLinkState(NS_EVENT_STATE_UNVISITED);
|
|
|
|
} else {
|
2014-04-03 04:18:36 +00:00
|
|
|
mElement->UpdateLinkState(EventStates());
|
2012-12-07 14:35:14 +00:00
|
|
|
}
|
2010-02-24 16:37:03 +00:00
|
|
|
}
|
2009-10-13 22:13:41 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 18:48:52 +00:00
|
|
|
void
|
|
|
|
Link::UnregisterFromHistory()
|
|
|
|
{
|
|
|
|
// If we are not registered, we have nothing to do.
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-16 00:01:53 +00:00
|
|
|
NS_ASSERTION(mCachedURI, "mRegistered is true, but we have no cached URI?!");
|
2009-11-23 18:48:52 +00:00
|
|
|
|
|
|
|
// And tell History to stop tracking us.
|
2012-11-09 09:55:54 +00: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 18:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-09 18:00:54 +00:00
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
Link::GetURIToMutate()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-11-09 18:00:54 +00: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 16:20:19 +00: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-02 02:35:17 +00:00
|
|
|
nsAutoCString href;
|
2009-11-09 18:00:54 +00:00
|
|
|
(void)aURI->GetSpec(href);
|
2011-06-01 01:46:57 +00:00
|
|
|
(void)mElement->SetAttr(kNameSpaceID_None, nsGkAtoms::href,
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ConvertUTF8toUTF16(href), true);
|
2009-11-09 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:51:48 +00:00
|
|
|
size_t
|
2013-06-23 12:03:39 +00:00
|
|
|
Link::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-20 03:51:48 +00: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;
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:32:35 +00:00
|
|
|
URLSearchParams*
|
|
|
|
Link::SearchParams()
|
|
|
|
{
|
|
|
|
CreateSearchParamsIfNeeded();
|
|
|
|
return mSearchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::SetSearchParams(URLSearchParams& aSearchParams)
|
|
|
|
{
|
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchParams = &aSearchParams;
|
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
|
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSearchParams);
|
|
|
|
MOZ_ASSERT(mSearchParams == aSearchParams);
|
|
|
|
|
|
|
|
nsString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::UpdateURLSearchParams()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchParams->ParseInput(search, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::CreateSearchParamsIfNeeded()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
mSearchParams = new URLSearchParams();
|
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
UpdateURLSearchParams();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::Unlink()
|
|
|
|
{
|
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
|
|
|
mSearchParams = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Link::Traverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
Link* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSearchParams);
|
|
|
|
}
|
|
|
|
|
2009-10-13 22:13:41 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|