Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2000 Dirk Mueller (mueller (at) kde.org)
      4  * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
      5  * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  *
     22  */
     23 
     24 #include "config.h"
     25 #include "core/rendering/RenderReplaced.h"
     26 
     27 #include "core/platform/graphics/GraphicsContext.h"
     28 #include "core/rendering/LayoutRepainter.h"
     29 #include "core/rendering/RenderBlock.h"
     30 #include "core/rendering/RenderLayer.h"
     31 #include "core/rendering/RenderView.h"
     32 
     33 using namespace std;
     34 
     35 namespace WebCore {
     36 
     37 const int cDefaultWidth = 300;
     38 const int cDefaultHeight = 150;
     39 
     40 RenderReplaced::RenderReplaced(Element* element)
     41     : RenderBox(element)
     42     , m_intrinsicSize(cDefaultWidth, cDefaultHeight)
     43 {
     44     setReplaced(true);
     45 }
     46 
     47 RenderReplaced::RenderReplaced(Element* element, const LayoutSize& intrinsicSize)
     48     : RenderBox(element)
     49     , m_intrinsicSize(intrinsicSize)
     50 {
     51     setReplaced(true);
     52 }
     53 
     54 RenderReplaced::~RenderReplaced()
     55 {
     56 }
     57 
     58 void RenderReplaced::willBeDestroyed()
     59 {
     60     if (!documentBeingDestroyed() && parent())
     61         parent()->dirtyLinesFromChangedChild(this);
     62 
     63     RenderBox::willBeDestroyed();
     64 }
     65 
     66 void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
     67 {
     68     RenderBox::styleDidChange(diff, oldStyle);
     69 
     70     bool hadStyle = (oldStyle != 0);
     71     float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
     72     if (style() && style()->effectiveZoom() != oldZoom)
     73         intrinsicSizeChanged();
     74 }
     75 
     76 void RenderReplaced::layout()
     77 {
     78     StackStats::LayoutCheckPoint layoutCheckPoint;
     79     ASSERT(needsLayout());
     80 
     81     LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
     82 
     83     setHeight(minimumReplacedHeight());
     84 
     85     updateLogicalWidth();
     86     updateLogicalHeight();
     87 
     88     m_overflow.clear();
     89     addVisualEffectOverflow();
     90     updateLayerTransform();
     91     invalidateBackgroundObscurationStatus();
     92 
     93     repainter.repaintAfterLayout();
     94     clearNeedsLayout();
     95 }
     96 
     97 void RenderReplaced::intrinsicSizeChanged()
     98 {
     99     int scaledWidth = static_cast<int>(cDefaultWidth * style()->effectiveZoom());
    100     int scaledHeight = static_cast<int>(cDefaultHeight * style()->effectiveZoom());
    101     m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
    102     setNeedsLayoutAndPrefWidthsRecalc();
    103 }
    104 
    105 void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
    106 {
    107     ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
    108 
    109     if (!shouldPaint(paintInfo, paintOffset))
    110         return;
    111 
    112     LayoutPoint adjustedPaintOffset = paintOffset + location();
    113 
    114     if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
    115         paintBoxDecorations(paintInfo, adjustedPaintOffset);
    116 
    117     if (paintInfo.phase == PaintPhaseMask) {
    118         paintMask(paintInfo, adjustedPaintOffset);
    119         return;
    120     }
    121 
    122     LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
    123     if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth())
    124         paintOutline(paintInfo, paintRect);
    125 
    126     if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && !canHaveChildren())
    127         return;
    128 
    129     if (!paintInfo.shouldPaintWithinRoot(this))
    130         return;
    131 
    132     bool drawSelectionTint = selectionState() != SelectionNone && !document()->printing();
    133     if (paintInfo.phase == PaintPhaseSelection) {
    134         if (selectionState() == SelectionNone)
    135             return;
    136         drawSelectionTint = false;
    137     }
    138 
    139     bool completelyClippedOut = false;
    140     if (style()->hasBorderRadius()) {
    141         LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
    142 
    143         if (borderRect.isEmpty())
    144             completelyClippedOut = true;
    145         else {
    146             // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
    147             paintInfo.context->save();
    148             RoundedRect roundedInnerRect = style()->getRoundedInnerBorderFor(paintRect,
    149                 paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true);
    150             clipRoundedInnerRect(paintInfo.context, paintRect, roundedInnerRect);
    151         }
    152     }
    153 
    154     if (!completelyClippedOut) {
    155         paintReplaced(paintInfo, adjustedPaintOffset);
    156 
    157         if (style()->hasBorderRadius())
    158             paintInfo.context->restore();
    159     }
    160 
    161     // The selection tint never gets clipped by border-radius rounding, since we want it to run right up to the edges of
    162     // surrounding content.
    163     if (drawSelectionTint) {
    164         LayoutRect selectionPaintingRect = localSelectionRect();
    165         selectionPaintingRect.moveBy(adjustedPaintOffset);
    166         paintInfo.context->fillRect(pixelSnappedIntRect(selectionPaintingRect), selectionBackgroundColor());
    167     }
    168 }
    169 
    170 bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
    171 {
    172     if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
    173             && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask)
    174         return false;
    175 
    176     if (!paintInfo.shouldPaintWithinRoot(this))
    177         return false;
    178 
    179     // if we're invisible or haven't received a layout yet, then just bail.
    180     if (style()->visibility() != VISIBLE)
    181         return false;
    182 
    183     LayoutPoint adjustedPaintOffset = paintOffset + location();
    184 
    185     // Early exit if the element touches the edges.
    186     LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y();
    187     LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY();
    188     if (isSelected() && m_inlineBoxWrapper) {
    189         LayoutUnit selTop = paintOffset.y() + m_inlineBoxWrapper->root()->selectionTop();
    190         LayoutUnit selBottom = paintOffset.y() + selTop + m_inlineBoxWrapper->root()->selectionHeight();
    191         top = min(selTop, top);
    192         bottom = max(selBottom, bottom);
    193     }
    194 
    195     LayoutRect localRepaintRect = paintInfo.rect;
    196     localRepaintRect.inflate(maximalOutlineSize(paintInfo.phase));
    197     if (adjustedPaintOffset.x() + visualOverflowRect().x() >= localRepaintRect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= localRepaintRect.x())
    198         return false;
    199 
    200     if (top >= localRepaintRect.maxY() || bottom <= localRepaintRect.y())
    201         return false;
    202 
    203     return true;
    204 }
    205 
    206 static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced)
    207 {
    208     // We have to lookup the containing block, which has an explicit width, which must not be equal to our direct containing block.
    209     // If the embedded document appears _after_ we performed the initial layout, our intrinsic size is 300x150. If our containing
    210     // block doesn't provide an explicit width, it's set to the 300 default, coming from the initial layout run.
    211     RenderBlock* containingBlock = replaced->containingBlock();
    212     if (!containingBlock)
    213         return 0;
    214 
    215     for (; !containingBlock->isRenderView() && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) {
    216         if (containingBlock->style()->logicalWidth().isSpecified())
    217             return containingBlock;
    218     }
    219 
    220     return 0;
    221 }
    222 
    223 bool RenderReplaced::hasReplacedLogicalWidth() const
    224 {
    225     if (style()->logicalWidth().isSpecified())
    226         return true;
    227 
    228     if (style()->logicalWidth().isAuto())
    229         return false;
    230 
    231     return firstContainingBlockWithLogicalWidth(this);
    232 }
    233 
    234 bool RenderReplaced::hasReplacedLogicalHeight() const
    235 {
    236     if (style()->logicalHeight().isAuto())
    237         return false;
    238 
    239     if (style()->logicalHeight().isSpecified()) {
    240         if (hasAutoHeightOrContainingBlockWithAutoHeight())
    241             return false;
    242         return true;
    243     }
    244 
    245     if (style()->logicalHeight().isIntrinsic())
    246         return true;
    247 
    248     return false;
    249 }
    250 
    251 bool RenderReplaced::needsPreferredWidthsRecalculation() const
    252 {
    253     // If the height is a percentage and the width is auto, then the containingBlocks's height changing can cause
    254     // this node to change it's preferred width because it maintains aspect ratio.
    255     return hasRelativeLogicalHeight() && style()->logicalWidth().isAuto() && !hasAutoHeightOrContainingBlockWithAutoHeight();
    256 }
    257 
    258 static inline bool rendererHasAspectRatio(const RenderObject* renderer)
    259 {
    260     ASSERT(renderer);
    261     return renderer->isImage() || renderer->isCanvas() || renderer->isVideo();
    262 }
    263 
    264 void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
    265 {
    266     FloatSize intrinsicSize;
    267     if (contentRenderer) {
    268         contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
    269         if (intrinsicRatio)
    270             ASSERT(!isPercentageIntrinsicSize);
    271 
    272         // Handle zoom & vertical writing modes here, as the embedded document doesn't know about them.
    273         if (!isPercentageIntrinsicSize)
    274             intrinsicSize.scale(style()->effectiveZoom());
    275 
    276         if (rendererHasAspectRatio(this) && isPercentageIntrinsicSize)
    277             intrinsicRatio = 1;
    278 
    279         // Update our intrinsic size to match what the content renderer has computed, so that when we
    280         // constrain the size below, the correct intrinsic size will be obtained for comparison against
    281         // min and max widths.
    282         if (intrinsicRatio && !isPercentageIntrinsicSize && !intrinsicSize.isEmpty())
    283             m_intrinsicSize = LayoutSize(intrinsicSize);
    284 
    285         if (!isHorizontalWritingMode()) {
    286             if (intrinsicRatio)
    287                 intrinsicRatio = 1 / intrinsicRatio;
    288             intrinsicSize = intrinsicSize.transposedSize();
    289         }
    290     } else {
    291         computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
    292         if (intrinsicRatio) {
    293             ASSERT(!isPercentageIntrinsicSize);
    294             if (!intrinsicSize.isEmpty())
    295                 m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
    296         }
    297     }
    298 
    299     // Now constrain the intrinsic size along each axis according to minimum and maximum width/heights along the
    300     // opposite axis. So for example a maximum width that shrinks our width will result in the height we compute here
    301     // having to shrink in order to preserve the aspect ratio. Because we compute these values independently along
    302     // each axis, the final returned size may in fact not preserve the aspect ratio.
    303     // FIXME: In the long term, it might be better to just return this code more to the way it used to be before this
    304     // function was added, since all it has done is make the code more unclear.
    305     constrainedSize = intrinsicSize;
    306     if (intrinsicRatio && !isPercentageIntrinsicSize && !intrinsicSize.isEmpty() && style()->logicalWidth().isAuto() && style()->logicalHeight().isAuto()) {
    307         // We can't multiply or divide by 'intrinsicRatio' here, it breaks tests, like fast/images/zoomed-img-size.html, which
    308         // can only be fixed once subpixel precision is available for things like intrinsicWidth/Height - which include zoom!
    309         constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height());
    310         constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width());
    311     }
    312 }
    313 
    314 void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
    315 {
    316     // If there's an embeddedContentBox() of a remote, referenced document available, this code-path should never be used.
    317     ASSERT(!embeddedContentBox());
    318     isPercentageIntrinsicSize = false;
    319     intrinsicSize = FloatSize(intrinsicLogicalWidth(), intrinsicLogicalHeight());
    320 
    321     // Figure out if we need to compute an intrinsic ratio.
    322     if (intrinsicSize.isEmpty() || !rendererHasAspectRatio(this))
    323         return;
    324 
    325     intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
    326 }
    327 
    328 LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
    329 {
    330     if (style()->logicalWidth().isSpecified() || style()->logicalWidth().isIntrinsic())
    331         return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style()->logicalWidth()), shouldComputePreferred);
    332 
    333     RenderBox* contentRenderer = embeddedContentBox();
    334 
    335     // 10.3.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width
    336     bool isPercentageIntrinsicSize = false;
    337     double intrinsicRatio = 0;
    338     FloatSize constrainedSize;
    339     computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio, isPercentageIntrinsicSize);
    340 
    341     if (style()->logicalWidth().isAuto()) {
    342         bool heightIsAuto = style()->logicalHeight().isAuto();
    343         bool hasIntrinsicWidth = !isPercentageIntrinsicSize && constrainedSize.width() > 0;
    344 
    345         // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width, then that intrinsic width is the used value of 'width'.
    346         if (heightIsAuto && hasIntrinsicWidth)
    347             return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
    348 
    349         bool hasIntrinsicHeight = !isPercentageIntrinsicSize && constrainedSize.height() > 0;
    350         if (intrinsicRatio || isPercentageIntrinsicSize) {
    351             // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width, but does have an intrinsic height and intrinsic ratio;
    352             // or if 'width' has a computed value of 'auto', 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value
    353             // of 'width' is: (used height) * (intrinsic ratio)
    354             if (intrinsicRatio && ((heightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !heightIsAuto)) {
    355                 LayoutUnit logicalHeight = computeReplacedLogicalHeight();
    356                 return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
    357             }
    358 
    359             // If 'height' and 'width' both have computed values of 'auto' and the element has an intrinsic ratio but no intrinsic height or width, then the used value of
    360             // 'width' is undefined in CSS 2.1. However, it is suggested that, if the containing block's width does not itself depend on the replaced element's width, then
    361             // the used value of 'width' is calculated from the constraint equation used for block-level, non-replaced elements in normal flow.
    362             if (heightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) {
    363                 // The aforementioned 'constraint equation' used for block-level, non-replaced elements in normal flow:
    364                 // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
    365                 LayoutUnit logicalWidth;
    366                 if (RenderBlock* blockWithWidth = firstContainingBlockWithLogicalWidth(this))
    367                     logicalWidth = blockWithWidth->computeReplacedLogicalWidthRespectingMinMaxWidth(blockWithWidth->computeReplacedLogicalWidthUsing(blockWithWidth->style()->logicalWidth()), shouldComputePreferred);
    368                 else
    369                     logicalWidth = containingBlock()->availableLogicalWidth();
    370 
    371                 // This solves above equation for 'width' (== logicalWidth).
    372                 LayoutUnit marginStart = minimumValueForLength(style()->marginStart(), logicalWidth);
    373                 LayoutUnit marginEnd = minimumValueForLength(style()->marginEnd(), logicalWidth);
    374                 logicalWidth = max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
    375                 if (isPercentageIntrinsicSize)
    376                     logicalWidth = logicalWidth * constrainedSize.width() / 100;
    377                 return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
    378             }
    379         }
    380 
    381         // Otherwise, if 'width' has a computed value of 'auto', and the element has an intrinsic width, then that intrinsic width is the used value of 'width'.
    382         if (hasIntrinsicWidth)
    383             return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
    384 
    385         // Otherwise, if 'width' has a computed value of 'auto', but none of the conditions above are met, then the used value of 'width' becomes 300px. If 300px is too
    386         // wide to fit the device, UAs should use the width of the largest rectangle that has a 2:1 ratio and fits the device instead.
    387         // Note: We fall through and instead return intrinsicLogicalWidth() here - to preserve existing WebKit behavior, which might or might not be correct, or desired.
    388         // Changing this to return cDefaultWidth, will affect lots of test results. Eg. some tests assume that a blank <img> tag (which implies width/height=auto)
    389         // has no intrinsic size, which is wrong per CSS 2.1, but matches our behavior since a long time.
    390     }
    391 
    392     return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred);
    393 }
    394 
    395 LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
    396 {
    397     // 10.5 Content height: the 'height' property: http://www.w3.org/TR/CSS21/visudet.html#propdef-height
    398     if (hasReplacedLogicalHeight())
    399         return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style()->logicalHeight()));
    400 
    401     RenderBox* contentRenderer = embeddedContentBox();
    402 
    403     // 10.6.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height
    404     bool isPercentageIntrinsicSize = false;
    405     double intrinsicRatio = 0;
    406     FloatSize constrainedSize;
    407     computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio, isPercentageIntrinsicSize);
    408 
    409     bool widthIsAuto = style()->logicalWidth().isAuto();
    410     bool hasIntrinsicHeight = !isPercentageIntrinsicSize && constrainedSize.height() > 0;
    411 
    412     // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic height, then that intrinsic height is the used value of 'height'.
    413     if (widthIsAuto && hasIntrinsicHeight)
    414         return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
    415 
    416     // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
    417     // (used width) / (intrinsic ratio)
    418     if (intrinsicRatio)
    419         return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
    420 
    421     // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic height, then that intrinsic height is the used value of 'height'.
    422     if (hasIntrinsicHeight)
    423         return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
    424 
    425     // Otherwise, if 'height' has a computed value of 'auto', but none of the conditions above are met, then the used value of 'height' must be set to the height
    426     // of the largest rectangle that has a 2:1 ratio, has a height not greater than 150px, and has a width not greater than the device width.
    427     return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
    428 }
    429 
    430 void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
    431 {
    432     minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth();
    433 }
    434 
    435 void RenderReplaced::computePreferredLogicalWidths()
    436 {
    437     ASSERT(preferredLogicalWidthsDirty());
    438 
    439     // We cannot resolve any percent logical width here as the available logical
    440     // width may not be set on our containing block.
    441     if (style()->logicalWidth().isPercent())
    442         computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
    443     else
    444         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
    445 
    446     RenderStyle* styleToUse = style();
    447     if (styleToUse->logicalWidth().isPercent() || styleToUse->logicalMaxWidth().isPercent() || hasRelativeIntrinsicLogicalWidth())
    448         m_minPreferredLogicalWidth = 0;
    449 
    450     if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
    451         m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
    452         m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
    453     }
    454 
    455     if (styleToUse->logicalMaxWidth().isFixed()) {
    456         m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
    457         m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
    458     }
    459 
    460     LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
    461     m_minPreferredLogicalWidth += borderAndPadding;
    462     m_maxPreferredLogicalWidth += borderAndPadding;
    463 
    464     setPreferredLogicalWidthsDirty(false);
    465 }
    466 
    467 PositionWithAffinity RenderReplaced::positionForPoint(const LayoutPoint& point)
    468 {
    469     // FIXME: This code is buggy if the replaced element is relative positioned.
    470     InlineBox* box = inlineBoxWrapper();
    471     RootInlineBox* rootBox = box ? box->root() : 0;
    472 
    473     LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
    474     LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
    475 
    476     LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
    477     LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
    478 
    479     if (blockDirectionPosition < top)
    480         return createPositionWithAffinity(caretMinOffset(), DOWNSTREAM); // coordinates are above
    481 
    482     if (blockDirectionPosition >= bottom)
    483         return createPositionWithAffinity(caretMaxOffset(), DOWNSTREAM); // coordinates are below
    484 
    485     if (node()) {
    486         if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
    487             return createPositionWithAffinity(0, DOWNSTREAM);
    488         return createPositionWithAffinity(1, DOWNSTREAM);
    489     }
    490 
    491     return RenderBox::positionForPoint(point);
    492 }
    493 
    494 LayoutRect RenderReplaced::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
    495 {
    496     ASSERT(!needsLayout());
    497 
    498     if (!isSelected())
    499         return LayoutRect();
    500 
    501     LayoutRect rect = localSelectionRect();
    502     if (clipToVisibleContent)
    503         computeRectForRepaint(repaintContainer, rect);
    504     else
    505         rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
    506 
    507     return rect;
    508 }
    509 
    510 LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
    511 {
    512     if (checkWhetherSelected && !isSelected())
    513         return LayoutRect();
    514 
    515     if (!m_inlineBoxWrapper)
    516         // We're a block-level replaced element.  Just return our own dimensions.
    517         return LayoutRect(LayoutPoint(), size());
    518 
    519     RootInlineBox* root = m_inlineBoxWrapper->root();
    520     LayoutUnit newLogicalTop = root->block()->style()->isFlippedBlocksWritingMode() ? m_inlineBoxWrapper->logicalBottom() - root->selectionBottom() : root->selectionTop() - m_inlineBoxWrapper->logicalTop();
    521     if (root->block()->style()->isHorizontalWritingMode())
    522         return LayoutRect(0, newLogicalTop, width(), root->selectionHeight());
    523     return LayoutRect(newLogicalTop, 0, root->selectionHeight(), height());
    524 }
    525 
    526 void RenderReplaced::setSelectionState(SelectionState state)
    527 {
    528     // The selection state for our containing block hierarchy is updated by the base class call.
    529     RenderBox::setSelectionState(state);
    530 
    531     if (m_inlineBoxWrapper && canUpdateSelectionOnRootLineBoxes())
    532         if (RootInlineBox* root = m_inlineBoxWrapper->root())
    533             root->setHasSelectedChildren(isSelected());
    534 }
    535 
    536 bool RenderReplaced::isSelected() const
    537 {
    538     SelectionState s = selectionState();
    539     if (s == SelectionNone)
    540         return false;
    541     if (s == SelectionInside)
    542         return true;
    543 
    544     int selectionStart, selectionEnd;
    545     selectionStartEnd(selectionStart, selectionEnd);
    546     if (s == SelectionStart)
    547         return selectionStart == 0;
    548 
    549     int end = node()->hasChildNodes() ? node()->childNodeCount() : 1;
    550     if (s == SelectionEnd)
    551         return selectionEnd == end;
    552     if (s == SelectionBoth)
    553         return selectionStart == 0 && selectionEnd == end;
    554 
    555     ASSERT(0);
    556     return false;
    557 }
    558 
    559 LayoutRect RenderReplaced::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
    560 {
    561     if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
    562         return LayoutRect();
    563 
    564     // The selectionRect can project outside of the overflowRect, so take their union
    565     // for repainting to avoid selection painting glitches.
    566     LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect());
    567 
    568     RenderView* v = view();
    569     if (v) {
    570         // FIXME: layoutDelta needs to be applied in parts before/after transforms and
    571         // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
    572         r.move(v->layoutDelta());
    573     }
    574 
    575     if (style()) {
    576         if (v)
    577             r.inflate(style()->outlineSize());
    578     }
    579     computeRectForRepaint(repaintContainer, r);
    580     return r;
    581 }
    582 
    583 }
    584