2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
28 #include "PseudoElement.h"
30 #include "ContentData.h"
31 #include "InspectorInstrumentation.h"
32 #include "RenderElement.h"
33 #include "RenderImage.h"
34 #include "RenderQuote.h"
38 const QualifiedName& pseudoElementTagName()
40 DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
44 String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
46 DEFINE_STATIC_LOCAL(const String, after, (ASCIILiteral("::after")));
47 DEFINE_STATIC_LOCAL(const String, before, (ASCIILiteral("::before")));
58 PseudoElement::PseudoElement(Element& host, PseudoId pseudoId)
59 : Element(pseudoElementTagName(), host.document(), CreatePseudoElement)
60 , m_hostElement(&host)
61 , m_pseudoId(pseudoId)
63 ASSERT(pseudoId == BEFORE || pseudoId == AFTER);
64 setHasCustomStyleResolveCallbacks();
67 PseudoElement::~PseudoElement()
69 ASSERT(!m_hostElement);
70 #if USE(ACCELERATED_COMPOSITING)
71 InspectorInstrumentation::pseudoElementDestroyed(document().page(), this);
75 PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
77 return m_hostElement->renderer()->getCachedPseudoStyle(m_pseudoId);
80 void PseudoElement::didAttachRenderers()
82 RenderElement* renderer = this->renderer();
83 if (!renderer || renderer->style().hasFlowFrom())
86 const RenderStyle& style = renderer->style();
87 ASSERT(style.contentData());
89 for (const ContentData* content = style.contentData(); content; content = content->next()) {
90 auto child = content->createContentRenderer(document(), style);
91 if (renderer->isChildAllowed(*child, style)) {
92 auto* childPtr = child.get();
93 renderer->addChild(child.leakPtr());
94 if (childPtr->isQuote())
95 toRenderQuote(childPtr)->attachQuote();
100 bool PseudoElement::rendererIsNeeded(const RenderStyle& style)
102 return pseudoElementRendererIsNeeded(&style);
105 void PseudoElement::didRecalcStyle(Style::Change)
110 // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
111 // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
112 RenderObject* renderer = this->renderer();
113 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
114 // We only manage the style for the generated content which must be images or text.
115 if (!child->isRenderImage())
117 toRenderImage(*child).setStyle(RenderImage::createStyleInheritingFromPseudoStyle(renderer->style()));