blob: fc7c7b9765b7983ae5baafbf2dc52f32bde769e3 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2012 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 ElementShadow_h
28#define ElementShadow_h
29
30#include "core/dom/ExceptionCode.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010031#include "core/dom/shadow/ContentDistributor.h"
32#include "core/dom/shadow/ShadowRoot.h"
33#include "wtf/DoublyLinkedList.h"
34#include "wtf/Noncopyable.h"
35#include "wtf/PassOwnPtr.h"
36#include "wtf/PassRefPtr.h"
37#include "wtf/Vector.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038
39namespace WebCore {
40
41class ElementShadow {
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010042 WTF_MAKE_NONCOPYABLE(ElementShadow); WTF_MAKE_FAST_ALLOCATED;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043public:
44 static PassOwnPtr<ElementShadow> create()
45 {
46 return adoptPtr(new ElementShadow());
47 }
48
49 ~ElementShadow()
50 {
51 removeAllShadowRoots();
52 }
53
54 Element* host() const;
55 ShadowRoot* youngestShadowRoot() const { return m_shadowRoots.head(); }
56 ShadowRoot* oldestShadowRoot() const { return m_shadowRoots.tail(); }
57 ElementShadow* containingShadow() const;
58
59 ShadowRoot* addShadowRoot(Element* shadowHost, ShadowRoot::ShadowRootType);
60
61 void attach();
62 void detach();
63
64 bool childNeedsStyleRecalc() const;
65 bool needsStyleRecalc() const;
66 void recalcStyle(Node::StyleChange);
67 void removeAllEventListeners();
68
69 void invalidateDistribution() { m_distributor.invalidateDistribution(host()); }
70 void didAffectSelector(AffectedSelectorMask mask) { m_distributor.didAffectSelector(host(), mask); }
71 void willAffectSelector() { m_distributor.willAffectSelector(host()); }
72
73 ContentDistributor& distributor() { return m_distributor; }
74 const ContentDistributor& distributor() const { return m_distributor; }
75
76 void reportMemoryUsage(MemoryObjectInfo*) const;
77
78private:
79 ElementShadow() { }
80
81 void removeAllShadowRoots();
82
83 DoublyLinkedList<ShadowRoot> m_shadowRoots;
84 ContentDistributor m_distributor;
85};
86
87inline Element* ElementShadow::host() const
88{
89 ASSERT(!m_shadowRoots.isEmpty());
90 return youngestShadowRoot()->host();
91}
92
93inline ShadowRoot* Node::youngestShadowRoot() const
94{
95 if (!this->isElementNode())
96 return 0;
97 if (ElementShadow* shadow = toElement(this)->shadow())
98 return shadow->youngestShadowRoot();
99 return 0;
100}
101
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100102inline void Element::ensureDistribution()
103{
104 ContentDistributor::ensureDistribution(this);
105}
106
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100107inline ElementShadow* ElementShadow::containingShadow() const
108{
109 if (ShadowRoot* parentRoot = host()->containingShadowRoot())
110 return parentRoot->owner();
111 return 0;
112}
113
114class ShadowRootVector : public Vector<RefPtr<ShadowRoot> > {
115public:
116 explicit ShadowRootVector(ElementShadow* tree)
117 {
118 for (ShadowRoot* root = tree->youngestShadowRoot(); root; root = root->olderShadowRoot())
119 append(root);
120 }
121};
122
123inline ElementShadow* shadowOfParent(const Node* node)
124{
125 if (!node)
126 return 0;
127 if (Node* parent = node->parentNode())
128 if (parent->isElementNode())
129 return toElement(parent)->shadow();
130 return 0;
131}
132
133
134} // namespace
135
136#endif