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"
30 #include "ContentDistributor.h"
31 #include "DOMSelection.h"
32 #include "DOMWindow.h"
34 #include "DocumentFragment.h"
36 #include "ElementShadow.h"
37 #include "HTMLContentElement.h"
38 #include "HTMLNames.h"
39 #include "InsertionPoint.h"
40 #include "NodeRareData.h"
42 #include "StyleResolver.h"
47 ShadowRoot::ShadowRoot(Document* document)
48 : DocumentFragment(document, CreateShadowRoot)
52 , m_applyAuthorStyles(false)
53 , m_resetStyleInheritance(false)
54 , m_insertionPointAssignedTo(0)
58 // Assume document as parent scope.
59 setParentTreeScope(document);
60 // Shadow tree scopes have the scope pointer point to themselves.
61 // This way, direct children will receive the correct scope pointer.
62 ensureRareData()->setTreeScope(this);
65 ShadowRoot::~ShadowRoot()
70 // We must call clearRareData() here since a ShadowRoot class inherits TreeScope
71 // as well as Node. See a comment on TreeScope.h for the reason.
76 static bool allowsAuthorShadowRoot(Element* element)
78 // FIXME: ValidationMessage recreates shadow root dynamically.
79 // https://bugs.webkit.org/show_bug.cgi?id=77937
80 // Especially, INPUT recreates shadow root dynamically.
81 // https://bugs.webkit.org/show_bug.cgi?id=77930
82 if (element->isFormControlElement())
85 // FIXME: We disable multiple shadow subtrees for SVG for while, because there will be problems to support it.
86 // https://bugs.webkit.org/show_bug.cgi?id=78205
87 // Especially SVG TREF recreates shadow root dynamically.
88 // https://bugs.webkit.org/show_bug.cgi?id=77938
89 if (element->isSVGElement())
95 PassRefPtr<ShadowRoot> ShadowRoot::create(Element* element, ExceptionCode& ec)
97 return create(element, AuthorShadowRoot, ec);
100 PassRefPtr<ShadowRoot> ShadowRoot::create(Element* element, ShadowRootType type, ExceptionCode& ec)
103 ec = HIERARCHY_REQUEST_ERR;
107 // Since some elements recreates shadow root dynamically, multiple shadow subtrees won't work well in that element.
108 // Until they are fixed, we disable adding author shadow root for them.
109 if (type == AuthorShadowRoot && !allowsAuthorShadowRoot(element)) {
110 ec = HIERARCHY_REQUEST_ERR;
114 RefPtr<ShadowRoot> shadowRoot = adoptRef(new ShadowRoot(element->document()));
116 shadowRoot->m_type = type;
120 element->ensureShadow()->addShadowRoot(element, shadowRoot, type, ec);
123 ASSERT(element == shadowRoot->host());
124 ASSERT(element->shadow());
125 return shadowRoot.release();
128 String ShadowRoot::nodeName() const
130 return "#shadow-root";
133 PassRefPtr<Node> ShadowRoot::cloneNode(bool)
135 // ShadowRoot should not be arbitrarily cloned.
139 String ShadowRoot::innerHTML() const
141 return createMarkup(this, ChildrenOnly);
144 void ShadowRoot::setInnerHTML(const String& markup, ExceptionCode& ec)
146 if (RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, host(), AllowScriptingContent, ec))
147 replaceChildrenWithFragment(this, fragment.release(), ec);
150 bool ShadowRoot::childTypeAllowed(NodeType type) const
154 case PROCESSING_INSTRUCTION_NODE:
157 case CDATA_SECTION_NODE:
158 case ENTITY_REFERENCE_NODE:
165 ElementShadow* ShadowRoot::owner() const
168 return host()->shadow();
172 bool ShadowRoot::hasInsertionPoint() const
174 for (Node* n = firstChild(); n; n = n->traverseNextNode(this)) {
175 if (isInsertionPoint(n))
182 bool ShadowRoot::applyAuthorStyles() const
184 return m_applyAuthorStyles;
187 void ShadowRoot::setApplyAuthorStyles(bool value)
189 if (m_applyAuthorStyles != value) {
190 m_applyAuthorStyles = value;
191 host()->setNeedsStyleRecalc();
195 bool ShadowRoot::resetStyleInheritance() const
197 return m_resetStyleInheritance;
200 void ShadowRoot::setResetStyleInheritance(bool value)
202 if (value != m_resetStyleInheritance) {
203 m_resetStyleInheritance = value;
204 if (attached() && owner())
205 owner()->recalcStyle(Force);
209 void ShadowRoot::attach()
211 StyleResolver* styleResolver = document()->styleResolver();
212 styleResolver->pushParentShadowRoot(this);
213 attachChildrenIfNeeded();
215 styleResolver->popParentShadowRoot(this);
218 void ShadowRoot::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
220 ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
221 owner()->invalidateDistribution();