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.
30 #include "ContainerNode.h"
32 #include "DocumentFragment.h"
34 #include "ExceptionCode.h"
35 #include "TreeScope.h"
36 #include <wtf/DoublyLinkedList.h>
45 class ShadowRoot : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
46 friend class WTF::DoublyLinkedListNode<ShadowRoot>;
48 static PassRefPtr<ShadowRoot> create(Element*, ExceptionCode&);
50 // FIXME: We will support multiple shadow subtrees, however current implementation does not work well
51 // if a shadow root is dynamically created. So we prohibit multiple shadow subtrees
52 // in several elements for a while.
53 // See https://bugs.webkit.org/show_bug.cgi?id=77503 and related bugs.
58 static PassRefPtr<ShadowRoot> create(Element*, ShadowRootType, ExceptionCode& = ASSERT_NO_EXCEPTION);
60 void recalcShadowTreeStyle(StyleChange);
62 InsertionPoint* insertionPointFor(Node*) const;
64 virtual bool applyAuthorStyles() const OVERRIDE;
65 void setApplyAuthorStyles(bool);
66 virtual bool resetStyleInheritance() const OVERRIDE;
67 void setResetStyleInheritance(bool);
69 Element* host() const;
70 void setHost(Element*);
71 ElementShadow* owner() const;
73 String innerHTML() const;
74 void setInnerHTML(const String&, ExceptionCode&);
76 Element* activeElement() const;
78 ShadowRoot* youngerShadowRoot() const { return prev(); }
79 ShadowRoot* olderShadowRoot() const { return next(); }
81 bool isYoungest() const { return !youngerShadowRoot(); }
82 bool isOldest() const { return !olderShadowRoot(); }
84 bool hasInsertionPoint() const;
86 virtual void attach();
88 bool isUsedForRendering() const;
89 InsertionPoint* assignedTo() const;
90 void setAssignedTo(InsertionPoint*);
93 ShadowRootType type() const { return m_type; }
97 explicit ShadowRoot(Document*);
98 virtual ~ShadowRoot();
99 virtual String nodeName() const;
100 virtual PassRefPtr<Node> cloneNode(bool deep);
101 virtual bool childTypeAllowed(NodeType) const;
102 virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
106 bool m_applyAuthorStyles : 1;
107 bool m_resetStyleInheritance : 1;
108 InsertionPoint* m_insertionPointAssignedTo;
111 ShadowRootType m_type;
115 inline Element* ShadowRoot::host() const
117 return toElement(parentOrHostNode());
120 inline void ShadowRoot::setHost(Element* host)
122 setParentOrHostNode(host);
125 inline InsertionPoint* ShadowRoot::assignedTo() const
127 return m_insertionPointAssignedTo;
130 inline void ShadowRoot::setAssignedTo(InsertionPoint* insertionPoint)
132 ASSERT(!m_insertionPointAssignedTo || !insertionPoint);
133 m_insertionPointAssignedTo = insertionPoint;
136 inline bool ShadowRoot::isUsedForRendering() const
138 return isYoungest() || assignedTo();
141 inline Element* ShadowRoot::activeElement() const
143 if (Node* node = treeScope()->focusedNode())
144 return node->isElementNode() ? toElement(node) : 0;
148 inline const ShadowRoot* toShadowRoot(const Node* node)
150 ASSERT(!node || node->isShadowRoot());
151 return static_cast<const ShadowRoot*>(node);
154 inline ShadowRoot* toShadowRoot(Node* node)
156 return const_cast<ShadowRoot*>(toShadowRoot(static_cast<const Node*>(node)));