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: */
|
2013-12-02 14:26:11 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
|
|
|
#include "mozilla/dom/ShadowRootBinding.h"
|
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2013-12-02 14:26:12 +04:00
|
|
|
#include "ChildIterator.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfoID.h"
|
|
|
|
#include "nsIDOMHTMLElement.h"
|
2013-12-02 14:26:12 +04:00
|
|
|
#include "nsIStyleSheetLinkingElement.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2017-12-04 11:06:34 +03:00
|
|
|
#include "mozilla/dom/HTMLSlotElement.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2017-12-04 11:06:40 +03:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2016-09-26 15:03:25 +03:00
|
|
|
#include "mozilla/StyleSheet.h"
|
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ShadowRoot)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ShadowRoot,
|
|
|
|
DocumentFragment)
|
2017-12-18 00:29:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMStyleSheets)
|
2013-12-02 14:26:12 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAssociatedBinding)
|
2015-07-22 19:42:01 +03:00
|
|
|
for (auto iter = tmp->mIdentifierMap.ConstIter(); !iter.Done();
|
|
|
|
iter.Next()) {
|
|
|
|
iter.Get()->Traverse(&cb);
|
|
|
|
}
|
2013-12-02 14:26:11 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2017-09-25 18:09:26 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ShadowRoot)
|
|
|
|
if (tmp->GetHost()) {
|
|
|
|
tmp->GetHost()->RemoveMutationObserver(tmp);
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
2017-12-18 00:29:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDOMStyleSheets)
|
2013-12-02 14:26:12 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAssociatedBinding)
|
2013-12-02 14:26:11 +04:00
|
|
|
tmp->mIdentifierMap.Clear();
|
2017-09-25 18:09:26 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(DocumentFragment)
|
2013-12-02 14:26:11 +04:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ShadowRoot)
|
2013-12-02 14:26:11 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent)
|
2013-12-02 14:26:12 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2013-12-02 14:26:11 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DocumentFragment)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ShadowRoot, DocumentFragment)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ShadowRoot, DocumentFragment)
|
|
|
|
|
2017-10-19 09:44:35 +03:00
|
|
|
ShadowRoot::ShadowRoot(Element* aElement, bool aClosed,
|
2014-06-20 06:01:40 +04:00
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
|
2013-12-02 14:26:12 +04:00
|
|
|
nsXBLPrototypeBinding* aProtoBinding)
|
2017-09-25 18:09:26 +03:00
|
|
|
: DocumentFragment(aNodeInfo)
|
2017-12-21 01:29:21 +03:00
|
|
|
, DocumentOrShadowRoot(*this)
|
2017-09-25 18:09:26 +03:00
|
|
|
, mProtoBinding(aProtoBinding)
|
|
|
|
, mInsertionPointChanged(false)
|
|
|
|
, mIsComposedDocParticipant(false)
|
2013-12-02 14:26:11 +04:00
|
|
|
{
|
2017-09-06 11:37:17 +03:00
|
|
|
SetHost(aElement);
|
2017-10-19 09:44:35 +03:00
|
|
|
mMode = aClosed ? ShadowRootMode::Closed : ShadowRootMode::Open;
|
2014-06-07 12:42:53 +04:00
|
|
|
|
|
|
|
// Nodes in a shadow tree should never store a value
|
|
|
|
// in the subtree root pointer, nodes in the shadow tree
|
|
|
|
// track the subtree root using GetContainingShadow().
|
|
|
|
ClearSubtreeRootPointer();
|
|
|
|
|
2013-12-02 14:26:11 +04:00
|
|
|
SetFlags(NODE_IS_IN_SHADOW_TREE);
|
2014-06-07 12:42:53 +04:00
|
|
|
|
2017-09-06 11:37:17 +03:00
|
|
|
ExtendedDOMSlots()->mBindingParent = aElement;
|
2017-07-18 01:25:49 +03:00
|
|
|
ExtendedDOMSlots()->mContainingShadow = this;
|
2013-12-02 14:26:12 +04:00
|
|
|
|
|
|
|
// Add the ShadowRoot as a mutation observer on the host to watch
|
|
|
|
// for mutations because the insertion points in this ShadowRoot
|
|
|
|
// may need to be updated when the host children are modified.
|
2017-09-25 18:09:26 +03:00
|
|
|
GetHost()->AddMutationObserver(this);
|
2013-12-02 14:26:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ShadowRoot::~ShadowRoot()
|
|
|
|
{
|
2017-10-17 19:22:29 +03:00
|
|
|
if (auto* host = GetHost()) {
|
|
|
|
// mHost may have been unlinked or a new ShadowRoot may have been
|
|
|
|
// created, making this one obsolete.
|
|
|
|
host->RemoveMutationObserver(this);
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
2014-06-07 12:42:53 +04:00
|
|
|
UnsetFlags(NODE_IS_IN_SHADOW_TREE);
|
|
|
|
|
|
|
|
// nsINode destructor expects mSubtreeRoot == this.
|
|
|
|
SetSubtreeRootPointer(this);
|
2013-12-02 14:26:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
ShadowRoot::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-12-02 14:26:11 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return mozilla::dom::ShadowRootBinding::Wrap(aCx, this, aGivenProto);
|
2013-12-02 14:26:11 +04:00
|
|
|
}
|
|
|
|
|
2017-12-04 11:06:34 +03:00
|
|
|
void
|
|
|
|
ShadowRoot::AddSlot(HTMLSlotElement* aSlot)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSlot);
|
|
|
|
|
|
|
|
// Note that if name attribute missing, the slot is a default slot.
|
|
|
|
nsAutoString name;
|
|
|
|
aSlot->GetName(name);
|
|
|
|
|
|
|
|
nsTArray<HTMLSlotElement*>* currentSlots = mSlotMap.LookupOrAdd(name);
|
|
|
|
MOZ_ASSERT(currentSlots);
|
|
|
|
|
|
|
|
HTMLSlotElement* oldSlot = currentSlots->IsEmpty() ?
|
|
|
|
nullptr : currentSlots->ElementAt(0);
|
|
|
|
|
|
|
|
TreeOrderComparator comparator;
|
|
|
|
currentSlots->InsertElementSorted(aSlot, comparator);
|
|
|
|
|
|
|
|
HTMLSlotElement* currentSlot = currentSlots->ElementAt(0);
|
|
|
|
if (currentSlot != aSlot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:16:33 +03:00
|
|
|
bool doEnqueueSlotChange = false;
|
2017-12-04 11:06:34 +03:00
|
|
|
if (oldSlot && oldSlot != currentSlot) {
|
|
|
|
// Move assigned nodes from old slot to new slot.
|
2017-12-04 11:06:37 +03:00
|
|
|
const nsTArray<RefPtr<nsINode>>& assignedNodes = oldSlot->AssignedNodes();
|
2017-12-04 11:06:34 +03:00
|
|
|
while (assignedNodes.Length() > 0) {
|
|
|
|
nsINode* assignedNode = assignedNodes[0];
|
|
|
|
|
|
|
|
oldSlot->RemoveAssignedNode(assignedNode);
|
|
|
|
currentSlot->AppendAssignedNode(assignedNode);
|
2017-12-19 18:16:33 +03:00
|
|
|
doEnqueueSlotChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doEnqueueSlotChange) {
|
|
|
|
oldSlot->EnqueueSlotChangeEvent();
|
|
|
|
currentSlot->EnqueueSlotChangeEvent();
|
2017-12-04 11:06:34 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise add appropriate nodes to this slot from the host.
|
|
|
|
for (nsIContent* child = GetHost()->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
nsAutoString slotName;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (child->IsElement()) {
|
|
|
|
child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::slot, slotName);
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
if (child->IsSlotable() && slotName.Equals(name)) {
|
|
|
|
currentSlot->AppendAssignedNode(child);
|
2017-12-19 18:16:33 +03:00
|
|
|
doEnqueueSlotChange = true;
|
2017-12-04 11:06:34 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 18:16:33 +03:00
|
|
|
|
|
|
|
if (doEnqueueSlotChange) {
|
|
|
|
currentSlot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::RemoveSlot(HTMLSlotElement* aSlot)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSlot);
|
|
|
|
|
|
|
|
nsAutoString name;
|
|
|
|
aSlot->GetName(name);
|
|
|
|
|
|
|
|
nsTArray<HTMLSlotElement*>* currentSlots = mSlotMap.Get(name);
|
|
|
|
|
|
|
|
if (currentSlots) {
|
|
|
|
if (currentSlots->Length() == 1) {
|
|
|
|
MOZ_ASSERT(currentSlots->ElementAt(0) == aSlot);
|
|
|
|
mSlotMap.Remove(name);
|
2017-12-19 18:16:33 +03:00
|
|
|
|
|
|
|
if (aSlot->AssignedNodes().Length() > 0) {
|
|
|
|
aSlot->ClearAssignedNodes();
|
|
|
|
aSlot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
} else {
|
2017-12-19 18:16:33 +03:00
|
|
|
bool doEnqueueSlotChange = false;
|
2017-12-04 11:06:34 +03:00
|
|
|
bool doReplaceSlot = currentSlots->ElementAt(0) == aSlot;
|
|
|
|
currentSlots->RemoveElement(aSlot);
|
|
|
|
HTMLSlotElement* replacementSlot = currentSlots->ElementAt(0);
|
|
|
|
|
|
|
|
// Move assigned nodes from removed slot to the next slot in
|
|
|
|
// tree order with the same name.
|
|
|
|
if (doReplaceSlot) {
|
2017-12-04 11:06:37 +03:00
|
|
|
const nsTArray<RefPtr<nsINode>>& assignedNodes = aSlot->AssignedNodes();
|
2017-12-04 11:06:34 +03:00
|
|
|
while (assignedNodes.Length() > 0) {
|
|
|
|
nsINode* assignedNode = assignedNodes[0];
|
|
|
|
|
|
|
|
aSlot->RemoveAssignedNode(assignedNode);
|
|
|
|
replacementSlot->AppendAssignedNode(assignedNode);
|
2017-12-19 18:16:33 +03:00
|
|
|
doEnqueueSlotChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doEnqueueSlotChange) {
|
|
|
|
aSlot->EnqueueSlotChangeEvent();
|
|
|
|
replacementSlot->EnqueueSlotChangeEvent();
|
2017-12-04 11:06:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
void
|
2014-06-07 12:42:54 +04:00
|
|
|
ShadowRoot::StyleSheetChanged()
|
2013-12-02 14:26:12 +04:00
|
|
|
{
|
|
|
|
mProtoBinding->FlushSkinSheets();
|
|
|
|
|
2017-10-17 19:22:29 +03:00
|
|
|
if (nsIPresShell* shell = OwnerDoc()->GetShell()) {
|
2013-12-02 14:26:12 +04:00
|
|
|
OwnerDoc()->BeginUpdate(UPDATE_STYLE);
|
2014-06-07 12:42:54 +04:00
|
|
|
shell->RecordShadowStyleChange(this);
|
2013-12-02 14:26:12 +04:00
|
|
|
OwnerDoc()->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-26 15:03:25 +03:00
|
|
|
ShadowRoot::InsertSheet(StyleSheet* aSheet,
|
2013-12-02 14:26:12 +04:00
|
|
|
nsIContent* aLinkingContent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement>
|
|
|
|
linkingElement = do_QueryInterface(aLinkingContent);
|
2017-12-18 00:29:10 +03:00
|
|
|
|
|
|
|
// FIXME(emilio, bug 1410578): <link> should probably also be allowed here.
|
2013-12-02 14:26:12 +04:00
|
|
|
MOZ_ASSERT(linkingElement, "The only styles in a ShadowRoot should come "
|
|
|
|
"from <style>.");
|
|
|
|
|
|
|
|
linkingElement->SetStyleSheet(aSheet); // This sets the ownerNode on the sheet
|
|
|
|
|
2017-12-21 01:29:21 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mProtoBinding->SheetCount() == DocumentOrShadowRoot::SheetCount());
|
2017-12-18 00:29:10 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
// FIXME(emilio, bug 1425759): For now we keep them duplicated, the proto
|
|
|
|
// binding will disappear soon (tm).
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
for (RefPtr<StyleSheet>& sheet : mStyleSheets) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sheet.get() == mProtoBinding->StyleSheetAt(i++));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
// Find the correct position to insert into the style sheet list (must
|
|
|
|
// be in tree order).
|
2017-12-18 00:29:10 +03:00
|
|
|
for (size_t i = 0; i <= SheetCount(); i++) {
|
|
|
|
if (i == SheetCount()) {
|
|
|
|
AppendStyleSheet(*aSheet);
|
2014-06-19 11:02:56 +04:00
|
|
|
mProtoBinding->AppendStyleSheet(aSheet);
|
2013-12-02 14:26:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-18 00:29:10 +03:00
|
|
|
nsINode* sheetOwningNode = SheetAt(i)->GetOwnerNode();
|
2016-02-24 10:01:12 +03:00
|
|
|
if (nsContentUtils::PositionIsBefore(aLinkingContent, sheetOwningNode)) {
|
2017-12-18 00:29:10 +03:00
|
|
|
InsertSheetAt(i, *aSheet);
|
2014-06-19 11:02:56 +04:00
|
|
|
mProtoBinding->InsertStyleSheetAt(i, aSheet);
|
2013-12-02 14:26:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:42:54 +04:00
|
|
|
if (aSheet->IsApplicable()) {
|
|
|
|
StyleSheetChanged();
|
|
|
|
}
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-26 15:03:25 +03:00
|
|
|
ShadowRoot::RemoveSheet(StyleSheet* aSheet)
|
2013-12-02 14:26:12 +04:00
|
|
|
{
|
2014-06-19 11:02:56 +04:00
|
|
|
mProtoBinding->RemoveStyleSheet(aSheet);
|
2017-12-21 01:29:21 +03:00
|
|
|
DocumentOrShadowRoot::RemoveSheet(*aSheet);
|
2013-12-02 14:26:12 +04:00
|
|
|
|
2014-06-07 12:42:54 +04:00
|
|
|
if (aSheet->IsApplicable()) {
|
|
|
|
StyleSheetChanged();
|
|
|
|
}
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:11 +04:00
|
|
|
void
|
2017-10-03 01:05:19 +03:00
|
|
|
ShadowRoot::AddToIdTable(Element* aElement, nsAtom* aId)
|
2013-12-02 14:26:11 +04:00
|
|
|
{
|
2017-04-13 22:12:20 +03:00
|
|
|
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aId);
|
2013-12-02 14:26:11 +04:00
|
|
|
if (entry) {
|
|
|
|
entry->AddIdElement(aElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-03 01:05:19 +03:00
|
|
|
ShadowRoot::RemoveFromIdTable(Element* aElement, nsAtom* aId)
|
2013-12-02 14:26:11 +04:00
|
|
|
{
|
2017-04-13 22:12:20 +03:00
|
|
|
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aId);
|
2013-12-02 14:26:11 +04:00
|
|
|
if (entry) {
|
|
|
|
entry->RemoveIdElement(aElement);
|
|
|
|
if (entry->IsEmpty()) {
|
2016-02-09 05:52:21 +03:00
|
|
|
mIdentifierMap.RemoveEntry(entry);
|
2013-12-02 14:26:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:06:40 +03:00
|
|
|
nsresult
|
|
|
|
ShadowRoot::GetEventTargetParent(EventChainPreVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
aVisitor.mCanHandle = true;
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.mRootOfClosedTree = IsClosed();
|
2017-12-04 11:06:40 +03:00
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#ref-for-get-the-parent%E2%91%A6
|
|
|
|
if (!aVisitor.mEvent->mFlags.mComposed) {
|
|
|
|
nsCOMPtr<nsIContent> originalTarget =
|
|
|
|
do_QueryInterface(aVisitor.mEvent->mOriginalTarget);
|
|
|
|
if (originalTarget->GetContainingShadow() == this) {
|
|
|
|
// If we do stop propagation, we still want to propagate
|
|
|
|
// the event to chrome (nsPIDOMWindow::GetParentTarget()).
|
|
|
|
// The load event is special in that we don't ever propagate it
|
|
|
|
// to chrome.
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> win = OwnerDoc()->GetWindow();
|
|
|
|
EventTarget* parentTarget = win && aVisitor.mEvent->mMessage != eLoad
|
|
|
|
? win->GetParentTarget() : nullptr;
|
|
|
|
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.SetParentTarget(parentTarget, true);
|
2017-12-04 11:06:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent* shadowHost = GetHost();
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.SetParentTarget(shadowHost, false);
|
2017-12-04 11:06:40 +03:00
|
|
|
|
|
|
|
if (aVisitor.mOriginalTargetIsInAnon) {
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(aVisitor.mEvent->mTarget));
|
|
|
|
if (content && content->GetBindingParent() == shadowHost) {
|
|
|
|
aVisitor.mEventTargetAtParent = shadowHost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:06:34 +03:00
|
|
|
const HTMLSlotElement*
|
|
|
|
ShadowRoot::AssignSlotFor(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
nsAutoString slotName;
|
|
|
|
// Note that if slot attribute is missing, assign it to the first default
|
|
|
|
// slot, if exists.
|
2017-12-07 21:13:50 +03:00
|
|
|
if (aContent->IsElement()) {
|
|
|
|
aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::slot, slotName);
|
|
|
|
}
|
|
|
|
|
2017-12-04 11:06:34 +03:00
|
|
|
nsTArray<HTMLSlotElement*>* slots = mSlotMap.Get(slotName);
|
|
|
|
if (!slots) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLSlotElement* slot = slots->ElementAt(0);
|
|
|
|
MOZ_ASSERT(slot);
|
|
|
|
|
|
|
|
// Find the appropriate position in the assigned node list for the
|
|
|
|
// newly assigned content.
|
2017-12-04 11:06:37 +03:00
|
|
|
const nsTArray<RefPtr<nsINode>>& assignedNodes = slot->AssignedNodes();
|
2017-12-04 11:06:34 +03:00
|
|
|
nsIContent* currentContent = GetHost()->GetFirstChild();
|
2018-01-04 18:47:29 +03:00
|
|
|
Maybe<uint32_t> insertionIndex;
|
2017-12-04 11:06:34 +03:00
|
|
|
for (uint32_t i = 0; i < assignedNodes.Length(); i++) {
|
|
|
|
// Seek through the host's explicit children until the
|
|
|
|
// assigned content is found.
|
|
|
|
while (currentContent && currentContent != assignedNodes[i]) {
|
|
|
|
if (currentContent == aContent) {
|
2018-01-04 18:47:29 +03:00
|
|
|
insertionIndex.emplace(i);
|
|
|
|
break;
|
2017-12-04 11:06:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
currentContent = currentContent->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:47:29 +03:00
|
|
|
if (insertionIndex) {
|
2017-12-04 11:06:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:47:29 +03:00
|
|
|
if (insertionIndex) {
|
|
|
|
slot->InsertAssignedNode(*insertionIndex, aContent);
|
2017-12-04 11:06:34 +03:00
|
|
|
} else {
|
|
|
|
slot->AppendAssignedNode(aContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HTMLSlotElement*
|
|
|
|
ShadowRoot::UnassignSlotFor(nsIContent* aNode, const nsAString& aSlotName)
|
|
|
|
{
|
|
|
|
// Find the insertion point to which the content belongs. Note that if slot
|
|
|
|
// attribute is missing, unassign it from the first default slot, if exists.
|
|
|
|
nsTArray<HTMLSlotElement*>* slots = mSlotMap.Get(aSlotName);
|
|
|
|
if (!slots) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLSlotElement* slot = slots->ElementAt(0);
|
|
|
|
MOZ_ASSERT(slot);
|
|
|
|
|
|
|
|
if (!slot->AssignedNodes().Contains(aNode)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot->RemoveAssignedNode(aNode);
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShadowRoot::MaybeReassignElement(Element* aElement,
|
|
|
|
const nsAttrValue* aOldValue)
|
|
|
|
{
|
|
|
|
nsIContent* parent = aElement->GetParent();
|
|
|
|
if (parent && parent == GetHost()) {
|
|
|
|
const HTMLSlotElement* oldSlot = UnassignSlotFor(aElement,
|
|
|
|
aOldValue ? aOldValue->GetStringValue() : EmptyString());
|
|
|
|
const HTMLSlotElement* newSlot = AssignSlotFor(aElement);
|
|
|
|
|
|
|
|
if (oldSlot != newSlot) {
|
2017-12-19 18:16:33 +03:00
|
|
|
if (oldSlot) {
|
|
|
|
oldSlot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
|
|
|
if (newSlot) {
|
|
|
|
newSlot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:24:53 +03:00
|
|
|
void
|
|
|
|
ShadowRoot::DistributionChanged()
|
|
|
|
{
|
|
|
|
// FIXME(emilio): We could be more granular in a bunch of cases.
|
|
|
|
auto* host = GetHost();
|
2017-10-18 17:03:41 +03:00
|
|
|
if (!host || !host->IsInComposedDoc()) {
|
2017-10-18 16:24:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* shell = OwnerDoc()->GetShell();
|
|
|
|
if (!shell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-26 00:12:25 +03:00
|
|
|
shell->DestroyFramesForAndRestyle(host);
|
2017-10-18 16:24:53 +03:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
void
|
|
|
|
ShadowRoot::DistributeAllNodes()
|
|
|
|
{
|
2013-12-21 10:43:58 +04:00
|
|
|
|
2017-11-30 11:57:15 +03:00
|
|
|
//XXX Handle <slot>.
|
2017-10-18 16:24:53 +03:00
|
|
|
|
|
|
|
DistributionChanged();
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
void
|
|
|
|
ShadowRoot::GetInnerHTML(nsAString& aInnerHTML)
|
|
|
|
{
|
|
|
|
GetMarkup(false, aInnerHTML);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
SetInnerHTMLInternal(aInnerHTML, aError);
|
|
|
|
}
|
|
|
|
|
2014-10-16 05:25:45 +04:00
|
|
|
Element*
|
|
|
|
ShadowRoot::Host()
|
|
|
|
{
|
|
|
|
nsIContent* host = GetHost();
|
|
|
|
MOZ_ASSERT(host && host->IsElement(),
|
|
|
|
"ShadowRoot host should always be an element, "
|
|
|
|
"how else did we create this ShadowRoot?");
|
|
|
|
return host->AsElement();
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
bool
|
|
|
|
ShadowRoot::ApplyAuthorStyles()
|
|
|
|
{
|
|
|
|
return mProtoBinding->InheritsStyle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::SetApplyAuthorStyles(bool aApplyAuthorStyles)
|
|
|
|
{
|
|
|
|
mProtoBinding->SetInheritsStyle(aApplyAuthorStyles);
|
|
|
|
|
|
|
|
nsIPresShell* shell = OwnerDoc()->GetShell();
|
|
|
|
if (shell) {
|
|
|
|
OwnerDoc()->BeginUpdate(UPDATE_STYLE);
|
2014-06-07 12:42:54 +04:00
|
|
|
shell->RecordShadowStyleChange(this);
|
2013-12-02 14:26:12 +04:00
|
|
|
OwnerDoc()->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
void
|
|
|
|
ShadowRoot::AttributeChanged(nsIDocument* aDocument,
|
|
|
|
Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aAttribute,
|
2015-07-25 09:01:19 +03:00
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue)
|
2013-12-02 14:26:12 +04:00
|
|
|
{
|
2017-12-04 11:06:34 +03:00
|
|
|
if (aNameSpaceID != kNameSpaceID_None || aAttribute != nsGkAtoms::slot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes may change insertion point matching, find its new distribution.
|
|
|
|
if (!MaybeReassignElement(aElement, aOldValue)) {
|
2013-12-02 14:26:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:03:41 +03:00
|
|
|
if (!aElement->IsInComposedDoc()) {
|
2013-12-02 14:26:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:03:41 +03:00
|
|
|
auto* shell = OwnerDoc()->GetShell();
|
|
|
|
if (!shell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-30 11:57:15 +03:00
|
|
|
//XXX optimize this!
|
2017-10-26 00:12:25 +03:00
|
|
|
shell->DestroyFramesForAndRestyle(aElement);
|
2017-10-18 17:03:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent)
|
|
|
|
{
|
|
|
|
for (nsIContent* content = aFirstNewContent;
|
|
|
|
content;
|
|
|
|
content = content->GetNextSibling()) {
|
2018-01-05 13:15:32 +03:00
|
|
|
ContentInserted(aDocument, aContainer, content);
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aChild)
|
2013-12-02 14:26:12 +04:00
|
|
|
{
|
2017-12-04 11:06:34 +03:00
|
|
|
// Check to ensure that the content is in the same anonymous tree
|
|
|
|
// as the container because anonymous content may report its container
|
|
|
|
// as the host but it may not be in the host's child list.
|
|
|
|
if (!nsContentUtils::IsInSameAnonymousTree(aContainer, aChild)) {
|
2013-12-02 14:26:12 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
|
2017-12-19 18:16:33 +03:00
|
|
|
if (!aChild->IsSlotable()) {
|
2017-12-04 11:06:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:16:33 +03:00
|
|
|
if (aContainer && aContainer == GetHost()) {
|
|
|
|
if (const HTMLSlotElement* slot = AssignSlotFor(aChild)) {
|
|
|
|
slot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If parent's root is a shadow root, and parent is a slot whose assigned
|
|
|
|
// nodes is the empty list, then run signal a slot change for parent.
|
|
|
|
HTMLSlotElement* slot = HTMLSlotElement::FromContentOrNull(aContainer);
|
|
|
|
if (slot && slot->GetContainingShadow() == this &&
|
|
|
|
slot->AssignedNodes().IsEmpty()) {
|
|
|
|
slot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShadowRoot::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling)
|
|
|
|
{
|
2017-12-04 11:06:34 +03:00
|
|
|
// Check to ensure that the content is in the same anonymous tree
|
|
|
|
// as the container because anonymous content may report its container
|
|
|
|
// as the host but it may not be in the host's child list.
|
|
|
|
if (!nsContentUtils::IsInSameAnonymousTree(aContainer, aChild)) {
|
2013-12-02 14:26:12 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 11:06:34 +03:00
|
|
|
|
2017-12-19 18:16:33 +03:00
|
|
|
if (!aChild->IsSlotable()) {
|
2017-12-04 11:06:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:16:33 +03:00
|
|
|
if (aContainer && aContainer == GetHost()) {
|
|
|
|
nsAutoString slotName;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (aChild->IsElement()) {
|
|
|
|
aChild->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::slot, slotName);
|
|
|
|
}
|
2017-12-19 18:16:33 +03:00
|
|
|
if (const HTMLSlotElement* slot = UnassignSlotFor(aChild, slotName)) {
|
|
|
|
slot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If parent's root is a shadow root, and parent is a slot whose assigned
|
|
|
|
// nodes is the empty list, then run signal a slot change for parent.
|
|
|
|
HTMLSlotElement* slot = HTMLSlotElement::FromContentOrNull(aContainer);
|
|
|
|
if (slot && slot->GetContainingShadow() == this &&
|
|
|
|
slot->AssignedNodes().IsEmpty()) {
|
|
|
|
slot->EnqueueSlotChangeEvent();
|
|
|
|
}
|
2013-12-02 14:26:12 +04:00
|
|
|
}
|
|
|
|
|
2015-07-07 04:26:29 +03:00
|
|
|
nsresult
|
2017-04-20 22:57:48 +03:00
|
|
|
ShadowRoot::Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
|
|
|
|
bool aPreallocateChildren) const
|
2015-07-07 04:26:29 +03:00
|
|
|
{
|
|
|
|
*aResult = nullptr;
|
2018-01-15 09:42:43 +03:00
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
2015-07-07 04:26:29 +03:00
|
|
|
}
|