initial import
[vuplus_webkit] / Source / WebCore / dom / ShadowRoot.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "ShadowRoot.h"
29 #include "Element.h"
30
31 #include "Document.h"
32 #include "NodeRareData.h"
33 #include "ShadowContentElement.h"
34 #include "ShadowInclusionSelector.h"
35 #include "Text.h"
36
37 namespace WebCore {
38
39 ShadowRoot::ShadowRoot(Document* document)
40     : TreeScope(document)
41     , m_applyAuthorSheets(false)
42 {
43     ASSERT(document);
44     
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);
50 }
51
52 ShadowRoot::~ShadowRoot()
53 {
54 }
55
56 String ShadowRoot::nodeName() const
57 {
58     return "#shadow-root";
59 }
60
61 Node::NodeType ShadowRoot::nodeType() const
62 {
63     return SHADOW_ROOT_NODE;
64 }
65
66 PassRefPtr<Node> ShadowRoot::cloneNode(bool)
67 {
68     // ShadowRoot should not be arbitrarily cloned.
69     return 0;
70 }
71
72 bool ShadowRoot::childTypeAllowed(NodeType type) const
73 {
74     switch (type) {
75     case ELEMENT_NODE:
76     case PROCESSING_INSTRUCTION_NODE:
77     case COMMENT_NODE:
78     case TEXT_NODE:
79     case CDATA_SECTION_NODE:
80     case ENTITY_REFERENCE_NODE:
81         return true;
82     default:
83         return false;
84     }
85 }
86
87 void ShadowRoot::recalcShadowTreeStyle(StyleChange change)
88 {
89     if (hasContentElement())
90         reattach();
91     else {
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);
97         }
98     }
99
100     clearNeedsStyleRecalc();
101     clearChildNeedsStyleRecalc();
102 }
103
104 ShadowContentElement* ShadowRoot::includerFor(Node* node) const
105 {
106     if (!m_inclusions)
107         return 0;
108     ShadowInclusion* found = m_inclusions->findFor(node);
109     if (!found)
110         return 0;
111     return found->includer();
112 }
113
114 void ShadowRoot::hostChildrenChanged()
115 {
116     if (!hasContentElement())
117         return;
118     // This results in forced detaching/attaching of the shadow render tree. See ShadowRoot::recalcStyle().
119     setNeedsStyleRecalc();
120 }
121
122 bool ShadowRoot::hasContentElement() const
123 {
124     for (Node* n = firstChild(); n; n = n->traverseNextNode(this)) {
125         if (n->isContentElement())
126             return true;
127     }
128
129     return false;
130 }
131
132 bool ShadowRoot::applyAuthorSheets() const
133 {
134     return m_applyAuthorSheets;
135 }
136
137 void ShadowRoot::setApplyAuthorSheets(bool value)
138 {
139     m_applyAuthorSheets = value;
140 }
141
142 void ShadowRoot::attach()
143 {
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());
148     TreeScope::attach();
149     if (m_inclusions)
150         m_inclusions->didSelect();
151 }
152
153 ShadowInclusionSelector* ShadowRoot::inclusions() const
154 {
155     return m_inclusions.get();
156 }
157
158 ShadowInclusionSelector* ShadowRoot::ensureInclusions()
159 {
160     if (!m_inclusions)
161         m_inclusions = adoptPtr(new ShadowInclusionSelector());
162     m_inclusions->willSelectOver(this);
163     return m_inclusions.get();
164 }
165
166
167 }