Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / shadow / ShadowRoot.h
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 #ifndef ShadowRoot_h
28 #define ShadowRoot_h
29
30 #include "core/dom/ContainerNode.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/DocumentFragment.h"
33 #include "core/dom/Element.h"
34 #include "core/dom/TreeScope.h"
35 #include "wtf/DoublyLinkedList.h"
36
37 namespace WebCore {
38
39 class ElementShadow;
40 class ExceptionState;
41 class HTMLShadowElement;
42 class InsertionPoint;
43 class ShadowRootRareData;
44
45 class ShadowRoot FINAL : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
46     friend class WTF::DoublyLinkedListNode<ShadowRoot>;
47 public:
48     // FIXME: We will support multiple shadow subtrees, however current implementation does not work well
49     // if a shadow root is dynamically created. So we prohibit multiple shadow subtrees
50     // in several elements for a while.
51     // See https://bugs.webkit.org/show_bug.cgi?id=77503 and related bugs.
52     enum ShadowRootType {
53         UserAgentShadowRoot = 0,
54         AuthorShadowRoot
55     };
56
57     static PassRefPtr<ShadowRoot> create(Document& document, ShadowRootType type)
58     {
59         return adoptRef(new ShadowRoot(document, type));
60     }
61
62     void recalcStyle(StyleRecalcChange);
63
64     // Disambiguate between Node and TreeScope hierarchies; TreeScope's implementation is simpler.
65     using TreeScope::document;
66
67     Element* host() const { return toElement(parentOrShadowHostNode()); }
68     ElementShadow* owner() const { return host() ? host()->shadow() : 0; }
69
70     ShadowRoot* youngerShadowRoot() const { return prev(); }
71
72     ShadowRoot* olderShadowRootForBindings() const;
73     bool shouldExposeToBindings() const { return type() == AuthorShadowRoot; }
74
75     bool isYoungest() const { return !youngerShadowRoot(); }
76     bool isOldest() const { return !olderShadowRoot(); }
77     bool isOldestAuthorShadowRoot() const;
78
79     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
80
81     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
82     virtual void removedFrom(ContainerNode*) OVERRIDE;
83
84     virtual void registerScopedHTMLStyleChild() OVERRIDE;
85     virtual void unregisterScopedHTMLStyleChild() OVERRIDE;
86
87     bool containsShadowElements() const;
88     bool containsContentElements() const;
89     bool containsInsertionPoints() const { return containsShadowElements() || containsContentElements(); }
90     bool containsShadowRoots() const;
91
92     unsigned descendantShadowElementCount() const;
93
94     // For Internals, don't use this.
95     unsigned childShadowRootCount() const;
96
97     HTMLShadowElement* shadowInsertionPointOfYoungerShadowRoot() const;
98     void setShadowInsertionPointOfYoungerShadowRoot(PassRefPtr<HTMLShadowElement>);
99
100     void didAddInsertionPoint(InsertionPoint*);
101     void didRemoveInsertionPoint(InsertionPoint*);
102     const Vector<RefPtr<InsertionPoint> >& descendantInsertionPoints();
103
104     ShadowRootType type() const { return static_cast<ShadowRootType>(m_type); }
105
106     // Make protected methods from base class public here.
107     using TreeScope::setDocument;
108     using TreeScope::setParentTreeScope;
109
110 public:
111     Element* activeElement() const;
112
113     bool applyAuthorStyles() const { return m_applyAuthorStyles; }
114     void setApplyAuthorStyles(bool);
115
116     bool resetStyleInheritance() const { return m_resetStyleInheritance; }
117     void setResetStyleInheritance(bool);
118
119     ShadowRoot* olderShadowRoot() const { return next(); }
120
121     String innerHTML() const;
122     void setInnerHTML(const String&, ExceptionState&);
123
124     PassRefPtr<Node> cloneNode(bool, ExceptionState&);
125     PassRefPtr<Node> cloneNode(ExceptionState& exceptionState) { return cloneNode(true, exceptionState); }
126
127     StyleSheetList* styleSheets();
128     bool isActiveForStyling() const;
129
130 private:
131     ShadowRoot(Document&, ShadowRootType);
132     virtual ~ShadowRoot();
133
134     virtual void dispose() OVERRIDE;
135     virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
136
137     ShadowRootRareData* ensureShadowRootRareData();
138
139     void addChildShadowRoot();
140     void removeChildShadowRoot();
141     void invalidateDescendantInsertionPoints();
142
143     // ShadowRoots should never be cloned.
144     virtual PassRefPtr<Node> cloneNode(bool) OVERRIDE { return 0; }
145
146     // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834
147     bool isOrphan() const { return !host(); }
148
149     ShadowRoot* m_prev;
150     ShadowRoot* m_next;
151     OwnPtr<ShadowRootRareData> m_shadowRootRareData;
152     unsigned m_numberOfStyles : 27;
153     unsigned m_applyAuthorStyles : 1;
154     unsigned m_resetStyleInheritance : 1;
155     unsigned m_type : 1;
156     unsigned m_registeredWithParentShadowRoot : 1;
157     unsigned m_descendantInsertionPointsIsValid : 1;
158 };
159
160 inline Element* ShadowRoot::activeElement() const
161 {
162     return adjustedFocusedElement();
163 }
164
165 DEFINE_NODE_TYPE_CASTS(ShadowRoot, isShadowRoot());
166 DEFINE_TYPE_CASTS(ShadowRoot, TreeScope, treeScope, treeScope->rootNode().isShadowRoot(), treeScope.rootNode().isShadowRoot());
167
168 } // namespace
169
170 #endif