OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
Julien - ping for review
2014/10/09 18:24:47
I don't think the new header applied to old conten
dsinclair
2014/10/09 21:14:22
Reverted to the old one to be safe.
| |
4 | |
5 #include "config.h" | |
6 #include "core/dom/FirstLetterPseudoElement.h" | |
7 | |
8 #include "core/dom/Element.h" | |
9 #include "core/rendering/RenderObject.h" | |
10 #include "core/rendering/RenderObjectInlines.h" | |
11 #include "core/rendering/RenderText.h" | |
12 #include "core/rendering/RenderTextFragment.h" | |
13 #include "wtf/text/WTFString.h" | |
14 #include "wtf/unicode/icu/UnicodeIcu.h" | |
15 | |
16 namespace blink { | |
17 | |
18 using namespace WTF; | |
19 using namespace Unicode; | |
20 | |
21 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter | |
22 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe), | |
23 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included" | |
24 static inline bool isPunctuationForFirstLetter(UChar c) | |
25 { | |
26 CharCategory charCategory = category(c); | |
27 return charCategory == Punctuation_Open | |
28 || charCategory == Punctuation_Close | |
29 || charCategory == Punctuation_InitialQuote | |
30 || charCategory == Punctuation_FinalQuote | |
31 || charCategory == Punctuation_Other; | |
32 } | |
33 | |
34 static inline bool isSpaceOrNewline(UChar c) | |
35 { | |
36 // Use isASCIISpace() for basic Latin-1. | |
37 // This will include newlines, which aren't included in Unicode DirWS. | |
38 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF: :Unicode::WhiteSpaceNeutral; | |
39 } | |
40 | |
41 static inline bool isSpaceForFirstLetter(UChar c) | |
42 { | |
43 return isSpaceOrNewline(c) || c == noBreakSpace; | |
44 } | |
45 | |
46 unsigned FirstLetterPseudoElement::firstLetterLength(const String& text) | |
47 { | |
48 unsigned length = 0; | |
49 unsigned textLength = text.length(); | |
50 | |
51 // Account for leading spaces first. | |
52 while (length < textLength && isSpaceForFirstLetter(text[length])) | |
53 length++; | |
54 | |
55 // Now account for leading punctuation. | |
56 while (length < textLength && isPunctuationForFirstLetter(text[length])) | |
57 length++; | |
58 | |
59 // Bail if we didn't find a letter before the end of the text or before a sp ace. | |
60 if (isSpaceForFirstLetter(text[length]) || (textLength && length == textLeng th)) | |
61 return 0; | |
62 | |
63 // Account the next character for first letter. | |
64 length++; | |
65 | |
66 // Keep looking for allowed punctuation for the :first-letter. | |
67 for (; length < textLength; ++length) { | |
68 UChar c = text[length]; | |
69 if (!isPunctuationForFirstLetter(c)) | |
70 break; | |
71 } | |
72 | |
73 // FIXME: If textLength is 0, length may still be 1! | |
74 return length; | |
75 } | |
76 | |
77 | |
78 RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& e lement) | |
79 { | |
80 RenderObject* parentRenderer = 0; | |
81 | |
82 // If we are looking at a first letter element then we need to find the | |
83 // first letter text renderer from the parent node, and not ourselves. | |
84 if (element.isFirstLetterPseudoElement()) | |
85 parentRenderer = element.parentOrShadowHostElement()->renderer(); | |
86 else | |
87 parentRenderer = element.renderer(); | |
88 | |
89 if (!parentRenderer | |
90 || !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER) | |
91 || !parentRenderer->canHaveGeneratedChildren() | |
92 || !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButt on())) | |
93 return nullptr; | |
94 | |
95 // Drill down into our children and look for our first text child. | |
96 RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild(); | |
97 while (firstLetterTextRenderer) { | |
98 // This can be called when the first letter renderer is already in the t ree. We do not | |
99 // want to consider that renderer for our text renderer so we go to the sibling. | |
100 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) { | |
101 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
102 } else if (firstLetterTextRenderer->isText()) { | |
103 // FIXME: If there is leading punctuation in a different RenderText than | |
104 // the first letter, we'll not apply the correct style to it. | |
105 if (firstLetterLength(toRenderText(firstLetterTextRenderer)->origina lText())) | |
106 break; | |
107 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
108 // FIXME: Need to try down why these conditions are needed. | |
109 } else if (firstLetterTextRenderer->isListMarker()) { | |
110 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
111 | |
112 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) { | |
113 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) { | |
114 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d(); | |
115 break; | |
116 } | |
117 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); | |
118 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton() | |
119 || firstLetterTextRenderer->isMenuList()) { | |
120 return nullptr; | |
121 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER ) | |
122 && firstLetterTextRenderer->canHaveGeneratedChildren()) { | |
123 // There is a renderer further down the tree which has FIRST_LETTER set. When that node | |
124 // is attached we will handle setting up the first letter then. | |
125 return nullptr; | |
126 } else { | |
127 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild(); | |
128 } | |
129 } | |
130 | |
131 // No first letter text to display, we're done. | |
132 // FIXME: This black-list of disallowed RenderText subclasses is fragile. | |
Julien - ping for review
2014/10/09 18:24:48
Could we do a white list instead?
dsinclair
2014/10/09 21:14:22
It _could_ be. But, it'd end up being like 50 entr
Julien - ping for review
2014/10/10 14:47:07
50 is too long indeed. Let's stick to a FIXME.
dsinclair
2014/10/10 18:06:52
Done. crbug.com/422336
| |
133 // Should counter be on this list? What about RenderTextFragment? | |
134 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() | |
135 || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRender er)->isWordBreak()) | |
136 return nullptr; | |
137 | |
138 return firstLetterTextRenderer; | |
139 } | |
140 | |
141 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent) | |
142 : PseudoElement(parent, FIRST_LETTER) | |
143 , m_remainingTextRenderer(nullptr) | |
144 , m_needsUpdate(false) | |
145 { | |
146 } | |
147 | |
148 void FirstLetterPseudoElement::attach(const AttachContext& context) | |
149 { | |
150 PseudoElement::attach(context); | |
151 attachFirstLetterTextRenderers(); | |
152 } | |
153 | |
154 void FirstLetterPseudoElement::detach(const AttachContext& context) | |
155 { | |
156 if (m_remainingTextRenderer) { | |
157 if (m_remainingTextRenderer->node()) { | |
158 Text* textNode = toText(m_remainingTextRenderer->node()); | |
159 m_remainingTextRenderer->setText(textNode->dataImpl(), true); | |
160 } | |
161 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr); | |
162 } | |
163 m_remainingTextRenderer = nullptr; | |
164 | |
165 PseudoElement::detach(context); | |
166 } | |
167 | |
168 | |
169 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere rContainer) | |
170 { | |
171 if (!rendererContainer) | |
172 return nullptr; | |
173 | |
174 RenderObject* styleContainer = parentOrShadowHostElement()->renderer(); | |
175 if (!styleContainer) | |
176 return nullptr; | |
177 | |
178 // We always force the pseudo style to recompute as the first-letter style | |
179 // computed by the style container may not have taken the renderers styles | |
180 // into account. | |
181 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER); | |
182 | |
183 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER , rendererContainer->firstLineStyle()); | |
184 ASSERT(pseudoStyle); | |
185 | |
186 // Force inline display (except for floating first-letters). | |
187 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE); | |
188 | |
189 // CSS2 says first-letter can't be positioned. | |
190 pseudoStyle->setPosition(StaticPosition); | |
191 | |
192 return pseudoStyle; | |
193 } | |
194 | |
195 void FirstLetterPseudoElement::attachFirstLetterTextRenderers() | |
196 { | |
197 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender er(*this); | |
198 ASSERT(nextRenderer && nextRenderer->isText()); | |
199 | |
200 // The original string is going to be either a generated content string or a DOM node's | |
201 // string. We want the original string before it got transformed in case fir st-letter has | |
202 // no text-transform or a different text-transform applied to it. | |
203 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex t(); | |
204 ASSERT(oldText.impl()); | |
205 | |
206 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent()); | |
207 renderer()->setStyle(pseudoStyle); | |
208 | |
209 // FIXME: This would already have been calculated in firstLetterRenderer. Ca n we pass the length through? | |
210 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText); | |
211 | |
212 // Construct a text fragment for the text after the first letter. | |
213 // This text fragment might be empty. | |
214 RenderTextFragment* remainingText = | |
215 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne xtRenderer->document(), oldText.impl(), length, oldText.length() - length); | |
216 remainingText->setStyle(nextRenderer->style()); | |
217 remainingText->setFirstLetterPseudoElement(this); | |
218 if (remainingText->node()) | |
219 remainingText->node()->setRenderer(remainingText); | |
220 m_remainingTextRenderer = remainingText; | |
221 | |
222 RenderObject* nextSibling = renderer()->nextSibling(); | |
223 renderer()->parent()->addChild(remainingText, nextSibling); | |
224 | |
225 // Construct text fragment for the first letter. | |
226 RenderTextFragment* letter = new RenderTextFragment(this, oldText.impl(), 0, length); | |
227 letter->setStyle(pseudoStyle); | |
228 renderer()->addChild(letter); | |
229 | |
230 nextRenderer->destroy(); | |
231 nextRenderer = remainingText; | |
232 } | |
233 | |
234 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange) | |
235 { | |
236 if (!renderer()) | |
237 return; | |
238 | |
239 // The renderers inside pseudo elements are anonymous so they don't get noti fied of recalcStyle and must have | |
240 // the style propagated downward manually similar to RenderObject::propagate StyleToAnonymousChildren. | |
241 RenderObject* renderer = this->renderer(); | |
242 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) { | |
243 // We need to re-calculate the correct style for the first letter elemen t | |
244 // and then apply that to the container and the text fragment inside. | |
245 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender er) { | |
246 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe nderer->parent())) | |
247 child->setPseudoStyle(pseudoStyle); | |
248 continue; | |
249 } | |
250 | |
251 // We only manage the style for the generated content items. | |
252 if (!child->isText() && !child->isQuote() && !child->isImage()) | |
253 continue; | |
254 | |
255 child->setPseudoStyle(renderer->style()); | |
256 } | |
257 } | |
258 | |
259 } // namespace blink | |
OLD | NEW |