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 "ContentDistributor.h"
35#include "ElementShadow.h"
36#include "HTMLElement.h"
37#include "HTMLNames.h"
38#include "ShadowRoot.h"
39#include <wtf/Forward.h>
40
41namespace WebCore {
42
43class InsertionPoint : public HTMLElement {
44public:
45    enum Type {
46        InternalType,
47        HTMLContentElementType
48    };
49
50    enum MatchType {
51        AlwaysMatches,
52        NeverMatches,
53    };
54
55    virtual ~InsertionPoint();
56
57    bool hasDistribution() const { return m_hasDistribution; }
58    void setHasDistribution() { m_hasDistribution = true; }
59    void clearDistribution() { m_hasDistribution = false; }
60    bool isShadowBoundary() const;
61    bool isActive() const;
62
63    virtual MatchType matchTypeFor(Node*) const { return AlwaysMatches; }
64    virtual Type insertionPointType() const { return InternalType; }
65
66    bool resetStyleInheritance() const;
67    void setResetStyleInheritance(bool);
68
69    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
70    virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
71
72    bool shouldUseFallbackElements() const;
73
74    Node* firstDistributed() const;
75    Node* lastDistributed() const;
76    Node* nextDistributedTo(const Node*) const;
77    Node* previousDistributedTo(const Node*) const;
78
79protected:
80    InsertionPoint(const QualifiedName&, Document*);
81    virtual bool rendererIsNeeded(const NodeRenderingContext&) OVERRIDE;
82    virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
83    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
84    virtual void removedFrom(ContainerNode*) OVERRIDE;
85    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
86    virtual bool isInsertionPointNode() const OVERRIDE { return true; }
87
88private:
89
90    bool m_hasDistribution;
91};
92
93inline InsertionPoint* toInsertionPoint(Node* node)
94{
95    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isInsertionPoint());
96    return static_cast<InsertionPoint*>(node);
97}
98
99inline const InsertionPoint* toInsertionPoint(const Node* node)
100{
101    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isInsertionPoint());
102    return static_cast<const InsertionPoint*>(node);
103}
104
105inline bool isActiveInsertionPoint(const Node* node)
106{
107    return node->isInsertionPoint() && toInsertionPoint(node)->isActive();
108}
109
110inline bool isLowerEncapsulationBoundary(Node* node)
111{
112    if (!node || !node->isInsertionPoint())
113        return false;
114    return toInsertionPoint(node)->isShadowBoundary();
115}
116
117inline Node* parentNodeForDistribution(const Node* node)
118{
119    ASSERT(node);
120
121    if (Node* parent = node->parentNode()) {
122        if (parent->isInsertionPoint() && toInsertionPoint(parent)->shouldUseFallbackElements())
123            return parent->parentNode();
124        return parent;
125    }
126
127    return 0;
128}
129
130inline Element* parentElementForDistribution(const Node* node)
131{
132    if (Node* parent = parentNodeForDistribution(node)) {
133        if (parent->isElementNode())
134            return toElement(parent);
135    }
136
137    return 0;
138}
139
140inline ElementShadow* shadowOfParentForDistribution(const Node* node)
141{
142    ASSERT(node);
143    if (Element* parent = parentElementForDistribution(node))
144        return parent->shadow();
145
146    return 0;
147}
148
149InsertionPoint* resolveReprojection(const Node*);
150
151} // namespace WebCore
152
153#endif // InsertionPoint_h
154