2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "ShadowRoot.h"
32 #include "NodeRareData.h"
33 #include "ShadowContentElement.h"
34 #include "ShadowInclusionSelector.h"
39 ShadowRoot::ShadowRoot(Document* document)
41 , m_applyAuthorSheets(false)
45 // Assume document as parent scope.
46 setParentTreeScope(document);
47 // Shadow tree scopes have the scope pointer point to themselves.
48 // This way, direct children will receive the correct scope pointer.
49 ensureRareData()->setTreeScope(this);
52 ShadowRoot::~ShadowRoot()
56 String ShadowRoot::nodeName() const
58 return "#shadow-root";
61 Node::NodeType ShadowRoot::nodeType() const
63 return SHADOW_ROOT_NODE;
66 PassRefPtr<Node> ShadowRoot::cloneNode(bool)
68 // ShadowRoot should not be arbitrarily cloned.
72 bool ShadowRoot::childTypeAllowed(NodeType type) const
76 case PROCESSING_INSTRUCTION_NODE:
79 case CDATA_SECTION_NODE:
80 case ENTITY_REFERENCE_NODE:
87 void ShadowRoot::recalcShadowTreeStyle(StyleChange change)
89 if (hasContentElement())
92 for (Node* n = firstChild(); n; n = n->nextSibling()) {
93 if (n->isElementNode())
94 static_cast<Element*>(n)->recalcStyle(change);
95 else if (n->isTextNode())
96 static_cast<Text*>(n)->recalcTextStyle(change);
100 clearNeedsStyleRecalc();
101 clearChildNeedsStyleRecalc();
104 ShadowContentElement* ShadowRoot::includerFor(Node* node) const
108 ShadowInclusion* found = m_inclusions->findFor(node);
111 return found->includer();
114 void ShadowRoot::hostChildrenChanged()
116 if (!hasContentElement())
118 // This results in forced detaching/attaching of the shadow render tree. See ShadowRoot::recalcStyle().
119 setNeedsStyleRecalc();
122 bool ShadowRoot::hasContentElement() const
124 for (Node* n = firstChild(); n; n = n->traverseNextNode(this)) {
125 if (n->isContentElement())
132 bool ShadowRoot::applyAuthorSheets() const
134 return m_applyAuthorSheets;
137 void ShadowRoot::setApplyAuthorSheets(bool value)
139 m_applyAuthorSheets = value;
142 void ShadowRoot::attach()
144 // Children of m_inclusions is populated lazily in
145 // ensureInclusions(), and here we just ensure that
146 // it is in clean state.
147 ASSERT(!m_inclusions || !m_inclusions->hasCandidates());
150 m_inclusions->didSelect();
153 ShadowInclusionSelector* ShadowRoot::inclusions() const
155 return m_inclusions.get();
158 ShadowInclusionSelector* ShadowRoot::ensureInclusions()
161 m_inclusions = adoptPtr(new ShadowInclusionSelector());
162 m_inclusions->willSelectOver(this);
163 return m_inclusions.get();