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

Side by Side Diff: Source/core/dom/FirstLetterPseudoElement.cpp

Issue 672953002: Convert first letter into a pseudo element. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month 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
(Empty)
1 /*
leviw_travelin_and_unemployed 2014/11/11 21:22:04 You should be able to use the new shorter license
dsinclair 2014/11/11 21:24:32 Heh, I played this game last time. I left it as th
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2007 David Smith (catfish.man@gmail.com)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25 #include "core/dom/FirstLetterPseudoElement.h"
26
27 #include "core/dom/Element.h"
28 #include "core/rendering/RenderObject.h"
29 #include "core/rendering/RenderObjectInlines.h"
30 #include "core/rendering/RenderText.h"
31 #include "core/rendering/RenderTextFragment.h"
32 #include "wtf/text/WTFString.h"
33 #include "wtf/unicode/icu/UnicodeIcu.h"
34
35 namespace blink {
36
37 using namespace WTF;
38 using namespace Unicode;
39
40 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
41 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
42 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
43 static inline bool isPunctuationForFirstLetter(UChar c)
44 {
45 CharCategory charCategory = category(c);
46 return charCategory == Punctuation_Open
47 || charCategory == Punctuation_Close
48 || charCategory == Punctuation_InitialQuote
49 || charCategory == Punctuation_FinalQuote
50 || charCategory == Punctuation_Other;
51 }
52
53 static inline bool isSpaceForFirstLetter(UChar c)
54 {
55 return isSpaceOrNewline(c) || c == noBreakSpace;
56 }
57
58 unsigned FirstLetterPseudoElement::firstLetterLength(const String& text)
59 {
60 unsigned length = 0;
61 unsigned textLength = text.length();
62
63 if (textLength == 0)
64 return length;
65
66 // Account for leading spaces first.
67 while (length < textLength && isSpaceForFirstLetter(text[length]))
68 length++;
69 // Now account for leading punctuation.
70 while (length < textLength && isPunctuationForFirstLetter(text[length]))
71 length++;
72
73 // Bail if we didn't find a letter before the end of the text or before a sp ace.
74 if (isSpaceForFirstLetter(text[length]) || length == textLength)
75 return 0;
76
77 // Account the next character for first letter.
78 length++;
79
80 // Keep looking for allowed punctuation for the :first-letter.
81 for (; length < textLength; ++length) {
82 UChar c = text[length];
83 if (!isPunctuationForFirstLetter(c))
84 break;
85 }
86 return length;
87 }
88
89 // Once we see any of these renderers we can stop looking for first-letter as
90 // they signal the end of the first line of text.
91 bool FirstLetterPseudoElement::isInvalidFirstLetterRenderer(const RenderObject* obj)
92 {
93 return (obj->isBR() || (obj->isText() && toRenderText(obj)->isWordBreak()));
94 }
95
96 RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& e lement)
97 {
98 RenderObject* parentRenderer = 0;
99
100 // If we are looking at a first letter element then we need to find the
101 // first letter text renderer from the parent node, and not ourselves.
102 if (element.isFirstLetterPseudoElement())
103 parentRenderer = element.parentOrShadowHostElement()->renderer();
104 else
105 parentRenderer = element.renderer();
106
107 if (!parentRenderer
108 || !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER)
109 || !parentRenderer->canHaveGeneratedChildren()
110 || !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButt on()))
111 return nullptr;
112
113 // Drill down into our children and look for our first text child.
114 RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild();
115 while (firstLetterTextRenderer) {
116 // This can be called when the first letter renderer is already in the t ree. We do not
117 // want to consider that renderer for our text renderer so we go to the sibling.
Julien - ping for review 2014/11/12 21:58:51 I don't understand this explanation: why is that c
dsinclair 2014/11/13 17:55:36 The sibling of the first-letter renderer will be t
118 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) {
119 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
120 } else if (firstLetterTextRenderer->isText()) {
121 // FIXME: If there is leading punctuation in a different RenderText than
122 // the first letter, we'll not apply the correct style to it.
123 RefPtr<StringImpl> str = toRenderText(firstLetterTextRenderer)->isTe xtFragment() ?
124 toRenderTextFragment(firstLetterTextRenderer)->completeText() :
125 toRenderText(firstLetterTextRenderer)->originalText();
126 if (firstLetterLength(str.get()) || isInvalidFirstLetterRenderer(fir stLetterTextRenderer))
127 break;
128 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
129 // FIXME: Need to track down why these conditions are needed.
Julien - ping for review 2014/11/12 21:58:51 Do we need a bug?
dsinclair 2014/11/13 17:55:36 Removed the comment.
130 } else if (firstLetterTextRenderer->isListMarker()) {
131 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
132 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) {
133 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
134 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d();
135 break;
136 }
137 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
138 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton()
139 || firstLetterTextRenderer->isMenuList()) {
140 return nullptr;
141 } else if (firstLetterTextRenderer->isFlexibleBoxIncludingDeprecated() | | firstLetterTextRenderer->isRenderGrid()) {
142 return nullptr;
143 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER )
144 && firstLetterTextRenderer->canHaveGeneratedChildren()) {
145 // There is a renderer further down the tree which has FIRST_LETTER set. When that node
146 // is attached we will handle setting up the first letter then.
147 return nullptr;
148 } else {
149 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
150 }
151 }
152
153 // No first letter text to display, we're done.
154 // FIXME: This black-list of disallowed RenderText subclasses is fragile. cr bug.com/422336.
155 // Should counter be on this list? What about RenderTextFragment?
156 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() || isInva lidFirstLetterRenderer(firstLetterTextRenderer))
157 return nullptr;
158
159 return firstLetterTextRenderer;
160 }
161
162 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent)
163 : PseudoElement(parent, FIRST_LETTER)
164 , m_remainingTextRenderer(nullptr)
165 , m_needsUpdate(false)
166 , m_detaching(false)
167 {
168 }
169
170 FirstLetterPseudoElement::~FirstLetterPseudoElement()
171 {
172 }
173
174 void FirstLetterPseudoElement::trace(Visitor* visitor)
175 {
176 visitor->trace(m_remainingTextRenderer);
177 PseudoElement::trace(visitor);
178 }
179
180 void FirstLetterPseudoElement::setNeedsUpdate()
181 {
182 m_needsUpdate = true;
183 bool neededRecalc = needsStyleRecalc();
184 setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::create(St yleChangeReason::PseudoClass));
185
186 // If style recalc is currently executing, and we have already been recalc'd
187 // we need to tell our parent to do the recalc again because we need to
188 // re-initialize the first letter state. This can happen for things like
189 // RenderQuote where quotes processed later can effect things already styled .
190 if (document().inStyleRecalc() && !m_detaching && !neededRecalc && parentNod e() && parentNode()->isElementNode())
191 toElement(parentNode())->recalcStyle(UpdatePseudoElements);
192 }
193
194 void FirstLetterPseudoElement::attach(const AttachContext& context)
195 {
196 PseudoElement::attach(context);
197 attachFirstLetterTextRenderers();
198 }
199
200 void FirstLetterPseudoElement::detach(const AttachContext& context)
201 {
202 m_detaching = true;
Julien - ping for review 2014/11/12 21:58:51 The name of this variable is weird. I thought it w
dsinclair 2014/11/13 17:55:36 I've added a reset of the variable to false at the
203 if (m_remainingTextRenderer) {
204 if (m_remainingTextRenderer->node()) {
205 Text* textNode = toText(m_remainingTextRenderer->node());
206 m_remainingTextRenderer->setText(textNode->dataImpl(), true);
207 }
208 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr);
209 }
210 m_remainingTextRenderer = nullptr;
211
212 PseudoElement::detach(context);
213 }
214
215
216 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere rContainer)
217 {
218 ASSERT(rendererContainer);
219
220 RenderObject* styleContainer = parentOrShadowHostElement()->renderer();
221 ASSERT(styleContainer);
222
223 // We always force the pseudo style to recompute as the first-letter style
224 // computed by the style container may not have taken the renderers styles
225 // into account.
226 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER);
227
228 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER , rendererContainer->firstLineStyle());
229 ASSERT(pseudoStyle);
230
231 // Force inline display (except for floating first-letters).
232 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
233
234 // CSS2 says first-letter can't be positioned.
235 pseudoStyle->setPosition(StaticPosition);
236
237 return pseudoStyle;
238 }
239
240 void FirstLetterPseudoElement::attachFirstLetterTextRenderers()
241 {
242 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender er(*this);
243 ASSERT(nextRenderer);
244 ASSERT(nextRenderer->isText());
245
246 // The original string is going to be either a generated content string or a DOM node's
247 // string. We want the original string before it got transformed in case fir st-letter has
248 // no text-transform or a different text-transform applied to it.
249 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex t();
250 ASSERT(oldText.impl());
251
252 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent());
253 renderer()->setStyle(pseudoStyle);
254
255 // FIXME: This would already have been calculated in firstLetterRenderer. Ca n we pass the length through?
256 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText);
257
258 // Construct a text fragment for the text after the first letter.
259 // This text fragment might be empty.
260 RenderTextFragment* remainingText =
261 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne xtRenderer->document(), oldText.impl(), length, oldText.length() - length);
262 remainingText->setFirstLetterPseudoElement(this);
263 remainingText->setIsRemainingTextRenderer();
264 remainingText->setStyle(nextRenderer->style());
265
266 if (remainingText->node())
267 remainingText->node()->setRenderer(remainingText);
268
269 m_remainingTextRenderer = remainingText;
270
271 RenderObject* nextSibling = renderer()->nextSibling();
272 renderer()->parent()->addChild(remainingText, nextSibling);
273
274 // Construct text fragment for the first letter.
275 RenderTextFragment* letter = new RenderTextFragment(&nextRenderer->document( ), oldText.impl(), 0, length);
276 letter->setFirstLetterPseudoElement(this);
277 letter->setStyle(pseudoStyle);
278 renderer()->addChild(letter);
279
280 nextRenderer->destroy();
281 }
282
283 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange)
284 {
285 if (!renderer())
286 return;
287
288 // The renderers inside pseudo elements are anonymous so they don't get noti fied of recalcStyle and must have
289 // the style propagated downward manually similar to RenderObject::propagate StyleToAnonymousChildren.
290 RenderObject* renderer = this->renderer();
291 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
292 // We need to re-calculate the correct style for the first letter elemen t
293 // and then apply that to the container and the text fragment inside.
294 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender er) {
295 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe nderer->parent()))
296 child->setPseudoStyle(pseudoStyle);
297 continue;
298 }
299
300 // We only manage the style for the generated content items.
301 if (!child->isText() && !child->isQuote() && !child->isImage())
302 continue;
303
304 child->setPseudoStyle(renderer->style());
305 }
306 }
Julien - ping for review 2014/11/12 21:58:51 It's a bit sad to have this function duplicated fr
dsinclair 2014/11/13 17:55:36 Acknowledged.
307
308 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698