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/dom/NodeRenderingContext.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(), CreatePseudoElement)
59    , m_pseudoId(pseudoId)
60{
61    ASSERT(pseudoId != NOPSEUDO);
62    setParentOrShadowHostNode(parent);
63    setHasCustomStyleCallbacks();
64}
65
66PseudoElement::~PseudoElement()
67{
68}
69
70PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
71{
72    return parentOrShadowHostElement()->renderer()->getCachedPseudoStyle(m_pseudoId);
73}
74
75void PseudoElement::attach(const AttachContext& context)
76{
77    ASSERT(!renderer());
78
79    Element::attach(context);
80
81    RenderObject* renderer = this->renderer();
82    if (!renderer)
83        return;
84    RenderStyle* style = renderer->style();
85    if (!style->regionThread().isEmpty())
86        return;
87    if (style->styleType() != BEFORE && style->styleType() != AFTER)
88        return;
89    ASSERT(style->contentData());
90
91    for (const ContentData* content = style->contentData(); content; content = content->next()) {
92        RenderObject* child = content->createRenderer(document(), style);
93        if (renderer->isChildAllowed(child, style)) {
94            renderer->addChild(child);
95            if (child->isQuote())
96                toRenderQuote(child)->attachQuote();
97        } else
98            child->destroy();
99    }
100}
101
102bool PseudoElement::rendererIsNeeded(const NodeRenderingContext& context)
103{
104    return pseudoElementRendererIsNeeded(context.style());
105}
106
107void PseudoElement::didRecalcStyle(StyleChange)
108{
109    if (!renderer())
110        return;
111
112    // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
113    // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
114    RenderObject* renderer = this->renderer();
115    for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
116        // We only manage the style for the generated content items.
117        if (!child->isText() && !child->isQuote() && !child->isImage())
118            continue;
119
120        // The style for the RenderTextFragment for first letter is managed by an enclosing block, not by us.
121        if (child->style()->styleType() == FIRST_LETTER)
122            continue;
123
124        child->setPseudoStyle(renderer->style());
125    }
126}
127
128} // namespace
129