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

Side by Side Diff: Source/core/dom/FirstLetterHelper.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/FirstLetterHelper.h"
26
27 #include "core/dom/Element.h"
28 #include "core/rendering/RenderObject.h"
29 #include "core/rendering/RenderText.h"
30 #include "wtf/text/WTFString.h"
31 #include "wtf/unicode/icu/UnicodeIcu.h"
32
33 namespace blink {
34
35 using namespace WTF;
36 using namespace Unicode;
37
38 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
39 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
40 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
41 static inline bool isPunctuationForFirstLetter(UChar c)
42 {
43 CharCategory charCategory = category(c);
44 return charCategory == Punctuation_Open
45 || charCategory == Punctuation_Close
46 || charCategory == Punctuation_InitialQuote
47 || charCategory == Punctuation_FinalQuote
48 || charCategory == Punctuation_Other;
49 }
50
51 static inline bool isSpaceOrNewline(UChar c)
52 {
53 // Use isASCIISpace() for basic Latin-1.
54 // This will include newlines, which aren't included in Unicode DirWS.
55 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF: :Unicode::WhiteSpaceNeutral;
56 }
57
58 static inline bool isSpaceForFirstLetter(UChar c)
59 {
60 return isSpaceOrNewline(c) || c == noBreakSpace;
61 }
62
63 unsigned FirstLetterHelper::firstLetterLength(const String& text)
64 {
65 unsigned length = 0;
66 unsigned textLength = text.length();
67
68 // Account for leading spaces first.
69 while (length < textLength && isSpaceForFirstLetter(text[length]))
70 length++;
71
72 // Now account for leading punctuation.
73 while (length < textLength && isPunctuationForFirstLetter(text[length]))
74 length++;
75
76 // Bail if we didn't find a letter before the end of the text or before a sp ace.
77 if (isSpaceForFirstLetter(text[length]) || (textLength && length == textLeng th))
78 return 0;
79
80 // Account the next character for first letter.
81 length++;
82
83 // Keep looking allowed punctuation for the :first-letter.
Julien - ping for review 2014/10/01 21:14:47 Keep looking *for* allowed punctuation?
dsinclair 2014/10/04 02:01:34 Done.
84 for (unsigned scanLength = length; scanLength < textLength; ++scanLength) {
85 UChar c = text[scanLength];
86
87 if (!isPunctuationForFirstLetter(c))
88 break;
89
90 length = scanLength + 1;
Julien - ping for review 2014/10/01 21:14:47 It seems wasteful to do this update in the loop in
dsinclair 2014/10/04 02:01:34 Fixed the code. We don't need scanLength at all as
91 }
92
93 // FIXME: If textLength is 0, length may still be 1!
94 return length;
95 }
96
97
98 RenderObject* FirstLetterHelper::firstLetterTextRenderer(const Element& element)
99 {
100 RenderObject* parentRenderer;
101
102 // If we are looking at a first letter element then we need to find the
103 // first letter text renderer from the parent node, and not ourselves.
104 if (element.isFirstLetterPseudoElement())
105 parentRenderer = element.parentOrShadowHostElement()->renderer();
106 else
107 parentRenderer = element.renderer();
108
109 if (!parentRenderer
110 || !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER)
111 || !parentRenderer->canHaveGeneratedChildren()
112 || !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButt on()))
113 return nullptr;
114
115 // Drill down into our children and look for our first text child.
116 RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild();
117 while (firstLetterTextRenderer) {
118
119 // This can be called when the first letter renderer is already in the t ree. We do not
120 // want to consider that renderer for our text renderer so we go the sib ling.
121 if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style() ->styleType() == FIRST_LETTER) {
122 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
123
Julien - ping for review 2014/10/01 21:14:47 While helpful, those extra spaces are not consiste
dsinclair 2014/10/04 02:01:34 Done.
124 } else if (firstLetterTextRenderer->isText()) {
125 // FIXME: If there is leading punctuation in a different RenderText than
126 // the first letter, we'll not apply the correct style to it.
Julien - ping for review 2014/10/01 21:14:47 Could you give an example of when this would happe
Julien - ping for review 2014/10/01 21:14:47 Could you give an example of when this would happe
dsinclair 2014/10/04 02:01:34 I don't know. This is from the original code. I'll
127 if (firstLetterLength(toRenderText(firstLetterTextRenderer)->origina lText()))
128 break;
129 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
130
131 } else if (firstLetterTextRenderer->isListMarker()) {
132 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
133
134 } else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) {
135 if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
136 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChil d();
137 break;
138 }
139 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
140
141 } else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRende rer->isRenderButton()
142 || firstLetterTextRenderer->isMenuList()) {
143 return nullptr;
144
145 } else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER )
146 && firstLetterTextRenderer->canHaveGeneratedChildren()) {
147 // Let the child handle it when it's attached.
148 return nullptr;
149
150 } else {
151 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
152 }
153 }
154
155 // No first letter text to display, we're done.
156 // FIXME: This black-list of disallowed RenderText subclasses is fragile.
157 // Should counter be on this list? What about RenderTextFragment?
158 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText()
159 || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRender er)->isWordBreak())
160 return nullptr;
161
162 return firstLetterTextRenderer;
163 }
164
165 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698