Index: Source/core/rendering/RenderBlock.cpp |
diff --git a/Source/core/rendering/RenderBlock.cpp b/Source/core/rendering/RenderBlock.cpp |
index 688af1cb760806023fb06db570ad475e39898d1e..f37f40d11d884c3c068a8e0ef58caf16ee15b7b9 100644 |
--- a/Source/core/rendering/RenderBlock.cpp |
+++ b/Source/core/rendering/RenderBlock.cpp |
@@ -1358,6 +1358,9 @@ void RenderBlock::layout() |
{ |
OverflowEventDispatcher dispatcher(this); |
+ // Update our first letter info now. |
+ updateFirstLetter(); |
+ |
// Table cells call layoutBlock directly, so don't add any logic here. Put code into |
// layoutBlock(). |
layoutBlock(false); |
@@ -3168,6 +3171,8 @@ void RenderBlock::computePreferredLogicalWidths() |
{ |
ASSERT(preferredLogicalWidthsDirty()); |
+ updateFirstLetter(); |
+ |
m_minPreferredLogicalWidth = 0; |
m_maxPreferredLogicalWidth = 0; |
@@ -3523,6 +3528,248 @@ RenderBlock* RenderBlock::firstLineBlock() const |
return firstLineBlock; |
} |
+static RenderStyle* styleForFirstLetter(RenderObject* firstLetterBlock, RenderObject* firstLetterContainer) |
+{ |
+ RenderStyle* pseudoStyle = firstLetterBlock->getCachedPseudoStyle(FIRST_LETTER, firstLetterContainer->firstLineStyle()); |
+ // Force inline display (except for floating first-letters). |
+ pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE); |
+ // CSS2 says first-letter can't be positioned. |
+ pseudoStyle->setPosition(StaticPosition); |
+ return pseudoStyle; |
+} |
+ |
+// CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter |
+// "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps), "close" (Pe), |
+// "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that precedes or follows the first letter should be included" |
+static inline bool isPunctuationForFirstLetter(UChar c) |
+{ |
+ CharCategory charCategory = category(c); |
+ return charCategory == Punctuation_Open |
+ || charCategory == Punctuation_Close |
+ || charCategory == Punctuation_InitialQuote |
+ || charCategory == Punctuation_FinalQuote |
+ || charCategory == Punctuation_Other; |
+} |
+ |
+static inline bool isSpaceForFirstLetter(UChar c) |
+{ |
+ return isSpaceOrNewline(c) || c == noBreakSpace; |
+} |
+ |
+static inline RenderObject* findFirstLetterBlock(RenderBlock* start) |
+{ |
+ RenderObject* firstLetterBlock = start; |
+ while (true) { |
+ bool canHaveFirstLetterRenderer = firstLetterBlock->style()->hasPseudoStyle(FIRST_LETTER) |
+ && firstLetterBlock->canHaveGeneratedChildren() |
+ && isRenderBlockFlowOrRenderButton(firstLetterBlock); |
+ if (canHaveFirstLetterRenderer) |
+ return firstLetterBlock; |
+ |
+ RenderObject* parentBlock = firstLetterBlock->parent(); |
+ if (firstLetterBlock->isReplaced() || !parentBlock |
+ || !isRenderBlockFlowOrRenderButton(parentBlock)) { |
+ return 0; |
+ } |
+ ASSERT(parentBlock->isRenderBlock()); |
+ if (toRenderBlock(parentBlock)->firstChild() != firstLetterBlock) |
+ return 0; |
+ firstLetterBlock = parentBlock; |
+ } |
+ |
+ return 0; |
+} |
+ |
+void RenderBlock::updateFirstLetterStyle(RenderObject* firstLetterBlock, RenderObject* currentChild) |
+{ |
+ RenderObject* firstLetter = currentChild->parent(); |
+ RenderObject* firstLetterContainer = firstLetter->parent(); |
+ RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetterContainer); |
+ ASSERT(firstLetter->isFloating() || firstLetter->isInline()); |
+ |
+ if (RenderStyle::stylePropagationDiff(firstLetter->style(), pseudoStyle) == Reattach) { |
+ // The first-letter renderer needs to be replaced. Create a new renderer of the right type. |
+ RenderBoxModelObject* newFirstLetter; |
+ if (pseudoStyle->display() == INLINE) |
+ newFirstLetter = RenderInline::createAnonymous(&document()); |
+ else |
+ newFirstLetter = RenderBlockFlow::createAnonymous(&document()); |
+ newFirstLetter->setStyle(pseudoStyle); |
+ |
+ // Move the first letter into the new renderer. |
+ while (RenderObject* child = firstLetter->slowFirstChild()) { |
+ if (child->isText()) |
+ toRenderText(child)->removeAndDestroyTextBoxes(); |
+ firstLetter->removeChild(child); |
+ newFirstLetter->addChild(child, 0); |
+ } |
+ |
+ RenderObject* nextSibling = firstLetter->nextSibling(); |
+ if (RenderTextFragment* remainingText = toRenderBoxModelObject(firstLetter)->firstLetterRemainingText()) { |
+ ASSERT(remainingText->isAnonymous() || remainingText->node()->renderer() == remainingText); |
+ // Replace the old renderer with the new one. |
+ remainingText->setFirstLetter(newFirstLetter); |
+ newFirstLetter->setFirstLetterRemainingText(remainingText); |
+ } |
+ // To prevent removal of single anonymous block in RenderBlock::removeChild and causing |
+ // |nextSibling| to go stale, we remove the old first letter using removeChildNode first. |
+ firstLetterContainer->virtualChildren()->removeChildNode(firstLetterContainer, firstLetter); |
+ firstLetter->destroy(); |
+ firstLetter = newFirstLetter; |
+ firstLetterContainer->addChild(firstLetter, nextSibling); |
+ } else { |
+ firstLetter->setStyle(pseudoStyle); |
+ } |
+ |
+ for (RenderObject* genChild = firstLetter->slowFirstChild(); genChild; genChild = genChild->nextSibling()) { |
+ if (genChild->isText()) |
+ genChild->setStyle(pseudoStyle); |
+ } |
+} |
+ |
+static inline unsigned firstLetterLength(const String& text) |
+{ |
+ unsigned length = 0; |
+ unsigned textLength = text.length(); |
+ |
+ // Account for leading spaces first. |
+ while (length < textLength && isSpaceForFirstLetter(text[length])) |
+ length++; |
+ |
+ // Now account for leading punctuation. |
+ while (length < textLength && isPunctuationForFirstLetter(text[length])) |
+ length++; |
+ |
+ // Bail if we didn't find a letter before the end of the text or before a space. |
+ if (isSpaceForFirstLetter(text[length]) || (textLength && length == textLength)) |
+ return 0; |
+ |
+ // Account the next character for first letter. |
+ length++; |
+ |
+ // Keep looking allowed punctuation for the :first-letter. |
+ for (unsigned scanLength = length; scanLength < textLength; ++scanLength) { |
+ UChar c = text[scanLength]; |
+ |
+ if (!isPunctuationForFirstLetter(c)) |
+ break; |
+ |
+ length = scanLength + 1; |
+ } |
+ |
+ // FIXME: If textLength is 0, length may still be 1! |
+ return length; |
+} |
+ |
+void RenderBlock::createFirstLetterRenderer(RenderObject* firstLetterBlock, RenderText& currentChild, unsigned length) |
+{ |
+ ASSERT(length); |
+ |
+ RenderObject* firstLetterContainer = currentChild.parent(); |
+ RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetterContainer); |
+ RenderBoxModelObject* firstLetter = 0; |
+ if (pseudoStyle->display() == INLINE) |
+ firstLetter = RenderInline::createAnonymous(&document()); |
+ else |
+ firstLetter = RenderBlockFlow::createAnonymous(&document()); |
+ firstLetter->setStyle(pseudoStyle); |
+ |
+ // FIXME: The first letter code should not modify the render tree during |
+ // layout. crbug.com/370458 |
+ DeprecatedDisableModifyRenderTreeStructureAsserts disabler; |
+ |
+ firstLetterContainer->addChild(firstLetter, ¤tChild); |
+ |
+ // The original string is going to be either a generated content string or a DOM node's |
+ // string. We want the original string before it got transformed in case first-letter has |
+ // no text-transform or a different text-transform applied to it. |
+ String oldText = currentChild.originalText(); |
+ ASSERT(oldText.impl()); |
+ |
+ // Construct a text fragment for the text after the first letter. |
+ // This text fragment might be empty. |
+ RenderTextFragment* remainingText = |
+ new RenderTextFragment(currentChild.node() ? currentChild.node() : ¤tChild.document(), oldText.impl(), length, oldText.length() - length); |
+ remainingText->setStyle(currentChild.style()); |
+ if (remainingText->node()) |
+ remainingText->node()->setRenderer(remainingText); |
+ |
+ firstLetterContainer->addChild(remainingText, ¤tChild); |
+ firstLetterContainer->removeChild(¤tChild); |
+ remainingText->setFirstLetter(firstLetter); |
+ firstLetter->setFirstLetterRemainingText(remainingText); |
+ |
+ // construct text fragment for the first letter |
+ RenderTextFragment* letter = |
+ new RenderTextFragment(remainingText->node() ? remainingText->node() : &remainingText->document(), oldText.impl(), 0, length); |
+ letter->setStyle(pseudoStyle); |
+ firstLetter->addChild(letter); |
+ |
+ currentChild.destroy(); |
+} |
+ |
+void RenderBlock::updateFirstLetter() |
+{ |
+ if (!document().styleEngine()->usesFirstLetterRules()) |
+ return; |
+ // Don't recur |
+ if (style()->styleType() == FIRST_LETTER) |
+ return; |
+ |
+ // FIXME: We need to destroy the first-letter object if it is no longer the first child. Need to find |
+ // an efficient way to check for that situation though before implementing anything. |
+ RenderObject* firstLetterBlock = findFirstLetterBlock(this); |
+ if (!firstLetterBlock) |
+ return; |
+ |
+ // Drill into inlines looking for our first text child. |
+ RenderObject* currChild = firstLetterBlock->slowFirstChild(); |
+ unsigned length = 0; |
+ while (currChild) { |
+ if (currChild->isText()) { |
+ // FIXME: If there is leading punctuation in a different RenderText than |
+ // the first letter, we'll not apply the correct style to it. |
+ length = firstLetterLength(toRenderText(currChild)->originalText()); |
+ if (length) |
+ break; |
+ currChild = currChild->nextSibling(); |
+ } else if (currChild->isListMarker()) { |
+ currChild = currChild->nextSibling(); |
+ } else if (currChild->isFloatingOrOutOfFlowPositioned()) { |
+ if (currChild->style()->styleType() == FIRST_LETTER) { |
+ currChild = currChild->slowFirstChild(); |
+ break; |
+ } |
+ currChild = currChild->nextSibling(); |
+ } else if (currChild->isReplaced() || currChild->isRenderButton() || currChild->isMenuList()) { |
+ break; |
+ } else if (currChild->style()->hasPseudoStyle(FIRST_LETTER) && currChild->canHaveGeneratedChildren()) { |
+ // We found a lower-level node with first-letter, which supersedes the higher-level style |
+ firstLetterBlock = currChild; |
+ currChild = currChild->slowFirstChild(); |
+ } else { |
+ currChild = currChild->slowFirstChild(); |
+ } |
+ } |
+ |
+ if (!currChild || !isRenderBlockFlowOrRenderButton(firstLetterBlock)) |
+ return; |
+ |
+ // If the child already has style, then it has already been created, so we just want |
+ // to update it. |
+ if (currChild->parent()->style()->styleType() == FIRST_LETTER) { |
+ updateFirstLetterStyle(firstLetterBlock, currChild); |
+ return; |
+ } |
+ |
+ // FIXME: This black-list of disallowed RenderText subclasses is fragile. |
+ // Should counter be on this list? What about RenderTextFragment? |
+ if (!currChild->isText() || currChild->isBR() || toRenderText(currChild)->isWordBreak()) |
+ return; |
+ |
+ createFirstLetterRenderer(firstLetterBlock, toRenderText(*currChild), length); |
+} |
+ |
// Helper methods for obtaining the last line, computing line counts and heights for line counts |
// (crawling into blocks). |
static bool shouldCheckLines(RenderObject* obj) |