Index: Source/core/rendering/RenderTextFragment.cpp |
diff --git a/Source/core/rendering/RenderTextFragment.cpp b/Source/core/rendering/RenderTextFragment.cpp |
index 1d274c83edadcc36139f909a24ac06316af0a676..801e8aa8511c600b67b81c61d997aa51badd1062 100644 |
--- a/Source/core/rendering/RenderTextFragment.cpp |
+++ b/Source/core/rendering/RenderTextFragment.cpp |
@@ -23,6 +23,9 @@ |
#include "config.h" |
#include "core/rendering/RenderTextFragment.h" |
+#include "core/dom/FirstLetterPseudoElement.h" |
+#include "core/dom/PseudoElement.h" |
+#include "core/dom/StyleChangeReason.h" |
#include "core/dom/Text.h" |
#include "core/rendering/HitTestResult.h" |
#include "core/rendering/RenderBlock.h" |
@@ -33,8 +36,9 @@ RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str, int startOff |
: RenderText(node, str ? str->substring(startOffset, length) : PassRefPtr<StringImpl>(nullptr)) |
, m_start(startOffset) |
, m_end(length) |
+ , m_isRemainingTextRenderer(false) |
, m_contentString(str) |
- , m_firstLetter(nullptr) |
+ , m_firstLetterPseudoElement(nullptr) |
{ |
} |
@@ -42,28 +46,35 @@ RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) |
: RenderText(node, str) |
, m_start(0) |
, m_end(str ? str->length() : 0) |
+ , m_isRemainingTextRenderer(false) |
, m_contentString(str) |
- , m_firstLetter(nullptr) |
+ , m_firstLetterPseudoElement(nullptr) |
{ |
} |
RenderTextFragment::~RenderTextFragment() |
{ |
+ ASSERT(!m_firstLetterPseudoElement); |
+} |
+ |
+void RenderTextFragment::destroy() |
+{ |
+ if (m_isRemainingTextRenderer && m_firstLetterPseudoElement) |
+ m_firstLetterPseudoElement->setRemainingTextRenderer(nullptr); |
+ m_firstLetterPseudoElement = nullptr; |
+ RenderText::destroy(); |
} |
void RenderTextFragment::trace(Visitor* visitor) |
{ |
- visitor->trace(m_firstLetter); |
+ visitor->trace(m_firstLetterPseudoElement); |
RenderText::trace(visitor); |
} |
-RenderText* RenderTextFragment::firstRenderTextInFirstLetter() const |
+PassRefPtr<StringImpl> RenderTextFragment::completeText() const |
{ |
- for (RenderObject* current = m_firstLetter; current; current = current->nextInPreOrder(m_firstLetter)) { |
- if (current->isText()) |
- return toRenderText(current); |
- } |
- return 0; |
+ Text* text = associatedTextNode(); |
+ return text ? text->dataImpl() : contentString(); |
} |
void RenderTextFragment::setContentString(StringImpl* str) |
@@ -74,54 +85,38 @@ void RenderTextFragment::setContentString(StringImpl* str) |
PassRefPtr<StringImpl> RenderTextFragment::originalText() const |
{ |
- RefPtr<StringImpl> result = RenderText::originalText(); |
- if (!result) |
- result = m_contentString; |
+ RefPtr<StringImpl> result = completeText(); |
if (!result) |
return nullptr; |
return result->substring(start(), end()); |
} |
-void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) |
-{ |
- RenderText::styleDidChange(diff, oldStyle); |
- |
- if (RenderBlock* block = blockForAccompanyingFirstLetter()) { |
- block->style()->removeCachedPseudoStyle(FIRST_LETTER); |
- block->updateFirstLetter(); |
- } |
-} |
- |
-void RenderTextFragment::willBeDestroyed() |
-{ |
- if (m_firstLetter) |
- m_firstLetter->destroy(); |
- RenderText::willBeDestroyed(); |
-} |
- |
void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) |
{ |
RenderText::setText(text, force); |
m_start = 0; |
m_end = textLength(); |
- if (m_firstLetter) { |
- // FIXME: We should not modify the structure of the render tree during |
- // layout. crbug.com/370458 |
- DeprecatedDisableModifyRenderTreeStructureAsserts disabler; |
- |
- m_firstLetter->destroy(); |
- m_firstLetter = nullptr; |
- if (Node* t = node()) { |
- ASSERT(!t->renderer()); |
- t->setRenderer(this); |
- } |
+ |
+ // If we're the remaining text from a first letter then we have to tell the |
+ // first letter pseudo element to reattach itself so it can re-calculate the |
+ // correct first-letter settings. |
+ if (RenderObject* previous = previousSibling()) { |
+ if (!previous->isPseudoElement() || !previous->node()->isFirstLetterPseudoElement()) |
+ return; |
+ |
+ // Tell the first letter container node, and the first-letter node |
+ // that their style may have changed. |
+ // e.g. fast/css/first-letter-detach.html |
+ toFirstLetterPseudoElement(previous->node())->setNeedsUpdate(); |
} |
} |
void RenderTextFragment::transformText() |
{ |
- // Don't reset first-letter here because we are only transforming the truncated fragment. |
+ // Note, we have to call RenderText::setText here because, if we use our |
+ // version we will, potentially, screw up the first-letter settings where |
+ // we only use portions of the string. |
if (RefPtr<StringImpl> textToTransform = originalText()) |
RenderText::setText(textToTransform.release(), true); |
} |
@@ -129,9 +124,7 @@ void RenderTextFragment::transformText() |
UChar RenderTextFragment::previousCharacter() const |
{ |
if (start()) { |
- RefPtr<StringImpl> original = RenderText::originalText(); |
- if (!original) |
- original = m_contentString; |
+ StringImpl* original = completeText().get(); |
if (original && start() <= original->length()) |
return (*original)[start() - 1]; |
} |
@@ -139,31 +132,22 @@ UChar RenderTextFragment::previousCharacter() const |
return RenderText::previousCharacter(); |
} |
-RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter() const |
+// If this is the renderer for a first-letter pseudoNode then we have to look |
+// at the node for the remaining text to find our content. |
+Text* RenderTextFragment::associatedTextNode() const |
{ |
- if (!m_firstLetter) |
- return 0; |
- for (RenderObject* block = m_firstLetter->parent(); block; block = block->parent()) { |
- if (block->style()->hasPseudoStyle(FIRST_LETTER) && block->canHaveChildren() && block->isRenderBlock()) |
- return toRenderBlock(block); |
- } |
- return 0; |
-} |
+ Node* node = m_isRemainingTextRenderer ? this->node() : this->firstLetterPseudoElement(); |
+ if (!node) |
+ return nullptr; |
-void RenderTextFragment::updateHitTestResult(HitTestResult& result, const LayoutPoint& point) |
-{ |
- if (result.innerNode()) |
- return; |
- |
- RenderObject::updateHitTestResult(result, point); |
- if (m_firstLetter || !node()) |
- return; |
- RenderObject* nodeRenderer = node()->renderer(); |
- if (!nodeRenderer || !nodeRenderer->isText() || !toRenderText(nodeRenderer)->isTextFragment()) |
- return; |
- |
- if (isDescendantOf(toRenderTextFragment(nodeRenderer)->m_firstLetter)) |
- result.setIsFirstLetter(true); |
+ if (node->isFirstLetterPseudoElement()) { |
+ FirstLetterPseudoElement* pseudo = toFirstLetterPseudoElement(node); |
+ RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRenderer(*pseudo); |
+ if (!nextRenderer) |
+ return nullptr; |
+ node = nextRenderer->node(); |
+ } |
+ return (node && node->isTextNode()) ? toText(node) : nullptr; |
} |
} // namespace blink |