content/base/src/ShadowRoot.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/ShadowRoot.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,705 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/Preferences.h"
    1.10 +#include "mozilla/dom/ShadowRoot.h"
    1.11 +#include "mozilla/dom/ShadowRootBinding.h"
    1.12 +#include "mozilla/dom/DocumentFragment.h"
    1.13 +#include "ChildIterator.h"
    1.14 +#include "nsContentUtils.h"
    1.15 +#include "nsDOMClassInfoID.h"
    1.16 +#include "nsIDOMHTMLElement.h"
    1.17 +#include "nsIStyleSheetLinkingElement.h"
    1.18 +#include "mozilla/dom/Element.h"
    1.19 +#include "mozilla/dom/HTMLContentElement.h"
    1.20 +#include "mozilla/dom/HTMLShadowElement.h"
    1.21 +#include "nsXBLPrototypeBinding.h"
    1.22 +
    1.23 +using namespace mozilla;
    1.24 +using namespace mozilla::dom;
    1.25 +
    1.26 +static PLDHashOperator
    1.27 +IdentifierMapEntryTraverse(nsIdentifierMapEntry *aEntry, void *aArg)
    1.28 +{
    1.29 +  nsCycleCollectionTraversalCallback *cb =
    1.30 +    static_cast<nsCycleCollectionTraversalCallback*>(aArg);
    1.31 +  aEntry->Traverse(cb);
    1.32 +  return PL_DHASH_NEXT;
    1.33 +}
    1.34 +
    1.35 +NS_IMPL_CYCLE_COLLECTION_CLASS(ShadowRoot)
    1.36 +
    1.37 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ShadowRoot,
    1.38 +                                                  DocumentFragment)
    1.39 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPoolHost)
    1.40 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheetList)
    1.41 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOlderShadow)
    1.42 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mYoungerShadow)
    1.43 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAssociatedBinding)
    1.44 +  tmp->mIdentifierMap.EnumerateEntries(IdentifierMapEntryTraverse, &cb);
    1.45 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    1.46 +
    1.47 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ShadowRoot,
    1.48 +                                                DocumentFragment)
    1.49 +  if (tmp->mPoolHost) {
    1.50 +    tmp->mPoolHost->RemoveMutationObserver(tmp);
    1.51 +  }
    1.52 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mPoolHost)
    1.53 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mStyleSheetList)
    1.54 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mOlderShadow)
    1.55 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mYoungerShadow)
    1.56 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mAssociatedBinding)
    1.57 +  tmp->mIdentifierMap.Clear();
    1.58 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END
    1.59 +
    1.60 +DOMCI_DATA(ShadowRoot, ShadowRoot)
    1.61 +
    1.62 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ShadowRoot)
    1.63 +  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent)
    1.64 +  NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
    1.65 +NS_INTERFACE_MAP_END_INHERITING(DocumentFragment)
    1.66 +
    1.67 +NS_IMPL_ADDREF_INHERITED(ShadowRoot, DocumentFragment)
    1.68 +NS_IMPL_RELEASE_INHERITED(ShadowRoot, DocumentFragment)
    1.69 +
    1.70 +ShadowRoot::ShadowRoot(nsIContent* aContent,
    1.71 +                       already_AddRefed<nsINodeInfo>&& aNodeInfo,
    1.72 +                       nsXBLPrototypeBinding* aProtoBinding)
    1.73 +  : DocumentFragment(aNodeInfo), mPoolHost(aContent),
    1.74 +    mProtoBinding(aProtoBinding), mShadowElement(nullptr),
    1.75 +    mInsertionPointChanged(false)
    1.76 +{
    1.77 +  SetHost(aContent);
    1.78 +  SetFlags(NODE_IS_IN_SHADOW_TREE);
    1.79 +  // ShadowRoot isn't really in the document but it behaves like it is.
    1.80 +  SetInDocument();
    1.81 +  DOMSlots()->mBindingParent = aContent;
    1.82 +  DOMSlots()->mContainingShadow = this;
    1.83 +
    1.84 +  // Add the ShadowRoot as a mutation observer on the host to watch
    1.85 +  // for mutations because the insertion points in this ShadowRoot
    1.86 +  // may need to be updated when the host children are modified.
    1.87 +  mPoolHost->AddMutationObserver(this);
    1.88 +}
    1.89 +
    1.90 +ShadowRoot::~ShadowRoot()
    1.91 +{
    1.92 +  if (mPoolHost) {
    1.93 +    // mPoolHost may have been unlinked or a new ShadowRoot may have been
    1.94 +    // creating, making this one obsolete.
    1.95 +    mPoolHost->RemoveMutationObserver(this);
    1.96 +  }
    1.97 +
    1.98 +  ClearInDocument();
    1.99 +  SetHost(nullptr);
   1.100 +}
   1.101 +
   1.102 +JSObject*
   1.103 +ShadowRoot::WrapObject(JSContext* aCx)
   1.104 +{
   1.105 +  return mozilla::dom::ShadowRootBinding::Wrap(aCx, this);
   1.106 +}
   1.107 +
   1.108 +ShadowRoot*
   1.109 +ShadowRoot::FromNode(nsINode* aNode)
   1.110 +{
   1.111 +  if (aNode->HasFlag(NODE_IS_IN_SHADOW_TREE) && !aNode->GetParentNode()) {
   1.112 +    MOZ_ASSERT(aNode->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE,
   1.113 +               "ShadowRoot is a document fragment.");
   1.114 +    return static_cast<ShadowRoot*>(aNode);
   1.115 +  }
   1.116 +
   1.117 +  return nullptr;
   1.118 +}
   1.119 +
   1.120 +void
   1.121 +ShadowRoot::Restyle()
   1.122 +{
   1.123 +  mProtoBinding->FlushSkinSheets();
   1.124 +
   1.125 +  nsIPresShell* shell = OwnerDoc()->GetShell();
   1.126 +  if (shell) {
   1.127 +    OwnerDoc()->BeginUpdate(UPDATE_STYLE);
   1.128 +    shell->RestyleShadowRoot(this);
   1.129 +    OwnerDoc()->EndUpdate(UPDATE_STYLE);
   1.130 +  }
   1.131 +}
   1.132 +
   1.133 +void
   1.134 +ShadowRoot::InsertSheet(nsCSSStyleSheet* aSheet,
   1.135 +                        nsIContent* aLinkingContent)
   1.136 +{
   1.137 +  nsCOMPtr<nsIStyleSheetLinkingElement>
   1.138 +    linkingElement = do_QueryInterface(aLinkingContent);
   1.139 +  MOZ_ASSERT(linkingElement, "The only styles in a ShadowRoot should come "
   1.140 +                             "from <style>.");
   1.141 +
   1.142 +  linkingElement->SetStyleSheet(aSheet); // This sets the ownerNode on the sheet
   1.143 +
   1.144 +  nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
   1.145 +    mProtoBinding->GetOrCreateStyleSheets();
   1.146 +  MOZ_ASSERT(sheets, "Style sheets array should never be null.");
   1.147 +
   1.148 +  // Find the correct position to insert into the style sheet list (must
   1.149 +  // be in tree order).
   1.150 +  for (uint32_t i = 0; i <= sheets->Length(); i++) {
   1.151 +    if (i == sheets->Length()) {
   1.152 +      sheets->AppendElement(aSheet);
   1.153 +      break;
   1.154 +    }
   1.155 +
   1.156 +    nsINode* sheetOwnerNode = sheets->ElementAt(i)->GetOwnerNode();
   1.157 +    if (nsContentUtils::PositionIsBefore(aLinkingContent, sheetOwnerNode)) {
   1.158 +      sheets->InsertElementAt(i, aSheet);
   1.159 +      break;
   1.160 +    }
   1.161 +  }
   1.162 +
   1.163 +  Restyle();
   1.164 +}
   1.165 +
   1.166 +void
   1.167 +ShadowRoot::RemoveSheet(nsCSSStyleSheet* aSheet)
   1.168 +{
   1.169 +  nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
   1.170 +    mProtoBinding->GetOrCreateStyleSheets();
   1.171 +  MOZ_ASSERT(sheets, "Style sheets array should never be null.");
   1.172 +
   1.173 +  DebugOnly<bool> found = sheets->RemoveElement(aSheet);
   1.174 +  MOZ_ASSERT(found, "Trying to remove a sheet from a ShadowRoot "
   1.175 +                    "that does not exist.");
   1.176 +
   1.177 +  Restyle();
   1.178 +}
   1.179 +
   1.180 +Element*
   1.181 +ShadowRoot::GetElementById(const nsAString& aElementId)
   1.182 +{
   1.183 +  nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(aElementId);
   1.184 +  return entry ? entry->GetIdElement() : nullptr;
   1.185 +}
   1.186 +
   1.187 +already_AddRefed<nsContentList>
   1.188 +ShadowRoot::GetElementsByTagName(const nsAString& aTagName)
   1.189 +{
   1.190 +  return NS_GetContentList(this, kNameSpaceID_Unknown, aTagName);
   1.191 +}
   1.192 +
   1.193 +already_AddRefed<nsContentList>
   1.194 +ShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
   1.195 +                                   const nsAString& aLocalName)
   1.196 +{
   1.197 +  int32_t nameSpaceId = kNameSpaceID_Wildcard;
   1.198 +
   1.199 +  if (!aNamespaceURI.EqualsLiteral("*")) {
   1.200 +    nsresult rv =
   1.201 +      nsContentUtils::NameSpaceManager()->RegisterNameSpace(aNamespaceURI,
   1.202 +                                                            nameSpaceId);
   1.203 +    NS_ENSURE_SUCCESS(rv, nullptr);
   1.204 +  }
   1.205 +
   1.206 +  NS_ASSERTION(nameSpaceId != kNameSpaceID_Unknown, "Unexpected namespace ID!");
   1.207 +
   1.208 +  return NS_GetContentList(this, nameSpaceId, aLocalName);
   1.209 +}
   1.210 +
   1.211 +void
   1.212 +ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
   1.213 +{
   1.214 +  nsIdentifierMapEntry *entry =
   1.215 +    mIdentifierMap.PutEntry(nsDependentAtomString(aId));
   1.216 +  if (entry) {
   1.217 +    entry->AddIdElement(aElement);
   1.218 +  }
   1.219 +}
   1.220 +
   1.221 +void
   1.222 +ShadowRoot::RemoveFromIdTable(Element* aElement, nsIAtom* aId)
   1.223 +{
   1.224 +  nsIdentifierMapEntry *entry =
   1.225 +    mIdentifierMap.GetEntry(nsDependentAtomString(aId));
   1.226 +  if (entry) {
   1.227 +    entry->RemoveIdElement(aElement);
   1.228 +    if (entry->IsEmpty()) {
   1.229 +      mIdentifierMap.RawRemoveEntry(entry);
   1.230 +    }
   1.231 +  }
   1.232 +}
   1.233 +
   1.234 +already_AddRefed<nsContentList>
   1.235 +ShadowRoot::GetElementsByClassName(const nsAString& aClasses)
   1.236 +{
   1.237 +  return nsContentUtils::GetElementsByClassName(this, aClasses);
   1.238 +}
   1.239 +
   1.240 +void
   1.241 +ShadowRoot::AddInsertionPoint(HTMLContentElement* aInsertionPoint)
   1.242 +{
   1.243 +  TreeOrderComparator comparator;
   1.244 +  mInsertionPoints.InsertElementSorted(aInsertionPoint, comparator);
   1.245 +}
   1.246 +
   1.247 +void
   1.248 +ShadowRoot::RemoveInsertionPoint(HTMLContentElement* aInsertionPoint)
   1.249 +{
   1.250 +  mInsertionPoints.RemoveElement(aInsertionPoint);
   1.251 +}
   1.252 +
   1.253 +void
   1.254 +ShadowRoot::SetYoungerShadow(ShadowRoot* aYoungerShadow)
   1.255 +{
   1.256 +  mYoungerShadow = aYoungerShadow;
   1.257 +  mYoungerShadow->mOlderShadow = this;
   1.258 +
   1.259 +  ChangePoolHost(mYoungerShadow->GetShadowElement());
   1.260 +}
   1.261 +
   1.262 +void
   1.263 +ShadowRoot::DistributeSingleNode(nsIContent* aContent)
   1.264 +{
   1.265 +  // Find the insertion point to which the content belongs.
   1.266 +  HTMLContentElement* insertionPoint = nullptr;
   1.267 +  for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
   1.268 +    if (mInsertionPoints[i]->Match(aContent)) {
   1.269 +      if (mInsertionPoints[i]->MatchedNodes().Contains(aContent)) {
   1.270 +        // Node is already matched into the insertion point. We are done.
   1.271 +        return;
   1.272 +      }
   1.273 +
   1.274 +      // Matching may cause the insertion point to drop fallback content.
   1.275 +      if (mInsertionPoints[i]->MatchedNodes().IsEmpty() &&
   1.276 +          static_cast<nsINode*>(mInsertionPoints[i])->GetFirstChild()) {
   1.277 +        // This match will cause the insertion point to drop all fallback
   1.278 +        // content and used matched nodes instead. Give up on the optimization
   1.279 +        // and just distribute all nodes.
   1.280 +        DistributeAllNodes();
   1.281 +        return;
   1.282 +      }
   1.283 +      insertionPoint = mInsertionPoints[i];
   1.284 +      break;
   1.285 +    }
   1.286 +  }
   1.287 +
   1.288 +  // Find the index into the insertion point.
   1.289 +  if (insertionPoint) {
   1.290 +    nsCOMArray<nsIContent>& matchedNodes = insertionPoint->MatchedNodes();
   1.291 +    // Find the appropriate position in the matched node list for the
   1.292 +    // newly distributed content.
   1.293 +    bool isIndexFound = false;
   1.294 +    MOZ_ASSERT(mPoolHost, "Where did the content come from if there is no pool host?");
   1.295 +    ExplicitChildIterator childIterator(mPoolHost);
   1.296 +    for (uint32_t i = 0; i < matchedNodes.Length(); i++) {
   1.297 +      // Seek through the host's explicit children until the inserted content
   1.298 +      // is found or when the current matched node is reached.
   1.299 +      if (childIterator.Seek(aContent, matchedNodes[i])) {
   1.300 +        // aContent was found before the current matched node.
   1.301 +        matchedNodes.InsertElementAt(i, aContent);
   1.302 +        isIndexFound = true;
   1.303 +        break;
   1.304 +      }
   1.305 +    }
   1.306 +
   1.307 +    if (!isIndexFound) {
   1.308 +      // We have still not found an index in the insertion point,
   1.309 +      // thus it must be at the end.
   1.310 +      MOZ_ASSERT(childIterator.Seek(aContent),
   1.311 +                 "Trying to match a node that is not a candidate to be matched");
   1.312 +      matchedNodes.AppendElement(aContent);
   1.313 +    }
   1.314 +
   1.315 +    // Handle the case where the parent of the insertion point is a ShadowRoot
   1.316 +    // that is projected into the younger ShadowRoot's shadow insertion point.
   1.317 +    // The node distributed into the insertion point must be reprojected
   1.318 +    // to the shadow insertion point.
   1.319 +    if (insertionPoint->GetParent() == this &&
   1.320 +        mYoungerShadow && mYoungerShadow->GetShadowElement()) {
   1.321 +      mYoungerShadow->GetShadowElement()->DistributeSingleNode(aContent);
   1.322 +    }
   1.323 +
   1.324 +    // Handle the case where the parent of the insertion point has a ShadowRoot.
   1.325 +    // The node distributed into the insertion point must be reprojected to the
   1.326 +    // insertion points of the parent's ShadowRoot.
   1.327 +    ShadowRoot* parentShadow = insertionPoint->GetParent()->GetShadowRoot();
   1.328 +    if (parentShadow) {
   1.329 +      parentShadow->DistributeSingleNode(aContent);
   1.330 +    }
   1.331 +
   1.332 +    // Handle the case where the parent of the insertion point is the <shadow>
   1.333 +    // element. The node distributed into the insertion point must be reprojected
   1.334 +    // into the older ShadowRoot's insertion points.
   1.335 +    if (mShadowElement && mShadowElement == insertionPoint->GetParent()) {
   1.336 +      ShadowRoot* olderShadow = mShadowElement->GetOlderShadowRoot();
   1.337 +      if (olderShadow) {
   1.338 +        olderShadow->DistributeSingleNode(aContent);
   1.339 +      }
   1.340 +    }
   1.341 +  }
   1.342 +}
   1.343 +
   1.344 +void
   1.345 +ShadowRoot::RemoveDistributedNode(nsIContent* aContent)
   1.346 +{
   1.347 +  // Find insertion point containing the content and remove the node.
   1.348 +  for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
   1.349 +    if (mInsertionPoints[i]->MatchedNodes().Contains(aContent)) {
   1.350 +      // Removing the matched node may cause the insertion point to use
   1.351 +      // fallback content.
   1.352 +      if (mInsertionPoints[i]->MatchedNodes().Length() == 1 &&
   1.353 +          static_cast<nsINode*>(mInsertionPoints[i])->GetFirstChild()) {
   1.354 +        // Removing the matched node will cause fallback content to be
   1.355 +        // used instead. Give up optimization and distribute all nodes.
   1.356 +        DistributeAllNodes();
   1.357 +        return;
   1.358 +      }
   1.359 +
   1.360 +      mInsertionPoints[i]->MatchedNodes().RemoveElement(aContent);
   1.361 +
   1.362 +      // Handle the case where the parent of the insertion point is a ShadowRoot
   1.363 +      // that is projected into the younger ShadowRoot's shadow insertion point.
   1.364 +      // The removed node needs to be removed from the shadow insertion point.
   1.365 +      if (mInsertionPoints[i]->GetParent() == this) {
   1.366 +        if (mYoungerShadow && mYoungerShadow->GetShadowElement()) {
   1.367 +          mYoungerShadow->GetShadowElement()->RemoveDistributedNode(aContent);
   1.368 +        }
   1.369 +      }
   1.370 +
   1.371 +      // Handle the case where the parent of the insertion point has a ShadowRoot.
   1.372 +      // The removed node needs to be removed from the insertion points of the
   1.373 +      // parent's ShadowRoot.
   1.374 +      ShadowRoot* parentShadow = mInsertionPoints[i]->GetParent()->GetShadowRoot();
   1.375 +      if (parentShadow) {
   1.376 +        parentShadow->RemoveDistributedNode(aContent);
   1.377 +      }
   1.378 +
   1.379 +      // Handle the case where the parent of the insertion point is the <shadow>
   1.380 +      // element. The removed node must be removed from the older ShadowRoot's
   1.381 +      // insertion points.
   1.382 +      if (mShadowElement && mShadowElement == mInsertionPoints[i]->GetParent()) {
   1.383 +        ShadowRoot* olderShadow = mShadowElement->GetOlderShadowRoot();
   1.384 +        if (olderShadow) {
   1.385 +          olderShadow->RemoveDistributedNode(aContent);
   1.386 +        }
   1.387 +      }
   1.388 +
   1.389 +      break;
   1.390 +    }
   1.391 +  }
   1.392 +}
   1.393 +
   1.394 +void
   1.395 +ShadowRoot::DistributeAllNodes()
   1.396 +{
   1.397 +  // Create node pool.
   1.398 +  nsTArray<nsIContent*> nodePool;
   1.399 +
   1.400 +  // Make sure there is a pool host, an older shadow may not have
   1.401 +  // one if the younger shadow does not have a <shadow> element.
   1.402 +  if (mPoolHost) {
   1.403 +    ExplicitChildIterator childIterator(mPoolHost);
   1.404 +    for (nsIContent* content = childIterator.GetNextChild();
   1.405 +         content;
   1.406 +         content = childIterator.GetNextChild()) {
   1.407 +      nodePool.AppendElement(content);
   1.408 +    }
   1.409 +  }
   1.410 +
   1.411 +  nsTArray<ShadowRoot*> shadowsToUpdate;
   1.412 +
   1.413 +  for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
   1.414 +    mInsertionPoints[i]->ClearMatchedNodes();
   1.415 +    // Assign matching nodes from node pool.
   1.416 +    for (uint32_t j = 0; j < nodePool.Length(); j++) {
   1.417 +      if (mInsertionPoints[i]->Match(nodePool[j])) {
   1.418 +        mInsertionPoints[i]->MatchedNodes().AppendElement(nodePool[j]);
   1.419 +        nodePool[j]->SetXBLInsertionParent(mInsertionPoints[i]);
   1.420 +        nodePool.RemoveElementAt(j--);
   1.421 +      }
   1.422 +    }
   1.423 +
   1.424 +    // Keep track of instances where the content insertion point is distributed
   1.425 +    // (parent of insertion point has a ShadowRoot).
   1.426 +    nsIContent* insertionParent = mInsertionPoints[i]->GetParent();
   1.427 +    MOZ_ASSERT(insertionParent, "The only way for an insertion point to be in the"
   1.428 +                                "mInsertionPoints array is to be a descendant of a"
   1.429 +                                "ShadowRoot, in which case, it should have a parent");
   1.430 +
   1.431 +    // If the parent of the insertion point has as ShadowRoot, the nodes distributed
   1.432 +    // to the insertion point must be reprojected to the insertion points of the
   1.433 +    // parent's ShadowRoot.
   1.434 +    ShadowRoot* parentShadow = insertionParent->GetShadowRoot();
   1.435 +    if (parentShadow && !shadowsToUpdate.Contains(parentShadow)) {
   1.436 +      shadowsToUpdate.AppendElement(parentShadow);
   1.437 +    }
   1.438 +  }
   1.439 +
   1.440 +  // If there is a shadow insertion point in this ShadowRoot, the children
   1.441 +  // of the shadow insertion point needs to be distributed into the insertion
   1.442 +  // points of the older ShadowRoot.
   1.443 +  if (mShadowElement && mOlderShadow) {
   1.444 +    mOlderShadow->DistributeAllNodes();
   1.445 +  }
   1.446 +
   1.447 +  // If there is a younger ShadowRoot with a shadow insertion point,
   1.448 +  // then the children of this ShadowRoot needs to be distributed to
   1.449 +  // the younger ShadowRoot's shadow insertion point.
   1.450 +  if (mYoungerShadow && mYoungerShadow->GetShadowElement()) {
   1.451 +    mYoungerShadow->GetShadowElement()->DistributeAllNodes();
   1.452 +  }
   1.453 +
   1.454 +  for (uint32_t i = 0; i < shadowsToUpdate.Length(); i++) {
   1.455 +    shadowsToUpdate[i]->DistributeAllNodes();
   1.456 +  }
   1.457 +}
   1.458 +
   1.459 +void
   1.460 +ShadowRoot::GetInnerHTML(nsAString& aInnerHTML)
   1.461 +{
   1.462 +  GetMarkup(false, aInnerHTML);
   1.463 +}
   1.464 +
   1.465 +void
   1.466 +ShadowRoot::SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError)
   1.467 +{
   1.468 +  SetInnerHTMLInternal(aInnerHTML, aError);
   1.469 +}
   1.470 +
   1.471 +bool
   1.472 +ShadowRoot::ApplyAuthorStyles()
   1.473 +{
   1.474 +  return mProtoBinding->InheritsStyle();
   1.475 +}
   1.476 +
   1.477 +void
   1.478 +ShadowRoot::SetApplyAuthorStyles(bool aApplyAuthorStyles)
   1.479 +{
   1.480 +  mProtoBinding->SetInheritsStyle(aApplyAuthorStyles);
   1.481 +
   1.482 +  nsIPresShell* shell = OwnerDoc()->GetShell();
   1.483 +  if (shell) {
   1.484 +    OwnerDoc()->BeginUpdate(UPDATE_STYLE);
   1.485 +    shell->RestyleShadowRoot(this);
   1.486 +    OwnerDoc()->EndUpdate(UPDATE_STYLE);
   1.487 +  }
   1.488 +}
   1.489 +
   1.490 +StyleSheetList*
   1.491 +ShadowRoot::StyleSheets()
   1.492 +{
   1.493 +  if (!mStyleSheetList) {
   1.494 +    mStyleSheetList = new ShadowRootStyleSheetList(this);
   1.495 +  }
   1.496 +
   1.497 +  return mStyleSheetList;
   1.498 +}
   1.499 +
   1.500 +void
   1.501 +ShadowRoot::SetShadowElement(HTMLShadowElement* aShadowElement)
   1.502 +{
   1.503 +  // If there is already a shadow element point, remove
   1.504 +  // the projected shadow because it is no longer an insertion
   1.505 +  // point.
   1.506 +  if (mShadowElement) {
   1.507 +    mShadowElement->SetProjectedShadow(nullptr);
   1.508 +  }
   1.509 +
   1.510 +  if (mOlderShadow) {
   1.511 +    // Nodes for distribution will come from the new shadow element.
   1.512 +    mOlderShadow->ChangePoolHost(aShadowElement);
   1.513 +  }
   1.514 +
   1.515 +  // Set the new shadow element to project the older ShadowRoot because
   1.516 +  // it is the current shadow insertion point.
   1.517 +  mShadowElement = aShadowElement;
   1.518 +  if (mShadowElement) {
   1.519 +    mShadowElement->SetProjectedShadow(mOlderShadow);
   1.520 +  }
   1.521 +}
   1.522 +
   1.523 +void
   1.524 +ShadowRoot::ChangePoolHost(nsIContent* aNewHost)
   1.525 +{
   1.526 +  if (mPoolHost) {
   1.527 +    mPoolHost->RemoveMutationObserver(this);
   1.528 +  }
   1.529 +
   1.530 +  // Clear the nodes matched to content insertion points
   1.531 +  // because it is no longer relevant.
   1.532 +  for (uint32_t i = 0; i < mInsertionPoints.Length(); i++) {
   1.533 +    mInsertionPoints[i]->ClearMatchedNodes();
   1.534 +  }
   1.535 +
   1.536 +  mPoolHost = aNewHost;
   1.537 +  if (mPoolHost) {
   1.538 +    mPoolHost->AddMutationObserver(this);
   1.539 +  }
   1.540 +}
   1.541 +
   1.542 +bool
   1.543 +ShadowRoot::IsShadowInsertionPoint(nsIContent* aContent)
   1.544 +{
   1.545 +  if (aContent && aContent->IsHTML(nsGkAtoms::shadow)) {
   1.546 +    HTMLShadowElement* shadowElem = static_cast<HTMLShadowElement*>(aContent);
   1.547 +    return shadowElem->IsInsertionPoint();
   1.548 +  }
   1.549 +  return false;
   1.550 +}
   1.551 +
   1.552 +/**
   1.553 + * Returns whether the web components pool population algorithm
   1.554 + * on the host would contain |aContent|. This function ignores
   1.555 + * insertion points in the pool, thus should only be used to
   1.556 + * test nodes that have not yet been distributed.
   1.557 + */
   1.558 +bool
   1.559 +ShadowRoot::IsPooledNode(nsIContent* aContent, nsIContent* aContainer,
   1.560 +                         nsIContent* aHost)
   1.561 +{
   1.562 +  if (nsContentUtils::IsContentInsertionPoint(aContent) ||
   1.563 +      IsShadowInsertionPoint(aContent)) {
   1.564 +    // Insertion points never end up in the pool.
   1.565 +    return false;
   1.566 +  }
   1.567 +
   1.568 +  if (aContainer->IsHTML(nsGkAtoms::content)) {
   1.569 +    // Fallback content will end up in pool if its parent is a child of the host.
   1.570 +    HTMLContentElement* content = static_cast<HTMLContentElement*>(aContainer);
   1.571 +    return content->IsInsertionPoint() && content->MatchedNodes().IsEmpty() &&
   1.572 +           aContainer->GetParentNode() == aHost;
   1.573 +  }
   1.574 +
   1.575 +  if (aContainer == aHost) {
   1.576 +    // Any other child nodes of the host will end up in the pool.
   1.577 +    return true;
   1.578 +  }
   1.579 +
   1.580 +  return false;
   1.581 +}
   1.582 +
   1.583 +void
   1.584 +ShadowRoot::AttributeChanged(nsIDocument* aDocument,
   1.585 +                             Element* aElement,
   1.586 +                             int32_t aNameSpaceID,
   1.587 +                             nsIAtom* aAttribute,
   1.588 +                             int32_t aModType)
   1.589 +{
   1.590 +  if (!IsPooledNode(aElement, aElement->GetParent(), mPoolHost)) {
   1.591 +    return;
   1.592 +  }
   1.593 +
   1.594 +  // Attributes may change insertion point matching, find its new distribution.
   1.595 +  RemoveDistributedNode(aElement);
   1.596 +  DistributeSingleNode(aElement);
   1.597 +}
   1.598 +
   1.599 +void
   1.600 +ShadowRoot::ContentAppended(nsIDocument* aDocument,
   1.601 +                            nsIContent* aContainer,
   1.602 +                            nsIContent* aFirstNewContent,
   1.603 +                            int32_t aNewIndexInContainer)
   1.604 +{
   1.605 +  if (mInsertionPointChanged) {
   1.606 +    DistributeAllNodes();
   1.607 +    mInsertionPointChanged = false;
   1.608 +    return;
   1.609 +  }
   1.610 +
   1.611 +  // Watch for new nodes added to the pool because the node
   1.612 +  // may need to be added to an insertion point.
   1.613 +  nsIContent* currentChild = aFirstNewContent;
   1.614 +  while (currentChild) {
   1.615 +    if (IsPooledNode(currentChild, aContainer, mPoolHost)) {
   1.616 +      DistributeSingleNode(currentChild);
   1.617 +    }
   1.618 +    currentChild = currentChild->GetNextSibling();
   1.619 +  }
   1.620 +}
   1.621 +
   1.622 +void
   1.623 +ShadowRoot::ContentInserted(nsIDocument* aDocument,
   1.624 +                            nsIContent* aContainer,
   1.625 +                            nsIContent* aChild,
   1.626 +                            int32_t aIndexInContainer)
   1.627 +{
   1.628 +  if (mInsertionPointChanged) {
   1.629 +    DistributeAllNodes();
   1.630 +    mInsertionPointChanged = false;
   1.631 +    return;
   1.632 +  }
   1.633 +
   1.634 +  // Watch for new nodes added to the pool because the node
   1.635 +  // may need to be added to an insertion point.
   1.636 +  if (IsPooledNode(aChild, aContainer, mPoolHost)) {
   1.637 +    DistributeSingleNode(aChild);
   1.638 +  }
   1.639 +}
   1.640 +
   1.641 +void
   1.642 +ShadowRoot::ContentRemoved(nsIDocument* aDocument,
   1.643 +                           nsIContent* aContainer,
   1.644 +                           nsIContent* aChild,
   1.645 +                           int32_t aIndexInContainer,
   1.646 +                           nsIContent* aPreviousSibling)
   1.647 +{
   1.648 +  if (mInsertionPointChanged) {
   1.649 +    DistributeAllNodes();
   1.650 +    mInsertionPointChanged = false;
   1.651 +    return;
   1.652 +  }
   1.653 +
   1.654 +  // Watch for node that is removed from the pool because
   1.655 +  // it may need to be removed from an insertion point.
   1.656 +  if (IsPooledNode(aChild, aContainer, mPoolHost)) {
   1.657 +    RemoveDistributedNode(aChild);
   1.658 +  }
   1.659 +}
   1.660 +
   1.661 +NS_IMPL_CYCLE_COLLECTION_INHERITED(ShadowRootStyleSheetList, StyleSheetList,
   1.662 +                                   mShadowRoot)
   1.663 +
   1.664 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ShadowRootStyleSheetList)
   1.665 +NS_INTERFACE_MAP_END_INHERITING(StyleSheetList)
   1.666 +
   1.667 +NS_IMPL_ADDREF_INHERITED(ShadowRootStyleSheetList, StyleSheetList)
   1.668 +NS_IMPL_RELEASE_INHERITED(ShadowRootStyleSheetList, StyleSheetList)
   1.669 +
   1.670 +ShadowRootStyleSheetList::ShadowRootStyleSheetList(ShadowRoot* aShadowRoot)
   1.671 +  : mShadowRoot(aShadowRoot)
   1.672 +{
   1.673 +  MOZ_COUNT_CTOR(ShadowRootStyleSheetList);
   1.674 +}
   1.675 +
   1.676 +ShadowRootStyleSheetList::~ShadowRootStyleSheetList()
   1.677 +{
   1.678 +  MOZ_COUNT_DTOR(ShadowRootStyleSheetList);
   1.679 +}
   1.680 +
   1.681 +nsCSSStyleSheet*
   1.682 +ShadowRootStyleSheetList::IndexedGetter(uint32_t aIndex, bool& aFound)
   1.683 +{
   1.684 +  nsTArray<nsRefPtr<nsCSSStyleSheet>>* sheets =
   1.685 +    mShadowRoot->mProtoBinding->GetStyleSheets();
   1.686 +
   1.687 +  if (!sheets) {
   1.688 +    aFound = false;
   1.689 +    return nullptr;
   1.690 +  }
   1.691 +
   1.692 +  aFound = aIndex < sheets->Length();
   1.693 +  return sheets->SafeElementAt(aIndex);
   1.694 +}
   1.695 +
   1.696 +uint32_t
   1.697 +ShadowRootStyleSheetList::Length()
   1.698 +{
   1.699 +  nsTArray<nsRefPtr<nsCSSStyleSheet> >* sheets =
   1.700 +    mShadowRoot->mProtoBinding->GetStyleSheets();
   1.701 +
   1.702 +  if (!sheets) {
   1.703 +    return 0;
   1.704 +  }
   1.705 +
   1.706 +  return sheets->Length();
   1.707 +}
   1.708 +

mercurial