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)
49 , TreeScope(this, document)
52 , m_applyAuthorStyles(false)
53 , m_resetStyleInheritance(false)
54 , m_insertionPointAssignedTo(0)
58 // Shadow tree scopes have the scope pointer point to themselves.
59 // This way, direct children will receive the correct scope pointer.
63 ShadowRoot::~ShadowRoot()
68 // We must call clearRareData() here since a ShadowRoot class inherits TreeScope
69 // as well as Node. See a comment on TreeScope.h for the reason.
74 static bool allowsAuthorShadowRoot(Element* element)
76 // FIXME: ValidationMessage recreates shadow root dynamically.
77 // https://bugs.webkit.org/show_bug.cgi?id=77937
78 // Especially, INPUT recreates shadow root dynamically.
79 // https://bugs.webkit.org/show_bug.cgi?id=77930
80 if (element->isFormControlElement())
83 // FIXME: We disable multiple shadow subtrees for SVG for while, because there will be problems to support it.
84 // https://bugs.webkit.org/show_bug.cgi?id=78205
85 // Especially SVG TREF recreates shadow root dynamically.
86 // https://bugs.webkit.org/show_bug.cgi?id=77938
87 if (element->isSVGElement())
93 PassRefPtr<ShadowRoot> ShadowRoot::create(Element* element, ExceptionCode& ec)
95 return create(element, AuthorShadowRoot, ec);
98 PassRefPtr<ShadowRoot> ShadowRoot::create(Element* element, ShadowRootType type, ExceptionCode& ec)
101 ec = HIERARCHY_REQUEST_ERR;
105 // Since some elements recreates shadow root dynamically, multiple shadow subtrees won't work well in that element.
106 // Until they are fixed, we disable adding author shadow root for them.
107 if (type == AuthorShadowRoot && !allowsAuthorShadowRoot(element)) {
108 ec = HIERARCHY_REQUEST_ERR;
112 RefPtr<ShadowRoot> shadowRoot = adoptRef(new ShadowRoot(element->document()));
114 shadowRoot->m_type = type;
118 element->ensureShadow()->addShadowRoot(element, shadowRoot, type, ec);
121 ASSERT(element == shadowRoot->host());
122 ASSERT(element->shadow());
123 return shadowRoot.release();
126 String ShadowRoot::nodeName() const
128 return "#shadow-root";
131 PassRefPtr<Node> ShadowRoot::cloneNode(bool)
133 // ShadowRoot should not be arbitrarily cloned.
137 String ShadowRoot::innerHTML() const
139 return createMarkup(this, ChildrenOnly);
142 void ShadowRoot::setInnerHTML(const String& markup, ExceptionCode& ec)
144 if (RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, host(), AllowScriptingContent, ec))
145 replaceChildrenWithFragment(this, fragment.release(), ec);
148 bool ShadowRoot::childTypeAllowed(NodeType type) const
152 case PROCESSING_INSTRUCTION_NODE:
155 case CDATA_SECTION_NODE:
156 case ENTITY_REFERENCE_NODE:
163 ElementShadow* ShadowRoot::owner() const
166 return host()->shadow();
170 bool ShadowRoot::hasInsertionPoint() const
172 for (Node* n = firstChild(); n; n = n->traverseNextNode(this)) {
173 if (isInsertionPoint(n))
180 bool ShadowRoot::applyAuthorStyles() const
182 return m_applyAuthorStyles;
185 void ShadowRoot::setApplyAuthorStyles(bool value)
187 if (m_applyAuthorStyles != value) {
188 m_applyAuthorStyles = value;
189 host()->setNeedsStyleRecalc();
193 bool ShadowRoot::resetStyleInheritance() const
195 return m_resetStyleInheritance;
198 void ShadowRoot::setResetStyleInheritance(bool value)
200 if (value != m_resetStyleInheritance) {
201 m_resetStyleInheritance = value;
202 if (attached() && owner())
203 owner()->recalcStyle(Force);
207 void ShadowRoot::attach()
209 StyleResolver* styleResolver = document()->styleResolver();
210 styleResolver->pushParentShadowRoot(this);
211 attachChildrenIfNeeded();
213 styleResolver->popParentShadowRoot(this);
216 void ShadowRoot::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
218 ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
219 owner()->invalidateDistribution();