1 /*
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.
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/editing/PositionWithAffinity.h"
28 #include "core/paint/BoxPainter.h"
29 #include "core/rendering/GraphicsContextAnnotator.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"
36
37 namespace blink {
38
39 const int RenderReplaced::defaultWidth = 300;
40 const int RenderReplaced::defaultHeight = 150;
41
RenderReplaced(Element * element)42 RenderReplaced::RenderReplaced(Element* element)
43 : RenderBox(element)
44 , m_intrinsicSize(defaultWidth, defaultHeight)
45 {
46 setReplaced(true);
47 }
48
RenderReplaced(Element * element,const LayoutSize & intrinsicSize)49 RenderReplaced::RenderReplaced(Element* element, const LayoutSize& intrinsicSize)
50 : RenderBox(element)
51 , m_intrinsicSize(intrinsicSize)
52 {
53 setReplaced(true);
54 }
55
~RenderReplaced()56 RenderReplaced::~RenderReplaced()
57 {
58 }
59
willBeDestroyed()60 void RenderReplaced::willBeDestroyed()
61 {
62 if (!documentBeingDestroyed() && parent())
63 parent()->dirtyLinesFromChangedChild(this);
64
65 RenderBox::willBeDestroyed();
66 }
67
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)68 void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
69 {
70 RenderBox::styleDidChange(diff, oldStyle);
71
72 bool hadStyle = (oldStyle != 0);
73 float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
74 if (style() && style()->effectiveZoom() != oldZoom)
75 intrinsicSizeChanged();
76 }
77
layout()78 void RenderReplaced::layout()
79 {
80 ASSERT(needsLayout());
81
82 LayoutRect oldContentRect = replacedContentRect();
83
84 setHeight(minimumReplacedHeight());
85
86 updateLogicalWidth();
87 updateLogicalHeight();
88
89 m_overflow.clear();
90 addVisualEffectOverflow();
91 updateLayerTransformAfterLayout();
92 invalidateBackgroundObscurationStatus();
93
94 clearNeedsLayout();
95
96 if (replacedContentRect() != oldContentRect)
97 setShouldDoFullPaintInvalidation(true);
98 }
99
intrinsicSizeChanged()100 void RenderReplaced::intrinsicSizeChanged()
101 {
102 int scaledWidth = static_cast<int>(defaultWidth * style()->effectiveZoom());
103 int scaledHeight = static_cast<int>(defaultHeight * style()->effectiveZoom());
104 m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
105 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
106 }
107
paint(PaintInfo & paintInfo,const LayoutPoint & paintOffset)108 void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
109 {
110 ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
111
112 if (!shouldPaint(paintInfo, paintOffset))
113 return;
114
115 LayoutPoint adjustedPaintOffset = paintOffset + location();
116
117 if (hasBoxDecorationBackground() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
118 paintBoxDecorationBackground(paintInfo, adjustedPaintOffset);
119
120 if (paintInfo.phase == PaintPhaseMask) {
121 paintMask(paintInfo, adjustedPaintOffset);
122 return;
123 }
124
125 if (paintInfo.phase == PaintPhaseClippingMask && (!hasLayer() || !layer()->hasCompositedClippingMask()))
126 return;
127
128 LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
129 if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth())
130 paintOutline(paintInfo, paintRect);
131
132 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && !canHaveChildren() && paintInfo.phase != PaintPhaseClippingMask)
133 return;
134
135 if (!paintInfo.shouldPaintWithinRoot(this))
136 return;
137
138 bool drawSelectionTint = selectionState() != SelectionNone && !document().printing();
139 if (paintInfo.phase == PaintPhaseSelection) {
140 if (selectionState() == SelectionNone)
141 return;
142 drawSelectionTint = false;
143 }
144
145 bool completelyClippedOut = false;
146 if (style()->hasBorderRadius()) {
147 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
148
149 if (borderRect.isEmpty())
150 completelyClippedOut = true;
151 else {
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 BoxPainter::clipRoundedInnerRect(paintInfo.context, paintRect, roundedInnerRect);
157 }
158 }
159
160 if (!completelyClippedOut) {
161 if (paintInfo.phase == PaintPhaseClippingMask) {
162 paintClippingMask(paintInfo, adjustedPaintOffset);
163 } else {
164 paintReplaced(paintInfo, adjustedPaintOffset);
165 }
166
167 if (style()->hasBorderRadius())
168 paintInfo.context->restore();
169 }
170
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());
177 }
178 }
179
shouldPaint(PaintInfo & paintInfo,const LayoutPoint & paintOffset)180 bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
181 {
182 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
183 && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask && paintInfo.phase != PaintPhaseClippingMask)
184 return false;
185
186 if (!paintInfo.shouldPaintWithinRoot(this))
187 return false;
188
189 // if we're invisible or haven't received a layout yet, then just bail.
190 if (style()->visibility() != VISIBLE)
191 return false;
192
193 LayoutPoint adjustedPaintOffset = paintOffset + location();
194
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 = std::min(selTop, top);
202 bottom = std::max(selBottom, bottom);
203 }
204
205 if (adjustedPaintOffset.x() + visualOverflowRect().x() >= paintInfo.rect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= paintInfo.rect.x())
206 return false;
207
208 if (top >= paintInfo.rect.maxY() || bottom <= paintInfo.rect.y())
209 return false;
210
211 return true;
212 }
213
hasReplacedLogicalHeight() const214 bool RenderReplaced::hasReplacedLogicalHeight() const
215 {
216 if (style()->logicalHeight().isAuto())
217 return false;
218
219 if (style()->logicalHeight().isSpecified()) {
220 if (hasAutoHeightOrContainingBlockWithAutoHeight())
221 return false;
222 return true;
223 }
224
225 if (style()->logicalHeight().isIntrinsic())
226 return true;
227
228 return false;
229 }
230
needsPreferredWidthsRecalculation() const231 bool RenderReplaced::needsPreferredWidthsRecalculation() const
232 {
233 // If the height is a percentage and the width is auto, then the containingBlocks's height changing can cause
234 // this node to change it's preferred width because it maintains aspect ratio.
235 return hasRelativeLogicalHeight() && style()->logicalWidth().isAuto() && !hasAutoHeightOrContainingBlockWithAutoHeight();
236 }
237
rendererHasAspectRatio(const RenderObject * renderer)238 static inline bool rendererHasAspectRatio(const RenderObject* renderer)
239 {
240 ASSERT(renderer);
241 return renderer->isImage() || renderer->isCanvas() || renderer->isVideo();
242 }
243
computeAspectRatioInformationForRenderBox(RenderBox * contentRenderer,FloatSize & constrainedSize,double & intrinsicRatio) const244 void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio) const
245 {
246 FloatSize intrinsicSize;
247 if (contentRenderer) {
248 contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
249
250 // Handle zoom & vertical writing modes here, as the embedded document doesn't know about them.
251 intrinsicSize.scale(style()->effectiveZoom());
252 if (isRenderImage())
253 intrinsicSize.scale(toRenderImage(this)->imageDevicePixelRatio());
254
255 // Update our intrinsic size to match what the content renderer has computed, so that when we
256 // constrain the size below, the correct intrinsic size will be obtained for comparison against
257 // min and max widths.
258 if (intrinsicRatio && !intrinsicSize.isEmpty())
259 m_intrinsicSize = LayoutSize(intrinsicSize);
260
261 if (!isHorizontalWritingMode()) {
262 if (intrinsicRatio)
263 intrinsicRatio = 1 / intrinsicRatio;
264 intrinsicSize = intrinsicSize.transposedSize();
265 }
266 } else {
267 computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
268 if (intrinsicRatio && !intrinsicSize.isEmpty())
269 m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
270 }
271
272 // Now constrain the intrinsic size along each axis according to minimum and maximum width/heights along the
273 // opposite axis. So for example a maximum width that shrinks our width will result in the height we compute here
274 // having to shrink in order to preserve the aspect ratio. Because we compute these values independently along
275 // each axis, the final returned size may in fact not preserve the aspect ratio.
276 // FIXME: In the long term, it might be better to just return this code more to the way it used to be before this
277 // function was added, since all it has done is make the code more unclear.
278 constrainedSize = intrinsicSize;
279 if (intrinsicRatio && !intrinsicSize.isEmpty() && style()->logicalWidth().isAuto() && style()->logicalHeight().isAuto()) {
280 // We can't multiply or divide by 'intrinsicRatio' here, it breaks tests, like fast/images/zoomed-img-size.html, which
281 // can only be fixed once subpixel precision is available for things like intrinsicWidth/Height - which include zoom!
282 constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height());
283 constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width());
284 }
285 }
286
replacedContentRect(const LayoutSize * overriddenIntrinsicSize) const287 LayoutRect RenderReplaced::replacedContentRect(const LayoutSize* overriddenIntrinsicSize) const
288 {
289 LayoutRect contentRect = contentBoxRect();
290 ObjectFit objectFit = style()->objectFit();
291
292 if (objectFit == ObjectFitFill && style()->objectPosition() == RenderStyle::initialObjectPosition()) {
293 return contentRect;
294 }
295
296 LayoutSize intrinsicSize = overriddenIntrinsicSize ? *overriddenIntrinsicSize : this->intrinsicSize();
297 if (!intrinsicSize.width() || !intrinsicSize.height())
298 return contentRect;
299
300 LayoutRect finalRect = contentRect;
301 switch (objectFit) {
302 case ObjectFitContain:
303 case ObjectFitScaleDown:
304 case ObjectFitCover:
305 finalRect.setSize(finalRect.size().fitToAspectRatio(intrinsicSize, objectFit == ObjectFitCover ? AspectRatioFitGrow : AspectRatioFitShrink));
306 if (objectFit != ObjectFitScaleDown || finalRect.width() <= intrinsicSize.width())
307 break;
308 // fall through
309 case ObjectFitNone:
310 finalRect.setSize(intrinsicSize);
311 break;
312 case ObjectFitFill:
313 break;
314 default:
315 ASSERT_NOT_REACHED();
316 }
317
318 LayoutUnit xOffset = minimumValueForLength(style()->objectPosition().x(), contentRect.width() - finalRect.width());
319 LayoutUnit yOffset = minimumValueForLength(style()->objectPosition().y(), contentRect.height() - finalRect.height());
320 finalRect.move(xOffset, yOffset);
321
322 return finalRect;
323 }
324
computeIntrinsicRatioInformation(FloatSize & intrinsicSize,double & intrinsicRatio) const325 void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
326 {
327 // If there's an embeddedContentBox() of a remote, referenced document available, this code-path should never be used.
328 ASSERT(!embeddedContentBox());
329 intrinsicSize = FloatSize(intrinsicLogicalWidth().toFloat(), intrinsicLogicalHeight().toFloat());
330
331 // Figure out if we need to compute an intrinsic ratio.
332 if (intrinsicSize.isEmpty() || !rendererHasAspectRatio(this))
333 return;
334
335 intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
336 }
337
computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const338 LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
339 {
340 if (style()->logicalWidth().isSpecified() || style()->logicalWidth().isIntrinsic())
341 return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style()->logicalWidth()), shouldComputePreferred);
342
343 RenderBox* contentRenderer = embeddedContentBox();
344
345 // 10.3.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width
346 double intrinsicRatio = 0;
347 FloatSize constrainedSize;
348 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
349
350 if (style()->logicalWidth().isAuto()) {
351 bool computedHeightIsAuto = hasAutoHeightOrContainingBlockWithAutoHeight();
352 bool hasIntrinsicWidth = constrainedSize.width() > 0;
353
354 // 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'.
355 if (computedHeightIsAuto && hasIntrinsicWidth)
356 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
357
358 bool hasIntrinsicHeight = constrainedSize.height() > 0;
359 if (intrinsicRatio) {
360 // 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;
361 // 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
362 // of 'width' is: (used height) * (intrinsic ratio)
363 if (intrinsicRatio && ((computedHeightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !computedHeightIsAuto)) {
364 LayoutUnit logicalHeight = computeReplacedLogicalHeight();
365 return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
366 }
367
368 // 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
369 // '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
370 // the used value of 'width' is calculated from the constraint equation used for block-level, non-replaced elements in normal flow.
371 if (computedHeightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) {
372 if (shouldComputePreferred == ComputePreferred)
373 return 0;
374 // The aforementioned 'constraint equation' used for block-level, non-replaced elements in normal flow:
375 // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
376 LayoutUnit logicalWidth = containingBlock()->availableLogicalWidth();
377
378 // This solves above equation for 'width' (== logicalWidth).
379 LayoutUnit marginStart = minimumValueForLength(style()->marginStart(), logicalWidth);
380 LayoutUnit marginEnd = minimumValueForLength(style()->marginEnd(), logicalWidth);
381 logicalWidth = std::max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
382 return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
383 }
384 }
385
386 // 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'.
387 if (hasIntrinsicWidth)
388 return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
389
390 // 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
391 // 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.
392 // Note: We fall through and instead return intrinsicLogicalWidth() here - to preserve existing WebKit behavior, which might or might not be correct, or desired.
393 // 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)
394 // has no intrinsic size, which is wrong per CSS 2.1, but matches our behavior since a long time.
395 }
396
397 return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred);
398 }
399
computeReplacedLogicalHeight() const400 LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
401 {
402 // 10.5 Content height: the 'height' property: http://www.w3.org/TR/CSS21/visudet.html#propdef-height
403 if (hasReplacedLogicalHeight())
404 return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style()->logicalHeight()));
405
406 RenderBox* contentRenderer = embeddedContentBox();
407
408 // 10.6.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height
409 double intrinsicRatio = 0;
410 FloatSize constrainedSize;
411 computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
412
413 bool widthIsAuto = style()->logicalWidth().isAuto();
414 bool hasIntrinsicHeight = constrainedSize.height() > 0;
415
416 // 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'.
417 if (widthIsAuto && hasIntrinsicHeight)
418 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
419
420 // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
421 // (used width) / (intrinsic ratio)
422 if (intrinsicRatio)
423 return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
424
425 // 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'.
426 if (hasIntrinsicHeight)
427 return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
428
429 // 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
430 // 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.
431 return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
432 }
433
computeIntrinsicLogicalWidths(LayoutUnit & minLogicalWidth,LayoutUnit & maxLogicalWidth) const434 void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
435 {
436 minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth();
437 }
438
computePreferredLogicalWidths()439 void RenderReplaced::computePreferredLogicalWidths()
440 {
441 ASSERT(preferredLogicalWidthsDirty());
442
443 // We cannot resolve any percent logical width here as the available logical
444 // width may not be set on our containing block.
445 if (style()->logicalWidth().isPercent())
446 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
447 else
448 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
449
450 RenderStyle* styleToUse = style();
451 if (styleToUse->logicalWidth().isPercent() || styleToUse->logicalMaxWidth().isPercent())
452 m_minPreferredLogicalWidth = 0;
453
454 if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
455 m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
456 m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
457 }
458
459 if (styleToUse->logicalMaxWidth().isFixed()) {
460 m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
461 m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
462 }
463
464 LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
465 m_minPreferredLogicalWidth += borderAndPadding;
466 m_maxPreferredLogicalWidth += borderAndPadding;
467
468 clearPreferredLogicalWidthsDirty();
469 }
470
positionForPoint(const LayoutPoint & point)471 PositionWithAffinity RenderReplaced::positionForPoint(const LayoutPoint& point)
472 {
473 // FIXME: This code is buggy if the replaced element is relative positioned.
474 InlineBox* box = inlineBoxWrapper();
475 RootInlineBox* rootBox = box ? &box->root() : 0;
476
477 LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
478 LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
479
480 LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
481 LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
482
483 if (blockDirectionPosition < top)
484 return createPositionWithAffinity(caretMinOffset(), DOWNSTREAM); // coordinates are above
485
486 if (blockDirectionPosition >= bottom)
487 return createPositionWithAffinity(caretMaxOffset(), DOWNSTREAM); // coordinates are below
488
489 if (node()) {
490 if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
491 return createPositionWithAffinity(0, DOWNSTREAM);
492 return createPositionWithAffinity(1, DOWNSTREAM);
493 }
494
495 return RenderBox::positionForPoint(point);
496 }
497
selectionRectForPaintInvalidation(const RenderLayerModelObject * paintInvalidationContainer) const498 LayoutRect RenderReplaced::selectionRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const
499 {
500 ASSERT(!needsLayout());
501
502 if (!isSelected())
503 return LayoutRect();
504
505 LayoutRect rect = localSelectionRect();
506 mapRectToPaintInvalidationBacking(paintInvalidationContainer, rect, 0);
507 return rect;
508 }
509
localSelectionRect(bool checkWhetherSelected) const510 LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
511 {
512 if (checkWhetherSelected && !isSelected())
513 return LayoutRect();
514
515 if (!inlineBoxWrapper())
516 // We're a block-level replaced element. Just return our own dimensions.
517 return LayoutRect(LayoutPoint(), size());
518
519 RootInlineBox& root = inlineBoxWrapper()->root();
520 LayoutUnit newLogicalTop = root.block().style()->isFlippedBlocksWritingMode() ? inlineBoxWrapper()->logicalBottom() - root.selectionBottom() : root.selectionTop() - 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
setSelectionState(SelectionState state)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 (!inlineBoxWrapper())
532 return;
533
534 // We only include the space below the baseline in our layer's cached paint invalidation rect if the
535 // image is selected. Since the selection state has changed update the rect.
536 if (hasLayer())
537 setPreviousPaintInvalidationRect(boundsRectForPaintInvalidation(containerForPaintInvalidation()));
538
539 if (canUpdateSelectionOnRootLineBoxes())
540 inlineBoxWrapper()->root().setHasSelectedChildren(isSelected());
541 }
542
isSelected() const543 bool RenderReplaced::isSelected() const
544 {
545 SelectionState s = selectionState();
546 if (s == SelectionNone)
547 return false;
548 if (s == SelectionInside)
549 return true;
550
551 int selectionStart, selectionEnd;
552 selectionStartEnd(selectionStart, selectionEnd);
553 if (s == SelectionStart)
554 return selectionStart == 0;
555
556 int end = node()->hasChildren() ? node()->countChildren() : 1;
557 if (s == SelectionEnd)
558 return selectionEnd == end;
559 if (s == SelectionBoth)
560 return selectionStart == 0 && selectionEnd == end;
561
562 ASSERT(0);
563 return false;
564 }
clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject * paintInvalidationContainer,const PaintInvalidationState * paintInvalidationState) const565 LayoutRect RenderReplaced::clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* paintInvalidationState) const
566 {
567 if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
568 return LayoutRect();
569
570 // The selectionRect can project outside of the overflowRect, so take their union
571 // for paint invalidation to avoid selection painting glitches.
572 LayoutRect r = isSelected() ? localSelectionRect() : visualOverflowRect();
573 mapRectToPaintInvalidationBacking(paintInvalidationContainer, r, paintInvalidationState);
574 return r;
575 }
576
577 }
578