content/base/src/ShadowRoot.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/Preferences.h"
michael@0 7 #include "mozilla/dom/ShadowRoot.h"
michael@0 8 #include "mozilla/dom/ShadowRootBinding.h"
michael@0 9 #include "mozilla/dom/DocumentFragment.h"
michael@0 10 #include "ChildIterator.h"
michael@0 11 #include "nsContentUtils.h"
michael@0 12 #include "nsDOMClassInfoID.h"
michael@0 13 #include "nsIDOMHTMLElement.h"
michael@0 14 #include "nsIStyleSheetLinkingElement.h"
michael@0 15 #include "mozilla/dom/Element.h"
michael@0 16 #include "mozilla/dom/HTMLContentElement.h"
michael@0 17 #include "mozilla/dom/HTMLShadowElement.h"
michael@0 18 #include "nsXBLPrototypeBinding.h"
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using namespace mozilla::dom;
michael@0 22
michael@0 23 static PLDHashOperator
michael@0 24 IdentifierMapEntryTraverse(nsIdentifierMapEntry *aEntry, void *aArg)
michael@0 25 {
michael@0 26 nsCycleCollectionTraversalCallback *cb =
michael@0 27 static_cast<nsCycleCollectionTraversalCallback*>(aArg);
michael@0 28 aEntry->Traverse(cb);
michael@0 29 return PL_DHASH_NEXT;
michael@0 30 }
michael@0 31
michael@0 32 NS_IMPL_CYCLE_COLLECTION_CLASS(ShadowRoot)
michael@0 33
michael@0 34 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ShadowRoot,
michael@0 35 DocumentFragment)
michael@0 36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPoolHost)
michael@0 37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheetList)
michael@0 38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOlderShadow)
michael@0 39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mYoungerShadow)
michael@0 40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAssociatedBinding)
michael@0 41 tmp->mIdentifierMap.EnumerateEntries(IdentifierMapEntryTraverse, &cb);
michael@0 42 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 43
michael@0 44 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ShadowRoot,
michael@0 45 DocumentFragment)
michael@0 46 if (tmp->mPoolHost) {
michael@0 47 tmp->mPoolHost->RemoveMutationObserver(tmp);
michael@0 48 }
michael@0 49 NS_IMPL_CYCLE_COLLECTION_UNLINK(mPoolHost)
michael@0 50 NS_IMPL_CYCLE_COLLECTION_UNLINK(mStyleSheetList)
michael@0 51 NS_IMPL_CYCLE_COLLECTION_UNLINK(mOlderShadow)
michael@0 52 NS_IMPL_CYCLE_COLLECTION_UNLINK(mYoungerShadow)
michael@0 53 NS_IMPL_CYCLE_COLLECTION_UNLINK(mAssociatedBinding)
michael@0 54 tmp->mIdentifierMap.Clear();
michael@0 55 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 56
michael@0 57 DOMCI_DATA(ShadowRoot, ShadowRoot)
michael@0 58
michael@0 59 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ShadowRoot)
michael@0 60 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent)
michael@0 61 NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
michael@0 62 NS_INTERFACE_MAP_END_INHERITING(DocumentFragment)
michael@0 63
michael@0 64 NS_IMPL_ADDREF_INHERITED(ShadowRoot, DocumentFragment)
michael@0 65 NS_IMPL_RELEASE_INHERITED(ShadowRoot, DocumentFragment)
michael@0 66
michael@0 67 ShadowRoot::ShadowRoot(nsIContent* aContent,
michael@0 68 already_AddRefed<nsINodeInfo>&& aNodeInfo,
michael@0 69 nsXBLPrototypeBinding* aProtoBinding)
michael@0 70 : DocumentFragment(aNodeInfo), mPoolHost(aContent),
michael@0 71 mProtoBinding(aProtoBinding), mShadowElement(nullptr),
michael@0 72 mInsertionPointChanged(false)
michael@0 73 {
michael@0 74 SetHost(aContent);
michael@0 75 SetFlags(NODE_IS_IN_SHADOW_TREE);
michael@0 76 // ShadowRoot isn't really in the document but it behaves like it is.
michael@0 77 SetInDocument();
michael@0 78 DOMSlots()->mBindingParent = aContent;
michael@0 79 DOMSlots()->mContainingShadow = this;
michael@0 80
michael@0 81 // Add the ShadowRoot as a mutation observer on the host to watch
michael@0 82 // for mutations because the insertion points in this ShadowRoot
michael@0 83 // may need to be updated when the host children are modified.
michael@0 84 mPoolHost->AddMutationObserver(this);
michael@0 85 }
michael@0 86
michael@0 87 ShadowRoot::~ShadowRoot()
michael@0 88 {
michael@0 89 if (mPoolHost) {
michael@0 90 // mPoolHost may have been unlinked or a new ShadowRoot may have been
michael@0 91 // creating, making this one obsolete.
michael@0 92 mPoolHost->RemoveMutationObserver(this);
michael@0 93 }
michael@0 94
michael@0 95 ClearInDocument();
michael@0 96 SetHost(nullptr);
michael@0 97 }
michael@0 98
michael@0 99 JSObject*
michael@0 100 ShadowRoot::WrapObject(JSContext* aCx)
michael@0 101 {
michael@0 102 return mozilla::dom::ShadowRootBinding::Wrap(aCx, this);
michael@0 103 }
michael@0 104
michael@0 105 ShadowRoot*
michael@0 106 ShadowRoot::FromNode(nsINode* aNode)
michael@0 107 {
michael@0 108 if (aNode->HasFlag(NODE_IS_IN_SHADOW_TREE) && !aNode->GetParentNode()) {
michael@0 109 MOZ_ASSERT(aNode->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE,
michael@0 110 "ShadowRoot is a document fragment.");
michael@0 111 return static_cast<ShadowRoot*>(aNode);
michael@0 112 }
michael@0 113
michael@0 114 return nullptr;
michael@0 115 }
michael@0 116
michael@0 117 void
michael@0 118 ShadowRoot::Restyle()
michael@0 119 {
michael@0 120 mProtoBinding->FlushSkinSheets();
michael@0 121
michael@0 122 nsIPresShell* shell = OwnerDoc()->GetShell();
michael@0 123 if (shell) {
michael@0 124 OwnerDoc()->BeginUpdate(UPDATE_STYLE);
michael@0 125 shell->RestyleShadowRoot(this);
michael@0 126 OwnerDoc()->EndUpdate(UPDATE_STYLE);
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 void
michael@0 131 ShadowRoot::InsertSheet(nsCSSStyleSheet* aSheet,
michael@0 132 nsIContent* aLinkingContent)
michael@0 133 {
michael@0 134 nsCOMPtr<nsIStyleSheetLinkingElement>
michael@0 135 linkingElement = do_QueryInterface(aLinkingContent);
michael@0 136 MOZ_ASSERT(linkingElement, "The only styles in a ShadowRoot should come "
michael@0 137 "from <style>.");
michael@0 138
michael@0 139 linkingElement->SetStyleSheet(aSheet); // This sets the ownerNode on the sheet
michael@0 140
michael@0 141 nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
michael@0 142 mProtoBinding->GetOrCreateStyleSheets();
michael@0 143 MOZ_ASSERT(sheets, "Style sheets array should never be null.");
michael@0 144
michael@0 145 // Find the correct position to insert into the style sheet list (must
michael@0 146 // be in tree order).
michael@0 147 for (uint32_t i = 0; i <= sheets->Length(); i++) {
michael@0 148 if (i == sheets->Length()) {
michael@0 149 sheets->AppendElement(aSheet);
michael@0 150 break;
michael@0 151 }
michael@0 152
michael@0 153 nsINode* sheetOwnerNode = sheets->ElementAt(i)->GetOwnerNode();
michael@0 154 if (nsContentUtils::PositionIsBefore(aLinkingContent, sheetOwnerNode)) {
michael@0 155 sheets->InsertElementAt(i, aSheet);
michael@0 156 break;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 Restyle();
michael@0 161 }
michael@0 162
michael@0 163 void
michael@0 164 ShadowRoot::RemoveSheet(nsCSSStyleSheet* aSheet)
michael@0 165 {
michael@0 166 nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
michael@0 167 mProtoBinding->GetOrCreateStyleSheets();
michael@0 168 MOZ_ASSERT(sheets, "Style sheets array should never be null.");
michael@0 169
michael@0 170 DebugOnly<bool> found = sheets->RemoveElement(aSheet);
michael@0 171 MOZ_ASSERT(found, "Trying to remove a sheet from a ShadowRoot "
michael@0 172 "that does not exist.");
michael@0 173
michael@0 174 Restyle();
michael@0 175 }
michael@0 176
michael@0 177 Element*
michael@0 178 ShadowRoot::GetElementById(const nsAString& aElementId)
michael@0 179 {
michael@0 180 nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aElementId);
michael@0 181 return entry ? entry->GetIdElement() : nullptr;
michael@0 182 }
michael@0 183
michael@0 184 already_AddRefed<nsContentList>
michael@0 185 ShadowRoot::GetElementsByTagName(const nsAString& aTagName)
michael@0 186 {
michael@0 187 return NS_GetContentList(this, kNameSpaceID_Unknown, aTagName);
michael@0 188 }
michael@0 189
michael@0 190 already_AddRefed<nsContentList>
michael@0 191 ShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
michael@0 192 const nsAString& aLocalName)
michael@0 193 {
michael@0 194 int32_t nameSpaceId = kNameSpaceID_Wildcard;
michael@0 195
michael@0 196 if (!aNamespaceURI.EqualsLiteral("*")) {
michael@0 197 nsresult rv =
michael@0 198 nsContentUtils::NameSpaceManager()->RegisterNameSpace(aNamespaceURI,
michael@0 199 nameSpaceId);
michael@0 200 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 201 }
michael@0 202
michael@0 203 NS_ASSERTION(nameSpaceId != kNameSpaceID_Unknown, "Unexpected namespace ID!");
michael@0 204
michael@0 205 return NS_GetContentList(this, nameSpaceId, aLocalName);
michael@0 206 }
michael@0 207
michael@0 208 void
michael@0 209 ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
michael@0 210 {
michael@0 211 nsIdentifierMapEntry *entry =
michael@0 212 mIdentifierMap.PutEntry(nsDependentAtomString(aId));
michael@0 213 if (entry) {
michael@0 214 entry->AddIdElement(aElement);
michael@0 215 }
michael@0 216 }
michael@0 217
michael@0 218 void
michael@0 219 ShadowRoot::RemoveFromIdTable(Element* aElement, nsIAtom* aId)
michael@0 220 {
michael@0 221 nsIdentifierMapEntry *entry =
michael@0 222 mIdentifierMap.GetEntry(nsDependentAtomString(aId));
michael@0 223 if (entry) {
michael@0 224 entry->RemoveIdElement(aElement);
michael@0 225 if (entry->IsEmpty()) {
michael@0 226 mIdentifierMap.RawRemoveEntry(entry);
michael@0 227 }
michael@0 228 }
michael@0 229 }
michael@0 230
michael@0 231 already_AddRefed<nsContentList>
michael@0 232 ShadowRoot::GetElementsByClassName(const nsAString& aClasses)
michael@0 233 {
michael@0 234 return nsContentUtils::GetElementsByClassName(this, aClasses);
michael@0 235 }
michael@0 236
michael@0 237 void
michael@0 238 ShadowRoot::AddInsertionPoint(HTMLContentElement* aInsertionPoint)
michael@0 239 {
michael@0 240 TreeOrderComparator comparator;
michael@0 241 mInsertionPoints.InsertElementSorted(aInsertionPoint, comparator);
michael@0 242 }
michael@0 243
michael@0 244 void
michael@0 245 ShadowRoot::RemoveInsertionPoint(HTMLContentElement* aInsertionPoint)
michael@0 246 {
michael@0 247 mInsertionPoints.RemoveElement(aInsertionPoint);
michael@0 248 }
michael@0 249
michael@0 250 void
michael@0 251 ShadowRoot::SetYoungerShadow(ShadowRoot* aYoungerShadow)
michael@0 252 {
michael@0 253 mYoungerShadow = aYoungerShadow;
michael@0 254 mYoungerShadow->mOlderShadow = this;
michael@0 255
michael@0 256 ChangePoolHost(mYoungerShadow->GetShadowElement());
michael@0 257 }
michael@0 258
michael@0 259 void
michael@0 260 ShadowRoot::DistributeSingleNode(nsIContent* aContent)
michael@0 261 {
michael@0 262 // Find the insertion point to which the content belongs.
michael@0 263 HTMLContentElement* insertionPoint = nullptr;
michael@0 264 for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
michael@0 265 if (mInsertionPoints[i]->Match(aContent)) {
michael@0 266 if (mInsertionPoints[i]->MatchedNodes().Contains(aContent)) {
michael@0 267 // Node is already matched into the insertion point. We are done.
michael@0 268 return;
michael@0 269 }
michael@0 270
michael@0 271 // Matching may cause the insertion point to drop fallback content.
michael@0 272 if (mInsertionPoints[i]->MatchedNodes().IsEmpty() &&
michael@0 273 static_cast<nsINode*>(mInsertionPoints[i])->GetFirstChild()) {
michael@0 274 // This match will cause the insertion point to drop all fallback
michael@0 275 // content and used matched nodes instead. Give up on the optimization
michael@0 276 // and just distribute all nodes.
michael@0 277 DistributeAllNodes();
michael@0 278 return;
michael@0 279 }
michael@0 280 insertionPoint = mInsertionPoints[i];
michael@0 281 break;
michael@0 282 }
michael@0 283 }
michael@0 284
michael@0 285 // Find the index into the insertion point.
michael@0 286 if (insertionPoint) {
michael@0 287 nsCOMArray<nsIContent>& matchedNodes = insertionPoint->MatchedNodes();
michael@0 288 // Find the appropriate position in the matched node list for the
michael@0 289 // newly distributed content.
michael@0 290 bool isIndexFound = false;
michael@0 291 MOZ_ASSERT(mPoolHost, "Where did the content come from if there is no pool host?");
michael@0 292 ExplicitChildIterator childIterator(mPoolHost);
michael@0 293 for (uint32_t i = 0; i < matchedNodes.Length(); i++) {
michael@0 294 // Seek through the host's explicit children until the inserted content
michael@0 295 // is found or when the current matched node is reached.
michael@0 296 if (childIterator.Seek(aContent, matchedNodes[i])) {
michael@0 297 // aContent was found before the current matched node.
michael@0 298 matchedNodes.InsertElementAt(i, aContent);
michael@0 299 isIndexFound = true;
michael@0 300 break;
michael@0 301 }
michael@0 302 }
michael@0 303
michael@0 304 if (!isIndexFound) {
michael@0 305 // We have still not found an index in the insertion point,
michael@0 306 // thus it must be at the end.
michael@0 307 MOZ_ASSERT(childIterator.Seek(aContent),
michael@0 308 "Trying to match a node that is not a candidate to be matched");
michael@0 309 matchedNodes.AppendElement(aContent);
michael@0 310 }
michael@0 311
michael@0 312 // Handle the case where the parent of the insertion point is a ShadowRoot
michael@0 313 // that is projected into the younger ShadowRoot's shadow insertion point.
michael@0 314 // The node distributed into the insertion point must be reprojected
michael@0 315 // to the shadow insertion point.
michael@0 316 if (insertionPoint->GetParent() == this &&
michael@0 317 mYoungerShadow && mYoungerShadow->GetShadowElement()) {
michael@0 318 mYoungerShadow->GetShadowElement()->DistributeSingleNode(aContent);
michael@0 319 }
michael@0 320
michael@0 321 // Handle the case where the parent of the insertion point has a ShadowRoot.
michael@0 322 // The node distributed into the insertion point must be reprojected to the
michael@0 323 // insertion points of the parent's ShadowRoot.
michael@0 324 ShadowRoot* parentShadow = insertionPoint->GetParent()->GetShadowRoot();
michael@0 325 if (parentShadow) {
michael@0 326 parentShadow->DistributeSingleNode(aContent);
michael@0 327 }
michael@0 328
michael@0 329 // Handle the case where the parent of the insertion point is the <shadow>
michael@0 330 // element. The node distributed into the insertion point must be reprojected
michael@0 331 // into the older ShadowRoot's insertion points.
michael@0 332 if (mShadowElement && mShadowElement == insertionPoint->GetParent()) {
michael@0 333 ShadowRoot* olderShadow = mShadowElement->GetOlderShadowRoot();
michael@0 334 if (olderShadow) {
michael@0 335 olderShadow->DistributeSingleNode(aContent);
michael@0 336 }
michael@0 337 }
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 void
michael@0 342 ShadowRoot::RemoveDistributedNode(nsIContent* aContent)
michael@0 343 {
michael@0 344 // Find insertion point containing the content and remove the node.
michael@0 345 for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
michael@0 346 if (mInsertionPoints[i]->MatchedNodes().Contains(aContent)) {
michael@0 347 // Removing the matched node may cause the insertion point to use
michael@0 348 // fallback content.
michael@0 349 if (mInsertionPoints[i]->MatchedNodes().Length() == 1 &&
michael@0 350 static_cast<nsINode*>(mInsertionPoints[i])->GetFirstChild()) {
michael@0 351 // Removing the matched node will cause fallback content to be
michael@0 352 // used instead. Give up optimization and distribute all nodes.
michael@0 353 DistributeAllNodes();
michael@0 354 return;
michael@0 355 }
michael@0 356
michael@0 357 mInsertionPoints[i]->MatchedNodes().RemoveElement(aContent);
michael@0 358
michael@0 359 // Handle the case where the parent of the insertion point is a ShadowRoot
michael@0 360 // that is projected into the younger ShadowRoot's shadow insertion point.
michael@0 361 // The removed node needs to be removed from the shadow insertion point.
michael@0 362 if (mInsertionPoints[i]->GetParent() == this) {
michael@0 363 if (mYoungerShadow && mYoungerShadow->GetShadowElement()) {
michael@0 364 mYoungerShadow->GetShadowElement()->RemoveDistributedNode(aContent);
michael@0 365 }
michael@0 366 }
michael@0 367
michael@0 368 // Handle the case where the parent of the insertion point has a ShadowRoot.
michael@0 369 // The removed node needs to be removed from the insertion points of the
michael@0 370 // parent's ShadowRoot.
michael@0 371 ShadowRoot* parentShadow = mInsertionPoints[i]->GetParent()->GetShadowRoot();
michael@0 372 if (parentShadow) {
michael@0 373 parentShadow->RemoveDistributedNode(aContent);
michael@0 374 }
michael@0 375
michael@0 376 // Handle the case where the parent of the insertion point is the <shadow>
michael@0 377 // element. The removed node must be removed from the older ShadowRoot's
michael@0 378 // insertion points.
michael@0 379 if (mShadowElement && mShadowElement == mInsertionPoints[i]->GetParent()) {
michael@0 380 ShadowRoot* olderShadow = mShadowElement->GetOlderShadowRoot();
michael@0 381 if (olderShadow) {
michael@0 382 olderShadow->RemoveDistributedNode(aContent);
michael@0 383 }
michael@0 384 }
michael@0 385
michael@0 386 break;
michael@0 387 }
michael@0 388 }
michael@0 389 }
michael@0 390
michael@0 391 void
michael@0 392 ShadowRoot::DistributeAllNodes()
michael@0 393 {
michael@0 394 // Create node pool.
michael@0 395 nsTArray<nsIContent*> nodePool;
michael@0 396
michael@0 397 // Make sure there is a pool host, an older shadow may not have
michael@0 398 // one if the younger shadow does not have a <shadow> element.
michael@0 399 if (mPoolHost) {
michael@0 400 ExplicitChildIterator childIterator(mPoolHost);
michael@0 401 for (nsIContent* content = childIterator.GetNextChild();
michael@0 402 content;
michael@0 403 content = childIterator.GetNextChild()) {
michael@0 404 nodePool.AppendElement(content);
michael@0 405 }
michael@0 406 }
michael@0 407
michael@0 408 nsTArray<ShadowRoot*> shadowsToUpdate;
michael@0 409
michael@0 410 for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
michael@0 411 mInsertionPoints[i]->ClearMatchedNodes();
michael@0 412 // Assign matching nodes from node pool.
michael@0 413 for (uint32_t j = 0; j < nodePool.Length(); j++) {
michael@0 414 if (mInsertionPoints[i]->Match(nodePool[j])) {
michael@0 415 mInsertionPoints[i]->MatchedNodes().AppendElement(nodePool[j]);
michael@0 416 nodePool[j]->SetXBLInsertionParent(mInsertionPoints[i]);
michael@0 417 nodePool.RemoveElementAt(j--);
michael@0 418 }
michael@0 419 }
michael@0 420
michael@0 421 // Keep track of instances where the content insertion point is distributed
michael@0 422 // (parent of insertion point has a ShadowRoot).
michael@0 423 nsIContent* insertionParent = mInsertionPoints[i]->GetParent();
michael@0 424 MOZ_ASSERT(insertionParent, "The only way for an insertion point to be in the"
michael@0 425 "mInsertionPoints array is to be a descendant of a"
michael@0 426 "ShadowRoot, in which case, it should have a parent");
michael@0 427
michael@0 428 // If the parent of the insertion point has as ShadowRoot, the nodes distributed
michael@0 429 // to the insertion point must be reprojected to the insertion points of the
michael@0 430 // parent's ShadowRoot.
michael@0 431 ShadowRoot* parentShadow = insertionParent->GetShadowRoot();
michael@0 432 if (parentShadow && !shadowsToUpdate.Contains(parentShadow)) {
michael@0 433 shadowsToUpdate.AppendElement(parentShadow);
michael@0 434 }
michael@0 435 }
michael@0 436
michael@0 437 // If there is a shadow insertion point in this ShadowRoot, the children
michael@0 438 // of the shadow insertion point needs to be distributed into the insertion
michael@0 439 // points of the older ShadowRoot.
michael@0 440 if (mShadowElement && mOlderShadow) {
michael@0 441 mOlderShadow->DistributeAllNodes();
michael@0 442 }
michael@0 443
michael@0 444 // If there is a younger ShadowRoot with a shadow insertion point,
michael@0 445 // then the children of this ShadowRoot needs to be distributed to
michael@0 446 // the younger ShadowRoot's shadow insertion point.
michael@0 447 if (mYoungerShadow && mYoungerShadow->GetShadowElement()) {
michael@0 448 mYoungerShadow->GetShadowElement()->DistributeAllNodes();
michael@0 449 }
michael@0 450
michael@0 451 for (uint32_t i = 0; i < shadowsToUpdate.Length(); i++) {
michael@0 452 shadowsToUpdate[i]->DistributeAllNodes();
michael@0 453 }
michael@0 454 }
michael@0 455
michael@0 456 void
michael@0 457 ShadowRoot::GetInnerHTML(nsAString& aInnerHTML)
michael@0 458 {
michael@0 459 GetMarkup(false, aInnerHTML);
michael@0 460 }
michael@0 461
michael@0 462 void
michael@0 463 ShadowRoot::SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError)
michael@0 464 {
michael@0 465 SetInnerHTMLInternal(aInnerHTML, aError);
michael@0 466 }
michael@0 467
michael@0 468 bool
michael@0 469 ShadowRoot::ApplyAuthorStyles()
michael@0 470 {
michael@0 471 return mProtoBinding->InheritsStyle();
michael@0 472 }
michael@0 473
michael@0 474 void
michael@0 475 ShadowRoot::SetApplyAuthorStyles(bool aApplyAuthorStyles)
michael@0 476 {
michael@0 477 mProtoBinding->SetInheritsStyle(aApplyAuthorStyles);
michael@0 478
michael@0 479 nsIPresShell* shell = OwnerDoc()->GetShell();
michael@0 480 if (shell) {
michael@0 481 OwnerDoc()->BeginUpdate(UPDATE_STYLE);
michael@0 482 shell->RestyleShadowRoot(this);
michael@0 483 OwnerDoc()->EndUpdate(UPDATE_STYLE);
michael@0 484 }
michael@0 485 }
michael@0 486
michael@0 487 StyleSheetList*
michael@0 488 ShadowRoot::StyleSheets()
michael@0 489 {
michael@0 490 if (!mStyleSheetList) {
michael@0 491 mStyleSheetList = new ShadowRootStyleSheetList(this);
michael@0 492 }
michael@0 493
michael@0 494 return mStyleSheetList;
michael@0 495 }
michael@0 496
michael@0 497 void
michael@0 498 ShadowRoot::SetShadowElement(HTMLShadowElement* aShadowElement)
michael@0 499 {
michael@0 500 // If there is already a shadow element point, remove
michael@0 501 // the projected shadow because it is no longer an insertion
michael@0 502 // point.
michael@0 503 if (mShadowElement) {
michael@0 504 mShadowElement->SetProjectedShadow(nullptr);
michael@0 505 }
michael@0 506
michael@0 507 if (mOlderShadow) {
michael@0 508 // Nodes for distribution will come from the new shadow element.
michael@0 509 mOlderShadow->ChangePoolHost(aShadowElement);
michael@0 510 }
michael@0 511
michael@0 512 // Set the new shadow element to project the older ShadowRoot because
michael@0 513 // it is the current shadow insertion point.
michael@0 514 mShadowElement = aShadowElement;
michael@0 515 if (mShadowElement) {
michael@0 516 mShadowElement->SetProjectedShadow(mOlderShadow);
michael@0 517 }
michael@0 518 }
michael@0 519
michael@0 520 void
michael@0 521 ShadowRoot::ChangePoolHost(nsIContent* aNewHost)
michael@0 522 {
michael@0 523 if (mPoolHost) {
michael@0 524 mPoolHost->RemoveMutationObserver(this);
michael@0 525 }
michael@0 526
michael@0 527 // Clear the nodes matched to content insertion points
michael@0 528 // because it is no longer relevant.
michael@0 529 for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
michael@0 530 mInsertionPoints[i]->ClearMatchedNodes();
michael@0 531 }
michael@0 532
michael@0 533 mPoolHost = aNewHost;
michael@0 534 if (mPoolHost) {
michael@0 535 mPoolHost->AddMutationObserver(this);
michael@0 536 }
michael@0 537 }
michael@0 538
michael@0 539 bool
michael@0 540 ShadowRoot::IsShadowInsertionPoint(nsIContent* aContent)
michael@0 541 {
michael@0 542 if (aContent && aContent->IsHTML(nsGkAtoms::shadow)) {
michael@0 543 HTMLShadowElement* shadowElem = static_cast<HTMLShadowElement*>(aContent);
michael@0 544 return shadowElem->IsInsertionPoint();
michael@0 545 }
michael@0 546 return false;
michael@0 547 }
michael@0 548
michael@0 549 /**
michael@0 550 * Returns whether the web components pool population algorithm
michael@0 551 * on the host would contain |aContent|. This function ignores
michael@0 552 * insertion points in the pool, thus should only be used to
michael@0 553 * test nodes that have not yet been distributed.
michael@0 554 */
michael@0 555 bool
michael@0 556 ShadowRoot::IsPooledNode(nsIContent* aContent, nsIContent* aContainer,
michael@0 557 nsIContent* aHost)
michael@0 558 {
michael@0 559 if (nsContentUtils::IsContentInsertionPoint(aContent) ||
michael@0 560 IsShadowInsertionPoint(aContent)) {
michael@0 561 // Insertion points never end up in the pool.
michael@0 562 return false;
michael@0 563 }
michael@0 564
michael@0 565 if (aContainer->IsHTML(nsGkAtoms::content)) {
michael@0 566 // Fallback content will end up in pool if its parent is a child of the host.
michael@0 567 HTMLContentElement* content = static_cast<HTMLContentElement*>(aContainer);
michael@0 568 return content->IsInsertionPoint() && content->MatchedNodes().IsEmpty() &&
michael@0 569 aContainer->GetParentNode() == aHost;
michael@0 570 }
michael@0 571
michael@0 572 if (aContainer == aHost) {
michael@0 573 // Any other child nodes of the host will end up in the pool.
michael@0 574 return true;
michael@0 575 }
michael@0 576
michael@0 577 return false;
michael@0 578 }
michael@0 579
michael@0 580 void
michael@0 581 ShadowRoot::AttributeChanged(nsIDocument* aDocument,
michael@0 582 Element* aElement,
michael@0 583 int32_t aNameSpaceID,
michael@0 584 nsIAtom* aAttribute,
michael@0 585 int32_t aModType)
michael@0 586 {
michael@0 587 if (!IsPooledNode(aElement, aElement->GetParent(), mPoolHost)) {
michael@0 588 return;
michael@0 589 }
michael@0 590
michael@0 591 // Attributes may change insertion point matching, find its new distribution.
michael@0 592 RemoveDistributedNode(aElement);
michael@0 593 DistributeSingleNode(aElement);
michael@0 594 }
michael@0 595
michael@0 596 void
michael@0 597 ShadowRoot::ContentAppended(nsIDocument* aDocument,
michael@0 598 nsIContent* aContainer,
michael@0 599 nsIContent* aFirstNewContent,
michael@0 600 int32_t aNewIndexInContainer)
michael@0 601 {
michael@0 602 if (mInsertionPointChanged) {
michael@0 603 DistributeAllNodes();
michael@0 604 mInsertionPointChanged = false;
michael@0 605 return;
michael@0 606 }
michael@0 607
michael@0 608 // Watch for new nodes added to the pool because the node
michael@0 609 // may need to be added to an insertion point.
michael@0 610 nsIContent* currentChild = aFirstNewContent;
michael@0 611 while (currentChild) {
michael@0 612 if (IsPooledNode(currentChild, aContainer, mPoolHost)) {
michael@0 613 DistributeSingleNode(currentChild);
michael@0 614 }
michael@0 615 currentChild = currentChild->GetNextSibling();
michael@0 616 }
michael@0 617 }
michael@0 618
michael@0 619 void
michael@0 620 ShadowRoot::ContentInserted(nsIDocument* aDocument,
michael@0 621 nsIContent* aContainer,
michael@0 622 nsIContent* aChild,
michael@0 623 int32_t aIndexInContainer)
michael@0 624 {
michael@0 625 if (mInsertionPointChanged) {
michael@0 626 DistributeAllNodes();
michael@0 627 mInsertionPointChanged = false;
michael@0 628 return;
michael@0 629 }
michael@0 630
michael@0 631 // Watch for new nodes added to the pool because the node
michael@0 632 // may need to be added to an insertion point.
michael@0 633 if (IsPooledNode(aChild, aContainer, mPoolHost)) {
michael@0 634 DistributeSingleNode(aChild);
michael@0 635 }
michael@0 636 }
michael@0 637
michael@0 638 void
michael@0 639 ShadowRoot::ContentRemoved(nsIDocument* aDocument,
michael@0 640 nsIContent* aContainer,
michael@0 641 nsIContent* aChild,
michael@0 642 int32_t aIndexInContainer,
michael@0 643 nsIContent* aPreviousSibling)
michael@0 644 {
michael@0 645 if (mInsertionPointChanged) {
michael@0 646 DistributeAllNodes();
michael@0 647 mInsertionPointChanged = false;
michael@0 648 return;
michael@0 649 }
michael@0 650
michael@0 651 // Watch for node that is removed from the pool because
michael@0 652 // it may need to be removed from an insertion point.
michael@0 653 if (IsPooledNode(aChild, aContainer, mPoolHost)) {
michael@0 654 RemoveDistributedNode(aChild);
michael@0 655 }
michael@0 656 }
michael@0 657
michael@0 658 NS_IMPL_CYCLE_COLLECTION_INHERITED(ShadowRootStyleSheetList, StyleSheetList,
michael@0 659 mShadowRoot)
michael@0 660
michael@0 661 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ShadowRootStyleSheetList)
michael@0 662 NS_INTERFACE_MAP_END_INHERITING(StyleSheetList)
michael@0 663
michael@0 664 NS_IMPL_ADDREF_INHERITED(ShadowRootStyleSheetList, StyleSheetList)
michael@0 665 NS_IMPL_RELEASE_INHERITED(ShadowRootStyleSheetList, StyleSheetList)
michael@0 666
michael@0 667 ShadowRootStyleSheetList::ShadowRootStyleSheetList(ShadowRoot* aShadowRoot)
michael@0 668 : mShadowRoot(aShadowRoot)
michael@0 669 {
michael@0 670 MOZ_COUNT_CTOR(ShadowRootStyleSheetList);
michael@0 671 }
michael@0 672
michael@0 673 ShadowRootStyleSheetList::~ShadowRootStyleSheetList()
michael@0 674 {
michael@0 675 MOZ_COUNT_DTOR(ShadowRootStyleSheetList);
michael@0 676 }
michael@0 677
michael@0 678 nsCSSStyleSheet*
michael@0 679 ShadowRootStyleSheetList::IndexedGetter(uint32_t aIndex, bool& aFound)
michael@0 680 {
michael@0 681 nsTArray<nsRefPtr<nsCSSStyleSheet>>* sheets =
michael@0 682 mShadowRoot->mProtoBinding->GetStyleSheets();
michael@0 683
michael@0 684 if (!sheets) {
michael@0 685 aFound = false;
michael@0 686 return nullptr;
michael@0 687 }
michael@0 688
michael@0 689 aFound = aIndex < sheets->Length();
michael@0 690 return sheets->SafeElementAt(aIndex);
michael@0 691 }
michael@0 692
michael@0 693 uint32_t
michael@0 694 ShadowRootStyleSheetList::Length()
michael@0 695 {
michael@0 696 nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
michael@0 697 mShadowRoot->mProtoBinding->GetStyleSheets();
michael@0 698
michael@0 699 if (!sheets) {
michael@0 700 return 0;
michael@0 701 }
michael@0 702
michael@0 703 return sheets->Length();
michael@0 704 }
michael@0 705

mercurial