2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2000 Dirk Mueller (mueller@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.
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.
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.
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.
25 #include "RenderReplaced.h"
27 #include "FloatRoundedRect.h"
29 #include "GraphicsContext.h"
30 #include "HTMLElement.h"
31 #include "InlineElementBox.h"
32 #include "LayoutRepainter.h"
34 #include "RenderBlock.h"
35 #include "RenderFlowThread.h"
36 #include "RenderImage.h"
37 #include "RenderLayer.h"
38 #include "RenderNamedFlowFragment.h"
39 #include "RenderTheme.h"
40 #include "RenderView.h"
41 #include "VisiblePosition.h"
42 #include <wtf/StackStats.h>
46 const int cDefaultWidth = 300;
47 const int cDefaultHeight = 150;
49 RenderReplaced::RenderReplaced(Element& element, RenderStyle&& style)
50 : RenderBox(element, WTFMove(style), RenderReplacedFlag)
51 , m_intrinsicSize(cDefaultWidth, cDefaultHeight)
56 RenderReplaced::RenderReplaced(Element& element, RenderStyle&& style, const LayoutSize& intrinsicSize)
57 : RenderBox(element, WTFMove(style), RenderReplacedFlag)
58 , m_intrinsicSize(intrinsicSize)
63 RenderReplaced::RenderReplaced(Document& document, RenderStyle&& style, const LayoutSize& intrinsicSize)
64 : RenderBox(document, WTFMove(style), RenderReplacedFlag)
65 , m_intrinsicSize(intrinsicSize)
70 RenderReplaced::~RenderReplaced()
74 void RenderReplaced::willBeDestroyed()
76 if (!documentBeingDestroyed() && parent())
77 parent()->dirtyLinesFromChangedChild(*this);
79 RenderBox::willBeDestroyed();
82 void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
84 RenderBox::styleDidChange(diff, oldStyle);
86 bool hadStyle = (oldStyle != 0);
87 float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
88 if (style().effectiveZoom() != oldZoom)
89 intrinsicSizeChanged();
92 void RenderReplaced::layout()
94 StackStats::LayoutCheckPoint layoutCheckPoint;
95 ASSERT(needsLayout());
97 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
99 setHeight(minimumReplacedHeight());
101 updateLogicalWidth();
102 updateLogicalHeight();
104 // Now that we've calculated our preferred layout, we check to see
105 // if we should further constrain sizing to the intrinsic aspect ratio.
106 if (style().aspectRatioType() == AspectRatioFromIntrinsic && !m_intrinsicSize.isEmpty()) {
107 float aspectRatio = m_intrinsicSize.aspectRatio();
108 LayoutSize frameSize = size();
109 float frameAspectRatio = frameSize.aspectRatio();
110 if (frameAspectRatio < aspectRatio)
111 setHeight(computeReplacedLogicalHeightRespectingMinMaxHeight(frameSize.height() * frameAspectRatio / aspectRatio));
112 else if (frameAspectRatio > aspectRatio)
113 setWidth(computeReplacedLogicalWidthRespectingMinMaxWidth(frameSize.width() * aspectRatio / frameAspectRatio, ComputePreferred));
117 addVisualEffectOverflow();
118 updateLayerTransform();
119 invalidateBackgroundObscurationStatus();
121 repainter.repaintAfterLayout();
125 void RenderReplaced::intrinsicSizeChanged()
127 int scaledWidth = static_cast<int>(cDefaultWidth * style().effectiveZoom());
128 int scaledHeight = static_cast<int>(cDefaultHeight * style().effectiveZoom());
129 m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
130 setNeedsLayoutAndPrefWidthsRecalc();
133 bool RenderReplaced::shouldDrawSelectionTint() const
135 return selectionState() != SelectionNone && !document().printing();
138 void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
140 if (!shouldPaint(paintInfo, paintOffset))
144 SetLayoutNeededForbiddenScope scope(this);
146 LayoutPoint adjustedPaintOffset = paintOffset + location();
148 if (hasVisibleBoxDecorations() && paintInfo.phase == PaintPhaseForeground)
149 paintBoxDecorations(paintInfo, adjustedPaintOffset);
151 if (paintInfo.phase == PaintPhaseMask) {
152 paintMask(paintInfo, adjustedPaintOffset);
156 LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
157 if (paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) {
158 if (style().outlineWidth())
159 paintOutline(paintInfo, paintRect);
163 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection)
166 if (!paintInfo.shouldPaintWithinRoot(*this))
169 bool drawSelectionTint = shouldDrawSelectionTint();
170 if (paintInfo.phase == PaintPhaseSelection) {
171 if (selectionState() == SelectionNone)
173 drawSelectionTint = false;
176 bool completelyClippedOut = false;
177 if (style().hasBorderRadius()) {
178 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
180 if (borderRect.isEmpty())
181 completelyClippedOut = true;
183 // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
184 paintInfo.context().save();
185 FloatRoundedRect roundedInnerRect = FloatRoundedRect(style().getRoundedInnerBorderFor(paintRect,
186 paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true));
187 clipRoundedInnerRect(paintInfo.context(), paintRect, roundedInnerRect);
191 if (!completelyClippedOut) {
192 paintReplaced(paintInfo, adjustedPaintOffset);
194 if (style().hasBorderRadius())
195 paintInfo.context().restore();
198 // The selection tint never gets clipped by border-radius rounding, since we want it to run right up to the edges of
199 // surrounding content.
200 if (drawSelectionTint) {
201 LayoutRect selectionPaintingRect = localSelectionRect();
202 selectionPaintingRect.moveBy(adjustedPaintOffset);
203 paintInfo.context().fillRect(snappedIntRect(selectionPaintingRect), selectionBackgroundColor());
207 bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
209 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
210 && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask)
213 if (!paintInfo.shouldPaintWithinRoot(*this))
216 // if we're invisible or haven't received a layout yet, then just bail.
217 if (style().visibility() != VISIBLE)
220 RenderNamedFlowFragment* namedFlowFragment = currentRenderNamedFlowFragment();
221 // Check our region range to make sure we need to be painting in this region.
222 if (namedFlowFragment && !namedFlowFragment->flowThread()->objectShouldFragmentInFlowRegion(this, namedFlowFragment))
225 LayoutPoint adjustedPaintOffset = paintOffset + location();
227 // Early exit if the element touches the edges.
228 LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y();
229 LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY();
230 if (isSelected() && m_inlineBoxWrapper) {
231 const RootInlineBox& rootBox = m_inlineBoxWrapper->root();
232 LayoutUnit selTop = paintOffset.y() + rootBox.selectionTop();
233 LayoutUnit selBottom = paintOffset.y() + selTop + rootBox.selectionHeight();
234 top = std::min(selTop, top);
235 bottom = std::max(selBottom, bottom);
238 LayoutRect localRepaintRect = paintInfo.rect;
239 if (adjustedPaintOffset.x() + visualOverflowRect().x() >= localRepaintRect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= localRepaintRect.x())
242 if (top >= localRepaintRect.maxY() || bottom <= localRepaintRect.y())
248 static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced)
250 // We have to lookup the containing block, which has an explicit width, which must not be equal to our direct containing block.
251 // If the embedded document appears _after_ we performed the initial layout, our intrinsic size is 300x150. If our containing
252 // block doesn't provide an explicit width, it's set to the 300 default, coming from the initial layout run.
253 RenderBlock* containingBlock = replaced->containingBlock();
254 if (!containingBlock)
257 for (; containingBlock && !is<RenderView>(*containingBlock) && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) {
258 if (containingBlock->style().logicalWidth().isSpecified())
259 return containingBlock;
265 bool RenderReplaced::hasReplacedLogicalWidth() const
267 if (style().logicalWidth().isSpecified())
270 if (style().logicalWidth().isAuto())
273 return firstContainingBlockWithLogicalWidth(this);
276 bool RenderReplaced::hasReplacedLogicalHeight() const
278 if (style().logicalHeight().isAuto())
281 if (style().logicalHeight().isSpecified()) {
282 if (hasAutoHeightOrContainingBlockWithAutoHeight())
287 if (style().logicalHeight().isIntrinsic())
293 bool RenderReplaced::setNeedsLayoutIfNeededAfterIntrinsicSizeChange()
295 setPreferredLogicalWidthsDirty(true);
297 // If the actual area occupied by the image has changed and it is not constrained by style then a layout is required.
298 bool imageSizeIsConstrained = style().logicalWidth().isSpecified() && style().logicalHeight().isSpecified();
300 // FIXME: We only need to recompute the containing block's preferred size
301 // if the containing block's size depends on the image's size (i.e., the container uses shrink-to-fit sizing).
302 // There's no easy way to detect that shrink-to-fit is needed, always force a layout.
303 bool containingBlockNeedsToRecomputePreferredSize =
304 style().logicalWidth().isPercentOrCalculated()
305 || style().logicalMaxWidth().isPercentOrCalculated()
306 || style().logicalMinWidth().isPercentOrCalculated();
308 bool layoutSizeDependsOnIntrinsicSize = style().aspectRatioType() == AspectRatioFromIntrinsic;
310 if (!imageSizeIsConstrained || containingBlockNeedsToRecomputePreferredSize || layoutSizeDependsOnIntrinsicSize) {
318 void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio) const
320 FloatSize intrinsicSize;
321 if (contentRenderer) {
322 contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
324 // Handle zoom & vertical writing modes here, as the embedded document doesn't know about them.
325 intrinsicSize.scale(style().effectiveZoom());
327 if (is<RenderImage>(*this))
328 intrinsicSize.scale(downcast<RenderImage>(*this).imageDevicePixelRatio());
330 // Update our intrinsic size to match what the content renderer has computed, so that when we
331 // constrain the size below, the correct intrinsic size will be obtained for comparison against
332 // min and max widths.
333 if (intrinsicRatio && !intrinsicSize.isEmpty())
334 m_intrinsicSize = LayoutSize(intrinsicSize);
336 if (!isHorizontalWritingMode()) {
338 intrinsicRatio = 1 / intrinsicRatio;
339 intrinsicSize = intrinsicSize.transposedSize();
342 computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
343 if (intrinsicRatio && !intrinsicSize.isEmpty())
344 m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
347 // Now constrain the intrinsic size along each axis according to minimum and maximum width/heights along the
348 // opposite axis. So for example a maximum width that shrinks our width will result in the height we compute here
349 // having to shrink in order to preserve the aspect ratio. Because we compute these values independently along
350 // each axis, the final returned size may in fact not preserve the aspect ratio.
351 // FIXME: In the long term, it might be better to just return this code more to the way it used to be before this
352 // function was added, since all it has done is make the code more unclear.
353 constrainedSize = intrinsicSize;
354 if (intrinsicRatio && !intrinsicSize.isEmpty() && style().logicalWidth().isAuto() && style().logicalHeight().isAuto()) {
355 // We can't multiply or divide by 'intrinsicRatio' here, it breaks tests, like fast/images/zoomed-img-size.html, which
356 // can only be fixed once subpixel precision is available for things like intrinsicWidth/Height - which include zoom!
357 constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height());
358 constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width());
362 LayoutRect RenderReplaced::replacedContentRect(const LayoutSize& intrinsicSize) const
364 LayoutRect contentRect = contentBoxRect();
365 if (intrinsicSize.isEmpty())
368 ObjectFit objectFit = style().objectFit();
370 LayoutRect finalRect = contentRect;
372 case ObjectFitContain:
373 case ObjectFitScaleDown:
375 finalRect.setSize(finalRect.size().fitToAspectRatio(intrinsicSize, objectFit == ObjectFitCover ? AspectRatioFitGrow : AspectRatioFitShrink));
376 if (objectFit != ObjectFitScaleDown || finalRect.width() <= intrinsicSize.width())
380 finalRect.setSize(intrinsicSize);
386 LengthPoint objectPosition = style().objectPosition();
388 LayoutUnit xOffset = minimumValueForLength(objectPosition.x(), contentRect.width() - finalRect.width());
389 LayoutUnit yOffset = minimumValueForLength(objectPosition.y(), contentRect.height() - finalRect.height());
391 finalRect.move(xOffset, yOffset);
396 void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
398 // If there's an embeddedContentBox() of a remote, referenced document available, this code-path should never be used.
399 ASSERT(!embeddedContentBox());
400 intrinsicSize = FloatSize(intrinsicLogicalWidth(), intrinsicLogicalHeight());
402 // Figure out if we need to compute an intrinsic ratio.
403 if (intrinsicSize.isEmpty() || !hasAspectRatio())
406 intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
409 LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
411 if (style().logicalWidth().isSpecified() || style().logicalWidth().isIntrinsic())
412 return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(MainOrPreferredSize, style().logicalWidth()), shouldComputePreferred);
414 RenderBox* contentRenderer = embeddedContentBox();
416 // 10.3.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width
417 double intrinsicRatio = 0;
418 FloatSize constrainedSize;
419 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
421 if (style().logicalWidth().isAuto()) {
422 bool computedHeightIsAuto = hasAutoHeightOrContainingBlockWithAutoHeight();
423 bool hasIntrinsicWidth = constrainedSize.width() > 0;
425 // 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'.
426 if (computedHeightIsAuto && hasIntrinsicWidth)
427 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
429 bool hasIntrinsicHeight = constrainedSize.height() > 0;
430 if (intrinsicRatio) {
431 // 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;
432 // 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
433 // of 'width' is: (used height) * (intrinsic ratio)
434 if (intrinsicRatio && ((computedHeightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !computedHeightIsAuto)) {
435 LayoutUnit logicalHeight = computeReplacedLogicalHeight();
436 return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
439 // 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
440 // '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
441 // the used value of 'width' is calculated from the constraint equation used for block-level, non-replaced elements in normal flow.
442 if (computedHeightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) {
443 // The aforementioned 'constraint equation' used for block-level, non-replaced elements in normal flow:
444 // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
445 LayoutUnit logicalWidth;
446 if (auto* blockWithWidth = firstContainingBlockWithLogicalWidth(this)) {
447 logicalWidth = blockWithWidth->computeReplacedLogicalWidthUsing(MainOrPreferredSize, blockWithWidth->style().logicalWidth());
448 if (!blockWithWidth->style().logicalMaxWidth().isMaxContent() && !blockWithWidth->style().logicalMinWidth().isMinContent())
449 logicalWidth = blockWithWidth->computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
451 logicalWidth = containingBlock()->availableLogicalWidth();
453 // This solves above equation for 'width' (== logicalWidth).
454 LayoutUnit marginStart = minimumValueForLength(style().marginStart(), logicalWidth);
455 LayoutUnit marginEnd = minimumValueForLength(style().marginEnd(), logicalWidth);
456 logicalWidth = std::max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
457 return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
461 // 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'.
462 if (hasIntrinsicWidth)
463 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
465 // 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
466 // 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.
467 // Note: We fall through and instead return intrinsicLogicalWidth() here - to preserve existing WebKit behavior, which might or might not be correct, or desired.
468 // 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)
469 // has no intrinsic size, which is wrong per CSS 2.1, but matches our behavior since a long time.
472 return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred);
475 LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
477 // 10.5 Content height: the 'height' property: http://www.w3.org/TR/CSS21/visudet.html#propdef-height
478 if (hasReplacedLogicalHeight())
479 return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(MainOrPreferredSize, style().logicalHeight()));
481 RenderBox* contentRenderer = embeddedContentBox();
483 // 10.6.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height
484 double intrinsicRatio = 0;
485 FloatSize constrainedSize;
486 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
488 bool widthIsAuto = style().logicalWidth().isAuto();
489 bool hasIntrinsicHeight = constrainedSize.height() > 0;
491 // 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'.
492 if (widthIsAuto && hasIntrinsicHeight)
493 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
495 // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
496 // (used width) / (intrinsic ratio)
498 return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
500 // 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'.
501 if (hasIntrinsicHeight)
502 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
504 // 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
505 // 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.
506 return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
509 void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
511 minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth();
514 void RenderReplaced::computePreferredLogicalWidths()
516 ASSERT(preferredLogicalWidthsDirty());
518 // We cannot resolve any percent logical width here as the available logical
519 // width may not be set on our containing block.
520 if (style().logicalWidth().isPercentOrCalculated())
521 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
523 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
525 const RenderStyle& styleToUse = style();
526 if (styleToUse.logicalWidth().isPercentOrCalculated() || styleToUse.logicalMaxWidth().isPercentOrCalculated())
527 m_minPreferredLogicalWidth = 0;
529 if (styleToUse.logicalMinWidth().isFixed() && styleToUse.logicalMinWidth().value() > 0) {
530 m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
531 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
534 if (styleToUse.logicalMaxWidth().isFixed()) {
535 m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
536 m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
539 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
540 m_minPreferredLogicalWidth += borderAndPadding;
541 m_maxPreferredLogicalWidth += borderAndPadding;
543 setPreferredLogicalWidthsDirty(false);
546 VisiblePosition RenderReplaced::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
548 // FIXME: This code is buggy if the replaced element is relative positioned.
549 InlineBox* box = inlineBoxWrapper();
550 const RootInlineBox* rootBox = box ? &box->root() : 0;
552 LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
553 LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
555 LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
556 LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
558 if (blockDirectionPosition < top)
559 return createVisiblePosition(caretMinOffset(), DOWNSTREAM); // coordinates are above
561 if (blockDirectionPosition >= bottom)
562 return createVisiblePosition(caretMaxOffset(), DOWNSTREAM); // coordinates are below
565 if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
566 return createVisiblePosition(0, DOWNSTREAM);
567 return createVisiblePosition(1, DOWNSTREAM);
570 return RenderBox::positionForPoint(point, region);
573 LayoutRect RenderReplaced::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
575 ASSERT(!needsLayout());
580 LayoutRect rect = localSelectionRect();
581 if (clipToVisibleContent)
582 return computeRectForRepaint(rect, repaintContainer);
583 return localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
586 LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
588 if (checkWhetherSelected && !isSelected())
591 if (!m_inlineBoxWrapper)
592 // We're a block-level replaced element. Just return our own dimensions.
593 return LayoutRect(LayoutPoint(), size());
595 const RootInlineBox& rootBox = m_inlineBoxWrapper->root();
596 LayoutUnit newLogicalTop = rootBox.blockFlow().style().isFlippedBlocksWritingMode() ? m_inlineBoxWrapper->logicalBottom() - rootBox.selectionBottom() : rootBox.selectionTop() - m_inlineBoxWrapper->logicalTop();
597 if (rootBox.blockFlow().style().isHorizontalWritingMode())
598 return LayoutRect(0, newLogicalTop, width(), rootBox.selectionHeight());
599 return LayoutRect(newLogicalTop, 0, rootBox.selectionHeight(), height());
602 void RenderReplaced::setSelectionState(SelectionState state)
604 // The selection state for our containing block hierarchy is updated by the base class call.
605 RenderBox::setSelectionState(state);
607 if (m_inlineBoxWrapper && canUpdateSelectionOnRootLineBoxes())
608 m_inlineBoxWrapper->root().setHasSelectedChildren(isSelected());
611 bool RenderReplaced::isSelected() const
613 SelectionState s = selectionState();
614 if (s == SelectionNone)
616 if (s == SelectionInside)
619 unsigned selectionStart, selectionEnd;
620 selectionStartEnd(selectionStart, selectionEnd);
621 if (s == SelectionStart)
622 return selectionStart == 0;
624 unsigned end = element()->hasChildNodes() ? element()->countChildNodes() : 1;
625 if (s == SelectionEnd)
626 return selectionEnd == end;
627 if (s == SelectionBoth)
628 return selectionStart == 0 && selectionEnd == end;
630 ASSERT_NOT_REACHED();
634 LayoutRect RenderReplaced::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
636 if (style().visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
639 // The selectionRect can project outside of the overflowRect, so take their union
640 // for repainting to avoid selection painting glitches.
641 LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect());
642 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
643 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
644 r.move(view().layoutDelta());
645 return computeRectForRepaint(r, repaintContainer);