1 /* 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 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef InsertionPoint_h 32 #define InsertionPoint_h 33 34 #include "core/css/CSSSelectorList.h" 35 #include "core/dom/shadow/ContentDistributor.h" 36 #include "core/dom/shadow/ElementShadow.h" 37 #include "core/html/HTMLElement.h" 38 #include "wtf/Forward.h" 39 40 namespace WebCore { 41 42 class InsertionPoint : public HTMLElement { 43 public: 44 virtual ~InsertionPoint(); 45 46 bool hasDistribution() const { return !m_distribution.isEmpty(); } 47 void setDistribution(ContentDistribution& distribution) { m_distribution.swap(distribution); } 48 void clearDistribution() { m_distribution.clear(); } 49 bool isActive() const; 50 51 PassRefPtr<NodeList> getDistributedNodes(); 52 53 virtual bool canAffectSelector() const { return false; } 54 55 bool resetStyleInheritance() const; 56 void setResetStyleInheritance(bool); 57 58 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE; 59 virtual void detach(const AttachContext& = AttachContext()) OVERRIDE; 60 61 bool shouldUseFallbackElements() const; 62 63 size_t size() const { return m_distribution.size(); } 64 Node* at(size_t index) const { return m_distribution.at(index).get(); } 65 Node* first() const { return m_distribution.isEmpty() ? 0 : m_distribution.first().get(); } 66 Node* last() const { return m_distribution.isEmpty() ? 0 : m_distribution.last().get(); } 67 Node* nextTo(const Node* node) const { return m_distribution.nextTo(node); } 68 Node* previousTo(const Node* node) const { return m_distribution.previousTo(node); } 69 70 protected: 71 InsertionPoint(const QualifiedName&, Document*); 72 virtual bool rendererIsNeeded(const NodeRenderingContext&) OVERRIDE; 73 virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE; 74 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE; 75 virtual void removedFrom(ContainerNode*) OVERRIDE; 76 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; 77 virtual void willRecalcStyle(StyleChange) OVERRIDE; 78 79 private: 80 81 ContentDistribution m_distribution; 82 bool m_registeredWithShadowRoot; 83 }; 84 85 inline InsertionPoint* toInsertionPoint(Node* node) 86 { 87 ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isInsertionPoint()); 88 return static_cast<InsertionPoint*>(node); 89 } 90 91 inline const InsertionPoint* toInsertionPoint(const Node* node) 92 { 93 ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isInsertionPoint()); 94 return static_cast<const InsertionPoint*>(node); 95 } 96 97 inline bool isActiveInsertionPoint(const Node* node) 98 { 99 return node->isInsertionPoint() && toInsertionPoint(node)->isActive(); 100 } 101 102 inline Node* parentNodeForDistribution(const Node* node) 103 { 104 ASSERT(node); 105 106 if (Node* parent = node->parentNode()) { 107 if (parent->isInsertionPoint() && toInsertionPoint(parent)->shouldUseFallbackElements()) 108 return parent->parentNode(); 109 return parent; 110 } 111 112 return 0; 113 } 114 115 inline Element* parentElementForDistribution(const Node* node) 116 { 117 if (Node* parent = parentNodeForDistribution(node)) { 118 if (parent->isElementNode()) 119 return toElement(parent); 120 } 121 122 return 0; 123 } 124 125 inline ElementShadow* shadowOfParentForDistribution(const Node* node) 126 { 127 ASSERT(node); 128 if (Element* parent = parentElementForDistribution(node)) 129 return parent->shadow(); 130 131 return 0; 132 } 133 134 InsertionPoint* resolveReprojection(const Node*); 135 136 void collectInsertionPointsWhereNodeIsDistributed(const Node*, Vector<InsertionPoint*, 8>& results); 137 138 } // namespace WebCore 139 140 #endif // InsertionPoint_h 141