1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2012 Google Inc. All rights reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modification, are permitted provided that the following conditions are
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * met:
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * this software without specific prior written permission.
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "config.h"
2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/PseudoElement.h"
29926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/NodeRenderingContext.h"
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/rendering/RenderObject.h"
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/rendering/RenderQuote.h"
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/rendering/style/ContentData.h"
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)namespace WebCore {
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)const QualifiedName& pseudoElementTagName()
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return name;
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
45d24c94b5090b6aa121f0f82325c9379004dc3196Ben Murdoch    DEFINE_STATIC_LOCAL(const String, after, ("::after"));
46d24c94b5090b6aa121f0f82325c9379004dc3196Ben Murdoch    DEFINE_STATIC_LOCAL(const String, before, ("::before"));
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    switch (pseudoId) {
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    case AFTER:
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return after;
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    case BEFORE:
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return before;
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    default:
53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return emptyString();
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)PseudoElement::PseudoElement(Element* parent, PseudoId pseudoId)
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    : Element(pseudoElementTagName(), parent->document(), CreatePseudoElement)
59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    , m_pseudoId(pseudoId)
60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ASSERT(pseudoId != NOPSEUDO);
62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    setParentOrShadowHostNode(parent);
63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    setHasCustomStyleCallbacks();
64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)PseudoElement::~PseudoElement()
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return parentOrShadowHostElement()->renderer()->getCachedPseudoStyle(m_pseudoId);
73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
75521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)void PseudoElement::attach(const AttachContext& context)
76926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ASSERT(!renderer());
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
79521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    Element::attach(context);
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RenderObject* renderer = this->renderer();
82e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    if (!renderer)
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return;
84926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RenderStyle* style = renderer->style();
85e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    if (!style->regionThread().isEmpty())
86e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch        return;
87e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    if (style->styleType() != BEFORE && style->styleType() != AFTER)
88e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch        return;
89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ASSERT(style->contentData());
90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
91926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    for (const ContentData* content = style->contentData(); content; content = content->next()) {
92926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        RenderObject* child = content->createRenderer(document(), style);
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (renderer->isChildAllowed(child, style)) {
94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            renderer->addChild(child);
95926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            if (child->isQuote())
96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                toRenderQuote(child)->attachQuote();
97926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        } else
98926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            child->destroy();
99926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
100926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
101926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)bool PseudoElement::rendererIsNeeded(const NodeRenderingContext& context)
103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return pseudoElementRendererIsNeeded(context.style());
105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)void PseudoElement::didRecalcStyle(StyleChange)
108926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!renderer())
110926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return;
111926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RenderObject* renderer = this->renderer();
115926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
11693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        // We only manage the style for the generated content items.
11793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        if (!child->isText() && !child->isQuote() && !child->isImage())
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            continue;
119926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        // The style for the RenderTextFragment for first letter is managed by an enclosing block, not by us.
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (child->style()->styleType() == FIRST_LETTER)
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            continue;
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        child->setPseudoStyle(renderer->style());
125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} // namespace
129