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 "ContainerNode.h"
31#include "Document.h"
32#include "DocumentFragment.h"
33#include "Element.h"
34#include "ExceptionCode.h"
35#include "TreeScope.h"
36
37namespace WebCore {
38
39class ElementShadow;
40
41class ShadowRoot FINAL : public DocumentFragment, public TreeScope {
42public:
43    // FIXME: We will support multiple shadow subtrees, however current implementation does not work well
44    // if a shadow root is dynamically created. So we prohibit multiple shadow subtrees
45    // in several elements for a while.
46    // See https://bugs.webkit.org/show_bug.cgi?id=77503 and related bugs.
47    enum ShadowRootType {
48        UserAgentShadowRoot = 0,
49        AuthorShadowRoot
50    };
51
52    static PassRefPtr<ShadowRoot> create(Document* document, ShadowRootType type)
53    {
54        return adoptRef(new ShadowRoot(document, type));
55    }
56
57    virtual ~ShadowRoot();
58
59    void recalcStyle(StyleChange);
60
61    virtual bool applyAuthorStyles() const OVERRIDE { return m_applyAuthorStyles; }
62    void setApplyAuthorStyles(bool);
63    virtual bool resetStyleInheritance() const OVERRIDE { return m_resetStyleInheritance; }
64    void setResetStyleInheritance(bool);
65
66    Element* host() const { return toElement(parentOrShadowHostNode()); }
67    ElementShadow* owner() const { return host() ? host()->shadow() : 0; }
68
69    String innerHTML() const;
70    void setInnerHTML(const String&, ExceptionCode&);
71
72    Element* activeElement() const;
73
74    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
75
76    virtual void registerScopedHTMLStyleChild() OVERRIDE;
77    virtual void unregisterScopedHTMLStyleChild() OVERRIDE;
78
79    ShadowRootType type() const { return static_cast<ShadowRootType>(m_type); }
80
81    PassRefPtr<Node> cloneNode(bool, ExceptionCode&);
82
83private:
84    ShadowRoot(Document*, ShadowRootType);
85
86    virtual void dispose() OVERRIDE;
87    virtual bool childTypeAllowed(NodeType) const OVERRIDE;
88    virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
89
90    // ShadowRoots should never be cloned.
91    virtual PassRefPtr<Node> cloneNode(bool) OVERRIDE { return 0; }
92
93    // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834
94    bool isOrphan() const { return !host(); }
95
96    unsigned m_numberOfStyles : 28;
97    unsigned m_applyAuthorStyles : 1;
98    unsigned m_resetStyleInheritance : 1;
99    unsigned m_type : 1;
100};
101
102inline Element* ShadowRoot::activeElement() const
103{
104    return treeScope()->focusedElement();
105}
106
107inline const ShadowRoot* toShadowRoot(const Node* node)
108{
109    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isShadowRoot());
110    return static_cast<const ShadowRoot*>(node);
111}
112
113inline ShadowRoot* toShadowRoot(Node* node)
114{
115    return const_cast<ShadowRoot*>(toShadowRoot(static_cast<const Node*>(node)));
116}
117
118} // namespace
119
120#endif
121