void PseudoElement::attach(const AttachContext& context) { ASSERT(!renderer()); Element::attach(context); RenderObject* renderer = this->renderer(); if (!renderer) return; RenderStyle* style = renderer->style(); if (style->hasFlowFrom()) return; if (style->styleType() != BEFORE && style->styleType() != AFTER) return; ASSERT(style->contentData()); for (const ContentData* content = style->contentData(); content; content = content->next()) { RenderObject* child = content->createRenderer(document(), style); if (renderer->isChildAllowed(child, style)) { renderer->addChild(child); if (child->isQuote()) toRenderQuote(child)->attachQuote(); } else child->destroy(); } }
void PseudoElement::didAttachRenderers() { RenderObject* renderer = this->renderer(); if (!renderer || !renderer->style()->regionThread().isEmpty()) return; RenderStyle* style = renderer->style(); ASSERT(style->contentData()); for (const ContentData* content = style->contentData(); content; content = content->next()) { RenderObject* child = content->createRenderer(document(), style); if (renderer->isChildAllowed(child, style)) { renderer->addChild(child); if (child->isQuote()) toRenderQuote(child)->attachQuote(); } else child->destroy(); } }
void RenderObjectChildList::updateBeforeAfterContent(RenderObject* owner, PseudoId type, const RenderObject* styledObject) { // Double check that the document did in fact use generated content rules. Otherwise we should not have been called. ASSERT(owner->document()->usesBeforeAfterRules()); // In CSS2, before/after pseudo-content cannot nest. Check this first. if (owner->style()->styleType() == BEFORE || owner->style()->styleType() == AFTER) return; if (!styledObject) styledObject = owner; RenderStyle* pseudoElementStyle = styledObject->getCachedPseudoStyle(type); RenderObject* child; switch (type) { case BEFORE: child = beforePseudoElementRenderer(owner); break; case AFTER: child = afterPseudoElementRenderer(owner); break; default: ASSERT_NOT_REACHED(); return; } // Whether or not we currently have generated content attached. bool oldContentPresent = child; // Whether or not we now want generated content. bool newContentWanted = pseudoElementStyle && pseudoElementStyle->display() != NONE; // For <q><p/></q>, if this object is the inline continuation of the <q>, we only want to generate // :after content and not :before content. if (newContentWanted && type == BEFORE && owner->isElementContinuation()) newContentWanted = false; // Similarly, if we're the beginning of a <q>, and there's an inline continuation for our object, // then we don't generate the :after content. if (newContentWanted && type == AFTER && owner->virtualContinuation()) newContentWanted = false; // If we don't want generated content any longer, or if we have generated content, but it's no longer // identical to the new content data we want to build render objects for, then we nuke all // of the old generated content. if (oldContentPresent && (!newContentWanted || Node::diff(child->style(), pseudoElementStyle, owner->document()) == Node::Detach)) { // Nuke the child. if (child->style()->styleType() == type) { oldContentPresent = false; child->destroy(); child = (type == BEFORE) ? owner->virtualChildren()->firstChild() : owner->virtualChildren()->lastChild(); } } // If we have no pseudo-element style or if the pseudo-element style's display type is NONE, then we // have no generated content and can now return. if (!newContentWanted) return; if (owner->isRenderInline() && !pseudoElementStyle->isDisplayInlineType() && !pseudoElementStyle->isFloating() && !(pseudoElementStyle->position() == AbsolutePosition || pseudoElementStyle->position() == FixedPosition)) // According to the CSS2 spec (the end of section 12.1), the only allowed // display values for the pseudo style are NONE and INLINE for inline flows. // FIXME: CSS2.1 lifted this restriction, but block display types will crash. // For now we at least relax the restriction to allow all inline types like inline-block // and inline-table. pseudoElementStyle->setDisplay(INLINE); if (oldContentPresent) { updateBeforeAfterStyle(child, type, pseudoElementStyle); return; // We've updated the generated content. That's all we needed to do. } RenderObject* insertBefore = (type == BEFORE) ? owner->virtualChildren()->firstChild() : 0; if (insertBefore && insertBefore->isAnonymousBlock() && insertBefore->childrenInline() && !insertBefore->isEmpty()) { // We are going to add the "before" element. We have to check whether the "insertBefore" element // is an anonymous block with inline children. If it is, then we should insert the "before" element // before the first inline child of the anonymous block, otherwise we will end up with the "before" // element in a different block. We do this only when the anonymous block has children, otherwise // we end up with the before element in a wrong block. insertBefore = insertBefore->firstChild(); } // Nothing goes before the intruded run-in, not even generated content. if (insertBefore && insertBefore->isRunIn() && owner->isRenderBlock() && toRenderBlock(owner)->runInIsPlacedIntoSiblingBlock(insertBefore)) insertBefore = insertBefore->nextSibling(); // Generated content consists of a single container that houses multiple children (specified // by the content property). This generated content container gets the pseudo-element style set on it. RenderObject* generatedContentContainer = 0; // Walk our list of generated content and create render objects for each. for (const ContentData* content = pseudoElementStyle->contentData(); content; content = content->next()) { RenderObject* renderer = createRendererForBeforeAfterContent(owner, content, pseudoElementStyle); if (renderer) { if (!generatedContentContainer) { // Make a generated box that might be any display type now that we are able to drill down into children // to find the original content properly. generatedContentContainer = RenderObject::createObject(owner->document(), pseudoElementStyle); ASSERT(styledObject->node()); // The styled object cannot be anonymous or else it could not have ':before' or ':after' pseudo elements. generatedContentContainer->setNode(styledObject->node()); // This allows access to the generatingNode. generatedContentContainer->setStyle(pseudoElementStyle); if (!owner->isChildAllowed(generatedContentContainer, pseudoElementStyle)) { // The generated content container is not allowed here -> abort. generatedContentContainer->destroy(); renderer->destroy(); return; } // When we don't have a first child and are part of a continuation chain, // insertBefore is incorrectly set to zero above, which causes the :before // child to end up at the end of continuation chain. // See https://bugs.webkit.org/show_bug.cgi?id=78380. if (!insertBefore && type == BEFORE && owner->virtualContinuation()) owner->addChildIgnoringContinuation(generatedContentContainer, 0); else owner->addChild(generatedContentContainer, insertBefore); } if (generatedContentContainer->isChildAllowed(renderer, pseudoElementStyle)) generatedContentContainer->addChild(renderer); else renderer->destroy(); } } if (!generatedContentContainer) return; // Handle placement of run-ins. We do the run-in placement at the end since generatedContentContainer can get destroyed. RenderObject* generatedContentContainerImmediateParent = generatedContentContainer->parent(); if (generatedContentContainerImmediateParent->isRenderBlock()) toRenderBlock(generatedContentContainerImmediateParent)->placeRunInIfNeeded(generatedContentContainer, PlaceGeneratedRunIn); }
void RenderObjectChildList::updateBeforeAfterContent(RenderObject* owner, PseudoId type, RenderObject* styledObject) { // Double check that the document did in fact use generated content rules. Otherwise we should not have been called. ASSERT(owner->document()->usesBeforeAfterRules()); // In CSS2, before/after pseudo-content cannot nest. Check this first. if (owner->style()->styleType() == BEFORE || owner->style()->styleType() == AFTER) return; if (!styledObject) styledObject = owner; RenderStyle* pseudoElementStyle = styledObject->getCachedPseudoStyle(type); RenderObject* child = beforeAfterContainer(owner, type); // Whether or not we currently have generated content attached. bool oldContentPresent = child; // Whether or not we now want generated content. bool newContentWanted = pseudoElementStyle && pseudoElementStyle->display() != NONE; // For <q><p/></q>, if this object is the inline continuation of the <q>, we only want to generate // :after content and not :before content. if (newContentWanted && type == BEFORE && owner->isElementContinuation()) newContentWanted = false; // Similarly, if we're the beginning of a <q>, and there's an inline continuation for our object, // then we don't generate the :after content. if (newContentWanted && type == AFTER && owner->virtualContinuation()) newContentWanted = false; // If we don't want generated content any longer, or if we have generated content, but it's no longer // identical to the new content data we want to build render objects for, then we nuke all // of the old generated content. if (oldContentPresent && (!newContentWanted || Node::diff(child->style(), pseudoElementStyle) == Node::Detach)) { // Nuke the child. if (child->style()->styleType() == type) { oldContentPresent = false; child->destroy(); child = (type == BEFORE) ? owner->virtualChildren()->firstChild() : owner->virtualChildren()->lastChild(); } } // If we have no pseudo-element style or if the pseudo-element style's display type is NONE, then we // have no generated content and can now return. if (!newContentWanted) return; if (owner->isRenderInline() && !pseudoElementStyle->isDisplayInlineType() && pseudoElementStyle->floating() == FNONE && !(pseudoElementStyle->position() == AbsolutePosition || pseudoElementStyle->position() == FixedPosition)) // According to the CSS2 spec (the end of section 12.1), the only allowed // display values for the pseudo style are NONE and INLINE for inline flows. // FIXME: CSS2.1 lifted this restriction, but block display types will crash. // For now we at least relax the restriction to allow all inline types like inline-block // and inline-table. pseudoElementStyle->setDisplay(INLINE); if (oldContentPresent) { if (child && child->style()->styleType() == type) { // We have generated content present still. We want to walk this content and update our // style information with the new pseudo-element style. child->setStyle(pseudoElementStyle); RenderObject* beforeAfterParent = findBeforeAfterParent(child); if (!beforeAfterParent) return; // Note that if we ever support additional types of generated content (which should be way off // in the future), this code will need to be patched. for (RenderObject* genChild = beforeAfterParent->firstChild(); genChild; genChild = genChild->nextSibling()) { if (genChild->isText()) // Generated text content is a child whose style also needs to be set to the pseudo-element style. genChild->setStyle(pseudoElementStyle); else if (genChild->isImage()) { // Images get an empty style that inherits from the pseudo. RefPtr<RenderStyle> style = RenderStyle::create(); style->inheritFrom(pseudoElementStyle); genChild->setStyle(style.release()); } else { // RenderListItem may insert a list marker here. We do not need to care about this case. // Otherwise, genChild must be a first-letter container. updateFirstLetter() will take care of it. ASSERT(genChild->isListMarker() || genChild->style()->styleType() == FIRST_LETTER); } } } return; // We've updated the generated content. That's all we needed to do. } RenderObject* insertBefore = (type == BEFORE) ? owner->virtualChildren()->firstChild() : 0; // Generated content consists of a single container that houses multiple children (specified // by the content property). This generated content container gets the pseudo-element style set on it. RenderObject* generatedContentContainer = 0; // Walk our list of generated content and create render objects for each. for (const ContentData* content = pseudoElementStyle->contentData(); content; content = content->next()) { RenderObject* renderer = 0; switch (content->type()) { case CONTENT_NONE: break; case CONTENT_TEXT: renderer = new (owner->renderArena()) RenderTextFragment(owner->document() /* anonymous object */, content->text()); renderer->setStyle(pseudoElementStyle); break; case CONTENT_OBJECT: { RenderImageGeneratedContent* image = new (owner->renderArena()) RenderImageGeneratedContent(owner->document()); // anonymous object RefPtr<RenderStyle> style = RenderStyle::create(); style->inheritFrom(pseudoElementStyle); image->setStyle(style.release()); if (StyleImage* styleImage = content->image()) image->setStyleImage(styleImage); renderer = image; break; } case CONTENT_COUNTER: renderer = new (owner->renderArena()) RenderCounter(owner->document(), *content->counter()); renderer->setStyle(pseudoElementStyle); break; } if (renderer) { if (!generatedContentContainer) { // Make a generated box that might be any display type now that we are able to drill down into children // to find the original content properly. generatedContentContainer = RenderObject::createObject(owner->document(), pseudoElementStyle); generatedContentContainer->setStyle(pseudoElementStyle); owner->addChild(generatedContentContainer, insertBefore); } generatedContentContainer->addChild(renderer); } } }
void RenderContainer::updatePseudoChild(RenderStyle::PseudoId type, RenderObject* child) { // In CSS2, before/after pseudo-content cannot nest. Check this first. if (style()->styleType() == RenderStyle::BEFORE || style()->styleType() == RenderStyle::AFTER) return; RenderStyle* pseudo = getPseudoStyle(type); // Whether or not we currently have generated content attached. bool oldContentPresent = child && (child->style()->styleType() == type); // Whether or not we now want generated content. bool newContentWanted = pseudo && pseudo->display() != NONE; // For <q><p/></q>, if this object is the inline continuation of the <q>, we only want to generate // :after content and not :before content. if (type == RenderStyle::BEFORE && isInlineContinuation()) newContentWanted = false; // Similarly, if we're the beginning of a <q>, and there's an inline continuation for our object, // then we don't generate the :after content. if (type == RenderStyle::AFTER && isRenderInline() && continuation()) newContentWanted = false; // If we don't want generated content any longer, or if we have generated content, but it's no longer // identical to the new content data we want to build render objects for, then we nuke all // of the old generated content. if (!newContentWanted || (oldContentPresent && !child->style()->contentDataEquivalent(pseudo))) { // Nuke the child. if (child && child->style()->styleType() == type) { oldContentPresent = false; removeChild(child); child = (type == RenderStyle::BEFORE) ? firstChild() : lastChild(); } } // If we have no pseudo-style or if the pseudo's display type is NONE, then we // have no generated content and can now return. if (!newContentWanted) return; if (isInlineFlow() && pseudo->display() != INLINE) // According to the CSS2 spec (the end of section 12.1), the only allowed // display values for the pseudo style are NONE and INLINE. Since we already // determined that the pseudo is not display NONE, any display other than // inline should be mutated to INLINE. pseudo->setDisplay(INLINE); if (oldContentPresent) { if (child && child->style()->styleType() == type) { // We have generated content present still. We want to walk this content and update our // style information with the new pseudo style. child->setStyle(pseudo); // Note that if we ever support additional types of generated content (which should be way off // in the future), this code will need to be patched. for (RenderObject* genChild = child->firstChild(); genChild; genChild = genChild->nextSibling()) { if (genChild->isText()) // Generated text content is a child whose style also needs to be set to the pseudo // style. genChild->setStyle(pseudo); else { // Images get an empty style that inherits from the pseudo. RenderStyle* style = new (renderArena()) RenderStyle(); style->inheritFrom(pseudo); genChild->setStyle(style); } } } return; // We've updated the generated content. That's all we needed to do. } RenderObject* insertBefore = (type == RenderStyle::BEFORE) ? child : 0; // Generated content consists of a single container that houses multiple children (specified // by the content property). This pseudo container gets the pseudo style set on it. RenderObject* pseudoContainer = 0; // Now walk our list of generated content and create render objects for every type // we encounter. for (ContentData* contentData = pseudo->contentData(); contentData; contentData = contentData->_nextContent) { if (!pseudoContainer) pseudoContainer = RenderFlow::createAnonymousFlow(document(), pseudo); /* anonymous box */ if (contentData->contentType() == CONTENT_TEXT) { RenderText* t = new (renderArena()) RenderTextFragment(document() /*anonymous object */, contentData->contentText()); t->setStyle(pseudo); pseudoContainer->addChild(t); } else if (contentData->contentType() == CONTENT_OBJECT) { RenderImage* img = new (renderArena()) RenderImage(document()); /* Anonymous object */ RenderStyle* style = new (renderArena()) RenderStyle(); style->inheritFrom(pseudo); img->setStyle(style); img->setContentObject(contentData->contentObject()); pseudoContainer->addChild(img); } } if (pseudoContainer) { // Add the pseudo after we've installed all our content, so that addChild will be able to find the text // inside the inline for e.g., first-letter styling. addChild(pseudoContainer, insertBefore); } }
static void loadPendingImages(const PendingResources& pendingResources, Document& document, RenderStyle& style, const Element* element) { for (auto currentProperty : pendingResources.pendingImages.keys()) { switch (currentProperty) { case CSSPropertyBackgroundImage: { for (FillLayer* backgroundLayer = &style.ensureBackgroundLayers(); backgroundLayer; backgroundLayer = backgroundLayer->next()) { auto* styleImage = backgroundLayer->image(); if (is<StylePendingImage>(styleImage)) backgroundLayer->setImage(loadPendingImage(document, *styleImage, element)); } break; } case CSSPropertyContent: { for (ContentData* contentData = const_cast<ContentData*>(style.contentData()); contentData; contentData = contentData->next()) { if (is<ImageContentData>(*contentData)) { auto& styleImage = downcast<ImageContentData>(*contentData).image(); if (is<StylePendingImage>(styleImage)) { if (auto loadedImage = loadPendingImage(document, styleImage, element)) downcast<ImageContentData>(*contentData).setImage(WTFMove(loadedImage)); } } } break; } case CSSPropertyCursor: { if (CursorList* cursorList = style.cursors()) { for (size_t i = 0; i < cursorList->size(); ++i) { CursorData& currentCursor = cursorList->at(i); auto* styleImage = currentCursor.image(); if (is<StylePendingImage>(styleImage)) currentCursor.setImage(loadPendingImage(document, *styleImage, element)); } } break; } case CSSPropertyListStyleImage: { auto* styleImage = style.listStyleImage(); if (is<StylePendingImage>(styleImage)) style.setListStyleImage(loadPendingImage(document, *styleImage, element)); break; } case CSSPropertyBorderImageSource: { auto* styleImage = style.borderImageSource(); if (is<StylePendingImage>(styleImage)) style.setBorderImageSource(loadPendingImage(document, *styleImage, element)); break; } case CSSPropertyWebkitBoxReflect: { if (StyleReflection* reflection = style.boxReflect()) { const NinePieceImage& maskImage = reflection->mask(); auto* styleImage = maskImage.image(); if (is<StylePendingImage>(styleImage)) { auto loadedImage = loadPendingImage(document, *styleImage, element); reflection->setMask(NinePieceImage(WTFMove(loadedImage), maskImage.imageSlices(), maskImage.fill(), maskImage.borderSlices(), maskImage.outset(), maskImage.horizontalRule(), maskImage.verticalRule())); } } break; } case CSSPropertyWebkitMaskBoxImageSource: { auto* styleImage = style.maskBoxImageSource(); if (is<StylePendingImage>(styleImage)) style.setMaskBoxImageSource(loadPendingImage(document, *styleImage, element)); break; } case CSSPropertyWebkitMaskImage: { for (FillLayer* maskLayer = &style.ensureMaskLayers(); maskLayer; maskLayer = maskLayer->next()) { auto* styleImage = maskLayer->image(); if (is<StylePendingImage>(styleImage)) maskLayer->setImage(loadPendingImage(document, *styleImage, element)); } break; } #if ENABLE(CSS_SHAPES) case CSSPropertyWebkitShapeOutside: { if (!style.shapeOutside()) return; StyleImage* image = style.shapeOutside()->image(); if (is<StylePendingImage>(image)) style.shapeOutside()->setImage(loadPendingImage(document, *image, element, LoadPolicy::ShapeOutside)); break; } #endif default: ASSERT_NOT_REACHED(); } } }