initial import
[vuplus_webkit] / Source / WebCore / 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 "RenderListItem.h"
26
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>
34
35 using namespace std;
36
37 namespace WebCore {
38
39 using namespace HTMLNames;
40
41 RenderListItem::RenderListItem(Node* node)
42     : RenderBlock(node)
43     , m_marker(0)
44     , m_hasExplicitValue(false)
45     , m_isValueUpToDate(false)
46     , m_notInList(false)
47 {
48     setInline(false);
49 }
50
51 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
52 {
53     RenderBlock::styleDidChange(diff, oldStyle);
54
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()); 
61         if (!m_marker)
62             m_marker = new (renderArena()) RenderListMarker(this);
63         m_marker->setStyle(newStyle.release());
64     } else if (m_marker) {
65         m_marker->destroy();
66         m_marker = 0;
67     }
68 }
69
70 void RenderListItem::willBeDestroyed()
71 {    
72     if (m_marker) {
73         m_marker->destroy();
74         m_marker = 0;
75     }
76     RenderBlock::willBeDestroyed();
77 }
78
79 static bool isList(Node* node)
80 {
81     return (node->hasTagName(ulTag) || node->hasTagName(olTag));
82 }
83
84 static Node* enclosingList(const RenderListItem* listItem)
85 {
86     Node* firstNode = 0;
87
88     for (const RenderObject* renderer = listItem->parent(); renderer; renderer = renderer->parent()) {
89         Node* node = renderer->node();
90         if (node) {
91             if (isList(node))
92                 return node;
93             if (!firstNode)
94                 firstNode = node;
95         }
96     }
97
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.
101     return firstNode;
102 }
103
104 static RenderListItem* previousListItem(Node* list, const RenderListItem* item)
105 {
106     for (RenderObject* renderer = item->previousInPreOrder(); renderer && renderer != list->renderer(); renderer = renderer->previousInPreOrder()) {
107         if (!renderer->isListItem())
108             continue;
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.
117         if (otherList)
118             renderer = otherList->renderer()->nextInPreOrder();
119     }
120     return 0;
121 }
122
123 inline int RenderListItem::calcValue() const
124 {
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();
134     return 1;
135 }
136
137 void RenderListItem::updateValueNow() const
138 {
139     m_value = calcValue();
140     m_isValueUpToDate = true;
141 }
142
143 bool RenderListItem::isEmpty() const
144 {
145     return lastChild() == m_marker;
146 }
147
148 static RenderObject* getParentOfFirstLineBox(RenderBlock* curr, RenderObject* marker)
149 {
150     RenderObject* firstChild = curr->firstChild();
151     if (!firstChild)
152         return 0;
153
154     bool inQuirksMode = curr->document()->inQuirksMode();
155     for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
156         if (currChild == marker)
157             continue;
158
159         if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
160             return curr;
161
162         if (currChild->isFloating() || currChild->isPositioned())
163             continue;
164
165         if (currChild->isTable() || !currChild->isRenderBlock() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
166             break;
167
168         if (curr->isListItem() && inQuirksMode && currChild->node() &&
169             (currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
170             break;
171
172         RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlock(currChild), marker);
173         if (lineBox)
174             return lineBox;
175     }
176
177     return 0;
178 }
179
180 void RenderListItem::updateValue()
181 {
182     if (!m_hasExplicitValue) {
183         m_isValueUpToDate = false;
184         if (m_marker)
185             m_marker->setNeedsLayoutAndPrefWidthsRecalc();
186     }
187 }
188
189 static RenderObject* firstNonMarkerChild(RenderObject* parent)
190 {
191     RenderObject* result = parent->firstChild();
192     while (result && result->isListMarker())
193         result = result->nextSibling();
194     return result;
195 }
196
197 void RenderListItem::updateMarkerLocation()
198 {
199     // Sanity check the location of our marker.
200     if (m_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
207             // in this case.
208             if (markerPar && markerPar->isAnonymousBlock())
209                 lineBoxParent = markerPar;
210             else
211                 lineBoxParent = this;
212         }
213
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());
218             updateFirstLetter();
219             m_marker->remove();
220             if (!lineBoxParent)
221                 lineBoxParent = this;
222             lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
223             if (m_marker->preferredLogicalWidthsDirty())
224                 m_marker->computePreferredLogicalWidths();
225         }
226     }
227 }
228
229 void RenderListItem::computePreferredLogicalWidths()
230 {
231     ASSERT(preferredLogicalWidthsDirty());
232     
233     updateMarkerLocation();
234
235     RenderBlock::computePreferredLogicalWidths();
236 }
237
238 void RenderListItem::layout()
239 {
240     ASSERT(needsLayout()); 
241
242     updateMarkerLocation();    
243     RenderBlock::layout();
244 }
245
246 void RenderListItem::addOverflowFromChildren()
247 {
248     RenderBlock::addOverflowFromChildren();
249     positionListMarker();
250 }
251
252 void RenderListItem::positionListMarker()
253 {
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();
261         }
262
263         bool adjustOverflow = false;
264         LayoutUnit markerLogicalLeft;
265         RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
266         bool hitSelfPaintingLayer = false;
267         
268         RootInlineBox* rootBox = m_marker->inlineBoxWrapper()->root();
269         LayoutUnit lineTop = rootBox->lineTop();
270         LayoutUnit lineBottom = rootBox->lineBottom();
271
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);
283                     if (box == root)
284                         adjustOverflow = true;
285                 }
286                 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
287                     newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
288                     newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
289                     if (box == root)
290                         adjustOverflow = true;
291                 }
292                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
293                 if (box->boxModelObject()->hasSelfPaintingLayer())
294                     hitSelfPaintingLayer = true;
295             }
296         } else {
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());
306                     if (box == root)
307                         adjustOverflow = true;
308                 }
309                 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
310                     newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
311                     if (box == root)
312                         adjustOverflow = true;
313                 }
314                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
315                 
316                 if (box->boxModelObject()->hasSelfPaintingLayer())
317                     hitSelfPaintingLayer = true;
318             }
319         }
320
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;
328             do {
329                 o = o->parentBox();
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);
337                 }
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);
344         }
345     }
346 }
347
348 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
349 {
350     if (!logicalHeight())
351         return;
352
353     RenderBlock::paint(paintInfo, paintOffset);
354 }
355
356 const String& RenderListItem::markerText() const
357 {
358     if (m_marker)
359         return m_marker->text();
360     DEFINE_STATIC_LOCAL(String, staticNullString, ());
361     return staticNullString;
362 }
363
364 String RenderListItem::markerTextWithSuffix() const
365 {
366     if (!m_marker)
367         return String();
368
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).
371
372     const String& markerText = m_marker->text();
373     const String markerSuffix = m_marker->suffix();
374     StringBuilder result;
375
376     if (!m_marker->style()->isLeftToRightDirection())
377         result.append(markerSuffix);
378
379     result.append(markerText);
380
381     if (m_marker->style()->isLeftToRightDirection())
382         result.append(markerSuffix);
383
384     return result.toString();
385 }
386
387 void RenderListItem::explicitValueChanged()
388 {
389     if (m_marker)
390         m_marker->setNeedsLayoutAndPrefWidthsRecalc();
391     Node* listNode = enclosingList(this);
392     RenderObject* listRenderer = 0;
393     if (listNode)
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();
402             }
403         }
404 }
405
406 void RenderListItem::setExplicitValue(int value)
407 {
408     ASSERT(node());
409
410     if (m_hasExplicitValue && m_explicitValue == value)
411         return;
412     m_explicitValue = value;
413     m_value = value;
414     m_hasExplicitValue = true;
415     explicitValueChanged();
416 }
417
418 void RenderListItem::clearExplicitValue()
419 {
420     ASSERT(node());
421
422     if (!m_hasExplicitValue)
423         return;
424     m_hasExplicitValue = false;
425     m_isValueUpToDate = false;
426     explicitValueChanged();
427 }
428
429 void RenderListItem::updateListMarkerNumbers()
430 {
431     Node* listNode = enclosingList(this);
432     ASSERT(listNode && listNode->renderer());
433     if (!listNode || !listNode->renderer())
434         return;
435
436     RenderObject* list = listNode->renderer();
437     RenderObject* child = nextInPreOrder(list);
438     while (child) {
439         if (child->node() && isList(child->node())) {
440             // We've found a nested, independent list: nothing to do here.
441             child = child->nextInPreOrderAfterChildren(list);
442             continue;
443         }
444
445         if (child->isListItem()) {
446             RenderListItem* item = toRenderListItem(child);
447
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.
453                 break;
454             }
455
456             item->updateValue();
457         }
458
459         child = child->nextInPreOrder(list);
460     }
461 }
462
463 } // namespace WebCore