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

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
« no previous file with comments | « Source/core/dom/FirstLetterPseudoElement.h ('k') | Source/core/dom/Node.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/TemporaryChange.h"
33 #include "wtf/text/WTFString.h"
34 #include "wtf/unicode/icu/UnicodeIcu.h"
35
36 namespace blink {
37
38 using namespace WTF;
39 using namespace Unicode;
40
41 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
42 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
43 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
44 static inline bool isPunctuationForFirstLetter(UChar c)
45 {
46 CharCategory charCategory = category(c);
47 return charCategory == Punctuation_Open
48 || charCategory == Punctuation_Close
49 || charCategory == Punctuation_InitialQuote
50 || charCategory == Punctuation_FinalQuote
51 || charCategory == Punctuation_Other;
52 }
53
54 static inline bool isSpaceForFirstLetter(UChar c)
55 {
56 return isSpaceOrNewline(c) || c == noBreakSpace;
57 }
58
59 unsigned FirstLetterPseudoElement::firstLetterLength(const String& text)
60 {
61 unsigned length = 0;
62 unsigned textLength = text.length();
63
64 if (textLength == 0)
65 return length;
66
67 // Account for leading spaces first.
68 while (length < textLength && isSpaceForFirstLetter(text[length]))
69 length++;
70 // Now account for leading punctuation.
71 while (length < textLength && isPunctuationForFirstLetter(text[length]))
72 length++;
73
74 // Bail if we didn't find a letter before the end of the text or before a sp ace.
75 if (isSpaceForFirstLetter(text[length]) || length == textLength)
76 return 0;
77
78 // Account the next character for first letter.
79 length++;
80
81 // Keep looking for allowed punctuation for the :first-letter.
82 for (; length < textLength; ++length) {
83 UChar c = text[length];
84 if (!isPunctuationForFirstLetter(c))
85 break;
86 }
87 return length;
88 }
89
90 // Once we see any of these renderers we can stop looking for first-letter as
91 // they signal the end of the first line of text.
92 static bool isInvalidFirstLetterRenderer(const RenderObject* obj)
93 {
94 return (obj->isBR() || (obj->isText() && toRenderText(obj)->isWordBreak()));
95 }
96
97 RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& e lement)
98 {
99 RenderObject* parentRenderer = 0;
100
101 // If we are looking at a first letter element then we need to find the
102 // first letter text renderer from the parent node, and not ourselves.
103 if (element.isFirstLetterPseudoElement())
104 parentRenderer = element.parentOrShadowHostElement()->renderer();
105 else
106 parentRenderer = element.renderer();
107
108 if (!parentRenderer
109 || !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER)
110 || !parentRenderer->canHaveGeneratedChildren()
111 || !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButt on()))
112 return nullptr;
113
114 // Drill down into our children and look for our first text child.
115 RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild();
116 while (firstLetterTextRenderer) {
117 // This can be called when the first letter renderer is already in the t ree. We do not
118 // want to consider that renderer for our text renderer so we go to the sibling (which is
119 // the RenderTextFragment for the remaining text).
120 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) {
121 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
122 } else if (firstLetterTextRenderer->isText()) {
123 // FIXME: If there is leading punctuation in a different RenderText than
124 // the first letter, we'll not apply the correct style to it.
125 RefPtr<StringImpl> str = toRenderText(firstLetterTextRenderer)->isTe xtFragment() ?
126 toRenderTextFragment(firstLetterTextRenderer)->completeText() :
127 toRenderText(firstLetterTextRenderer)->originalText();
128 if (firstLetterLength(str.get()) || isInvalidFirstLetterRenderer(fir stLetterTextRenderer))
129 break;
130 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
131 } else if (firstLetterTextRenderer->isListMarker()) {
132 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
133 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) {
134 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
135 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d();
136 break;
137 }
138 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
139 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton()
140 || firstLetterTextRenderer->isMenuList()) {
141 return nullptr;
142 } else if (firstLetterTextRenderer->isFlexibleBoxIncludingDeprecated() | | firstLetterTextRenderer->isRenderGrid()) {
143 return nullptr;
144 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER )
145 && firstLetterTextRenderer->canHaveGeneratedChildren()) {
146 // There is a renderer further down the tree which has FIRST_LETTER set. When that node
147 // is attached we will handle setting up the first letter then.
148 return nullptr;
149 } else {
150 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
151 }
152 }
153
154 // No first letter text to display, we're done.
155 // FIXME: This black-list of disallowed RenderText subclasses is fragile. cr bug.com/422336.
156 // Should counter be on this list? What about RenderTextFragment?
157 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() || isInva lidFirstLetterRenderer(firstLetterTextRenderer))
158 return nullptr;
159
160 return firstLetterTextRenderer;
161 }
162
163 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent)
164 : PseudoElement(parent, FIRST_LETTER)
165 , m_remainingTextRenderer(nullptr)
166 , m_needsUpdate(false)
167 , m_isInDetach(false)
168 {
169 }
170
171 FirstLetterPseudoElement::~FirstLetterPseudoElement()
172 {
173 }
174
175 void FirstLetterPseudoElement::trace(Visitor* visitor)
176 {
177 visitor->trace(m_remainingTextRenderer);
178 PseudoElement::trace(visitor);
179 }
180
181 void FirstLetterPseudoElement::setNeedsUpdate()
182 {
183 m_needsUpdate = true;
184 bool neededRecalc = needsStyleRecalc();
185 setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::create(St yleChangeReason::PseudoClass));
186
187 // If style recalc is currently executing, and we have already been recalc'd
188 // we need to tell our parent to do the recalc again because we need to
189 // re-initialize the first letter state. This can happen for things like
190 // RenderQuote where quotes processed later can effect things already styled .
191 if (document().inStyleRecalc() && !m_isInDetach && !neededRecalc && parentNo de() && parentNode()->isElementNode())
192 toElement(parentNode())->recalcStyle(UpdatePseudoElements);
193 }
194
195 void FirstLetterPseudoElement::attach(const AttachContext& context)
196 {
197 PseudoElement::attach(context);
198 attachFirstLetterTextRenderers();
199 }
200
201 void FirstLetterPseudoElement::detach(const AttachContext& context)
202 {
203 TemporaryChange<bool> isInDetach(m_isInDetach, true);
204
205 if (m_remainingTextRenderer) {
206 if (m_remainingTextRenderer->node()) {
207 Text* textNode = toText(m_remainingTextRenderer->node());
208 m_remainingTextRenderer->setText(textNode->dataImpl(), true);
209 }
210 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr);
211 }
212 m_remainingTextRenderer = nullptr;
213
214 PseudoElement::detach(context);
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
« no previous file with comments | « Source/core/dom/FirstLetterPseudoElement.h ('k') | Source/core/dom/Node.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698