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#include "config.h"
32#include "InsertionPoint.h"
33
34#include "QualifiedName.h"
35#include "StaticNodeList.h"
36#include "Text.h"
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42InsertionPoint::InsertionPoint(const QualifiedName& tagName, Document& document)
43    : HTMLElement(tagName, document, CreateInsertionPoint)
44    , m_hasDistribution(false)
45{
46}
47
48InsertionPoint::~InsertionPoint()
49{
50}
51
52bool InsertionPoint::shouldUseFallbackElements() const
53{
54    return isActive() && !hasDistribution();
55}
56
57bool InsertionPoint::isActive() const
58{
59    if (!containingShadowRoot())
60        return false;
61    const Node* node = parentNode();
62    while (node) {
63        if (node->isInsertionPoint())
64            return false;
65
66        node = node->parentNode();
67    }
68    return true;
69}
70
71bool InsertionPoint::rendererIsNeeded(const RenderStyle& style)
72{
73    return !isActive() && HTMLElement::rendererIsNeeded(style);
74}
75
76void InsertionPoint::childrenChanged(const ChildChange& change)
77{
78    HTMLElement::childrenChanged(change);
79    if (ShadowRoot* root = containingShadowRoot())
80        root->invalidateDistribution();
81}
82
83Node::InsertionNotificationRequest InsertionPoint::insertedInto(ContainerNode& insertionPoint)
84{
85    HTMLElement::insertedInto(insertionPoint);
86
87    if (ShadowRoot* root = containingShadowRoot()) {
88        root->distributor().didShadowBoundaryChange(root->hostElement());
89        root->distributor().invalidateInsertionPointList();
90    }
91
92    return InsertionDone;
93}
94
95void InsertionPoint::removedFrom(ContainerNode& insertionPoint)
96{
97    ShadowRoot* root = containingShadowRoot();
98    if (!root)
99        root = insertionPoint.containingShadowRoot();
100
101    if (root && root->hostElement()) {
102        root->invalidateDistribution();
103        root->distributor().invalidateInsertionPointList();
104    }
105
106    // Since this insertion point is no longer visible from the shadow subtree, it need to clean itself up.
107    clearDistribution();
108
109    HTMLElement::removedFrom(insertionPoint);
110}
111
112Node* InsertionPoint::firstDistributed() const
113{
114    if (!m_hasDistribution)
115        return 0;
116    for (Node* current = shadowHost()->firstChild(); current; current = current->nextSibling()) {
117        if (matchTypeFor(current) == InsertionPoint::AlwaysMatches)
118            return current;
119    }
120    return 0;
121}
122
123Node* InsertionPoint::lastDistributed() const
124{
125    if (!m_hasDistribution)
126        return 0;
127    for (Node* current = shadowHost()->lastChild(); current; current = current->previousSibling()) {
128        if (matchTypeFor(current) == InsertionPoint::AlwaysMatches)
129            return current;
130    }
131    return 0;
132}
133
134Node* InsertionPoint::nextDistributedTo(const Node* node) const
135{
136    for (Node* current = node->nextSibling(); current; current = current->nextSibling()) {
137        if (matchTypeFor(current) == InsertionPoint::AlwaysMatches)
138            return current;
139    }
140    return 0;
141}
142
143Node* InsertionPoint::previousDistributedTo(const Node* node) const
144{
145    for (Node* current = node->previousSibling(); current; current = current->previousSibling()) {
146        if (matchTypeFor(current) == InsertionPoint::AlwaysMatches)
147            return current;
148    }
149    return 0;
150}
151
152InsertionPoint* findInsertionPointOf(const Node* projectedNode)
153{
154    if (ShadowRoot* shadowRoot = shadowRootOfParentForDistribution(projectedNode)) {
155        if (ShadowRoot* root = projectedNode->containingShadowRoot())
156            ContentDistributor::ensureDistribution(root);
157        return shadowRoot->distributor().findInsertionPointFor(projectedNode);
158    }
159    return 0;
160}
161
162} // namespace WebCore
163