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

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

Powered by Google App Engine
This is Rietveld 408576698