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

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
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 1366 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 gDelayedUpdateScrollInfoSet->add(this); 1377 gDelayedUpdateScrollInfoSet->add(this);
1378 else 1378 else
1379 layer()->scrollableArea()->updateAfterLayout(); 1379 layer()->scrollableArea()->updateAfterLayout();
1380 } 1380 }
1381 } 1381 }
1382 1382
1383 void RenderBlock::layout() 1383 void RenderBlock::layout()
1384 { 1384 {
1385 OverflowEventDispatcher dispatcher(this); 1385 OverflowEventDispatcher dispatcher(this);
1386 1386
1387 // Update our first letter info now.
1388 updateFirstLetter();
1389
1390 // Table cells call layoutBlock directly, so don't add any logic here. Put code into 1387 // Table cells call layoutBlock directly, so don't add any logic here. Put code into
1391 // layoutBlock(). 1388 // layoutBlock().
1392 layoutBlock(false); 1389 layoutBlock(false);
1393 1390
1394 // It's safe to check for control clip here, since controls can never be tab le cells. 1391 // It's safe to check for control clip here, since controls can never be tab le cells.
1395 // If we have a lightweight clip, there can never be any overflow from child ren. 1392 // If we have a lightweight clip, there can never be any overflow from child ren.
1396 if (hasControlClip() && m_overflow) 1393 if (hasControlClip() && m_overflow)
1397 clearLayoutOverflow(); 1394 clearLayoutOverflow();
1398 1395
1399 invalidateBackgroundObscurationStatus(); 1396 invalidateBackgroundObscurationStatus();
(...skipping 1802 matching lines...) Expand 10 before | Expand all | Expand 10 after
3202 3199
3203 int scrollbarWidth = instrinsicScrollbarLogicalWidth(); 3200 int scrollbarWidth = instrinsicScrollbarLogicalWidth();
3204 maxLogicalWidth += scrollbarWidth; 3201 maxLogicalWidth += scrollbarWidth;
3205 minLogicalWidth += scrollbarWidth; 3202 minLogicalWidth += scrollbarWidth;
3206 } 3203 }
3207 3204
3208 void RenderBlock::computePreferredLogicalWidths() 3205 void RenderBlock::computePreferredLogicalWidths()
3209 { 3206 {
3210 ASSERT(preferredLogicalWidthsDirty()); 3207 ASSERT(preferredLogicalWidthsDirty());
3211 3208
3212 updateFirstLetter();
3213
3214 m_minPreferredLogicalWidth = 0; 3209 m_minPreferredLogicalWidth = 0;
3215 m_maxPreferredLogicalWidth = 0; 3210 m_maxPreferredLogicalWidth = 0;
3216 3211
3217 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you 3212 // FIXME: The isFixed() calls here should probably be checking for isSpecifi ed since you
3218 // should be able to use percentage, calc or viewport relative values for wi dth. 3213 // should be able to use percentage, calc or viewport relative values for wi dth.
3219 RenderStyle* styleToUse = style(); 3214 RenderStyle* styleToUse = style();
3220 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0 3215 if (!isTableCell() && styleToUse->logicalWidth().isFixed() && styleToUse->lo gicalWidth().value() >= 0
3221 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue())) 3216 && !(isDeprecatedFlexItem() && !styleToUse->logicalWidth().intValue()))
3222 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value()); 3217 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentB oxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
3223 else 3218 else
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
3561 break; 3556 break;
3562 firstLineBlock = toRenderBlock(parentBlock); 3557 firstLineBlock = toRenderBlock(parentBlock);
3563 } 3558 }
3564 3559
3565 if (!hasPseudo) 3560 if (!hasPseudo)
3566 return 0; 3561 return 0;
3567 3562
3568 return firstLineBlock; 3563 return firstLineBlock;
3569 } 3564 }
3570 3565
3571 static RenderStyle* styleForFirstLetter(RenderObject* firstLetterBlock, RenderOb ject* firstLetterContainer)
3572 {
3573 RenderStyle* pseudoStyle = firstLetterBlock->getCachedPseudoStyle(FIRST_LETT ER, firstLetterContainer->firstLineStyle());
3574 // Force inline display (except for floating first-letters).
3575 pseudoStyle->setDisplay(pseudoStyle->isFloating() ? BLOCK : INLINE);
3576 // CSS2 says first-letter can't be positioned.
3577 pseudoStyle->setPosition(StaticPosition);
3578 return pseudoStyle;
3579 }
3580
3581 // CSS 2.1 http://www.w3.org/TR/CSS21/selector.html#first-letter
3582 // "Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps) , "close" (Pe),
3583 // "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that prec edes or follows the first letter should be included"
3584 static inline bool isPunctuationForFirstLetter(UChar c)
3585 {
3586 CharCategory charCategory = category(c);
3587 return charCategory == Punctuation_Open
3588 || charCategory == Punctuation_Close
3589 || charCategory == Punctuation_InitialQuote
3590 || charCategory == Punctuation_FinalQuote
3591 || charCategory == Punctuation_Other;
3592 }
3593
3594 static inline bool isSpaceForFirstLetter(UChar c)
3595 {
3596 return isSpaceOrNewline(c) || c == noBreakSpace;
3597 }
3598
3599 static inline RenderObject* findFirstLetterBlock(RenderBlock* start)
3600 {
3601 RenderObject* firstLetterBlock = start;
3602 while (true) {
3603 bool canHaveFirstLetterRenderer = firstLetterBlock->style()->hasPseudoSt yle(FIRST_LETTER)
3604 && firstLetterBlock->canHaveGeneratedChildren()
3605 && isRenderBlockFlowOrRenderButton(firstLetterBlock);
3606 if (canHaveFirstLetterRenderer)
3607 return firstLetterBlock;
3608
3609 RenderObject* parentBlock = firstLetterBlock->parent();
3610 if (firstLetterBlock->isReplaced() || !parentBlock
3611 || !isRenderBlockFlowOrRenderButton(parentBlock)) {
3612 return 0;
3613 }
3614 ASSERT(parentBlock->isRenderBlock());
3615 if (toRenderBlock(parentBlock)->firstChild() != firstLetterBlock)
3616 return 0;
3617 firstLetterBlock = parentBlock;
3618 }
3619
3620 return 0;
3621 }
3622
3623 void RenderBlock::updateFirstLetterStyle(RenderObject* firstLetterBlock, RenderO bject* currentChild)
3624 {
3625 RenderObject* firstLetter = currentChild->parent();
3626 RenderObject* firstLetterContainer = firstLetter->parent();
3627 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3628 ASSERT(firstLetter->isFloating() || firstLetter->isInline());
3629
3630 if (RenderStyle::stylePropagationDiff(firstLetter->style(), pseudoStyle) == Reattach) {
3631 // The first-letter renderer needs to be replaced. Create a new renderer of the right type.
3632 RenderBoxModelObject* newFirstLetter;
3633 if (pseudoStyle->display() == INLINE)
3634 newFirstLetter = RenderInline::createAnonymous(&document());
3635 else
3636 newFirstLetter = RenderBlockFlow::createAnonymous(&document());
3637 newFirstLetter->setStyle(pseudoStyle);
3638
3639 // Move the first letter into the new renderer.
3640 while (RenderObject* child = firstLetter->slowFirstChild()) {
3641 if (child->isText())
3642 toRenderText(child)->removeAndDestroyTextBoxes();
3643 firstLetter->removeChild(child);
3644 newFirstLetter->addChild(child, 0);
3645 }
3646
3647 RenderObject* nextSibling = firstLetter->nextSibling();
3648 if (RenderTextFragment* remainingText = toRenderBoxModelObject(firstLett er)->firstLetterRemainingText()) {
3649 ASSERT(remainingText->isAnonymous() || remainingText->node()->render er() == remainingText);
3650 // Replace the old renderer with the new one.
3651 remainingText->setFirstLetter(newFirstLetter);
3652 newFirstLetter->setFirstLetterRemainingText(remainingText);
3653 }
3654 // To prevent removal of single anonymous block in RenderBlock::removeCh ild and causing
3655 // |nextSibling| to go stale, we remove the old first letter using remov eChildNode first.
3656 firstLetterContainer->virtualChildren()->removeChildNode(firstLetterCont ainer, firstLetter);
3657 firstLetter->destroy();
3658 firstLetter = newFirstLetter;
3659 firstLetterContainer->addChild(firstLetter, nextSibling);
3660 } else {
3661 firstLetter->setStyle(pseudoStyle);
3662 }
3663
3664 for (RenderObject* genChild = firstLetter->slowFirstChild(); genChild; genCh ild = genChild->nextSibling()) {
3665 if (genChild->isText())
3666 genChild->setStyle(pseudoStyle);
3667 }
3668 }
3669
3670 static inline unsigned firstLetterLength(const String& text)
3671 {
3672 unsigned length = 0;
3673 unsigned textLength = text.length();
3674
3675 if (textLength == 0)
3676 return length;
3677
3678 // Account for leading spaces first.
3679 while (length < textLength && isSpaceForFirstLetter(text[length]))
3680 length++;
3681
3682 // Now account for leading punctuation.
3683 while (length < textLength && isPunctuationForFirstLetter(text[length]))
3684 length++;
3685
3686 // Bail if we didn't find a letter before the end of the text or before a sp ace.
3687 if (isSpaceForFirstLetter(text[length]) || length == textLength)
3688 return 0;
3689
3690 // Account the next character for first letter.
3691 length++;
3692
3693 // Keep looking allowed punctuation for the :first-letter.
3694 for (unsigned scanLength = length; scanLength < textLength; ++scanLength) {
3695 UChar c = text[scanLength];
3696
3697 if (!isPunctuationForFirstLetter(c))
3698 break;
3699
3700 length = scanLength + 1;
3701 }
3702 return length;
3703 }
3704
3705 void RenderBlock::createFirstLetterRenderer(RenderObject* firstLetterBlock, Rend erText& currentChild, unsigned length)
3706 {
3707 ASSERT(length);
3708
3709 RenderObject* firstLetterContainer = currentChild.parent();
3710 RenderStyle* pseudoStyle = styleForFirstLetter(firstLetterBlock, firstLetter Container);
3711 RenderBoxModelObject* firstLetter = 0;
3712 if (pseudoStyle->display() == INLINE)
3713 firstLetter = RenderInline::createAnonymous(&document());
3714 else
3715 firstLetter = RenderBlockFlow::createAnonymous(&document());
3716 firstLetter->setStyle(pseudoStyle);
3717
3718 // FIXME: The first letter code should not modify the render tree during
3719 // layout. crbug.com/370458
3720 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
3721
3722 firstLetterContainer->addChild(firstLetter, &currentChild);
3723
3724 // The original string is going to be either a generated content string or a DOM node's
3725 // string. We want the original string before it got transformed in case fi rst-letter has
3726 // no text-transform or a different text-transform applied to it.
3727 String oldText = currentChild.originalText();
3728 ASSERT(oldText.impl());
3729
3730 // Construct a text fragment for the text after the first letter.
3731 // This text fragment might be empty.
3732 RenderTextFragment* remainingText =
3733 new RenderTextFragment(currentChild.node() ? currentChild.node() : &curr entChild.document(), oldText.impl(), length, oldText.length() - length);
3734 remainingText->setStyle(currentChild.style());
3735 if (remainingText->node())
3736 remainingText->node()->setRenderer(remainingText);
3737
3738 firstLetterContainer->addChild(remainingText, &currentChild);
3739 firstLetterContainer->removeChild(&currentChild);
3740 remainingText->setFirstLetter(firstLetter);
3741 firstLetter->setFirstLetterRemainingText(remainingText);
3742
3743 // construct text fragment for the first letter
3744 RenderTextFragment* letter =
3745 new RenderTextFragment(remainingText->node() ? remainingText->node() : & remainingText->document(), oldText.impl(), 0, length);
3746 letter->setStyle(pseudoStyle);
3747 firstLetter->addChild(letter);
3748
3749 currentChild.destroy();
3750 }
3751
3752 // Once we see any of these renderers we can stop looking for first-letter as
3753 // they signal the end of the first line of text.
3754 bool RenderBlock::isInvalidFirstLetterRenderer(RenderObject* obj) const
3755 {
3756 return (obj->isBR() || (obj->isText() && toRenderText(obj)->isWordBreak()));
3757 }
3758
3759 void RenderBlock::updateFirstLetter()
3760 {
3761 if (!document().styleEngine()->usesFirstLetterRules())
3762 return;
3763 // Don't recur
3764 if (style()->styleType() == FIRST_LETTER)
3765 return;
3766
3767 // FIXME: We need to destroy the first-letter object if it is no longer the first child. Need to find
3768 // an efficient way to check for that situation though before implementing a nything.
3769 RenderObject* firstLetterBlock = findFirstLetterBlock(this);
3770 if (!firstLetterBlock)
3771 return;
3772
3773 // Drill into inlines looking for our first text child.
3774 RenderObject* currChild = firstLetterBlock->slowFirstChild();
3775 unsigned length = 0;
3776 while (currChild) {
3777 if (currChild->isText()) {
3778 // FIXME: If there is leading punctuation in a different RenderText than
3779 // the first letter, we'll not apply the correct style to it.
3780 length = firstLetterLength(toRenderText(currChild)->originalText());
3781 if (length || isInvalidFirstLetterRenderer(currChild))
3782 break;
3783 currChild = currChild->nextSibling();
3784 } else if (currChild->isListMarker()) {
3785 currChild = currChild->nextSibling();
3786 } else if (currChild->isFloatingOrOutOfFlowPositioned()) {
3787 if (currChild->style()->styleType() == FIRST_LETTER) {
3788 currChild = currChild->slowFirstChild();
3789 break;
3790 }
3791 currChild = currChild->nextSibling();
3792 } else if (currChild->isReplaced() || currChild->isRenderButton() || cur rChild->isMenuList()) {
3793 break;
3794 } else if (currChild->isFlexibleBoxIncludingDeprecated() || currChild->i sRenderGrid()) {
3795 return;
3796 } else if (currChild->style()->hasPseudoStyle(FIRST_LETTER) && currChild ->canHaveGeneratedChildren()) {
3797 // We found a lower-level node with first-letter, which supersedes t he higher-level style
3798 firstLetterBlock = currChild;
3799 currChild = currChild->slowFirstChild();
3800 } else {
3801 currChild = currChild->slowFirstChild();
3802 }
3803 }
3804
3805 if (!currChild)
3806 return;
3807
3808 // If the child already has style, then it has already been created, so we j ust want
3809 // to update it.
3810 if (currChild->parent()->style()->styleType() == FIRST_LETTER) {
3811 updateFirstLetterStyle(firstLetterBlock, currChild);
3812 return;
3813 }
3814
3815 // FIXME: This black-list of disallowed RenderText subclasses is fragile.
3816 // Should counter be on this list? What about RenderTextFragment?
3817 if (!currChild->isText() || isInvalidFirstLetterRenderer(currChild))
3818 return;
3819
3820 createFirstLetterRenderer(firstLetterBlock, toRenderText(*currChild), length );
3821 }
3822
3823 // Helper methods for obtaining the last line, computing line counts and heights for line counts 3566 // Helper methods for obtaining the last line, computing line counts and heights for line counts
3824 // (crawling into blocks). 3567 // (crawling into blocks).
3825 static bool shouldCheckLines(RenderObject* obj) 3568 static bool shouldCheckLines(RenderObject* obj)
3826 { 3569 {
3827 return !obj->isFloatingOrOutOfFlowPositioned() 3570 return !obj->isFloatingOrOutOfFlowPositioned()
3828 && obj->isRenderBlock() && obj->style()->height().isAuto() 3571 && obj->isRenderBlock() && obj->style()->height().isAuto()
3829 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL); 3572 && (!obj->isDeprecatedFlexibleBox() || obj->style()->boxOrient() == VERT ICAL);
3830 } 3573 }
3831 3574
3832 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count) 3575 static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count)
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
4409 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const 4152 void RenderBlock::showLineTreeAndMark(const InlineBox* markedBox1, const char* m arkedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const Render Object* obj) const
4410 { 4153 {
4411 showRenderObject(); 4154 showRenderObject();
4412 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box()) 4155 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRoot Box())
4413 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1); 4156 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLa bel2, obj, 1);
4414 } 4157 }
4415 4158
4416 #endif 4159 #endif
4417 4160
4418 } // namespace blink 4161 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698