2008-06-25 05:55:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
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
|
|
|
|
|
|
|
#include "nsReferencedElement.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsBindingManager.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
|
|
|
void
|
2010-08-13 17:35:36 +04:00
|
|
|
nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aWatch, bool aReferenceImage)
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
2015-02-07 02:04:32 +03:00
|
|
|
NS_ABORT_IF_FALSE(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
|
|
|
|
// origin charset of the URL, hopefully...
|
|
|
|
NS_UnescapeURL(refPart);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString charset;
|
2011-05-22 05:12:46 +04:00
|
|
|
aURI->GetOriginCharset(charset);
|
2008-06-25 05:55:43 +04:00
|
|
|
nsAutoString ref;
|
2013-12-17 14:47:25 +04:00
|
|
|
nsresult rv = nsContentUtils::ConvertStringFromEncoding(charset,
|
|
|
|
refPart,
|
|
|
|
ref);
|
2008-06-25 05:55:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-12-17 14:47:25 +04:00
|
|
|
// XXX Eww. If fallible malloc failed, using a conversion method that
|
|
|
|
// assumes UTF-8 and doesn't handle UTF-8 errors.
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=951082
|
2008-06-25 05:55:43 +04:00
|
|
|
CopyUTF8toUTF16(refPart, ref);
|
|
|
|
}
|
|
|
|
if (ref.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the current document
|
2014-10-02 22:45:44 +04:00
|
|
|
nsIDocument *doc = aFromContent->GetComposedDoc();
|
2008-06-25 05:55:43 +04:00
|
|
|
if (!doc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsIContent* bindingParent = aFromContent->GetBindingParent();
|
|
|
|
if (bindingParent) {
|
2013-07-17 20:05:03 +04:00
|
|
|
nsXBLBinding* binding = bindingParent->GetXBLBinding();
|
2008-06-25 05:55:43 +04:00
|
|
|
if (binding) {
|
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) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length;
|
2010-11-05 19:01:17 +03:00
|
|
|
anonymousChildren->GetLength(&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) {
|
2008-09-28 23:16:15 +04:00
|
|
|
nsRefPtr<nsIDocument::ExternalResourceLoad> load;
|
2011-05-22 05:12:46 +04:00
|
|
|
doc = doc->RequestExternalResource(aURI, aFromContent,
|
|
|
|
getter_AddRefs(load));
|
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;
|
|
|
|
if (observer) {
|
|
|
|
load->AddObserver(observer);
|
|
|
|
}
|
|
|
|
// Keep going so we set up our watching stuff a bit
|
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aWatch) {
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(ref);
|
|
|
|
if (!atom)
|
|
|
|
return;
|
|
|
|
atom.swap(mWatchID);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
|
|
|
|
2010-08-13 17:35:36 +04:00
|
|
|
mReferencingImage = aReferenceImage;
|
|
|
|
|
2008-09-28 23:16:15 +04:00
|
|
|
HaveNewDocument(doc, aWatch, ref);
|
|
|
|
}
|
|
|
|
|
2010-01-12 23:00:49 +03:00
|
|
|
void
|
|
|
|
nsReferencedElement::ResetWithID(nsIContent* aFromContent, const nsString& aID,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aWatch)
|
2010-01-12 23:00:49 +03:00
|
|
|
{
|
2014-10-02 22:45:44 +04:00
|
|
|
nsIDocument *doc = aFromContent->GetComposedDoc();
|
2010-01-12 23:00:49 +03:00
|
|
|
if (!doc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// XXX Need to take care of XBL/XBL2
|
|
|
|
|
|
|
|
if (aWatch) {
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(aID);
|
|
|
|
if (!atom)
|
|
|
|
return;
|
|
|
|
atom.swap(mWatchID);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mReferencingImage = false;
|
2010-08-13 17:35:36 +04:00
|
|
|
|
2010-01-12 23:00:49 +03:00
|
|
|
HaveNewDocument(doc, aWatch, aID);
|
|
|
|
}
|
|
|
|
|
2008-09-28 23:16:15 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsReferencedElement::HaveNewDocument(nsIDocument* aDocument, bool aWatch,
|
2008-09-28 23:16:15 +04:00
|
|
|
const nsString& aRef)
|
|
|
|
{
|
|
|
|
if (aWatch) {
|
|
|
|
mWatchDocument = aDocument;
|
|
|
|
if (mWatchDocument) {
|
2010-08-13 17:35:36 +04:00
|
|
|
mElement = mWatchDocument->AddIDTargetObserver(mWatchID, Observe, this,
|
|
|
|
mReferencingImage);
|
2008-09-28 23:16:15 +04:00
|
|
|
}
|
2008-06-25 05:55:43 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-28 23:16:15 +04:00
|
|
|
if (!aDocument) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-24 01:35:57 +04:00
|
|
|
|
2010-08-13 17:35:36 +04:00
|
|
|
Element *e = mReferencingImage ? aDocument->LookupImageElement(aRef) :
|
|
|
|
aDocument->GetElementById(aRef);
|
2010-06-24 01:35:57 +04:00
|
|
|
if (e) {
|
|
|
|
mElement = e;
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsReferencedElement::Traverse(nsCycleCollectionTraversalCallback* aCB)
|
|
|
|
{
|
2008-11-27 20:45:25 +03:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mWatchDocument");
|
2008-06-25 05:55:43 +04:00
|
|
|
aCB->NoteXPCOMChild(mWatchDocument);
|
2008-11-27 20:45:25 +03:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mContent");
|
2010-05-14 21:04:51 +04:00
|
|
|
aCB->NoteXPCOMChild(mElement);
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsReferencedElement::Unlink()
|
|
|
|
{
|
|
|
|
if (mWatchDocument && mWatchID) {
|
2010-08-13 17:35:36 +04:00
|
|
|
mWatchDocument->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
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mWatchDocument = nullptr;
|
|
|
|
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
|
2010-05-14 21:04:51 +04:00
|
|
|
nsReferencedElement::Observe(Element* aOldElement,
|
|
|
|
Element* aNewElement, void* aData)
|
2008-06-25 05:55:43 +04:00
|
|
|
{
|
|
|
|
nsReferencedElement* p = static_cast<nsReferencedElement*>(aData);
|
|
|
|
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) {
|
2012-07-30 18:20:58 +04:00
|
|
|
p->mWatchDocument = nullptr;
|
|
|
|
p->mWatchID = nullptr;
|
2008-06-25 05:55:43 +04:00
|
|
|
}
|
|
|
|
return keepTracking;
|
|
|
|
}
|
2008-09-28 23:16:15 +04:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(nsReferencedElement::ChangeNotification,
|
|
|
|
nsRunnable)
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsReferencedElement::DocumentLoadNotification,
|
|
|
|
nsIObserver)
|
2008-09-28 23:16:15 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsReferencedElement::DocumentLoadNotification::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
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().
|
2008-09-28 23:16:15 +04:00
|
|
|
mTarget->HaveNewDocument(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;
|
|
|
|
}
|