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 "RenderElement.h"
33#include "RenderImage.h"
34#include "RenderQuote.h"
35
36namespace WebCore {
37
38const QualifiedName& pseudoElementTagName()
39{
40    DEPRECATED_DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
41    return name;
42}
43
44String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
45{
46    DEPRECATED_DEFINE_STATIC_LOCAL(const String, after, (ASCIILiteral("::after")));
47    DEPRECATED_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& host, PseudoId pseudoId)
59    : Element(pseudoElementTagName(), host.document(), CreatePseudoElement)
60    , m_hostElement(&host)
61    , m_pseudoId(pseudoId)
62{
63    ASSERT(pseudoId == BEFORE || pseudoId == AFTER);
64    setHasCustomStyleResolveCallbacks();
65}
66
67PseudoElement::~PseudoElement()
68{
69    ASSERT(!m_hostElement);
70    InspectorInstrumentation::pseudoElementDestroyed(document().page(), this);
71}
72
73PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer(RenderStyle& parentStyle)
74{
75    return m_hostElement->renderer()->getCachedPseudoStyle(m_pseudoId, &parentStyle);
76}
77
78void PseudoElement::didAttachRenderers()
79{
80    RenderElement* renderer = this->renderer();
81    if (!renderer || renderer->style().hasFlowFrom())
82        return;
83
84    const RenderStyle& style = renderer->style();
85    ASSERT(style.contentData());
86
87    for (const ContentData* content = style.contentData(); content; content = content->next()) {
88        auto child = content->createContentRenderer(document(), style);
89        if (renderer->isChildAllowed(*child, style)) {
90            auto* childPtr = child.get();
91            renderer->addChild(child.leakPtr());
92            if (childPtr->isQuote())
93                toRenderQuote(childPtr)->attachQuote();
94        }
95    }
96}
97
98bool PseudoElement::rendererIsNeeded(const RenderStyle& style)
99{
100    return pseudoElementRendererIsNeeded(&style);
101}
102
103void PseudoElement::didRecalcStyle(Style::Change)
104{
105    if (!renderer())
106        return;
107
108    // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
109    // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
110    RenderObject* renderer = this->renderer();
111    for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
112        // We only manage the style for the generated content which must be images or text.
113        if (!child->isRenderImage() && !child->isQuote())
114            continue;
115        PassRef<RenderStyle> createdStyle = RenderStyle::createStyleInheritingFromPseudoStyle(renderer->style());
116        toRenderElement(*child).setStyle(WTF::move(createdStyle));
117    }
118}
119
120} // namespace
121