Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1336)

Side by Side Diff: Source/core/rendering/RenderTextFragment.cpp

Issue 571603003: Convert first letter into a pseudo element. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * (C) 1999 Lars Knoll (knoll@kde.org) 2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org) 3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. 4 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 * 20 *
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/rendering/RenderTextFragment.h" 24 #include "core/rendering/RenderTextFragment.h"
25 25
26 #include "core/dom/FirstLetterPseudoElement.h"
27 #include "core/dom/PseudoElement.h"
28 #include "core/dom/StyleChangeReason.h"
26 #include "core/dom/Text.h" 29 #include "core/dom/Text.h"
27 #include "core/rendering/HitTestResult.h" 30 #include "core/rendering/HitTestResult.h"
28 #include "core/rendering/RenderBlock.h" 31 #include "core/rendering/RenderBlock.h"
29 32
30 namespace blink { 33 namespace blink {
31 34
32 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str, int startOff set, int length) 35 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str, int startOff set, int length)
33 : RenderText(node, str ? str->substring(startOffset, length) : PassRefPtr<St ringImpl>(nullptr)) 36 : RenderText(node, str ? str->substring(startOffset, length) : PassRefPtr<St ringImpl>(nullptr))
34 , m_start(startOffset) 37 , m_start(startOffset)
35 , m_end(length) 38 , m_end(length)
36 , m_firstLetter(nullptr) 39 , m_firstLetterPseudoElement(nullptr)
37 { 40 {
38 } 41 }
39 42
40 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str) 43 RenderTextFragment::RenderTextFragment(Node* node, StringImpl* str)
41 : RenderText(node, str) 44 : RenderText(node, str)
42 , m_start(0) 45 , m_start(0)
43 , m_end(str ? str->length() : 0) 46 , m_end(str ? str->length() : 0)
44 , m_contentString(str) 47 , m_contentString(str)
45 , m_firstLetter(nullptr) 48 , m_firstLetterPseudoElement(nullptr)
46 { 49 {
47 } 50 }
48 51
49 RenderTextFragment::~RenderTextFragment() 52 RenderTextFragment::~RenderTextFragment()
50 { 53 {
54 if (m_firstLetterPseudoElement)
55 m_firstLetterPseudoElement->setRemainingTextRenderer(nullptr);
56 m_firstLetterPseudoElement = nullptr;
51 } 57 }
52 58
53 void RenderTextFragment::trace(Visitor* visitor) 59 PassRefPtr<StringImpl> RenderTextFragment::completeText() const
54 { 60 {
55 visitor->trace(m_firstLetter); 61 Text* text = associatedTextNode();
56 RenderText::trace(visitor); 62 return text ? text->dataImpl() : contentString();
57 }
58
59 RenderText* RenderTextFragment::firstRenderTextInFirstLetter() const
60 {
61 for (RenderObject* current = m_firstLetter; current; current = current->next InPreOrder(m_firstLetter)) {
62 if (current->isText())
63 return toRenderText(current);
64 }
65 return 0;
66 } 63 }
67 64
68 PassRefPtr<StringImpl> RenderTextFragment::originalText() const 65 PassRefPtr<StringImpl> RenderTextFragment::originalText() const
69 { 66 {
70 Node* e = node(); 67 RefPtr<StringImpl> result = completeText();
71 RefPtr<StringImpl> result = ((e && e->isTextNode()) ? toText(e)->dataImpl() : contentString());
72 if (!result) 68 if (!result)
73 return nullptr; 69 return nullptr;
74 return result->substring(start(), end()); 70 return result->substring(start(), end());
75 } 71 }
76 72
77 void RenderTextFragment::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
78 {
79 RenderText::styleDidChange(diff, oldStyle);
80
81 if (RenderBlock* block = blockForAccompanyingFirstLetter()) {
82 block->style()->removeCachedPseudoStyle(FIRST_LETTER);
83 block->updateFirstLetter();
84 }
85 }
86
87 void RenderTextFragment::willBeDestroyed()
88 {
89 if (m_firstLetter)
90 m_firstLetter->destroy();
91 RenderText::willBeDestroyed();
92 }
93
94 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force) 73 void RenderTextFragment::setText(PassRefPtr<StringImpl> text, bool force)
95 { 74 {
96 RenderText::setText(text, force); 75 RenderText::setText(text, force);
97 76
98 m_start = 0; 77 m_start = 0;
99 m_end = textLength(); 78 m_end = textLength();
100 if (m_firstLetter) {
101 // FIXME: We should not modify the structure of the render tree during
102 // layout. crbug.com/370458
103 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
104 79
105 ASSERT(!m_contentString); 80 // If we're the remaining text from a first letter then we have to tell the
106 m_firstLetter->destroy(); 81 // first letter pseudo element to reattach itself so it can re-calculate the
107 m_firstLetter = nullptr; 82 // correct first-letter settings.
108 if (Node* t = node()) { 83 if (RenderObject* previous = previousSibling()) {
109 ASSERT(!t->renderer()); 84 if (!previous->isPseudoElement() || !previous->node()->isFirstLetterPseu doElement())
110 t->setRenderer(this); 85 return;
111 } 86
87 // Tell the first letter container node, and the first-letter node
88 // that their style may have changed.
89 // e.g. fast/css/first-letter-detach.html
90 toFirstLetterPseudoElement(previous->node())->setIsStale();
esprehn 2014/10/07 00:17:40 setIsStale() should probably be something like set
dsinclair 2014/10/07 19:36:21 Done.
91 if (document().lifecycle().state() != DocumentLifecycle::InStyleRecalc)
92 document().setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReason ForTracing::create(StyleChangeReason::PseudoClass));
esprehn 2014/10/07 00:17:40 This is definitely not right, you're causing a _fu
dsinclair 2014/10/07 19:36:21 How do I tell the system that it needs to run styl
112 } 93 }
113 } 94 }
114 95
115 void RenderTextFragment::transformText() 96 void RenderTextFragment::transformText()
116 { 97 {
117 // Don't reset first-letter here because we are only transforming the trunca ted fragment.
Julien - ping for review 2014/10/06 17:47:33 Doesn't this comment still applies?
dsinclair 2014/10/07 19:36:21 Done.
118 if (RefPtr<StringImpl> textToTransform = originalText()) 98 if (RefPtr<StringImpl> textToTransform = originalText())
119 RenderText::setText(textToTransform.release(), true); 99 RenderText::setText(textToTransform.release(), true);
120 } 100 }
121 101
122 UChar RenderTextFragment::previousCharacter() const 102 UChar RenderTextFragment::previousCharacter() const
123 { 103 {
124 if (start()) { 104 if (start()) {
125 Node* e = node(); 105 StringImpl* original = completeText().get();
126 StringImpl* original = ((e && e->isTextNode()) ? toText(e)->dataImpl() : contentString());
127 if (original && start() <= original->length()) 106 if (original && start() <= original->length())
128 return (*original)[start() - 1]; 107 return (*original)[start() - 1];
129 } 108 }
130 109
131 return RenderText::previousCharacter(); 110 return RenderText::previousCharacter();
132 } 111 }
133 112
134 RenderBlock* RenderTextFragment::blockForAccompanyingFirstLetter() const
135 {
136 if (!m_firstLetter)
137 return 0;
138 for (RenderObject* block = m_firstLetter->parent(); block; block = block->pa rent()) {
139 if (block->style()->hasPseudoStyle(FIRST_LETTER) && block->canHaveChildr en() && block->isRenderBlock())
140 return toRenderBlock(block);
141 }
142 return 0;
143 }
144
145 void RenderTextFragment::updateHitTestResult(HitTestResult& result, const Layout Point& point) 113 void RenderTextFragment::updateHitTestResult(HitTestResult& result, const Layout Point& point)
146 { 114 {
147 if (result.innerNode()) 115 if (result.innerNode())
148 return; 116 return;
149 117
150 RenderObject::updateHitTestResult(result, point); 118 RenderObject::updateHitTestResult(result, point);
151 if (m_firstLetter || !node()) 119 if (!node())
152 return;
153 RenderObject* nodeRenderer = node()->renderer();
154 if (!nodeRenderer || !nodeRenderer->isText() || !toRenderText(nodeRenderer)- >isTextFragment())
155 return; 120 return;
156 121
157 if (isDescendantOf(toRenderTextFragment(nodeRenderer)->m_firstLetter)) 122 // FIXME(dsinclair): First letter?
Julien - ping for review 2014/10/06 17:47:33 Let's put an actionable FIXME, preferably with a b
dsinclair 2014/10/07 19:36:21 Leftover from when I was cutting things out, remov
158 result.setIsFirstLetter(true);
159 } 123 }
160 124
125 // If this is the renderer for a first-letter pseudoNode then we have to look
126 // at the node for the remaining text to find our content.
127 Text* RenderTextFragment::associatedTextNode() const
128 {
129 Node* node = this->node();
130 if (!node)
131 return nullptr;
132
133 if (node->isFirstLetterPseudoElement()) {
134 PseudoElement* pseudo = toPseudoElement(node);
135 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRe nderer(*pseudo);
136 if (!nextRenderer)
137 return nullptr;
138 node = nextRenderer->node();
139 }
140 return (node && node->isTextNode()) ? toText(node) : nullptr;
141 }
142
143
Julien - ping for review 2014/10/06 17:47:33 Unneeded space to be removed.
dsinclair 2014/10/07 19:36:21 Done.
161 } // namespace blink 144 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698