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

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 /*
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 static bool 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 (which is
118 // the RenderTextFragment for the remaining text).
119 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) {
120 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
121 } else if (firstLetterTextRenderer->isText()) {
122 // FIXME: If there is leading punctuation in a different RenderText than
123 // the first letter, we'll not apply the correct style to it.
124 RefPtr<StringImpl> str = toRenderText(firstLetterTextRenderer)->isTe xtFragment() ?
125 toRenderTextFragment(firstLetterTextRenderer)->completeText() :
126 toRenderText(firstLetterTextRenderer)->originalText();
127 if (firstLetterLength(str.get()) || isInvalidFirstLetterRenderer(fir stLetterTextRenderer))
128 break;
129 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
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_isDetaching(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_isDetaching && !neededRecalc && parentN ode() && 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_isDetaching = true;
Julien - ping for review 2014/11/14 19:31:00 TemporaryChange<bool> isInDetach(&m_isDetaching);
dsinclair 2014/11/14 20:17:39 Done.
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 m_isDetaching = false;
214 }
215
216
217 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere rContainer)
218 {
219 ASSERT(rendererContainer);
220
221 RenderObject* styleContainer = parentOrShadowHostElement()->renderer();
222 ASSERT(styleContainer);
223
224 // We always force the pseudo style to recompute as the first-letter style
225 // computed by the style container may not have taken the renderers styles
226 // into account.
227 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER);
228
229 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER , rendererContainer->firstLineStyle());
230 ASSERT(pseudoStyle);
231
232 // Force inline display (except for floating first-letters).
233 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
234
235 // CSS2 says first-letter can't be positioned.
236 pseudoStyle->setPosition(StaticPosition);
237
238 return pseudoStyle;
239 }
240
241 void FirstLetterPseudoElement::attachFirstLetterTextRenderers()
242 {
243 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender er(*this);
244 ASSERT(nextRenderer);
245 ASSERT(nextRenderer->isText());
246
247 // The original string is going to be either a generated content string or a DOM node's
248 // string. We want the original string before it got transformed in case fir st-letter has
249 // no text-transform or a different text-transform applied to it.
250 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex t();
251 ASSERT(oldText.impl());
252
253 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent());
254 renderer()->setStyle(pseudoStyle);
255
256 // FIXME: This would already have been calculated in firstLetterRenderer. Ca n we pass the length through?
257 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText);
258
259 // Construct a text fragment for the text after the first letter.
260 // This text fragment might be empty.
261 RenderTextFragment* remainingText =
262 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne xtRenderer->document(), oldText.impl(), length, oldText.length() - length);
263 remainingText->setFirstLetterPseudoElement(this);
264 remainingText->setIsRemainingTextRenderer();
265 remainingText->setStyle(nextRenderer->style());
266
267 if (remainingText->node())
268 remainingText->node()->setRenderer(remainingText);
269
270 m_remainingTextRenderer = remainingText;
271
272 RenderObject* nextSibling = renderer()->nextSibling();
273 renderer()->parent()->addChild(remainingText, nextSibling);
274
275 // Construct text fragment for the first letter.
276 RenderTextFragment* letter = new RenderTextFragment(&nextRenderer->document( ), oldText.impl(), 0, length);
277 letter->setFirstLetterPseudoElement(this);
278 letter->setStyle(pseudoStyle);
279 renderer()->addChild(letter);
280
281 nextRenderer->destroy();
282 }
283
284 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange)
285 {
286 if (!renderer())
287 return;
288
289 // The renderers inside pseudo elements are anonymous so they don't get noti fied of recalcStyle and must have
290 // the style propagated downward manually similar to RenderObject::propagate StyleToAnonymousChildren.
291 RenderObject* renderer = this->renderer();
292 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
293 // We need to re-calculate the correct style for the first letter elemen t
294 // and then apply that to the container and the text fragment inside.
295 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender er) {
296 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe nderer->parent()))
297 child->setPseudoStyle(pseudoStyle);
298 continue;
299 }
300
301 // We only manage the style for the generated content items.
302 if (!child->isText() && !child->isQuote() && !child->isImage())
303 continue;
304
305 child->setPseudoStyle(renderer->style());
306 }
307 }
308
309 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698