Index: Source/core/dom/FirstLetterPseudoElement.cpp |
diff --git a/Source/core/dom/FirstLetterPseudoElement.cpp b/Source/core/dom/FirstLetterPseudoElement.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a63e17aa1efef793e200880f482a49a75c8a398b |
--- /dev/null |
+++ b/Source/core/dom/FirstLetterPseudoElement.cpp |
@@ -0,0 +1,259 @@ |
+// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
Julien - ping for review
2014/10/09 18:24:47
I don't think the new header applied to old conten
dsinclair
2014/10/09 21:14:22
Reverted to the old one to be safe.
|
+ |
+#include "config.h" |
+#include "core/dom/FirstLetterPseudoElement.h" |
+ |
+#include "core/dom/Element.h" |
+#include "core/rendering/RenderObject.h" |
+#include "core/rendering/RenderObjectInlines.h" |
+#include "core/rendering/RenderText.h" |
+#include "core/rendering/RenderTextFragment.h" |
+#include "wtf/text/WTFString.h" |
+#include "wtf/unicode/icu/UnicodeIcu.h" |
+ |
+namespace blink { |
+ |
+using namespace WTF; |
+using namespace Unicode; |
+ |
+// 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 isSpaceOrNewline(UChar c) |
+{ |
+ // Use isASCIISpace() for basic Latin-1. |
+ // This will include newlines, which aren't included in Unicode DirWS. |
+ return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral; |
+} |
+ |
+static inline bool isSpaceForFirstLetter(UChar c) |
+{ |
+ return isSpaceOrNewline(c) || c == noBreakSpace; |
+} |
+ |
+unsigned FirstLetterPseudoElement::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 for allowed punctuation for the :first-letter. |
+ for (; length < textLength; ++length) { |
+ UChar c = text[length]; |
+ if (!isPunctuationForFirstLetter(c)) |
+ break; |
+ } |
+ |
+ // FIXME: If textLength is 0, length may still be 1! |
+ return length; |
+} |
+ |
+ |
+RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& element) |
+{ |
+ RenderObject* parentRenderer = 0; |
+ |
+ // If we are looking at a first letter element then we need to find the |
+ // first letter text renderer from the parent node, and not ourselves. |
+ if (element.isFirstLetterPseudoElement()) |
+ parentRenderer = element.parentOrShadowHostElement()->renderer(); |
+ else |
+ parentRenderer = element.renderer(); |
+ |
+ if (!parentRenderer |
+ || !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER) |
+ || !parentRenderer->canHaveGeneratedChildren() |
+ || !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButton())) |
+ return nullptr; |
+ |
+ // Drill down into our children and look for our first text child. |
+ RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild(); |
+ while (firstLetterTextRenderer) { |
+ // This can be called when the first letter renderer is already in the tree. We do not |
+ // want to consider that renderer for our text renderer so we go to the sibling. |
+ if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) { |
+ firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); |
+ } else if (firstLetterTextRenderer->isText()) { |
+ // FIXME: If there is leading punctuation in a different RenderText than |
+ // the first letter, we'll not apply the correct style to it. |
+ if (firstLetterLength(toRenderText(firstLetterTextRenderer)->originalText())) |
+ break; |
+ firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); |
+ // FIXME: Need to try down why these conditions are needed. |
+ } else if (firstLetterTextRenderer->isListMarker()) { |
+ firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); |
+ |
+ } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) { |
+ if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) { |
+ firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild(); |
+ break; |
+ } |
+ firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); |
+ } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRenderer->isRenderButton() |
+ || firstLetterTextRenderer->isMenuList()) { |
+ return nullptr; |
+ } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER) |
+ && firstLetterTextRenderer->canHaveGeneratedChildren()) { |
+ // There is a renderer further down the tree which has FIRST_LETTER set. When that node |
+ // is attached we will handle setting up the first letter then. |
+ return nullptr; |
+ } else { |
+ firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild(); |
+ } |
+ } |
+ |
+ // No first letter text to display, we're done. |
+ // FIXME: This black-list of disallowed RenderText subclasses is fragile. |
Julien - ping for review
2014/10/09 18:24:48
Could we do a white list instead?
dsinclair
2014/10/09 21:14:22
It _could_ be. But, it'd end up being like 50 entr
Julien - ping for review
2014/10/10 14:47:07
50 is too long indeed. Let's stick to a FIXME.
dsinclair
2014/10/10 18:06:52
Done. crbug.com/422336
|
+ // Should counter be on this list? What about RenderTextFragment? |
+ if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() |
+ || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRenderer)->isWordBreak()) |
+ return nullptr; |
+ |
+ return firstLetterTextRenderer; |
+} |
+ |
+FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent) |
+ : PseudoElement(parent, FIRST_LETTER) |
+ , m_remainingTextRenderer(nullptr) |
+ , m_needsUpdate(false) |
+{ |
+} |
+ |
+void FirstLetterPseudoElement::attach(const AttachContext& context) |
+{ |
+ PseudoElement::attach(context); |
+ attachFirstLetterTextRenderers(); |
+} |
+ |
+void FirstLetterPseudoElement::detach(const AttachContext& context) |
+{ |
+ if (m_remainingTextRenderer) { |
+ if (m_remainingTextRenderer->node()) { |
+ Text* textNode = toText(m_remainingTextRenderer->node()); |
+ m_remainingTextRenderer->setText(textNode->dataImpl(), true); |
+ } |
+ m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr); |
+ } |
+ m_remainingTextRenderer = nullptr; |
+ |
+ PseudoElement::detach(context); |
+} |
+ |
+ |
+RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendererContainer) |
+{ |
+ if (!rendererContainer) |
+ return nullptr; |
+ |
+ RenderObject* styleContainer = parentOrShadowHostElement()->renderer(); |
+ if (!styleContainer) |
+ return nullptr; |
+ |
+ // We always force the pseudo style to recompute as the first-letter style |
+ // computed by the style container may not have taken the renderers styles |
+ // into account. |
+ styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER); |
+ |
+ RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER, rendererContainer->firstLineStyle()); |
+ ASSERT(pseudoStyle); |
+ |
+ // 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; |
+} |
+ |
+void FirstLetterPseudoElement::attachFirstLetterTextRenderers() |
+{ |
+ RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRenderer(*this); |
+ ASSERT(nextRenderer && nextRenderer->isText()); |
+ |
+ // 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 = toRenderText(nextRenderer)->isTextFragment() ? toRenderTextFragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalText(); |
+ ASSERT(oldText.impl()); |
+ |
+ RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent()); |
+ renderer()->setStyle(pseudoStyle); |
+ |
+ // FIXME: This would already have been calculated in firstLetterRenderer. Can we pass the length through? |
+ unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText); |
+ |
+ // Construct a text fragment for the text after the first letter. |
+ // This text fragment might be empty. |
+ RenderTextFragment* remainingText = |
+ new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &nextRenderer->document(), oldText.impl(), length, oldText.length() - length); |
+ remainingText->setStyle(nextRenderer->style()); |
+ remainingText->setFirstLetterPseudoElement(this); |
+ if (remainingText->node()) |
+ remainingText->node()->setRenderer(remainingText); |
+ m_remainingTextRenderer = remainingText; |
+ |
+ RenderObject* nextSibling = renderer()->nextSibling(); |
+ renderer()->parent()->addChild(remainingText, nextSibling); |
+ |
+ // Construct text fragment for the first letter. |
+ RenderTextFragment* letter = new RenderTextFragment(this, oldText.impl(), 0, length); |
+ letter->setStyle(pseudoStyle); |
+ renderer()->addChild(letter); |
+ |
+ nextRenderer->destroy(); |
+ nextRenderer = remainingText; |
+} |
+ |
+void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange) |
+{ |
+ if (!renderer()) |
+ return; |
+ |
+ // The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have |
+ // the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren. |
+ RenderObject* renderer = this->renderer(); |
+ for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) { |
+ // We need to re-calculate the correct style for the first letter element |
+ // and then apply that to the container and the text fragment inside. |
+ if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRenderer) { |
+ if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRenderer->parent())) |
+ child->setPseudoStyle(pseudoStyle); |
+ continue; |
+ } |
+ |
+ // We only manage the style for the generated content items. |
+ if (!child->isText() && !child->isQuote() && !child->isImage()) |
+ continue; |
+ |
+ child->setPseudoStyle(renderer->style()); |
+ } |
+} |
+ |
+} // namespace blink |