OLD | NEW |
---|---|
(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 = 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. | |
119 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) { | |
Julien - ping for review
2014/10/10 22:47:23
If firstLetterTextRenderer->style() is NULL, sever
dsinclair
2014/10/14 14:31:27
I'd prefer to do this in a followup so I can go th
| |
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 // FIXME: Need to try down why these conditions are needed. | |
Julien - ping for review
2014/10/10 22:47:23
Do we really want to land that?
dsinclair
2014/10/14 14:31:27
Fixed the spelling, I think we want to keep it so
| |
128 } else if (firstLetterTextRenderer->isListMarker()) { | |
129 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
130 | |
Julien - ping for review
2014/10/10 22:47:23
Unneeded empty space.
dsinclair
2014/10/14 14:31:27
Done.
| |
131 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) { | |
132 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) { | |
133 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d(); | |
134 break; | |
135 } | |
136 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
137 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton() | |
138 || firstLetterTextRenderer->isMenuList()) { | |
139 return nullptr; | |
140 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER ) | |
141 && firstLetterTextRenderer->canHaveGeneratedChildren()) { | |
142 // There is a renderer further down the tree which has FIRST_LETTER set. When that node | |
143 // is attached we will handle setting up the first letter then. | |
144 return nullptr; | |
145 } else { | |
146 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild(); | |
147 } | |
148 } | |
149 | |
150 // No first letter text to display, we're done. | |
151 // FIXME: This black-list of disallowed RenderText subclasses is fragile. cr bug.com/422336. | |
152 // Should counter be on this list? What about RenderTextFragment? | |
153 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() | |
154 || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRender er)->isWordBreak()) | |
155 return nullptr; | |
156 | |
157 return firstLetterTextRenderer; | |
158 } | |
159 | |
160 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent) | |
161 : PseudoElement(parent, FIRST_LETTER) | |
162 , m_remainingTextRenderer(nullptr) | |
163 , m_needsUpdate(false) | |
164 { | |
165 } | |
166 | |
167 void FirstLetterPseudoElement::attach(const AttachContext& context) | |
168 { | |
169 PseudoElement::attach(context); | |
170 attachFirstLetterTextRenderers(); | |
171 } | |
172 | |
173 void FirstLetterPseudoElement::detach(const AttachContext& context) | |
174 { | |
175 if (m_remainingTextRenderer) { | |
176 if (m_remainingTextRenderer->node()) { | |
177 Text* textNode = toText(m_remainingTextRenderer->node()); | |
178 m_remainingTextRenderer->setText(textNode->dataImpl(), true); | |
179 } | |
180 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr); | |
181 } | |
182 m_remainingTextRenderer = nullptr; | |
183 | |
184 PseudoElement::detach(context); | |
185 } | |
186 | |
187 | |
188 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere rContainer) | |
Julien - ping for review
2014/10/10 22:47:23
I don't think this function could return nullptr o
dsinclair
2014/10/14 14:31:27
Done.
| |
189 { | |
190 if (!rendererContainer) | |
191 return nullptr; | |
192 | |
193 RenderObject* styleContainer = parentOrShadowHostElement()->renderer(); | |
194 if (!styleContainer) | |
195 return nullptr; | |
196 | |
197 // We always force the pseudo style to recompute as the first-letter style | |
198 // computed by the style container may not have taken the renderers styles | |
199 // into account. | |
200 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER); | |
201 | |
202 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER , rendererContainer->firstLineStyle()); | |
203 ASSERT(pseudoStyle); | |
204 | |
205 // Force inline display (except for floating first-letters). | |
206 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE); | |
207 | |
208 // CSS2 says first-letter can't be positioned. | |
209 pseudoStyle->setPosition(StaticPosition); | |
210 | |
211 return pseudoStyle; | |
212 } | |
213 | |
214 void FirstLetterPseudoElement::attachFirstLetterTextRenderers() | |
215 { | |
216 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender er(*this); | |
217 ASSERT(nextRenderer); | |
218 ASSERT(nextRenderer->isText()); | |
219 | |
220 // The original string is going to be either a generated content string or a DOM node's | |
221 // string. We want the original string before it got transformed in case fir st-letter has | |
222 // no text-transform or a different text-transform applied to it. | |
223 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex t(); | |
224 ASSERT(oldText.impl()); | |
225 | |
226 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent()); | |
227 renderer()->setStyle(pseudoStyle); | |
228 | |
229 // FIXME: This would already have been calculated in firstLetterRenderer. Ca n we pass the length through? | |
230 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText); | |
231 | |
232 // Construct a text fragment for the text after the first letter. | |
233 // This text fragment might be empty. | |
234 RenderTextFragment* remainingText = | |
235 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne xtRenderer->document(), oldText.impl(), length, oldText.length() - length); | |
236 remainingText->setStyle(nextRenderer->style()); | |
237 remainingText->setFirstLetterPseudoElement(this); | |
238 if (remainingText->node()) | |
239 remainingText->node()->setRenderer(remainingText); | |
240 m_remainingTextRenderer = remainingText; | |
241 | |
242 RenderObject* nextSibling = renderer()->nextSibling(); | |
243 renderer()->parent()->addChild(remainingText, nextSibling); | |
244 | |
245 // Construct text fragment for the first letter. | |
246 RenderTextFragment* letter = new RenderTextFragment(this, oldText.impl(), 0, length); | |
247 letter->setStyle(pseudoStyle); | |
248 renderer()->addChild(letter); | |
249 | |
250 nextRenderer->destroy(); | |
251 nextRenderer = remainingText; | |
Julien - ping for review
2014/10/10 22:47:23
That doesn't give us anything (but nice paranoia).
dsinclair
2014/10/14 14:31:27
Done.
Left over from nextRenderer being m_nextRen
| |
252 } | |
253 | |
254 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange) | |
255 { | |
256 if (!renderer()) | |
257 return; | |
258 | |
259 // The renderers inside pseudo elements are anonymous so they don't get noti fied of recalcStyle and must have | |
260 // the style propagated downward manually similar to RenderObject::propagate StyleToAnonymousChildren. | |
261 RenderObject* renderer = this->renderer(); | |
262 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) { | |
263 // We need to re-calculate the correct style for the first letter elemen t | |
264 // and then apply that to the container and the text fragment inside. | |
265 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender er) { | |
266 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe nderer->parent())) | |
267 child->setPseudoStyle(pseudoStyle); | |
268 continue; | |
269 } | |
270 | |
271 // We only manage the style for the generated content items. | |
272 if (!child->isText() && !child->isQuote() && !child->isImage()) | |
esprehn
2014/10/11 04:46:19
This is code duplication? We should probably fix t
dsinclair
2014/10/14 14:31:27
Acknowledged.
| |
273 continue; | |
274 | |
275 child->setPseudoStyle(renderer->style()); | |
276 } | |
277 } | |
278 | |
279 } // namespace blink | |
OLD | NEW |