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 "RenderListItem.h"
27 #include "CachedImage.h"
28 #include "HTMLNames.h"
29 #include "HTMLOListElement.h"
30 #include "RenderListMarker.h"
31 #include "RenderView.h"
32 #include <wtf/StdLibExtras.h>
33 #include <wtf/text/StringBuilder.h>
39 using namespace HTMLNames;
41 RenderListItem::RenderListItem(Node* node)
44 , m_hasExplicitValue(false)
45 , m_isValueUpToDate(false)
51 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
53 RenderBlock::styleDidChange(diff, oldStyle);
55 if (style()->listStyleType() != NoneListStyle
56 || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
57 RefPtr<RenderStyle> newStyle = RenderStyle::create();
58 // The marker always inherits from the list item, regardless of where it might end
59 // up (e.g., in some deeply nested line box). See CSS3 spec.
60 newStyle->inheritFrom(style());
62 m_marker = new (renderArena()) RenderListMarker(this);
63 m_marker->setStyle(newStyle.release());
64 } else if (m_marker) {
70 void RenderListItem::willBeDestroyed()
76 RenderBlock::willBeDestroyed();
79 static bool isList(Node* node)
81 return (node->hasTagName(ulTag) || node->hasTagName(olTag));
84 static Node* enclosingList(const RenderListItem* listItem)
88 for (const RenderObject* renderer = listItem->parent(); renderer; renderer = renderer->parent()) {
89 Node* node = renderer->node();
98 // If there's no actual <ul> or <ol> list element, then the first found
99 // node acts as our list for purposes of determining what other list items
100 // should be numbered as part of the same list.
104 static RenderListItem* previousListItem(Node* list, const RenderListItem* item)
106 for (RenderObject* renderer = item->previousInPreOrder(); renderer && renderer != list->renderer(); renderer = renderer->previousInPreOrder()) {
107 if (!renderer->isListItem())
109 Node* otherList = enclosingList(toRenderListItem(renderer));
110 // This item is part of our current list, so it's what we're looking for.
111 if (list == otherList)
112 return toRenderListItem(renderer);
113 // We found ourself inside another list; lets skip the rest of it.
114 // Use nextInPreOrder() here because the other list itself may actually
115 // be a list item itself. We need to examine it, so we do this to counteract
116 // the previousInPreOrder() that will be done by the loop.
118 renderer = otherList->renderer()->nextInPreOrder();
123 inline int RenderListItem::calcValue() const
125 if (m_hasExplicitValue)
126 return m_explicitValue;
127 Node* list = enclosingList(this);
128 // FIXME: This recurses to a possible depth of the length of the list.
129 // That's not good -- we need to change this to an iterative algorithm.
130 if (RenderListItem* previousItem = previousListItem(list, this))
131 return previousItem->value() + 1;
132 if (list && list->hasTagName(olTag))
133 return static_cast<HTMLOListElement*>(list)->start();
137 void RenderListItem::updateValueNow() const
139 m_value = calcValue();
140 m_isValueUpToDate = true;
143 bool RenderListItem::isEmpty() const
145 return lastChild() == m_marker;
148 static RenderObject* getParentOfFirstLineBox(RenderBlock* curr, RenderObject* marker)
150 RenderObject* firstChild = curr->firstChild();
154 bool inQuirksMode = curr->document()->inQuirksMode();
155 for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
156 if (currChild == marker)
159 if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
162 if (currChild->isFloating() || currChild->isPositioned())
165 if (currChild->isTable() || !currChild->isRenderBlock() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
168 if (curr->isListItem() && inQuirksMode && currChild->node() &&
169 (currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
172 RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlock(currChild), marker);
180 void RenderListItem::updateValue()
182 if (!m_hasExplicitValue) {
183 m_isValueUpToDate = false;
185 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
189 static RenderObject* firstNonMarkerChild(RenderObject* parent)
191 RenderObject* result = parent->firstChild();
192 while (result && result->isListMarker())
193 result = result->nextSibling();
197 void RenderListItem::updateMarkerLocation()
199 // Sanity check the location of our marker.
201 RenderObject* markerPar = m_marker->parent();
202 RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
203 if (!lineBoxParent) {
204 // If the marker is currently contained inside an anonymous box,
205 // then we are the only item in that anonymous box (since no line box
206 // parent was found). It's ok to just leave the marker where it is
208 if (markerPar && markerPar->isAnonymousBlock())
209 lineBoxParent = markerPar;
211 lineBoxParent = this;
214 if (markerPar != lineBoxParent || m_marker->preferredLogicalWidthsDirty()) {
215 // Removing and adding the marker can trigger repainting in
216 // containers other than ourselves, so we need to disable LayoutState.
217 LayoutStateDisabler layoutStateDisabler(view());
221 lineBoxParent = this;
222 lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
223 if (m_marker->preferredLogicalWidthsDirty())
224 m_marker->computePreferredLogicalWidths();
229 void RenderListItem::computePreferredLogicalWidths()
231 ASSERT(preferredLogicalWidthsDirty());
233 updateMarkerLocation();
235 RenderBlock::computePreferredLogicalWidths();
238 void RenderListItem::layout()
240 ASSERT(needsLayout());
242 updateMarkerLocation();
243 RenderBlock::layout();
246 void RenderListItem::addOverflowFromChildren()
248 RenderBlock::addOverflowFromChildren();
249 positionListMarker();
252 void RenderListItem::positionListMarker()
254 if (m_marker && m_marker->parent()->isBox() && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
255 LayoutUnit markerOldLogicalLeft = m_marker->logicalLeft();
256 LayoutUnit blockOffset = 0;
257 LayoutUnit lineOffset = 0;
258 for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
259 blockOffset += o->logicalTop();
260 lineOffset += o->logicalLeft();
263 bool adjustOverflow = false;
264 LayoutUnit markerLogicalLeft;
265 RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
266 bool hitSelfPaintingLayer = false;
268 RootInlineBox* rootBox = m_marker->inlineBoxWrapper()->root();
269 LayoutUnit lineTop = rootBox->lineTop();
270 LayoutUnit lineBottom = rootBox->lineBottom();
272 // FIXME: Need to account for relative positioning in the layout overflow.
273 if (style()->isLeftToRightDirection()) {
274 LayoutUnit leftLineOffset = logicalLeftOffsetForLine(blockOffset, logicalLeftOffsetForLine(blockOffset, false), false);
275 markerLogicalLeft = leftLineOffset - lineOffset - paddingStart() - borderStart() + m_marker->marginStart();
276 m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
277 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
278 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
279 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
280 if (markerLogicalLeft < newLogicalVisualOverflowRect.x() && !hitSelfPaintingLayer) {
281 newLogicalVisualOverflowRect.setWidth(newLogicalVisualOverflowRect.maxX() - markerLogicalLeft);
282 newLogicalVisualOverflowRect.setX(markerLogicalLeft);
284 adjustOverflow = true;
286 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
287 newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
288 newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
290 adjustOverflow = true;
292 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
293 if (box->boxModelObject()->hasSelfPaintingLayer())
294 hitSelfPaintingLayer = true;
297 markerLogicalLeft = m_marker->logicalLeft() + paddingStart() + borderStart() + m_marker->marginEnd();
298 LayoutUnit rightLineOffset = logicalRightOffsetForLine(blockOffset, logicalRightOffsetForLine(blockOffset, false), false);
299 markerLogicalLeft = rightLineOffset - lineOffset + paddingStart() + borderStart() + m_marker->marginEnd();
300 m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
301 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
302 LayoutRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
303 LayoutRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
304 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalVisualOverflowRect.maxX() && !hitSelfPaintingLayer) {
305 newLogicalVisualOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalVisualOverflowRect.x());
307 adjustOverflow = true;
309 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
310 newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
312 adjustOverflow = true;
314 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
316 if (box->boxModelObject()->hasSelfPaintingLayer())
317 hitSelfPaintingLayer = true;
321 if (adjustOverflow) {
322 LayoutRect markerRect(markerLogicalLeft + lineOffset, blockOffset, m_marker->width(), m_marker->height());
323 if (!style()->isHorizontalWritingMode())
324 markerRect = markerRect.transposedRect();
325 RenderBox* o = m_marker;
326 bool propagateVisualOverflow = true;
327 bool propagateLayoutOverflow = true;
330 if (o->hasOverflowClip())
331 propagateVisualOverflow = false;
332 if (o->isRenderBlock()) {
333 if (propagateVisualOverflow)
334 toRenderBlock(o)->addVisualOverflow(markerRect);
335 if (propagateLayoutOverflow)
336 toRenderBlock(o)->addLayoutOverflow(markerRect);
338 if (o->hasOverflowClip())
339 propagateLayoutOverflow = false;
340 if (o->hasSelfPaintingLayer())
341 propagateVisualOverflow = false;
342 markerRect.moveBy(-o->location());
343 } while (o != this && propagateVisualOverflow && propagateLayoutOverflow);
348 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
350 if (!logicalHeight())
353 RenderBlock::paint(paintInfo, paintOffset);
356 const String& RenderListItem::markerText() const
359 return m_marker->text();
360 DEFINE_STATIC_LOCAL(String, staticNullString, ());
361 return staticNullString;
364 String RenderListItem::markerTextWithSuffix() const
369 // Append the suffix for the marker in the right place depending
370 // on the direction of the text (right-to-left or left-to-right).
372 const String& markerText = m_marker->text();
373 const String markerSuffix = m_marker->suffix();
374 StringBuilder result;
376 if (!m_marker->style()->isLeftToRightDirection())
377 result.append(markerSuffix);
379 result.append(markerText);
381 if (m_marker->style()->isLeftToRightDirection())
382 result.append(markerSuffix);
384 return result.toString();
387 void RenderListItem::explicitValueChanged()
390 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
391 Node* listNode = enclosingList(this);
392 RenderObject* listRenderer = 0;
394 listRenderer = listNode->renderer();
395 for (RenderObject* renderer = this; renderer; renderer = renderer->nextInPreOrder(listRenderer))
396 if (renderer->isListItem()) {
397 RenderListItem* item = toRenderListItem(renderer);
398 if (!item->m_hasExplicitValue) {
399 item->m_isValueUpToDate = false;
400 if (RenderListMarker* marker = item->m_marker)
401 marker->setNeedsLayoutAndPrefWidthsRecalc();
406 void RenderListItem::setExplicitValue(int value)
410 if (m_hasExplicitValue && m_explicitValue == value)
412 m_explicitValue = value;
414 m_hasExplicitValue = true;
415 explicitValueChanged();
418 void RenderListItem::clearExplicitValue()
422 if (!m_hasExplicitValue)
424 m_hasExplicitValue = false;
425 m_isValueUpToDate = false;
426 explicitValueChanged();
429 void RenderListItem::updateListMarkerNumbers()
431 Node* listNode = enclosingList(this);
432 ASSERT(listNode && listNode->renderer());
433 if (!listNode || !listNode->renderer())
436 RenderObject* list = listNode->renderer();
437 RenderObject* child = nextInPreOrder(list);
439 if (child->node() && isList(child->node())) {
440 // We've found a nested, independent list: nothing to do here.
441 child = child->nextInPreOrderAfterChildren(list);
445 if (child->isListItem()) {
446 RenderListItem* item = toRenderListItem(child);
448 if (!item->m_isValueUpToDate) {
449 // If an item has been marked for update before, we can safely
450 // assume that all the following ones have too.
451 // This gives us the opportunity to stop here and avoid
452 // marking the same nodes again.
459 child = child->nextInPreOrder(list);
463 } // namespace WebCore