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 "core/dom/PseudoElement.h"
30 #include "core/inspector/InspectorInstrumentation.h"
31 #include "core/rendering/RenderObject.h"
32 #include "core/rendering/RenderQuote.h"
33 #include "core/rendering/style/ContentData.h"
37 const QualifiedName& pseudoElementTagName()
39 DEFINE_STATIC_LOCAL(QualifiedName, name, (nullAtom, "<pseudo>", nullAtom));
43 String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
45 DEFINE_STATIC_LOCAL(const String, after, ("::after"));
46 DEFINE_STATIC_LOCAL(const String, before, ("::before"));
57 PseudoElement::PseudoElement(Element* parent, PseudoId pseudoId)
58 : Element(pseudoElementTagName(), &parent->document(), CreateElement)
59 , m_pseudoId(pseudoId)
61 ASSERT(pseudoId != NOPSEUDO);
62 setParentOrShadowHostNode(parent);
63 setHasCustomStyleCallbacks();
66 PassRefPtr<RenderStyle> PseudoElement::customStyleForRenderer()
68 return parentOrShadowHostElement()->renderer()->getCachedPseudoStyle(m_pseudoId);
71 void PseudoElement::dispose()
73 ASSERT(parentOrShadowHostElement());
75 InspectorInstrumentation::pseudoElementDestroyed(this);
77 ASSERT(!nextSibling());
78 ASSERT(!previousSibling());
81 RefPtrWillBeRawPtr<Element> parent = parentOrShadowHostElement();
82 setParentOrShadowHostNode(0);
83 removedFrom(parent.get());
86 void PseudoElement::attach(const AttachContext& context)
90 Element::attach(context);
92 RenderObject* renderer = this->renderer();
95 RenderStyle* style = renderer->style();
96 if (style->styleType() != BEFORE && style->styleType() != AFTER)
98 ASSERT(style->contentData());
100 for (const ContentData* content = style->contentData(); content; content = content->next()) {
101 RenderObject* child = content->createRenderer(document(), style);
102 if (renderer->isChildAllowed(child, style)) {
103 renderer->addChild(child);
104 if (child->isQuote())
105 toRenderQuote(child)->attachQuote();
111 bool PseudoElement::rendererIsNeeded(const RenderStyle& style)
113 return pseudoElementRendererIsNeeded(&style);
116 void PseudoElement::didRecalcStyle(StyleRecalcChange)
121 // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
122 // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
123 RenderObject* renderer = this->renderer();
124 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
125 // We only manage the style for the generated content items.
126 if (!child->isText() && !child->isQuote() && !child->isImage())
129 // The style for the RenderTextFragment for first letter is managed by an enclosing block, not by us.
130 if (child->style()->styleType() == FIRST_LETTER)
133 child->setPseudoStyle(renderer->style());