2008-06-24 18:55:43 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.org.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Robert O'Callahan <robert@ocallahan.org>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsReferencedElement.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsBindingManager.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
|
|
|
void
|
2010-08-13 06:35:36 -07:00
|
|
|
nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
|
|
|
|
PRBool aWatch, PRBool aReferenceImage)
|
2008-06-24 18:55:43 -07:00
|
|
|
{
|
2011-05-21 18:12:46 -07:00
|
|
|
NS_ABORT_IF_FALSE(aFromContent, "Reset() expects non-null content pointer");
|
2008-06-24 18:55:43 -07:00
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
Unlink();
|
2008-06-24 18:55:43 -07:00
|
|
|
|
2011-05-22 16:13:57 -07:00
|
|
|
if (!aURI)
|
|
|
|
return;
|
|
|
|
|
2008-06-24 18:55:43 -07:00
|
|
|
nsCAutoString refPart;
|
2011-05-21 18:12:46 -07:00
|
|
|
aURI->GetRef(refPart);
|
2008-06-24 18:55:43 -07:00
|
|
|
// Unescape %-escapes in the reference. The result will be in the
|
|
|
|
// origin charset of the URL, hopefully...
|
|
|
|
NS_UnescapeURL(refPart);
|
|
|
|
|
|
|
|
nsCAutoString charset;
|
2011-05-21 18:12:46 -07:00
|
|
|
aURI->GetOriginCharset(charset);
|
2008-06-24 18:55:43 -07:00
|
|
|
nsAutoString ref;
|
|
|
|
nsresult rv = nsContentUtils::ConvertStringFromCharset(charset, refPart, ref);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
CopyUTF8toUTF16(refPart, ref);
|
|
|
|
}
|
|
|
|
if (ref.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the current document
|
|
|
|
nsIDocument *doc = aFromContent->GetCurrentDoc();
|
|
|
|
if (!doc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsIContent* bindingParent = aFromContent->GetBindingParent();
|
|
|
|
if (bindingParent) {
|
|
|
|
nsXBLBinding* binding = doc->BindingManager()->GetBinding(bindingParent);
|
|
|
|
if (binding) {
|
2011-05-21 18:12:46 -07:00
|
|
|
PRBool isEqualExceptRef;
|
|
|
|
rv = aURI->EqualsExceptRef(binding->PrototypeBinding()->DocURI(),
|
|
|
|
&isEqualExceptRef);
|
|
|
|
if (NS_SUCCEEDED(rv) && isEqualExceptRef) {
|
2010-11-05 09:01:17 -07: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) {
|
|
|
|
PRUint32 length;
|
|
|
|
anonymousChildren->GetLength(&length);
|
|
|
|
for (PRUint32 i = 0; i < length && !mElement; ++i) {
|
|
|
|
mElement =
|
|
|
|
nsContentUtils::MatchElementId(anonymousChildren->GetNodeAt(i), ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have watching working yet for XBL, so bail out here.
|
|
|
|
return;
|
|
|
|
}
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 09:01:17 -07:00
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
PRBool isEqualExceptRef;
|
|
|
|
rv = aURI->EqualsExceptRef(doc->GetDocumentURI(), &isEqualExceptRef);
|
|
|
|
if (NS_FAILED(rv) || !isEqualExceptRef) {
|
2008-09-28 12:16:15 -07:00
|
|
|
nsRefPtr<nsIDocument::ExternalResourceLoad> load;
|
2011-05-21 18:12:46 -07:00
|
|
|
doc = doc->RequestExternalResource(aURI, aFromContent,
|
|
|
|
getter_AddRefs(load));
|
2008-09-28 12:16:15 -07: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-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aWatch) {
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(ref);
|
|
|
|
if (!atom)
|
|
|
|
return;
|
|
|
|
atom.swap(mWatchID);
|
2008-09-28 12:16:15 -07:00
|
|
|
}
|
|
|
|
|
2010-08-13 06:35:36 -07:00
|
|
|
mReferencingImage = aReferenceImage;
|
|
|
|
|
2008-09-28 12:16:15 -07:00
|
|
|
HaveNewDocument(doc, aWatch, ref);
|
|
|
|
}
|
|
|
|
|
2010-01-12 12:00:49 -08:00
|
|
|
void
|
|
|
|
nsReferencedElement::ResetWithID(nsIContent* aFromContent, const nsString& aID,
|
|
|
|
PRBool aWatch)
|
|
|
|
{
|
|
|
|
nsIDocument *doc = aFromContent->GetCurrentDoc();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-13 06:35:36 -07:00
|
|
|
mReferencingImage = PR_FALSE;
|
|
|
|
|
2010-01-12 12:00:49 -08:00
|
|
|
HaveNewDocument(doc, aWatch, aID);
|
|
|
|
}
|
|
|
|
|
2008-09-28 12:16:15 -07:00
|
|
|
void
|
|
|
|
nsReferencedElement::HaveNewDocument(nsIDocument* aDocument, PRBool aWatch,
|
|
|
|
const nsString& aRef)
|
|
|
|
{
|
|
|
|
if (aWatch) {
|
|
|
|
mWatchDocument = aDocument;
|
|
|
|
if (mWatchDocument) {
|
2010-08-13 06:35:36 -07:00
|
|
|
mElement = mWatchDocument->AddIDTargetObserver(mWatchID, Observe, this,
|
|
|
|
mReferencingImage);
|
2008-09-28 12:16:15 -07:00
|
|
|
}
|
2008-06-24 18:55:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-28 12:16:15 -07:00
|
|
|
if (!aDocument) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-23 14:35:57 -07:00
|
|
|
|
2010-08-13 06:35:36 -07:00
|
|
|
Element *e = mReferencingImage ? aDocument->LookupImageElement(aRef) :
|
|
|
|
aDocument->GetElementById(aRef);
|
2010-06-23 14:35:57 -07:00
|
|
|
if (e) {
|
|
|
|
mElement = e;
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsReferencedElement::Traverse(nsCycleCollectionTraversalCallback* aCB)
|
|
|
|
{
|
2008-11-27 09:45:25 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mWatchDocument");
|
2008-06-24 18:55:43 -07:00
|
|
|
aCB->NoteXPCOMChild(mWatchDocument);
|
2008-11-27 09:45:25 -08:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCB, "mContent");
|
2010-05-14 10:04:51 -07:00
|
|
|
aCB->NoteXPCOMChild(mElement);
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsReferencedElement::Unlink()
|
|
|
|
{
|
|
|
|
if (mWatchDocument && mWatchID) {
|
2010-08-13 06:35:36 -07:00
|
|
|
mWatchDocument->RemoveIDTargetObserver(mWatchID, Observe, this,
|
|
|
|
mReferencingImage);
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
2008-09-28 12:16:15 -07:00
|
|
|
if (mPendingNotification) {
|
|
|
|
mPendingNotification->Clear();
|
2009-01-19 14:49:21 -08:00
|
|
|
mPendingNotification = nsnull;
|
2008-09-28 12:16:15 -07:00
|
|
|
}
|
2008-06-24 18:55:43 -07:00
|
|
|
mWatchDocument = nsnull;
|
|
|
|
mWatchID = nsnull;
|
2010-05-14 10:04:51 -07:00
|
|
|
mElement = nsnull;
|
2010-08-13 06:35:36 -07:00
|
|
|
mReferencingImage = PR_FALSE;
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2010-05-14 10:04:51 -07:00
|
|
|
nsReferencedElement::Observe(Element* aOldElement,
|
|
|
|
Element* aNewElement, void* aData)
|
2008-06-24 18:55:43 -07:00
|
|
|
{
|
|
|
|
nsReferencedElement* p = static_cast<nsReferencedElement*>(aData);
|
|
|
|
if (p->mPendingNotification) {
|
2010-05-14 10:04:51 -07:00
|
|
|
p->mPendingNotification->SetTo(aNewElement);
|
2008-06-24 18:55:43 -07:00
|
|
|
} else {
|
2010-05-14 10:04:51 -07:00
|
|
|
NS_ASSERTION(aOldElement == p->mElement, "Failed to track content!");
|
2008-09-28 12:16:15 -07:00
|
|
|
ChangeNotification* watcher =
|
2010-05-14 10:04:51 -07:00
|
|
|
new ChangeNotification(p, aOldElement, aNewElement);
|
2008-09-28 12:16:15 -07:00
|
|
|
p->mPendingNotification = watcher;
|
|
|
|
nsContentUtils::AddScriptRunner(watcher);
|
2008-06-24 18:55:43 -07:00
|
|
|
}
|
|
|
|
PRBool keepTracking = p->IsPersistent();
|
|
|
|
if (!keepTracking) {
|
|
|
|
p->mWatchDocument = nsnull;
|
|
|
|
p->mWatchID = nsnull;
|
|
|
|
}
|
|
|
|
return keepTracking;
|
|
|
|
}
|
2008-09-28 12:16:15 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(nsReferencedElement::ChangeNotification,
|
|
|
|
nsRunnable)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsReferencedElement::DocumentLoadNotification,
|
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsReferencedElement::DocumentLoadNotification::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(PL_strcmp(aTopic, "external-resource-document-created") == 0,
|
|
|
|
"Unexpected topic");
|
|
|
|
if (mTarget) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aSubject);
|
|
|
|
mTarget->mPendingNotification = nsnull;
|
2010-05-14 10:04:51 -07:00
|
|
|
NS_ASSERTION(!mTarget->mElement, "Why do we have content here?");
|
2008-09-28 12:16:15 -07:00
|
|
|
// If we got here, that means we had Reset() called with aWatch ==
|
|
|
|
// PR_TRUE. So keep watching if IsPersistent().
|
|
|
|
mTarget->HaveNewDocument(doc, mTarget->IsPersistent(), mRef);
|
2010-05-14 10:04:51 -07:00
|
|
|
mTarget->ElementChanged(nsnull, mTarget->mElement);
|
2008-09-28 12:16:15 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|