#include "config.h"
#include "PseudoElement.h"
#include "ContentData.h"
#include "InspectorInstrumentation.h"
#include "RenderElement.h"
#include "RenderImage.h"
#include "RenderQuote.h"
#include "StyleResolver.h"
namespace WebCore {
const QualifiedName& pseudoElementTagName()
{
static NeverDestroyed<QualifiedName> name(nullAtom(), "<pseudo>", nullAtom());
return name;
}
String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId)
{
static NeverDestroyed<const String> after(MAKE_STATIC_STRING_IMPL("::after"));
static NeverDestroyed<const String> before(MAKE_STATIC_STRING_IMPL("::before"));
switch (pseudoId) {
case AFTER:
return after;
case BEFORE:
return before;
default:
return emptyString();
}
}
PseudoElement::PseudoElement(Element& host, PseudoId pseudoId)
: Element(pseudoElementTagName(), host.document(), CreatePseudoElement)
, m_hostElement(&host)
, m_pseudoId(pseudoId)
{
ASSERT(pseudoId == BEFORE || pseudoId == AFTER);
setHasCustomStyleResolveCallbacks();
}
PseudoElement::~PseudoElement()
{
ASSERT(!m_hostElement);
}
void PseudoElement::clearHostElement()
{
InspectorInstrumentation::pseudoElementDestroyed(document().page(), *this);
m_hostElement = nullptr;
}
std::optional<ElementStyle> PseudoElement::resolveCustomStyle(const RenderStyle& parentStyle, const RenderStyle*)
{
auto* style = m_hostElement->renderer()->getCachedPseudoStyle(m_pseudoId, &parentStyle);
if (!style)
return std::nullopt;
return ElementStyle(RenderStyle::clonePtr(*style));
}
void PseudoElement::didAttachRenderers()
{
RenderElement* renderer = this->renderer();
if (!renderer || renderer->style().hasFlowFrom())
return;
const RenderStyle& style = renderer->style();
ASSERT(style.contentData());
for (const ContentData* content = style.contentData(); content; content = content->next()) {
auto child = content->createContentRenderer(document(), style);
if (renderer->isChildAllowed(*child, style))
renderer->addChild(child.leakPtr());
}
}
bool PseudoElement::rendererIsNeeded(const RenderStyle& style)
{
return pseudoElementRendererIsNeeded(&style);
}
void PseudoElement::didRecalcStyle(Style::Change)
{
if (!renderer())
return;
RenderElement& renderer = *this->renderer();
for (RenderObject* child = renderer.nextInPreOrder(&renderer); child; child = child->nextInPreOrder(&renderer)) {
if (!is<RenderImage>(*child) && !is<RenderQuote>(*child))
continue;
auto createdStyle = RenderStyle::createStyleInheritingFromPseudoStyle(renderer.style());
downcast<RenderElement>(*child).setStyle(WTFMove(createdStyle));
}
}
}