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/. */
|
2000-03-21 16:14:34 +03:00
|
|
|
|
2000-01-13 05:23:54 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2010-07-15 05:53:11 +04:00
|
|
|
#include "nsXBLDocumentInfo.h"
|
2000-01-13 05:23:54 +03:00
|
|
|
#include "nsIInputStream.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2000-01-13 05:23:54 +03:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIChannel.h"
|
2017-08-17 08:29:03 +03:00
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-01-13 05:23:54 +03:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2004-09-01 20:50:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-02 02:50:08 +04:00
|
|
|
#include "ChildIterator.h"
|
2003-04-11 04:56:27 +04:00
|
|
|
#ifdef MOZ_XUL
|
2018-01-31 22:49:28 +03:00
|
|
|
#include "XULDocument.h"
|
2003-04-11 04:56:27 +04:00
|
|
|
#endif
|
2000-01-13 05:23:54 +03:00
|
|
|
#include "nsIXMLContentSink.h"
|
2001-02-19 15:55:42 +03:00
|
|
|
#include "nsContentCID.h"
|
2013-03-20 20:22:26 +04: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 20:46:42 +04:00
|
|
|
#include "jsapi.h"
|
2000-05-28 08:10:50 +04: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 20:46:42 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2001-07-16 06:40:48 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2000-01-13 05:23:54 +03:00
|
|
|
|
2000-01-26 14:43:31 +03:00
|
|
|
// Event listeners
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2011-08-08 22:26:26 +04:00
|
|
|
#include "nsIDOMEventListener.h"
|
2005-12-29 00:52:39 +03:00
|
|
|
#include "nsAttrName.h"
|
2000-01-26 14:43:31 +03:00
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2001-11-02 04:53:13 +03:00
|
|
|
|
|
|
|
#include "nsXBLPrototypeHandler.h"
|
2000-09-20 11:16:04 +04:00
|
|
|
|
2003-03-07 02:59:18 +03:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2000-06-22 04:36:19 +04:00
|
|
|
#include "nsXBLBinding.h"
|
2005-06-03 05:54:50 +04:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-07-02 02:09:37 +04:00
|
|
|
#include "mozilla/dom/XBLChildrenElement.h"
|
2000-01-13 05:23:54 +03:00
|
|
|
|
2006-09-12 16:49:04 +04:00
|
|
|
#include "nsNodeUtils.h"
|
2013-02-08 18:24:21 +04:00
|
|
|
#include "nsJSUtils.h"
|
2004-01-06 02:19:58 +03:00
|
|
|
|
2015-03-18 21:36:03 +03:00
|
|
|
#include "mozilla/DeferredFinalize.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-07-15 19:39:17 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
|
2012-12-16 05:26:05 +04:00
|
|
|
using namespace mozilla;
|
2012-09-13 00:29:30 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-12-16 05:26:05 +04:00
|
|
|
|
2000-01-25 09:35:27 +03:00
|
|
|
// Helper classes
|
2000-01-13 05:23:54 +03:00
|
|
|
|
2000-03-31 07:13:43 +04:00
|
|
|
/***********************************************************************/
|
|
|
|
//
|
|
|
|
// The JS class for XBLBinding
|
|
|
|
//
|
2008-09-07 02:21:43 +04:00
|
|
|
static void
|
2012-03-19 18:34:55 +04:00
|
|
|
XBLFinalize(JSFreeOp *fop, JSObject *obj)
|
2000-03-31 07:13:43 +04:00
|
|
|
{
|
2010-07-15 05:53:11 +04:00
|
|
|
nsXBLDocumentInfo* docInfo =
|
2012-02-06 00:07:23 +04:00
|
|
|
static_cast<nsXBLDocumentInfo*>(::JS_GetPrivate(obj));
|
2015-03-18 21:36:03 +03:00
|
|
|
DeferredFinalize(docInfo);
|
2000-03-31 07:13:43 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2012-05-29 23:01:30 +04: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 17:45:01 +04:00
|
|
|
}
|
|
|
|
|
2016-04-01 03:00:01 +03:00
|
|
|
static const JSClassOps gPrototypeJSClassOps = {
|
2017-08-25 11:12:16 +03:00
|
|
|
nullptr, nullptr,
|
2017-06-14 11:37:44 +03:00
|
|
|
XBLEnumerate, nullptr, nullptr,
|
2016-04-01 03:00:01 +03:00
|
|
|
nullptr, XBLFinalize,
|
|
|
|
nullptr, nullptr, nullptr, nullptr
|
|
|
|
};
|
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
static const JSClass gPrototypeJSClass = {
|
|
|
|
"XBL prototype JSClass",
|
2016-08-24 16:18:10 +03:00
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_PRIVATE_IS_NSISUPPORTS |
|
|
|
|
JSCLASS_FOREGROUND_FINALIZE |
|
2007-09-28 17:45:01 +04:00
|
|
|
// Our one reserved slot holds the relevant nsXBLPrototypeBinding
|
2014-04-11 18:59:47 +04:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1),
|
2016-04-01 03:00:01 +03:00
|
|
|
&gPrototypeJSClassOps
|
2014-04-11 18:59:47 +04:00
|
|
|
};
|
2000-05-27 12:20:04 +04:00
|
|
|
|
2000-01-13 05:23:54 +03:00
|
|
|
// Implementation /////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Constructors/Destructors
|
2003-03-07 02:59:18 +03:00
|
|
|
nsXBLBinding::nsXBLBinding(nsXBLPrototypeBinding* aBinding)
|
2013-11-01 18:31:58 +04:00
|
|
|
: mMarkedForDeath(false)
|
2014-06-03 05:04:21 +04:00
|
|
|
, mUsingContentXBLScope(false)
|
2014-09-27 02:07:40 +04:00
|
|
|
, mIsShadowRootBinding(false)
|
2013-11-01 18:31:58 +04:00
|
|
|
, mPrototypeBinding(aBinding)
|
2018-06-15 17:01:06 +03:00
|
|
|
, mBoundElement(nullptr)
|
2000-01-13 05:23:54 +03:00
|
|
|
{
|
2004-02-15 22:24:42 +03: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 05:23:54 +03:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:11 +04:00
|
|
|
// Constructor used by web components.
|
2018-04-13 16:01:28 +03:00
|
|
|
nsXBLBinding::nsXBLBinding(ShadowRoot* aShadowRoot, nsXBLPrototypeBinding* aBinding)
|
|
|
|
: mMarkedForDeath(false),
|
|
|
|
mUsingContentXBLScope(false),
|
|
|
|
mIsShadowRootBinding(true),
|
|
|
|
mPrototypeBinding(aBinding),
|
2018-06-15 17:01:06 +03:00
|
|
|
mContent(aShadowRoot),
|
|
|
|
mBoundElement(nullptr)
|
2013-12-02 14:26:11 +04: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-04-03 11:13:07 +04:00
|
|
|
|
2000-01-13 05:23:54 +03:00
|
|
|
nsXBLBinding::~nsXBLBinding(void)
|
|
|
|
{
|
2014-09-27 02:07:40 +04: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.
|
2017-09-11 14:11:01 +03:00
|
|
|
nsXBLBinding::UnbindAnonymousContent(mContent->OwnerDoc(), mContent);
|
2009-01-06 22:37:28 +03:00
|
|
|
}
|
2010-07-15 05:53:11 +04:00
|
|
|
nsXBLDocumentInfo* info = mPrototypeBinding->XBLDocumentInfo();
|
2004-02-15 22:24:42 +03:00
|
|
|
NS_RELEASE(info);
|
2000-01-13 05:23:54 +03:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLBinding)
|
|
|
|
|
2012-11-22 21:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLBinding)
|
2007-05-24 18:10:02 +04:00
|
|
|
// XXX Probably can't unlink mPrototypeBinding->XBLDocumentInfo(), because
|
|
|
|
// mPrototypeBinding is weak.
|
2014-09-27 02:07:40 +04:00
|
|
|
if (tmp->mContent && !tmp->mIsShadowRootBinding) {
|
2017-09-11 14:11:01 +03:00
|
|
|
nsXBLBinding::UnbindAnonymousContent(tmp->mContent->OwnerDoc(),
|
|
|
|
tmp->mContent);
|
2009-01-06 22:37:28 +03:00
|
|
|
}
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNextBinding)
|
2013-05-02 02:50:08 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDefaultInsertionPoint)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInsertionPoints)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnonymousContentList)
|
2007-05-24 18:10:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-11-22 21:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLBinding)
|
2012-01-24 03:25:53 +04:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
|
|
|
|
"mPrototypeBinding->XBLDocumentInfo()");
|
2013-08-09 20:25:14 +04:00
|
|
|
cb.NoteXPCOMChild(tmp->mPrototypeBinding->XBLDocumentInfo());
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNextBinding)
|
2013-05-02 02:50:08 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDefaultInsertionPoint)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInsertionPoints)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnonymousContentList)
|
2007-05-24 18:10:02 +04: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-26 01:07:01 +03:00
|
|
|
void
|
|
|
|
nsXBLBinding::SetBaseBinding(nsXBLBinding* aBinding)
|
2000-01-13 05:23:54 +03:00
|
|
|
{
|
2000-08-14 08:04:18 +04:00
|
|
|
if (mNextBinding) {
|
|
|
|
NS_ERROR("Base XBL binding is already defined!");
|
2005-02-26 01:07:01 +03:00
|
|
|
return;
|
2000-08-14 08:04:18 +04:00
|
|
|
}
|
|
|
|
|
2000-01-13 12:21:09 +03:00
|
|
|
mNextBinding = aBinding; // Comptr handles rel/add
|
2000-01-13 05:23:54 +03:00
|
|
|
}
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
nsXBLBinding*
|
|
|
|
nsXBLBinding::GetBindingWithContent()
|
|
|
|
{
|
|
|
|
if (mContent) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNextBinding ? mNextBinding->GetBindingWithContent() : nullptr;
|
|
|
|
}
|
|
|
|
|
2001-03-07 04:46:13 +03:00
|
|
|
void
|
2017-09-11 14:11:01 +03:00
|
|
|
nsXBLBinding::BindAnonymousContent(nsIContent* aAnonParent,
|
|
|
|
nsIContent* aElement,
|
|
|
|
bool aChromeOnlyContent)
|
2000-01-13 05:23:54 +03:00
|
|
|
{
|
2001-03-07 04:46:13 +03:00
|
|
|
// We need to ensure two things.
|
2000-01-13 12:43:42 +03:00
|
|
|
// (1) The anonymous content should be fooled into thinking it's in the bound
|
2004-12-02 05:24:28 +03:00
|
|
|
// element's document, assuming that the bound element is in a document
|
2005-04-06 03:54:35 +04: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 12:43:42 +03:00
|
|
|
// (2) The children's parent back pointer should not be to this synthetic root
|
2001-03-07 04:46:13 +03:00
|
|
|
// but should instead point to the enclosing parent element.
|
2016-03-31 14:46:32 +03:00
|
|
|
nsIDocument* doc = aElement->GetUncomposedDoc();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allowScripts = AllowScripts();
|
2005-04-06 03:54:35 +04:00
|
|
|
|
2008-04-11 21:29:06 +04:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* child = aAnonParent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2005-04-06 03:54:35 +04:00
|
|
|
child->UnbindFromTree();
|
2012-10-10 23:04:42 +04:00
|
|
|
if (aChromeOnlyContent) {
|
|
|
|
child->SetFlags(NODE_CHROME_ONLY_ACCESS |
|
|
|
|
NODE_IS_ROOT_OF_CHROME_ONLY_ACCESS);
|
|
|
|
}
|
2017-06-28 09:56:12 +03:00
|
|
|
child->SetFlags(NODE_IS_ANONYMOUS_ROOT);
|
2005-04-06 03:54:35 +04: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-19 01:25:39 +04:00
|
|
|
}
|
2001-02-20 04:05:34 +03:00
|
|
|
|
2003-04-11 04:56:27 +04:00
|
|
|
#ifdef MOZ_XUL
|
2001-02-20 04:05:34 +03: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.
|
2018-04-21 19:42:31 +03:00
|
|
|
if (doc && doc->IsXULDocument()) {
|
|
|
|
doc->AsXULDocument()->AddSubtreeToDocument(child);
|
2018-01-31 22:49:28 +03:00
|
|
|
}
|
2003-04-11 04:56:27 +04:00
|
|
|
#endif
|
2000-01-13 12:43:42 +03:00
|
|
|
}
|
2001-03-07 04:46:13 +03:00
|
|
|
}
|
2000-01-13 12:43:42 +03:00
|
|
|
|
2009-01-06 22:37:28 +03:00
|
|
|
void
|
2017-09-11 14:11:01 +03:00
|
|
|
nsXBLBinding::UnbindAnonymousContent(nsIDocument* aDocument,
|
2017-09-08 15:59:04 +03:00
|
|
|
nsIContent* aAnonParent,
|
|
|
|
bool aNullParent)
|
2009-01-06 22:37:28 +03:00
|
|
|
{
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
// Hold a strong ref while doing this, just in case.
|
|
|
|
nsCOMPtr<nsIContent> anonParent = aAnonParent;
|
|
|
|
#ifdef MOZ_XUL
|
2018-04-21 19:42:31 +03:00
|
|
|
const bool isXULDocument = aDocument && aDocument->IsXULDocument();
|
2009-01-06 22:37:28 +03:00
|
|
|
#endif
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* child = aAnonParent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2017-09-08 15:59:04 +03:00
|
|
|
child->UnbindFromTree(true, aNullParent);
|
2009-01-06 22:37:28 +03:00
|
|
|
#ifdef MOZ_XUL
|
2018-04-21 19:42:31 +03:00
|
|
|
if (isXULDocument) {
|
|
|
|
aDocument->AsXULDocument()->RemoveSubtreeFromDocument(child);
|
2009-01-06 22:37:28 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2017-12-05 20:05:51 +03:00
|
|
|
nsXBLBinding::SetBoundElement(Element* aElement)
|
2000-01-13 11:54:16 +03:00
|
|
|
{
|
2000-06-02 12:13:29 +04:00
|
|
|
mBoundElement = aElement;
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->SetBoundElement(aElement);
|
2013-11-01 18:31:58 +04: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());
|
2017-06-28 22:31:53 +03:00
|
|
|
mUsingContentXBLScope = xpc::UseContentXBLScope(JS::GetObjectRealmOrNull(go->GetGlobalJSObject()));
|
2000-06-02 12:13:29 +04:00
|
|
|
}
|
2000-01-13 12:43:42 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-02-26 01:07:01 +03:00
|
|
|
nsXBLBinding::HasStyleSheets() const
|
2000-09-08 14:01:18 +04: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-26 01:07:01 +03:00
|
|
|
if (mPrototypeBinding->HasStyleSheets())
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-09-08 14:01:18 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return mNextBinding ? mNextBinding->HasStyleSheets() : false;
|
2000-09-08 14:01:18 +04:00
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2001-02-23 02:47:30 +03:00
|
|
|
nsXBLBinding::GenerateAnonymousContent()
|
2000-06-02 12:13:29 +04:00
|
|
|
{
|
2009-11-18 18:14:14 +03:00
|
|
|
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
|
|
|
|
"Someone forgot a script blocker");
|
|
|
|
|
2000-01-13 12:21:09 +03:00
|
|
|
// Fetch the content element for this binding.
|
2017-12-05 20:05:51 +03:00
|
|
|
Element* content =
|
2006-12-26 20:47:52 +03:00
|
|
|
mPrototypeBinding->GetImmediateChild(nsGkAtoms::content);
|
2000-01-13 12:21:09 +03:00
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
// We have no anonymous content.
|
|
|
|
if (mNextBinding)
|
2005-02-26 01:07:01 +03:00
|
|
|
mNextBinding->GenerateAnonymousContent();
|
|
|
|
|
|
|
|
return;
|
2000-01-13 12:21:09 +03:00
|
|
|
}
|
2014-03-19 01:25:39 +04:00
|
|
|
|
2000-09-28 00:23:49 +04:00
|
|
|
// Find out if we're really building kids or if we're just
|
|
|
|
// using the attribute-setting shorthand hack.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t contentCount = content->GetChildCount();
|
2000-07-27 10:20:57 +04:00
|
|
|
|
2000-09-28 00:23:49 +04:00
|
|
|
// Plan to build the content by default.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasContent = (contentCount > 0);
|
2013-05-02 02:50:08 +04:00
|
|
|
if (hasContent) {
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = mBoundElement->OwnerDoc();
|
2001-03-17 03:27:13 +03:00
|
|
|
|
2017-09-13 20:34:55 +03:00
|
|
|
nsCOMPtr<nsINode> clonedNode =
|
2018-02-01 22:21:14 +03:00
|
|
|
nsNodeUtils::Clone(content, true, doc->NodeInfoManager(), nullptr,
|
|
|
|
IgnoreErrors());
|
2017-09-13 20:34:55 +03:00
|
|
|
// FIXME: Bug 1399558, Why is this code OK assuming that nsNodeUtils::Clone
|
|
|
|
// never fails?
|
2013-05-02 02:50:08 +04:00
|
|
|
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-02 02:09:37 +04:00
|
|
|
XBLChildrenElement* point = static_cast<XBLChildrenElement*>(child);
|
2013-05-02 02:50:08 +04:00
|
|
|
if (point->IsDefaultInsertion()) {
|
|
|
|
mDefaultInsertionPoint = point;
|
|
|
|
} else {
|
|
|
|
mInsertionPoints.AppendElement(point);
|
2000-01-13 12:21:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
// Do this after looking for <children> as this messes up the parent
|
|
|
|
// pointer which would make the GetNextNode call above fail
|
2017-09-11 14:11:01 +03:00
|
|
|
BindAnonymousContent(mContent, mBoundElement,
|
|
|
|
mPrototypeBinding->ChromeOnlyContent());
|
2001-03-07 04:46:13 +03:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
// Insert explicit children into insertion points
|
|
|
|
if (mDefaultInsertionPoint && mInsertionPoints.IsEmpty()) {
|
|
|
|
ExplicitChildIterator iter(mBoundElement);
|
|
|
|
for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
|
2018-04-14 16:04:53 +03:00
|
|
|
// Pass aNotify = false because we're just setting up the whole thing.
|
|
|
|
// Furthermore we do it from frame construction, so passing true here
|
|
|
|
// would reenter into it which is... not great.
|
|
|
|
mDefaultInsertionPoint->AppendInsertedChild(child, false);
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
2013-07-12 01:05:54 +04: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-18 03:20:37 +04:00
|
|
|
// node has any non <xul:template> or <xul:observes> children.
|
2013-05-02 02:50:08 +04:00
|
|
|
ExplicitChildIterator iter(mBoundElement);
|
|
|
|
for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
|
2013-07-02 02:09:37 +04:00
|
|
|
XBLChildrenElement* point = FindInsertionPointForInternal(child);
|
2013-05-02 02:50:08 +04:00
|
|
|
if (point) {
|
2018-04-14 16:04:53 +03:00
|
|
|
// Pass aNotify = false because we're just setting up the whole thing.
|
|
|
|
// (see the similar call above for more details).
|
|
|
|
point->AppendInsertedChild(child, false);
|
2013-07-12 01:05:54 +04:00
|
|
|
} else {
|
2014-06-20 06:01:40 +04:00
|
|
|
NodeInfo *ni = child->NodeInfo();
|
2013-07-12 01:05:54 +04:00
|
|
|
if (ni->NamespaceID() != kNameSpaceID_XUL ||
|
|
|
|
(!ni->Equals(nsGkAtoms::_template) &&
|
2013-07-18 03:20:37 +04:00
|
|
|
!ni->Equals(nsGkAtoms::observes))) {
|
2013-07-12 01:05:54 +04: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.
|
|
|
|
|
2017-09-11 14:11:01 +03:00
|
|
|
// Undo BindAnonymousContent
|
|
|
|
UnbindAnonymousContent(doc, mContent);
|
2013-07-12 01:05:54 +04:00
|
|
|
|
|
|
|
// 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-19 01:35:21 +04:00
|
|
|
}
|
2001-03-17 03:27:13 +03:00
|
|
|
}
|
2001-02-02 03:54:47 +03:00
|
|
|
}
|
2001-08-14 04:07:36 +04:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
// Set binding parent on default content if need
|
|
|
|
if (mDefaultInsertionPoint) {
|
2013-07-17 20:05:06 +04:00
|
|
|
mDefaultInsertionPoint->MaybeSetupDefaultContent();
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
2013-07-17 20:05:06 +04:00
|
|
|
mInsertionPoints[i]->MaybeSetupDefaultContent();
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
|
2001-08-14 04:07:36 +04:00
|
|
|
mPrototypeBinding->SetInitialAttributes(mBoundElement, mContent);
|
2000-09-28 00:23:49 +04:00
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
|
2000-09-28 00:23:49 +04:00
|
|
|
// Always check the content element for potential attributes.
|
|
|
|
// This shorthand hack always happens, even when we didn't
|
|
|
|
// build anonymous content.
|
2016-07-23 00:11:41 +03:00
|
|
|
BorrowedAttrInfo attrInfo;
|
2016-07-22 05:32:38 +03:00
|
|
|
for (uint32_t i = 0; (attrInfo = content->GetAttrInfoAt(i)); ++i) {
|
|
|
|
int32_t namespaceID = attrInfo.mName->NamespaceID();
|
2008-03-19 22:44:08 +03:00
|
|
|
// Hold a strong reference here so that the atom doesn't go away during
|
|
|
|
// UnsetAttr.
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> name = attrInfo.mName->LocalName();
|
2000-09-15 10:38:35 +04:00
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if (name != nsGkAtoms::includes) {
|
2006-04-24 09:40:11 +04:00
|
|
|
if (!nsContentUtils::HasNonEmptyAttr(mBoundElement, namespaceID, name)) {
|
2000-09-28 00:23:49 +04:00
|
|
|
nsAutoString value2;
|
2016-07-22 05:32:38 +03:00
|
|
|
attrInfo.mValue->ToString(value2);
|
|
|
|
mBoundElement->SetAttr(namespaceID, name, attrInfo.mName->GetPrefix(),
|
2011-10-17 18:59:28 +04:00
|
|
|
value2, false);
|
2000-09-28 00:23:49 +04:00
|
|
|
}
|
2000-03-14 14:09:46 +03:00
|
|
|
}
|
2000-09-28 00:23:49 +04:00
|
|
|
|
|
|
|
// Conserve space by wiping the attributes off the clone.
|
2017-12-05 20:05:51 +03:00
|
|
|
//
|
|
|
|
// FIXME(emilio): It'd be nice to make `mContent` a `RefPtr<Element>`, but
|
|
|
|
// as of right now it can also be a ShadowRoot (we don't enter in this
|
|
|
|
// codepath though). Move Shadow DOM outside XBL and then fix that.
|
2000-09-28 00:23:49 +04:00
|
|
|
if (mContent)
|
2017-12-05 20:05:51 +03:00
|
|
|
mContent->AsElement()->UnsetAttr(namespaceID, name, false);
|
2000-01-13 12:21:09 +03:00
|
|
|
}
|
2000-01-13 11:54:16 +03:00
|
|
|
}
|
|
|
|
|
2016-09-20 09:13:13 +03:00
|
|
|
nsIURI*
|
|
|
|
nsXBLBinding::GetSourceDocURI()
|
|
|
|
{
|
|
|
|
nsIContent* targetContent =
|
|
|
|
mPrototypeBinding->GetImmediateChild(nsGkAtoms::content);
|
|
|
|
if (!targetContent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetContent->OwnerDoc()->GetDocumentURI();
|
|
|
|
}
|
|
|
|
|
2013-07-02 02:09:37 +04:00
|
|
|
XBLChildrenElement*
|
2013-05-02 02:50:08 +04: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-19 01:25:39 +04:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
return mNextBinding ? mNextBinding->FindInsertionPointFor(aChild)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-02 02:09:37 +04:00
|
|
|
XBLChildrenElement*
|
2013-05-02 02:50:08 +04:00
|
|
|
nsXBLBinding::FindInsertionPointForInternal(nsIContent* aChild)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mInsertionPoints.Length(); ++i) {
|
2013-07-02 02:09:37 +04:00
|
|
|
XBLChildrenElement* point = mInsertionPoints[i];
|
2013-05-02 02:50:08 +04:00
|
|
|
if (point->Includes(aChild)) {
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 01:25:39 +04:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
return mDefaultInsertionPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLBinding::ClearInsertionPoints()
|
|
|
|
{
|
|
|
|
if (mDefaultInsertionPoint) {
|
|
|
|
mDefaultInsertionPoint->ClearInsertedChildren();
|
|
|
|
}
|
|
|
|
|
2017-10-23 16:52:08 +03:00
|
|
|
for (const auto& insertionPoint : mInsertionPoints) {
|
|
|
|
insertionPoint->ClearInsertedChildren();
|
2013-05-02 02:50:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAnonymousContentList*
|
|
|
|
nsXBLBinding::GetAnonymousNodeList()
|
|
|
|
{
|
|
|
|
if (!mContent) {
|
|
|
|
return mNextBinding ? mNextBinding->GetAnonymousNodeList() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAnonymousContentList) {
|
|
|
|
mAnonymousContentList = new nsAnonymousContentList(mContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAnonymousContentList;
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2001-03-10 04:43:09 +03:00
|
|
|
nsXBLBinding::InstallEventHandlers()
|
2000-01-13 11:54:16 +03:00
|
|
|
{
|
2000-09-02 05:09:47 +04:00
|
|
|
// Don't install handlers if scripts aren't allowed.
|
2000-09-03 09:35:36 +04:00
|
|
|
if (AllowScripts()) {
|
|
|
|
// Fetch the handlers prototypes for this binding.
|
2003-03-07 02:59:18 +03:00
|
|
|
nsXBLPrototypeHandler* handlerChain = mPrototypeBinding->GetPrototypeHandlers();
|
2000-06-02 12:13:29 +04:00
|
|
|
|
2003-05-21 07:14:47 +04:00
|
|
|
if (handlerChain) {
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* manager = mBoundElement->GetOrCreateListenerManager();
|
2005-08-22 02:20:36 +04:00
|
|
|
if (!manager)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isChromeDoc =
|
2011-10-18 14:53:36 +04:00
|
|
|
nsContentUtils::IsChromeDoc(mBoundElement->OwnerDoc());
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isChromeBinding = mPrototypeBinding->IsChrome();
|
2003-09-11 16:25:06 +04:00
|
|
|
nsXBLPrototypeHandler* curr;
|
|
|
|
for (curr = handlerChain; curr; curr = curr->GetNextHandler()) {
|
2003-05-21 07:14:47 +04:00
|
|
|
// Fetch the event type.
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> eventAtom = curr->GetEventName();
|
2003-09-11 16:25:06 +04:00
|
|
|
if (!eventAtom ||
|
2006-12-26 20:47:52 +03:00
|
|
|
eventAtom == nsGkAtoms::keyup ||
|
|
|
|
eventAtom == nsGkAtoms::keydown ||
|
|
|
|
eventAtom == nsGkAtoms::keypress)
|
2003-05-21 07:14:47 +04:00
|
|
|
continue;
|
2003-09-11 16:25:06 +04:00
|
|
|
|
|
|
|
nsXBLEventHandler* handler = curr->GetEventHandler();
|
|
|
|
if (handler) {
|
2005-08-22 02:20:36 +04:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 10:56:52 +04:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mCapture = (curr->GetPhase() == NS_PHASE_CAPTURING);
|
2005-08-22 02:20:36 +04:00
|
|
|
|
2011-06-24 06:18:02 +04: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 05:26:05 +04:00
|
|
|
flags.mInSystemGroup = true;
|
2011-06-24 06:18:02 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasAllowUntrustedAttr = curr->HasAllowUntrustedAttr();
|
2007-04-15 20:28:53 +04:00
|
|
|
if ((hasAllowUntrustedAttr && curr->AllowUntrustedEvents()) ||
|
2014-06-03 05:04:21 +04:00
|
|
|
(!hasAllowUntrustedAttr && !isChromeDoc && !mUsingContentXBLScope)) {
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mAllowUntrustedEvents = true;
|
2005-08-22 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
manager->AddEventListenerByType(handler,
|
|
|
|
nsDependentAtomString(eventAtom),
|
2011-06-24 06:18:02 +04:00
|
|
|
flags);
|
2001-03-30 06:30:17 +04:00
|
|
|
}
|
2003-09-11 16:25:06 +04:00
|
|
|
}
|
2000-09-03 09:35:36 +04:00
|
|
|
|
2003-09-11 16:25:06 +04:00
|
|
|
const nsCOMArray<nsXBLKeyEventHandler>* keyHandlers =
|
|
|
|
mPrototypeBinding->GetKeyEventHandlers();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2003-09-11 16:25:06 +04:00
|
|
|
for (i = 0; i < keyHandlers->Count(); ++i) {
|
|
|
|
nsXBLKeyEventHandler* handler = keyHandlers->ObjectAt(i);
|
2007-04-15 20:28:53 +04:00
|
|
|
handler->SetIsBoundToChrome(isChromeDoc);
|
2014-06-03 05:04:21 +04:00
|
|
|
handler->SetUsingContentXBLScope(mUsingContentXBLScope);
|
2000-09-03 09:35:36 +04:00
|
|
|
|
2003-09-11 16:25:06 +04:00
|
|
|
nsAutoString type;
|
|
|
|
handler->GetEventName(type);
|
|
|
|
|
2014-03-19 01:25:39 +04:00
|
|
|
// If this is a command, add it in the system event group, otherwise
|
2003-09-11 16:25:06 +04:00
|
|
|
// add it to the standard event group.
|
|
|
|
|
2005-08-22 02:20:36 +04:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 10:56:52 +04:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mCapture = (handler->GetPhase() == NS_PHASE_CAPTURING);
|
2005-08-22 02:20:36 +04:00
|
|
|
|
2011-06-24 06:18:02 +04:00
|
|
|
if ((handler->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND |
|
|
|
|
NS_HANDLER_TYPE_SYSTEM)) &&
|
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mInSystemGroup = true;
|
2011-06-24 06:18:02 +04:00
|
|
|
}
|
|
|
|
|
2012-12-16 05:26:05 +04:00
|
|
|
// For key handlers we have to set mAllowUntrustedEvents flag.
|
2005-08-22 02:20:36 +04:00
|
|
|
// Whether the handling of the event is allowed or not is handled in
|
|
|
|
// nsXBLKeyEventHandler::HandleEvent
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mAllowUntrustedEvents = true;
|
2005-08-22 02:20:36 +04:00
|
|
|
|
2011-06-24 06:18:02 +04:00
|
|
|
manager->AddEventListenerByType(handler, type, flags);
|
2000-01-26 14:43:31 +03:00
|
|
|
}
|
2000-09-03 09:35:36 +04:00
|
|
|
}
|
2000-01-26 14:43:31 +03:00
|
|
|
}
|
|
|
|
|
2001-03-10 04:43:09 +03:00
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->InstallEventHandlers();
|
2000-01-13 11:54:16 +03:00
|
|
|
}
|
|
|
|
|
2005-06-12 01:30:20 +04:00
|
|
|
nsresult
|
2001-11-13 07:09:56 +03:00
|
|
|
nsXBLBinding::InstallImplementation()
|
2000-03-29 05:24:35 +04:00
|
|
|
{
|
|
|
|
// Always install the base class properties first, so that
|
|
|
|
// derived classes can reference the base class properties.
|
2001-09-05 11:36:55 +04:00
|
|
|
|
2005-06-12 01:30:20 +04:00
|
|
|
if (mNextBinding) {
|
|
|
|
nsresult rv = mNextBinding->InstallImplementation();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-04-05 17:21:03 +04:00
|
|
|
|
2001-09-05 11:36:55 +04:00
|
|
|
// iterate through each property in the prototype's list and install the property.
|
2001-11-13 07:09:56 +03:00
|
|
|
if (AllowScripts())
|
2013-02-08 18:24:20 +04:00
|
|
|
return mPrototypeBinding->InstallImplementation(this);
|
2005-06-12 01:30:20 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-03-29 05:24:35 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsXBLBinding::GetBaseTag(int32_t* aNameSpaceID)
|
2000-03-11 13:36:39 +03:00
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *tag = mPrototypeBinding->GetBaseTag(aNameSpaceID);
|
2005-02-26 01:07:01 +03:00
|
|
|
if (!tag && mNextBinding)
|
|
|
|
return mNextBinding->GetBaseTag(aNameSpaceID);
|
|
|
|
|
|
|
|
return tag;
|
2000-03-11 13:36:39 +03:00
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXBLBinding::AttributeChanged(nsAtom* aAttribute, int32_t aNameSpaceID,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aRemoveFlag, bool aNotify)
|
2000-01-25 09:35:27 +03:00
|
|
|
{
|
2000-09-28 00:23:49 +04:00
|
|
|
// XXX Change if we ever allow multiple bindings in a chain to contribute anonymous content
|
|
|
|
if (!mContent) {
|
|
|
|
if (mNextBinding)
|
2005-02-26 01:07:01 +03:00
|
|
|
mNextBinding->AttributeChanged(aAttribute, aNameSpaceID,
|
|
|
|
aRemoveFlag, aNotify);
|
|
|
|
} else {
|
|
|
|
mPrototypeBinding->AttributeChanged(aAttribute, aNameSpaceID, aRemoveFlag,
|
|
|
|
mBoundElement, mContent, aNotify);
|
2000-01-25 09:35:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2000-07-28 04:35:02 +04:00
|
|
|
nsXBLBinding::ExecuteAttachedHandler()
|
|
|
|
{
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->ExecuteAttachedHandler();
|
|
|
|
|
2011-06-12 00:43:33 +04:00
|
|
|
if (AllowScripts())
|
2005-01-27 22:21:14 +03:00
|
|
|
mPrototypeBinding->BindingAttached(mBoundElement);
|
2000-07-28 04:35:02 +04:00
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2000-07-28 04:35:02 +04:00
|
|
|
nsXBLBinding::ExecuteDetachedHandler()
|
|
|
|
{
|
2005-01-27 22:21:14 +03:00
|
|
|
if (AllowScripts())
|
|
|
|
mPrototypeBinding->BindingDetached(mBoundElement);
|
2000-07-28 04:35:02 +04:00
|
|
|
|
|
|
|
if (mNextBinding)
|
|
|
|
mNextBinding->ExecuteDetachedHandler();
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2000-06-22 04:36:19 +04:00
|
|
|
nsXBLBinding::UnhookEventHandlers()
|
|
|
|
{
|
2003-09-11 16:25:06 +04:00
|
|
|
nsXBLPrototypeHandler* handlerChain = mPrototypeBinding->GetPrototypeHandlers();
|
|
|
|
|
|
|
|
if (handlerChain) {
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* manager = mBoundElement->GetExistingListenerManager();
|
2007-11-16 01:29:08 +03:00
|
|
|
if (!manager) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-19 01:25:39 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isChromeBinding = mPrototypeBinding->IsChrome();
|
2003-09-11 16:25:06 +04:00
|
|
|
nsXBLPrototypeHandler* curr;
|
|
|
|
for (curr = handlerChain; curr; curr = curr->GetNextHandler()) {
|
|
|
|
nsXBLEventHandler* handler = curr->GetCachedEventHandler();
|
2007-11-16 01:29:08 +03:00
|
|
|
if (!handler) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-19 01:25:39 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> eventAtom = curr->GetEventName();
|
2007-11-16 01:29:08 +03:00
|
|
|
if (!eventAtom ||
|
|
|
|
eventAtom == nsGkAtoms::keyup ||
|
|
|
|
eventAtom == nsGkAtoms::keydown ||
|
|
|
|
eventAtom == nsGkAtoms::keypress)
|
|
|
|
continue;
|
2003-09-11 16:25:06 +04:00
|
|
|
|
2007-11-16 01:29:08 +03:00
|
|
|
// Figure out if we're using capturing or not.
|
2014-03-17 10:56:52 +04:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mCapture = (curr->GetPhase() == NS_PHASE_CAPTURING);
|
2003-09-11 16:25:06 +04:00
|
|
|
|
2007-11-16 01:29:08 +03:00
|
|
|
// If this is a command, remove it from the system event group,
|
|
|
|
// otherwise remove it from the standard event group.
|
2003-09-11 16:25:06 +04:00
|
|
|
|
2011-06-24 06:18:02 +04:00
|
|
|
if ((curr->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND |
|
|
|
|
NS_HANDLER_TYPE_SYSTEM)) &&
|
2009-06-23 14:07:39 +04:00
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mInSystemGroup = true;
|
2003-09-11 16:25:06 +04:00
|
|
|
}
|
2007-11-16 01:29:08 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
manager->RemoveEventListenerByType(handler,
|
|
|
|
nsDependentAtomString(eventAtom),
|
2011-06-24 06:18:02 +04:00
|
|
|
flags);
|
2003-09-11 16:25:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsCOMArray<nsXBLKeyEventHandler>* keyHandlers =
|
|
|
|
mPrototypeBinding->GetKeyEventHandlers();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2003-09-11 16:25:06 +04: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 10:56:52 +04:00
|
|
|
EventListenerFlags flags;
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mCapture = (handler->GetPhase() == NS_PHASE_CAPTURING);
|
2003-09-11 16:25:06 +04:00
|
|
|
|
2014-03-19 01:25:39 +04:00
|
|
|
// If this is a command, remove it from the system event group, otherwise
|
2003-09-11 16:25:06 +04:00
|
|
|
// remove it from the standard event group.
|
|
|
|
|
2009-06-23 14:07:39 +04:00
|
|
|
if ((handler->GetType() & (NS_HANDLER_TYPE_XBL_COMMAND | NS_HANDLER_TYPE_SYSTEM)) &&
|
|
|
|
(isChromeBinding || mBoundElement->IsInNativeAnonymousSubtree())) {
|
2012-12-16 05:26:05 +04:00
|
|
|
flags.mInSystemGroup = true;
|
2003-09-11 16:25:06 +04:00
|
|
|
}
|
|
|
|
|
2011-06-24 06:18:02 +04:00
|
|
|
manager->RemoveEventListenerByType(handler, type, flags);
|
2003-09-11 16:25:06 +04:00
|
|
|
}
|
2000-06-22 04:36:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2000-05-04 08:25:50 +04:00
|
|
|
nsXBLBinding::ChangeDocument(nsIDocument* aOldDocument, nsIDocument* aNewDocument)
|
2000-03-31 14:27:12 +04:00
|
|
|
{
|
2013-04-05 17:21:01 +04:00
|
|
|
if (aOldDocument == aNewDocument)
|
|
|
|
return;
|
|
|
|
|
2013-07-11 19:58:28 +04:00
|
|
|
// Now the binding dies. Unhook our prototypes.
|
|
|
|
if (mPrototypeBinding->HasImplementation()) {
|
2014-07-15 19:39:17 +04: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.
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(cx, scriptObject);
|
2014-07-15 19:39:17 +04:00
|
|
|
|
|
|
|
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 19:58:28 +04:00
|
|
|
break;
|
2000-05-27 12:20:04 +04:00
|
|
|
}
|
2013-07-11 19:58:28 +04:00
|
|
|
|
2014-07-15 19:39:17 +04:00
|
|
|
if (JS_GetClass(proto) != &gPrototypeJSClass) {
|
|
|
|
// Clearly not the right class
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXBLDocumentInfo> docInfo =
|
2014-07-15 19:39:17 +04:00
|
|
|
static_cast<nsXBLDocumentInfo*>(::JS_GetPrivate(proto));
|
|
|
|
if (!docInfo) {
|
|
|
|
// Not the proto we seek
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value protoBinding = ::JS_GetReservedSlot(proto, 0);
|
2013-07-11 19:58:28 +04:00
|
|
|
|
2014-07-15 19:39:17 +04: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 08:25:50 +04:00
|
|
|
}
|
2014-07-15 19:39:17 +04: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 08:25:50 +04:00
|
|
|
}
|
|
|
|
}
|
2013-04-05 17:21:01 +04:00
|
|
|
}
|
|
|
|
|
2013-07-11 19:58:28 +04:00
|
|
|
// Remove our event handlers
|
|
|
|
UnhookEventHandlers();
|
|
|
|
|
2013-04-05 17:21:01 +04:00
|
|
|
{
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2007-10-20 08:22:43 +04:00
|
|
|
|
2013-04-05 17:21:01 +04: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 04:46:13 +03:00
|
|
|
|
2013-04-05 17:21:01 +04:00
|
|
|
// Update the anonymous content.
|
|
|
|
// XXXbz why not only for style bindings?
|
2014-09-27 02:07:40 +04:00
|
|
|
if (mContent && !mIsShadowRootBinding) {
|
2017-09-11 14:11:01 +03:00
|
|
|
nsXBLBinding::UnbindAnonymousContent(aOldDocument, mContent);
|
2013-04-05 17:21:01 +04:00
|
|
|
}
|
2009-11-18 18:14:14 +03:00
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
ClearInsertionPoints();
|
2000-03-31 14:27:12 +04:00
|
|
|
}
|
2000-08-12 10:28:02 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-02-26 01:07:01 +03:00
|
|
|
nsXBLBinding::InheritsStyle() const
|
2000-08-12 10:28:02 +04:00
|
|
|
{
|
2000-09-28 00:23:49 +04: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-26 01:07:01 +03:00
|
|
|
if (mContent)
|
|
|
|
return mPrototypeBinding->InheritsStyle();
|
2014-03-19 01:25:39 +04:00
|
|
|
|
2000-09-28 00:23:49 +04:00
|
|
|
if (mNextBinding)
|
2005-02-26 01:07:01 +03:00
|
|
|
return mNextBinding->InheritsStyle();
|
2000-08-24 13:21:27 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-08-12 10:28:02 +04:00
|
|
|
}
|
|
|
|
|
2000-04-27 06:08:35 +04:00
|
|
|
|
2018-02-12 15:57:26 +03:00
|
|
|
const RawServoAuthorStyles*
|
|
|
|
nsXBLBinding::GetServoStyles() const
|
2017-06-05 09:00:27 +03:00
|
|
|
{
|
2018-02-12 15:57:26 +03:00
|
|
|
return mPrototypeBinding->GetServoStyles();
|
2017-06-05 09:00:27 +03:00
|
|
|
}
|
|
|
|
|
2000-05-27 12:20:04 +04:00
|
|
|
// Internal helper methods ////////////////////////////////////////////////////////////////
|
2000-03-31 07:13:43 +04:00
|
|
|
|
2014-04-11 18:59:47 +04: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.
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::Rooted<JS::PropertyDescriptor> desc(cx);
|
2014-04-26 01:11:02 +04:00
|
|
|
if (!JS_GetOwnPropertyDescriptor(cx, scope, mapName, &desc)) {
|
2014-04-11 18:59:47 +04: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 06:11:17 +04:00
|
|
|
if (!map || !JS_DefineProperty(cx, scope, mapName, map,
|
2017-08-28 11:39:57 +03:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY))
|
2014-04-11 18:59:47 +04: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 05:04:21 +04:00
|
|
|
const char* name = xpc::IsInContentXBLScope(proto) ? "__ContentClassObjectMap__"
|
|
|
|
: "__XBLClassObjectMap__";
|
2014-04-11 18:59:47 +04:00
|
|
|
|
|
|
|
// Now, enter the XBL scope, since that's where we need to operate, and wrap
|
2014-06-24 03:33:37 +04: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 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> scope(cx, xpc::GetXBLScopeOrGlobal(cx, proto));
|
2014-08-05 20:10:34 +04:00
|
|
|
NS_ENSURE_TRUE(scope, nullptr);
|
2015-02-26 17:40:07 +03:00
|
|
|
MOZ_ASSERT(js::GetGlobalForObjectCrossCompartment(scope) == scope);
|
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> wrappedProto(cx, proto);
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(cx, scope);
|
2014-04-11 18:59:47 +04:00
|
|
|
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);
|
2015-05-14 00:07:34 +03:00
|
|
|
entry = JS_NewObjectWithGivenProto(cx, nullptr, nullptr);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!entry) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> entryVal(cx, JS::ObjectValue(*entry));
|
|
|
|
if (!JS::SetWeakMapEntry(cx, map, wrappedProto, entryVal)) {
|
2014-04-15 01:17:53 +04:00
|
|
|
NS_WARNING("SetWeakMapEntry failed, probably due to non-preservable WeakMap "
|
|
|
|
"key. XBL binding will fail for this element.");
|
2014-04-11 18:59:47 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:54:12 +03:00
|
|
|
static
|
|
|
|
nsXBLPrototypeBinding*
|
|
|
|
GetProtoBindingFromClassObject(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(JS_GetClass(obj) == &gPrototypeJSClass);
|
|
|
|
return static_cast<nsXBLPrototypeBinding*>(::JS_GetReservedSlot(obj, 0).toPrivate());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-22 00:55:17 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
2014-03-19 20:35:45 +04:00
|
|
|
nsXBLBinding::DoInitJSClass(JSContext *cx,
|
2013-04-05 17:21:01 +04:00
|
|
|
JS::Handle<JSObject*> obj,
|
2017-06-20 12:19:05 +03:00
|
|
|
const nsString& aClassName,
|
2007-09-28 17:45:01 +04:00
|
|
|
nsXBLPrototypeBinding* aProtoBinding,
|
2013-04-05 17:21:01 +04:00
|
|
|
JS::MutableHandle<JSObject*> aClassObject,
|
|
|
|
bool* aNew)
|
2000-03-31 07:13:43 +04:00
|
|
|
{
|
2014-04-11 18:59:46 +04:00
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
|
2014-03-19 20:35:45 +04: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 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2014-06-24 03:33:37 +04:00
|
|
|
|
|
|
|
// We never store class objects in add-on scopes.
|
2014-04-11 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> xblScope(cx, xpc::GetXBLScopeOrGlobal(cx, global));
|
2014-08-05 20:10:34 +04:00
|
|
|
NS_ENSURE_TRUE(xblScope, NS_ERROR_UNEXPECTED);
|
2014-04-11 18:59:46 +04:00
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> parent_proto(cx);
|
2018-05-16 02:01:36 +03:00
|
|
|
{
|
|
|
|
JS::RootedObject wrapped(cx, obj);
|
2018-05-26 22:03:31 +03:00
|
|
|
JSAutoRealm ar(cx, xblScope);
|
2018-05-16 02:01:36 +03:00
|
|
|
if (!JS_WrapObject(cx, &wrapped)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!JS_GetPrototype(cx, wrapped, &parent_proto)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!JS_WrapObject(cx, &parent_proto)) {
|
2014-04-11 18:59:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-12-12 19:10:02 +04:00
|
|
|
|
2014-04-11 18:59:47 +04: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 {
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm innerAR(cx, xblScope);
|
2014-04-11 18:59:47 +04:00
|
|
|
holder = GetOrCreateClassObjectMap(cx, xblScope, "__ContentClassObjectMap__");
|
2004-01-06 02:19:58 +03:00
|
|
|
}
|
2014-04-15 01:17:53 +04:00
|
|
|
if (NS_WARN_IF(!holder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-04-11 18:59:47 +04:00
|
|
|
js::AssertSameCompartment(holder, xblScope);
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(cx, holder);
|
2004-01-06 02:19:58 +03:00
|
|
|
|
2014-04-11 18:59:47 +04: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 17:21:01 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::Rooted<JS::PropertyDescriptor> desc(cx);
|
2015-03-13 22:05:57 +03:00
|
|
|
if (!JS_GetOwnUCPropertyDescriptor(cx, holder, aClassName.get(), &desc)) {
|
2013-04-05 17:21:01 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-04-11 18:59:47 +04:00
|
|
|
}
|
|
|
|
*aNew = !desc.object();
|
|
|
|
if (desc.object()) {
|
|
|
|
proto = &desc.value().toObject();
|
2016-04-29 21:54:12 +03:00
|
|
|
DebugOnly<nsXBLPrototypeBinding*> cachedBinding =
|
|
|
|
GetProtoBindingFromClassObject(js::UncheckedUnwrap(proto));
|
|
|
|
MOZ_ASSERT(cachedBinding == aProtoBinding);
|
2013-04-05 17:21:01 +04:00
|
|
|
} else {
|
2000-07-01 06:36:18 +04:00
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// We need to create the prototype. First, enter the realm where it's
|
2014-04-11 18:59:47 +04:00
|
|
|
// going to live, and create it.
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar2(cx, global);
|
2015-02-26 17:40:07 +03:00
|
|
|
proto = JS_NewObjectWithGivenProto(cx, &gPrototypeJSClass, parent_proto);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!proto) {
|
2000-07-01 06:36:18 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2000-03-31 07:13:43 +04:00
|
|
|
}
|
|
|
|
|
2007-09-28 17:45:01 +04: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 23:40:11 +04: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 05:53:11 +04:00
|
|
|
nsXBLDocumentInfo* docInfo = aProtoBinding->XBLDocumentInfo();
|
2012-02-06 00:07:23 +04:00
|
|
|
::JS_SetPrivate(proto, docInfo);
|
2007-09-28 17:45:01 +04:00
|
|
|
NS_ADDREF(docInfo);
|
2015-06-30 21:20:58 +03:00
|
|
|
JS_SetReservedSlot(proto, 0, JS::PrivateValue(aProtoBinding));
|
2014-04-11 18:59:47 +04:00
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// Next, enter the realm of the property holder, wrap the proto, and
|
2014-04-11 18:59:47 +04:00
|
|
|
// stick it on.
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar3(cx, holder);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!JS_WrapObject(cx, &proto) ||
|
2015-03-13 22:05:57 +03:00
|
|
|
!JS_DefineUCProperty(cx, holder, aClassName.get(), -1, proto,
|
2017-08-28 11:39:57 +03:00
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT))
|
2014-04-11 18:59:47 +04:00
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-09-14 17:33:54 +04:00
|
|
|
}
|
2002-06-22 00:55:17 +04:00
|
|
|
}
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// Whew. We have the proto. Wrap it back into the realm of |obj|,
|
2014-04-11 18:59:47 +04:00
|
|
|
// splice it in, and return it.
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar4(cx, obj);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!JS_WrapObject(cx, &proto) || !JS_SetPrototype(cx, obj, proto)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
aClassObject.set(proto);
|
2002-06-22 00:55:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2000-05-19 08:48:43 +04:00
|
|
|
nsXBLBinding::AllowScripts()
|
|
|
|
{
|
2013-12-17 20:46:33 +04:00
|
|
|
return mBoundElement && mPrototypeBinding->GetAllowScripts();
|
2000-05-19 08:48:43 +04:00
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
nsXBLBinding*
|
|
|
|
nsXBLBinding::RootBinding()
|
2000-06-02 12:13:29 +04:00
|
|
|
{
|
|
|
|
if (mNextBinding)
|
2005-02-26 01:07:01 +03:00
|
|
|
return mNextBinding->RootBinding();
|
2000-06-02 12:13:29 +04:00
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
return this;
|
2000-06-02 12:13:29 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-04-05 17:21:03 +04:00
|
|
|
nsXBLBinding::ResolveAllFields(JSContext *cx, JS::Handle<JSObject*> obj) const
|
2007-09-28 17:45:01 +04:00
|
|
|
{
|
|
|
|
if (!mPrototypeBinding->ResolveAllFields(cx, obj)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-09-28 17:45:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mNextBinding) {
|
|
|
|
return mNextBinding->ResolveAllFields(cx, obj);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-09-28 17:45:01 +04:00
|
|
|
}
|
|
|
|
|
2013-02-08 18:24:21 +04:00
|
|
|
bool
|
2013-11-11 12:04:41 +04:00
|
|
|
nsXBLBinding::LookupMember(JSContext* aCx, JS::Handle<jsid> aId,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> aDesc)
|
2013-02-08 18:24:21 +04:00
|
|
|
{
|
|
|
|
// We should never enter this function with a pre-filled property descriptor.
|
2013-08-12 15:09:14 +04:00
|
|
|
MOZ_ASSERT(!aDesc.object());
|
2013-02-08 18:24:21 +04: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 19:30:54 +04:00
|
|
|
nsAutoJSString name;
|
|
|
|
if (!name.init(aCx, JSID_TO_STRING(aId))) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-08 18:24:21 +04: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 22:16:19 +04: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 20:35:45 +04: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-24 03:33:37 +04:00
|
|
|
//
|
|
|
|
// This code is only called for content XBL, so we don't have to worry about
|
|
|
|
// add-on scopes here.
|
2013-04-05 17:21:01 +04:00
|
|
|
JS::Rooted<JSObject*> boundScope(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(mBoundElement->GetWrapper()));
|
2014-06-03 05:04:21 +04:00
|
|
|
MOZ_RELEASE_ASSERT(!xpc::IsInContentXBLScope(boundScope));
|
2013-04-05 17:21:01 +04:00
|
|
|
JS::Rooted<JSObject*> xblScope(aCx, xpc::GetXBLScope(aCx, boundScope));
|
2013-04-05 23:04:09 +04:00
|
|
|
NS_ENSURE_TRUE(xblScope, false);
|
2013-02-13 22:16:19 +04:00
|
|
|
MOZ_ASSERT(boundScope != xblScope);
|
2013-02-08 18:24:21 +04:00
|
|
|
|
2013-02-13 22:16:19 +04:00
|
|
|
// Enter the xbl scope and invoke the internal version.
|
2013-02-08 18:24:21 +04:00
|
|
|
{
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(aCx, xblScope);
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<jsid> id(aCx, aId);
|
2014-06-23 19:57:02 +04:00
|
|
|
if (!LookupMemberInternal(aCx, name, id, aDesc, xblScope)) {
|
2013-02-08 18:24:21 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap into the caller's scope.
|
|
|
|
return JS_WrapPropertyDescriptor(aCx, aDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsXBLBinding::LookupMemberInternal(JSContext* aCx, nsString& aName,
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Handle<jsid> aNameAsId,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> aDesc,
|
2013-04-05 17:21:01 +04:00
|
|
|
JS::Handle<JSObject*> aXBLScope)
|
2013-02-08 18:24:21 +04:00
|
|
|
{
|
2014-04-11 18:59:47 +04: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 18:24:21 +04:00
|
|
|
if (!mNextBinding) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return mNextBinding->LookupMemberInternal(aCx, aName, aNameAsId,
|
2013-02-13 22:16:19 +04:00
|
|
|
aDesc, aXBLScope);
|
2013-02-08 18:24:21 +04:00
|
|
|
}
|
|
|
|
|
2013-02-13 22:16:19 +04: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 12:04:41 +04:00
|
|
|
JS::Rooted<JS::Value> classObject(aCx);
|
2015-03-13 22:05:57 +03:00
|
|
|
if (!JS_GetUCProperty(aCx, aXBLScope, PrototypeBinding()->ClassName().get(),
|
|
|
|
-1, &classObject)) {
|
2013-02-08 18:24:21 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 20:05:03 +04: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 18:24:21 +04: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 20:17:58 +04:00
|
|
|
JS::Rooted<JSObject*> object(aCx, &classObject.toObject());
|
|
|
|
if (impl && !impl->LookupMember(aCx, aName, aNameAsId, aDesc, object)) {
|
2013-02-08 18:24:21 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
if (aDesc.object() || !mNextBinding) {
|
2013-02-08 18:24:21 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNextBinding->LookupMemberInternal(aCx, aName, aNameAsId, aDesc,
|
2013-02-13 22:16:19 +04:00
|
|
|
aXBLScope);
|
2013-02-08 18:24:21 +04:00
|
|
|
}
|
|
|
|
|
2013-02-08 18:24:22 +04:00
|
|
|
bool
|
|
|
|
nsXBLBinding::HasField(nsString& aName)
|
|
|
|
{
|
|
|
|
// See if this binding has such a field.
|
|
|
|
return mPrototypeBinding->FindField(aName) ||
|
|
|
|
(mNextBinding && mNextBinding->HasField(aName));
|
|
|
|
}
|
|
|
|
|
2005-02-26 01:07:01 +03:00
|
|
|
void
|
2000-08-14 08:04:18 +04:00
|
|
|
nsXBLBinding::MarkForDeath()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mMarkedForDeath = true;
|
2000-09-28 00:23:49 +04:00
|
|
|
ExecuteDetachedHandler();
|
2000-08-14 08:04:18 +04:00
|
|
|
}
|
2000-06-02 12:13:29 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-02-26 01:07:01 +03:00
|
|
|
nsXBLBinding::ImplementsInterface(REFNSIID aIID) const
|
2000-12-07 13:11:21 +03:00
|
|
|
{
|
2005-02-26 01:07:01 +03:00
|
|
|
return mPrototypeBinding->ImplementsInterface(aIID) ||
|
|
|
|
(mNextBinding && mNextBinding->ImplementsInterface(aIID));
|
2000-12-07 13:11:21 +03:00
|
|
|
}
|