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

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

Powered by Google App Engine
This is Rietveld 408576698