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

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

Powered by Google App Engine
This is Rietveld 408576698