Home | History | Annotate | Download | only in shadow
      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/ContentDistribution.h"
     36 #include "core/dom/shadow/ShadowRoot.h"
     37 #include "core/html/HTMLElement.h"
     38 
     39 namespace WebCore {
     40 
     41 class InsertionPoint : public HTMLElement {
     42 public:
     43     virtual ~InsertionPoint();
     44 
     45     bool hasDistribution() const { return !m_distribution.isEmpty(); }
     46     void setDistribution(ContentDistribution&);
     47     void clearDistribution() { m_distribution.clear(); }
     48     bool isActive() const;
     49     bool canBeActive() const;
     50 
     51     bool isShadowInsertionPoint() const;
     52     bool isContentInsertionPoint() const;
     53 
     54     PassRefPtrWillBeRawPtr<StaticNodeList> getDistributedNodes();
     55 
     56     virtual bool canAffectSelector() const { return false; }
     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     virtual void trace(Visitor*) OVERRIDE;
     71 
     72 protected:
     73     InsertionPoint(const QualifiedName&, Document&);
     74     virtual bool rendererIsNeeded(const RenderStyle&) OVERRIDE;
     75     virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
     76     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
     77     virtual void removedFrom(ContainerNode*) OVERRIDE;
     78     virtual void willRecalcStyle(StyleRecalcChange) OVERRIDE;
     79 
     80 private:
     81     ContentDistribution m_distribution;
     82     bool m_registeredWithShadowRoot;
     83 };
     84 
     85 typedef WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> > DestinationInsertionPoints;
     86 
     87 DEFINE_ELEMENT_TYPE_CASTS(InsertionPoint, isInsertionPoint());
     88 
     89 inline bool isActiveInsertionPoint(const Node& node)
     90 {
     91     return node.isInsertionPoint() && toInsertionPoint(node).isActive();
     92 }
     93 
     94 inline bool isActiveShadowInsertionPoint(const Node& node)
     95 {
     96     return node.isInsertionPoint() && toInsertionPoint(node).isShadowInsertionPoint();
     97 }
     98 
     99 inline ElementShadow* shadowWhereNodeCanBeDistributed(const Node& node)
    100 {
    101     Node* parent = node.parentNode();
    102     if (!parent)
    103         return 0;
    104     if (parent->isShadowRoot() && !toShadowRoot(parent)->isYoungest())
    105         return node.shadowHost()->shadow();
    106     if (isActiveInsertionPoint(*parent))
    107         return node.shadowHost()->shadow();
    108     if (parent->isElementNode())
    109         return toElement(parent)->shadow();
    110     return 0;
    111 }
    112 
    113 const InsertionPoint* resolveReprojection(const Node*);
    114 
    115 void collectDestinationInsertionPoints(const Node&, WillBeHeapVector<RawPtrWillBeMember<InsertionPoint>, 8>& results);
    116 
    117 } // namespace WebCore
    118 
    119 #endif // InsertionPoint_h
    120