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 "PseudoElement.h"
29
30#include "ContentData.h"
31#include "InspectorInstrumentation.h"
32#include "NodeRenderingContext.h"
33#include "RenderObject.h"
34#include "RenderQuote.h"
35
36namespace WebCore {
37
38const QualifiedName& pseudoElementTagName()
39{
40    DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
41    return name;
42}
43
44String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
45{
46    DEFINE_STATIC_LOCAL(const String, after, (ASCIILiteral("::after")));
47    DEFINE_STATIC_LOCAL(const String, before, (ASCIILiteral("::before")));
48    switch (pseudoId) {
49    case AFTER:
50        return after;
51    case BEFORE:
52        return before;
53    default:
54        return emptyString();
55    }
56}
57
58PseudoElement::PseudoElement(Element* parent, PseudoId pseudoId)
59    : Element(pseudoElementTagName(), parent->document(), CreatePseudoElement)
60    , m_pseudoId(pseudoId)
61{
62    ASSERT(pseudoId != NOPSEUDO);
63    setParentOrShadowHostNode(parent);
64    setHasCustomStyleCallbacks();
65}
66
67PseudoElement::~PseudoElement()
68{
69#if USE(ACCELERATED_COMPOSITING)
70    InspectorInstrumentation::pseudoElementDestroyed(document()->page(), this);
71#endif
72}
73
74PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
75{
76    return parentOrShadowHostElement()->renderer()->getCachedPseudoStyle(m_pseudoId);
77}
78
79void PseudoElement::attach(const AttachContext& context)
80{
81    ASSERT(!renderer());
82
83    Element::attach(context);
84
85    RenderObject* renderer = this->renderer();
86    if (!renderer || !renderer->style()->regionThread().isEmpty())
87        return;
88
89    RenderStyle* style = renderer->style();
90    ASSERT(style->contentData());
91
92    for (const ContentData* content = style->contentData(); content; content = content->next()) {
93        RenderObject* child = content->createRenderer(document(), style);
94        if (renderer->isChildAllowed(child, style)) {
95            renderer->addChild(child);
96            if (child->isQuote())
97                toRenderQuote(child)->attachQuote();
98        } else
99            child->destroy();
100    }
101}
102
103bool PseudoElement::rendererIsNeeded(const NodeRenderingContext& context)
104{
105    return pseudoElementRendererIsNeeded(context.style());
106}
107
108void PseudoElement::didRecalcStyle(StyleChange)
109{
110    if (!renderer())
111        return;
112
113    // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
114    // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
115    RenderObject* renderer = this->renderer();
116    for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
117        // We only manage the style for the generated content which must be images or text.
118        if (!child->isText() && !child->isRenderImage() && !child->isQuote())
119            continue;
120
121        // The style for the RenderTextFragment for first letter is managed by an enclosing block, not by us.
122        if (child->style()->styleType() == FIRST_LETTER)
123            continue;
124
125        child->setPseudoStyle(renderer->style());
126    }
127}
128
129} // namespace
130