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