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 "RenderReplaced.h" |
26 | |
27 | #include "DocumentMarkerController.h" |
28 | #include "FloatRoundedRect.h" |
29 | #include "Frame.h" |
30 | #include "GraphicsContext.h" |
31 | #include "HTMLElement.h" |
32 | #include "InlineElementBox.h" |
33 | #include "LayoutRepainter.h" |
34 | #include "RenderBlock.h" |
35 | #include "RenderFragmentedFlow.h" |
36 | #include "RenderImage.h" |
37 | #include "RenderLayer.h" |
38 | #include "RenderTheme.h" |
39 | #include "RenderView.h" |
40 | #include "RenderedDocumentMarker.h" |
41 | #include "VisiblePosition.h" |
42 | #include <wtf/IsoMallocInlines.h> |
43 | #include <wtf/StackStats.h> |
44 | |
45 | namespace WebCore { |
46 | |
47 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderReplaced); |
48 | |
49 | const int cDefaultWidth = 300; |
50 | const int cDefaultHeight = 150; |
51 | |
52 | RenderReplaced::RenderReplaced(Element& element, RenderStyle&& style) |
53 | : RenderBox(element, WTFMove(style), RenderReplacedFlag) |
54 | , m_intrinsicSize(cDefaultWidth, cDefaultHeight) |
55 | { |
56 | setReplaced(true); |
57 | } |
58 | |
59 | RenderReplaced::RenderReplaced(Element& element, RenderStyle&& style, const LayoutSize& intrinsicSize) |
60 | : RenderBox(element, WTFMove(style), RenderReplacedFlag) |
61 | , m_intrinsicSize(intrinsicSize) |
62 | { |
63 | setReplaced(true); |
64 | } |
65 | |
66 | RenderReplaced::RenderReplaced(Document& document, RenderStyle&& style, const LayoutSize& intrinsicSize) |
67 | : RenderBox(document, WTFMove(style), RenderReplacedFlag) |
68 | , m_intrinsicSize(intrinsicSize) |
69 | { |
70 | setReplaced(true); |
71 | } |
72 | |
73 | RenderReplaced::~RenderReplaced() = default; |
74 | |
75 | void RenderReplaced::willBeDestroyed() |
76 | { |
77 | if (!renderTreeBeingDestroyed() && parent()) |
78 | parent()->dirtyLinesFromChangedChild(*this); |
79 | |
80 | RenderBox::willBeDestroyed(); |
81 | } |
82 | |
83 | void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) |
84 | { |
85 | RenderBox::styleDidChange(diff, oldStyle); |
86 | |
87 | bool hadStyle = (oldStyle != 0); |
88 | float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom(); |
89 | if (style().effectiveZoom() != oldZoom) |
90 | intrinsicSizeChanged(); |
91 | } |
92 | |
93 | void RenderReplaced::layout() |
94 | { |
95 | StackStats::LayoutCheckPoint layoutCheckPoint; |
96 | ASSERT(needsLayout()); |
97 | |
98 | LayoutRepainter repainter(*this, checkForRepaintDuringLayout()); |
99 | |
100 | setHeight(minimumReplacedHeight()); |
101 | |
102 | updateLogicalWidth(); |
103 | updateLogicalHeight(); |
104 | |
105 | // Now that we've calculated our preferred layout, we check to see |
106 | // if we should further constrain sizing to the intrinsic aspect ratio. |
107 | if (style().aspectRatioType() == AspectRatioType::FromIntrinsic && !m_intrinsicSize.isEmpty()) { |
108 | float aspectRatio = m_intrinsicSize.aspectRatio(); |
109 | LayoutSize frameSize = size(); |
110 | float frameAspectRatio = frameSize.aspectRatio(); |
111 | if (frameAspectRatio < aspectRatio) |
112 | setHeight(computeReplacedLogicalHeightRespectingMinMaxHeight(frameSize.height() * frameAspectRatio / aspectRatio)); |
113 | else if (frameAspectRatio > aspectRatio) |
114 | setWidth(computeReplacedLogicalWidthRespectingMinMaxWidth(frameSize.width() * aspectRatio / frameAspectRatio, ComputePreferred)); |
115 | } |
116 | |
117 | clearOverflow(); |
118 | addVisualEffectOverflow(); |
119 | updateLayerTransform(); |
120 | invalidateBackgroundObscurationStatus(); |
121 | |
122 | repainter.repaintAfterLayout(); |
123 | clearNeedsLayout(); |
124 | } |
125 | |
126 | void RenderReplaced::intrinsicSizeChanged() |
127 | { |
128 | int scaledWidth = static_cast<int>(cDefaultWidth * style().effectiveZoom()); |
129 | int scaledHeight = static_cast<int>(cDefaultHeight * style().effectiveZoom()); |
130 | m_intrinsicSize = IntSize(scaledWidth, scaledHeight); |
131 | setNeedsLayoutAndPrefWidthsRecalc(); |
132 | } |
133 | |
134 | bool RenderReplaced::shouldDrawSelectionTint() const |
135 | { |
136 | return selectionState() != SelectionNone && !document().printing(); |
137 | } |
138 | |
139 | inline static bool draggedContentContainsReplacedElement(const Vector<RenderedDocumentMarker*>& markers, const Element& element) |
140 | { |
141 | if (markers.isEmpty()) |
142 | return false; |
143 | |
144 | for (auto* marker : markers) { |
145 | auto& draggedContentData = WTF::get<DocumentMarker::DraggedContentData>(marker->data()); |
146 | if (draggedContentData.targetNode == &element) |
147 | return true; |
148 | } |
149 | |
150 | return false; |
151 | } |
152 | |
153 | void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
154 | { |
155 | if (!shouldPaint(paintInfo, paintOffset)) |
156 | return; |
157 | |
158 | #ifndef NDEBUG |
159 | SetLayoutNeededForbiddenScope scope(this); |
160 | #endif |
161 | |
162 | GraphicsContextStateSaver savedGraphicsContext(paintInfo.context(), false); |
163 | if (element() && element()->parentOrShadowHostElement()) { |
164 | auto* parentContainer = element()->parentOrShadowHostElement(); |
165 | ASSERT(parentContainer); |
166 | if (draggedContentContainsReplacedElement(document().markers().markersFor(*parentContainer, DocumentMarker::DraggedContent), *element())) { |
167 | savedGraphicsContext.save(); |
168 | paintInfo.context().setAlpha(0.25); |
169 | } |
170 | } |
171 | |
172 | LayoutPoint adjustedPaintOffset = paintOffset + location(); |
173 | |
174 | if (hasVisibleBoxDecorations() && paintInfo.phase == PaintPhase::Foreground) |
175 | paintBoxDecorations(paintInfo, adjustedPaintOffset); |
176 | |
177 | if (paintInfo.phase == PaintPhase::Mask) { |
178 | paintMask(paintInfo, adjustedPaintOffset); |
179 | return; |
180 | } |
181 | |
182 | LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size()); |
183 | if (paintInfo.phase == PaintPhase::Outline || paintInfo.phase == PaintPhase::SelfOutline) { |
184 | if (style().outlineWidth()) |
185 | paintOutline(paintInfo, paintRect); |
186 | return; |
187 | } |
188 | |
189 | if (paintInfo.phase != PaintPhase::Foreground && paintInfo.phase != PaintPhase::Selection) |
190 | return; |
191 | |
192 | if (!paintInfo.shouldPaintWithinRoot(*this)) |
193 | return; |
194 | |
195 | bool drawSelectionTint = shouldDrawSelectionTint(); |
196 | if (paintInfo.phase == PaintPhase::Selection) { |
197 | if (selectionState() == SelectionNone) |
198 | return; |
199 | drawSelectionTint = false; |
200 | } |
201 | |
202 | bool completelyClippedOut = false; |
203 | if (style().hasBorderRadius()) { |
204 | LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size()); |
205 | |
206 | if (borderRect.isEmpty()) |
207 | completelyClippedOut = true; |
208 | else { |
209 | // Push a clip if we have a border radius, since we want to round the foreground content that gets painted. |
210 | paintInfo.context().save(); |
211 | FloatRoundedRect roundedInnerRect = FloatRoundedRect(style().getRoundedInnerBorderFor(paintRect, |
212 | paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true)); |
213 | clipRoundedInnerRect(paintInfo.context(), paintRect, roundedInnerRect); |
214 | } |
215 | } |
216 | |
217 | if (!completelyClippedOut) { |
218 | paintReplaced(paintInfo, adjustedPaintOffset); |
219 | |
220 | if (style().hasBorderRadius()) |
221 | paintInfo.context().restore(); |
222 | } |
223 | |
224 | // The selection tint never gets clipped by border-radius rounding, since we want it to run right up to the edges of |
225 | // surrounding content. |
226 | if (drawSelectionTint) { |
227 | LayoutRect selectionPaintingRect = localSelectionRect(); |
228 | selectionPaintingRect.moveBy(adjustedPaintOffset); |
229 | paintInfo.context().fillRect(snappedIntRect(selectionPaintingRect), selectionBackgroundColor()); |
230 | } |
231 | } |
232 | |
233 | bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
234 | { |
235 | if ((paintInfo.paintBehavior.contains(PaintBehavior::ExcludeSelection)) && isSelected()) |
236 | return false; |
237 | |
238 | if (paintInfo.phase != PaintPhase::Foreground |
239 | && paintInfo.phase != PaintPhase::Outline |
240 | && paintInfo.phase != PaintPhase::SelfOutline |
241 | && paintInfo.phase != PaintPhase::Selection |
242 | && paintInfo.phase != PaintPhase::Mask) |
243 | return false; |
244 | |
245 | if (!paintInfo.shouldPaintWithinRoot(*this)) |
246 | return false; |
247 | |
248 | // if we're invisible or haven't received a layout yet, then just bail. |
249 | if (style().visibility() != Visibility::Visible) |
250 | return false; |
251 | |
252 | LayoutPoint adjustedPaintOffset = paintOffset + location(); |
253 | |
254 | // Early exit if the element touches the edges. |
255 | LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y(); |
256 | LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY(); |
257 | if (isSelected() && m_inlineBoxWrapper) { |
258 | const RootInlineBox& rootBox = m_inlineBoxWrapper->root(); |
259 | LayoutUnit selTop = paintOffset.y() + rootBox.selectionTop(); |
260 | LayoutUnit selBottom = paintOffset.y() + selTop + rootBox.selectionHeight(); |
261 | top = std::min(selTop, top); |
262 | bottom = std::max(selBottom, bottom); |
263 | } |
264 | |
265 | LayoutRect localRepaintRect = paintInfo.rect; |
266 | if (adjustedPaintOffset.x() + visualOverflowRect().x() >= localRepaintRect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= localRepaintRect.x()) |
267 | return false; |
268 | |
269 | if (top >= localRepaintRect.maxY() || bottom <= localRepaintRect.y()) |
270 | return false; |
271 | |
272 | return true; |
273 | } |
274 | |
275 | static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced) |
276 | { |
277 | // We have to lookup the containing block, which has an explicit width, which must not be equal to our direct containing block. |
278 | // If the embedded document appears _after_ we performed the initial layout, our intrinsic size is 300x150. If our containing |
279 | // block doesn't provide an explicit width, it's set to the 300 default, coming from the initial layout run. |
280 | RenderBlock* containingBlock = replaced->containingBlock(); |
281 | if (!containingBlock) |
282 | return 0; |
283 | |
284 | for (; containingBlock && !is<RenderView>(*containingBlock) && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) { |
285 | if (containingBlock->style().logicalWidth().isSpecified()) |
286 | return containingBlock; |
287 | } |
288 | |
289 | return 0; |
290 | } |
291 | |
292 | bool RenderReplaced::hasReplacedLogicalWidth() const |
293 | { |
294 | if (style().logicalWidth().isSpecified()) |
295 | return true; |
296 | |
297 | if (style().logicalWidth().isAuto()) |
298 | return false; |
299 | |
300 | return firstContainingBlockWithLogicalWidth(this); |
301 | } |
302 | |
303 | bool RenderReplaced::hasReplacedLogicalHeight() const |
304 | { |
305 | if (style().logicalHeight().isAuto()) |
306 | return false; |
307 | |
308 | if (style().logicalHeight().isSpecified()) { |
309 | if (hasAutoHeightOrContainingBlockWithAutoHeight()) |
310 | return false; |
311 | return true; |
312 | } |
313 | |
314 | if (style().logicalHeight().isIntrinsic()) |
315 | return true; |
316 | |
317 | return false; |
318 | } |
319 | |
320 | bool RenderReplaced::setNeedsLayoutIfNeededAfterIntrinsicSizeChange() |
321 | { |
322 | setPreferredLogicalWidthsDirty(true); |
323 | |
324 | // If the actual area occupied by the image has changed and it is not constrained by style then a layout is required. |
325 | bool imageSizeIsConstrained = style().logicalWidth().isSpecified() && style().logicalHeight().isSpecified(); |
326 | |
327 | // FIXME: We only need to recompute the containing block's preferred size |
328 | // if the containing block's size depends on the image's size (i.e., the container uses shrink-to-fit sizing). |
329 | // There's no easy way to detect that shrink-to-fit is needed, always force a layout. |
330 | bool containingBlockNeedsToRecomputePreferredSize = |
331 | style().logicalWidth().isPercentOrCalculated() |
332 | || style().logicalMaxWidth().isPercentOrCalculated() |
333 | || style().logicalMinWidth().isPercentOrCalculated(); |
334 | |
335 | bool layoutSizeDependsOnIntrinsicSize = style().aspectRatioType() == AspectRatioType::FromIntrinsic; |
336 | |
337 | if (!imageSizeIsConstrained || containingBlockNeedsToRecomputePreferredSize || layoutSizeDependsOnIntrinsicSize) { |
338 | setNeedsLayout(); |
339 | return true; |
340 | } |
341 | |
342 | return false; |
343 | } |
344 | |
345 | void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio) const |
346 | { |
347 | FloatSize intrinsicSize; |
348 | if (contentRenderer) { |
349 | contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio); |
350 | |
351 | // Handle zoom & vertical writing modes here, as the embedded document doesn't know about them. |
352 | intrinsicSize.scale(style().effectiveZoom()); |
353 | |
354 | if (is<RenderImage>(*this)) |
355 | intrinsicSize.scale(downcast<RenderImage>(*this).imageDevicePixelRatio()); |
356 | |
357 | // Update our intrinsic size to match what the content renderer has computed, so that when we |
358 | // constrain the size below, the correct intrinsic size will be obtained for comparison against |
359 | // min and max widths. |
360 | if (intrinsicRatio && !intrinsicSize.isEmpty()) |
361 | m_intrinsicSize = LayoutSize(intrinsicSize); |
362 | |
363 | if (!isHorizontalWritingMode()) { |
364 | if (intrinsicRatio) |
365 | intrinsicRatio = 1 / intrinsicRatio; |
366 | intrinsicSize = intrinsicSize.transposedSize(); |
367 | } |
368 | } else { |
369 | computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio); |
370 | if (intrinsicRatio && !intrinsicSize.isEmpty()) |
371 | m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize()); |
372 | } |
373 | |
374 | // Now constrain the intrinsic size along each axis according to minimum and maximum width/heights along the |
375 | // opposite axis. So for example a maximum width that shrinks our width will result in the height we compute here |
376 | // having to shrink in order to preserve the aspect ratio. Because we compute these values independently along |
377 | // each axis, the final returned size may in fact not preserve the aspect ratio. |
378 | // FIXME: In the long term, it might be better to just return this code more to the way it used to be before this |
379 | // function was added, since all it has done is make the code more unclear. |
380 | constrainedSize = intrinsicSize; |
381 | if (intrinsicRatio && !intrinsicSize.isEmpty() && style().logicalWidth().isAuto() && style().logicalHeight().isAuto()) { |
382 | // We can't multiply or divide by 'intrinsicRatio' here, it breaks tests, like fast/images/zoomed-img-size.html, which |
383 | // can only be fixed once subpixel precision is available for things like intrinsicWidth/Height - which include zoom! |
384 | constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height()); |
385 | constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width()); |
386 | } |
387 | } |
388 | |
389 | LayoutRect RenderReplaced::replacedContentRect(const LayoutSize& intrinsicSize) const |
390 | { |
391 | LayoutRect contentRect = contentBoxRect(); |
392 | if (intrinsicSize.isEmpty()) |
393 | return contentRect; |
394 | |
395 | ObjectFit objectFit = style().objectFit(); |
396 | |
397 | LayoutRect finalRect = contentRect; |
398 | switch (objectFit) { |
399 | case ObjectFit::Contain: |
400 | case ObjectFit::ScaleDown: |
401 | case ObjectFit::Cover: |
402 | finalRect.setSize(finalRect.size().fitToAspectRatio(intrinsicSize, objectFit == ObjectFit::Cover ? AspectRatioFitGrow : AspectRatioFitShrink)); |
403 | if (objectFit != ObjectFit::ScaleDown || finalRect.width() <= intrinsicSize.width()) |
404 | break; |
405 | FALLTHROUGH; |
406 | case ObjectFit::None: |
407 | finalRect.setSize(intrinsicSize); |
408 | break; |
409 | case ObjectFit::Fill: |
410 | break; |
411 | } |
412 | |
413 | LengthPoint objectPosition = style().objectPosition(); |
414 | |
415 | LayoutUnit xOffset = minimumValueForLength(objectPosition.x(), contentRect.width() - finalRect.width()); |
416 | LayoutUnit yOffset = minimumValueForLength(objectPosition.y(), contentRect.height() - finalRect.height()); |
417 | |
418 | finalRect.move(xOffset, yOffset); |
419 | |
420 | return finalRect; |
421 | } |
422 | |
423 | void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const |
424 | { |
425 | // If there's an embeddedContentBox() of a remote, referenced document available, this code-path should never be used. |
426 | ASSERT(!embeddedContentBox()); |
427 | intrinsicSize = FloatSize(intrinsicLogicalWidth(), intrinsicLogicalHeight()); |
428 | |
429 | // Figure out if we need to compute an intrinsic ratio. |
430 | if (intrinsicSize.isEmpty() || !hasAspectRatio()) |
431 | return; |
432 | |
433 | intrinsicRatio = intrinsicSize.width() / intrinsicSize.height(); |
434 | } |
435 | |
436 | LayoutUnit RenderReplaced::computeConstrainedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const |
437 | { |
438 | if (shouldComputePreferred == ComputePreferred) |
439 | return computeReplacedLogicalWidthRespectingMinMaxWidth(0_lu, ComputePreferred); |
440 | |
441 | // The aforementioned 'constraint equation' used for block-level, non-replaced |
442 | // elements in normal flow: |
443 | // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + |
444 | // 'padding-right' + 'border-right-width' + 'margin-right' = width of |
445 | // containing block |
446 | LayoutUnit logicalWidth = containingBlock()->availableLogicalWidth(); |
447 | |
448 | // This solves above equation for 'width' (== logicalWidth). |
449 | LayoutUnit marginStart = minimumValueForLength(style().marginStart(), logicalWidth); |
450 | LayoutUnit marginEnd = minimumValueForLength(style().marginEnd(), logicalWidth); |
451 | logicalWidth = std::max(0_lu, (logicalWidth - (marginStart + marginEnd + (size().width() - clientWidth())))); |
452 | return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred); |
453 | } |
454 | |
455 | LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const |
456 | { |
457 | if (style().logicalWidth().isSpecified() || style().logicalWidth().isIntrinsic()) |
458 | return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(MainOrPreferredSize, style().logicalWidth()), shouldComputePreferred); |
459 | |
460 | RenderBox* contentRenderer = embeddedContentBox(); |
461 | |
462 | // 10.3.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width |
463 | double intrinsicRatio = 0; |
464 | FloatSize constrainedSize; |
465 | computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio); |
466 | |
467 | if (style().logicalWidth().isAuto()) { |
468 | bool computedHeightIsAuto = style().logicalHeight().isAuto(); |
469 | bool hasIntrinsicWidth = constrainedSize.width() > 0; |
470 | |
471 | // 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'. |
472 | if (computedHeightIsAuto && hasIntrinsicWidth) |
473 | return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred); |
474 | |
475 | bool hasIntrinsicHeight = constrainedSize.height() > 0; |
476 | if (intrinsicRatio) { |
477 | // 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; |
478 | // 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 |
479 | // of 'width' is: (used height) * (intrinsic ratio) |
480 | if (intrinsicRatio && ((computedHeightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !computedHeightIsAuto)) { |
481 | LayoutUnit estimatedUsedWidth = hasIntrinsicWidth ? LayoutUnit(constrainedSize.width()) : computeConstrainedLogicalWidth(shouldComputePreferred); |
482 | LayoutUnit logicalHeight = computeReplacedLogicalHeight(Optional<LayoutUnit>(estimatedUsedWidth)); |
483 | return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred); |
484 | } |
485 | |
486 | |
487 | // If 'height' and 'width' both have computed values of 'auto' and the |
488 | // element has an intrinsic ratio but no intrinsic height or width, then |
489 | // the used value of 'width' is undefined in CSS 2.1. However, it is |
490 | // suggested that, if the containing block's width does not itself depend |
491 | // on the replaced element's width, then the used value of 'width' is |
492 | // calculated from the constraint equation used for block-level, |
493 | // non-replaced elements in normal flow. |
494 | if (computedHeightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) |
495 | return computeConstrainedLogicalWidth(shouldComputePreferred); |
496 | } |
497 | |
498 | // 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'. |
499 | if (hasIntrinsicWidth) |
500 | return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred); |
501 | |
502 | // 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 |
503 | // 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. |
504 | // Note: We fall through and instead return intrinsicLogicalWidth() here - to preserve existing WebKit behavior, which might or might not be correct, or desired. |
505 | // 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) |
506 | // has no intrinsic size, which is wrong per CSS 2.1, but matches our behavior since a long time. |
507 | } |
508 | |
509 | return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred); |
510 | } |
511 | |
512 | LayoutUnit RenderReplaced::computeReplacedLogicalHeight(Optional<LayoutUnit> estimatedUsedWidth) const |
513 | { |
514 | // 10.5 Content height: the 'height' property: http://www.w3.org/TR/CSS21/visudet.html#propdef-height |
515 | if (hasReplacedLogicalHeight()) |
516 | return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(MainOrPreferredSize, style().logicalHeight())); |
517 | |
518 | RenderBox* contentRenderer = embeddedContentBox(); |
519 | |
520 | // 10.6.2 Inline, replaced elements: http://www.w3.org/TR/CSS21/visudet.html#inline-replaced-height |
521 | double intrinsicRatio = 0; |
522 | FloatSize constrainedSize; |
523 | computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio); |
524 | |
525 | bool widthIsAuto = style().logicalWidth().isAuto(); |
526 | bool hasIntrinsicHeight = constrainedSize.height() > 0; |
527 | |
528 | // 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'. |
529 | if (widthIsAuto && hasIntrinsicHeight) |
530 | return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height()); |
531 | |
532 | // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is: |
533 | // (used width) / (intrinsic ratio) |
534 | if (intrinsicRatio) { |
535 | LayoutUnit usedWidth = estimatedUsedWidth ? estimatedUsedWidth.value() : availableLogicalWidth(); |
536 | return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(usedWidth / intrinsicRatio))); |
537 | } |
538 | |
539 | // 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'. |
540 | if (hasIntrinsicHeight) |
541 | return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height()); |
542 | |
543 | // 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 |
544 | // 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. |
545 | return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight()); |
546 | } |
547 | |
548 | void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const |
549 | { |
550 | minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth(); |
551 | } |
552 | |
553 | void RenderReplaced::computePreferredLogicalWidths() |
554 | { |
555 | ASSERT(preferredLogicalWidthsDirty()); |
556 | |
557 | // We cannot resolve any percent logical width here as the available logical |
558 | // width may not be set on our containing block. |
559 | if (style().logicalWidth().isPercentOrCalculated()) |
560 | computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth); |
561 | else |
562 | m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred); |
563 | |
564 | const RenderStyle& styleToUse = style(); |
565 | if (styleToUse.logicalWidth().isPercentOrCalculated() || styleToUse.logicalMaxWidth().isPercentOrCalculated()) |
566 | m_minPreferredLogicalWidth = 0; |
567 | |
568 | if (styleToUse.logicalMinWidth().isFixed() && styleToUse.logicalMinWidth().value() > 0) { |
569 | m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value())); |
570 | m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value())); |
571 | } |
572 | |
573 | if (styleToUse.logicalMaxWidth().isFixed()) { |
574 | m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value())); |
575 | m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value())); |
576 | } |
577 | |
578 | LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth(); |
579 | m_minPreferredLogicalWidth += borderAndPadding; |
580 | m_maxPreferredLogicalWidth += borderAndPadding; |
581 | |
582 | setPreferredLogicalWidthsDirty(false); |
583 | } |
584 | |
585 | VisiblePosition RenderReplaced::positionForPoint(const LayoutPoint& point, const RenderFragmentContainer* fragment) |
586 | { |
587 | // FIXME: This code is buggy if the replaced element is relative positioned. |
588 | InlineBox* box = inlineBoxWrapper(); |
589 | const RootInlineBox* rootBox = box ? &box->root() : 0; |
590 | |
591 | LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop(); |
592 | LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom(); |
593 | |
594 | LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x(); |
595 | LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y(); |
596 | |
597 | if (blockDirectionPosition < top) |
598 | return createVisiblePosition(caretMinOffset(), DOWNSTREAM); // coordinates are above |
599 | |
600 | if (blockDirectionPosition >= bottom) |
601 | return createVisiblePosition(caretMaxOffset(), DOWNSTREAM); // coordinates are below |
602 | |
603 | if (element()) { |
604 | if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2)) |
605 | return createVisiblePosition(0, DOWNSTREAM); |
606 | return createVisiblePosition(1, DOWNSTREAM); |
607 | } |
608 | |
609 | return RenderBox::positionForPoint(point, fragment); |
610 | } |
611 | |
612 | LayoutRect RenderReplaced::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent) |
613 | { |
614 | ASSERT(!needsLayout()); |
615 | |
616 | if (!isSelected()) |
617 | return LayoutRect(); |
618 | |
619 | LayoutRect rect = localSelectionRect(); |
620 | if (clipToVisibleContent) |
621 | return computeRectForRepaint(rect, repaintContainer); |
622 | return localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox(); |
623 | } |
624 | |
625 | LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const |
626 | { |
627 | if (checkWhetherSelected && !isSelected()) |
628 | return LayoutRect(); |
629 | |
630 | if (!m_inlineBoxWrapper) |
631 | // We're a block-level replaced element. Just return our own dimensions. |
632 | return LayoutRect(LayoutPoint(), size()); |
633 | |
634 | const RootInlineBox& rootBox = m_inlineBoxWrapper->root(); |
635 | LayoutUnit newLogicalTop = rootBox.blockFlow().style().isFlippedBlocksWritingMode() ? m_inlineBoxWrapper->logicalBottom() - rootBox.selectionBottom() : rootBox.selectionTop() - m_inlineBoxWrapper->logicalTop(); |
636 | if (rootBox.blockFlow().style().isHorizontalWritingMode()) |
637 | return LayoutRect(0_lu, newLogicalTop, width(), rootBox.selectionHeight()); |
638 | return LayoutRect(newLogicalTop, 0_lu, rootBox.selectionHeight(), height()); |
639 | } |
640 | |
641 | void RenderReplaced::setSelectionState(SelectionState state) |
642 | { |
643 | // The selection state for our containing block hierarchy is updated by the base class call. |
644 | RenderBox::setSelectionState(state); |
645 | |
646 | if (m_inlineBoxWrapper && canUpdateSelectionOnRootLineBoxes()) |
647 | m_inlineBoxWrapper->root().setHasSelectedChildren(isSelected()); |
648 | } |
649 | |
650 | bool RenderReplaced::isSelected() const |
651 | { |
652 | SelectionState state = selectionState(); |
653 | if (state == SelectionNone) |
654 | return false; |
655 | if (state == SelectionInside) |
656 | return true; |
657 | |
658 | auto selectionStart = view().selection().startPosition(); |
659 | auto selectionEnd = view().selection().endPosition(); |
660 | if (state == SelectionStart) |
661 | return !selectionStart; |
662 | |
663 | unsigned end = element()->hasChildNodes() ? element()->countChildNodes() : 1; |
664 | if (state == SelectionEnd) |
665 | return selectionEnd == end; |
666 | if (state == SelectionBoth) |
667 | return !selectionStart && selectionEnd == end; |
668 | ASSERT_NOT_REACHED(); |
669 | return false; |
670 | } |
671 | |
672 | LayoutRect RenderReplaced::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const |
673 | { |
674 | if (style().visibility() != Visibility::Visible && !enclosingLayer()->hasVisibleContent()) |
675 | return LayoutRect(); |
676 | |
677 | // The selectionRect can project outside of the overflowRect, so take their union |
678 | // for repainting to avoid selection painting glitches. |
679 | LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect()); |
680 | // FIXME: layoutDelta needs to be applied in parts before/after transforms and |
681 | // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308 |
682 | r.move(view().frameView().layoutContext().layoutDelta()); |
683 | return computeRectForRepaint(r, repaintContainer); |
684 | } |
685 | |
686 | } |
687 | |