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

Side by Side Diff: Source/core/rendering/RenderBlock.cpp

Issue 672953002: Convert first letter into a pseudo element. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « Source/core/rendering/RenderBlock.h ('k') | Source/core/rendering/RenderBoxModelObject.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2007 David Smith (catfish.man@gmail.com) 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. 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. 6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 1363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 gDelayedUpdateScrollInfoSet->add(this); 1374 gDelayedUpdateScrollInfoSet->add(this);
1375 else 1375 else
1376 layer()->scrollableArea()->updateAfterLayout(); 1376 layer()->scrollableArea()->updateAfterLayout();
1377 } 1377 }
1378 } 1378 }
1379 1379
1380 void RenderBlock::layout() 1380 void RenderBlock::layout()
1381 { 1381 {
1382 OverflowEventDispatcher dispatcher(this); 1382 OverflowEventDispatcher dispatcher(this);
1383 1383
1384 // Update our first letter info now.
1385 updateFirstLetter();
1386
1387 // Table cells call layoutBlock directly, so don't add any logic here. Put code into 1384 // Table cells call layoutBlock directly, so don't add any logic here. Put code into
1388 // layoutBlock(). 1385 // layoutBlock().
1389 layoutBlock(false); 1386 layoutBlock(false);
1390 1387
1391 // It's safe to check for control clip here, since controls can never be tab le cells. 1388 // It's safe to check for control clip here, since controls can never be tab le cells.
1392 // If we have a lightweight clip, there can never be any overflow from child ren. 1389 // If we have a lightweight clip, there can never be any overflow from child ren.
1393 if (hasControlClip() && m_overflow) 1390 if (hasControlClip() && m_overflow)
1394 clearLayoutOverflow(); 1391 clearLayoutOverflow();
1395 1392
1396 invalidateBackgroundObscurationStatus(); 1393 invalidateBackgroundObscurationStatus();
(...skipping 1579 matching lines...) Expand 10 before | Expand all | Expand 10 after
2976 2973
2977 int scrollbarWidth = instrinsicScrollbarLogicalWidth(); 2974 int scrollbarWidth = instrinsicScrollbarLogicalWidth();
2978 maxLogicalWidth += scrollbarWidth; 2975 maxLogicalWidth += scrollbarWidth;
2979 minLogicalWidth += scrollbarWidth; 2976 minLogicalWidth += scrollbarWidth;
2980 } 2977 }
2981 2978
2982 void RenderBlock::computePreferredLogicalWidths() 2979 void RenderBlock::computePreferredLogicalWidths()
2983 { 2980 {
2984 ASSERT(preferredLogicalWidthsDirty()); 2981 ASSERT(preferredLogicalWidthsDirty());
2985 2982
2986 updateFirstLetter();
2987
2988 m_minPreferredLogicalWidth = 0; 2983 m_minPreferredLogicalWidth = 0;
2989 m_maxPreferredLogicalWidth = 0; 2984 m_maxPreferredLogicalWidth = 0;
2990 2985
2991 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you 2986 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you
2992 // should be able to use percentage, calc or viewport relative values for wi dth. 2987 // should be able to use percentage, calc or viewport relative values for wi dth.
2993 RenderStyle* styleToUse = style(); 2988 RenderStyle* styleToUse = style();
2994 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0 2989 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0
2995 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue())) 2990 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue()))
2996 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value()); 2991 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
2997 else 2992 else
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
3335 break; 3330 break;
3336 firstLineBlock = toRenderBlock(parentBlock); 3331 firstLineBlock = toRenderBlock(parentBlock);
3337 } 3332 }
3338 3333
3339 if (!hasPseudo) 3334 if (!hasPseudo)
3340 return 0; 3335 return 0;
3341 3336
3342 return firstLineBlock; 3337 return firstLineBlock;
3343 } 3338 }
3344 3339
3345 static RenderStyle* styleForFirstLetter(RenderObject* firstLetterBlock, RenderOb ject* firstLetterContainer)
3346 {
3347 RenderStyle* pseudoStyle = firstLetterBlock->getCachedPseudoStyle(FIRST_LETT ER, firstLetterContainer->firstLineStyle());
3348 // Force inline display (except for floating first-letters).
3349 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
3350 // CSS2 says first-letter can't be positioned.
3351 pseudoStyle->setPosition(StaticPosition);
3352 return pseudoStyle;
3353 }
3354
3355 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
3356 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
3357 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
3358 static inline bool isPunctuationForFirstLetter(UChar c)
3359 {
3360 CharCategory charCategory = category(c);
3361 return charCategory == Punctuation_Open
3362 || charCategory == Punctuation_Close
3363 || charCategory == Punctuation_InitialQuote
3364 || charCategory == Punctuation_FinalQuote
3365 || charCategory == Punctuation_Other;
3366 }
3367
3368 static inline bool isSpaceForFirstLetter(UChar c)
3369 {
3370 return isSpaceOrNewline(c) || c == noBreakSpace;
3371 }
3372
3373 static inline RenderObject* findFirstLetterBlock(RenderBlock* start)
3374 {
3375 RenderObject* firstLetterBlock = start;
3376 while (true) {
3377 bool canHaveFirstLetterRenderer = firstLetterBlock->style()->hasPseudoSt yle(FIRST_LETTER)
3378 && firstLetterBlock->canHaveGeneratedChildren()
3379 && isRenderBlockFlowOrRenderButton(firstLetterBlock);
3380 if (canHaveFirstLetterRenderer)
3381 return firstLetterBlock;
3382
3383 RenderObject* parentBlock = firstLetterBlock->parent();
3384 if (firstLetterBlock->isReplaced() || !parentBlock
3385 || !isRenderBlockFlowOrRenderButton(parentBlock)) {
3386 return 0;
3387 }
3388 ASSERT(parentBlock->isRenderBlock());
3389 if (toRenderBlock(parentBlock)->firstChild() != firstLetterBlock)
3390 return 0;
3391 firstLetterBlock = parentBlock;
3392 }
3393
3394 return 0;
3395 }
3396
3397 void RenderBlock::updateFirstLetterStyle(RenderObject* firstLetterBlock, RenderO bject* currentChild)
3398 {
3399 RenderObject* firstLetter = currentChild->parent();
3400 RenderObject* firstLetterContainer = firstLetter->parent();
3401 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3402 ASSERT(firstLetter->isFloating() || firstLetter->isInline());
3403
3404 if (RenderStyle::stylePropagationDiff(firstLetter->style(), pseudoStyle) == Reattach) {
3405 // The first-letter renderer needs to be replaced. Create a new renderer of the right type.
3406 RenderBoxModelObject* newFirstLetter;
3407 if (pseudoStyle->display() == INLINE)
3408 newFirstLetter = RenderInline::createAnonymous(&document());
3409 else
3410 newFirstLetter = RenderBlockFlow::createAnonymous(&document());
3411 newFirstLetter->setStyle(pseudoStyle);
3412
3413 // Move the first letter into the new renderer.
3414 while (RenderObject* child = firstLetter->slowFirstChild()) {
3415 if (child->isText())
3416 toRenderText(child)->removeAndDestroyTextBoxes();
3417 firstLetter->removeChild(child);
3418 newFirstLetter->addChild(child, 0);
3419 }
3420
3421 RenderObject* nextSibling = firstLetter->nextSibling();
3422 if (RenderTextFragment* remainingText = toRenderBoxModelObject(firstLett er)->firstLetterRemainingText()) {
3423 ASSERT(remainingText->isAnonymous() || remainingText->node()->render er() == remainingText);
3424 // Replace the old renderer with the new one.
3425 remainingText->setFirstLetter(newFirstLetter);
3426 newFirstLetter->setFirstLetterRemainingText(remainingText);
3427 }
3428 // To prevent removal of single anonymous block in RenderBlock::removeCh ild and causing
3429 // |nextSibling| to go stale, we remove the old first letter using remov eChildNode first.
3430 firstLetterContainer->virtualChildren()->removeChildNode(firstLetterCont ainer, firstLetter);
3431 firstLetter->destroy();
3432 firstLetter = newFirstLetter;
3433 firstLetterContainer->addChild(firstLetter, nextSibling);
3434 } else {
3435 firstLetter->setStyle(pseudoStyle);
3436 }
3437
3438 for (RenderObject* genChild = firstLetter->slowFirstChild(); genChild; genCh ild = genChild->nextSibling()) {
3439 if (genChild->isText())
3440 genChild->setStyle(pseudoStyle);
3441 }
3442 }
3443
3444 static inline unsigned firstLetterLength(const String& text)
3445 {
3446 unsigned length = 0;
3447 unsigned textLength = text.length();
3448
3449 if (textLength == 0)
3450 return length;
3451
3452 // Account for leading spaces first.
3453 while (length < textLength && isSpaceForFirstLetter(text[length]))
3454 length++;
3455
3456 // Now account for leading punctuation.
3457 while (length < textLength && isPunctuationForFirstLetter(text[length]))
3458 length++;
3459
3460 // Bail if we didn't find a letter before the end of the text or before a sp ace.
3461 if (isSpaceForFirstLetter(text[length]) || length == textLength)
3462 return 0;
3463
3464 // Account the next character for first letter.
3465 length++;
3466
3467 // Keep looking allowed punctuation for the :first-letter.
3468 for (unsigned scanLength = length; scanLength < textLength; ++scanLength) {
3469 UChar c = text[scanLength];
3470
3471 if (!isPunctuationForFirstLetter(c))
3472 break;
3473
3474 length = scanLength + 1;
3475 }
3476 return length;
3477 }
3478
3479 void RenderBlock::createFirstLetterRenderer(RenderObject* firstLetterBlock, Rend erText& currentChild, unsigned length)
3480 {
3481 ASSERT(length);
3482
3483 RenderObject* firstLetterContainer = currentChild.parent();
3484 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3485 RenderBoxModelObject* firstLetter = 0;
3486 if (pseudoStyle->display() == INLINE)
3487 firstLetter = RenderInline::createAnonymous(&document());
3488 else
3489 firstLetter = RenderBlockFlow::createAnonymous(&document());
3490 firstLetter->setStyle(pseudoStyle);
3491
3492 // FIXME: The first letter code should not modify the render tree during
3493 // layout. crbug.com/370458
3494 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
3495
3496 firstLetterContainer->addChild(firstLetter, &currentChild);
3497
3498 // The original string is going to be either a generated content string or a DOM node's
3499 // string. We want the original string before it got transformed in case fi rst-letter has
3500 // no text-transform or a different text-transform applied to it.
3501 String oldText = currentChild.originalText();
3502 ASSERT(oldText.impl());
3503
3504 // Construct a text fragment for the text after the first letter.
3505 // This text fragment might be empty.
3506 RenderTextFragment* remainingText =
3507 new RenderTextFragment(currentChild.node() ? currentChild.node() : &curr entChild.document(), oldText.impl(), length, oldText.length() - length);
3508 remainingText->setStyle(currentChild.style());
3509 if (remainingText->node())
3510 remainingText->node()->setRenderer(remainingText);
3511
3512 firstLetterContainer->addChild(remainingText, &currentChild);
3513 firstLetterContainer->removeChild(&currentChild);
3514 remainingText->setFirstLetter(firstLetter);
3515 firstLetter->setFirstLetterRemainingText(remainingText);
3516
3517 // construct text fragment for the first letter
3518 RenderTextFragment* letter =
3519 new RenderTextFragment(remainingText->node() ? remainingText->node() : & remainingText->document(), oldText.impl(), 0, length);
3520 letter->setStyle(pseudoStyle);
3521 firstLetter->addChild(letter);
3522
3523 currentChild.destroy();
3524 }
3525
3526 // Once we see any of these renderers we can stop looking for first-letter as
3527 // they signal the end of the first line of text.
3528 bool RenderBlock::isInvalidFirstLetterRenderer(RenderObject* obj) const
3529 {
3530 return (obj->isBR() || (obj->isText() && toRenderText(obj)->isWordBreak()));
3531 }
3532
3533 void RenderBlock::updateFirstLetter()
3534 {
3535 if (!document().styleEngine()->usesFirstLetterRules())
3536 return;
3537 // Don't recur
3538 if (style()->styleType() == FIRST_LETTER)
3539 return;
3540
3541 // FIXME: We need to destroy the first-letter object if it is no longer the first child. Need to find
3542 // an efficient way to check for that situation though before implementing a nything.
3543 RenderObject* firstLetterBlock = findFirstLetterBlock(this);
3544 if (!firstLetterBlock)
3545 return;
3546
3547 // Drill into inlines looking for our first text child.
3548 RenderObject* currChild = firstLetterBlock->slowFirstChild();
3549 unsigned length = 0;
3550 while (currChild) {
3551 if (currChild->isText()) {
3552 // FIXME: If there is leading punctuation in a different RenderText than
3553 // the first letter, we'll not apply the correct style to it.
3554 length = firstLetterLength(toRenderText(currChild)->originalText());
3555 if (length || isInvalidFirstLetterRenderer(currChild))
3556 break;
3557 currChild = currChild->nextSibling();
3558 } else if (currChild->isListMarker()) {
3559 currChild = currChild->nextSibling();
3560 } else if (currChild->isFloatingOrOutOfFlowPositioned()) {
3561 if (currChild->style()->styleType() == FIRST_LETTER) {
3562 currChild = currChild->slowFirstChild();
3563 break;
3564 }
3565 currChild = currChild->nextSibling();
3566 } else if (currChild->isReplaced() || currChild->isRenderButton() || cur rChild->isMenuList()) {
3567 break;
3568 } else if (currChild->isFlexibleBoxIncludingDeprecated() || currChild->i sRenderGrid()) {
3569 return;
3570 } else if (currChild->style()->hasPseudoStyle(FIRST_LETTER) && currChild ->canHaveGeneratedChildren()) {
3571 // We found a lower-level node with first-letter, which supersedes t he higher-level style
3572 firstLetterBlock = currChild;
3573 currChild = currChild->slowFirstChild();
3574 } else {
3575 currChild = currChild->slowFirstChild();
3576 }
3577 }
3578
3579 if (!currChild)
3580 return;
3581
3582 // If the child already has style, then it has already been created, so we j ust want
3583 // to update it.
3584 if (currChild->parent()->style()->styleType() == FIRST_LETTER) {
3585 updateFirstLetterStyle(firstLetterBlock, currChild);
3586 return;
3587 }
3588
3589 // FIXME: This black-list of disallowed RenderText subclasses is fragile.
3590 // Should counter be on this list? What about RenderTextFragment?
3591 if (!currChild->isText() || isInvalidFirstLetterRenderer(currChild))
3592 return;
3593
3594 createFirstLetterRenderer(firstLetterBlock, toRenderText(*currChild), length );
3595 }
3596
3597 // Helper methods for obtaining the last line, computing line counts and heights for line counts 3340 // Helper methods for obtaining the last line, computing line counts and heights for line counts
3598 // (crawling into blocks). 3341 // (crawling into blocks).
3599 static bool shouldCheckLines(RenderObject* obj) 3342 static bool shouldCheckLines(RenderObject* obj)
3600 { 3343 {
3601 return !obj->isFloatingOrOutOfFlowPositioned() 3344 return !obj->isFloatingOrOutOfFlowPositioned()
3602 && obj->isRenderBlock() && obj->style()->height().isAuto() 3345 && obj->isRenderBlock() && obj->style()->height().isAuto()
3603 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL); 3346 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL);
3604 } 3347 }
3605 3348
3606 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count) 3349 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count)
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
4183 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const 3926 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const
4184 { 3927 {
4185 showRenderObject(); 3928 showRenderObject();
4186 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box()) 3929 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box())
4187 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1); 3930 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1);
4188 } 3931 }
4189 3932
4190 #endif 3933 #endif
4191 3934
4192 } // namespace blink 3935 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/rendering/RenderBlock.h ('k') | Source/core/rendering/RenderBoxModelObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698