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

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

Powered by Google App Engine
This is Rietveld 408576698