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 Element* host() const { return toElement(parentOrShadowHostNode()); } 65 ElementShadow* owner() const { return host() ? host()->shadow() : 0; } 66 67 ShadowRoot* youngerShadowRoot() const { return prev(); } 68 69 ShadowRoot* bindingsOlderShadowRoot() const; 70 bool shouldExposeToBindings() const { return type() == AuthorShadowRoot; } 71 72 bool isYoungest() const { return !youngerShadowRoot(); } 73 bool isOldest() const { return !olderShadowRoot(); } 74 bool isOldestAuthorShadowRoot() const; 75 76 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE; 77 78 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE; 79 virtual void removedFrom(ContainerNode*) OVERRIDE; 80 81 virtual void registerScopedHTMLStyleChild() OVERRIDE; 82 virtual void unregisterScopedHTMLStyleChild() OVERRIDE; 83 84 bool containsShadowElements() const; 85 bool containsContentElements() const; 86 bool containsInsertionPoints() const { return containsShadowElements() || containsContentElements(); } 87 bool containsShadowRoots() const; 88 89 unsigned descendantShadowElementCount() const; 90 91 // For Internals, don't use this. 92 unsigned childShadowRootCount() const; 93 94 HTMLShadowElement* shadowInsertionPointOfYoungerShadowRoot() const; 95 void setShadowInsertionPointOfYoungerShadowRoot(PassRefPtr<HTMLShadowElement>); 96 97 void didAddInsertionPoint(InsertionPoint*); 98 void didRemoveInsertionPoint(InsertionPoint*); 99 const Vector<RefPtr<InsertionPoint> >& descendantInsertionPoints(); 100 101 ShadowRootType type() const { return static_cast<ShadowRootType>(m_type); } 102 103 public: 104 Element* activeElement() const; 105 106 bool applyAuthorStyles() const { return m_applyAuthorStyles; } 107 void setApplyAuthorStyles(bool); 108 109 bool resetStyleInheritance() const { return m_resetStyleInheritance; } 110 void setResetStyleInheritance(bool); 111 112 ShadowRoot* olderShadowRoot() const { return next(); } 113 114 String innerHTML() const; 115 void setInnerHTML(const String&, ExceptionState&); 116 117 PassRefPtr<Node> cloneNode(bool, ExceptionState&); 118 PassRefPtr<Node> cloneNode(ExceptionState& exceptionState) { return cloneNode(true, exceptionState); } 119 120 StyleSheetList* styleSheets(); 121 122 private: 123 ShadowRoot(Document*, ShadowRootType); 124 virtual ~ShadowRoot(); 125 126 virtual void dispose() OVERRIDE; 127 virtual bool childTypeAllowed(NodeType) const OVERRIDE; 128 virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE; 129 130 ShadowRootRareData* ensureShadowRootRareData(); 131 132 void addChildShadowRoot(); 133 void removeChildShadowRoot(); 134 void invalidateDescendantInsertionPoints(); 135 136 // ShadowRoots should never be cloned. 137 virtual PassRefPtr<Node> cloneNode(bool) OVERRIDE { return 0; } 138 139 // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834 140 bool isOrphan() const { return !host(); } 141 bool isActive() const; 142 143 ShadowRoot* m_prev; 144 ShadowRoot* m_next; 145 OwnPtr<ShadowRootRareData> m_shadowRootRareData; 146 unsigned m_numberOfStyles : 27; 147 unsigned m_applyAuthorStyles : 1; 148 unsigned m_resetStyleInheritance : 1; 149 unsigned m_type : 1; 150 unsigned m_registeredWithParentShadowRoot : 1; 151 unsigned m_descendantInsertionPointsIsValid : 1; 152 }; 153 154 inline Element* ShadowRoot::activeElement() const 155 { 156 return adjustedFocusedElement(); 157 } 158 159 DEFINE_NODE_TYPE_CASTS(ShadowRoot, isShadowRoot()); 160 DEFINE_TYPE_CASTS(ShadowRoot, TreeScope, treeScope, treeScope->rootNode() && treeScope->rootNode()->isShadowRoot(), treeScope.rootNode() && treeScope.rootNode()->isShadowRoot()); 161 162 } // namespace 163 164 #endif 165