d2ff88561584e196092ec6286ad2e4d2a65e7366
[WebKit-https.git] / Source / WebCore / rendering / RenderNamedFlowFragment.cpp
1 /*
2  * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "RenderNamedFlowFragment.h"
32
33 #include "FlowThreadController.h"
34 #include "PaintInfo.h"
35 #include "RenderBoxRegionInfo.h"
36 #include "RenderFlowThread.h"
37 #include "RenderIterator.h"
38 #include "RenderNamedFlowThread.h"
39 #include "RenderTableCell.h"
40 #include "RenderView.h"
41 #include "StyleResolver.h"
42
43 #include <wtf/StackStats.h>
44
45 namespace WebCore {
46
47 RenderNamedFlowFragment::RenderNamedFlowFragment(Document& document, PassRef<RenderStyle> style)
48     : RenderRegion(document, WTF::move(style), nullptr)
49     , m_hasCustomRegionStyle(false)
50     , m_hasAutoLogicalHeight(false)
51     , m_hasComputedAutoHeight(false)
52     , m_computedAutoHeight(0)
53 {
54 }
55
56 RenderNamedFlowFragment::~RenderNamedFlowFragment()
57 {
58 }
59
60 PassRef<RenderStyle> RenderNamedFlowFragment::createStyle(const RenderStyle& parentStyle)
61 {
62     auto style = RenderStyle::createAnonymousStyleWithDisplay(&parentStyle, BLOCK);
63
64     style.get().setFlowThread(parentStyle.flowThread());
65     style.get().setRegionThread(parentStyle.regionThread());
66     style.get().setRegionFragment(parentStyle.regionFragment());
67
68     return style;
69 }
70
71 void RenderNamedFlowFragment::updateRegionFlags()
72 {
73     checkRegionStyle();
74     updateRegionHasAutoLogicalHeightFlag();
75 }
76
77 void RenderNamedFlowFragment::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
78 {
79     RenderRegion::styleDidChange(diff, oldStyle);
80
81     if (!isValid())
82         return;
83
84     updateRegionFlags();
85
86     if (parent() && parent()->needsLayout())
87         setNeedsLayout(MarkOnlyThis);
88 }
89
90 void RenderNamedFlowFragment::getRanges(Vector<RefPtr<Range>>& rangeObjects) const
91 {
92     const RenderNamedFlowThread& namedFlow = view().flowThreadController().ensureRenderFlowThreadWithName(style().regionThread());
93     namedFlow.getRanges(rangeObjects, this);
94 }
95
96 bool RenderNamedFlowFragment::shouldHaveAutoLogicalHeight() const
97 {
98     ASSERT(parent());
99
100     const RenderStyle& styleToUse = parent()->style();
101     bool hasSpecifiedEndpointsForHeight = styleToUse.logicalTop().isSpecified() && styleToUse.logicalBottom().isSpecified();
102     bool hasAnchoredEndpointsForHeight = parent()->isOutOfFlowPositioned() && hasSpecifiedEndpointsForHeight;
103     return styleToUse.logicalHeight().isAuto() && !hasAnchoredEndpointsForHeight;
104 }
105
106 void RenderNamedFlowFragment::incrementAutoLogicalHeightCount()
107 {
108     ASSERT(isValid());
109     ASSERT(m_hasAutoLogicalHeight);
110
111     m_flowThread->incrementAutoLogicalHeightRegions();
112 }
113
114 void RenderNamedFlowFragment::decrementAutoLogicalHeightCount()
115 {
116     ASSERT(isValid());
117
118     m_flowThread->decrementAutoLogicalHeightRegions();
119 }
120
121 void RenderNamedFlowFragment::updateRegionHasAutoLogicalHeightFlag()
122 {
123     ASSERT(isValid());
124
125     bool didHaveAutoLogicalHeight = m_hasAutoLogicalHeight;
126     m_hasAutoLogicalHeight = shouldHaveAutoLogicalHeight();
127     if (didHaveAutoLogicalHeight == m_hasAutoLogicalHeight)
128         return;
129
130     if (m_hasAutoLogicalHeight)
131         incrementAutoLogicalHeightCount();
132     else {
133         clearComputedAutoHeight();
134         decrementAutoLogicalHeightCount();
135     }
136 }
137
138 void RenderNamedFlowFragment::updateLogicalHeight()
139 {
140     RenderRegion::updateLogicalHeight();
141
142     if (!hasAutoLogicalHeight())
143         return;
144
145     // We want to update the logical height based on the computed auto-height
146     // only after the measure cotnent layout phase when all the
147     // auto logical height regions have a computed auto-height.
148     if (m_flowThread->inMeasureContentLayoutPhase())
149         return;
150
151     // There may be regions with auto logical height that during the prerequisite layout phase
152     // did not have the chance to layout flow thread content. Because of that, these regions do not
153     // have a computedAutoHeight and they will not be able to fragment any flow
154     // thread content.
155     if (!hasComputedAutoHeight())
156         return;
157
158     LayoutUnit newLogicalHeight = computedAutoHeight() + borderAndPaddingLogicalHeight();
159     ASSERT(newLogicalHeight < RenderFlowThread::maxLogicalHeight());
160     if (newLogicalHeight > logicalHeight()) {
161         setLogicalHeight(newLogicalHeight);
162         // Recalculate position of the render block after new logical height is set.
163         // (needed in absolute positioning case with bottom alignment for example)
164         RenderRegion::updateLogicalHeight();
165     }
166 }
167
168 LayoutUnit RenderNamedFlowFragment::pageLogicalHeight() const
169 {
170     ASSERT(isValid());
171     if (hasComputedAutoHeight() && m_flowThread->inMeasureContentLayoutPhase()) {
172         ASSERT(hasAutoLogicalHeight());
173         return computedAutoHeight();
174     }
175     return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
176 }
177
178 // This method returns the maximum page size of a region with auto-height. This is the initial
179 // height value for auto-height regions in the first layout phase of the parent named flow.
180 LayoutUnit RenderNamedFlowFragment::maxPageLogicalHeight() const
181 {
182     ASSERT(isValid());
183     ASSERT(hasAutoLogicalHeight() && m_flowThread->inMeasureContentLayoutPhase());
184     ASSERT(isAnonymous());
185     ASSERT(parent());
186
187     const RenderStyle& styleToUse = parent()->style();
188     return styleToUse.logicalMaxHeight().isUndefined() ? RenderFlowThread::maxLogicalHeight() : downcast<RenderBlock>(*parent()).computeReplacedLogicalHeightUsing(styleToUse.logicalMaxHeight());
189 }
190
191 LayoutRect RenderNamedFlowFragment::flowThreadPortionRectForClipping(bool isFirstRegionInRange, bool isLastRegionInRange) const
192 {
193     // Elements flowed into a region should not be painted past the region's content box
194     // if they continue to flow into another region in that direction.
195     // If they do not continue into another region in that direction, they should be
196     // painted all the way to the region's border box.
197     // Regions with overflow:hidden will apply clip at the border box, not the content box.
198     
199     LayoutRect clippingRect = flowThreadPortionRect();
200     RenderBlockFlow& container = fragmentContainer();
201     if (container.style().hasPadding()) {
202         if (isFirstRegionInRange) {
203             if (flowThread()->isHorizontalWritingMode()) {
204                 clippingRect.move(0, -container.paddingBefore());
205                 clippingRect.expand(0, container.paddingBefore());
206             } else {
207                 clippingRect.move(-container.paddingBefore(), 0);
208                 clippingRect.expand(container.paddingBefore(), 0);
209             }
210         }
211         
212         if (isLastRegionInRange) {
213             if (flowThread()->isHorizontalWritingMode())
214                 clippingRect.expand(0, container.paddingAfter());
215             else
216                 clippingRect.expand(container.paddingAfter(), 0);
217         }
218         
219         if (flowThread()->isHorizontalWritingMode()) {
220             clippingRect.move(-container.paddingStart(), 0);
221             clippingRect.expand(container.paddingStart() + container.paddingEnd(), 0);
222         } else {
223             clippingRect.move(0, -container.paddingStart());
224             clippingRect.expand(0, container.paddingStart() + container.paddingEnd());
225         }
226     }
227     
228     return clippingRect;
229 }
230
231 RenderBlockFlow& RenderNamedFlowFragment::fragmentContainer() const
232 {
233     ASSERT(parent());
234     ASSERT(parent()->isRenderNamedFlowFragmentContainer());
235     return downcast<RenderBlockFlow>(*parent());
236 }
237
238 RenderLayer& RenderNamedFlowFragment::fragmentContainerLayer() const
239 {
240     ASSERT(fragmentContainer().layer());
241     return *fragmentContainer().layer();
242 }
243
244 bool RenderNamedFlowFragment::shouldClipFlowThreadContent() const
245 {
246     if (fragmentContainer().hasOverflowClip())
247         return true;
248     
249     return isLastRegion() && (style().regionFragment() == BreakRegionFragment);
250 }
251     
252 LayoutSize RenderNamedFlowFragment::offsetFromContainer(RenderElement& container, const LayoutPoint&, bool*) const
253 {
254     ASSERT_UNUSED(container, &fragmentContainer() == &container);
255     ASSERT_UNUSED(container, this->container() == &container);
256     return topLeftLocationOffset();
257 }
258
259 void RenderNamedFlowFragment::layoutBlock(bool relayoutChildren, LayoutUnit)
260 {
261     StackStats::LayoutCheckPoint layoutCheckPoint;
262     RenderRegion::layoutBlock(relayoutChildren);
263
264     if (isValid()) {
265         if (m_flowThread->inOverflowLayoutPhase() || m_flowThread->inFinalLayoutPhase()) {
266             computeOverflowFromFlowThread();
267             updateOversetState();
268         }
269
270         if (hasAutoLogicalHeight() && m_flowThread->inMeasureContentLayoutPhase()) {
271             m_flowThread->invalidateRegions();
272             clearComputedAutoHeight();
273             return;
274         }
275     }
276 }
277
278 void RenderNamedFlowFragment::invalidateRegionIfNeeded()
279 {
280     if (!isValid())
281         return;
282
283     LayoutRect oldRegionRect(flowThreadPortionRect());
284     if (!isHorizontalWritingMode())
285         oldRegionRect = oldRegionRect.transposedRect();
286
287     if ((oldRegionRect.width() != pageLogicalWidth() || oldRegionRect.height() != pageLogicalHeight()) && !m_flowThread->inFinalLayoutPhase()) {
288         // This can happen even if we are in the inConstrainedLayoutPhase and it will trigger a pathological layout of the flow thread.
289         m_flowThread->invalidateRegions();
290     }
291 }
292
293 void RenderNamedFlowFragment::setRegionOversetState(RegionOversetState state)
294 {
295     ASSERT(generatingElement());
296
297     generatingElement()->setRegionOversetState(state);
298 }
299
300 RegionOversetState RenderNamedFlowFragment::regionOversetState() const
301 {
302     ASSERT(generatingElement());
303
304     if (!isValid())
305         return RegionUndefined;
306
307     return generatingElement()->regionOversetState();
308 }
309
310 void RenderNamedFlowFragment::updateOversetState()
311 {
312     ASSERT(isValid());
313
314     RenderNamedFlowThread* flowThread = namedFlowThread();
315     ASSERT(flowThread && (flowThread->inOverflowLayoutPhase() || flowThread->inFinalLayoutPhase()));
316
317     LayoutUnit flowContentBottom = flowThread->flowContentBottom();
318     bool isHorizontalWritingMode = flowThread->isHorizontalWritingMode();
319
320     LayoutUnit flowMin = flowContentBottom - (isHorizontalWritingMode ? flowThreadPortionRect().y() : flowThreadPortionRect().x());
321     LayoutUnit flowMax = flowContentBottom - (isHorizontalWritingMode ? flowThreadPortionRect().maxY() : flowThreadPortionRect().maxX());
322
323     RegionOversetState previousState = regionOversetState();
324     RegionOversetState state = RegionFit;
325     if (flowMin <= 0)
326         state = RegionEmpty;
327     if (flowMax > 0 && isLastRegion())
328         state = RegionOverset;
329     
330     setRegionOversetState(state);
331
332     // Determine whether the NamedFlow object should dispatch a regionOversetChange event
333     if (previousState != state)
334         flowThread->setDispatchRegionOversetChangeEvent(true);
335 }
336
337 void RenderNamedFlowFragment::checkRegionStyle()
338 {
339     ASSERT(isValid());
340
341     bool customRegionStyle = false;
342
343     // FIXME: Region styling doesn't work for pseudo elements.
344     if (!isPseudoElement())
345         customRegionStyle = view().document().ensureStyleResolver().checkRegionStyle(generatingElement());
346     setHasCustomRegionStyle(customRegionStyle);
347     toRenderNamedFlowThread(m_flowThread)->checkRegionsWithStyling();
348 }
349
350 PassRefPtr<RenderStyle> RenderNamedFlowFragment::computeStyleInRegion(RenderElement& renderer, RenderStyle& parentStyle) const
351 {
352     ASSERT(!renderer.isAnonymous());
353
354     // FIXME: Region styling fails for pseudo-elements because the renderers don't have a node.
355     RefPtr<RenderStyle> renderObjectRegionStyle = renderer.view().document().ensureStyleResolver().styleForElement(renderer.element(), &parentStyle, DisallowStyleSharing, MatchAllRules, this);
356
357     return renderObjectRegionStyle.release();
358 }
359
360 void RenderNamedFlowFragment::computeChildrenStyleInRegion(RenderElement& renderer)
361 {
362     for (auto& child : childrenOfType<RenderObject>(renderer)) {
363
364         auto it = m_renderObjectRegionStyle.find(&child);
365
366         RefPtr<RenderStyle> childStyleInRegion;
367         bool objectRegionStyleCached = false;
368         if (it != m_renderObjectRegionStyle.end()) {
369             childStyleInRegion = it->value.style;
370             objectRegionStyleCached = true;
371         } else {
372             if (child.isAnonymous() || child.isInFlowRenderFlowThread())
373                 childStyleInRegion = RenderStyle::createAnonymousStyleWithDisplay(&renderer.style(), child.style().display());
374             else if (is<RenderText>(child))
375                 childStyleInRegion = RenderStyle::clone(&renderer.style());
376             else
377                 childStyleInRegion = computeStyleInRegion(downcast<RenderElement>(child), renderer.style());
378         }
379
380         setObjectStyleInRegion(&child, childStyleInRegion, objectRegionStyleCached);
381
382         if (is<RenderElement>(child))
383             computeChildrenStyleInRegion(downcast<RenderElement>(child));
384     }
385 }
386
387 void RenderNamedFlowFragment::setObjectStyleInRegion(RenderObject* object, PassRefPtr<RenderStyle> styleInRegion, bool objectRegionStyleCached)
388 {
389     ASSERT(object->flowThreadContainingBlock());
390
391     RefPtr<RenderStyle> objectOriginalStyle = &object->style();
392     if (is<RenderElement>(*object))
393         downcast<RenderElement>(*object).setStyleInternal(*styleInRegion);
394
395     if (is<RenderBoxModelObject>(*object) && !object->hasBoxDecorations()) {
396         bool hasBoxDecorations = is<RenderTableCell>(*object)
397         || object->style().hasBackground()
398         || object->style().hasBorder()
399         || object->style().hasAppearance()
400         || object->style().boxShadow();
401         object->setHasBoxDecorations(hasBoxDecorations);
402     }
403
404     ObjectRegionStyleInfo styleInfo;
405     styleInfo.style = objectOriginalStyle;
406     styleInfo.cached = objectRegionStyleCached;
407     m_renderObjectRegionStyle.set(object, styleInfo);
408 }
409
410 void RenderNamedFlowFragment::clearObjectStyleInRegion(const RenderObject* object)
411 {
412     ASSERT(object);
413     m_renderObjectRegionStyle.remove(object);
414
415     // Clear the style for the children of this object.
416     for (RenderObject* child = object->firstChildSlow(); child; child = child->nextSibling())
417         clearObjectStyleInRegion(child);
418 }
419
420 void RenderNamedFlowFragment::setRegionObjectsRegionStyle()
421 {
422     if (!hasCustomRegionStyle())
423         return;
424
425     // Start from content nodes and recursively compute the style in region for the render objects below.
426     // If the style in region was already computed, used that style instead of computing a new one.
427     const RenderNamedFlowThread& namedFlow = view().flowThreadController().ensureRenderFlowThreadWithName(style().regionThread());
428     const NamedFlowContentElements& contentElements = namedFlow.contentElements();
429
430     for (const auto& element : contentElements) {
431         // The list of content nodes contains also the nodes with display:none.
432         if (!element->renderer())
433             continue;
434
435         RenderElement* object = element->renderer();
436         // If the content node does not flow any of its children in this region,
437         // we do not compute any style for them in this region.
438         if (!flowThread()->objectInFlowRegion(object, this))
439             continue;
440
441         // If the object has style in region, use that instead of computing a new one.
442         auto it = m_renderObjectRegionStyle.find(object);
443         RefPtr<RenderStyle> objectStyleInRegion;
444         bool objectRegionStyleCached = false;
445         if (it != m_renderObjectRegionStyle.end()) {
446             objectStyleInRegion = it->value.style;
447             ASSERT(it->value.cached);
448             objectRegionStyleCached = true;
449         } else
450             objectStyleInRegion = computeStyleInRegion(*object, style());
451
452         setObjectStyleInRegion(object, objectStyleInRegion, objectRegionStyleCached);
453
454         computeChildrenStyleInRegion(*object);
455     }
456 }
457
458 void RenderNamedFlowFragment::restoreRegionObjectsOriginalStyle()
459 {
460     if (!hasCustomRegionStyle())
461         return;
462
463     RenderObjectRegionStyleMap temp;
464     for (auto& objectPair : m_renderObjectRegionStyle) {
465         RenderObject* object = const_cast<RenderObject*>(objectPair.key);
466         RefPtr<RenderStyle> objectRegionStyle = &object->style();
467         RefPtr<RenderStyle> objectOriginalStyle = objectPair.value.style;
468         if (is<RenderElement>(*object))
469             downcast<RenderElement>(*object).setStyleInternal(*objectOriginalStyle);
470
471         bool shouldCacheRegionStyle = objectPair.value.cached;
472         if (!shouldCacheRegionStyle) {
473             // Check whether we should cache the computed style in region.
474             unsigned changedContextSensitiveProperties = ContextSensitivePropertyNone;
475             StyleDifference styleDiff = objectOriginalStyle->diff(objectRegionStyle.get(), changedContextSensitiveProperties);
476             if (styleDiff < StyleDifferenceLayoutPositionedMovementOnly)
477                 shouldCacheRegionStyle = true;
478         }
479         if (shouldCacheRegionStyle) {
480             ObjectRegionStyleInfo styleInfo;
481             styleInfo.style = objectRegionStyle;
482             styleInfo.cached = true;
483             temp.set(object, styleInfo);
484         }
485     }
486
487     m_renderObjectRegionStyle.swap(temp);
488 }
489
490 RenderNamedFlowThread* RenderNamedFlowFragment::namedFlowThread() const
491 {
492     return toRenderNamedFlowThread(flowThread());
493 }
494
495 LayoutRect RenderNamedFlowFragment::visualOverflowRect() const
496 {
497     if (isValid()) {
498         RenderBoxRegionInfo* boxInfo = renderBoxRegionInfo(flowThread());
499         if (boxInfo && boxInfo->overflow())
500             return boxInfo->overflow()->visualOverflowRect();
501     }
502     
503     return RenderRegion::visualOverflowRect();
504 }
505
506 void RenderNamedFlowFragment::attachRegion()
507 {
508     RenderRegion::attachRegion();
509
510     if (documentBeingDestroyed() || !isValid())
511         return;
512
513     updateRegionFlags();
514 }
515
516 void RenderNamedFlowFragment::detachRegion()
517 {
518     if (hasAutoLogicalHeight()) {
519         ASSERT(isValid());
520         m_hasAutoLogicalHeight = false;
521         clearComputedAutoHeight();
522         decrementAutoLogicalHeightCount();
523     }
524     
525     RenderRegion::detachRegion();
526 }
527
528 void RenderNamedFlowFragment::absoluteQuadsForBoxInRegion(Vector<FloatQuad>& quads, bool* wasFixed, const RenderBox* renderer, float localTop, float localBottom)
529 {
530     LayoutRect layoutLocalRect(0, localTop, renderer->borderBoxRectInRegion(this).width(), localBottom - localTop);
531     LayoutRect fragmentRect = rectFlowPortionForBox(renderer, layoutLocalRect);
532
533     // We want to skip the 0px height fragments for non-empty boxes that may appear in case the bottom of the box
534     // overlaps the bottom of a region.
535     if (localBottom != localTop && !fragmentRect.height())
536         return;
537
538     CurrentRenderRegionMaintainer regionMaintainer(*this);
539     quads.append(renderer->localToAbsoluteQuad(FloatRect(fragmentRect), 0 /* mode */, wasFixed));
540 }
541
542 } // namespace WebCore