1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2007 David Smith (catfish.man@gmail.com)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "core/dom/FirstLetterPseudoElement.h"
#include "core/dom/Element.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/RenderObjectInlines.h"
#include "core/rendering/RenderText.h"
#include "core/rendering/RenderTextFragment.h"
#include "wtf/TemporaryChange.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/icu/UnicodeIcu.h"
namespace blink {
using namespace WTF;
using namespace Unicode;
// CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
// "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps), "close" (Pe),
// "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that precedes or follows the first letter should be included"
static inline bool isPunctuationForFirstLetter(UChar c)
{
CharCategory charCategory = category(c);
return charCategory == Punctuation_Open
|| charCategory == Punctuation_Close
|| charCategory == Punctuation_InitialQuote
|| charCategory == Punctuation_FinalQuote
|| charCategory == Punctuation_Other;
}
static inline bool isSpaceForFirstLetter(UChar c)
{
return isSpaceOrNewline(c) || c == noBreakSpace;
}
unsigned FirstLetterPseudoElement::firstLetterLength(const String& text)
{
unsigned length = 0;
unsigned textLength = text.length();
if (textLength == 0)
return length;
// Account for leading spaces first.
while (length < textLength && isSpaceForFirstLetter(text[length]))
length++;
// Now account for leading punctuation.
while (length < textLength && isPunctuationForFirstLetter(text[length]))
length++;
// Bail if we didn't find a letter before the end of the text or before a space.
if (isSpaceForFirstLetter(text[length]) || length == textLength)
return 0;
// Account the next character for first letter.
length++;
// Keep looking for allowed punctuation for the :first-letter.
for (; length < textLength; ++length) {
UChar c = text[length];
if (!isPunctuationForFirstLetter(c))
break;
}
return length;
}
// Once we see any of these renderers we can stop looking for first-letter as
// they signal the end of the first line of text.
static bool isInvalidFirstLetterRenderer(const RenderObject* obj)
{
return (obj->isBR() || (obj->isText() && toRenderText(obj)->isWordBreak()));
}
RenderObject* FirstLetterPseudoElement::firstLetterTextRenderer(const Element& element)
{
RenderObject* parentRenderer = 0;
// If we are looking at a first letter element then we need to find the
// first letter text renderer from the parent node, and not ourselves.
if (element.isFirstLetterPseudoElement())
parentRenderer = element.parentOrShadowHostElement()->renderer();
else
parentRenderer = element.renderer();
if (!parentRenderer
|| !parentRenderer->style()->hasPseudoStyle(FIRST_LETTER)
|| !parentRenderer->canHaveGeneratedChildren()
|| !(parentRenderer->isRenderBlockFlow() || parentRenderer->isRenderButton()))
return nullptr;
// Drill down into our children and look for our first text child.
RenderObject* firstLetterTextRenderer = parentRenderer->slowFirstChild();
while (firstLetterTextRenderer) {
// This can be called when the first letter renderer is already in the tree. We do not
// want to consider that renderer for our text renderer so we go to the sibling (which is
// the RenderTextFragment for the remaining text).
if (firstLetterTextRenderer->style() && firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
} else if (firstLetterTextRenderer->isText()) {
// FIXME: If there is leading punctuation in a different RenderText than
// the first letter, we'll not apply the correct style to it.
RefPtr<StringImpl> str = toRenderText(firstLetterTextRenderer)->isTextFragment() ?
toRenderTextFragment(firstLetterTextRenderer)->completeText() :
toRenderText(firstLetterTextRenderer)->originalText();
if (firstLetterLength(str.get()) || isInvalidFirstLetterRenderer(firstLetterTextRenderer))
break;
firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
} else if (firstLetterTextRenderer->isListMarker()) {
firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
} else if (firstLetterTextRenderer->isFloatingOrOutOfFlowPositioned()) {
if (firstLetterTextRenderer->style()->styleType() == FIRST_LETTER) {
firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
break;
}
firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
} else if (firstLetterTextRenderer->isReplaced() || firstLetterTextRenderer->isRenderButton()
|| firstLetterTextRenderer->isMenuList()) {
return nullptr;
} else if (firstLetterTextRenderer->isFlexibleBoxIncludingDeprecated() || firstLetterTextRenderer->isRenderGrid()) {
firstLetterTextRenderer = firstLetterTextRenderer->nextSibling();
} else if (firstLetterTextRenderer->style()->hasPseudoStyle(FIRST_LETTER)
&& firstLetterTextRenderer->canHaveGeneratedChildren()) {
// There is a renderer further down the tree which has FIRST_LETTER set. When that node
// is attached we will handle setting up the first letter then.
return nullptr;
} else {
firstLetterTextRenderer = firstLetterTextRenderer->slowFirstChild();
}
}
// No first letter text to display, we're done.
// FIXME: This black-list of disallowed RenderText subclasses is fragile. crbug.com/422336.
// Should counter be on this list? What about RenderTextFragment?
if (!firstLetterTextRenderer || !firstLetterTextRenderer->isText() || isInvalidFirstLetterRenderer(firstLetterTextRenderer))
return nullptr;
return firstLetterTextRenderer;
}
FirstLetterPseudoElement::FirstLetterPseudoElement(Element* parent)
: PseudoElement(parent, FIRST_LETTER)
, m_remainingTextRenderer(nullptr)
{
}
FirstLetterPseudoElement::~FirstLetterPseudoElement()
{
}
void FirstLetterPseudoElement::trace(Visitor* visitor)
{
visitor->trace(m_remainingTextRenderer);
PseudoElement::trace(visitor);
}
void FirstLetterPseudoElement::updateTextFragments()
{
String oldText = m_remainingTextRenderer->completeText();
ASSERT(oldText.impl());
unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText);
m_remainingTextRenderer->setTextFragment(oldText.impl()->substring(length, oldText.length()), length, oldText.length() - length);
m_remainingTextRenderer->dirtyLineBoxes();
for (auto child = renderer()->slowFirstChild(); child; child = child->nextSibling()) {
if (!child->isText() || !toRenderText(child)->isTextFragment())
continue;
RenderTextFragment* childFragment = toRenderTextFragment(child);
if (childFragment->firstLetterPseudoElement() != this)
continue;
childFragment->setTextFragment(oldText.impl()->substring(0, length), 0, length);
childFragment->dirtyLineBoxes();
break;
}
}
void FirstLetterPseudoElement::setRemainingTextRenderer(RenderTextFragment* fragment)
{
// The text fragment we get our content from is being destroyed. We need
// to tell our parent element to recalcStyle so we can get cleaned up
// as well.
if (!fragment)
setNeedsStyleRecalc(LocalStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::PseudoClass));
m_remainingTextRenderer = fragment;
}
void FirstLetterPseudoElement::attach(const AttachContext& context)
{
PseudoElement::attach(context);
attachFirstLetterTextRenderers();
}
void FirstLetterPseudoElement::detach(const AttachContext& context)
{
if (m_remainingTextRenderer) {
if (m_remainingTextRenderer->node() && document().isActive()) {
Text* textNode = toText(m_remainingTextRenderer->node());
m_remainingTextRenderer->setTextFragment(textNode->dataImpl(), 0, textNode->dataImpl()->length());
}
m_remainingTextRenderer->setFirstLetterPseudoElement(nullptr);
}
m_remainingTextRenderer = nullptr;
PseudoElement::detach(context);
}
RenderStyle* FirstLetterPseudoElement::styleForFirstLetter(RenderObject* rendererContainer)
{
ASSERT(rendererContainer);
RenderObject* styleContainer = parentOrShadowHostElement()->renderer();
ASSERT(styleContainer);
// We always force the pseudo style to recompute as the first-letter style
// computed by the style container may not have taken the renderers styles
// into account.
styleContainer->style()->removeCachedPseudoStyle(FIRST_LETTER);
RenderStyle* pseudoStyle = styleContainer->getCachedPseudoStyle(FIRST_LETTER, rendererContainer->firstLineStyle());
ASSERT(pseudoStyle);
return pseudoStyle;
}
void FirstLetterPseudoElement::attachFirstLetterTextRenderers()
{
RenderObject* nextRenderer = FirstLetterPseudoElement::firstLetterTextRenderer(*this);
ASSERT(nextRenderer);
ASSERT(nextRenderer->isText());
// The original string is going to be either a generated content string or a DOM node's
// string. We want the original string before it got transformed in case first-letter has
// no text-transform or a different text-transform applied to it.
String oldText = toRenderText(nextRenderer)->isTextFragment() ? toRenderTextFragment(nextRenderer)->completeText() : toRenderText(nextRenderer)->originalText();
ASSERT(oldText.impl());
RenderStyle* pseudoStyle = styleForFirstLetter(nextRenderer->parent());
renderer()->setStyle(pseudoStyle);
// FIXME: This would already have been calculated in firstLetterRenderer. Can we pass the length through?
unsigned length = FirstLetterPseudoElement::firstLetterLength(oldText);
// Construct a text fragment for the text after the first letter.
// This text fragment might be empty.
RenderTextFragment* remainingText =
new RenderTextFragment(nextRenderer->node() ? nextRenderer->node() : &nextRenderer->document(), oldText.impl(), length, oldText.length() - length);
remainingText->setFirstLetterPseudoElement(this);
remainingText->setIsRemainingTextRenderer();
remainingText->setStyle(nextRenderer->style());
if (remainingText->node())
remainingText->node()->setRenderer(remainingText);
m_remainingTextRenderer = remainingText;
RenderObject* nextSibling = renderer()->nextSibling();
renderer()->parent()->addChild(remainingText, nextSibling);
// Construct text fragment for the first letter.
RenderTextFragment* letter = new RenderTextFragment(&nextRenderer->document(), oldText.impl(), 0, length);
letter->setFirstLetterPseudoElement(this);
letter->setStyle(pseudoStyle);
renderer()->addChild(letter);
nextRenderer->destroy();
}
void FirstLetterPseudoElement::didRecalcStyle(StyleRecalcChange)
{
if (!renderer())
return;
// The renderers inside pseudo elements are anonymous so they don't get notified of recalcStyle and must have
// the style propagated downward manually similar to RenderObject::propagateStyleToAnonymousChildren.
RenderObject* renderer = this->renderer();
for (RenderObject* child = renderer->nextInPreOrder(renderer); child; child = child->nextInPreOrder(renderer)) {
// We need to re-calculate the correct style for the first letter element
// and then apply that to the container and the text fragment inside.
if (child->style()->styleType() == FIRST_LETTER && m_remainingTextRenderer) {
if (RenderStyle* pseudoStyle = styleForFirstLetter(m_remainingTextRenderer->parent()))
child->setPseudoStyle(pseudoStyle);
continue;
}
// We only manage the style for the generated content items.
if (!child->isText() && !child->isQuote() && !child->isImage())
continue;
child->setPseudoStyle(renderer->style());
}
}
} // namespace blink
|