2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-05-01 22:50:08 +00:00
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
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/. */
|
2000-03-21 13:14:34 +00:00
|
|
|
|
2000-01-13 02:23:54 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2002-07-25 18:31:10 +00:00
|
|
|
#include "nsIAtom.h"
|
2010-07-15 01:53:11 +00:00
|
|
|
#include "nsXBLDocumentInfo.h"
|
2000-01-13 02:23:54 +00:00
|
|
|
#include "nsIInputStream.h"
|
2014-02-27 23:04:46 +00:00
|
|
|
#include "nsNameSpaceManager.h"
|
2000-01-13 02:23:54 +00:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2000-01-13 02:23:54 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2004-09-01 16:50:12 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-01 22:50:08 +00:00
|
|
|
#include "ChildIterator.h"
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2001-02-20 01:05:34 +00:00
|
|
|
#include "nsIXULDocument.h"
|
2003-04-11 00:56:27 +00:00
|
|
|
#endif
|
2000-01-13 02:23:54 +00:00
|
|
|
#include "nsIXMLContentSink.h"
|
2001-02-19 12:55:42 +00:00
|
|
|
#include "nsContentCID.h"
|
2013-03-20 16:22:26 +00:00
|
|
|
#include "mozilla/dom/XMLDocument.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
#include "jsapi.h"
|
2000-05-28 04:10:50 +00:00
|
|
|
#include "nsXBLService.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
#include "nsIXPConnect.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsIScriptContext.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2000-01-13 02:23:54 +00:00
|
|
|
|
2000-01-26 11:43:31 +00:00
|
|
|
// Event listeners
|
2014-03-17 06:56:53 +00:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2011-08-08 18:26:26 +00:00
|
|
|
#include "nsIDOMEventListener.h"
|
2005-12-28 21:52:39 +00:00
|
|
|
#include "nsAttrName.h"
|
2000-01-26 11:43:31 +00:00
|
|
|
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2001-11-02 01:53:13 +00:00
|
|
|
|
|
|
|
#include "nsXBLPrototypeHandler.h"
|
2000-09-20 07:16:04 +00:00
|
|
|
|
2003-03-06 23:59:18 +00:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2000-06-22 00:36:19 +00:00
|
|
|
#include "nsXBLBinding.h"
|
2005-06-03 01:54:50 +00:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-07-01 22:09:37 +00:00
|
|
|
#include "mozilla/dom/XBLChildrenElement.h"
|
2000-01-13 02:23:54 +00:00
|
|
|
|
2004-01-05 23:19:58 +00:00
|
|
|
#include "prprf.h"
|
2006-09-12 12:49:04 +00:00
|
|
|
#include "nsNodeUtils.h"
|
2013-02-08 14:24:21 +00:00
|
|
|
#include "nsJSUtils.h"
|
2014-06-26 16:29:05 +00:00
|
|
|
#include "nsCycleCollector.h"
|
2004-01-05 23:19:58 +00:00
|
|
|
|
2007-09-28 13:45:01 +00:00
|
|
|
// Nasty hack. Maybe we could move some of the classinfo utility methods
|
2012-06-19 23:01:10 +00:00
|
|
|
// (e.g. WrapNative) over to nsContentUtils?
|
2007-09-28 13:45:01 +00:00
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
|
2011-07-20 19:18:54 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-07-15 15:39:17 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-12-02 10:26:11 +00:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2011-07-20 19:18:54 +00:00
|
|
|
|
2012-12-16 01:26:05 +00:00
|
|
|
using namespace mozilla;
|
2012-09-12 20:29:30 +00:00
|
|
|
using namespace mozilla::dom;
|
2012-12-16 01:26:05 +00:00
|
|
|
|
2000-01-25 06:35:27 +00:00
|
|
|
// Helper classes
|
2000-01-13 02:23:54 +00:00
|
|
|
|
2000-03-31 03:13:43 +00:00
|
|
|
/***********************************************************************/
|
|
|
|
//
|
|
|
|
// The JS class for XBLBinding
|
|
|
|
//
|
2008-09-06 22:21:43 +00:00
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
XBLFinalize(JSFreeOp *fop, JSObject *obj)
|
2000-03-31 03:13:43 +00:00
|
|
|
{
|
2010-07-15 01:53:11 +00:00
|
|
|
nsXBLDocumentInfo* docInfo =
|
2012-02-05 20:07:23 +00:00
|
|
|
static_cast<nsXBLDocumentInfo*>(::JS_GetPrivate(obj));
|
2014-06-26 16:29:05 +00:00
|
|
|
cyclecollector::DeferredFinalize(docInfo);
|
2000-03-31 03:13:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2012-05-29 19:01:30 +00:00
|
|
|
XBLEnumerate(JSContext *cx, JS::Handle<JSObject*> obj)
|
|
|
|
{
|
|
|
|
nsXBLPrototypeBinding* protoBinding =
|
|
|
|
static_cast<nsXBLPrototypeBinding*>(::JS_GetReservedSlot(obj, 0).toPrivate());
|
|
|
|
MOZ_ASSERT(protoBinding);
|
|
|
|
|
|
|
|
return protoBinding->ResolveAllFields(cx, obj);
|
2007-09-28 13:45:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
static const JSClass gPrototypeJSClass = {
|
|
|
|
"XBL prototype JSClass",
|
2007-09-28 13:45:01 +00:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS |
|
|
|
|
// Our one reserved slot holds the relevant nsXBLPrototypeBinding
|
2014-04-11 14:59:47 +00:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1),
|
2014-11-22 18:23:39 +00:00
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
2014-12-02 22:56:35 +00:00
|
|
|
XBLEnumerate, nullptr,
|
|
|
|
nullptr, XBLFinalize,
|
2014-04-11 14:59:47 +00:00
|
|
|
nullptr, nullptr, nullptr, nullptr
|
|
|
|
};
|
2000-05-27 08:20:04 +00:00
|
|
|
|
2000-01-13 02:23:54 +00:00
|
|
|
// Implementation /////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Constructors/Destructors
|
2003-03-06 23:59:18 +00:00
|
|
|
nsXBLBinding::nsXBLBinding(nsXBLPrototypeBinding* aBinding)
|
2013-11-01 14:31:58 +00:00
|
|
|
: mMarkedForDeath(false)
|
2014-06-03 01:04:21 +00:00
|
|
|
, mUsingContentXBLScope(false)
|
2014-09-26 22:07:40 +00:00
|
|
|
, mIsShadowRootBinding(false)
|
2013-11-01 14:31:58 +00:00
|
|
|
, mPrototypeBinding(aBinding)
|
2000-01-13 02:23:54 +00:00
|
|
|
{
|
2004-02-15 19:24:42 +00:00
|
|
|
NS_ASSERTION(mPrototypeBinding, "Must have a prototype binding!");
|
|
|
|
// Grab a ref to the document info so the prototype binding won't die
|
|
|
|
NS_ADDREF(mPrototypeBinding->XBLDocumentInfo());
|
2000-01-13 02:23:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 10:26:11 +00:00
|
|
|
// Constructor used by web components.
|
|
|
|
nsXBLBinding::nsXBLBinding(ShadowRoot* aShadowRoot, nsXBLPrototypeBinding* aBinding)
|
|
|
|
: mMarkedForDeath(false),
|
2014-06-03 01:04:21 +00:00
|
|
|
mUsingContentXBLScope(false),
|
2014-09-26 22:07:40 +00:00
|
|
|
mIsShadowRootBinding(true),
|
2013-12-02 10:26:11 +00:00
|
|
|
mPrototypeBinding(aBinding),
|
|
|
|
mContent(aShadowRoot)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mPrototypeBinding, "Must have a prototype binding!");
|
|
|
|
// Grab a ref to the document info so the prototype binding won't die
|
|
|
|
NS_ADDREF(mPrototypeBinding->XBLDocumentInfo());
|
|
|
|
}
|
2000-04-03 07:13:07 +00:00
|
|
|
|
2000-01-13 02:23:54 +00:00
|
|
|
nsXBLBinding::~nsXBLBinding(void)
|
|
|
|
{
|
2014-09-26 22:07:40 +00:00
|
|
|
if (mContent && !mIsShadowRootBinding) {
|
|
|
|
// It is unnecessary to uninstall anonymous content in a shadow tree
|
|
|
|
// because the ShadowRoot itself is a DocumentFragment and does not
|
|
|
|
// need any additional cleanup.
|
2011-10-18 10:53:36 +00:00
|
|
|
nsXBLBinding::UninstallAnonymousContent(mContent->OwnerDoc(), mContent);
|
2009-01-06 19:37:28 +00:00
|
|
|
}
|
2010-07-15 01:53:11 +00:00
|
|
|
nsXBLDocumentInfo* info = mPrototypeBinding->XBLDocumentInfo();
|
2004-02-15 19:24:42 +00:00
|
|
|
NS_RELEASE(info);
|
2000-01-13 02:23:54 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLBinding)
|
|
|
|
|
2012-11-22 17:15:38 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLBinding)
|
2007-05-24 14:10:02 +00:00
|
|
|
// XXX Probably can't unlink mPrototypeBinding->XBLDocumentInfo(), because
|
|
|
|
// mPrototypeBinding is weak.
|
2014-09-26 22:07:40 +00:00
|
|
|
if (tmp->mContent && !tmp->mIsShadowRootBinding) {
|
2011-10-18 10:53:36 +00:00
|
|
|
nsXBLBinding::UninstallAnonymousContent(tmp->mContent->OwnerDoc(),
|
2009-01-06 19:37:28 +00:00
|
|
|
tmp->mContent);
|
|
|
|
}
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNextBinding)
|
2013-05-01 22:50:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDefaultInsertionPoint)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInsertionPoints)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnonymousContentList)
|
2007-05-24 14:10:02 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-11-22 17:15:38 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLBinding)
|
2012-01-23 23:25:53 +00:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
|
|
|
|
"mPrototypeBinding->XBLDocumentInfo()");
|
2013-08-09 16:25:14 +00:00
|
|
|
cb.NoteXPCOMChild(tmp->mPrototypeBinding->XBLDocumentInfo());
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNextBinding)
|
2013-05-01 22:50:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDefaultInsertionPoint)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInsertionPoints)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnonymousContentList)
|
2007-05-24 14:10:02 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsXBLBinding, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsXBLBinding, Release)
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
|
|
|
nsXBLBinding::SetBaseBinding(nsXBLBinding* aBinding)
|
2000-01-13 02:23:54 +00:00
|
|
|
{
|
2000-08-14 04:04:18 +00:00
|
|
|
if (mNextBinding) {
|
|
|
|
NS_ERROR("Base XBL binding is already defined!");
|
2005-02-25 22:07:01 +00:00
|
|
|
return;
|
2000-08-14 04:04:18 +00:00
|
|
|
}
|
|
|
|
|
2000-01-13 09:21:09 +00:00
|
|
|
mNextBinding = aBinding; // Comptr handles rel/add
|
2000-01-13 02:23:54 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
nsXBLBinding*
|
|
|
|
nsXBLBinding::GetBindingWithContent()
|
|
|
|
{
|
|
|
|
if (mContent) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNextBinding ? mNextBinding->GetBindingWithContent() : nullptr;
|
|
|
|
}
|
|
|
|
|
2001-03-07 01:46:13 +00:00
|
|
|
void
|
2012-10-10 19:04:42 +00:00
|
|
|
nsXBLBinding::InstallAnonymousContent(nsIContent* aAnonParent, nsIContent* aElement,
|
|
|
|
bool aChromeOnlyContent)
|
2000-01-13 02:23:54 +00:00
|
|
|
{
|
2001-03-07 01:46:13 +00:00
|
|
|
// We need to ensure two things.
|
2000-01-13 09:43:42 +00:00
|
|
|
// (1) The anonymous content should be fooled into thinking it's in the bound
|
2004-12-02 02:24:28 +00:00
|
|
|
// element's document, assuming that the bound element is in a document
|
2005-04-05 23:54:35 +00:00
|
|
|
// Note that we don't change the current doc of aAnonParent here, since that
|
|
|
|
// quite simply does not matter. aAnonParent is just a way of keeping refs
|
|
|
|
// to all its kids, which are anonymous content from the point of view of
|
|
|
|
// aElement.
|
2000-01-13 09:43:42 +00:00
|
|
|
// (2) The children's parent back pointer should not be to this synthetic root
|
2001-03-07 01:46:13 +00:00
|
|
|
// but should instead point to the enclosing parent element.
|
2005-04-05 23:54:35 +00:00
|
|
|
nsIDocument* doc = aElement->GetCurrentDoc();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool allowScripts = AllowScripts();
|
2005-04-05 23:54:35 +00:00
|
|
|
|
2008-04-11 17:29:06 +00:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2011-09-27 07:54:58 +00:00
|
|
|
for (nsIContent* child = aAnonParent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2005-04-05 23:54:35 +00:00
|
|
|
child->UnbindFromTree();
|
2012-10-10 19:04:42 +00:00
|
|
|
if (aChromeOnlyContent) {
|
|
|
|
child->SetFlags(NODE_CHROME_ONLY_ACCESS |
|
|
|
|
NODE_IS_ROOT_OF_CHROME_ONLY_ACCESS);
|
|
|
|
}
|
2005-04-05 23:54:35 +00:00
|
|
|
nsresult rv =
|
|
|
|
child->BindToTree(doc, aElement, mBoundElement, allowScripts);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Oh, well... Just give up.
|
|
|
|
// XXXbz This really shouldn't be a void method!
|
|
|
|
child->UnbindFromTree();
|
|
|
|
return;
|
2014-03-18 21:25:39 +00:00
|
|
|
}
|
2001-02-20 01:05:34 +00:00
|
|
|
|
2013-09-24 19:28:32 +00:00
|
|
|
child->SetFlags(NODE_IS_ANONYMOUS_ROOT);
|
2009-02-24 18:39:09 +00:00
|
|
|
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2001-02-20 01:05:34 +00:00
|
|
|
// To make XUL templates work (and other goodies that happen when
|
|
|
|
// an element is added to a XUL document), we need to notify the
|
|
|
|
// XUL document using its special API.
|
2003-04-11 00:56:27 +00:00
|
|
|
nsCOMPtr<nsIXULDocument> xuldoc(do_QueryInterface(doc));
|
2001-02-20 01:05:34 +00:00
|
|
|
if (xuldoc)
|
|
|
|
xuldoc->AddSubtreeToDocument(child);
|
2003-04-11 00:56:27 +00:00
|
|
|
#endif
|
2000-01-13 09:43:42 +00:00
|
|
|
}
|
2001-03-07 01:46:13 +00:00
|
|
|
}
|
2000-01-13 09:43:42 +00:00
|
|
|
|
2009-01-06 19:37:28 +00:00
|
|
|
void
|
|
|
|
nsXBLBinding::UninstallAnonymousContent(nsIDocument* aDocument,
|
|
|
|
nsIContent* aAnonParent)
|
|
|
|
{
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
// Hold a strong ref while doing this, just in case.
|
|
|
|
nsCOMPtr<nsIContent> anonParent = aAnonParent;
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
nsCOMPtr<nsIXULDocument> xuldoc =
|
|
|
|
do_QueryInterface(aDocument);
|
|
|
|
#endif
|
2011-09-27 07:54:58 +00:00
|
|
|
for (nsIContent* child = aAnonParent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2009-01-06 19:37:28 +00:00
|
|
|
child->UnbindFromTree();
|
|
|
|
#ifdef MOZ_XUL
|
|
|
|
if (xuldoc) {
|
|
|
|
xuldoc->RemoveSubtreeFromDocument(child);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-06-02 08:13:29 +00:00
|
|
|
nsXBLBinding::SetBoundElement(nsIContent* aElement)
|
2000-01-13 08:54:16 +00:00
|
|
|
{
|
2000-06-02 08:13:29 +00:00
|
|
|
mBoundElement = aElement;
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->SetBoundElement(aElement);
|
2013-11-01 14:31:58 +00:00
|
|
|
|
|
|
|
if (!mBoundElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute whether we're using an XBL scope.
|
|
|
|
//
|
|
|
|
// We disable XBL scopes for remote XUL, where we care about compat more
|
|
|
|
// than security. So we need to know whether we're using an XBL scope so that
|
|
|
|
// we can decide what to do about untrusted events when "allowuntrusted"
|
|
|
|
// is not given in the handler declaration.
|
|
|
|
nsCOMPtr<nsIGlobalObject> go = mBoundElement->OwnerDoc()->GetScopeObject();
|
|
|
|
NS_ENSURE_TRUE_VOID(go && go->GetGlobalJSObject());
|
2014-06-03 01:04:21 +00:00
|
|
|
mUsingContentXBLScope = xpc::UseContentXBLScope(js::GetObjectCompartment(go->GetGlobalJSObject()));
|
2000-06-02 08:13:29 +00:00
|
|
|
}
|
2000-01-13 09:43:42 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2005-02-25 22:07:01 +00:00
|
|
|
nsXBLBinding::HasStyleSheets() const
|
2000-09-08 10:01:18 +00:00
|
|
|
{
|
|
|
|
// Find out if we need to re-resolve style. We'll need to do this
|
|
|
|
// if we have additional stylesheets in our binding document.
|
2005-02-25 22:07:01 +00:00
|
|
|
if (mPrototypeBinding->HasStyleSheets())
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2000-09-08 10:01:18 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return mNextBinding ? mNextBinding->HasStyleSheets() : false;
|
2000-09-08 10:01:18 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2001-02-22 23:47:30 +00:00
|
|
|
nsXBLBinding::GenerateAnonymousContent()
|
2000-06-02 08:13:29 +00:00
|
|
|
{
|
2009-11-18 15:14:14 +00:00
|
|
|
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Someone forgot a script blocker");
|
|
|
|
|
2000-01-13 09:21:09 +00:00
|
|
|
// Fetch the content element for this binding.
|
2005-03-02 03:20:00 +00:00
|
|
|
nsIContent* content =
|
2006-12-26 17:47:52 +00:00
|
|
|
mPrototypeBinding->GetImmediateChild(nsGkAtoms::content);
|
2000-01-13 09:21:09 +00:00
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
// We have no anonymous content.
|
|
|
|
if (mNextBinding)
|
2005-02-25 22:07:01 +00:00
|
|
|
mNextBinding->GenerateAnonymousContent();
|
|
|
|
|
|
|
|
return;
|
2000-01-13 09:21:09 +00:00
|
|
|
}
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2000-09-27 20:23:49 +00:00
|
|
|
// Find out if we're really building kids or if we're just
|
|
|
|
// using the attribute-setting shorthand hack.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t contentCount = content->GetChildCount();
|
2000-07-27 06:20:57 +00:00
|
|
|
|
2000-09-27 20:23:49 +00:00
|
|
|
// Plan to build the content by default.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasContent = (contentCount > 0);
|
2013-05-01 22:50:08 +00:00
|
|
|
if (hasContent) {
|
2011-10-18 10:53:36 +00:00
|
|
|
nsIDocument* doc = mBoundElement->OwnerDoc();
|
2001-03-17 00:27:13 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
nsCOMPtr<nsINode> clonedNode;
|
|
|
|
nsCOMArray<nsINode> nodesWithProperties;
|
|
|
|
nsNodeUtils::Clone(content, true, doc->NodeInfoManager(),
|
|
|
|
nodesWithProperties, getter_AddRefs(clonedNode));
|
|
|
|
mContent = clonedNode->AsElement();
|
|
|
|
|
|
|
|
// Search for <xbl:children> elements in the XBL content. In the presence
|
|
|
|
// of multiple default insertion points, we use the last one in document
|
|
|
|
// order.
|
|
|
|
for (nsIContent* child = mContent; child; child = child->GetNextNode(mContent)) {
|
|
|
|
if (child->NodeInfo()->Equals(nsGkAtoms::children, kNameSpaceID_XBL)) {
|
2013-07-01 22:09:37 +00:00
|
|
|
XBLChildrenElement* point = static_cast<XBLChildrenElement*>(child);
|
2013-05-01 22:50:08 +00:00
|
|
|
if (point->IsDefaultInsertion()) {
|
|
|
|
mDefaultInsertionPoint = point;
|
|
|
|
} else {
|
|
|
|
mInsertionPoints.AppendElement(point);
|
2000-01-13 09:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// Do this after looking for <children> as this messes up the parent
|
|
|
|
// pointer which would make the GetNextNode call above fail
|
|
|
|
InstallAnonymousContent(mContent, mBoundElement,
|
|
|
|
mPrototypeBinding->ChromeOnlyContent());
|
2001-03-07 01:46:13 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// Insert explicit children into insertion points
|
|
|
|
if (mDefaultInsertionPoint && mInsertionPoints.IsEmpty()) {
|
|
|
|
ExplicitChildIterator iter(mBoundElement);
|
|
|
|
for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
|
2013-07-17 16:05:06 +00:00
|
|
|
mDefaultInsertionPoint->AppendInsertedChild(child);
|
2013-05-01 22:50:08 +00:00
|
|
|
}
|
2013-07-11 21:05:54 +00:00
|
|
|
} else {
|
|
|
|
// It is odd to come into this code if mInsertionPoints is not empty, but
|
|
|
|
// we need to make sure to do the compatibility hack below if the bound
|
2013-07-17 23:20:37 +00:00
|
|
|
// node has any non <xul:template> or <xul:observes> children.
|
2013-05-01 22:50:08 +00:00
|
|
|
ExplicitChildIterator iter(mBoundElement);
|
|
|
|
for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
|
2013-07-01 22:09:37 +00:00
|
|
|
XBLChildrenElement* point = FindInsertionPointForInternal(child);
|
2013-05-01 22:50:08 +00:00
|
|
|
if (point) {
|
2013-07-17 16:05:06 +00:00
|
|
|
point->AppendInsertedChild(child);
|
2013-07-11 21:05:54 +00:00
|
|
|
} else {
|
2014-06-20 02:01:40 +00:00
|
|
|
NodeInfo *ni = child->NodeInfo();
|
2013-07-11 21:05:54 +00:00
|
|
|
if (ni->NamespaceID() != kNameSpaceID_XUL ||
|
|
|
|
(!ni->Equals(nsGkAtoms::_template) &&
|
2013-07-17 23:20:37 +00:00
|
|
|
!ni->Equals(nsGkAtoms::observes))) {
|
2013-07-11 21:05:54 +00:00
|
|
|
// Compatibility hack. For some reason the original XBL
|
|
|
|
// implementation dropped the content of a binding if any child of
|
|
|
|
// the bound element didn't match any of the <children> in the
|
|
|
|
// binding. This became a pseudo-API that we have to maintain.
|
|
|
|
|
|
|
|
// Undo InstallAnonymousContent
|
|
|
|
UninstallAnonymousContent(doc, mContent);
|
|
|
|
|
|
|
|
// Clear out our children elements to avoid dangling references.
|
|
|
|
ClearInsertionPoints();
|
|
|
|
|
|
|
|
// Pretend as though there was no content in the binding.
|
|
|
|
mContent = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
2006-09-18 21:35:21 +00:00
|
|
|
}
|
2001-03-17 00:27:13 +00:00
|
|
|
}
|
2001-02-02 00:54:47 +00:00
|
|
|
}
|
2001-08-14 00:07:36 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// Set binding parent on default content if need
|
|
|
|
if (mDefaultInsertionPoint) {
|
2013-07-17 16:05:06 +00:00
|
|
|
mDefaultInsertionPoint->MaybeSetupDefaultContent();
|
2013-05-01 22:50:08 +00:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
2013-07-17 16:05:06 +00:00
|
|
|
mInsertionPoints[i]->MaybeSetupDefaultContent();
|
2013-05-01 22:50:08 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 00:07:36 +00:00
|
|
|
mPrototypeBinding->SetInitialAttributes(mBoundElement, mContent);
|
2000-09-27 20:23:49 +00:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
|
2000-09-27 20:23:49 +00:00
|
|
|
// Always check the content element for potential attributes.
|
|
|
|
// This shorthand hack always happens, even when we didn't
|
|
|
|
// build anonymous content.
|
2005-12-28 21:52:39 +00:00
|
|
|
const nsAttrName* attrName;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; (attrName = content->GetAttrNameAt(i)); ++i) {
|
|
|
|
int32_t namespaceID = attrName->NamespaceID();
|
2008-03-19 19:44:08 +00:00
|
|
|
// Hold a strong reference here so that the atom doesn't go away during
|
|
|
|
// UnsetAttr.
|
|
|
|
nsCOMPtr<nsIAtom> name = attrName->LocalName();
|
2000-09-15 06:38:35 +00:00
|
|
|
|
2006-12-26 17:47:52 +00:00
|
|
|
if (name != nsGkAtoms::includes) {
|
2006-04-24 05:40:11 +00:00
|
|
|
if (!nsContentUtils::HasNonEmptyAttr(mBoundElement, namespaceID, name)) {
|
2000-09-27 20:23:49 +00:00
|
|
|
nsAutoString value2;
|
2001-08-17 08:14:14 +00:00
|
|
|
content->GetAttr(namespaceID, name, value2);
|
2006-04-06 20:55:25 +00:00
|
|
|
mBoundElement->SetAttr(namespaceID, name, attrName->GetPrefix(),
|
2011-10-17 14:59:28 +00:00
|
|
|
value2, false);
|
2000-09-27 20:23:49 +00:00
|
|
|
}
|
2000-03-14 11:09:46 +00:00
|
|
|
}
|
2000-09-27 20:23:49 +00:00
|
|
|
|
|
|
|
// Conserve space by wiping the attributes off the clone.
|
|
|
|
if (mContent)
|
2011-10-17 14:59:28 +00:00
|
|
|
mContent->UnsetAttr(namespaceID, name, false);
|
2000-01-13 09:21:09 +00:00
|
|
|
}
|
2000-01-13 08:54:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 22:09:37 +00:00
|
|
|
XBLChildrenElement*
|
2013-05-01 22:50:08 +00:00
|
|
|
nsXBLBinding::FindInsertionPointFor(nsIContent* aChild)
|
|
|
|
{
|
|
|
|
// XXX We should get rid of this function as it causes us to traverse the
|
|
|
|
// binding chain multiple times
|
|
|
|
if (mContent) {
|
|
|
|
return FindInsertionPointForInternal(aChild);
|
|
|
|
}
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
return mNextBinding ? mNextBinding->FindInsertionPointFor(aChild)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:09:37 +00:00
|
|
|
XBLChildrenElement*
|
2013-05-01 22:50:08 +00:00
|
|
|
nsXBLBinding::FindInsertionPointForInternal(nsIContent* aChild)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
2013-07-01 22:09:37 +00:00
|
|
|
XBLChildrenElement* point = mInsertionPoints[i];
|
2013-05-01 22:50:08 +00:00
|
|
|
if (point->Includes(aChild)) {
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
}
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
return mDefaultInsertionPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLBinding::ClearInsertionPoints()
|
|
|
|
{
|
|
|
|
if (mDefaultInsertionPoint) {
|
|
|
|
mDefaultInsertionPoint->ClearInsertedChildren();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
|
|
|
mInsertionPoints[i]->ClearInsertedChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAnonymousContentList*
|
|
|
|
nsXBLBinding::GetAnonymousNodeList()
|
|
|
|
{
|
|
|
|
if (!mContent) {
|
|
|
|
return mNextBinding ? mNextBinding->GetAnonymousNodeList() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAnonymousContentList) {
|
|
|
|
mAnonymousContentList = new nsAnonymousContentList(mContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAnonymousContentList;
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2001-03-10 01:43:09 +00:00
|
|
|
nsXBLBinding::InstallEventHandlers()
|
2000-01-13 08:54:16 +00:00
|
|
|
{
|
2000-09-02 01:09:47 +00:00
|
|
|
// Don't install handlers if scripts aren't allowed.
|
2000-09-03 05:35:36 +00:00
|
|
|
if (AllowScripts()) {
|
|
|
|
// Fetch the handlers prototypes for this binding.
|
2003-03-06 23:59:18 +00:00
|
|
|
nsXBLPrototypeHandler* handlerChain = mPrototypeBinding->GetPrototypeHandlers();
|
2000-06-02 08:13:29 +00:00
|
|
|
|
2003-05-21 03:14:47 +00:00
|
|
|
if (handlerChain) {
|
2014-03-17 06:56:53 +00:00
|
|
|
EventListenerManager* manager = mBoundElement->GetOrCreateListenerManager();
|
2005-08-21 22:20:36 +00:00
|
|
|
if (!manager)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isChromeDoc =
|
2011-10-18 10:53:36 +00:00
|
|
|
nsContentUtils::IsChromeDoc(mBoundElement->OwnerDoc());
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isChromeBinding = mPrototypeBinding->IsChrome();
|
2003-09-11 12:25:06 +00:00
|
|
|
nsXBLPrototypeHandler* curr;
|
|
|
|
for (curr = handlerChain; curr; curr = curr->GetNextHandler()) {
|
2003-05-21 03:14:47 +00:00
|
|
|
// Fetch the event type.
|
|
|
|
nsCOMPtr<nsIAtom> eventAtom = curr->GetEventName();
|
2003-09-11 12:25:06 +00:00
|
|
|
if (!eventAtom ||
|
2006-12-26 17:47:52 +00:00
|
|
|
eventAtom == nsGkAtoms::keyup ||
|
|
|
|
eventAtom == nsGkAtoms::keydown ||
|
|
|
|
eventAtom == nsGkAtoms::keypress)
|
2003-05-21 03:14:47 +00:00
|
|
|
continue;
|
2003-09-11 12:25:06 +00:00
|
|
|
|
|
|
|
nsXBLEventHandler* handler = curr->GetEventHandler();
|
|
|
|
if (handler) {
|
2005-08-21 22:20:36 +00:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 06:56:52 +00:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mCapture = (curr->GetPhase() == NS_PHASE_CAPTURING);
|
2005-08-21 22:20:36 +00:00
|
|
|
|
2011-06-24 02:18:02 +00:00
|
|
|
// If this is a command, add it in the system event group
|
|
|
|
if ((curr->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND |
|
|
|
|
NS_HANDLER_TYPE_SYSTEM)) &&
|
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mInSystemGroup = true;
|
2011-06-24 02:18:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasAllowUntrustedAttr = curr->HasAllowUntrustedAttr();
|
2007-04-15 16:28:53 +00:00
|
|
|
if ((hasAllowUntrustedAttr && curr->AllowUntrustedEvents()) ||
|
2014-06-03 01:04:21 +00:00
|
|
|
(!hasAllowUntrustedAttr && !isChromeDoc && !mUsingContentXBLScope)) {
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mAllowUntrustedEvents = true;
|
2005-08-21 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
manager->AddEventListenerByType(handler,
|
|
|
|
nsDependentAtomString(eventAtom),
|
2011-06-24 02:18:02 +00:00
|
|
|
flags);
|
2001-03-30 02:30:17 +00:00
|
|
|
}
|
2003-09-11 12:25:06 +00:00
|
|
|
}
|
2000-09-03 05:35:36 +00:00
|
|
|
|
2003-09-11 12:25:06 +00:00
|
|
|
const nsCOMArray<nsXBLKeyEventHandler>* keyHandlers =
|
|
|
|
mPrototypeBinding->GetKeyEventHandlers();
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i;
|
2003-09-11 12:25:06 +00:00
|
|
|
for (i = 0; i < keyHandlers->Count(); ++i) {
|
|
|
|
nsXBLKeyEventHandler* handler = keyHandlers->ObjectAt(i);
|
2007-04-15 16:28:53 +00:00
|
|
|
handler->SetIsBoundToChrome(isChromeDoc);
|
2014-06-03 01:04:21 +00:00
|
|
|
handler->SetUsingContentXBLScope(mUsingContentXBLScope);
|
2000-09-03 05:35:36 +00:00
|
|
|
|
2003-09-11 12:25:06 +00:00
|
|
|
nsAutoString type;
|
|
|
|
handler->GetEventName(type);
|
|
|
|
|
2014-03-18 21:25:39 +00:00
|
|
|
// If this is a command, add it in the system event group, otherwise
|
2003-09-11 12:25:06 +00:00
|
|
|
// add it to the standard event group.
|
|
|
|
|
2005-08-21 22:20:36 +00:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 06:56:52 +00:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mCapture = (handler->GetPhase() == NS_PHASE_CAPTURING);
|
2005-08-21 22:20:36 +00:00
|
|
|
|
2011-06-24 02:18:02 +00:00
|
|
|
if ((handler->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND |
|
|
|
|
NS_HANDLER_TYPE_SYSTEM)) &&
|
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mInSystemGroup = true;
|
2011-06-24 02:18:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 01:26:05 +00:00
|
|
|
// For key handlers we have to set mAllowUntrustedEvents flag.
|
2005-08-21 22:20:36 +00:00
|
|
|
// Whether the handling of the event is allowed or not is handled in
|
|
|
|
// nsXBLKeyEventHandler::HandleEvent
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mAllowUntrustedEvents = true;
|
2005-08-21 22:20:36 +00:00
|
|
|
|
2011-06-24 02:18:02 +00:00
|
|
|
manager->AddEventListenerByType(handler, type, flags);
|
2000-01-26 11:43:31 +00:00
|
|
|
}
|
2000-09-03 05:35:36 +00:00
|
|
|
}
|
2000-01-26 11:43:31 +00:00
|
|
|
}
|
|
|
|
|
2001-03-10 01:43:09 +00:00
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->InstallEventHandlers();
|
2000-01-13 08:54:16 +00:00
|
|
|
}
|
|
|
|
|
2005-06-11 21:30:20 +00:00
|
|
|
nsresult
|
2001-11-13 04:09:56 +00:00
|
|
|
nsXBLBinding::InstallImplementation()
|
2000-03-29 01:24:35 +00:00
|
|
|
{
|
|
|
|
// Always install the base class properties first, so that
|
|
|
|
// derived classes can reference the base class properties.
|
2001-09-05 07:36:55 +00:00
|
|
|
|
2005-06-11 21:30:20 +00:00
|
|
|
if (mNextBinding) {
|
|
|
|
nsresult rv = mNextBinding->InstallImplementation();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-04-05 13:21:03 +00:00
|
|
|
|
2001-09-05 07:36:55 +00:00
|
|
|
// iterate through each property in the prototype's list and install the property.
|
2001-11-13 04:09:56 +00:00
|
|
|
if (AllowScripts())
|
2013-02-08 14:24:20 +00:00
|
|
|
return mPrototypeBinding->InstallImplementation(this);
|
2005-06-11 21:30:20 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-03-29 01:24:35 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
nsIAtom*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsXBLBinding::GetBaseTag(int32_t* aNameSpaceID)
|
2000-03-11 10:36:39 +00:00
|
|
|
{
|
2005-02-25 22:07:01 +00:00
|
|
|
nsIAtom *tag = mPrototypeBinding->GetBaseTag(aNameSpaceID);
|
|
|
|
if (!tag && mNextBinding)
|
|
|
|
return mNextBinding->GetBaseTag(aNameSpaceID);
|
|
|
|
|
|
|
|
return tag;
|
2000-03-11 10:36:39 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsXBLBinding::AttributeChanged(nsIAtom* aAttribute, int32_t aNameSpaceID,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aRemoveFlag, bool aNotify)
|
2000-01-25 06:35:27 +00:00
|
|
|
{
|
2000-09-27 20:23:49 +00:00
|
|
|
// XXX Change if we ever allow multiple bindings in a chain to contribute anonymous content
|
|
|
|
if (!mContent) {
|
|
|
|
if (mNextBinding)
|
2005-02-25 22:07:01 +00:00
|
|
|
mNextBinding->AttributeChanged(aAttribute, aNameSpaceID,
|
|
|
|
aRemoveFlag, aNotify);
|
|
|
|
} else {
|
|
|
|
mPrototypeBinding->AttributeChanged(aAttribute, aNameSpaceID, aRemoveFlag,
|
|
|
|
mBoundElement, mContent, aNotify);
|
2000-01-25 06:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-07-28 00:35:02 +00:00
|
|
|
nsXBLBinding::ExecuteAttachedHandler()
|
|
|
|
{
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->ExecuteAttachedHandler();
|
|
|
|
|
2011-06-11 20:43:33 +00:00
|
|
|
if (AllowScripts())
|
2005-01-27 19:21:14 +00:00
|
|
|
mPrototypeBinding->BindingAttached(mBoundElement);
|
2000-07-28 00:35:02 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-07-28 00:35:02 +00:00
|
|
|
nsXBLBinding::ExecuteDetachedHandler()
|
|
|
|
{
|
2005-01-27 19:21:14 +00:00
|
|
|
if (AllowScripts())
|
|
|
|
mPrototypeBinding->BindingDetached(mBoundElement);
|
2000-07-28 00:35:02 +00:00
|
|
|
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->ExecuteDetachedHandler();
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-06-22 00:36:19 +00:00
|
|
|
nsXBLBinding::UnhookEventHandlers()
|
|
|
|
{
|
2003-09-11 12:25:06 +00:00
|
|
|
nsXBLPrototypeHandler* handlerChain = mPrototypeBinding->GetPrototypeHandlers();
|
|
|
|
|
|
|
|
if (handlerChain) {
|
2014-03-17 06:56:53 +00:00
|
|
|
EventListenerManager* manager = mBoundElement->GetExistingListenerManager();
|
2007-11-15 22:29:08 +00:00
|
|
|
if (!manager) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isChromeBinding = mPrototypeBinding->IsChrome();
|
2003-09-11 12:25:06 +00:00
|
|
|
nsXBLPrototypeHandler* curr;
|
|
|
|
for (curr = handlerChain; curr; curr = curr->GetNextHandler()) {
|
|
|
|
nsXBLEventHandler* handler = curr->GetCachedEventHandler();
|
2007-11-15 22:29:08 +00:00
|
|
|
if (!handler) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2007-11-15 22:29:08 +00:00
|
|
|
nsCOMPtr<nsIAtom> eventAtom = curr->GetEventName();
|
|
|
|
if (!eventAtom ||
|
|
|
|
eventAtom == nsGkAtoms::keyup ||
|
|
|
|
eventAtom == nsGkAtoms::keydown ||
|
|
|
|
eventAtom == nsGkAtoms::keypress)
|
|
|
|
continue;
|
2003-09-11 12:25:06 +00:00
|
|
|
|
2007-11-15 22:29:08 +00:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 06:56:52 +00:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mCapture = (curr->GetPhase() == NS_PHASE_CAPTURING);
|
2003-09-11 12:25:06 +00:00
|
|
|
|
2007-11-15 22:29:08 +00:00
|
|
|
// If this is a command, remove it from the system event group,
|
|
|
|
// otherwise remove it from the standard event group.
|
2003-09-11 12:25:06 +00:00
|
|
|
|
2011-06-24 02:18:02 +00:00
|
|
|
if ((curr->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND |
|
|
|
|
NS_HANDLER_TYPE_SYSTEM)) &&
|
2009-06-23 10:07:39 +00:00
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mInSystemGroup = true;
|
2003-09-11 12:25:06 +00:00
|
|
|
}
|
2007-11-15 22:29:08 +00:00
|
|
|
|
2010-03-08 15:45:00 +00:00
|
|
|
manager->RemoveEventListenerByType(handler,
|
|
|
|
nsDependentAtomString(eventAtom),
|
2011-06-24 02:18:02 +00:00
|
|
|
flags);
|
2003-09-11 12:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsCOMArray<nsXBLKeyEventHandler>* keyHandlers =
|
|
|
|
mPrototypeBinding->GetKeyEventHandlers();
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i;
|
2003-09-11 12:25:06 +00:00
|
|
|
for (i = 0; i < keyHandlers->Count(); ++i) {
|
|
|
|
nsXBLKeyEventHandler* handler = keyHandlers->ObjectAt(i);
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
handler->GetEventName(type);
|
|
|
|
|
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 06:56:52 +00:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mCapture = (handler->GetPhase() == NS_PHASE_CAPTURING);
|
2003-09-11 12:25:06 +00:00
|
|
|
|
2014-03-18 21:25:39 +00:00
|
|
|
// If this is a command, remove it from the system event group, otherwise
|
2003-09-11 12:25:06 +00:00
|
|
|
// remove it from the standard event group.
|
|
|
|
|
2009-06-23 10:07:39 +00:00
|
|
|
if ((handler->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND | NS_HANDLER_TYPE_SYSTEM)) &&
|
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 01:26:05 +00:00
|
|
|
flags.mInSystemGroup = true;
|
2003-09-11 12:25:06 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 02:18:02 +00:00
|
|
|
manager->RemoveEventListenerByType(handler, type, flags);
|
2003-09-11 12:25:06 +00:00
|
|
|
}
|
2000-06-22 00:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
static void
|
2013-07-17 16:05:06 +00:00
|
|
|
UpdateInsertionParent(XBLChildrenElement* aPoint,
|
2013-05-01 22:50:08 +00:00
|
|
|
nsIContent* aOldBoundElement)
|
|
|
|
{
|
|
|
|
if (aPoint->IsDefaultInsertion()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < aPoint->InsertedChildrenLength(); ++i) {
|
2014-06-22 11:53:01 +00:00
|
|
|
nsIContent* child = aPoint->InsertedChild(i);
|
2013-05-01 22:50:08 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(child->GetParentNode());
|
|
|
|
|
|
|
|
// Here, we're iterating children that we inserted. There are two cases:
|
|
|
|
// either |child| is an explicit child of |aOldBoundElement| and is no
|
|
|
|
// longer inserted anywhere or it's a child of a <children> element
|
|
|
|
// parented to |aOldBoundElement|. In the former case, the child is no
|
|
|
|
// longer inserted anywhere, so we set its insertion parent to null. In the
|
|
|
|
// latter case, the child is now inserted into |aOldBoundElement| from some
|
|
|
|
// binding above us, so we set its insertion parent to aOldBoundElement.
|
|
|
|
if (child->GetParentNode() == aOldBoundElement) {
|
2013-07-17 16:05:06 +00:00
|
|
|
child->SetXBLInsertionParent(nullptr);
|
2013-05-01 22:50:08 +00:00
|
|
|
} else {
|
2013-07-17 16:05:06 +00:00
|
|
|
child->SetXBLInsertionParent(aOldBoundElement);
|
2013-05-01 22:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-05-04 04:25:50 +00:00
|
|
|
nsXBLBinding::ChangeDocument(nsIDocument* aOldDocument, nsIDocument* aNewDocument)
|
2000-03-31 10:27:12 +00:00
|
|
|
{
|
2013-04-05 13:21:01 +00:00
|
|
|
if (aOldDocument == aNewDocument)
|
|
|
|
return;
|
|
|
|
|
2013-07-11 15:58:28 +00:00
|
|
|
// Now the binding dies. Unhook our prototypes.
|
|
|
|
if (mPrototypeBinding->HasImplementation()) {
|
2014-07-15 15:39:17 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
// Init might fail here if we've cycle-collected the global object, since
|
|
|
|
// the Unlink phase of cycle collection happens after JS GC finalization.
|
|
|
|
// But in that case, we don't care about fixing the prototype chain, since
|
|
|
|
// everything's going away immediately.
|
|
|
|
if (jsapi.Init(aOldDocument->GetScopeObject())) {
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> scriptObject(cx, mBoundElement->GetWrapper());
|
|
|
|
if (scriptObject) {
|
|
|
|
// XXX Stay in sync! What if a layered binding has an
|
|
|
|
// <interface>?!
|
|
|
|
// XXXbz what does that comment mean, really? It seems to date
|
|
|
|
// back to when there was such a thing as an <interface>, whever
|
|
|
|
// that was...
|
|
|
|
|
|
|
|
// Find the right prototype.
|
|
|
|
JSAutoCompartment ac(cx, scriptObject);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> base(cx, scriptObject);
|
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
|
|
|
for ( ; true; base = proto) { // Will break out on null proto
|
|
|
|
if (!JS_GetPrototype(cx, base, &proto)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!proto) {
|
2013-07-11 15:58:28 +00:00
|
|
|
break;
|
2000-05-27 08:20:04 +00:00
|
|
|
}
|
2013-07-11 15:58:28 +00:00
|
|
|
|
2014-07-15 15:39:17 +00:00
|
|
|
if (JS_GetClass(proto) != &gPrototypeJSClass) {
|
|
|
|
// Clearly not the right class
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsXBLDocumentInfo> docInfo =
|
|
|
|
static_cast<nsXBLDocumentInfo*>(::JS_GetPrivate(proto));
|
|
|
|
if (!docInfo) {
|
|
|
|
// Not the proto we seek
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value protoBinding = ::JS_GetReservedSlot(proto, 0);
|
2013-07-11 15:58:28 +00:00
|
|
|
|
2014-07-15 15:39:17 +00:00
|
|
|
if (protoBinding.toPrivate() != mPrototypeBinding) {
|
|
|
|
// Not the right binding
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright! This is the right prototype. Pull it out of the
|
|
|
|
// proto chain.
|
|
|
|
JS::Rooted<JSObject*> grandProto(cx);
|
|
|
|
if (!JS_GetPrototype(cx, proto, &grandProto)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::JS_SetPrototype(cx, base, grandProto);
|
|
|
|
break;
|
2000-05-04 04:25:50 +00:00
|
|
|
}
|
2014-07-15 15:39:17 +00:00
|
|
|
|
|
|
|
mPrototypeBinding->UndefineFields(cx, scriptObject);
|
|
|
|
|
|
|
|
// Don't remove the reference from the document to the
|
|
|
|
// wrapper here since it'll be removed by the element
|
|
|
|
// itself when that's taken out of the document.
|
2000-05-04 04:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-05 13:21:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 15:58:28 +00:00
|
|
|
// Remove our event handlers
|
|
|
|
UnhookEventHandlers();
|
|
|
|
|
2013-04-05 13:21:01 +00:00
|
|
|
{
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2007-10-20 04:22:43 +00:00
|
|
|
|
2013-04-05 13:21:01 +00:00
|
|
|
// Then do our ancestors. This reverses the construction order, so that at
|
|
|
|
// all times things are consistent as far as everyone is concerned.
|
|
|
|
if (mNextBinding) {
|
|
|
|
mNextBinding->ChangeDocument(aOldDocument, aNewDocument);
|
|
|
|
}
|
2001-03-07 01:46:13 +00:00
|
|
|
|
2013-04-05 13:21:01 +00:00
|
|
|
// Update the anonymous content.
|
|
|
|
// XXXbz why not only for style bindings?
|
2014-09-26 22:07:40 +00:00
|
|
|
if (mContent && !mIsShadowRootBinding) {
|
2013-05-01 22:50:08 +00:00
|
|
|
nsXBLBinding::UninstallAnonymousContent(aOldDocument, mContent);
|
2013-04-05 13:21:01 +00:00
|
|
|
}
|
2009-11-18 15:14:14 +00:00
|
|
|
|
2013-05-01 22:50:08 +00:00
|
|
|
// Now that we've unbound our anonymous content from the tree and updated
|
|
|
|
// its binding parent, update the insertion parent for content inserted
|
|
|
|
// into our <children> elements.
|
|
|
|
if (mDefaultInsertionPoint) {
|
2013-07-17 16:05:06 +00:00
|
|
|
UpdateInsertionParent(mDefaultInsertionPoint, mBoundElement);
|
2004-10-24 17:30:47 +00:00
|
|
|
}
|
2013-05-01 22:50:08 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
2013-07-17 16:05:06 +00:00
|
|
|
UpdateInsertionParent(mInsertionPoints[i], mBoundElement);
|
2013-05-01 22:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that our inserted children no longer think they're inserted
|
|
|
|
// anywhere, make sure our internal state reflects that as well.
|
|
|
|
ClearInsertionPoints();
|
2000-03-31 10:27:12 +00:00
|
|
|
}
|
2000-08-12 06:28:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2005-02-25 22:07:01 +00:00
|
|
|
nsXBLBinding::InheritsStyle() const
|
2000-08-12 06:28:02 +00:00
|
|
|
{
|
2000-09-27 20:23:49 +00:00
|
|
|
// XXX Will have to change if we ever allow multiple bindings to contribute anonymous content.
|
|
|
|
// Most derived binding with anonymous content determines style inheritance for now.
|
|
|
|
|
|
|
|
// XXX What about bindings with <content> but no kids, e.g., my treecell-text binding?
|
2005-02-25 22:07:01 +00:00
|
|
|
if (mContent)
|
|
|
|
return mPrototypeBinding->InheritsStyle();
|
2014-03-18 21:25:39 +00:00
|
|
|
|
2000-09-27 20:23:49 +00:00
|
|
|
if (mNextBinding)
|
2005-02-25 22:07:01 +00:00
|
|
|
return mNextBinding->InheritsStyle();
|
2000-08-24 09:21:27 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2000-08-12 06:28:02 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2004-01-07 22:30:53 +00:00
|
|
|
nsXBLBinding::WalkRules(nsIStyleRuleProcessor::EnumFunc aFunc, void* aData)
|
2000-04-27 02:08:35 +00:00
|
|
|
{
|
2005-02-25 22:07:01 +00:00
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->WalkRules(aFunc, aData);
|
2000-08-12 06:28:02 +00:00
|
|
|
|
2004-07-28 07:08:41 +00:00
|
|
|
nsIStyleRuleProcessor *rules = mPrototypeBinding->GetRuleProcessor();
|
2000-09-08 10:01:18 +00:00
|
|
|
if (rules)
|
2004-07-28 07:08:41 +00:00
|
|
|
(*aFunc)(rules, aData);
|
2000-04-27 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2000-05-27 08:20:04 +00:00
|
|
|
// Internal helper methods ////////////////////////////////////////////////////////////////
|
2000-03-31 03:13:43 +00:00
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
// Get or create a WeakMap object on a given XBL-hosting global.
|
|
|
|
//
|
|
|
|
// The scheme is as follows. XBL-hosting globals (either privileged content
|
|
|
|
// Windows or XBL scopes) get two lazily-defined WeakMap properties. Each
|
|
|
|
// WeakMap is keyed by the grand-proto - i.e. the original prototype of the
|
|
|
|
// content before it was bound, and the prototype of the class object that we
|
|
|
|
// splice in. The values in the WeakMap are simple dictionary-style objects,
|
|
|
|
// mapping from XBL class names to class objects.
|
|
|
|
static JSObject*
|
|
|
|
GetOrCreateClassObjectMap(JSContext *cx, JS::Handle<JSObject*> scope, const char *mapName)
|
|
|
|
{
|
|
|
|
AssertSameCompartment(cx, scope);
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(scope));
|
|
|
|
MOZ_ASSERT(scope == xpc::GetXBLScopeOrGlobal(cx, scope));
|
|
|
|
|
|
|
|
// First, see if the map is already defined.
|
|
|
|
JS::Rooted<JSPropertyDescriptor> desc(cx);
|
2014-04-25 21:11:02 +00:00
|
|
|
if (!JS_GetOwnPropertyDescriptor(cx, scope, mapName, &desc)) {
|
2014-04-11 14:59:47 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (desc.object() && desc.value().isObject() &&
|
|
|
|
JS::IsWeakMapObject(&desc.value().toObject())) {
|
|
|
|
return &desc.value().toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's not there. Create and define it.
|
|
|
|
JS::Rooted<JSObject*> map(cx, JS::NewWeakMapObject(cx));
|
2014-01-22 02:11:17 +00:00
|
|
|
if (!map || !JS_DefineProperty(cx, scope, mapName, map,
|
2014-10-29 19:06:32 +00:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2014-10-29 19:06:31 +00:00
|
|
|
JS_STUBGETTER, JS_STUBSETTER))
|
2014-04-11 14:59:47 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
GetOrCreateMapEntryForPrototype(JSContext *cx, JS::Handle<JSObject*> proto)
|
|
|
|
{
|
|
|
|
AssertSameCompartment(cx, proto);
|
|
|
|
// We want to hang our class objects off the XBL scope. But since we also
|
|
|
|
// hoist anonymous content into the XBL scope, this creates the potential for
|
|
|
|
// tricky collisions, since we can simultaneously have a bound in-content
|
|
|
|
// node with grand-proto HTMLDivElement and a bound anonymous node whose
|
|
|
|
// grand-proto is the XBL scope's cross-compartment wrapper to HTMLDivElement.
|
|
|
|
// Since we have to wrap the WeakMap keys into its scope, this distinction
|
|
|
|
// would be lost if we don't do something about it.
|
|
|
|
//
|
|
|
|
// So we define two maps - one class objects that live in content (prototyped
|
|
|
|
// to content prototypes), and the other for class objects that live in the
|
|
|
|
// XBL scope (prototyped to cross-compartment-wrapped content prototypes).
|
2014-06-03 01:04:21 +00:00
|
|
|
const char* name = xpc::IsInContentXBLScope(proto) ? "__ContentClassObjectMap__"
|
|
|
|
: "__XBLClassObjectMap__";
|
2014-04-11 14:59:47 +00:00
|
|
|
|
|
|
|
// Now, enter the XBL scope, since that's where we need to operate, and wrap
|
2014-06-23 23:33:37 +00:00
|
|
|
// the proto accordingly. We hang the map off of the content XBL scope for
|
|
|
|
// content, and the Window for chrome (whether add-ons are involved or not).
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSObject*> scope(cx, xpc::GetXBLScopeOrGlobal(cx, proto));
|
2014-08-05 16:10:34 +00:00
|
|
|
NS_ENSURE_TRUE(scope, nullptr);
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSObject*> wrappedProto(cx, proto);
|
|
|
|
JSAutoCompartment ac(cx, scope);
|
|
|
|
if (!JS_WrapObject(cx, &wrappedProto)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the appropriate WeakMap.
|
|
|
|
JS::Rooted<JSObject*> map(cx, GetOrCreateClassObjectMap(cx, scope, name));
|
|
|
|
if (!map) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we already have a map entry for that prototype.
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
if (!JS::GetWeakMapEntry(cx, map, wrappedProto, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (val.isObject()) {
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have an entry. Create one and stick it in the map.
|
|
|
|
JS::Rooted<JSObject*> entry(cx);
|
|
|
|
entry = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), scope);
|
|
|
|
if (!entry) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> entryVal(cx, JS::ObjectValue(*entry));
|
|
|
|
if (!JS::SetWeakMapEntry(cx, map, wrappedProto, entryVal)) {
|
2014-04-14 21:17:53 +00:00
|
|
|
NS_WARNING("SetWeakMapEntry failed, probably due to non-preservable WeakMap "
|
|
|
|
"key. XBL binding will fail for this element.");
|
2014-04-11 14:59:47 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2002-06-21 20:55:17 +00:00
|
|
|
// static
|
|
|
|
nsresult
|
2014-03-19 16:35:45 +00:00
|
|
|
nsXBLBinding::DoInitJSClass(JSContext *cx,
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::Handle<JSObject*> obj,
|
2002-06-21 20:55:17 +00:00
|
|
|
const nsAFlatCString& aClassName,
|
2007-09-28 13:45:01 +00:00
|
|
|
nsXBLPrototypeBinding* aProtoBinding,
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::MutableHandle<JSObject*> aClassObject,
|
|
|
|
bool* aNew)
|
2000-03-31 03:13:43 +00:00
|
|
|
{
|
2014-04-11 14:59:46 +00:00
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
|
2014-03-19 16:35:45 +00:00
|
|
|
// Note that, now that NAC reflectors are created in the XBL scope, the
|
|
|
|
// reflector is not necessarily same-compartment with the document. So we'll
|
|
|
|
// end up creating a separate instance of the oddly-named XBL class object
|
|
|
|
// and defining it as a property on the XBL scope's global. This works fine,
|
|
|
|
// but we need to make sure never to assume that the the reflector and
|
|
|
|
// prototype are same-compartment with the bound document.
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2014-06-23 23:33:37 +00:00
|
|
|
|
|
|
|
// We never store class objects in add-on scopes.
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSObject*> xblScope(cx, xpc::GetXBLScopeOrGlobal(cx, global));
|
2014-08-05 16:10:34 +00:00
|
|
|
NS_ENSURE_TRUE(xblScope, NS_ERROR_UNEXPECTED);
|
2014-04-11 14:59:46 +00:00
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSObject*> parent_proto(cx);
|
2014-04-11 14:59:46 +00:00
|
|
|
if (!JS_GetPrototype(cx, obj, &parent_proto)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-12-12 15:10:02 +00:00
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
// Get the map entry for the parent prototype. In the one-off case that the
|
|
|
|
// parent prototype is null, we somewhat hackily just use the WeakMap itself
|
|
|
|
// as a property holder.
|
|
|
|
JS::Rooted<JSObject*> holder(cx);
|
|
|
|
if (parent_proto) {
|
|
|
|
holder = GetOrCreateMapEntryForPrototype(cx, parent_proto);
|
|
|
|
} else {
|
|
|
|
JSAutoCompartment innerAC(cx, xblScope);
|
|
|
|
holder = GetOrCreateClassObjectMap(cx, xblScope, "__ContentClassObjectMap__");
|
2004-01-05 23:19:58 +00:00
|
|
|
}
|
2014-04-14 21:17:53 +00:00
|
|
|
if (NS_WARN_IF(!holder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-04-11 14:59:47 +00:00
|
|
|
js::AssertSameCompartment(holder, xblScope);
|
|
|
|
JSAutoCompartment ac(cx, holder);
|
2004-01-05 23:19:58 +00:00
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
// Look up the class on the property holder. The only properties on the
|
|
|
|
// holder should be class objects. If we don't find the class object, we need
|
|
|
|
// to create and define it.
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2014-04-11 14:59:47 +00:00
|
|
|
JS::Rooted<JSPropertyDescriptor> desc(cx);
|
2014-04-25 21:11:02 +00:00
|
|
|
if (!JS_GetOwnPropertyDescriptor(cx, holder, aClassName.get(), &desc)) {
|
2013-04-05 13:21:01 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-04-11 14:59:47 +00:00
|
|
|
}
|
|
|
|
*aNew = !desc.object();
|
|
|
|
if (desc.object()) {
|
|
|
|
proto = &desc.value().toObject();
|
2014-04-11 14:59:47 +00:00
|
|
|
MOZ_ASSERT(JS_GetClass(js::UncheckedUnwrap(proto)) == &gPrototypeJSClass);
|
2013-04-05 13:21:01 +00:00
|
|
|
} else {
|
2000-07-01 02:36:18 +00:00
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
// We need to create the prototype. First, enter the compartment where it's
|
|
|
|
// going to live, and create it.
|
|
|
|
JSAutoCompartment ac2(cx, global);
|
2014-04-11 14:59:47 +00:00
|
|
|
proto = JS_NewObjectWithGivenProto(cx, &gPrototypeJSClass, parent_proto, global);
|
2014-04-11 14:59:47 +00:00
|
|
|
if (!proto) {
|
2000-07-01 02:36:18 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2000-03-31 03:13:43 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 13:45:01 +00:00
|
|
|
// Keep this proto binding alive while we're alive. Do this first so that
|
|
|
|
// we can guarantee that in XBLFinalize this will be non-null.
|
2010-09-15 19:40:11 +00:00
|
|
|
// Note that we can't just store aProtoBinding in the private and
|
|
|
|
// addref/release the nsXBLDocumentInfo through it, because cycle
|
|
|
|
// collection doesn't seem to work right if the private is not an
|
|
|
|
// nsISupports.
|
2010-07-15 01:53:11 +00:00
|
|
|
nsXBLDocumentInfo* docInfo = aProtoBinding->XBLDocumentInfo();
|
2012-02-05 20:07:23 +00:00
|
|
|
::JS_SetPrivate(proto, docInfo);
|
2007-09-28 13:45:01 +00:00
|
|
|
NS_ADDREF(docInfo);
|
2014-04-11 14:59:47 +00:00
|
|
|
JS_SetReservedSlot(proto, 0, PRIVATE_TO_JSVAL(aProtoBinding));
|
|
|
|
|
|
|
|
// Next, enter the compartment of the property holder, wrap the proto, and
|
|
|
|
// stick it on.
|
|
|
|
JSAutoCompartment ac3(cx, holder);
|
|
|
|
if (!JS_WrapObject(cx, &proto) ||
|
2014-01-22 02:11:17 +00:00
|
|
|
!JS_DefineProperty(cx, holder, aClassName.get(), proto,
|
2014-10-29 19:06:32 +00:00
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT,
|
2014-10-29 19:06:31 +00:00
|
|
|
JS_STUBGETTER, JS_STUBSETTER))
|
2014-04-11 14:59:47 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-09-14 13:33:54 +00:00
|
|
|
}
|
2002-06-21 20:55:17 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 14:59:47 +00:00
|
|
|
// Whew. We have the proto. Wrap it back into the compartment of |obj|,
|
|
|
|
// splice it in, and return it.
|
|
|
|
JSAutoCompartment ac4(cx, obj);
|
|
|
|
if (!JS_WrapObject(cx, &proto) || !JS_SetPrototype(cx, obj, proto)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
aClassObject.set(proto);
|
2002-06-21 20:55:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-05-19 04:48:43 +00:00
|
|
|
nsXBLBinding::AllowScripts()
|
|
|
|
{
|
2013-12-17 16:46:33 +00:00
|
|
|
return mBoundElement && mPrototypeBinding->GetAllowScripts();
|
2000-05-19 04:48:43 +00:00
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
nsXBLBinding*
|
|
|
|
nsXBLBinding::RootBinding()
|
2000-06-02 08:13:29 +00:00
|
|
|
{
|
|
|
|
if (mNextBinding)
|
2005-02-25 22:07:01 +00:00
|
|
|
return mNextBinding->RootBinding();
|
2000-06-02 08:13:29 +00:00
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
return this;
|
2000-06-02 08:13:29 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-04-05 13:21:03 +00:00
|
|
|
nsXBLBinding::ResolveAllFields(JSContext *cx, JS::Handle<JSObject*> obj) const
|
2007-09-28 13:45:01 +00:00
|
|
|
{
|
|
|
|
if (!mPrototypeBinding->ResolveAllFields(cx, obj)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2007-09-28 13:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mNextBinding) {
|
|
|
|
return mNextBinding->ResolveAllFields(cx, obj);
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2007-09-28 13:45:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 14:24:21 +00:00
|
|
|
bool
|
2013-11-11 08:04:41 +00:00
|
|
|
nsXBLBinding::LookupMember(JSContext* aCx, JS::Handle<jsid> aId,
|
2013-08-12 11:09:14 +00:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> aDesc)
|
2013-02-08 14:24:21 +00:00
|
|
|
{
|
|
|
|
// We should never enter this function with a pre-filled property descriptor.
|
2013-08-12 11:09:14 +00:00
|
|
|
MOZ_ASSERT(!aDesc.object());
|
2013-02-08 14:24:21 +00:00
|
|
|
|
|
|
|
// Get the string as an nsString before doing anything, so we can make
|
|
|
|
// convenient comparisons during our search.
|
|
|
|
if (!JSID_IS_STRING(aId)) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-05 15:30:54 +00:00
|
|
|
nsAutoJSString name;
|
|
|
|
if (!name.init(aCx, JSID_TO_STRING(aId))) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-08 14:24:21 +00:00
|
|
|
|
|
|
|
// We have a weak reference to our bound element, so make sure it's alive.
|
|
|
|
if (!mBoundElement || !mBoundElement->GetWrapper()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-13 18:16:19 +00:00
|
|
|
// Get the scope of mBoundElement and the associated XBL scope. We should only
|
|
|
|
// be calling into this machinery if we're running in a separate XBL scope.
|
2014-03-19 16:35:45 +00:00
|
|
|
//
|
|
|
|
// Note that we only end up in LookupMember for XrayWrappers from XBL scopes
|
|
|
|
// into content. So for NAC reflectors that live in the XBL scope, we should
|
|
|
|
// never get here. But on the off-chance that someone adds new callsites to
|
|
|
|
// LookupMember, we do a release-mode assertion as belt-and-braces.
|
|
|
|
// We do a release-mode assertion here to be extra safe.
|
2014-06-23 23:33:37 +00:00
|
|
|
//
|
|
|
|
// This code is only called for content XBL, so we don't have to worry about
|
|
|
|
// add-on scopes here.
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::Rooted<JSObject*> boundScope(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(mBoundElement->GetWrapper()));
|
2014-06-23 23:33:37 +00:00
|
|
|
MOZ_RELEASE_ASSERT(!xpc::IsInAddonScope(boundScope));
|
2014-06-03 01:04:21 +00:00
|
|
|
MOZ_RELEASE_ASSERT(!xpc::IsInContentXBLScope(boundScope));
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::Rooted<JSObject*> xblScope(aCx, xpc::GetXBLScope(aCx, boundScope));
|
2013-04-05 19:04:09 +00:00
|
|
|
NS_ENSURE_TRUE(xblScope, false);
|
2013-02-13 18:16:19 +00:00
|
|
|
MOZ_ASSERT(boundScope != xblScope);
|
2013-02-08 14:24:21 +00:00
|
|
|
|
2013-02-13 18:16:19 +00:00
|
|
|
// Enter the xbl scope and invoke the internal version.
|
2013-02-08 14:24:21 +00:00
|
|
|
{
|
2013-02-13 18:16:19 +00:00
|
|
|
JSAutoCompartment ac(aCx, xblScope);
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Rooted<jsid> id(aCx, aId);
|
2014-06-23 15:57:02 +00:00
|
|
|
if (!LookupMemberInternal(aCx, name, id, aDesc, xblScope)) {
|
2013-02-08 14:24:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap into the caller's scope.
|
|
|
|
return JS_WrapPropertyDescriptor(aCx, aDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsXBLBinding::LookupMemberInternal(JSContext* aCx, nsString& aName,
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Handle<jsid> aNameAsId,
|
2013-08-12 11:09:14 +00:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> aDesc,
|
2013-04-05 13:21:01 +00:00
|
|
|
JS::Handle<JSObject*> aXBLScope)
|
2013-02-08 14:24:21 +00:00
|
|
|
{
|
2014-04-11 14:59:47 +00:00
|
|
|
// First, see if we have an implementation. If we don't, it means that this
|
|
|
|
// binding doesn't have a class object, and thus doesn't have any members.
|
|
|
|
// Skip it.
|
|
|
|
if (!PrototypeBinding()->HasImplementation()) {
|
2013-02-08 14:24:21 +00:00
|
|
|
if (!mNextBinding) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return mNextBinding->LookupMemberInternal(aCx, aName, aNameAsId,
|
2013-02-13 18:16:19 +00:00
|
|
|
aDesc, aXBLScope);
|
2013-02-08 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 18:16:19 +00:00
|
|
|
// Find our class object. It's in a protected scope and permanent just in case,
|
|
|
|
// so should be there no matter what.
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Rooted<JS::Value> classObject(aCx);
|
2014-04-11 14:59:47 +00:00
|
|
|
if (!JS_GetProperty(aCx, aXBLScope, PrototypeBinding()->ClassName().get(),
|
|
|
|
&classObject)) {
|
2013-02-08 14:24:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 16:05:03 +00:00
|
|
|
|
|
|
|
// The bound element may have been adoped by a document and have a different
|
|
|
|
// wrapper (and different xbl scope) than when the binding was applied, in
|
|
|
|
// this case getting the class object will fail. Behave as if the class
|
|
|
|
// object did not exist.
|
|
|
|
if (classObject.isUndefined()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:24:21 +00:00
|
|
|
MOZ_ASSERT(classObject.isObject());
|
|
|
|
|
|
|
|
// Look for the property on this binding. If it's not there, try the next
|
|
|
|
// binding on the chain.
|
|
|
|
nsXBLProtoImpl* impl = mPrototypeBinding->GetImplementation();
|
2014-03-17 16:17:58 +00:00
|
|
|
JS::Rooted<JSObject*> object(aCx, &classObject.toObject());
|
|
|
|
if (impl && !impl->LookupMember(aCx, aName, aNameAsId, aDesc, object)) {
|
2013-02-08 14:24:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 11:09:14 +00:00
|
|
|
if (aDesc.object() || !mNextBinding) {
|
2013-02-08 14:24:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNextBinding->LookupMemberInternal(aCx, aName, aNameAsId, aDesc,
|
2013-02-13 18:16:19 +00:00
|
|
|
aXBLScope);
|
2013-02-08 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 14:24:22 +00:00
|
|
|
bool
|
|
|
|
nsXBLBinding::HasField(nsString& aName)
|
|
|
|
{
|
|
|
|
// See if this binding has such a field.
|
|
|
|
return mPrototypeBinding->FindField(aName) ||
|
|
|
|
(mNextBinding && mNextBinding->HasField(aName));
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:07:01 +00:00
|
|
|
void
|
2000-08-14 04:04:18 +00:00
|
|
|
nsXBLBinding::MarkForDeath()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mMarkedForDeath = true;
|
2000-09-27 20:23:49 +00:00
|
|
|
ExecuteDetachedHandler();
|
2000-08-14 04:04:18 +00:00
|
|
|
}
|
2000-06-02 08:13:29 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2005-02-25 22:07:01 +00:00
|
|
|
nsXBLBinding::ImplementsInterface(REFNSIID aIID) const
|
2000-12-07 10:11:21 +00:00
|
|
|
{
|
2005-02-25 22:07:01 +00:00
|
|
|
return mPrototypeBinding->ImplementsInterface(aIID) ||
|
|
|
|
(mNextBinding && mNextBinding->ImplementsInterface(aIID));
|
2000-12-07 10:11:21 +00:00
|
|
|
}
|