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 "core/rendering/RenderReplaced.h"
27 #include "RuntimeEnabledFeatures.h"
28 #include "core/rendering/GraphicsContextAnnotator.h"
29 #include "core/rendering/LayoutRepainter.h"
30 #include "core/rendering/RenderBlock.h"
31 #include "core/rendering/RenderImage.h"
32 #include "core/rendering/RenderLayer.h"
33 #include "core/rendering/RenderView.h"
34 #include "platform/LengthFunctions.h"
35 #include "platform/graphics/GraphicsContext.h"
41 const int cDefaultWidth = 300;
42 const int cDefaultHeight = 150;
44 RenderReplaced::RenderReplaced(Element* element)
46 , m_intrinsicSize(cDefaultWidth, cDefaultHeight)
51 RenderReplaced::RenderReplaced(Element* element, const LayoutSize& intrinsicSize)
53 , m_intrinsicSize(intrinsicSize)
58 RenderReplaced::~RenderReplaced()
62 void RenderReplaced::willBeDestroyed()
64 if (!documentBeingDestroyed() && parent())
65 parent()->dirtyLinesFromChangedChild(this);
67 RenderBox::willBeDestroyed();
70 void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
72 RenderBox::styleDidChange(diff, oldStyle);
74 bool hadStyle = (oldStyle != 0);
75 float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
76 if (style() && style()->effectiveZoom() != oldZoom)
77 intrinsicSizeChanged();
80 void RenderReplaced::layout()
82 ASSERT(needsLayout());
84 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
86 setHeight(minimumReplacedHeight());
89 updateLogicalHeight();
92 addVisualEffectOverflow();
93 updateLayerTransform();
94 invalidateBackgroundObscurationStatus();
96 repainter.repaintAfterLayout();
100 void RenderReplaced::intrinsicSizeChanged()
102 int scaledWidth = static_cast<int>(cDefaultWidth * style()->effectiveZoom());
103 int scaledHeight = static_cast<int>(cDefaultHeight * style()->effectiveZoom());
104 m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
105 setNeedsLayoutAndPrefWidthsRecalc();
108 void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
110 ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
112 if (!shouldPaint(paintInfo, paintOffset))
115 LayoutPoint adjustedPaintOffset = paintOffset + location();
117 if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
118 paintBoxDecorations(paintInfo, adjustedPaintOffset);
120 if (paintInfo.phase == PaintPhaseMask) {
121 paintMask(paintInfo, adjustedPaintOffset);
125 if (paintInfo.phase == PaintPhaseClippingMask && (!hasLayer() || !layer()->hasCompositedClippingMask()))
128 LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
129 if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth())
130 paintOutline(paintInfo, paintRect);
132 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && !canHaveChildren() && paintInfo.phase != PaintPhaseClippingMask)
135 if (!paintInfo.shouldPaintWithinRoot(this))
138 bool drawSelectionTint = selectionState() != SelectionNone && !document().printing();
139 if (paintInfo.phase == PaintPhaseSelection) {
140 if (selectionState() == SelectionNone)
142 drawSelectionTint = false;
145 bool completelyClippedOut = false;
146 if (style()->hasBorderRadius()) {
147 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
149 if (borderRect.isEmpty())
150 completelyClippedOut = true;
152 // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
153 paintInfo.context->save();
154 RoundedRect roundedInnerRect = style()->getRoundedInnerBorderFor(paintRect,
155 paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true);
156 clipRoundedInnerRect(paintInfo.context, paintRect, roundedInnerRect);
160 if (!completelyClippedOut) {
161 if (paintInfo.phase == PaintPhaseClippingMask) {
162 paintClippingMask(paintInfo, adjustedPaintOffset);
164 paintReplaced(paintInfo, adjustedPaintOffset);
167 if (style()->hasBorderRadius())
168 paintInfo.context->restore();
171 // The selection tint never gets clipped by border-radius rounding, since we want it to run right up to the edges of
172 // surrounding content.
173 if (drawSelectionTint) {
174 LayoutRect selectionPaintingRect = localSelectionRect();
175 selectionPaintingRect.moveBy(adjustedPaintOffset);
176 paintInfo.context->fillRect(pixelSnappedIntRect(selectionPaintingRect), selectionBackgroundColor());
180 bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
182 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
183 && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask && paintInfo.phase != PaintPhaseClippingMask)
186 if (!paintInfo.shouldPaintWithinRoot(this))
189 // if we're invisible or haven't received a layout yet, then just bail.
190 if (style()->visibility() != VISIBLE)
193 LayoutPoint adjustedPaintOffset = paintOffset + location();
195 // Early exit if the element touches the edges.
196 LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y();
197 LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY();
198 if (isSelected() && inlineBoxWrapper()) {
199 LayoutUnit selTop = paintOffset.y() + inlineBoxWrapper()->root().selectionTop();
200 LayoutUnit selBottom = paintOffset.y() + selTop + inlineBoxWrapper()->root().selectionHeight();
201 top = min(selTop, top);
202 bottom = max(selBottom, bottom);
205 if (adjustedPaintOffset.x() + visualOverflowRect().x() >= paintInfo.rect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= paintInfo.rect.x())
208 if (top >= paintInfo.rect.maxY() || bottom <= paintInfo.rect.y())
214 static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced)
216 // We have to lookup the containing block, which has an explicit width, which must not be equal to our direct containing block.
217 // If the embedded document appears _after_ we performed the initial layout, our intrinsic size is 300x150. If our containing
218 // block doesn't provide an explicit width, it's set to the 300 default, coming from the initial layout run.
219 RenderBlock* containingBlock = replaced->containingBlock();
220 if (!containingBlock)
223 for (; !containingBlock->isRenderView() && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) {
224 if (containingBlock->style()->logicalWidth().isSpecified()
225 && containingBlock->style()->logicalMinWidth().isSpecified()
226 && (containingBlock->style()->logicalMaxWidth().isSpecified() || containingBlock->style()->logicalMaxWidth().isUndefined()))
227 return containingBlock;
233 bool RenderReplaced::hasReplacedLogicalWidth() const
235 if (style()->logicalWidth().isSpecified())
238 if (style()->logicalWidth().isAuto())
241 return firstContainingBlockWithLogicalWidth(this);
244 bool RenderReplaced::hasReplacedLogicalHeight() const
246 if (style()->logicalHeight().isAuto())
249 if (style()->logicalHeight().isSpecified()) {
250 if (hasAutoHeightOrContainingBlockWithAutoHeight())
255 if (style()->logicalHeight().isIntrinsic())
261 bool RenderReplaced::needsPreferredWidthsRecalculation() const
263 // If the height is a percentage and the width is auto, then the containingBlocks's height changing can cause
264 // this node to change it's preferred width because it maintains aspect ratio.
265 return hasRelativeLogicalHeight() && style()->logicalWidth().isAuto() && !hasAutoHeightOrContainingBlockWithAutoHeight();
268 static inline bool rendererHasAspectRatio(const RenderObject* renderer)
271 return renderer->isImage() || renderer->isCanvas() || renderer->isVideo();
274 void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio) const
276 FloatSize intrinsicSize;
277 if (contentRenderer) {
278 contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
280 // Handle zoom & vertical writing modes here, as the embedded document doesn't know about them.
281 intrinsicSize.scale(style()->effectiveZoom());
283 intrinsicSize.scale(toRenderImage(this)->imageDevicePixelRatio());
285 // Update our intrinsic size to match what the content renderer has computed, so that when we
286 // constrain the size below, the correct intrinsic size will be obtained for comparison against
287 // min and max widths.
288 if (intrinsicRatio && !intrinsicSize.isEmpty())
289 m_intrinsicSize = LayoutSize(intrinsicSize);
291 if (!isHorizontalWritingMode()) {
293 intrinsicRatio = 1 / intrinsicRatio;
294 intrinsicSize = intrinsicSize.transposedSize();
297 computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
298 if (intrinsicRatio && !intrinsicSize.isEmpty())
299 m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
302 // Now constrain the intrinsic size along each axis according to minimum and maximum width/heights along the
303 // opposite axis. So for example a maximum width that shrinks our width will result in the height we compute here
304 // having to shrink in order to preserve the aspect ratio. Because we compute these values independently along
305 // each axis, the final returned size may in fact not preserve the aspect ratio.
306 // FIXME: In the long term, it might be better to just return this code more to the way it used to be before this
307 // function was added, since all it has done is make the code more unclear.
308 constrainedSize = intrinsicSize;
309 if (intrinsicRatio && !intrinsicSize.isEmpty() && style()->logicalWidth().isAuto() && style()->logicalHeight().isAuto()) {
310 // We can't multiply or divide by 'intrinsicRatio' here, it breaks tests, like fast/images/zoomed-img-size.html, which
311 // can only be fixed once subpixel precision is available for things like intrinsicWidth/Height - which include zoom!
312 constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height());
313 constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width());
317 LayoutRect RenderReplaced::replacedContentRect(const LayoutSize* overriddenIntrinsicSize) const
319 LayoutRect contentRect = contentBoxRect();
320 ObjectFit objectFit = style()->objectFit();
322 if (objectFit == ObjectFitFill && style()->objectPosition() == RenderStyle::initialObjectPosition()) {
323 if (!isVideo() || RuntimeEnabledFeatures::objectFitPositionEnabled())
325 objectFit = ObjectFitContain;
328 LayoutSize intrinsicSize = overriddenIntrinsicSize ? *overriddenIntrinsicSize : this->intrinsicSize();
329 if (!intrinsicSize.width() || !intrinsicSize.height())
332 LayoutRect finalRect = contentRect;
334 case ObjectFitContain:
335 case ObjectFitScaleDown:
337 finalRect.setSize(finalRect.size().fitToAspectRatio(intrinsicSize, objectFit == ObjectFitCover ? AspectRatioFitGrow : AspectRatioFitShrink));
338 if (objectFit != ObjectFitScaleDown || finalRect.width() <= intrinsicSize.width())
342 finalRect.setSize(intrinsicSize);
347 ASSERT_NOT_REACHED();
350 LayoutUnit xOffset = minimumValueForLength(style()->objectPosition().x(), contentRect.width() - finalRect.width());
351 LayoutUnit yOffset = minimumValueForLength(style()->objectPosition().y(), contentRect.height() - finalRect.height());
352 finalRect.move(xOffset, yOffset);
357 void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
359 // If there's an embeddedContentBox() of a remote, referenced document available, this code-path should never be used.
360 ASSERT(!embeddedContentBox());
361 intrinsicSize = FloatSize(intrinsicLogicalWidth().toFloat(), intrinsicLogicalHeight().toFloat());
363 // Figure out if we need to compute an intrinsic ratio.
364 if (intrinsicSize.isEmpty() || !rendererHasAspectRatio(this))
367 intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
370 LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
372 if (style()->logicalWidth().isSpecified() || style()->logicalWidth().isIntrinsic())
373 return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style()->logicalWidth()), shouldComputePreferred);
375 RenderBox* contentRenderer = embeddedContentBox();
377 // 10.3.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width
378 double intrinsicRatio = 0;
379 FloatSize constrainedSize;
380 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
382 if (style()->logicalWidth().isAuto()) {
383 bool computedHeightIsAuto = hasAutoHeightOrContainingBlockWithAutoHeight();
384 bool hasIntrinsicWidth = constrainedSize.width() > 0;
386 // 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'.
387 if (computedHeightIsAuto && hasIntrinsicWidth)
388 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
390 bool hasIntrinsicHeight = constrainedSize.height() > 0;
391 if (intrinsicRatio) {
392 // 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;
393 // 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
394 // of 'width' is: (used height) * (intrinsic ratio)
395 if (intrinsicRatio && ((computedHeightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !computedHeightIsAuto)) {
396 LayoutUnit logicalHeight = computeReplacedLogicalHeight();
397 return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
400 // 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
401 // '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
402 // the used value of 'width' is calculated from the constraint equation used for block-level, non-replaced elements in normal flow.
403 if (computedHeightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) {
404 // The aforementioned 'constraint equation' used for block-level, non-replaced elements in normal flow:
405 // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
406 LayoutUnit logicalWidth;
407 // FIXME: This walking up the containgBlock chain to find the first one with a specified width is bonkers.
408 // If nothing else, it requires making sure that computeReplacedLogicalWidthRespectingMinMaxWidth cannot
409 // depend on the width of the replaced element or we infinite loop. Right now we do that in
410 // firstContainingBlockWithLogicalWidth by checking that width/min-width/max-width are all specified.
412 // Firefox 27 seems to only do this if the <svg> has a viewbox.
413 if (RenderBlock* blockWithWidth = firstContainingBlockWithLogicalWidth(this)) {
414 logicalWidth = blockWithWidth->computeReplacedLogicalWidthRespectingMinMaxWidth(blockWithWidth->computeReplacedLogicalWidthUsing(blockWithWidth->style()->logicalWidth()), shouldComputePreferred);
416 // FIXME: If shouldComputePreferred == ComputePreferred, then we're reading this during preferred width
417 // computation, at which point this is reading stale data from a previous layout.
418 logicalWidth = containingBlock()->availableLogicalWidth();
421 // This solves above equation for 'width' (== logicalWidth).
422 LayoutUnit marginStart = minimumValueForLength(style()->marginStart(), logicalWidth);
423 LayoutUnit marginEnd = minimumValueForLength(style()->marginEnd(), logicalWidth);
424 logicalWidth = max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
425 return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
429 // 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'.
430 if (hasIntrinsicWidth)
431 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
433 // 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
434 // 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.
435 // Note: We fall through and instead return intrinsicLogicalWidth() here - to preserve existing WebKit behavior, which might or might not be correct, or desired.
436 // 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)
437 // has no intrinsic size, which is wrong per CSS 2.1, but matches our behavior since a long time.
440 return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred);
443 LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
445 // 10.5 Content height: the 'height' property: http://www.w3.org/TR/CSS21/visudet.html#propdef-height
446 if (hasReplacedLogicalHeight())
447 return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style()->logicalHeight()));
449 RenderBox* contentRenderer = embeddedContentBox();
451 // 10.6.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height
452 double intrinsicRatio = 0;
453 FloatSize constrainedSize;
454 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
456 bool widthIsAuto = style()->logicalWidth().isAuto();
457 bool hasIntrinsicHeight = constrainedSize.height() > 0;
459 // 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'.
460 if (widthIsAuto && hasIntrinsicHeight)
461 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
463 // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
464 // (used width) / (intrinsic ratio)
466 return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
468 // 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'.
469 if (hasIntrinsicHeight)
470 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
472 // 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
473 // 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.
474 return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
477 void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
479 minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth();
482 void RenderReplaced::computePreferredLogicalWidths()
484 ASSERT(preferredLogicalWidthsDirty());
486 // We cannot resolve any percent logical width here as the available logical
487 // width may not be set on our containing block.
488 if (style()->logicalWidth().isPercent())
489 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
491 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
493 RenderStyle* styleToUse = style();
494 if (styleToUse->logicalWidth().isPercent() || styleToUse->logicalMaxWidth().isPercent())
495 m_minPreferredLogicalWidth = 0;
497 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
498 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
499 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
502 if (styleToUse->logicalMaxWidth().isFixed()) {
503 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
504 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
507 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
508 m_minPreferredLogicalWidth += borderAndPadding;
509 m_maxPreferredLogicalWidth += borderAndPadding;
511 clearPreferredLogicalWidthsDirty();
514 PositionWithAffinity RenderReplaced::positionForPoint(const LayoutPoint& point)
516 // FIXME: This code is buggy if the replaced element is relative positioned.
517 InlineBox* box = inlineBoxWrapper();
518 RootInlineBox* rootBox = box ? &box->root() : 0;
520 LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
521 LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
523 LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
524 LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
526 if (blockDirectionPosition < top)
527 return createPositionWithAffinity(caretMinOffset(), DOWNSTREAM); // coordinates are above
529 if (blockDirectionPosition >= bottom)
530 return createPositionWithAffinity(caretMaxOffset(), DOWNSTREAM); // coordinates are below
533 if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
534 return createPositionWithAffinity(0, DOWNSTREAM);
535 return createPositionWithAffinity(1, DOWNSTREAM);
538 return RenderBox::positionForPoint(point);
541 LayoutRect RenderReplaced::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
543 ASSERT(!needsLayout());
548 LayoutRect rect = localSelectionRect();
549 if (clipToVisibleContent)
550 computeRectForRepaint(repaintContainer, rect);
552 rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
557 LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
559 if (checkWhetherSelected && !isSelected())
562 if (!inlineBoxWrapper())
563 // We're a block-level replaced element. Just return our own dimensions.
564 return LayoutRect(LayoutPoint(), size());
566 RootInlineBox& root = inlineBoxWrapper()->root();
567 LayoutUnit newLogicalTop = root.block().style()->isFlippedBlocksWritingMode() ? inlineBoxWrapper()->logicalBottom() - root.selectionBottom() : root.selectionTop() - inlineBoxWrapper()->logicalTop();
568 if (root.block().style()->isHorizontalWritingMode())
569 return LayoutRect(0, newLogicalTop, width(), root.selectionHeight());
570 return LayoutRect(newLogicalTop, 0, root.selectionHeight(), height());
573 void RenderReplaced::setSelectionState(SelectionState state)
575 // The selection state for our containing block hierarchy is updated by the base class call.
576 RenderBox::setSelectionState(state);
578 if (inlineBoxWrapper() && canUpdateSelectionOnRootLineBoxes())
579 inlineBoxWrapper()->root().setHasSelectedChildren(isSelected());
582 bool RenderReplaced::isSelected() const
584 SelectionState s = selectionState();
585 if (s == SelectionNone)
587 if (s == SelectionInside)
590 int selectionStart, selectionEnd;
591 selectionStartEnd(selectionStart, selectionEnd);
592 if (s == SelectionStart)
593 return selectionStart == 0;
595 int end = node()->hasChildren() ? node()->countChildren() : 1;
596 if (s == SelectionEnd)
597 return selectionEnd == end;
598 if (s == SelectionBoth)
599 return selectionStart == 0 && selectionEnd == end;
605 LayoutRect RenderReplaced::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
607 if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
610 // The selectionRect can project outside of the overflowRect, so take their union
611 // for repainting to avoid selection painting glitches.
612 LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect());
614 RenderView* v = view();
615 if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled() && v) {
616 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
617 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
618 r.move(v->layoutDelta());
621 computeRectForRepaint(repaintContainer, r);