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 *     * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "core/dom/PseudoElement.h"
29
30#include "core/inspector/InspectorInstrumentation.h"
31#include "core/rendering/RenderObject.h"
32#include "core/rendering/RenderQuote.h"
33#include "core/rendering/style/ContentData.h"
34
35namespace WebCore {
36
37const QualifiedName& pseudoElementTagName()
38{
39    DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
40    return name;
41}
42
43String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
44{
45    DEFINE_STATIC_LOCAL(const String, after, ("::after"));
46    DEFINE_STATIC_LOCAL(const String, before, ("::before"));
47    switch (pseudoId) {
48    case AFTER:
49        return after;
50    case BEFORE:
51        return before;
52    default:
53        return emptyString();
54    }
55}
56
57PseudoElement::PseudoElement(Element* parent, PseudoId pseudoId)
58    : Element(pseudoElementTagName(), &parent->document(), CreateElement)
59    , m_pseudoId(pseudoId)
60{
61    ASSERT(pseudoId != NOPSEUDO);
62    setParentOrShadowHostNode(parent);
63    setHasCustomStyleCallbacks();
64}
65
66PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
67{
68    return parentOrShadowHostElement()->renderer()->getCachedPseudoStyle(m_pseudoId);
69}
70
71void PseudoElement::dispose()
72{
73    InspectorInstrumentation::pseudoElementDestroyed(this);
74
75    ASSERT(!nextSibling());
76    ASSERT(!previousSibling());
77
78    detach();
79    RefPtr<Element> parent = parentOrShadowHostElement();
80    setParentOrShadowHostNode(0);
81    removedFrom(parent.get());
82}
83
84void PseudoElement::attach(const AttachContext& context)
85{
86    ASSERT(!renderer());
87
88    Element::attach(context);
89
90    RenderObject* renderer = this->renderer();
91    if (!renderer)
92        return;
93    RenderStyle* style = renderer->style();
94    if (style->hasFlowFrom())
95        return;
96    if (style->styleType() != BEFORE && style->styleType() != AFTER)
97        return;
98    ASSERT(style->contentData());
99
100    for (const ContentData* content = style->contentData(); content; content = content->next()) {
101        RenderObject* child = content->createRenderer(document(), style);
102        if (renderer->isChildAllowed(child, style)) {
103            renderer->addChild(child);
104            if (child->isQuote())
105                toRenderQuote(child)->attachQuote();
106        } else
107            child->destroy();
108    }
109}
110
111bool PseudoElement::rendererIsNeeded(const RenderStyle& style)
112{
113    return pseudoElementRendererIsNeeded(&style);
114}
115
116void PseudoElement::didRecalcStyle(StyleRecalcChange)
117{
118    if (!renderer())
119        return;
120
121    // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
122    // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
123    RenderObject* renderer = this->renderer();
124    for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
125        // We only manage the style for the generated content items.
126        if (!child->isText() && !child->isQuote() && !child->isImage())
127            continue;
128
129        // The style for the RenderTextFragment for first letter is managed by an enclosing block, not by us.
130        if (child->style()->styleType() == FIRST_LETTER)
131            continue;
132
133        child->setPseudoStyle(renderer->style());
134    }
135}
136
137} // namespace
138