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)
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.
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.
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.
25 #include "core/rendering/RenderListItem.h"
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"
38 using namespace HTMLNames;
40 RenderListItem::RenderListItem(Element* element)
41 : RenderBlockFlow(element)
43 , m_hasExplicitValue(false)
44 , m_isValueUpToDate(false)
50 void RenderListItem::trace(Visitor* visitor)
52 visitor->trace(m_marker);
53 RenderBlockFlow::trace(visitor);
56 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
58 RenderBlockFlow::styleDidChange(diff, oldStyle);
60 if (style()->listStyleType() != NoneListStyle
61 || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
63 m_marker = RenderListMarker::createAnonymous(this);
64 m_marker->listItemStyleDidChange();
65 } else if (m_marker) {
71 void RenderListItem::willBeDestroyed()
77 RenderBlockFlow::willBeDestroyed();
80 void RenderListItem::insertedIntoTree()
82 RenderBlockFlow::insertedIntoTree();
84 updateListMarkerNumbers();
87 void RenderListItem::willBeRemovedFromTree()
89 RenderBlockFlow::willBeRemovedFromTree();
91 updateListMarkerNumbers();
94 static bool isList(const Node& node)
96 return isHTMLUListElement(node) || isHTMLOListElement(node);
99 // Returns the enclosing list with respect to the DOM order.
100 static Node* enclosingList(const RenderListItem* listItem)
102 Node* listItemNode = listItem->node();
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)) {
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.
118 // Returns the next list item with respect to the DOM order.
119 static RenderListItem* nextListItem(const Node* listNode, const RenderListItem* item = 0)
124 const Node* current = item ? item->node() : listNode;
126 ASSERT(!current->document().childNeedsDistributionRecalc());
127 current = NodeRenderingTraversal::next(current, listNode);
130 if (isList(*current)) {
131 // We've found a nested, independent list: nothing to do here.
132 current = NodeRenderingTraversal::next(current, listNode);
136 RenderObject* renderer = current->renderer();
137 if (renderer && renderer->isListItem())
138 return toRenderListItem(renderer);
140 // FIXME: Can this be optimized to skip the children of the elements without a renderer?
141 current = NodeRenderingTraversal::next(current, listNode);
147 // Returns the previous list item with respect to the DOM order.
148 static RenderListItem* previousListItem(const Node* listNode, const RenderListItem* item)
150 Node* current = item->node();
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()))
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.
166 current = NodeRenderingTraversal::next(otherList, listNode);
171 void RenderListItem::updateItemValuesForOrderedList(const HTMLOListElement* listNode)
175 for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
176 listItem->updateValue();
179 unsigned RenderListItem::itemCountForOrderedList(const HTMLOListElement* listNode)
183 unsigned itemCount = 0;
184 for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
190 inline int RenderListItem::calcValue() const
192 if (m_hasExplicitValue)
193 return m_explicitValue;
195 Node* list = enclosingList(this);
196 HTMLOListElement* oListElement = isHTMLOListElement(list) ? toHTMLOListElement(list) : 0;
198 if (oListElement && oListElement->isReversed())
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;
207 return oListElement->start();
212 void RenderListItem::updateValueNow() const
214 m_value = calcValue();
215 m_isValueUpToDate = true;
218 bool RenderListItem::isEmpty() const
220 return lastChild() == m_marker;
223 static RenderObject* getParentOfFirstLineBox(RenderBlockFlow* curr, RenderObject* marker)
225 RenderObject* firstChild = curr->firstChild();
229 bool inQuirksMode = curr->document().inQuirksMode();
230 for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
231 if (currChild == marker)
234 if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
237 if (currChild->isFloating() || currChild->isOutOfFlowPositioned())
240 if (!currChild->isRenderBlockFlow() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
243 if (curr->isListItem() && inQuirksMode && currChild->node() &&
244 (isHTMLUListElement(*currChild->node()) || isHTMLOListElement(*currChild->node())))
247 RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlockFlow(currChild), marker);
255 void RenderListItem::updateValue()
257 if (!m_hasExplicitValue) {
258 m_isValueUpToDate = false;
260 m_marker->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
264 static RenderObject* firstNonMarkerChild(RenderObject* parent)
266 RenderObject* result = parent->slowFirstChild();
267 while (result && result->isListMarker())
268 result = result->nextSibling();
272 void RenderListItem::updateMarkerLocationAndInvalidateWidth()
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();
287 bool RenderListItem::updateMarkerLocation()
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;
299 lineBoxParent = this;
302 if (markerParent != lineBoxParent) {
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();
315 void RenderListItem::layout()
317 ASSERT(needsLayout());
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);
326 updateMarkerLocationAndInvalidateWidth();
329 RenderBlockFlow::layout();
332 void RenderListItem::addOverflowFromChildren()
334 RenderBlockFlow::addOverflowFromChildren();
335 positionListMarker();
338 void RenderListItem::positionListMarker()
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();
349 bool adjustOverflow = false;
350 LayoutUnit markerLogicalLeft;
351 RootInlineBox& root = m_marker->inlineBoxWrapper()->root();
352 bool hitSelfPaintingLayer = false;
354 LayoutUnit lineTop = root.lineTop();
355 LayoutUnit lineBottom = root.lineBottom();
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);
369 adjustOverflow = true;
371 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
372 newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
373 newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
375 adjustOverflow = true;
377 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
378 if (box->boxModelObject()->hasSelfPaintingLayer())
379 hitSelfPaintingLayer = true;
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());
391 adjustOverflow = true;
393 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
394 newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
396 adjustOverflow = true;
398 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
400 if (box->boxModelObject()->hasSelfPaintingLayer())
401 hitSelfPaintingLayer = true;
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;
414 if (o->isRenderBlock()) {
415 if (propagateVisualOverflow)
416 toRenderBlock(o)->addContentsVisualOverflow(markerRect);
417 if (propagateLayoutOverflow)
418 toRenderBlock(o)->addLayoutOverflow(markerRect);
420 if (o->hasOverflowClip()) {
421 propagateLayoutOverflow = false;
422 propagateVisualOverflow = false;
424 if (o->hasSelfPaintingLayer())
425 propagateVisualOverflow = false;
426 markerRect.moveBy(-o->location());
427 } while (o != this && propagateVisualOverflow && propagateLayoutOverflow);
432 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
434 if (!logicalHeight() && hasOverflowClip())
437 RenderBlockFlow::paint(paintInfo, paintOffset);
440 const String& RenderListItem::markerText() const
443 return m_marker->text();
444 return nullAtom.string();
447 void RenderListItem::explicitValueChanged()
450 m_marker->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
451 Node* listNode = enclosingList(this);
452 for (RenderListItem* item = this; item; item = nextListItem(listNode, item))
456 void RenderListItem::setExplicitValue(int value)
460 if (m_hasExplicitValue && m_explicitValue == value)
462 m_explicitValue = value;
464 m_hasExplicitValue = true;
465 explicitValueChanged();
468 void RenderListItem::clearExplicitValue()
472 if (!m_hasExplicitValue)
474 m_hasExplicitValue = false;
475 m_isValueUpToDate = false;
476 explicitValueChanged();
479 void RenderListItem::setNotInList(bool notInList)
481 m_notInList = notInList;
483 updateMarkerLocation();
486 static RenderListItem* previousOrNextItem(bool isListReversed, Node* list, RenderListItem* item)
488 return isListReversed ? previousListItem(list, item) : nextListItem(list, item);
491 void RenderListItem::updateListMarkerNumbers()
493 // If distribution recalc is needed, updateListMarkerNumber will be re-invoked
494 // after distribution is calculated.
495 if (node()->document().childNeedsDistributionRecalc())
498 Node* listNode = enclosingList(this);
501 bool isListReversed = false;
502 HTMLOListElement* oListElement = isHTMLOListElement(listNode) ? toHTMLOListElement(listNode) : 0;
504 oListElement->itemCountChanged();
505 isListReversed = oListElement->isReversed();
508 // FIXME: The n^2 protection below doesn't help if the elements were inserted after the
509 // the list had already been displayed.
511 // Avoid an O(n^2) walk over the children below when they're all known to be attaching.
512 if (listNode->needsAttach())
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.