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 "HTMLNames.h"
28 #include "core/dom/ElementTraversal.h"
29 #include "core/html/HTMLOListElement.h"
30 #include "core/rendering/FastTextAutosizer.h"
31 #include "core/rendering/LayoutRectRecorder.h"
32 #include "core/rendering/RenderListMarker.h"
33 #include "core/rendering/RenderView.h"
34 #include "wtf/StdLibExtras.h"
35 #include "wtf/text/StringBuilder.h"
41 using namespace HTMLNames;
43 RenderListItem::RenderListItem(Element* element)
44 : RenderBlockFlow(element)
46 , m_hasExplicitValue(false)
47 , m_isValueUpToDate(false)
53 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
55 RenderBlockFlow::styleDidChange(diff, oldStyle);
57 if (style()->listStyleType() != NoneListStyle
58 || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
59 RefPtr<RenderStyle> newStyle = RenderStyle::create();
60 // Markers update their own margin style. By copying the existing style we can
61 // avoid an unnecessary layout in setStyle below.
63 newStyle->copyNonInheritedFrom(m_marker->style());
64 // The marker always inherits from the list item, regardless of where it might end
65 // up (e.g., in some deeply nested line box). See CSS3 spec.
66 newStyle->inheritFrom(style());
68 m_marker = RenderListMarker::createAnonymous(this);
69 m_marker->setStyle(newStyle.release());
70 } else if (m_marker) {
76 void RenderListItem::willBeDestroyed()
82 RenderBlockFlow::willBeDestroyed();
85 void RenderListItem::insertedIntoTree()
87 RenderBlockFlow::insertedIntoTree();
89 updateListMarkerNumbers();
92 void RenderListItem::willBeRemovedFromTree()
94 RenderBlockFlow::willBeRemovedFromTree();
96 updateListMarkerNumbers();
99 static bool isList(const Node* node)
101 return (node->hasTagName(ulTag) || node->hasTagName(olTag));
104 // Returns the enclosing list with respect to the DOM order.
105 static Node* enclosingList(const RenderListItem* listItem)
107 Node* listItemNode = listItem->node();
109 // We use parentNode because the enclosing list could be a ShadowRoot that's not Element.
110 for (Node* parent = listItemNode->parentNode(); parent; parent = parent->parentNode()) {
117 // If there's no actual <ul> or <ol> list element, then the first found
118 // node acts as our list for purposes of determining what other list items
119 // should be numbered as part of the same list.
123 // Returns the next list item with respect to the DOM order.
124 static RenderListItem* nextListItem(const Node* listNode, const RenderListItem* item = 0)
129 const Node* current = item ? item->node() : listNode;
131 current = ElementTraversal::nextIncludingPseudo(*current, listNode);
134 if (isList(current)) {
135 // We've found a nested, independent list: nothing to do here.
136 current = ElementTraversal::nextIncludingPseudoSkippingChildren(*current, listNode);
140 RenderObject* renderer = current->renderer();
141 if (renderer && renderer->isListItem())
142 return toRenderListItem(renderer);
144 // FIXME: Can this be optimized to skip the children of the elements without a renderer?
145 current = ElementTraversal::nextIncludingPseudo(*current, listNode);
151 // Returns the previous list item with respect to the DOM order.
152 static RenderListItem* previousListItem(const Node* listNode, const RenderListItem* item)
154 Node* current = item->node();
156 for (current = ElementTraversal::previousIncludingPseudo(*current, listNode); current; current = ElementTraversal::previousIncludingPseudo(*current, listNode)) {
157 RenderObject* renderer = current->renderer();
158 if (!renderer || (renderer && !renderer->isListItem()))
160 Node* otherList = enclosingList(toRenderListItem(renderer));
161 // This item is part of our current list, so it's what we're looking for.
162 if (listNode == otherList)
163 return toRenderListItem(renderer);
164 // We found ourself inside another list; lets skip the rest of it.
165 // Use nextIncludingPseudo() here because the other list itself may actually
166 // be a list item itself. We need to examine it, so we do this to counteract
167 // the previousIncludingPseudo() that will be done by the loop.
169 current = ElementTraversal::nextIncludingPseudo(*otherList);
174 void RenderListItem::updateItemValuesForOrderedList(const HTMLOListElement* listNode)
178 for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
179 listItem->updateValue();
182 unsigned RenderListItem::itemCountForOrderedList(const HTMLOListElement* listNode)
186 unsigned itemCount = 0;
187 for (RenderListItem* listItem = nextListItem(listNode); listItem; listItem = nextListItem(listNode, listItem))
193 inline int RenderListItem::calcValue() const
195 if (m_hasExplicitValue)
196 return m_explicitValue;
198 Node* list = enclosingList(this);
199 HTMLOListElement* oListElement = (list && list->hasTagName(olTag)) ? toHTMLOListElement(list) : 0;
201 if (oListElement && oListElement->isReversed())
204 // FIXME: This recurses to a possible depth of the length of the list.
205 // That's not good -- we need to change this to an iterative algorithm.
206 if (RenderListItem* previousItem = previousListItem(list, this))
207 return previousItem->value() + valueStep;
210 return oListElement->start();
215 void RenderListItem::updateValueNow() const
217 m_value = calcValue();
218 m_isValueUpToDate = true;
221 bool RenderListItem::isEmpty() const
223 return lastChild() == m_marker;
226 static RenderObject* getParentOfFirstLineBox(RenderBlockFlow* curr, RenderObject* marker)
228 RenderObject* firstChild = curr->firstChild();
232 bool inQuirksMode = curr->document().inQuirksMode();
233 for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
234 if (currChild == marker)
237 if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
240 if (currChild->isFloating() || currChild->isOutOfFlowPositioned())
243 if (!currChild->isRenderBlockFlow() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
246 if (curr->isListItem() && inQuirksMode && currChild->node() &&
247 (currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
250 RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlockFlow(currChild), marker);
258 void RenderListItem::updateValue()
260 if (!m_hasExplicitValue) {
261 m_isValueUpToDate = false;
263 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
267 static RenderObject* firstNonMarkerChild(RenderObject* parent)
269 RenderObject* result = parent->firstChild();
270 while (result && result->isListMarker())
271 result = result->nextSibling();
275 void RenderListItem::updateMarkerLocation()
277 // Sanity check the location of our marker.
279 RenderObject* markerParent = m_marker->parent();
280 RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
281 if (!lineBoxParent) {
282 // If the marker is currently contained inside an anonymous box,
283 // then we are the only item in that anonymous box (since no line box
284 // parent was found). It's ok to just leave the marker where it is
286 if (markerParent && markerParent->isAnonymousBlock())
287 lineBoxParent = markerParent;
289 lineBoxParent = this;
292 if (markerParent != lineBoxParent || m_marker->preferredLogicalWidthsDirty()) {
293 // Removing and adding the marker can trigger repainting in
294 // containers other than ourselves, so we need to disable LayoutState.
295 LayoutStateDisabler layoutStateDisabler(view());
299 markerParent->dirtyLinesFromChangedChild(m_marker);
301 lineBoxParent = this;
302 lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
303 m_marker->updateMarginsAndContent();
304 // If markerParent is an anonymous block that has lost all its children, destroy it.
305 if (markerParent && markerParent->isAnonymousBlock() && !markerParent->firstChild() && !toRenderBlock(markerParent)->continuation())
306 markerParent->destroy();
308 // If the marker is inside we need to redo the preferred width calculations
309 // as the size of the item now includes the size of the list marker.
310 if (m_marker->isInside())
311 containingBlock()->updateLogicalWidth();
316 void RenderListItem::layout()
318 ASSERT(needsLayout());
320 // The marker must be autosized before calling updateMarkerLocation.
321 // It cannot be done in the parent's beginLayout because it is not yet in the render tree.
323 FastTextAutosizer* textAutosizer = document().fastTextAutosizer();
325 textAutosizer->inflateListItem(this, m_marker);
328 LayoutRectRecorder recorder(*this);
329 updateMarkerLocation();
330 RenderBlockFlow::layout();
333 void RenderListItem::addOverflowFromChildren()
335 RenderBlockFlow::addOverflowFromChildren();
336 positionListMarker();
339 void RenderListItem::positionListMarker()
341 if (m_marker && m_marker->parent()->isBox() && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
342 LayoutUnit markerOldLogicalLeft = m_marker->logicalLeft();
343 LayoutUnit blockOffset = 0;
344 LayoutUnit lineOffset = 0;
345 for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
346 blockOffset += o->logicalTop();
347 lineOffset += o->logicalLeft();
350 bool adjustOverflow = false;
351 LayoutUnit markerLogicalLeft;
352 RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
353 bool hitSelfPaintingLayer = false;
355 RootInlineBox* rootBox = m_marker->inlineBoxWrapper()->root();
356 LayoutUnit lineTop = rootBox->lineTop();
357 LayoutUnit lineBottom = rootBox->lineBottom();
359 // FIXME: Need to account for relative positioning in the layout overflow.
360 if (style()->isLeftToRightDirection()) {
361 LayoutUnit leftLineOffset = logicalLeftOffsetForLine(blockOffset, logicalLeftOffsetForLine(blockOffset, false), false);
362 markerLogicalLeft = leftLineOffset - lineOffset - paddingStart() - borderStart() + m_marker->marginStart();
363 m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
364 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
365 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
366 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
367 if (markerLogicalLeft < newLogicalVisualOverflowRect.x() && !hitSelfPaintingLayer) {
368 newLogicalVisualOverflowRect.setWidth(newLogicalVisualOverflowRect.maxX() - markerLogicalLeft);
369 newLogicalVisualOverflowRect.setX(markerLogicalLeft);
371 adjustOverflow = true;
373 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
374 newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
375 newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
377 adjustOverflow = true;
379 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
380 if (box->boxModelObject()->hasSelfPaintingLayer())
381 hitSelfPaintingLayer = true;
384 LayoutUnit rightLineOffset = logicalRightOffsetForLine(blockOffset, logicalRightOffsetForLine(blockOffset, false), false);
385 markerLogicalLeft = rightLineOffset - lineOffset + paddingStart() + borderStart() + m_marker->marginEnd();
386 m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
387 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
388 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
389 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
390 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalVisualOverflowRect.maxX() && !hitSelfPaintingLayer) {
391 newLogicalVisualOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalVisualOverflowRect.x());
393 adjustOverflow = true;
395 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
396 newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
398 adjustOverflow = true;
400 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
402 if (box->boxModelObject()->hasSelfPaintingLayer())
403 hitSelfPaintingLayer = true;
407 if (adjustOverflow) {
408 LayoutRect markerRect(markerLogicalLeft + lineOffset, blockOffset, m_marker->width(), m_marker->height());
409 if (!style()->isHorizontalWritingMode())
410 markerRect = markerRect.transposedRect();
411 RenderBox* o = m_marker;
412 bool propagateVisualOverflow = true;
413 bool propagateLayoutOverflow = true;
416 if (o->isRenderBlock()) {
417 if (propagateVisualOverflow)
418 toRenderBlock(o)->addContentsVisualOverflow(markerRect);
419 if (propagateLayoutOverflow)
420 toRenderBlock(o)->addLayoutOverflow(markerRect);
422 if (o->hasOverflowClip()) {
423 propagateLayoutOverflow = false;
424 propagateVisualOverflow = false;
426 if (o->hasSelfPaintingLayer())
427 propagateVisualOverflow = false;
428 markerRect.moveBy(-o->location());
429 } while (o != this && propagateVisualOverflow && propagateLayoutOverflow);
434 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
436 if (!logicalHeight() && hasOverflowClip())
439 RenderBlockFlow::paint(paintInfo, paintOffset);
442 const String& RenderListItem::markerText() const
445 return m_marker->text();
446 return nullAtom.string();
449 String RenderListItem::markerTextWithSuffix() const
454 // Append the suffix for the marker in the right place depending
455 // on the direction of the text (right-to-left or left-to-right).
457 const String& markerText = m_marker->text();
458 const String markerSuffix = m_marker->suffix();
459 StringBuilder result;
461 if (!m_marker->style()->isLeftToRightDirection())
462 result.append(markerSuffix);
464 result.append(markerText);
466 if (m_marker->style()->isLeftToRightDirection())
467 result.append(markerSuffix);
469 return result.toString();
472 void RenderListItem::explicitValueChanged()
475 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
476 Node* listNode = enclosingList(this);
477 for (RenderListItem* item = this; item; item = nextListItem(listNode, item))
481 void RenderListItem::setExplicitValue(int value)
485 if (m_hasExplicitValue && m_explicitValue == value)
487 m_explicitValue = value;
489 m_hasExplicitValue = true;
490 explicitValueChanged();
493 void RenderListItem::clearExplicitValue()
497 if (!m_hasExplicitValue)
499 m_hasExplicitValue = false;
500 m_isValueUpToDate = false;
501 explicitValueChanged();
504 static RenderListItem* previousOrNextItem(bool isListReversed, Node* list, RenderListItem* item)
506 return isListReversed ? previousListItem(list, item) : nextListItem(list, item);
509 void RenderListItem::updateListMarkerNumbers()
511 Node* listNode = enclosingList(this);
512 // The list node can be the shadow root which has no renderer.
517 bool isListReversed = false;
518 HTMLOListElement* oListElement = (listNode && listNode->hasTagName(olTag)) ? toHTMLOListElement(listNode) : 0;
520 oListElement->itemCountChanged();
521 isListReversed = oListElement->isReversed();
523 for (RenderListItem* item = previousOrNextItem(isListReversed, listNode, this); item; item = previousOrNextItem(isListReversed, listNode, item)) {
524 if (!item->m_isValueUpToDate) {
525 // If an item has been marked for update before, we can safely
526 // assume that all the following ones have too.
527 // This gives us the opportunity to stop here and avoid
528 // marking the same nodes again.
535 } // namespace WebCore