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#include "wtf/Forward.h"
39
40namespace WebCore {
41
42class InsertionPoint : public HTMLElement {
43public:
44    virtual ~InsertionPoint();
45
46    bool hasDistribution() const { return !m_distribution.isEmpty(); }
47    void setDistribution(ContentDistribution&);
48    void clearDistribution() { m_distribution.clear(); }
49    bool isActive() const;
50    bool canBeActive() const;
51
52    bool isShadowInsertionPoint() const;
53    bool isContentInsertionPoint() const;
54
55    PassRefPtr<NodeList> getDistributedNodes();
56
57    virtual bool canAffectSelector() const { return false; }
58
59    bool resetStyleInheritance() const;
60    void setResetStyleInheritance(bool);
61
62    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
63    virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
64
65    bool shouldUseFallbackElements() const;
66
67    size_t size() const { return m_distribution.size(); }
68    Node* at(size_t index)  const { return m_distribution.at(index).get(); }
69    Node* first() const { return m_distribution.isEmpty() ? 0 : m_distribution.first().get(); }
70    Node* last() const { return m_distribution.isEmpty() ? 0 : m_distribution.last().get(); }
71    Node* nextTo(const Node* node) const { return m_distribution.nextTo(node); }
72    Node* previousTo(const Node* node) const { return m_distribution.previousTo(node); }
73
74protected:
75    InsertionPoint(const QualifiedName&, Document&);
76    virtual bool rendererIsNeeded(const RenderStyle&) OVERRIDE;
77    virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
78    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
79    virtual void removedFrom(ContainerNode*) OVERRIDE;
80    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
81    virtual void willRecalcStyle(StyleRecalcChange) OVERRIDE;
82
83private:
84    ContentDistribution m_distribution;
85    bool m_registeredWithShadowRoot;
86};
87
88typedef Vector<RefPtr<InsertionPoint> > DestinationInsertionPoints;
89
90DEFINE_NODE_TYPE_CASTS(InsertionPoint, isInsertionPoint());
91
92inline bool isActiveInsertionPoint(const Node& node)
93{
94    return node.isInsertionPoint() && toInsertionPoint(node).isActive();
95}
96
97inline bool isActiveShadowInsertionPoint(const Node& node)
98{
99    return node.isInsertionPoint() && toInsertionPoint(node).isShadowInsertionPoint();
100}
101
102inline ElementShadow* shadowWhereNodeCanBeDistributed(const Node& node)
103{
104    Node* parent = node.parentNode();
105    if (!parent)
106        return 0;
107    if (parent->isShadowRoot() && !toShadowRoot(parent)->isYoungest())
108        return node.shadowHost()->shadow();
109    if (isActiveInsertionPoint(*parent))
110        return node.shadowHost()->shadow();
111    if (parent->isElementNode())
112        return toElement(parent)->shadow();
113    return 0;
114}
115
116const InsertionPoint* resolveReprojection(const Node*);
117
118void collectDestinationInsertionPoints(const Node&, Vector<InsertionPoint*, 8>& results);
119
120} // namespace WebCore
121
122#endif // InsertionPoint_h
123