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

Side by Side Diff: Source/core/rendering/RenderBlock.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
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 1338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1349 gDelayedUpdateScrollInfoSet->add(this); 1349 gDelayedUpdateScrollInfoSet->add(this);
1350 else 1350 else
1351 layer()->scrollableArea()->updateAfterLayout(); 1351 layer()->scrollableArea()->updateAfterLayout();
1352 } 1352 }
1353 } 1353 }
1354 1354
1355 void RenderBlock::layout() 1355 void RenderBlock::layout()
1356 { 1356 {
1357 OverflowEventDispatcher dispatcher(this); 1357 OverflowEventDispatcher dispatcher(this);
1358 1358
1359 // Update our first letter info now.
1360 updateFirstLetter();
1361
1362 // Table cells call layoutBlock directly, so don't add any logic here. Put code into 1359 // Table cells call layoutBlock directly, so don't add any logic here. Put code into
1363 // layoutBlock(). 1360 // layoutBlock().
1364 layoutBlock(false); 1361 layoutBlock(false);
1365 1362
1366 // It's safe to check for control clip here, since controls can never be tab le cells. 1363 // It's safe to check for control clip here, since controls can never be tab le cells.
1367 // If we have a lightweight clip, there can never be any overflow from child ren. 1364 // If we have a lightweight clip, there can never be any overflow from child ren.
1368 if (hasControlClip() && m_overflow) 1365 if (hasControlClip() && m_overflow)
1369 clearLayoutOverflow(); 1366 clearLayoutOverflow();
1370 1367
1371 invalidateBackgroundObscurationStatus(); 1368 invalidateBackgroundObscurationStatus();
(...skipping 1790 matching lines...) Expand 10 before | Expand all | Expand 10 after
3162 3159
3163 int scrollbarWidth = instrinsicScrollbarLogicalWidth(); 3160 int scrollbarWidth = instrinsicScrollbarLogicalWidth();
3164 maxLogicalWidth += scrollbarWidth; 3161 maxLogicalWidth += scrollbarWidth;
3165 minLogicalWidth += scrollbarWidth; 3162 minLogicalWidth += scrollbarWidth;
3166 } 3163 }
3167 3164
3168 void RenderBlock::computePreferredLogicalWidths() 3165 void RenderBlock::computePreferredLogicalWidths()
3169 { 3166 {
3170 ASSERT(preferredLogicalWidthsDirty()); 3167 ASSERT(preferredLogicalWidthsDirty());
3171 3168
3172 updateFirstLetter();
3173
3174 m_minPreferredLogicalWidth = 0; 3169 m_minPreferredLogicalWidth = 0;
3175 m_maxPreferredLogicalWidth = 0; 3170 m_maxPreferredLogicalWidth = 0;
3176 3171
3177 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you 3172 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you
3178 // should be able to use percentage, calc or viewport relative values for wi dth. 3173 // should be able to use percentage, calc or viewport relative values for wi dth.
3179 RenderStyle* styleToUse = style(); 3174 RenderStyle* styleToUse = style();
3180 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0 3175 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0
3181 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue())) 3176 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue()))
3182 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value()); 3177 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
3183 else 3178 else
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
3519 break; 3514 break;
3520 firstLineBlock = toRenderBlock(parentBlock); 3515 firstLineBlock = toRenderBlock(parentBlock);
3521 } 3516 }
3522 3517
3523 if (!hasPseudo) 3518 if (!hasPseudo)
3524 return 0; 3519 return 0;
3525 3520
3526 return firstLineBlock; 3521 return firstLineBlock;
3527 } 3522 }
3528 3523
3529 static RenderStyle* styleForFirstLetter(RenderObject* firstLetterBlock, RenderOb ject* firstLetterContainer)
3530 {
3531 RenderStyle* pseudoStyle = firstLetterBlock->getCachedPseudoStyle(FIRST_LETT ER, firstLetterContainer->firstLineStyle());
3532 // Force inline display (except for floating first-letters).
3533 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
3534 // CSS2 says first-letter can't be positioned.
3535 pseudoStyle->setPosition(StaticPosition);
3536 return pseudoStyle;
3537 }
3538
3539 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
3540 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
3541 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
3542 static inline bool isPunctuationForFirstLetter(UChar c)
3543 {
3544 CharCategory charCategory = category(c);
3545 return charCategory == Punctuation_Open
3546 || charCategory == Punctuation_Close
3547 || charCategory == Punctuation_InitialQuote
3548 || charCategory == Punctuation_FinalQuote
3549 || charCategory == Punctuation_Other;
3550 }
3551
3552 static inline bool isSpaceForFirstLetter(UChar c)
3553 {
3554 return isSpaceOrNewline(c) || c == noBreakSpace;
3555 }
3556
3557 static inline RenderObject* findFirstLetterBlock(RenderBlock* start)
3558 {
3559 RenderObject* firstLetterBlock = start;
3560 while (true) {
3561 bool canHaveFirstLetterRenderer = firstLetterBlock->style()->hasPseudoSt yle(FIRST_LETTER)
3562 && firstLetterBlock->canHaveGeneratedChildren()
3563 && isRenderBlockFlowOrRenderButton(firstLetterBlock);
3564 if (canHaveFirstLetterRenderer)
3565 return firstLetterBlock;
3566
3567 RenderObject* parentBlock = firstLetterBlock->parent();
3568 if (firstLetterBlock->isReplaced() || !parentBlock
3569 || !isRenderBlockFlowOrRenderButton(parentBlock)) {
3570 return 0;
3571 }
3572 ASSERT(parentBlock->isRenderBlock());
3573 if (toRenderBlock(parentBlock)->firstChild() != firstLetterBlock)
3574 return 0;
3575 firstLetterBlock = parentBlock;
3576 }
3577
3578 return 0;
3579 }
3580
3581 void RenderBlock::updateFirstLetterStyle(RenderObject* firstLetterBlock, RenderO bject* currentChild)
3582 {
3583 RenderObject* firstLetter = currentChild->parent();
3584 RenderObject* firstLetterContainer = firstLetter->parent();
3585 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3586 ASSERT(firstLetter->isFloating() || firstLetter->isInline());
3587
3588 if (RenderStyle::stylePropagationDiff(firstLetter->style(), pseudoStyle) == Reattach) {
3589 // The first-letter renderer needs to be replaced. Create a new renderer of the right type.
3590 RenderBoxModelObject* newFirstLetter;
3591 if (pseudoStyle->display() == INLINE)
3592 newFirstLetter = RenderInline::createAnonymous(&document());
3593 else
3594 newFirstLetter = RenderBlockFlow::createAnonymous(&document());
3595 newFirstLetter->setStyle(pseudoStyle);
3596
3597 // Move the first letter into the new renderer.
3598 while (RenderObject* child = firstLetter->slowFirstChild()) {
3599 if (child->isText())
3600 toRenderText(child)->removeAndDestroyTextBoxes();
3601 firstLetter->removeChild(child);
3602 newFirstLetter->addChild(child, 0);
3603 }
3604
3605 RenderObject* nextSibling = firstLetter->nextSibling();
3606 if (RenderTextFragment* remainingText = toRenderBoxModelObject(firstLett er)->firstLetterRemainingText()) {
3607 ASSERT(remainingText->isAnonymous() || remainingText->node()->render er() == remainingText);
3608 // Replace the old renderer with the new one.
3609 remainingText->setFirstLetter(newFirstLetter);
3610 newFirstLetter->setFirstLetterRemainingText(remainingText);
3611 }
3612 // To prevent removal of single anonymous block in RenderBlock::removeCh ild and causing
3613 // |nextSibling| to go stale, we remove the old first letter using remov eChildNode first.
3614 firstLetterContainer->virtualChildren()->removeChildNode(firstLetterCont ainer, firstLetter);
3615 firstLetter->destroy();
3616 firstLetter = newFirstLetter;
3617 firstLetterContainer->addChild(firstLetter, nextSibling);
3618 } else
3619 firstLetter->setStyle(pseudoStyle);
3620
3621 for (RenderObject* genChild = firstLetter->slowFirstChild(); genChild; genCh ild = genChild->nextSibling()) {
3622 if (genChild->isText())
3623 genChild->setStyle(pseudoStyle);
3624 }
3625 }
3626
3627 static inline unsigned firstLetterLength(const String& text)
3628 {
3629 unsigned length = 0;
3630 unsigned textLength = text.length();
3631
3632 // Account for leading spaces first.
3633 while (length < textLength && isSpaceForFirstLetter(text[length]))
3634 length++;
3635
3636 // Now account for leading punctuation.
3637 while (length < textLength && isPunctuationForFirstLetter(text[length]))
3638 length++;
3639
3640 // Bail if we didn't find a letter before the end of the text or before a sp ace.
3641 if (isSpaceForFirstLetter(text[length]) || (textLength && length == textLeng th))
3642 return 0;
3643
3644 // Account the next character for first letter.
3645 length++;
3646
3647 // Keep looking allowed punctuation for the :first-letter.
3648 for (unsigned scanLength = length; scanLength < textLength; ++scanLength) {
3649 UChar c = text[scanLength];
3650
3651 if (!isPunctuationForFirstLetter(c))
3652 break;
3653
3654 length = scanLength + 1;
3655 }
3656
3657 // FIXME: If textLength is 0, length may still be 1!
3658 return length;
3659 }
3660
3661 void RenderBlock::createFirstLetterRenderer(RenderObject* firstLetterBlock, Rend erText& currentChild, unsigned length)
3662 {
3663 ASSERT(length);
3664
3665 RenderObject* firstLetterContainer = currentChild.parent();
3666 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3667 RenderBoxModelObject* firstLetter = 0;
3668 if (pseudoStyle->display() == INLINE)
3669 firstLetter = RenderInline::createAnonymous(&document());
3670 else
3671 firstLetter = RenderBlockFlow::createAnonymous(&document());
3672 firstLetter->setStyle(pseudoStyle);
3673
3674 // FIXME: The first letter code should not modify the render tree during
3675 // layout. crbug.com/370458
3676 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
3677
3678 firstLetterContainer->addChild(firstLetter, &currentChild);
3679
3680 // The original string is going to be either a generated content string or a DOM node's
3681 // string. We want the original string before it got transformed in case fi rst-letter has
3682 // no text-transform or a different text-transform applied to it.
3683 String oldText = currentChild.originalText();
3684 ASSERT(oldText.impl());
3685
3686 // Construct a text fragment for the text after the first letter.
3687 // This text fragment might be empty.
3688 RenderTextFragment* remainingText =
3689 new RenderTextFragment(currentChild.node() ? currentChild.node() : &curr entChild.document(), oldText.impl(), length, oldText.length() - length);
3690 remainingText->setStyle(currentChild.style());
3691 if (remainingText->node())
3692 remainingText->node()->setRenderer(remainingText);
3693
3694 firstLetterContainer->addChild(remainingText, &currentChild);
3695 firstLetterContainer->removeChild(&currentChild);
3696 remainingText->setFirstLetter(firstLetter);
3697 firstLetter->setFirstLetterRemainingText(remainingText);
3698
3699 // construct text fragment for the first letter
3700 RenderTextFragment* letter =
3701 new RenderTextFragment(remainingText->node() ? remainingText->node() : & remainingText->document(), oldText.impl(), 0, length);
3702 letter->setStyle(pseudoStyle);
3703 firstLetter->addChild(letter);
3704
3705 currentChild.destroy();
3706 }
3707
3708 void RenderBlock::updateFirstLetter()
3709 {
3710 if (!document().styleEngine()->usesFirstLetterRules())
3711 return;
3712 // Don't recur
3713 if (style()->styleType() == FIRST_LETTER)
3714 return;
3715
3716 // FIXME: We need to destroy the first-letter object if it is no longer the first child. Need to find
3717 // an efficient way to check for that situation though before implementing a nything.
3718 RenderObject* firstLetterBlock = findFirstLetterBlock(this);
3719 if (!firstLetterBlock)
3720 return;
3721
3722 // Drill into inlines looking for our first text child.
3723 RenderObject* currChild = firstLetterBlock->slowFirstChild();
3724 unsigned length = 0;
3725 while (currChild) {
3726 if (currChild->isText()) {
3727 // FIXME: If there is leading punctuation in a different RenderText than
3728 // the first letter, we'll not apply the correct style to it.
3729 length = firstLetterLength(toRenderText(currChild)->originalText());
3730 if (length)
3731 break;
3732 currChild = currChild->nextSibling();
3733 } else if (currChild->isListMarker()) {
3734 currChild = currChild->nextSibling();
3735 } else if (currChild->isFloatingOrOutOfFlowPositioned()) {
3736 if (currChild->style()->styleType() == FIRST_LETTER) {
3737 currChild = currChild->slowFirstChild();
3738 break;
3739 }
3740 currChild = currChild->nextSibling();
3741 } else if (currChild->isReplaced() || currChild->isRenderButton() || cur rChild->isMenuList()) {
3742 break;
3743 } else if (currChild->style()->hasPseudoStyle(FIRST_LETTER) && currChild ->canHaveGeneratedChildren()) {
3744 // We found a lower-level node with first-letter, which supersedes t he higher-level style
3745 firstLetterBlock = currChild;
3746 currChild = currChild->slowFirstChild();
3747 } else {
3748 currChild = currChild->slowFirstChild();
3749 }
3750 }
3751
3752 if (!currChild || !isRenderBlockFlowOrRenderButton(firstLetterBlock))
3753 return;
3754
3755 // If the child already has style, then it has already been created, so we j ust want
3756 // to update it.
3757 if (currChild->parent()->style()->styleType() == FIRST_LETTER) {
3758 updateFirstLetterStyle(firstLetterBlock, currChild);
3759 return;
3760 }
3761
3762 // FIXME: This black-list of disallowed RenderText subclasses is fragile.
3763 // Should counter be on this list? What about RenderTextFragment?
3764 if (!currChild->isText() || currChild->isBR() || toRenderText(currChild)->is WordBreak())
3765 return;
3766
3767 createFirstLetterRenderer(firstLetterBlock, toRenderText(*currChild), length );
3768 }
3769
3770 // Helper methods for obtaining the last line, computing line counts and heights for line counts 3524 // Helper methods for obtaining the last line, computing line counts and heights for line counts
3771 // (crawling into blocks). 3525 // (crawling into blocks).
3772 static bool shouldCheckLines(RenderObject* obj) 3526 static bool shouldCheckLines(RenderObject* obj)
3773 { 3527 {
3774 return !obj->isFloatingOrOutOfFlowPositioned() 3528 return !obj->isFloatingOrOutOfFlowPositioned()
3775 && obj->isRenderBlock() && obj->style()->height().isAuto() 3529 && obj->isRenderBlock() && obj->style()->height().isAuto()
3776 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL); 3530 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL);
3777 } 3531 }
3778 3532
3779 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count) 3533 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count)
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
4356 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const 4110 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const
4357 { 4111 {
4358 showRenderObject(); 4112 showRenderObject();
4359 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box()) 4113 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box())
4360 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1); 4114 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1);
4361 } 4115 }
4362 4116
4363 #endif 4117 #endif
4364 4118
4365 } // namespace blink 4119 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698