Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderListItem.cpp
1 /**
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
5  * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "core/rendering/RenderListItem.h"
26
27 #include "core/HTMLNames.h"
28 #include "core/dom/NodeRenderingTraversal.h"
29 #include "core/html/HTMLOListElement.h"
30 #include "core/rendering/RenderListMarker.h"
31 #include "core/rendering/RenderView.h"
32 #include "core/rendering/TextAutosizer.h"
33 #include "wtf/StdLibExtras.h"
34 #include "wtf/text/StringBuilder.h"
35
36 namespace blink {
37
38 using namespace HTMLNames;
39
40 RenderListItem::RenderListItem(Element* element)
41     : RenderBlockFlow(element)
42     , m_marker(nullptr)
43     , m_hasExplicitValue(false)
44     , m_isValueUpToDate(false)
45     , m_notInList(false)
46 {
47     setInline(false);
48 }
49
50 void RenderListItem::trace(Visitor* visitor)
51 {
52     visitor->trace(m_marker);
53     RenderBlockFlow::trace(visitor);
54 }
55
56 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
57 {
58     RenderBlockFlow::styleDidChange(diff, oldStyle);
59
60     if (style()->listStyleType() != NoneListStyle
61         || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
62         if (!m_marker)
63             m_marker = RenderListMarker::createAnonymous(this);
64         m_marker->listItemStyleDidChange();
65     } else if (m_marker) {
66         m_marker->destroy();
67         m_marker = nullptr;
68     }
69 }
70
71 void RenderListItem::willBeDestroyed()
72 {
73     if (m_marker) {
74         m_marker->destroy();
75         m_marker = nullptr;
76     }
77     RenderBlockFlow::willBeDestroyed();
78 }
79
80 void RenderListItem::insertedIntoTree()
81 {
82     RenderBlockFlow::insertedIntoTree();
83
84     updateListMarkerNumbers();
85 }
86
87 void RenderListItem::willBeRemovedFromTree()
88 {
89     RenderBlockFlow::willBeRemovedFromTree();
90
91     updateListMarkerNumbers();
92 }
93
94 static bool isList(const Node& node)
95 {
96     return isHTMLUListElement(node) || isHTMLOListElement(node);
97 }
98
99 // Returns the enclosing list with respect to the DOM order.
100 static Node* enclosingList(const RenderListItem* listItem)
101 {
102     Node* listItemNode = listItem->node();
103     Node* firstNode = 0;
104     // We use parentNode because the enclosing list could be a ShadowRoot that's not Element.
105     for (Node* parent = NodeRenderingTraversal::parent(listItemNode); parent; parent = NodeRenderingTraversal::parent(parent)) {
106         if (isList(*parent))
107             return parent;
108         if (!firstNode)
109             firstNode = parent;
110     }
111
112     // If there's no actual <ul> or <ol> list element, then the first found
113     // node acts as our list for purposes of determining what other list items
114     // should be numbered as part of the same list.
115     return firstNode;
116 }
117
118 // Returns the next list item with respect to the DOM order.
119 static RenderListItem* nextListItem(const Node* listNode, const RenderListItem* item = 0)
120 {
121     if (!listNode)
122         return 0;
123
124     const Node* current = item ? item->node() : listNode;
125     ASSERT(current);
126     ASSERT(!current->document().childNeedsDistributionRecalc());
127     current = NodeRenderingTraversal::next(current, listNode);
128
129     while (current) {
130         if (isList(*current)) {
131             // We've found a nested, independent list: nothing to do here.
132             current = NodeRenderingTraversal::next(current, listNode);
133             continue;
134         }
135
136         RenderObject* renderer = current->renderer();
137         if (renderer && renderer->isListItem())
138             return toRenderListItem(renderer);
139
140         // FIXME: Can this be optimized to skip the children of the elements without a renderer?
141         current = NodeRenderingTraversal::next(current, listNode);
142     }
143
144     return 0;
145 }
146
147 // Returns the previous list item with respect to the DOM order.
148 static RenderListItem* previousListItem(const Node* listNode, const RenderListItem* item)
149 {
150     Node* current = item->node();
151     ASSERT(current);
152     ASSERT(!current->document().childNeedsDistributionRecalc());
153     for (current = NodeRenderingTraversal::previous(current, listNode); current && current != listNode; current = NodeRenderingTraversal::previous(current, listNode)) {
154         RenderObject* renderer = current->renderer();
155         if (!renderer || (renderer && !renderer->isListItem()))
156             continue;
157         Node* otherList = enclosingList(toRenderListItem(renderer));
158         // This item is part of our current list, so it's what we're looking for.
159         if (listNode == otherList)
160             return toRenderListItem(renderer);
161         // We found ourself inside another list; lets skip the rest of it.
162         // Use nextIncludingPseudo() here because the other list itself may actually
163         // be a list item itself. We need to examine it, so we do this to counteract
164         // the previousIncludingPseudo() that will be done by the loop.
165         if (otherList)
166             current = NodeRenderingTraversal::next(otherList, listNode);
167     }
168     return 0;
169 }
170
171 void RenderListItem::updateItemValuesForOrderedList(const HTMLOListElement* listNode)
172 {
173     ASSERT(listNode);
174
175     for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
176         listItem->updateValue();
177 }
178
179 unsigned RenderListItem::itemCountForOrderedList(const HTMLOListElement* listNode)
180 {
181     ASSERT(listNode);
182
183     unsigned itemCount = 0;
184     for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
185         itemCount++;
186
187     return itemCount;
188 }
189
190 inline int RenderListItem::calcValue() const
191 {
192     if (m_hasExplicitValue)
193         return m_explicitValue;
194
195     Node* list = enclosingList(this);
196     HTMLOListElement* oListElement = isHTMLOListElement(list) ? toHTMLOListElement(list) : 0;
197     int valueStep = 1;
198     if (oListElement && oListElement->isReversed())
199         valueStep = -1;
200
201     // FIXME: This recurses to a possible depth of the length of the list.
202     // That's not good -- we need to change this to an iterative algorithm.
203     if (RenderListItem* previousItem = previousListItem(list, this))
204         return previousItem->value() + valueStep;
205
206     if (oListElement)
207         return oListElement->start();
208
209     return 1;
210 }
211
212 void RenderListItem::updateValueNow() const
213 {
214     m_value = calcValue();
215     m_isValueUpToDate = true;
216 }
217
218 bool RenderListItem::isEmpty() const
219 {
220     return lastChild() == m_marker;
221 }
222
223 static RenderObject* getParentOfFirstLineBox(RenderBlockFlow* curr, RenderObject* marker)
224 {
225     RenderObject* firstChild = curr->firstChild();
226     if (!firstChild)
227         return 0;
228
229     bool inQuirksMode = curr->document().inQuirksMode();
230     for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
231         if (currChild == marker)
232             continue;
233
234         if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
235             return curr;
236
237         if (currChild->isFloating() || currChild->isOutOfFlowPositioned())
238             continue;
239
240         if (!currChild->isRenderBlockFlow() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
241             break;
242
243         if (curr->isListItem() && inQuirksMode && currChild->node() &&
244             (isHTMLUListElement(*currChild->node()) || isHTMLOListElement(*currChild->node())))
245             break;
246
247         RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlockFlow(currChild), marker);
248         if (lineBox)
249             return lineBox;
250     }
251
252     return 0;
253 }
254
255 void RenderListItem::updateValue()
256 {
257     if (!m_hasExplicitValue) {
258         m_isValueUpToDate = false;
259         if (m_marker)
260             m_marker->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
261     }
262 }
263
264 static RenderObject* firstNonMarkerChild(RenderObject* parent)
265 {
266     RenderObject* result = parent->slowFirstChild();
267     while (result && result->isListMarker())
268         result = result->nextSibling();
269     return result;
270 }
271
272 void RenderListItem::updateMarkerLocationAndInvalidateWidth()
273 {
274     ASSERT(m_marker);
275
276     // FIXME: We should not modify the structure of the render tree
277     // during layout. crbug.com/370461
278     DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
279     if (updateMarkerLocation()) {
280         // If the marker is inside we need to redo the preferred width calculations
281         // as the size of the item now includes the size of the list marker.
282         if (m_marker->isInside())
283             containingBlock()->updateLogicalWidth();
284     }
285 }
286
287 bool RenderListItem::updateMarkerLocation()
288 {
289     ASSERT(m_marker);
290     RenderObject* markerParent = m_marker->parent();
291     RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
292     if (!lineBoxParent) {
293         // If the marker is currently contained inside an anonymous box, then we
294         // are the only item in that anonymous box (since no line box parent was
295         // found). It's ok to just leave the marker where it is in this case.
296         if (markerParent && markerParent->isAnonymousBlock())
297             lineBoxParent = markerParent;
298         else
299             lineBoxParent = this;
300     }
301
302     if (markerParent != lineBoxParent) {
303         m_marker->remove();
304         lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
305         m_marker->updateMarginsAndContent();
306         // If markerParent is an anonymous block with no children, destroy it.
307         if (markerParent && markerParent->isAnonymousBlock() && !toRenderBlock(markerParent)->firstChild() && !toRenderBlock(markerParent)->continuation())
308             markerParent->destroy();
309         return true;
310     }
311
312     return false;
313 }
314
315 void RenderListItem::layout()
316 {
317     ASSERT(needsLayout());
318
319     if (m_marker) {
320         // The marker must be autosized before calling
321         // updateMarkerLocationAndInvalidateWidth. It cannot be done in the
322         // parent's beginLayout because it is not yet in the render tree.
323         if (TextAutosizer* textAutosizer = document().textAutosizer())
324             textAutosizer->inflateListItem(this, m_marker);
325
326         updateMarkerLocationAndInvalidateWidth();
327     }
328
329     RenderBlockFlow::layout();
330 }
331
332 void RenderListItem::addOverflowFromChildren()
333 {
334     RenderBlockFlow::addOverflowFromChildren();
335     positionListMarker();
336 }
337
338 void RenderListItem::positionListMarker()
339 {
340     if (m_marker && m_marker->parent()->isBox() && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
341         LayoutUnit markerOldLogicalLeft = m_marker->logicalLeft();
342         LayoutUnit blockOffset = 0;
343         LayoutUnit lineOffset = 0;
344         for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
345             blockOffset += o->logicalTop();
346             lineOffset += o->logicalLeft();
347         }
348
349         bool adjustOverflow = false;
350         LayoutUnit markerLogicalLeft;
351         RootInlineBox& root = m_marker->inlineBoxWrapper()->root();
352         bool hitSelfPaintingLayer = false;
353
354         LayoutUnit lineTop = root.lineTop();
355         LayoutUnit lineBottom = root.lineBottom();
356
357         // FIXME: Need to account for relative positioning in the layout overflow.
358         if (style()->isLeftToRightDirection()) {
359             LayoutUnit leftLineOffset = logicalLeftOffsetForLine(blockOffset, logicalLeftOffsetForLine(blockOffset, false), false);
360             markerLogicalLeft = leftLineOffset - lineOffset - paddingStart() - borderStart() + m_marker->marginStart();
361             m_marker->inlineBoxWrapper()->adjustLineDirectionPosition((markerLogicalLeft - markerOldLogicalLeft).toFloat());
362             for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
363                 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
364                 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
365                 if (markerLogicalLeft < newLogicalVisualOverflowRect.x() && !hitSelfPaintingLayer) {
366                     newLogicalVisualOverflowRect.setWidth(newLogicalVisualOverflowRect.maxX() - markerLogicalLeft);
367                     newLogicalVisualOverflowRect.setX(markerLogicalLeft);
368                     if (box == root)
369                         adjustOverflow = true;
370                 }
371                 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
372                     newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
373                     newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
374                     if (box == root)
375                         adjustOverflow = true;
376                 }
377                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
378                 if (box->boxModelObject()->hasSelfPaintingLayer())
379                     hitSelfPaintingLayer = true;
380             }
381         } else {
382             LayoutUnit rightLineOffset = logicalRightOffsetForLine(blockOffset, logicalRightOffsetForLine(blockOffset, false), false);
383             markerLogicalLeft = rightLineOffset - lineOffset + paddingStart() + borderStart() + m_marker->marginEnd();
384             m_marker->inlineBoxWrapper()->adjustLineDirectionPosition((markerLogicalLeft - markerOldLogicalLeft).toFloat());
385             for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
386                 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
387                 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
388                 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalVisualOverflowRect.maxX() && !hitSelfPaintingLayer) {
389                     newLogicalVisualOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalVisualOverflowRect.x());
390                     if (box == root)
391                         adjustOverflow = true;
392                 }
393                 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
394                     newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
395                     if (box == root)
396                         adjustOverflow = true;
397                 }
398                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
399
400                 if (box->boxModelObject()->hasSelfPaintingLayer())
401                     hitSelfPaintingLayer = true;
402             }
403         }
404
405         if (adjustOverflow) {
406             LayoutRect markerRect(markerLogicalLeft + lineOffset, blockOffset, m_marker->width(), m_marker->height());
407             if (!style()->isHorizontalWritingMode())
408                 markerRect = markerRect.transposedRect();
409             RenderBox* o = m_marker;
410             bool propagateVisualOverflow = true;
411             bool propagateLayoutOverflow = true;
412             do {
413                 o = o->parentBox();
414                 if (o->isRenderBlock()) {
415                     if (propagateVisualOverflow)
416                         toRenderBlock(o)->addContentsVisualOverflow(markerRect);
417                     if (propagateLayoutOverflow)
418                         toRenderBlock(o)->addLayoutOverflow(markerRect);
419                 }
420                 if (o->hasOverflowClip()) {
421                     propagateLayoutOverflow = false;
422                     propagateVisualOverflow = false;
423                 }
424                 if (o->hasSelfPaintingLayer())
425                     propagateVisualOverflow = false;
426                 markerRect.moveBy(-o->location());
427             } while (o != this && propagateVisualOverflow && propagateLayoutOverflow);
428         }
429     }
430 }
431
432 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
433 {
434     if (!logicalHeight() && hasOverflowClip())
435         return;
436
437     RenderBlockFlow::paint(paintInfo, paintOffset);
438 }
439
440 const String& RenderListItem::markerText() const
441 {
442     if (m_marker)
443         return m_marker->text();
444     return nullAtom.string();
445 }
446
447 void RenderListItem::explicitValueChanged()
448 {
449     if (m_marker)
450         m_marker->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
451     Node* listNode = enclosingList(this);
452     for (RenderListItem* item = this; item; item = nextListItem(listNode, item))
453         item->updateValue();
454 }
455
456 void RenderListItem::setExplicitValue(int value)
457 {
458     ASSERT(node());
459
460     if (m_hasExplicitValue && m_explicitValue == value)
461         return;
462     m_explicitValue = value;
463     m_value = value;
464     m_hasExplicitValue = true;
465     explicitValueChanged();
466 }
467
468 void RenderListItem::clearExplicitValue()
469 {
470     ASSERT(node());
471
472     if (!m_hasExplicitValue)
473         return;
474     m_hasExplicitValue = false;
475     m_isValueUpToDate = false;
476     explicitValueChanged();
477 }
478
479 void RenderListItem::setNotInList(bool notInList)
480 {
481     m_notInList = notInList;
482     if (m_marker)
483         updateMarkerLocation();
484 }
485
486 static RenderListItem* previousOrNextItem(bool isListReversed, Node* list, RenderListItem* item)
487 {
488     return isListReversed ? previousListItem(list, item) : nextListItem(list, item);
489 }
490
491 void RenderListItem::updateListMarkerNumbers()
492 {
493     // If distribution recalc is needed, updateListMarkerNumber will be re-invoked
494     // after distribution is calculated.
495     if (node()->document().childNeedsDistributionRecalc())
496         return;
497
498     Node* listNode = enclosingList(this);
499     ASSERT(listNode);
500
501     bool isListReversed = false;
502     HTMLOListElement* oListElement = isHTMLOListElement(listNode) ? toHTMLOListElement(listNode) : 0;
503     if (oListElement) {
504         oListElement->itemCountChanged();
505         isListReversed = oListElement->isReversed();
506     }
507
508     // FIXME: The n^2 protection below doesn't help if the elements were inserted after the
509     // the list had already been displayed.
510
511     // Avoid an O(n^2) walk over the children below when they're all known to be attaching.
512     if (listNode->needsAttach())
513         return;
514
515     for (RenderListItem* item = previousOrNextItem(isListReversed, listNode, this); item; item = previousOrNextItem(isListReversed, listNode, item)) {
516         if (!item->m_isValueUpToDate) {
517             // If an item has been marked for update before, we can safely
518             // assume that all the following ones have too.
519             // This gives us the opportunity to stop here and avoid
520             // marking the same nodes again.
521             break;
522         }
523         item->updateValue();
524     }
525 }
526
527 } // namespace blink