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/dom/FirstLetterPseudoElement.h"
31 #include "core/frame/UseCounter.h"
32 #include "core/inspector/InspectorInstrumentation.h"
33 #include "core/layout/LayoutObject.h"
34 #include "core/layout/LayoutQuote.h"
35 #include "core/style/ContentData.h"
39 PassRefPtrWillBeRawPtr
<PseudoElement
> PseudoElement::create(Element
* parent
, PseudoId pseudoId
)
41 return adoptRefWillBeNoop(new PseudoElement(parent
, pseudoId
));
44 const QualifiedName
& pseudoElementTagName(PseudoId pseudoId
)
48 DEFINE_STATIC_LOCAL(QualifiedName
, after
, (nullAtom
, "<pseudo:after>", nullAtom
));
52 DEFINE_STATIC_LOCAL(QualifiedName
, before
, (nullAtom
, "<pseudo:before>", nullAtom
));
56 DEFINE_STATIC_LOCAL(QualifiedName
, backdrop
, (nullAtom
, "<pseudo:backdrop>", nullAtom
));
60 DEFINE_STATIC_LOCAL(QualifiedName
, firstLetter
, (nullAtom
, "<pseudo:first-letter>", nullAtom
));
67 DEFINE_STATIC_LOCAL(QualifiedName
, name
, (nullAtom
, "<pseudo>", nullAtom
));
71 String
PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId
)
73 DEFINE_STATIC_LOCAL(const String
, after
, ("::after"));
74 DEFINE_STATIC_LOCAL(const String
, before
, ("::before"));
85 PseudoElement::PseudoElement(Element
* parent
, PseudoId pseudoId
)
86 : Element(pseudoElementTagName(pseudoId
), &parent
->document(), CreateElement
)
87 , m_pseudoId(pseudoId
)
89 ASSERT(pseudoId
!= NOPSEUDO
);
90 parent
->treeScope().adoptIfNeeded(*this);
91 setParentOrShadowHostNode(parent
);
92 setHasCustomStyleCallbacks();
93 if ((pseudoId
== BEFORE
|| pseudoId
== AFTER
) && parent
->hasTagName(HTMLNames::inputTag
))
94 UseCounter::count(parent
->document(), UseCounter::PseudoBeforeAfterForInputElement
);
97 PassRefPtr
<ComputedStyle
> PseudoElement::customStyleForLayoutObject()
99 return parentOrShadowHostElement()->layoutObject()->getCachedPseudoStyle(m_pseudoId
);
102 void PseudoElement::dispose()
104 ASSERT(parentOrShadowHostElement());
106 InspectorInstrumentation::pseudoElementDestroyed(this);
108 ASSERT(!nextSibling());
109 ASSERT(!previousSibling());
112 RefPtrWillBeRawPtr
<Element
> parent
= parentOrShadowHostElement();
113 document().adoptIfNeeded(*this);
114 setParentOrShadowHostNode(0);
115 removedFrom(parent
.get());
118 void PseudoElement::attach(const AttachContext
& context
)
120 ASSERT(!layoutObject());
122 Element::attach(context
);
124 LayoutObject
* layoutObject
= this->layoutObject();
128 ComputedStyle
& style
= layoutObject
->mutableStyleRef();
129 if (style
.styleType() != BEFORE
&& style
.styleType() != AFTER
)
131 ASSERT(style
.contentData());
133 for (const ContentData
* content
= style
.contentData(); content
; content
= content
->next()) {
134 LayoutObject
* child
= content
->createLayoutObject(document(), style
);
135 if (layoutObject
->isChildAllowed(child
, style
)) {
136 layoutObject
->addChild(child
);
137 if (child
->isQuote())
138 toLayoutQuote(child
)->attachQuote();
145 bool PseudoElement::layoutObjectIsNeeded(const ComputedStyle
& style
)
147 return pseudoElementLayoutObjectIsNeeded(&style
);
150 void PseudoElement::didRecalcStyle(StyleRecalcChange
)
155 // The layoutObjects inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
156 // the style propagated downward manually similar to LayoutObject::propagateStyleToAnonymousChildren.
157 LayoutObject
* layoutObject
= this->layoutObject();
158 for (LayoutObject
* child
= layoutObject
->nextInPreOrder(layoutObject
); child
; child
= child
->nextInPreOrder(layoutObject
)) {
159 // We only manage the style for the generated content items.
160 if (!child
->isText() && !child
->isQuote() && !child
->isImage())
163 child
->setPseudoStyle(layoutObject
->mutableStyle());
167 // With PseudoElements the DOM tree and Layout tree can differ. When you attach
168 // a, first-letter for example, into the DOM we walk down the Layout
169 // tree to find the correct insertion point for the LayoutObject. But, this
170 // means if we ask for the parentOrShadowHost Node from the first-letter
171 // pseudo element we will get some arbitrary ancestor of the LayoutObject.
173 // For hit testing, we need the parent Node of the LayoutObject for the
174 // first-letter pseudo element. So, by walking up the Layout tree we know
175 // we will get the parent and not some other ancestor.
176 Node
* PseudoElement::findAssociatedNode() const
178 // The ::backdrop element is parented to the LayoutView, not to the node
179 // that it's associated with. We need to make sure ::backdrop sends the
180 // events to the parent node correctly.
181 if (pseudoId() == BACKDROP
)
182 return parentOrShadowHostNode();
184 ASSERT(layoutObject());
185 ASSERT(layoutObject()->parent());
187 // We can have any number of anonymous layout objects inserted between
188 // us and our parent so make sure we skip over them.
189 LayoutObject
* ancestor
= layoutObject()->parent();
190 while (ancestor
->isAnonymous() || (ancestor
->node() && ancestor
->node()->isPseudoElement())) {
191 ASSERT(ancestor
->parent());
192 ancestor
= ancestor
->parent();
194 return ancestor
->node();