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) { |
| 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 track down why these conditions are needed. |
| 128 } else if (firstLetterTextRenderer->isListMarker()) { |
| 129 firstLetterTextRenderer = firstLetterTextRenderer->nextSibling(); |
| 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 // There is a renderer further down the tree which has FIRST_LETTER
set. When that node |
| 142 // is attached we will handle setting up the first letter then. |
| 143 return nullptr; |
| 144 } else { |
| 145 firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild(); |
| 146 } |
| 147 } |
| 148 |
| 149 // No first letter text to display, we're done. |
| 150 // FIXME: This black-list of disallowed RenderText subclasses is fragile. cr
bug.com/422336. |
| 151 // Should counter be on this list? What about RenderTextFragment? |
| 152 if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() |
| 153 || firstLetterTextRenderer->isBR() || toRenderText(firstLetterTextRender
er)->isWordBreak()) |
| 154 return nullptr; |
| 155 |
| 156 return firstLetterTextRenderer; |
| 157 } |
| 158 |
| 159 PassRefPtrWillBeRawPtr<FirstLetterPseudoElement> FirstLetterPseudoElement::creat
e(Element* parent) |
| 160 { |
| 161 return adoptRefWillBeNoop(new FirstLetterPseudoElement(parent)); |
| 162 } |
| 163 |
| 164 FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent) |
| 165 : PseudoElement(parent, FIRST_LETTER) |
| 166 , m_remainingTextRenderer(nullptr) |
| 167 , m_needsUpdate(false) |
| 168 { |
| 169 } |
| 170 |
| 171 void FirstLetterPseudoElement::setNeedsUpdate() |
| 172 { |
| 173 m_needsUpdate = true; |
| 174 setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::create(St
yleChangeReason::PseudoClass)); |
| 175 } |
| 176 |
| 177 void FirstLetterPseudoElement::attach(const AttachContext& context) |
| 178 { |
| 179 PseudoElement::attach(context); |
| 180 attachFirstLetterTextRenderers(); |
| 181 } |
| 182 |
| 183 void FirstLetterPseudoElement::detach(const AttachContext& context) |
| 184 { |
| 185 if (m_remainingTextRenderer) { |
| 186 if (m_remainingTextRenderer->node()) { |
| 187 Text* textNode = toText(m_remainingTextRenderer->node()); |
| 188 m_remainingTextRenderer->setText(textNode->dataImpl(), true); |
| 189 } |
| 190 m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr); |
| 191 } |
| 192 m_remainingTextRenderer = nullptr; |
| 193 |
| 194 PseudoElement::detach(context); |
| 195 } |
| 196 |
| 197 |
| 198 RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendere
rContainer) |
| 199 { |
| 200 ASSERT(rendererContainer); |
| 201 |
| 202 RenderObject* styleContainer = parentOrShadowHostElement()->renderer(); |
| 203 ASSERT(styleContainer); |
| 204 |
| 205 // We always force the pseudo style to recompute as the first-letter style |
| 206 // computed by the style container may not have taken the renderers styles |
| 207 // into account. |
| 208 styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER); |
| 209 |
| 210 RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER
, rendererContainer->firstLineStyle()); |
| 211 ASSERT(pseudoStyle); |
| 212 |
| 213 // Force inline display (except for floating first-letters). |
| 214 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE); |
| 215 |
| 216 // CSS2 says first-letter can't be positioned. |
| 217 pseudoStyle->setPosition(StaticPosition); |
| 218 |
| 219 return pseudoStyle; |
| 220 } |
| 221 |
| 222 void FirstLetterPseudoElement::attachFirstLetterTextRenderers() |
| 223 { |
| 224 RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRender
er(*this); |
| 225 ASSERT(nextRenderer); |
| 226 ASSERT(nextRenderer->isText()); |
| 227 |
| 228 // The original string is going to be either a generated content string or a
DOM node's |
| 229 // string. We want the original string before it got transformed in case fir
st-letter has |
| 230 // no text-transform or a different text-transform applied to it. |
| 231 String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderText
Fragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalTex
t(); |
| 232 ASSERT(oldText.impl()); |
| 233 |
| 234 RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent()); |
| 235 renderer()->setStyle(pseudoStyle); |
| 236 |
| 237 // FIXME: This would already have been calculated in firstLetterRenderer. Ca
n we pass the length through? |
| 238 unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText); |
| 239 |
| 240 // Construct a text fragment for the text after the first letter. |
| 241 // This text fragment might be empty. |
| 242 RenderTextFragment* remainingText = |
| 243 new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &ne
xtRenderer->document(), oldText.impl(), length, oldText.length() - length); |
| 244 remainingText->setFirstLetterPseudoElement(this); |
| 245 remainingText->setIsRemainingTextRenderer(); |
| 246 remainingText->setStyle(nextRenderer->style()); |
| 247 |
| 248 if (remainingText->node()) |
| 249 remainingText->node()->setRenderer(remainingText); |
| 250 |
| 251 m_remainingTextRenderer = remainingText; |
| 252 |
| 253 RenderObject* nextSibling = renderer()->nextSibling(); |
| 254 renderer()->parent()->addChild(remainingText, nextSibling); |
| 255 |
| 256 // Construct text fragment for the first letter. |
| 257 RenderTextFragment* letter = new RenderTextFragment(&nextRenderer->document(
), oldText.impl(), 0, length); |
| 258 letter->setFirstLetterPseudoElement(this); |
| 259 letter->setStyle(pseudoStyle); |
| 260 renderer()->addChild(letter); |
| 261 |
| 262 nextRenderer->destroy(); |
| 263 } |
| 264 |
| 265 void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange) |
| 266 { |
| 267 if (!renderer()) |
| 268 return; |
| 269 |
| 270 // The renderers inside pseudo elements are anonymous so they don't get noti
fied of recalcStyle and must have |
| 271 // the style propagated downward manually similar to RenderObject::propagate
StyleToAnonymousChildren. |
| 272 RenderObject* renderer = this->renderer(); |
| 273 for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child
= child->nextInPreOrder(renderer)) { |
| 274 // We need to re-calculate the correct style for the first letter elemen
t |
| 275 // and then apply that to the container and the text fragment inside. |
| 276 if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRender
er) { |
| 277 if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRe
nderer->parent())) |
| 278 child->setPseudoStyle(pseudoStyle); |
| 279 continue; |
| 280 } |
| 281 |
| 282 // We only manage the style for the generated content items. |
| 283 if (!child->isText() && !child->isQuote() && !child->isImage()) |
| 284 continue; |
| 285 |
| 286 child->setPseudoStyle(renderer->style()); |
| 287 } |
| 288 } |
| 289 |
| 290 } // namespace blink |
OLD | NEW |