blob: 54a7dd29087551376bfa070cb60cb824ed812192 [file] [log] [blame]
eseidele34973b2006-05-15 21:16:29 +00001/**
eseidele34973b2006-05-15 21:16:29 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
mitz@apple.com9feca282010-01-26 22:48:56 +00004 * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
eseidele34973b2006-05-15 21:16:29 +00005 * 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
ddkilzerc8eccec2007-09-26 02:29:57 +000019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
eseidele34973b2006-05-15 21:16:29 +000021 *
22 */
darinec375482007-01-06 01:36:24 +000023
eseidele34973b2006-05-15 21:16:29 +000024#include "config.h"
25#include "RenderListItem.h"
26
27#include "CachedImage.h"
28#include "HTMLNames.h"
29#include "HTMLOListElement.h"
30#include "RenderListMarker.h"
weinigfef13632007-04-29 20:09:08 +000031#include "RenderView.h"
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000032#include <wtf/StdLibExtras.h>
eseidele34973b2006-05-15 21:16:29 +000033
34using namespace std;
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40RenderListItem::RenderListItem(Node* node)
41 : RenderBlock(node)
eseidele34973b2006-05-15 21:16:29 +000042 , m_marker(0)
darinec375482007-01-06 01:36:24 +000043 , m_hasExplicitValue(false)
44 , m_isValueUpToDate(false)
eseidele34973b2006-05-15 21:16:29 +000045 , m_notInList(false)
eseidele34973b2006-05-15 21:16:29 +000046{
darinec375482007-01-06 01:36:24 +000047 setInline(false);
eseidele34973b2006-05-15 21:16:29 +000048}
49
weinig@apple.com1a57c822009-02-04 22:27:50 +000050void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
eseidele34973b2006-05-15 21:16:29 +000051{
simon.fraser@apple.coma89b8cd2008-10-10 03:15:31 +000052 RenderBlock::styleDidChange(diff, oldStyle);
eseidele34973b2006-05-15 21:16:29 +000053
dbates@webkit.org267a5c82009-12-23 07:28:54 +000054 if (style()->listStyleType() != NoneListStyle
55 || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
hyatt@apple.comdec0cbf22008-10-17 00:25:33 +000056 RefPtr<RenderStyle> newStyle = RenderStyle::create();
eseidele34973b2006-05-15 21:16:29 +000057 // The marker always inherits from the list item, regardless of where it might end
darinec375482007-01-06 01:36:24 +000058 // up (e.g., in some deeply nested line box). See CSS3 spec.
eseidele34973b2006-05-15 21:16:29 +000059 newStyle->inheritFrom(style());
darinec375482007-01-06 01:36:24 +000060 if (!m_marker)
61 m_marker = new (renderArena()) RenderListMarker(this);
hyatt@apple.comdec0cbf22008-10-17 00:25:33 +000062 m_marker->setStyle(newStyle.release());
eseidele34973b2006-05-15 21:16:29 +000063 } else if (m_marker) {
64 m_marker->destroy();
65 m_marker = 0;
66 }
67}
68
69void RenderListItem::destroy()
70{
71 if (m_marker) {
72 m_marker->destroy();
73 m_marker = 0;
74 }
75 RenderBlock::destroy();
76}
77
mitz@apple.com9feca282010-01-26 22:48:56 +000078static bool isList(Node* node)
79{
80 return (node->hasTagName(ulTag) || node->hasTagName(olTag));
81}
82
eseidele34973b2006-05-15 21:16:29 +000083static Node* enclosingList(Node* node)
84{
darin76d3b832006-07-29 15:06:04 +000085 Node* parent = node->parentNode();
86 for (Node* n = parent; n; n = n->parentNode())
mitz@apple.com9feca282010-01-26 22:48:56 +000087 if (isList(n))
eseidele34973b2006-05-15 21:16:29 +000088 return n;
darin76d3b832006-07-29 15:06:04 +000089 // If there's no actual <ul> or <ol> list element, then our parent acts as
90 // our list for purposes of determining what other list items should be
91 // numbered as part of the same list.
92 return parent;
eseidele34973b2006-05-15 21:16:29 +000093}
94
mitz@apple.com9feca282010-01-26 22:48:56 +000095static Node* enclosingList(const RenderObject* renderer)
96{
97 Node* node = renderer->node();
98 if (node)
99 return enclosingList(node);
100
101 renderer = renderer->parent();
102 while (renderer && !renderer->node())
103 renderer = renderer->parent();
104
105 node = renderer->node();
106 if (isList(node))
107 return node;
108
109 return enclosingList(node);
110}
111
darinec375482007-01-06 01:36:24 +0000112static RenderListItem* previousListItem(Node* list, const RenderListItem* item)
eseidele34973b2006-05-15 21:16:29 +0000113{
mitz@apple.com9feca282010-01-26 22:48:56 +0000114 for (RenderObject* renderer = item->previousInPreOrder(); renderer != list->renderer(); renderer = renderer->previousInPreOrder()) {
115 if (!renderer->isListItem())
darin@apple.comb6cb2562009-08-05 21:25:09 +0000116 continue;
mitz@apple.com9feca282010-01-26 22:48:56 +0000117 Node* otherList = enclosingList(renderer);
darin@apple.comb6cb2562009-08-05 21:25:09 +0000118 // This item is part of our current list, so it's what we're looking for.
119 if (list == otherList)
120 return toRenderListItem(renderer);
121 // We found ourself inside another list; lets skip the rest of it.
mitz@apple.com9feca282010-01-26 22:48:56 +0000122 // Use nextInPreOrder() here because the other list itself may actually
darin@apple.comb6cb2562009-08-05 21:25:09 +0000123 // be a list item itself. We need to examine it, so we do this to counteract
mitz@apple.com9feca282010-01-26 22:48:56 +0000124 // the previousInPreOrder() that will be done by the loop.
darin@apple.comb6cb2562009-08-05 21:25:09 +0000125 if (otherList)
mitz@apple.com9feca282010-01-26 22:48:56 +0000126 renderer = otherList->renderer()->nextInPreOrder();
eseidele34973b2006-05-15 21:16:29 +0000127 }
128 return 0;
129}
130
darinec375482007-01-06 01:36:24 +0000131inline int RenderListItem::calcValue() const
eseidele34973b2006-05-15 21:16:29 +0000132{
darinec375482007-01-06 01:36:24 +0000133 if (m_hasExplicitValue)
134 return m_explicitValue;
mitz@apple.com9feca282010-01-26 22:48:56 +0000135 Node* list = enclosingList(this);
darinec375482007-01-06 01:36:24 +0000136 // FIXME: This recurses to a possible depth of the length of the list.
137 // That's not good -- we need to change this to an iterative algorithm.
138 if (RenderListItem* previousItem = previousListItem(list, this))
139 return previousItem->value() + 1;
140 if (list && list->hasTagName(olTag))
141 return static_cast<HTMLOListElement*>(list)->start();
142 return 1;
143}
144
145void RenderListItem::updateValueNow() const
146{
147 m_value = calcValue();
148 m_isValueUpToDate = true;
eseidele34973b2006-05-15 21:16:29 +0000149}
150
151bool RenderListItem::isEmpty() const
152{
153 return lastChild() == m_marker;
154}
155
bdashccffb432007-07-13 11:51:40 +0000156static RenderObject* getParentOfFirstLineBox(RenderBlock* curr, RenderObject* marker)
eseidele34973b2006-05-15 21:16:29 +0000157{
158 RenderObject* firstChild = curr->firstChild();
159 if (!firstChild)
160 return 0;
weinigc24ab182006-10-30 22:41:29 +0000161
eseidele34973b2006-05-15 21:16:29 +0000162 for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
163 if (currChild == marker)
164 continue;
weinigc24ab182006-10-30 22:41:29 +0000165
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000166 if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
eseidele34973b2006-05-15 21:16:29 +0000167 return curr;
weinigc24ab182006-10-30 22:41:29 +0000168
eseidele34973b2006-05-15 21:16:29 +0000169 if (currChild->isFloating() || currChild->isPositioned())
170 continue;
weinigc24ab182006-10-30 22:41:29 +0000171
eseidele34973b2006-05-15 21:16:29 +0000172 if (currChild->isTable() || !currChild->isRenderBlock())
173 break;
weinigc24ab182006-10-30 22:41:29 +0000174
hyatt@apple.com7e032532009-02-11 22:06:32 +0000175 if (curr->isListItem() && currChild->style()->htmlHacks() && currChild->node() &&
176 (currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
eseidele34973b2006-05-15 21:16:29 +0000177 break;
weinigc24ab182006-10-30 22:41:29 +0000178
hyatt@apple.com801ed3f2009-01-30 18:16:03 +0000179 RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlock(currChild), marker);
eseidele34973b2006-05-15 21:16:29 +0000180 if (lineBox)
181 return lineBox;
182 }
weinigc24ab182006-10-30 22:41:29 +0000183
eseidele34973b2006-05-15 21:16:29 +0000184 return 0;
185}
186
darinec375482007-01-06 01:36:24 +0000187void RenderListItem::updateValue()
eseidele34973b2006-05-15 21:16:29 +0000188{
darinec375482007-01-06 01:36:24 +0000189 if (!m_hasExplicitValue) {
190 m_isValueUpToDate = false;
191 if (m_marker)
darin7e7c8e42007-04-25 01:14:03 +0000192 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
darinec375482007-01-06 01:36:24 +0000193 }
eseidele34973b2006-05-15 21:16:29 +0000194}
195
hyatt818bb122007-04-25 19:10:24 +0000196static RenderObject* firstNonMarkerChild(RenderObject* parent)
197{
198 RenderObject* result = parent->firstChild();
199 while (result && result->isListMarker())
200 result = result->nextSibling();
201 return result;
202}
203
eseidele34973b2006-05-15 21:16:29 +0000204void RenderListItem::updateMarkerLocation()
205{
206 // Sanity check the location of our marker.
207 if (m_marker) {
208 RenderObject* markerPar = m_marker->parent();
209 RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
210 if (!lineBoxParent) {
211 // If the marker is currently contained inside an anonymous box,
212 // then we are the only item in that anonymous box (since no line box
213 // parent was found). It's ok to just leave the marker where it is
214 // in this case.
215 if (markerPar && markerPar->isAnonymousBlock())
216 lineBoxParent = markerPar;
217 else
218 lineBoxParent = this;
219 }
weinigc24ab182006-10-30 22:41:29 +0000220
darin7e7c8e42007-04-25 01:14:03 +0000221 if (markerPar != lineBoxParent || m_marker->prefWidthsDirty()) {
weinigfef13632007-04-29 20:09:08 +0000222 // Removing and adding the marker can trigger repainting in
223 // containers other than ourselves, so we need to disable LayoutState.
224 view()->disableLayoutState();
hyatt818bb122007-04-25 19:10:24 +0000225 updateFirstLetter();
hyattb8663442006-08-04 21:04:11 +0000226 m_marker->remove();
eseidele34973b2006-05-15 21:16:29 +0000227 if (!lineBoxParent)
228 lineBoxParent = this;
hyatt818bb122007-04-25 19:10:24 +0000229 lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
darin7e7c8e42007-04-25 01:14:03 +0000230 if (m_marker->prefWidthsDirty())
231 m_marker->calcPrefWidths();
weinigfef13632007-04-29 20:09:08 +0000232 view()->enableLayoutState();
eseidele34973b2006-05-15 21:16:29 +0000233 }
234 }
235}
236
darin7e7c8e42007-04-25 01:14:03 +0000237void RenderListItem::calcPrefWidths()
eseidele34973b2006-05-15 21:16:29 +0000238{
hyatt818bb122007-04-25 19:10:24 +0000239 ASSERT(prefWidthsDirty());
240
eseidele34973b2006-05-15 21:16:29 +0000241 updateMarkerLocation();
hyatt818bb122007-04-25 19:10:24 +0000242
243 RenderBlock::calcPrefWidths();
eseidele34973b2006-05-15 21:16:29 +0000244}
245
246void RenderListItem::layout()
247{
hyatt818bb122007-04-25 19:10:24 +0000248 ASSERT(needsLayout());
249
eseidele34973b2006-05-15 21:16:29 +0000250 updateMarkerLocation();
251 RenderBlock::layout();
252}
253
254void RenderListItem::positionListMarker()
255{
mitz@apple.comb2141332008-09-10 18:19:39 +0000256 if (m_marker && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000257 int markerOldX = m_marker->x();
mitz@apple.comb2141332008-09-10 18:19:39 +0000258 int yOffset = 0;
259 int xOffset = 0;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000260 for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
261 yOffset += o->y();
262 xOffset += o->x();
weinigf1db1db2007-02-28 00:04:39 +0000263 }
mitz@apple.com8de23dd2008-09-10 18:17:14 +0000264
mitz@apple.comb2141332008-09-10 18:19:39 +0000265 bool adjustOverflow = false;
266 int markerXPos;
267 RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
268
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000269 // FIXME: Inline flows in the line box hierarchy that have self-painting layers should act as cutoff points
270 // and really shouldn't keep propagating overflow up. This won't really break anything other than repainting
271 // not being as tight as it could be though.
mitz@apple.comb2141332008-09-10 18:19:39 +0000272 if (style()->direction() == LTR) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000273 int leftLineOffset = leftRelOffset(yOffset, leftOffset(yOffset, false), false);
mitz@apple.comb2141332008-09-10 18:19:39 +0000274 markerXPos = leftLineOffset - xOffset - paddingLeft() - borderLeft() + m_marker->marginLeft();
275 m_marker->inlineBoxWrapper()->adjustPosition(markerXPos - markerOldX, 0);
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000276 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
277 if (markerXPos < box->leftLayoutOverflow()) {
278 box->setHorizontalOverflowPositions(markerXPos, box->rightLayoutOverflow(), box->leftVisualOverflow(), box->rightVisualOverflow());
279 if (box == root)
280 adjustOverflow = true;
281 }
mitz@apple.comb2141332008-09-10 18:19:39 +0000282 }
283 } else {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000284 int rightLineOffset = rightRelOffset(yOffset, rightOffset(yOffset, false), false);
mitz@apple.comb2141332008-09-10 18:19:39 +0000285 markerXPos = rightLineOffset - xOffset + paddingRight() + borderRight() + m_marker->marginLeft();
286 m_marker->inlineBoxWrapper()->adjustPosition(markerXPos - markerOldX, 0);
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000287 for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
288 if (markerXPos + m_marker->width() > box->rightLayoutOverflow()) {
289 box->setHorizontalOverflowPositions(box->leftLayoutOverflow(), markerXPos + m_marker->width(), box->leftVisualOverflow(), box->rightVisualOverflow());
290 if (box == root)
291 adjustOverflow = true;
292 }
mitz@apple.comb2141332008-09-10 18:19:39 +0000293 }
294 }
295
296 if (adjustOverflow) {
297 IntRect markerRect(markerXPos + xOffset, yOffset, m_marker->width(), m_marker->height());
hyatt@apple.comd885df72009-01-22 02:31:52 +0000298 RenderBox* o = m_marker;
mitz@apple.comb2141332008-09-10 18:19:39 +0000299 do {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000300 o = o->parentBox();
mitz@apple.comb2141332008-09-10 18:19:39 +0000301 if (o->isRenderBlock())
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000302 toRenderBlock(o)->addLayoutOverflow(markerRect);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000303 markerRect.move(-o->x(), -o->y());
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000304 } while (o != this && !o->hasSelfPaintingLayer());
mitz@apple.comb2141332008-09-10 18:19:39 +0000305 }
eseidele34973b2006-05-15 21:16:29 +0000306 }
307}
308
weinigc24ab182006-10-30 22:41:29 +0000309void RenderListItem::paint(PaintInfo& paintInfo, int tx, int ty)
eseidele34973b2006-05-15 21:16:29 +0000310{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000311 if (!height())
eseidele34973b2006-05-15 21:16:29 +0000312 return;
313
weinigc24ab182006-10-30 22:41:29 +0000314 RenderBlock::paint(paintInfo, tx, ty);
eseidele34973b2006-05-15 21:16:29 +0000315}
316
darinec375482007-01-06 01:36:24 +0000317const String& RenderListItem::markerText() const
eseidele34973b2006-05-15 21:16:29 +0000318{
darinec375482007-01-06 01:36:24 +0000319 if (m_marker)
320 return m_marker->text();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +0000321 DEFINE_STATIC_LOCAL(String, staticNullString, ());
darinec375482007-01-06 01:36:24 +0000322 return staticNullString;
323}
324
325void RenderListItem::explicitValueChanged()
326{
327 if (m_marker)
darin7e7c8e42007-04-25 01:14:03 +0000328 m_marker->setNeedsLayoutAndPrefWidthsRecalc();
darinec375482007-01-06 01:36:24 +0000329 Node* listNode = enclosingList(node());
adele60f28362007-01-06 06:24:08 +0000330 RenderObject* listRenderer = 0;
darinec375482007-01-06 01:36:24 +0000331 if (listNode)
332 listRenderer = listNode->renderer();
darin@apple.comb6cb2562009-08-05 21:25:09 +0000333 for (RenderObject* renderer = this; renderer; renderer = renderer->nextInPreOrder(listRenderer))
334 if (renderer->isListItem()) {
335 RenderListItem* item = toRenderListItem(renderer);
darinec375482007-01-06 01:36:24 +0000336 if (!item->m_hasExplicitValue) {
337 item->m_isValueUpToDate = false;
338 if (RenderListMarker* marker = item->m_marker)
darin7e7c8e42007-04-25 01:14:03 +0000339 marker->setNeedsLayoutAndPrefWidthsRecalc();
darinec375482007-01-06 01:36:24 +0000340 }
341 }
342}
343
344void RenderListItem::setExplicitValue(int value)
345{
mitz@apple.com9feca282010-01-26 22:48:56 +0000346 ASSERT(node());
347
darinec375482007-01-06 01:36:24 +0000348 if (m_hasExplicitValue && m_explicitValue == value)
349 return;
350 m_explicitValue = value;
351 m_value = value;
352 m_hasExplicitValue = true;
353 explicitValueChanged();
354}
355
356void RenderListItem::clearExplicitValue()
357{
mitz@apple.com9feca282010-01-26 22:48:56 +0000358 ASSERT(node());
359
darinec375482007-01-06 01:36:24 +0000360 if (!m_hasExplicitValue)
361 return;
362 m_hasExplicitValue = false;
363 m_isValueUpToDate = false;
364 explicitValueChanged();
eseidele34973b2006-05-15 21:16:29 +0000365}
366
weinigc24ab182006-10-30 22:41:29 +0000367} // namespace WebCore