blob: db990d8402f7a100cc9f6e7c81517e181b7d6d98 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2008 Apple Inc. 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 copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "core/rendering/RenderLineBoxList.h"
31
32#include "core/rendering/HitTestResult.h"
33#include "core/rendering/InlineTextBox.h"
34#include "core/rendering/PaintInfo.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010035#include "core/rendering/RenderInline.h"
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010036#include "core/rendering/RenderView.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010037#include "core/rendering/RootInlineBox.h"
38
39using namespace std;
40
41namespace WebCore {
42
43#ifndef NDEBUG
44RenderLineBoxList::~RenderLineBoxList()
45{
46 ASSERT(!m_firstLineBox);
47 ASSERT(!m_lastLineBox);
48}
49#endif
50
51void RenderLineBoxList::appendLineBox(InlineFlowBox* box)
52{
53 checkConsistency();
Ben Murdoch02772c62013-07-26 10:21:05 +010054
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010055 if (!m_firstLineBox)
56 m_firstLineBox = m_lastLineBox = box;
57 else {
58 m_lastLineBox->setNextLineBox(box);
59 box->setPreviousLineBox(m_lastLineBox);
60 m_lastLineBox = box;
61 }
62
63 checkConsistency();
64}
65
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010066void RenderLineBoxList::deleteLineBoxTree()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010067{
68 InlineFlowBox* line = m_firstLineBox;
69 InlineFlowBox* nextLine;
70 while (line) {
71 nextLine = line->nextLineBox();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010072 line->deleteLine();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010073 line = nextLine;
74 }
75 m_firstLineBox = m_lastLineBox = 0;
76}
77
78void RenderLineBoxList::extractLineBox(InlineFlowBox* box)
79{
80 checkConsistency();
Ben Murdoch02772c62013-07-26 10:21:05 +010081
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082 m_lastLineBox = box->prevLineBox();
83 if (box == m_firstLineBox)
84 m_firstLineBox = 0;
85 if (box->prevLineBox())
86 box->prevLineBox()->setNextLineBox(0);
87 box->setPreviousLineBox(0);
88 for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox())
89 curr->setExtracted();
90
91 checkConsistency();
92}
93
94void RenderLineBoxList::attachLineBox(InlineFlowBox* box)
95{
96 checkConsistency();
97
98 if (m_lastLineBox) {
99 m_lastLineBox->setNextLineBox(box);
100 box->setPreviousLineBox(m_lastLineBox);
101 } else
102 m_firstLineBox = box;
103 InlineFlowBox* last = box;
104 for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox()) {
105 curr->setExtracted(false);
106 last = curr;
107 }
108 m_lastLineBox = last;
109
110 checkConsistency();
111}
112
113void RenderLineBoxList::removeLineBox(InlineFlowBox* box)
114{
115 checkConsistency();
116
117 if (box == m_firstLineBox)
118 m_firstLineBox = box->nextLineBox();
119 if (box == m_lastLineBox)
120 m_lastLineBox = box->prevLineBox();
121 if (box->nextLineBox())
122 box->nextLineBox()->setPreviousLineBox(box->prevLineBox());
123 if (box->prevLineBox())
124 box->prevLineBox()->setNextLineBox(box->nextLineBox());
125
126 checkConsistency();
127}
128
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100129void RenderLineBoxList::deleteLineBoxes()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100130{
131 if (m_firstLineBox) {
132 InlineFlowBox* next;
133 for (InlineFlowBox* curr = m_firstLineBox; curr; curr = next) {
134 next = curr->nextLineBox();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100135 curr->destroy();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100136 }
137 m_firstLineBox = 0;
138 m_lastLineBox = 0;
139 }
140}
141
142void RenderLineBoxList::dirtyLineBoxes()
143{
144 for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox())
145 curr->dirtyLineBoxes();
146}
147
148bool RenderLineBoxList::rangeIntersectsRect(RenderBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const
149{
150 RenderBox* block;
151 if (renderer->isBox())
152 block = toRenderBox(renderer);
153 else
154 block = renderer->containingBlock();
155 LayoutUnit physicalStart = block->flipForWritingMode(logicalTop);
156 LayoutUnit physicalEnd = block->flipForWritingMode(logicalBottom);
157 LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart);
158 physicalStart = min(physicalStart, physicalEnd);
Ben Murdoch02772c62013-07-26 10:21:05 +0100159
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100160 if (renderer->style()->isHorizontalWritingMode()) {
161 physicalStart += offset.y();
162 if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y())
163 return false;
164 } else {
165 physicalStart += offset.x();
166 if (physicalStart >= rect.maxX() || physicalStart + physicalExtent <= rect.x())
167 return false;
168 }
Ben Murdoch02772c62013-07-26 10:21:05 +0100169
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100170 return true;
171}
172
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100173bool RenderLineBoxList::anyLineIntersectsRect(RenderBoxModelObject* renderer, const LayoutRect& rect, const LayoutPoint& offset, LayoutUnit outlineSize) const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100174{
175 // We can check the first box and last box and avoid painting/hit testing if we don't
176 // intersect. This is a quick short-circuit that we can take to avoid walking any lines.
177 // FIXME: This check is flawed in the following extremely obscure way:
178 // if some line in the middle has a huge overflow, it might actually extend below the last line.
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000179 RootInlineBox& firstRootBox = firstLineBox()->root();
180 RootInlineBox& lastRootBox = lastLineBox()->root();
181 LayoutUnit firstLineTop = firstLineBox()->logicalTopVisualOverflow(firstRootBox.lineTop());
182 LayoutUnit lastLineBottom = lastLineBox()->logicalBottomVisualOverflow(lastRootBox.lineBottom());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100183 LayoutUnit logicalTop = firstLineTop - outlineSize;
184 LayoutUnit logicalBottom = outlineSize + lastLineBottom;
Ben Murdoch02772c62013-07-26 10:21:05 +0100185
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100186 return rangeIntersectsRect(renderer, logicalTop, logicalBottom, rect, offset);
187}
188
189bool RenderLineBoxList::lineIntersectsDirtyRect(RenderBoxModelObject* renderer, InlineFlowBox* box, const PaintInfo& paintInfo, const LayoutPoint& offset) const
190{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000191 RootInlineBox& root = box->root();
192 LayoutUnit logicalTop = min<LayoutUnit>(box->logicalTopVisualOverflow(root.lineTop()), root.selectionTop()) - renderer->maximalOutlineSize(paintInfo.phase);
193 LayoutUnit logicalBottom = box->logicalBottomVisualOverflow(root.lineBottom()) + renderer->maximalOutlineSize(paintInfo.phase);
Ben Murdoch02772c62013-07-26 10:21:05 +0100194
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100195 return rangeIntersectsRect(renderer, logicalTop, logicalBottom, paintInfo.rect, offset);
196}
197
198void RenderLineBoxList::paint(RenderBoxModelObject* renderer, PaintInfo& paintInfo, const LayoutPoint& paintOffset) const
199{
200 // Only paint during the foreground/selection phases.
Ben Murdoch02772c62013-07-26 10:21:05 +0100201 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseOutline
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100202 && paintInfo.phase != PaintPhaseSelfOutline && paintInfo.phase != PaintPhaseChildOutlines && paintInfo.phase != PaintPhaseTextClip
203 && paintInfo.phase != PaintPhaseMask)
204 return;
205
206 ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could paint like this is if it has a layer.
207
208 // If we have no lines then we have no work to do.
209 if (!firstLineBox())
210 return;
211
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100212 LayoutUnit outlineSize = renderer->maximalOutlineSize(paintInfo.phase);
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100213 if (!anyLineIntersectsRect(renderer, paintInfo.rect, paintOffset, outlineSize))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100214 return;
215
216 PaintInfo info(paintInfo);
217 ListHashSet<RenderInline*> outlineObjects;
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100218 info.setOutlineObjects(&outlineObjects);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100219
220 // See if our root lines intersect with the dirty rect. If so, then we paint
221 // them. Note that boxes can easily overlap, so we can't make any assumptions
222 // based off positions of our first line box or our last line box.
223 for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100224 if (lineIntersectsDirtyRect(renderer, curr, info, paintOffset)) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000225 RootInlineBox& root = curr->root();
226 curr->paint(info, paintOffset, root.lineTop(), root.lineBottom());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100227 }
228 }
229
230 if (info.phase == PaintPhaseOutline || info.phase == PaintPhaseSelfOutline || info.phase == PaintPhaseChildOutlines) {
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100231 ListHashSet<RenderInline*>::iterator end = info.outlineObjects()->end();
232 for (ListHashSet<RenderInline*>::iterator it = info.outlineObjects()->begin(); it != end; ++it) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100233 RenderInline* flow = *it;
234 flow->paintOutline(info, paintOffset);
235 }
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100236 info.outlineObjects()->clear();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100237 }
238}
239
240bool RenderLineBoxList::hitTest(RenderBoxModelObject* renderer, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) const
241{
242 if (hitTestAction != HitTestForeground)
243 return false;
244
245 ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could hit test like this is if it has a layer.
246
247 // If we have no lines then we have no work to do.
248 if (!firstLineBox())
249 return false;
250
251 LayoutPoint point = locationInContainer.point();
252 LayoutRect rect = firstLineBox()->isHorizontal() ?
253 IntRect(point.x(), point.y() - locationInContainer.topPadding(), 1, locationInContainer.topPadding() + locationInContainer.bottomPadding() + 1) :
254 IntRect(point.x() - locationInContainer.leftPadding(), point.y(), locationInContainer.rightPadding() + locationInContainer.leftPadding() + 1, 1);
255
256 if (!anyLineIntersectsRect(renderer, rect, accumulatedOffset))
257 return false;
258
259 // See if our root lines contain the point. If so, then we hit test
260 // them further. Note that boxes can easily overlap, so we can't make any assumptions
261 // based off positions of our first line box or our last line box.
262 for (InlineFlowBox* curr = lastLineBox(); curr; curr = curr->prevLineBox()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000263 RootInlineBox& root = curr->root();
264 if (rangeIntersectsRect(renderer, curr->logicalTopVisualOverflow(root.lineTop()), curr->logicalBottomVisualOverflow(root.lineBottom()), rect, accumulatedOffset)) {
265 bool inside = curr->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, root.lineTop(), root.lineBottom());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100266 if (inside) {
267 renderer->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
268 return true;
269 }
270 }
271 }
272
273 return false;
274}
275
276void RenderLineBoxList::dirtyLinesFromChangedChild(RenderObject* container, RenderObject* child)
277{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100278 if (!container->parent() || (container->isRenderBlock() && (container->selfNeedsLayout() || !container->isRenderBlockFlow())))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100279 return;
280
281 RenderInline* inlineContainer = container->isRenderInline() ? toRenderInline(container) : 0;
282 InlineBox* firstBox = inlineContainer ? inlineContainer->firstLineBoxIncludingCulling() : firstLineBox();
283
284 // If we have no first line box, then just bail early.
285 if (!firstBox) {
286 // For an empty inline, go ahead and propagate the check up to our parent, unless the parent
287 // is already dirty.
288 if (container->isInline() && !container->ancestorLineBoxDirty()) {
289 container->parent()->dirtyLinesFromChangedChild(container);
290 container->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
291 }
292 return;
293 }
294
295 // Try to figure out which line box we belong in. First try to find a previous
Ben Murdoch02772c62013-07-26 10:21:05 +0100296 // line box by examining our siblings. If we didn't find a line box, then use our
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100297 // parent's first line box.
298 RootInlineBox* box = 0;
299 RenderObject* curr = 0;
300 for (curr = child->previousSibling(); curr; curr = curr->previousSibling()) {
301 if (curr->isFloatingOrOutOfFlowPositioned())
302 continue;
303
304 if (curr->isReplaced()) {
305 InlineBox* wrapper = toRenderBox(curr)->inlineBoxWrapper();
306 if (wrapper)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000307 box = &wrapper->root();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100308 } else if (curr->isText()) {
309 InlineTextBox* textBox = toRenderText(curr)->lastTextBox();
310 if (textBox)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000311 box = &textBox->root();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100312 } else if (curr->isRenderInline()) {
313 InlineBox* lastSiblingBox = toRenderInline(curr)->lastLineBoxIncludingCulling();
314 if (lastSiblingBox)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000315 box = &lastSiblingBox->root();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100316 }
317
318 if (box)
319 break;
320 }
321 if (!box) {
322 if (inlineContainer && !inlineContainer->alwaysCreateLineBoxes()) {
323 // https://bugs.webkit.org/show_bug.cgi?id=60778
324 // We may have just removed a <br> with no line box that was our first child. In this case
325 // we won't find a previous sibling, but firstBox can be pointing to a following sibling.
326 // This isn't good enough, since we won't locate the root line box that encloses the removed
327 // <br>. We have to just over-invalidate a bit and go up to our parent.
328 if (!inlineContainer->ancestorLineBoxDirty()) {
329 inlineContainer->parent()->dirtyLinesFromChangedChild(inlineContainer);
330 inlineContainer->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
331 }
332 return;
333 }
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000334 box = &firstBox->root();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100335 }
336
337 // If we found a line box, then dirty it.
338 if (box) {
339 RootInlineBox* adjacentBox;
340 box->markDirty();
341
342 // dirty the adjacent lines that might be affected
343 // NOTE: we dirty the previous line because RootInlineBox objects cache
344 // the address of the first object on the next line after a BR, which we may be
345 // invalidating here. For more info, see how RenderBlock::layoutInlineChildren
346 // calls setLineBreakInfo with the result of findNextLineBreak. findNextLineBreak,
347 // despite the name, actually returns the first RenderObject after the BR.
348 // <rdar://problem/3849947> "Typing after pasting line does not appear until after window resize."
349 adjacentBox = box->prevRootBox();
350 if (adjacentBox)
351 adjacentBox->markDirty();
352 adjacentBox = box->nextRootBox();
353 // If |child| has been inserted before the first element in the linebox, but after collapsed leading
354 // space, the search for |child|'s linebox will go past the leading space to the previous linebox and select that
Ben Murdoch02772c62013-07-26 10:21:05 +0100355 // one as |box|. If we hit that situation here, dirty the |box| actually containing the child too.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100356 bool insertedAfterLeadingSpace = box->lineBreakObj() == child->previousSibling();
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100357 if (adjacentBox && (adjacentBox->lineBreakObj() == child || child->isBR() || (curr && curr->isBR())
358 || insertedAfterLeadingSpace || isIsolated(container->style()->unicodeBidi()))) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100359 adjacentBox->markDirty();
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100360 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100361 }
362}
363
364#ifndef NDEBUG
365
366void RenderLineBoxList::checkConsistency() const
367{
368#ifdef CHECK_CONSISTENCY
369 const InlineFlowBox* prev = 0;
370 for (const InlineFlowBox* child = m_firstLineBox; child != 0; child = child->nextLineBox()) {
371 ASSERT(child->prevLineBox() == prev);
372 prev = child;
373 }
374 ASSERT(prev == m_lastLineBox);
375#endif
376}
377
378#endif
379
380}