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

Side by Side Diff: Source/core/dom/FirstLetterPseudoElement.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
(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 isSpaceOrNewline(UChar c)
54 {
55 // Use isASCIISpace() for basic Latin-1.
56 // This will include newlines, which aren't included in Unicode DirWS.
57 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF: :Unicode::WhiteSpaceNeutral;
58 }
59
60 static inline bool isSpaceForFirstLetter(UChar c)
61 {
62 return isSpaceOrNewline(c) || c == noBreakSpace;
63 }
64
65 unsigned FirstLetterPseudoElement::firstLetterLength(const String& text)
66 {
67 unsigned length = 0;
68 unsigned textLength = text.length();
69
70 // Account for leading spaces first.
71 while (length < textLength && isSpaceForFirstLetter(text[length]))
72 length++;
73
74 // Now account for leading punctuation.
75 while (length < textLength && isPunctuationForFirstLetter(text[length]))
76 length++;
77
78 // Bail if we didn't find a letter before the end of the text or before a sp ace.
79 if (isSpaceForFirstLetter(text[length]) || (textLength && length == textLeng th))
80 return 0;
81
82 // Account the next character for first letter.
83 length++;
84
85 // Keep looking for allowed punctuation for the :first-letter.
86 for (; length < textLength; ++length) {
87 UChar c = text[length];
88 if (!isPunctuationForFirstLetter(c))
89 break;
90 }
91
92 // FIXME: If textLength is 0, length may still be 1!
93 return length;
94 }
95
96
97 RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& e lement)
98 {
99 RenderObject* parentRenderer;
esprehn 2014/10/07 00:17:40 = 0;
dsinclair 2014/10/07 19:36:20 Done.
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())
esprehn 2014/10/07 00:17:40 This doesn't make sense, why does this code think
dsinclair 2014/10/07 19:36:20 This does get called on non-PseudoElements. We use
104 parentRenderer = element.parentOrShadowHostElement()->renderer();
Julien - ping for review 2014/10/06 17:47:33 It seems like following shadow hosts could potenti
esprehn 2014/10/07 00:17:40 PseudoElements are never direct children of Shadow
dsinclair 2014/10/07 19:36:20 Acknowledged.
Julien - ping for review 2014/10/09 18:24:47 I don't deny that we don't use pseudo as direct ch
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 the sib ling.
Julien - ping for review 2014/10/06 17:47:32 we go *to* the sibling?
dsinclair 2014/10/07 19:36:20 Done.
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 if (firstLetterLength(toRenderText(firstLetterTextRenderer)->origina lText()))
125 break;
126 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
127 } else if (firstLetterTextRenderer->isListMarker()) {
Julien - ping for review 2014/10/06 17:47:33 These conditions just seems like magic to me. It w
Julien - ping for review 2014/10/06 17:47:33 These conditions just seems like magic to me. It w
dsinclair 2014/10/07 19:36:20 This is from the original code (see RenderBlock).
128 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
129
130 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) {
131 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
132 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d();
133 break;
134 }
135 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
136 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton()
137 || firstLetterTextRenderer->isMenuList()) {
138 return nullptr;
139 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER )
140 && firstLetterTextRenderer->canHaveGeneratedChildren()) {
141 // Let the child handle it when it's attached.
Julien - ping for review 2014/10/06 17:47:33 Not totally sure what this means: are you delegati
dsinclair 2014/10/07 19:36:20 Done. Better?
142 return nullptr;
143 } else {
144 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
145 }
146 }
Julien - ping for review 2014/10/06 17:47:33 It really looks like this could go into a helper f
dsinclair 2014/10/07 19:36:20 I'm not sure how much that would help. The code wo
147
148 // No first letter text to display, we're done.
149 // FIXME: This black-list of disallowed RenderText subclasses is fragile.
150 // Should counter be on this list? What about RenderTextFragment?
151 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText()
152 || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRender er)->isWordBreak())
153 return nullptr;
154
155 return firstLetterTextRenderer;
156 }
157
158 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent)
159 : PseudoElement(parent, FIRST_LETTER)
160 , m_remainingTextRenderer(nullptr)
161 , m_isStale(false)
162 {
163 }
164
165 void FirstLetterPseudoElement::attach(const AttachContext& context)
166 {
167 PseudoElement::attach(context);
168 attachFirstLetterTextRenderers();
169 }
170
171 void FirstLetterPseudoElement::dispose()
172 {
173 if (m_remainingTextRenderer) {
174 if (m_remainingTextRenderer->node()) {
175 Text* textNode = toText(m_remainingTextRenderer->node());
176 m_remainingTextRenderer->setText(textNode->dataImpl(), true);
177 }
178 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr);
179 }
180 m_remainingTextRenderer = nullptr;
181
182 PseudoElement::dispose();
183 }
184
185
186 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere rContainer)
187 {
188 if (!rendererContainer)
189 return nullptr;
190
191 RenderObject* styleContainer = parentOrShadowHostElement()->renderer();
Julien - ping for review 2014/10/06 17:47:33 Same comment about the use of parentOrShadowHostEl
dsinclair 2014/10/07 19:36:20 Acknowledged.
192 if (!styleContainer)
193 return nullptr;
194
195 // We always force the pseudo style to recompute as the first-letter style
196 // computed by the style container may not have taken the renderers styles
197 // into account.
198 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER);
199
200 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER , rendererContainer->firstLineStyle());
201 ASSERT(pseudoStyle);
202
203 // Force inline display (except for floating first-letters).
204 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
205
206 // CSS2 says first-letter can't be positioned.
207 pseudoStyle->setPosition(StaticPosition);
208
209 return pseudoStyle;
210 }
211
212 void FirstLetterPseudoElement::attachFirstLetterTextRenderers()
213 {
214 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender er(*this);
215 ASSERT(nextRenderer && nextRenderer->isText());
216
217 // The original string is going to be either a generated content string or a DOM node's
218 // string. We want the original string before it got transformed in case fir st-letter has
219 // no text-transform or a different text-transform applied to it.
220 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex t();
221 ASSERT(oldText.impl());
222
223 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent());
224 renderer()->setStyle(pseudoStyle);
225
226 // FIXME: This would already have been calculated in firstLetterRenderer. Ca n we pass the length through?
227 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText);
228
229 // Construct a text fragment for the text after the first letter.
230 // This text fragment might be empty.
231 RenderTextFragment* remainingText =
232 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne xtRenderer->document(), oldText.impl(), length, oldText.length() - length);
233 remainingText->setStyle(nextRenderer->style());
234 remainingText->setFirstLetterPseudoElement(this);
235 if (remainingText->node())
236 remainingText->node()->setRenderer(remainingText);
237 m_remainingTextRenderer = remainingText;
238
239 RenderObject* nextSibling = renderer()->nextSibling();
240 renderer()->parent()->addChild(remainingText, nextSibling);
241
242 // Construct text fragment for the first letter.
243 RenderTextFragment* letter = new RenderTextFragment(this, oldText.impl(), 0, length);
244 letter->setStyle(pseudoStyle);
245 renderer()->addChild(letter);
246
247 nextRenderer->destroy();
248 nextRenderer = remainingText;
249 }
250
251 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange)
252 {
253 if (!renderer())
254 return;
255
256 // The renderers inside pseudo elements are anonymous so they don't get noti fied of recalcStyle and must have
257 // the style propagated downward manually similar to RenderObject::propagate StyleToAnonymousChildren.
258 RenderObject* renderer = this->renderer();
259 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
260 // We need to re-calculate the correct style for the first letter elemen t
261 // and then apply that to the container and the text fragment inside.
262 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender er) {
263 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe nderer->parent()))
264 child->setPseudoStyle(pseudoStyle);
265 continue;
266 }
267
268 // We only manage the style for the generated content items.
269 if (!child->isText() && !child->isQuote() && !child->isImage())
270 continue;
271
272 child->setPseudoStyle(renderer->style());
273 }
274 }
275
276 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698