2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2008-06-25 05:55:43 +04:00
|
|
|
|
2017-09-08 12:02:05 +03:00
|
|
|
#include "IDTracker.h"
|
2017-09-08 12:02:05 +03:00
|
|
|
|
|
|
|
#include "mozilla/Encoding.h"
|
2008-06-25 05:55:43 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsBindingManager.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
2017-09-08 12:02:05 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
static DocumentOrShadowRoot*
|
|
|
|
DocOrShadowFromContent(nsIContent& aContent)
|
|
|
|
{
|
|
|
|
ShadowRoot* shadow = aContent.GetContainingShadow();
|
|
|
|
|
|
|
|
// We never look in <svg:use> shadow trees, for backwards compat.
|
|
|
|
while (shadow && shadow->Host()->IsSVGElement(nsGkAtoms::use)) {
|
|
|
|
shadow = shadow->Host()->GetContainingShadow();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shadow) {
|
|
|
|
return shadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aContent.OwnerDoc();
|
|
|
|
}
|
|
|
|
|
2008-06-25 05:55:43 +04:00
|
|
|
void
|
2018-08-17 14:35:15 +03:00
|
|
|
IDTracker::Reset(nsIContent* aFromContent,
|
|
|
|
nsIURI* aURI,
|
|
|
|
bool aWatch,
|
|
|
|
bool aReferenceImage)
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aFromContent, "Reset() expects non-null content pointer");
|
2008-06-25 05:55:43 +04:00
|
|
|
|
2011-05-22 05:12:46 +04:00
|
|
|
Unlink();
|
2008-06-25 05:55:43 +04:00
|
|
|
|
2011-05-23 03:13:57 +04:00
|
|
|
if (!aURI)
|
|
|
|
return;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString refPart;
|
2011-05-22 05:12:46 +04:00
|
|
|
aURI->GetRef(refPart);
|
2008-06-25 05:55:43 +04:00
|
|
|
// Unescape %-escapes in the reference. The result will be in the
|
2017-08-02 14:43:30 +03:00
|
|
|
// document charset, hopefully...
|
2008-06-25 05:55:43 +04:00
|
|
|
NS_UnescapeURL(refPart);
|
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
// Get the thing to observe changes to.
|
|
|
|
nsIDocument* doc = aFromContent->OwnerDoc();
|
|
|
|
DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(*aFromContent);
|
2018-08-17 18:13:14 +03:00
|
|
|
auto encoding = doc->GetDocumentCharacterSet();
|
2018-08-17 14:35:15 +03:00
|
|
|
|
2008-06-25 05:55:43 +04:00
|
|
|
nsAutoString ref;
|
2017-06-16 15:11:03 +03:00
|
|
|
nsresult rv = encoding->DecodeWithoutBOMHandling(refPart, ref);
|
2016-05-06 16:21:12 +03:00
|
|
|
if (NS_FAILED(rv) || ref.IsEmpty()) {
|
2008-06-25 05:55:43 +04:00
|
|
|
return;
|
2016-05-06 16:21:12 +03:00
|
|
|
}
|
2017-06-16 15:11:03 +03:00
|
|
|
rv = NS_OK;
|
2008-06-25 05:55:43 +04:00
|
|
|
|
|
|
|
nsIContent* bindingParent = aFromContent->GetBindingParent();
|
2018-08-17 14:35:15 +03:00
|
|
|
if (bindingParent && !aFromContent->IsInShadowTree()) {
|
2013-07-17 20:05:03 +04:00
|
|
|
nsXBLBinding* binding = bindingParent->GetXBLBinding();
|
2016-05-09 20:33:19 +03:00
|
|
|
if (!binding) {
|
|
|
|
// This happens, for example, if aFromContent is part of the content
|
|
|
|
// inserted by a call to nsIDocument::InsertAnonymousContent, which we
|
2016-05-16 21:39:16 +03:00
|
|
|
// also want to handle. (It also happens for <use>'s anonymous
|
|
|
|
// content etc.)
|
2016-05-09 20:33:19 +03:00
|
|
|
Element* anonRoot =
|
|
|
|
doc->GetAnonRootIfInAnonymousContentContainer(aFromContent);
|
|
|
|
if (anonRoot) {
|
|
|
|
mElement = nsContentUtils::MatchElementId(anonRoot, ref);
|
2016-05-16 21:39:16 +03:00
|
|
|
// We don't have watching working yet for anonymous content, so bail out here.
|
|
|
|
return;
|
2016-05-09 20:33:19 +03:00
|
|
|
}
|
|
|
|
} else {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEqualExceptRef;
|
2011-05-22 05:12:46 +04:00
|
|
|
rv = aURI->EqualsExceptRef(binding->PrototypeBinding()->DocURI(),
|
|
|
|
&isEqualExceptRef);
|
|
|
|
if (NS_SUCCEEDED(rv) && isEqualExceptRef) {
|
2010-11-05 19:01:17 +03:00
|
|
|
// XXX sXBL/XBL2 issue
|
|
|
|
// Our content is an anonymous XBL element from a binding inside the
|
|
|
|
// same document that the referenced URI points to. In order to avoid
|
|
|
|
// the risk of ID collisions we restrict ourselves to anonymous
|
|
|
|
// elements from this binding; specifically, URIs that are relative to
|
|
|
|
// the binding document should resolve to the copy of the target
|
|
|
|
// element that has been inserted into the bound document.
|
|
|
|
// If the URI points to a different document we don't need this
|
|
|
|
// restriction.
|
|
|
|
nsINodeList* anonymousChildren =
|
|
|
|
doc->BindingManager()->GetAnonymousNodesFor(bindingParent);
|
|
|
|
|
|
|
|
if (anonymousChildren) {
|
2018-03-20 21:02:08 +03:00
|
|
|
uint32_t length = anonymousChildren->Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < length && !mElement; ++i) {
|
2010-11-05 19:01:17 +03:00
|
|
|
mElement =
|
2012-10-13 16:50:24 +04:00
|
|
|
nsContentUtils::MatchElementId(anonymousChildren->Item(i), ref);
|
2010-11-05 19:01:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have watching working yet for XBL, so bail out here.
|
|
|
|
return;
|
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 19:01:17 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEqualExceptRef;
|
2011-05-22 05:12:46 +04:00
|
|
|
rv = aURI->EqualsExceptRef(doc->GetDocumentURI(), &isEqualExceptRef);
|
|
|
|
if (NS_FAILED(rv) || !isEqualExceptRef) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIDocument::ExternalResourceLoad> load;
|
2011-05-22 05:12:46 +04:00
|
|
|
doc = doc->RequestExternalResource(aURI, aFromContent,
|
|
|
|
getter_AddRefs(load));
|
2018-08-17 14:35:15 +03:00
|
|
|
docOrShadow = doc;
|
2008-09-28 23:16:15 +04:00
|
|
|
if (!doc) {
|
|
|
|
if (!load || !aWatch) {
|
|
|
|
// Nothing will ever happen here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentLoadNotification* observer =
|
|
|
|
new DocumentLoadNotification(this, ref);
|
|
|
|
mPendingNotification = observer;
|
2018-08-17 14:35:15 +03:00
|
|
|
load->AddObserver(observer);
|
2008-09-28 23:16:15 +04:00
|
|
|
// Keep going so we set up our watching stuff a bit
|
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aWatch) {
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> atom = NS_Atomize(ref);
|
2008-06-25 05:55:43 +04:00
|
|
|
if (!atom)
|
|
|
|
return;
|
|
|
|
atom.swap(mWatchID);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
|
|
|
|
2010-08-13 17:35:36 +04:00
|
|
|
mReferencingImage = aReferenceImage;
|
2018-08-17 14:35:15 +03:00
|
|
|
HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, ref);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
|
|
|
|
2010-01-12 23:00:49 +03:00
|
|
|
void
|
2018-08-17 14:35:15 +03:00
|
|
|
IDTracker::ResetWithID(nsIContent* aFromContent,
|
|
|
|
nsAtom* aID,
|
2017-09-08 12:02:05 +03:00
|
|
|
bool aWatch)
|
2010-01-12 23:00:49 +03:00
|
|
|
{
|
2018-08-17 14:35:15 +03:00
|
|
|
MOZ_ASSERT(aFromContent);
|
|
|
|
MOZ_ASSERT(aID);
|
2010-01-12 23:00:49 +03:00
|
|
|
|
|
|
|
if (aWatch) {
|
2018-08-17 14:35:15 +03:00
|
|
|
RefPtr<nsAtom> atom = aID;
|
2010-01-12 23:00:49 +03:00
|
|
|
atom.swap(mWatchID);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mReferencingImage = false;
|
2010-08-13 17:35:36 +04:00
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
DocumentOrShadowRoot* docOrShadow = DocOrShadowFromContent(*aFromContent);
|
|
|
|
HaveNewDocumentOrShadowRoot(docOrShadow, aWatch, nsDependentAtomString(aID));
|
2010-01-12 23:00:49 +03:00
|
|
|
}
|
|
|
|
|
2008-09-28 23:16:15 +04:00
|
|
|
void
|
2018-08-17 14:35:15 +03:00
|
|
|
IDTracker::HaveNewDocumentOrShadowRoot(
|
|
|
|
DocumentOrShadowRoot* aDocOrShadow,
|
|
|
|
bool aWatch,
|
|
|
|
const nsString& aRef)
|
2008-09-28 23:16:15 +04:00
|
|
|
{
|
|
|
|
if (aWatch) {
|
2018-08-17 14:35:15 +03:00
|
|
|
mWatchDocumentOrShadowRoot = nullptr;
|
|
|
|
if (aDocOrShadow) {
|
|
|
|
mWatchDocumentOrShadowRoot = &aDocOrShadow->AsNode();
|
|
|
|
mElement = aDocOrShadow->AddIDTargetObserver(mWatchID, Observe, this, mReferencingImage);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
if (!aDocOrShadow) {
|
2008-09-28 23:16:15 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-06-24 01:35:57 +04:00
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
Element* e = mReferencingImage ? aDocOrShadow->LookupImageElement(aRef)
|
|
|
|
: aDocOrShadow->GetElementById(aRef);
|
2010-06-24 01:35:57 +04:00
|
|
|
if (e) {
|
|
|
|
mElement = e;
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-08 12:02:05 +03:00
|
|
|
IDTracker::Traverse(nsCycleCollectionTraversalCallback* aCB)
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
2018-08-17 14:35:15 +03:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mWatchDocumentOrShadowRoot");
|
|
|
|
aCB->NoteXPCOMChild(mWatchDocumentOrShadowRoot);
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mElement");
|
2010-05-14 21:04:51 +04:00
|
|
|
aCB->NoteXPCOMChild(mElement);
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-08 12:02:05 +03:00
|
|
|
IDTracker::Unlink()
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
2018-08-17 14:35:15 +03:00
|
|
|
if (mWatchID) {
|
|
|
|
if (DocumentOrShadowRoot* docOrShadow = GetWatchDocOrShadowRoot()) {
|
|
|
|
docOrShadow->RemoveIDTargetObserver(
|
|
|
|
mWatchID, Observe, this, mReferencingImage);
|
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
2008-09-28 23:16:15 +04:00
|
|
|
if (mPendingNotification) {
|
|
|
|
mPendingNotification->Clear();
|
2012-07-30 18:20:58 +04:00
|
|
|
mPendingNotification = nullptr;
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
2018-08-17 14:35:15 +03:00
|
|
|
mWatchDocumentOrShadowRoot = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mWatchID = nullptr;
|
|
|
|
mElement = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mReferencingImage = false;
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2018-08-17 14:35:15 +03:00
|
|
|
IDTracker::Observe(Element* aOldElement, Element* aNewElement, void* aData)
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
2017-09-08 12:02:05 +03:00
|
|
|
IDTracker* p = static_cast<IDTracker*>(aData);
|
2008-06-25 05:55:43 +04:00
|
|
|
if (p->mPendingNotification) {
|
2010-05-14 21:04:51 +04:00
|
|
|
p->mPendingNotification->SetTo(aNewElement);
|
2008-06-25 05:55:43 +04:00
|
|
|
} else {
|
2010-05-14 21:04:51 +04:00
|
|
|
NS_ASSERTION(aOldElement == p->mElement, "Failed to track content!");
|
2008-09-28 23:16:15 +04:00
|
|
|
ChangeNotification* watcher =
|
2010-05-14 21:04:51 +04:00
|
|
|
new ChangeNotification(p, aOldElement, aNewElement);
|
2008-09-28 23:16:15 +04:00
|
|
|
p->mPendingNotification = watcher;
|
|
|
|
nsContentUtils::AddScriptRunner(watcher);
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool keepTracking = p->IsPersistent();
|
2008-06-25 05:55:43 +04:00
|
|
|
if (!keepTracking) {
|
2018-08-17 14:35:15 +03:00
|
|
|
p->mWatchDocumentOrShadowRoot = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
p->mWatchID = nullptr;
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
return keepTracking;
|
|
|
|
}
|
2008-09-28 23:16:15 +04:00
|
|
|
|
2018-08-17 14:35:15 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(IDTracker::ChangeNotification, mozilla::Runnable)
|
|
|
|
NS_IMPL_ISUPPORTS(IDTracker::DocumentLoadNotification, nsIObserver)
|
2008-09-28 23:16:15 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-09-08 12:02:05 +03:00
|
|
|
IDTracker::DocumentLoadNotification::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2008-09-28 23:16:15 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(PL_strcmp(aTopic, "external-resource-document-created") == 0,
|
|
|
|
"Unexpected topic");
|
|
|
|
if (mTarget) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aSubject);
|
2012-07-30 18:20:58 +04:00
|
|
|
mTarget->mPendingNotification = nullptr;
|
2010-05-14 21:04:51 +04:00
|
|
|
NS_ASSERTION(!mTarget->mElement, "Why do we have content here?");
|
2008-09-28 23:16:15 +04:00
|
|
|
// If we got here, that means we had Reset() called with aWatch ==
|
2011-10-17 18:59:28 +04:00
|
|
|
// true. So keep watching if IsPersistent().
|
2018-08-17 14:35:15 +03:00
|
|
|
mTarget->HaveNewDocumentOrShadowRoot(doc, mTarget->IsPersistent(), mRef);
|
2012-07-30 18:20:58 +04:00
|
|
|
mTarget->ElementChanged(nullptr, mTarget->mElement);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-09-08 12:02:05 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|