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

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

Powered by Google App Engine
This is Rietveld 408576698