Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RootInlineBox.cpp
1 /*
2  * Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "RootInlineBox.h"
22
23 #include "BidiResolver.h"
24 #include "Chrome.h"
25 #include "ChromeClient.h"
26 #include "Document.h"
27 #include "EllipsisBox.h"
28 #include "Frame.h"
29 #include "GraphicsContext.h"
30 #include "HitTestResult.h"
31 #include "InlineTextBox.h"
32 #include "Page.h"
33 #include "PaintInfo.h"
34 #include "RenderArena.h"
35 #include "RenderBlock.h"
36 #include "RenderView.h"
37 #include "VerticalPositionCache.h"
38 #include <wtf/unicode/Unicode.h>
39
40 using namespace std;
41
42 namespace WebCore {
43     
44 typedef WTF::HashMap<const RootInlineBox*, EllipsisBox*> EllipsisBoxMap;
45 static EllipsisBoxMap* gEllipsisBoxMap = 0;
46
47 RootInlineBox::RootInlineBox(RenderBlock* block)
48     : InlineFlowBox(block)
49     , m_lineBreakPos(0)
50     , m_lineBreakObj(0)
51     , m_lineTop(0)
52     , m_lineBottom(0)
53     , m_lineTopWithLeading(0)
54     , m_lineBottomWithLeading(0)
55     , m_paginationStrut(0)
56     , m_paginatedLineWidth(0)
57 {
58     setIsHorizontal(block->isHorizontalWritingMode());
59 }
60
61
62 void RootInlineBox::destroy(RenderArena* arena)
63 {
64     detachEllipsisBox(arena);
65     InlineFlowBox::destroy(arena);
66 }
67
68 void RootInlineBox::detachEllipsisBox(RenderArena* arena)
69 {
70     if (hasEllipsisBox()) {
71         EllipsisBox* box = gEllipsisBoxMap->take(this);
72         box->setParent(0);
73         box->destroy(arena);
74         setHasEllipsisBox(false);
75     }
76 }
77
78 RenderLineBoxList* RootInlineBox::rendererLineBoxes() const
79 {
80     return block()->lineBoxes();
81 }
82
83 void RootInlineBox::clearTruncation()
84 {
85     if (hasEllipsisBox()) {
86         detachEllipsisBox(renderer()->renderArena());
87         InlineFlowBox::clearTruncation();
88     }
89 }
90
91 bool RootInlineBox::isHyphenated() const
92 {
93     for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
94         if (box->isInlineTextBox()) {
95             if (toInlineTextBox(box)->hasHyphen())
96                 return true;
97         }
98     }
99
100     return false;
101 }
102
103 LayoutUnit RootInlineBox::baselinePosition(FontBaseline baselineType) const
104 {
105     return boxModelObject()->baselinePosition(baselineType, isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
106 }
107
108 LayoutUnit RootInlineBox::lineHeight() const
109 {
110     return boxModelObject()->lineHeight(isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
111 }
112
113 bool RootInlineBox::lineCanAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth)
114 {
115     // First sanity-check the unoverflowed width of the whole line to see if there is sufficient room.
116     int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge;
117     if (logicalWidth() - delta < ellipsisWidth)
118         return false;
119
120     // Next iterate over all the line boxes on the line.  If we find a replaced element that intersects
121     // then we refuse to accommodate the ellipsis.  Otherwise we're ok.
122     return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth);
123 }
124
125 float RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr,  bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth,
126                                   InlineBox* markupBox)
127 {
128     // Create an ellipsis box.
129     EllipsisBox* ellipsisBox = new (renderer()->renderArena()) EllipsisBox(renderer(), ellipsisStr, this,
130                                                               ellipsisWidth - (markupBox ? markupBox->logicalWidth() : 0), logicalHeight(),
131                                                               y(), !prevRootBox(), isHorizontal(), markupBox);
132     
133     if (!gEllipsisBoxMap)
134         gEllipsisBoxMap = new EllipsisBoxMap();
135     gEllipsisBoxMap->add(this, ellipsisBox);
136     setHasEllipsisBox(true);
137
138     // FIXME: Do we need an RTL version of this?
139     if (ltr && (x() + logicalWidth() + ellipsisWidth) <= blockRightEdge) {
140         ellipsisBox->setX(x() + logicalWidth());
141         return logicalWidth() + ellipsisWidth;
142     }
143
144     // Now attempt to find the nearest glyph horizontally and place just to the right (or left in RTL)
145     // of that glyph.  Mark all of the objects that intersect the ellipsis box as not painting (as being
146     // truncated).
147     bool foundBox = false;
148     float truncatedWidth = 0;
149     float position = placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
150     ellipsisBox->setX(position);
151     return truncatedWidth;
152 }
153
154 float RootInlineBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
155 {
156     float result = InlineFlowBox::placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
157     if (result == -1) {
158         result = ltr ? blockRightEdge - ellipsisWidth : blockLeftEdge;
159         truncatedWidth = blockRightEdge - blockLeftEdge;
160     }
161     return result;
162 }
163
164 void RootInlineBox::paintEllipsisBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) const
165 {
166     if (hasEllipsisBox() && paintInfo.shouldPaintWithinRoot(renderer()) && renderer()->style()->visibility() == VISIBLE
167             && paintInfo.phase == PaintPhaseForeground)
168         ellipsisBox()->paint(paintInfo, paintOffset, lineTop, lineBottom);
169 }
170
171 #if PLATFORM(MAC)
172
173 void RootInlineBox::addHighlightOverflow()
174 {
175     Frame* frame = renderer()->frame();
176     if (!frame)
177         return;
178     Page* page = frame->page();
179     if (!page)
180         return;
181
182     // Highlight acts as a selection inflation.
183     FloatRect rootRect(0, selectionTop(), logicalWidth(), selectionHeight());
184     IntRect inflatedRect = enclosingIntRect(page->chrome()->client()->customHighlightRect(renderer()->node(), renderer()->style()->highlight(), rootRect));
185     setOverflowFromLogicalRects(inflatedRect, inflatedRect, lineTop(), lineBottom());
186 }
187
188 void RootInlineBox::paintCustomHighlight(PaintInfo& paintInfo, const LayoutPoint& paintOffset, const AtomicString& highlightType)
189 {
190     if (!paintInfo.shouldPaintWithinRoot(renderer()) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseForeground)
191         return;
192
193     Frame* frame = renderer()->frame();
194     if (!frame)
195         return;
196     Page* page = frame->page();
197     if (!page)
198         return;
199
200     // Get the inflated rect so that we can properly hit test.
201     FloatRect rootRect(paintOffset.x() + x(), paintOffset.y() + selectionTop(), logicalWidth(), selectionHeight());
202     FloatRect inflatedRect = page->chrome()->client()->customHighlightRect(renderer()->node(), highlightType, rootRect);
203     if (inflatedRect.intersects(paintInfo.rect))
204         page->chrome()->client()->paintCustomHighlight(renderer()->node(), highlightType, rootRect, rootRect, false, true);
205 }
206
207 #endif
208
209 void RootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
210 {
211     InlineFlowBox::paint(paintInfo, paintOffset, lineTop, lineBottom);
212     paintEllipsisBox(paintInfo, paintOffset, lineTop, lineBottom);
213 #if PLATFORM(MAC)
214     RenderStyle* styleToUse = renderer()->style(isFirstLineStyle());
215     if (styleToUse->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
216         paintCustomHighlight(paintInfo, paintOffset, styleToUse->highlight());
217 #endif
218 }
219
220 bool RootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
221 {
222     if (hasEllipsisBox() && visibleToHitTesting()) {
223         if (ellipsisBox()->nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom)) {
224             renderer()->updateHitTestResult(result, pointInContainer.point() - toLayoutSize(accumulatedOffset));
225             return true;
226         }
227     }
228     return InlineFlowBox::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom);
229 }
230
231 void RootInlineBox::adjustPosition(float dx, float dy)
232 {
233     InlineFlowBox::adjustPosition(dx, dy);
234     LayoutUnit blockDirectionDelta = isHorizontal() ? dy : dx; // The block direction delta is a LayoutUnit.
235     m_lineTop += blockDirectionDelta;
236     m_lineBottom += blockDirectionDelta;
237     m_lineTopWithLeading += blockDirectionDelta;
238     m_lineBottomWithLeading += blockDirectionDelta;
239     if (hasEllipsisBox())
240         ellipsisBox()->adjustPosition(dx, dy);
241 }
242
243 void RootInlineBox::childRemoved(InlineBox* box)
244 {
245     if (box->renderer() == m_lineBreakObj)
246         setLineBreakInfo(0, 0, BidiStatus());
247
248     for (RootInlineBox* prev = prevRootBox(); prev && prev->lineBreakObj() == box->renderer(); prev = prev->prevRootBox()) {
249         prev->setLineBreakInfo(0, 0, BidiStatus());
250         prev->markDirty();
251     }
252 }
253
254 LayoutUnit RootInlineBox::alignBoxesInBlockDirection(LayoutUnit heightOfBlock, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
255 {
256 #if ENABLE(SVG)
257     // SVG will handle vertical alignment on its own.
258     if (isSVGRootInlineBox())
259         return 0;
260 #endif
261
262     LayoutUnit maxPositionTop = 0;
263     LayoutUnit maxPositionBottom = 0;
264     LayoutUnit maxAscent = 0;
265     LayoutUnit maxDescent = 0;
266     bool setMaxAscent = false;
267     bool setMaxDescent = false;
268
269     // Figure out if we're in no-quirks mode.
270     bool noQuirksMode = renderer()->document()->inNoQuirksMode();
271
272     m_baselineType = requiresIdeographicBaseline(textBoxDataMap) ? IdeographicBaseline : AlphabeticBaseline;
273
274     computeLogicalBoxHeights(this, maxPositionTop, maxPositionBottom, maxAscent, maxDescent, setMaxAscent, setMaxDescent, noQuirksMode,
275                              textBoxDataMap, baselineType(), verticalPositionCache);
276
277     if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
278         adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
279
280     LayoutUnit maxHeight = maxAscent + maxDescent;
281     LayoutUnit lineTop = heightOfBlock;
282     LayoutUnit lineBottom = heightOfBlock;
283     LayoutUnit lineTopIncludingMargins = heightOfBlock;
284     LayoutUnit lineBottomIncludingMargins = heightOfBlock;
285     bool setLineTop = false;
286     bool hasAnnotationsBefore = false;
287     bool hasAnnotationsAfter = false;
288     placeBoxesInBlockDirection(heightOfBlock, maxHeight, maxAscent, noQuirksMode, lineTop, lineBottom, setLineTop,
289                                lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType());
290     m_hasAnnotationsBefore = hasAnnotationsBefore;
291     m_hasAnnotationsAfter = hasAnnotationsAfter;
292     
293     maxHeight = max<LayoutUnit>(0, maxHeight); // FIXME: Is this really necessary?
294
295     setLineTopBottomPositions(lineTop, lineBottom, heightOfBlock, heightOfBlock + maxHeight);
296     setPaginatedLineWidth(block()->availableLogicalWidthForContent(heightOfBlock));
297
298     LayoutUnit annotationsAdjustment = beforeAnnotationsAdjustment();
299     if (annotationsAdjustment) {
300         // FIXME: Need to handle pagination here. We might have to move to the next page/column as a result of the
301         // ruby expansion.
302         adjustBlockDirectionPosition(annotationsAdjustment);
303         heightOfBlock += annotationsAdjustment;
304     }
305
306     LayoutUnit gridSnapAdjustment = lineSnapAdjustment();
307     if (gridSnapAdjustment) {
308         adjustBlockDirectionPosition(gridSnapAdjustment);
309         heightOfBlock += gridSnapAdjustment;
310     }
311
312     return heightOfBlock + maxHeight;
313 }
314
315 LayoutUnit RootInlineBox::beforeAnnotationsAdjustment() const
316 {
317     LayoutUnit result = 0;
318
319     if (!renderer()->style()->isFlippedLinesWritingMode()) {
320         // Annotations under the previous line may push us down.
321         if (prevRootBox() && prevRootBox()->hasAnnotationsAfter())
322             result = prevRootBox()->computeUnderAnnotationAdjustment(lineTop());
323
324         if (!hasAnnotationsBefore())
325             return result;
326
327         // Annotations over this line may push us further down.
328         LayoutUnit highestAllowedPosition = prevRootBox() ? min(prevRootBox()->lineBottom(), lineTop()) + result : static_cast<LayoutUnit>(block()->borderBefore());
329         result = computeOverAnnotationAdjustment(highestAllowedPosition);
330     } else {
331         // Annotations under this line may push us up.
332         if (hasAnnotationsBefore())
333             result = computeUnderAnnotationAdjustment(prevRootBox() ? prevRootBox()->lineBottom() : static_cast<LayoutUnit>(block()->borderBefore()));
334
335         if (!prevRootBox() || !prevRootBox()->hasAnnotationsAfter())
336             return result;
337
338         // We have to compute the expansion for annotations over the previous line to see how much we should move.
339         LayoutUnit lowestAllowedPosition = max(prevRootBox()->lineBottom(), lineTop()) - result;
340         result = prevRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
341     }
342
343     return result;
344 }
345
346 LayoutUnit RootInlineBox::lineSnapAdjustment(LayoutUnit delta) const
347 {
348     // If our block doesn't have snapping turned on, do nothing.
349     // FIXME: Implement bounds snapping.
350     if (block()->style()->lineSnap() == LineSnapNone)
351         return 0;
352
353     // Get the current line grid and offset.
354     LayoutState* layoutState = block()->view()->layoutState();
355     RenderBlock* lineGrid = layoutState->lineGrid();
356     LayoutSize lineGridOffset = layoutState->lineGridOffset();
357     if (!lineGrid || lineGrid->style()->writingMode() != block()->style()->writingMode())
358         return 0;
359
360     // Get the hypothetical line box used to establish the grid.
361     RootInlineBox* lineGridBox = lineGrid->lineGridBox();
362     if (!lineGridBox)
363         return 0;
364     
365     LayoutUnit lineGridBlockOffset = lineGrid->isHorizontalWritingMode() ? lineGridOffset.height() : lineGridOffset.width();
366     LayoutUnit blockOffset = block()->isHorizontalWritingMode() ? layoutState->layoutOffset().height() : layoutState->layoutOffset().width();
367
368     // Now determine our position on the grid. Our baseline needs to be adjusted to the nearest baseline multiple
369     // as established by the line box.
370     // FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume
371     // the grid should honor line-box-contain.
372     LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
373     if (!gridLineHeight)
374         return 0;
375
376     LayoutUnit lineGridFontAscent = lineGrid->style()->fontMetrics().ascent(baselineType());
377     LayoutUnit lineGridFontHeight = lineGridBox->logicalHeight();
378     LayoutUnit firstTextTop = lineGridBlockOffset + lineGridBox->logicalTop();
379     LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading();
380     LayoutUnit firstBaselinePosition = firstTextTop + lineGridFontAscent;
381
382     LayoutUnit currentTextTop = blockOffset + logicalTop() + delta;
383     LayoutUnit currentFontAscent = block()->style()->fontMetrics().ascent(baselineType());
384     LayoutUnit currentBaselinePosition = currentTextTop + currentFontAscent;
385
386     LayoutUnit lineGridPaginationOrigin = isHorizontal() ? layoutState->lineGridPaginationOrigin().height() : layoutState->lineGridPaginationOrigin().width();
387
388     // If we're paginated, see if we're on a page after the first one. If so, the grid resets on subsequent pages.
389     // FIXME: If the grid is an ancestor of the pagination establisher, then this is incorrect.
390     LayoutUnit pageLogicalTop = 0;
391     if (layoutState->isPaginated() && layoutState->pageLogicalHeight()) {
392         pageLogicalTop = block()->pageLogicalTopForOffset(lineTopWithLeading() + delta);
393         if (pageLogicalTop > firstLineTopWithLeading)
394             firstTextTop = pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderBefore() - lineGrid->paddingBefore() + lineGridPaginationOrigin;
395     }
396
397     if (block()->style()->lineSnap() == LineSnapContain) {
398         // Compute the desired offset from the text-top of a grid line.
399         // Look at our height (logicalHeight()).
400         // Look at the total available height. It's going to be (textBottom - textTop) + (n-1)*(multiple with leading)
401         // where n is number of grid lines required to enclose us.
402         if (logicalHeight() <= lineGridFontHeight)
403             firstTextTop += (lineGridFontHeight - logicalHeight()) / 2;
404         else {
405             LayoutUnit numberOfLinesWithLeading = ceilf(static_cast<float>(logicalHeight() - lineGridFontHeight) / gridLineHeight);
406             LayoutUnit totalHeight = lineGridFontHeight + numberOfLinesWithLeading * gridLineHeight;
407             firstTextTop += (totalHeight - logicalHeight()) / 2;
408         }
409         firstBaselinePosition = firstTextTop + currentFontAscent;
410     } else
411         firstBaselinePosition = firstTextTop + lineGridFontAscent;
412
413     // If we're above the first line, just push to the first line.
414     if (currentBaselinePosition < firstBaselinePosition)
415         return delta + firstBaselinePosition - currentBaselinePosition;
416
417     // Otherwise we're in the middle of the grid somewhere. Just push to the next line.
418     LayoutUnit baselineOffset = currentBaselinePosition - firstBaselinePosition;
419     LayoutUnit remainder = roundToInt(baselineOffset) % roundToInt(gridLineHeight);
420     LayoutUnit result = delta;
421     if (remainder)
422         result += gridLineHeight - remainder;
423
424     // If we aren't paginated we can return the result.
425     if (!layoutState->isPaginated() || !layoutState->pageLogicalHeight() || result == delta)
426         return result;
427     
428     // We may end up shifted to a new page. We need to do a re-snap when that happens.
429     LayoutUnit newPageLogicalTop = block()->pageLogicalTopForOffset(lineBottomWithLeading() + result);
430     if (newPageLogicalTop == pageLogicalTop)
431         return result;
432     
433     // Put ourselves at the top of the next page to force a snap onto the new grid established by that page.
434     return lineSnapAdjustment(newPageLogicalTop - (blockOffset + lineTopWithLeading()));
435 }
436
437 GapRects RootInlineBox::lineSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock, 
438                                          LayoutUnit selTop, LayoutUnit selHeight, const PaintInfo* paintInfo)
439 {
440     RenderObject::SelectionState lineState = selectionState();
441
442     bool leftGap, rightGap;
443     block()->getSelectionGapInfo(lineState, leftGap, rightGap);
444
445     GapRects result;
446
447     InlineBox* firstBox = firstSelectedBox();
448     InlineBox* lastBox = lastSelectedBox();
449     if (leftGap)
450         result.uniteLeft(block()->logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
451                                                           firstBox->parent()->renderer(), firstBox->logicalLeft(), selTop, selHeight, paintInfo));
452     if (rightGap)
453         result.uniteRight(block()->logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
454                                                             lastBox->parent()->renderer(), lastBox->logicalRight(), selTop, selHeight, paintInfo));
455
456     // When dealing with bidi text, a non-contiguous selection region is possible.
457     // e.g. The logical text aaaAAAbbb (capitals denote RTL text and non-capitals LTR) is layed out
458     // visually as 3 text runs |aaa|bbb|AAA| if we select 4 characters from the start of the text the
459     // selection will look like (underline denotes selection):
460     // |aaa|bbb|AAA|
461     //  ___       _
462     // We can see that the |bbb| run is not part of the selection while the runs around it are.
463     if (firstBox && firstBox != lastBox) {
464         // Now fill in any gaps on the line that occurred between two selected elements.
465         LayoutUnit lastLogicalLeft = firstBox->logicalRight();
466         bool isPreviousBoxSelected = firstBox->selectionState() != RenderObject::SelectionNone;
467         for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) {
468             if (box->selectionState() != RenderObject::SelectionNone) {
469                 LayoutRect logicalRect(lastLogicalLeft, selTop, box->logicalLeft() - lastLogicalLeft, selHeight);
470                 logicalRect.move(renderer()->isHorizontalWritingMode() ? offsetFromRootBlock : LayoutSize(offsetFromRootBlock.height(), offsetFromRootBlock.width()));
471                 LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
472                 if (isPreviousBoxSelected && gapRect.width() > 0 && gapRect.height() > 0) {
473                     if (paintInfo && box->parent()->renderer()->style()->visibility() == VISIBLE)
474                         paintInfo->context->fillRect(gapRect, box->parent()->renderer()->selectionBackgroundColor(), box->parent()->renderer()->style()->colorSpace());
475                     // VisibleSelection may be non-contiguous, see comment above.
476                     result.uniteCenter(gapRect);
477                 }
478                 lastLogicalLeft = box->logicalRight();
479             }
480             if (box == lastBox)
481                 break;
482             isPreviousBoxSelected = box->selectionState() != RenderObject::SelectionNone;
483         }
484     }
485
486     return result;
487 }
488
489 RenderObject::SelectionState RootInlineBox::selectionState()
490 {
491     // Walk over all of the selected boxes.
492     RenderObject::SelectionState state = RenderObject::SelectionNone;
493     for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
494         RenderObject::SelectionState boxState = box->selectionState();
495         if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) ||
496             (boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart))
497             state = RenderObject::SelectionBoth;
498         else if (state == RenderObject::SelectionNone ||
499                  ((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) &&
500                   (state == RenderObject::SelectionNone || state == RenderObject::SelectionInside)))
501             state = boxState;
502         if (state == RenderObject::SelectionBoth)
503             break;
504     }
505
506     return state;
507 }
508
509 InlineBox* RootInlineBox::firstSelectedBox()
510 {
511     for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
512         if (box->selectionState() != RenderObject::SelectionNone)
513             return box;
514     }
515
516     return 0;
517 }
518
519 InlineBox* RootInlineBox::lastSelectedBox()
520 {
521     for (InlineBox* box = lastLeafChild(); box; box = box->prevLeafChild()) {
522         if (box->selectionState() != RenderObject::SelectionNone)
523             return box;
524     }
525
526     return 0;
527 }
528
529 LayoutUnit RootInlineBox::selectionTop() const
530 {
531     LayoutUnit selectionTop = m_lineTop;
532
533     if (m_hasAnnotationsBefore)
534         selectionTop -= !renderer()->style()->isFlippedLinesWritingMode() ? computeOverAnnotationAdjustment(m_lineTop) : computeUnderAnnotationAdjustment(m_lineTop);
535
536     if (renderer()->style()->isFlippedLinesWritingMode())
537         return selectionTop;
538
539     LayoutUnit prevBottom = prevRootBox() ? prevRootBox()->selectionBottom() : block()->borderBefore() + block()->paddingBefore();
540     if (prevBottom < selectionTop && block()->containsFloats()) {
541         // This line has actually been moved further down, probably from a large line-height, but possibly because the
542         // line was forced to clear floats.  If so, let's check the offsets, and only be willing to use the previous
543         // line's bottom if the offsets are greater on both sides.
544         LayoutUnit prevLeft = block()->logicalLeftOffsetForLine(prevBottom, false);
545         LayoutUnit prevRight = block()->logicalRightOffsetForLine(prevBottom, false);
546         LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionTop, false);
547         LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionTop, false);
548 #if ENABLE(TIZEN_ROOT_INLINE_BOX_SELECTION_TOP)
549         /* To avoid too high highlight on bbc.co.uk
550          * This issue is reported on bugzilla:
551          * https://bugs.webkit.org/show_bug.cgi?id=65307
552          * You can find there simple pages to test it.
553          */
554         if (prevLeft >= newLeft || prevRight <= newRight)
555 #else
556         if (prevLeft > newLeft || prevRight < newRight)
557 #endif
558             return selectionTop;
559     }
560
561     return prevBottom;
562 }
563
564 LayoutUnit RootInlineBox::selectionTopAdjustedForPrecedingBlock() const
565 {
566     LayoutUnit top = selectionTop();
567
568     RenderObject::SelectionState blockSelectionState = root()->block()->selectionState();
569     if (blockSelectionState != RenderObject::SelectionInside && blockSelectionState != RenderObject::SelectionEnd)
570         return top;
571
572     LayoutSize offsetToBlockBefore;
573     if (RenderBlock* block = root()->block()->blockBeforeWithinSelectionRoot(offsetToBlockBefore)) {
574         if (RootInlineBox* lastLine = block->lastRootBox()) {
575             RenderObject::SelectionState lastLineSelectionState = lastLine->selectionState();
576             if (lastLineSelectionState != RenderObject::SelectionInside && lastLineSelectionState != RenderObject::SelectionStart)
577                 return top;
578
579             LayoutUnit lastLineSelectionBottom = lastLine->selectionBottom() + offsetToBlockBefore.height();
580             top = max(top, lastLineSelectionBottom);
581         }
582     }
583
584     return top;
585 }
586
587 LayoutUnit RootInlineBox::selectionBottom() const
588 {
589     LayoutUnit selectionBottom = m_lineBottom;
590
591     if (m_hasAnnotationsAfter)
592         selectionBottom += !renderer()->style()->isFlippedLinesWritingMode() ? computeUnderAnnotationAdjustment(m_lineBottom) : computeOverAnnotationAdjustment(m_lineBottom);
593
594     if (!renderer()->style()->isFlippedLinesWritingMode() || !nextRootBox())
595         return selectionBottom;
596
597     LayoutUnit nextTop = nextRootBox()->selectionTop();
598     if (nextTop > selectionBottom && block()->containsFloats()) {
599         // The next line has actually been moved further over, probably from a large line-height, but possibly because the
600         // line was forced to clear floats.  If so, let's check the offsets, and only be willing to use the next
601         // line's top if the offsets are greater on both sides.
602         LayoutUnit nextLeft = block()->logicalLeftOffsetForLine(nextTop, false);
603         LayoutUnit nextRight = block()->logicalRightOffsetForLine(nextTop, false);
604         LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionBottom, false);
605         LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionBottom, false);
606         if (nextLeft > newLeft || nextRight < newRight)
607             return selectionBottom;
608     }
609
610     return nextTop;
611 }
612
613 int RootInlineBox::blockDirectionPointInLine() const
614 {
615     return !block()->style()->isFlippedBlocksWritingMode() ? max(lineTop(), selectionTop()) : min(lineBottom(), selectionBottom());
616 }
617
618 RenderBlock* RootInlineBox::block() const
619 {
620     return toRenderBlock(renderer());
621 }
622
623 static bool isEditableLeaf(InlineBox* leaf)
624 {
625     return leaf && leaf->renderer() && leaf->renderer()->node() && leaf->renderer()->node()->rendererIsEditable();
626 }
627
628 InlineBox* RootInlineBox::closestLeafChildForPoint(const IntPoint& pointInContents, bool onlyEditableLeaves)
629 {
630     return closestLeafChildForLogicalLeftPosition(block()->isHorizontalWritingMode() ? pointInContents.x() : pointInContents.y(), onlyEditableLeaves);
631 }
632
633 InlineBox* RootInlineBox::closestLeafChildForLogicalLeftPosition(int leftPosition, bool onlyEditableLeaves)
634 {
635     InlineBox* firstLeaf = firstLeafChild();
636     InlineBox* lastLeaf = lastLeafChild();
637
638     if (firstLeaf != lastLeaf) {
639         if (firstLeaf->isLineBreak())
640             firstLeaf = firstLeaf->nextLeafChildIgnoringLineBreak();
641         else if (lastLeaf->isLineBreak())
642             lastLeaf = lastLeaf->prevLeafChildIgnoringLineBreak();
643     }
644
645     if (firstLeaf == lastLeaf && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
646         return firstLeaf;
647
648     // Avoid returning a list marker when possible.
649     if (leftPosition <= firstLeaf->logicalLeft() && !firstLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
650         // The leftPosition coordinate is less or equal to left edge of the firstLeaf.
651         // Return it.
652         return firstLeaf;
653
654     if (leftPosition >= lastLeaf->logicalRight() && !lastLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(lastLeaf)))
655         // The leftPosition coordinate is greater or equal to right edge of the lastLeaf.
656         // Return it.
657         return lastLeaf;
658
659     InlineBox* closestLeaf = 0;
660     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChildIgnoringLineBreak()) {
661         if (!leaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(leaf))) {
662             closestLeaf = leaf;
663             if (leftPosition < leaf->logicalRight())
664                 // The x coordinate is less than the right edge of the box.
665                 // Return it.
666                 return leaf;
667         }
668     }
669
670     return closestLeaf ? closestLeaf : lastLeaf;
671 }
672
673 BidiStatus RootInlineBox::lineBreakBidiStatus() const
674
675     return BidiStatus(static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusEor), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLastStrong), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLast), m_lineBreakContext);
676 }
677
678 void RootInlineBox::setLineBreakInfo(RenderObject* obj, unsigned breakPos, const BidiStatus& status)
679 {
680     m_lineBreakObj = obj;
681     m_lineBreakPos = breakPos;
682     m_lineBreakBidiStatusEor = status.eor;
683     m_lineBreakBidiStatusLastStrong = status.lastStrong;
684     m_lineBreakBidiStatusLast = status.last;
685     m_lineBreakContext = status.context;
686 }
687
688 EllipsisBox* RootInlineBox::ellipsisBox() const
689 {
690     if (!hasEllipsisBox())
691         return 0;
692     return gEllipsisBoxMap->get(this);
693 }
694
695 void RootInlineBox::removeLineBoxFromRenderObject()
696 {
697     block()->lineBoxes()->removeLineBox(this);
698 }
699
700 void RootInlineBox::extractLineBoxFromRenderObject()
701 {
702     block()->lineBoxes()->extractLineBox(this);
703 }
704
705 void RootInlineBox::attachLineBoxToRenderObject()
706 {
707     block()->lineBoxes()->attachLineBox(this);
708 }
709
710 LayoutRect RootInlineBox::paddedLayoutOverflowRect(LayoutUnit endPadding) const
711 {
712     LayoutRect lineLayoutOverflow = layoutOverflowRect(lineTop(), lineBottom());
713     if (!endPadding)
714         return lineLayoutOverflow;
715     
716     // FIXME: Audit whether to use pixel snapped values when not using integers for layout: https://bugs.webkit.org/show_bug.cgi?id=63656
717     if (isHorizontal()) {
718         if (isLeftToRightDirection())
719             lineLayoutOverflow.shiftMaxXEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxX(), pixelSnappedLogicalRight() + endPadding));
720         else
721             lineLayoutOverflow.shiftXEdgeTo(min<LayoutUnit>(lineLayoutOverflow.x(), pixelSnappedLogicalLeft() - endPadding));
722     } else {
723         if (isLeftToRightDirection())
724             lineLayoutOverflow.shiftMaxYEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxY(), pixelSnappedLogicalRight() + endPadding));
725         else
726             lineLayoutOverflow.shiftYEdgeTo(min<LayoutUnit>(lineLayoutOverflow.y(), pixelSnappedLogicalLeft() - endPadding));
727     }
728     
729     return lineLayoutOverflow;
730 }
731
732 static void setAscentAndDescent(int& ascent, int& descent, int newAscent, int newDescent, bool& ascentDescentSet)
733 {
734     if (!ascentDescentSet) {
735         ascentDescentSet = true;
736         ascent = newAscent;
737         descent = newDescent;
738     } else {
739         ascent = max(ascent, newAscent);
740         descent = max(descent, newDescent);
741     }
742 }
743
744 void RootInlineBox::ascentAndDescentForBox(InlineBox* box, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, int& ascent, int& descent,
745                                            bool& affectsAscent, bool& affectsDescent) const
746 {
747     bool ascentDescentSet = false;
748
749     // Replaced boxes will return 0 for the line-height if line-box-contain says they are
750     // not to be included.
751     if (box->renderer()->isReplaced()) {
752         if (renderer()->style(isFirstLineStyle())->lineBoxContain() & LineBoxContainReplaced) {
753             ascent = box->baselinePosition(baselineType());
754             descent = box->lineHeight() - ascent;
755             
756             // Replaced elements always affect both the ascent and descent.
757             affectsAscent = true;
758             affectsDescent = true;
759         }
760         return;
761     }
762
763     Vector<const SimpleFontData*>* usedFonts = 0;
764     GlyphOverflow* glyphOverflow = 0;
765     if (box->isText()) {
766         GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(toInlineTextBox(box));
767         usedFonts = it == textBoxDataMap.end() ? 0 : &it->second.first;
768         glyphOverflow = it == textBoxDataMap.end() ? 0 : &it->second.second;
769     }
770         
771     bool includeLeading = includeLeadingForBox(box);
772     bool includeFont = includeFontForBox(box);
773     
774     bool setUsedFont = false;
775     bool setUsedFontWithLeading = false;
776
777     if (usedFonts && !usedFonts->isEmpty() && (includeFont || (box->renderer()->style(isFirstLineStyle())->lineHeight().isNegative() && includeLeading))) {
778         usedFonts->append(box->renderer()->style(isFirstLineStyle())->font().primaryFont());
779         for (size_t i = 0; i < usedFonts->size(); ++i) {
780             const FontMetrics& fontMetrics = usedFonts->at(i)->fontMetrics();
781             int usedFontAscent = fontMetrics.ascent(baselineType());
782             int usedFontDescent = fontMetrics.descent(baselineType());
783             int halfLeading = (fontMetrics.lineSpacing() - fontMetrics.height()) / 2;
784             int usedFontAscentAndLeading = usedFontAscent + halfLeading;
785             int usedFontDescentAndLeading = fontMetrics.lineSpacing() - usedFontAscentAndLeading;
786             if (includeFont) {
787                 setAscentAndDescent(ascent, descent, usedFontAscent, usedFontDescent, ascentDescentSet);
788                 setUsedFont = true;
789             }
790             if (includeLeading) {
791                 setAscentAndDescent(ascent, descent, usedFontAscentAndLeading, usedFontDescentAndLeading, ascentDescentSet);
792                 setUsedFontWithLeading = true;
793             }
794             if (!affectsAscent)
795                 affectsAscent = usedFontAscent - box->logicalTop() > 0;
796             if (!affectsDescent)
797                 affectsDescent = usedFontDescent + box->logicalTop() > 0;
798         }
799     }
800
801     // If leading is included for the box, then we compute that box.
802     if (includeLeading && !setUsedFontWithLeading) {
803         int ascentWithLeading = box->baselinePosition(baselineType());
804         int descentWithLeading = box->lineHeight() - ascentWithLeading;
805         setAscentAndDescent(ascent, descent, ascentWithLeading, descentWithLeading, ascentDescentSet);
806         
807         // Examine the font box for inline flows and text boxes to see if any part of it is above the baseline.
808         // If the top of our font box relative to the root box baseline is above the root box baseline, then
809         // we are contributing to the maxAscent value. Descent is similar. If any part of our font box is below
810         // the root box's baseline, then we contribute to the maxDescent value.
811         affectsAscent = ascentWithLeading - box->logicalTop() > 0;
812         affectsDescent = descentWithLeading + box->logicalTop() > 0; 
813     }
814     
815     if (includeFontForBox(box) && !setUsedFont) {
816         int fontAscent = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
817         int fontDescent = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
818         setAscentAndDescent(ascent, descent, fontAscent, fontDescent, ascentDescentSet);
819         affectsAscent = fontAscent - box->logicalTop() > 0;
820         affectsDescent = fontDescent + box->logicalTop() > 0; 
821     }
822
823     if (includeGlyphsForBox(box) && glyphOverflow && glyphOverflow->computeBounds) {
824         setAscentAndDescent(ascent, descent, glyphOverflow->top, glyphOverflow->bottom, ascentDescentSet);
825         affectsAscent = glyphOverflow->top - box->logicalTop() > 0;
826         affectsDescent = glyphOverflow->bottom + box->logicalTop() > 0; 
827         glyphOverflow->top = min(glyphOverflow->top, max(0, glyphOverflow->top - box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
828         glyphOverflow->bottom = min(glyphOverflow->bottom, max(0, glyphOverflow->bottom - box->renderer()->style(isFirstLineStyle())->fontMetrics().descent()));
829     }
830
831     if (includeMarginForBox(box)) {
832         LayoutUnit ascentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
833         LayoutUnit descentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
834         if (box->parent() && !box->renderer()->isText()) {
835             ascentWithMargin += box->boxModelObject()->borderBefore() + box->boxModelObject()->paddingBefore() + box->boxModelObject()->marginBefore();
836             descentWithMargin += box->boxModelObject()->borderAfter() + box->boxModelObject()->paddingAfter() + box->boxModelObject()->marginAfter();
837         }
838         setAscentAndDescent(ascent, descent, ascentWithMargin, descentWithMargin, ascentDescentSet);
839         
840         // Treat like a replaced element, since we're using the margin box.
841         affectsAscent = true;
842         affectsDescent = true;
843     }
844 }
845
846 LayoutUnit RootInlineBox::verticalPositionForBox(InlineBox* box, VerticalPositionCache& verticalPositionCache)
847 {
848     if (box->renderer()->isText())
849         return box->parent()->logicalTop();
850     
851     RenderBoxModelObject* renderer = box->boxModelObject();
852     ASSERT(renderer->isInline());
853     if (!renderer->isInline())
854         return 0;
855
856     // This method determines the vertical position for inline elements.
857     bool firstLine = isFirstLineStyle();
858     if (firstLine && !renderer->document()->usesFirstLineRules())
859         firstLine = false;
860
861     // Check the cache.
862     bool isRenderInline = renderer->isRenderInline();
863     if (isRenderInline && !firstLine) {
864         LayoutUnit verticalPosition = verticalPositionCache.get(renderer, baselineType());
865         if (verticalPosition != PositionUndefined)
866             return verticalPosition;
867     }
868
869     LayoutUnit verticalPosition = 0;
870     EVerticalAlign verticalAlign = renderer->style()->verticalAlign();
871     if (verticalAlign == TOP || verticalAlign == BOTTOM)
872         return 0;
873    
874     RenderObject* parent = renderer->parent();
875     if (parent->isRenderInline() && parent->style()->verticalAlign() != TOP && parent->style()->verticalAlign() != BOTTOM)
876         verticalPosition = box->parent()->logicalTop();
877     
878     if (verticalAlign != BASELINE) {
879         const Font& font = parent->style(firstLine)->font();
880         const FontMetrics& fontMetrics = font.fontMetrics();
881         int fontSize = font.pixelSize();
882
883         LineDirectionMode lineDirection = parent->isHorizontalWritingMode() ? HorizontalLine : VerticalLine;
884
885         if (verticalAlign == SUB)
886             verticalPosition += fontSize / 5 + 1;
887         else if (verticalAlign == SUPER)
888             verticalPosition -= fontSize / 3 + 1;
889         else if (verticalAlign == TEXT_TOP)
890             verticalPosition += renderer->baselinePosition(baselineType(), firstLine, lineDirection) - fontMetrics.ascent(baselineType());
891         else if (verticalAlign == MIDDLE)
892             verticalPosition = (verticalPosition - static_cast<LayoutUnit>(fontMetrics.xHeight() / 2) - renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection)).round();
893         else if (verticalAlign == TEXT_BOTTOM) {
894             verticalPosition += fontMetrics.descent(baselineType());
895             // lineHeight - baselinePosition is always 0 for replaced elements (except inline blocks), so don't bother wasting time in that case.
896             if (!renderer->isReplaced() || renderer->isInlineBlockOrInlineTable())
897                 verticalPosition -= (renderer->lineHeight(firstLine, lineDirection) - renderer->baselinePosition(baselineType(), firstLine, lineDirection));
898         } else if (verticalAlign == BASELINE_MIDDLE)
899             verticalPosition += -renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
900         else if (verticalAlign == LENGTH) {
901             LayoutUnit lineHeight;
902             //Per http://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align: 'Percentages: refer to the 'line-height' of the element itself'.
903             if (renderer->style()->verticalAlignLength().isPercent())
904                 lineHeight = renderer->style()->computedLineHeight();
905             else
906                 lineHeight = renderer->lineHeight(firstLine, lineDirection);
907             verticalPosition -= valueForLength(renderer->style()->verticalAlignLength(), lineHeight, renderer->view());
908         }
909     }
910
911     // Store the cached value.
912     if (isRenderInline && !firstLine)
913         verticalPositionCache.set(renderer, baselineType(), verticalPosition);
914
915     return verticalPosition;
916 }
917
918 bool RootInlineBox::includeLeadingForBox(InlineBox* box) const
919 {
920     if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
921         return false;
922
923     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
924     return (lineBoxContain & LineBoxContainInline) || (box == this && (lineBoxContain & LineBoxContainBlock));
925 }
926
927 bool RootInlineBox::includeFontForBox(InlineBox* box) const
928 {
929     if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
930         return false;
931     
932     if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
933         return false;
934
935     // For now map "glyphs" to "font" in vertical text mode until the bounds returned by glyphs aren't garbage.
936     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
937     return (lineBoxContain & LineBoxContainFont) || (!isHorizontal() && (lineBoxContain & LineBoxContainGlyphs));
938 }
939
940 bool RootInlineBox::includeGlyphsForBox(InlineBox* box) const
941 {
942     if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
943         return false;
944     
945     if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
946         return false;
947
948     // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
949     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
950     return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
951 }
952
953 bool RootInlineBox::includeMarginForBox(InlineBox* box) const
954 {
955     if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
956         return false;
957
958     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
959     return lineBoxContain & LineBoxContainInlineBox;
960 }
961
962
963 bool RootInlineBox::fitsToGlyphs() const
964 {
965     // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
966     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
967     return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
968 }
969
970 bool RootInlineBox::includesRootLineBoxFontOrLeading() const
971 {
972     LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
973     return (lineBoxContain & LineBoxContainBlock) || (lineBoxContain & LineBoxContainInline) || (lineBoxContain & LineBoxContainFont);
974 }
975
976 Node* RootInlineBox::getLogicalStartBoxWithNode(InlineBox*& startBox) const
977 {
978     Vector<InlineBox*> leafBoxesInLogicalOrder;
979     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
980     for (size_t i = 0; i < leafBoxesInLogicalOrder.size(); ++i) {
981         if (leafBoxesInLogicalOrder[i]->renderer()->node()) {
982             startBox = leafBoxesInLogicalOrder[i];
983             return startBox->renderer()->node();
984         }
985     }
986     startBox = 0;
987     return 0;
988 }
989     
990 Node* RootInlineBox::getLogicalEndBoxWithNode(InlineBox*& endBox) const
991 {
992     Vector<InlineBox*> leafBoxesInLogicalOrder;
993     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
994     for (size_t i = leafBoxesInLogicalOrder.size(); i > 0; --i) { 
995         if (leafBoxesInLogicalOrder[i - 1]->renderer()->node()) {
996             endBox = leafBoxesInLogicalOrder[i - 1];
997             return endBox->renderer()->node();
998         }
999     }
1000     endBox = 0;
1001     return 0;
1002 }
1003
1004 #ifndef NDEBUG
1005 const char* RootInlineBox::boxName() const
1006 {
1007     return "RootInlineBox";
1008 }
1009 #endif
1010
1011 } // namespace WebCore