2e3494535fe1b34a5422722da31fd961e9b5a1d1
[WebKit-https.git] / 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
34 using namespace std;
35
36 namespace WebCore {
37
38 using namespace HTMLNames;
39
40 RenderListItem::RenderListItem(Node* node)
41     : RenderBlock(node)
42     , m_marker(0)
43     , m_hasExplicitValue(false)
44     , m_isValueUpToDate(false)
45     , m_notInList(false)
46 {
47     setInline(false);
48 }
49
50 void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
51 {
52     RenderBlock::styleDidChange(diff, oldStyle);
53
54     if (style()->listStyleType() != NoneListStyle
55         || (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
56         RefPtr<RenderStyle> newStyle = RenderStyle::create();
57         // The marker always inherits from the list item, regardless of where it might end
58         // up (e.g., in some deeply nested line box). See CSS3 spec.
59         newStyle->inheritFrom(style()); 
60         if (!m_marker)
61             m_marker = new (renderArena()) RenderListMarker(this);
62         m_marker->setStyle(newStyle.release());
63     } else if (m_marker) {
64         m_marker->destroy();
65         m_marker = 0;
66     }
67 }
68
69 void RenderListItem::destroy()
70 {    
71     if (m_marker) {
72         m_marker->destroy();
73         m_marker = 0;
74     }
75     RenderBlock::destroy();
76 }
77
78 static bool isList(Node* node)
79 {
80     return (node->hasTagName(ulTag) || node->hasTagName(olTag));
81 }
82
83 static Node* enclosingList(const RenderListItem* listItem)
84 {
85     Node* firstNode = 0;
86
87     for (const RenderObject* renderer = listItem->parent(); renderer; renderer = renderer->parent()) {
88         Node* node = renderer->node();
89         if (node) {
90             if (isList(node))
91                 return node;
92             if (!firstNode)
93                 firstNode = node;
94         }
95     }
96
97     // If there's no actual <ul> or <ol> list element, then the first found
98     // node acts as our list for purposes of determining what other list items
99     // should be numbered as part of the same list.
100     return firstNode;
101 }
102
103 static RenderListItem* previousListItem(Node* list, const RenderListItem* item)
104 {
105     for (RenderObject* renderer = item->previousInPreOrder(); renderer && renderer != list->renderer(); renderer = renderer->previousInPreOrder()) {
106         if (!renderer->isListItem())
107             continue;
108         Node* otherList = enclosingList(toRenderListItem(renderer));
109         // This item is part of our current list, so it's what we're looking for.
110         if (list == otherList)
111             return toRenderListItem(renderer);
112         // We found ourself inside another list; lets skip the rest of it.
113         // Use nextInPreOrder() here because the other list itself may actually
114         // be a list item itself. We need to examine it, so we do this to counteract
115         // the previousInPreOrder() that will be done by the loop.
116         if (otherList)
117             renderer = otherList->renderer()->nextInPreOrder();
118     }
119     return 0;
120 }
121
122 inline int RenderListItem::calcValue() const
123 {
124     if (m_hasExplicitValue)
125         return m_explicitValue;
126     Node* list = enclosingList(this);
127     // FIXME: This recurses to a possible depth of the length of the list.
128     // That's not good -- we need to change this to an iterative algorithm.
129     if (RenderListItem* previousItem = previousListItem(list, this))
130         return previousItem->value() + 1;
131     if (list && list->hasTagName(olTag))
132         return static_cast<HTMLOListElement*>(list)->start();
133     return 1;
134 }
135
136 void RenderListItem::updateValueNow() const
137 {
138     m_value = calcValue();
139     m_isValueUpToDate = true;
140 }
141
142 bool RenderListItem::isEmpty() const
143 {
144     return lastChild() == m_marker;
145 }
146
147 static RenderObject* getParentOfFirstLineBox(RenderBlock* curr, RenderObject* marker)
148 {
149     RenderObject* firstChild = curr->firstChild();
150     if (!firstChild)
151         return 0;
152
153     bool inQuirksMode = curr->document()->inQuirksMode();
154     for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
155         if (currChild == marker)
156             continue;
157
158         if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
159             return curr;
160
161         if (currChild->isFloating() || currChild->isPositioned())
162             continue;
163
164         if (currChild->isTable() || !currChild->isRenderBlock() || (currChild->isBox() && toRenderBox(currChild)->isWritingModeRoot()))
165             break;
166
167         if (curr->isListItem() && inQuirksMode && currChild->node() &&
168             (currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
169             break;
170
171         RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlock(currChild), marker);
172         if (lineBox)
173             return lineBox;
174     }
175
176     return 0;
177 }
178
179 void RenderListItem::updateValue()
180 {
181     if (!m_hasExplicitValue) {
182         m_isValueUpToDate = false;
183         if (m_marker)
184             m_marker->setNeedsLayoutAndPrefWidthsRecalc();
185     }
186 }
187
188 static RenderObject* firstNonMarkerChild(RenderObject* parent)
189 {
190     RenderObject* result = parent->firstChild();
191     while (result && result->isListMarker())
192         result = result->nextSibling();
193     return result;
194 }
195
196 void RenderListItem::updateMarkerLocation()
197 {
198     // Sanity check the location of our marker.
199     if (m_marker) {
200         RenderObject* markerPar = m_marker->parent();
201         RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
202         if (!lineBoxParent) {
203             // If the marker is currently contained inside an anonymous box,
204             // then we are the only item in that anonymous box (since no line box
205             // parent was found).  It's ok to just leave the marker where it is
206             // in this case.
207             if (markerPar && markerPar->isAnonymousBlock())
208                 lineBoxParent = markerPar;
209             else
210                 lineBoxParent = this;
211         }
212
213         if (markerPar != lineBoxParent || m_marker->preferredLogicalWidthsDirty()) {
214             // Removing and adding the marker can trigger repainting in
215             // containers other than ourselves, so we need to disable LayoutState.
216             LayoutStateDisabler layoutStateDisabler(view());
217             updateFirstLetter();
218             m_marker->remove();
219             if (!lineBoxParent)
220                 lineBoxParent = this;
221             lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
222             if (m_marker->preferredLogicalWidthsDirty())
223                 m_marker->computePreferredLogicalWidths();
224         }
225     }
226 }
227
228 void RenderListItem::computePreferredLogicalWidths()
229 {
230     ASSERT(preferredLogicalWidthsDirty());
231     
232     updateMarkerLocation();
233
234     RenderBlock::computePreferredLogicalWidths();
235 }
236
237 void RenderListItem::layout()
238 {
239     ASSERT(needsLayout()); 
240
241     updateMarkerLocation();    
242     RenderBlock::layout();
243 }
244
245 void RenderListItem::addOverflowFromChildren()
246 {
247     RenderBlock::addOverflowFromChildren();
248     positionListMarker();
249 }
250
251 void RenderListItem::positionListMarker()
252 {
253     if (m_marker && m_marker->parent()->isBox() && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
254         int markerOldLogicalLeft = m_marker->logicalLeft();
255         int blockOffset = 0;
256         int lineOffset = 0;
257         for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
258             blockOffset += o->logicalTop();
259             lineOffset += o->logicalLeft();
260         }
261
262         bool adjustOverflow = false;
263         int markerLogicalLeft;
264         RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
265         bool hitSelfPaintingLayer = false;
266         
267         RootInlineBox* rootBox = m_marker->inlineBoxWrapper()->root();
268         int lineTop = rootBox->lineTop();
269         int lineBottom = rootBox->lineBottom();
270
271         // FIXME: Need to account for relative positioning in the layout overflow.
272         if (style()->isLeftToRightDirection()) {
273             int leftLineOffset = logicalLeftOffsetForLine(blockOffset, logicalLeftOffsetForLine(blockOffset, false), false);
274             markerLogicalLeft = leftLineOffset - lineOffset - paddingStart() - borderStart() + m_marker->marginStart();
275             m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
276             for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
277                 IntRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
278                 IntRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
279                 if (markerLogicalLeft < newLogicalVisualOverflowRect.x() && !hitSelfPaintingLayer) {
280                     newLogicalVisualOverflowRect.setWidth(newLogicalVisualOverflowRect.maxX() - markerLogicalLeft);
281                     newLogicalVisualOverflowRect.setX(markerLogicalLeft);
282                     if (box == root)
283                         adjustOverflow = true;
284                 }
285                 if (markerLogicalLeft < newLogicalLayoutOverflowRect.x()) {
286                     newLogicalLayoutOverflowRect.setWidth(newLogicalLayoutOverflowRect.maxX() - markerLogicalLeft);
287                     newLogicalLayoutOverflowRect.setX(markerLogicalLeft);
288                     if (box == root)
289                         adjustOverflow = true;
290                 }
291                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
292                 if (box->boxModelObject()->hasSelfPaintingLayer())
293                     hitSelfPaintingLayer = true;
294             }
295         } else {
296             markerLogicalLeft = m_marker->logicalLeft() + paddingStart() + borderStart() + m_marker->marginEnd();
297             int rightLineOffset = logicalRightOffsetForLine(blockOffset, logicalRightOffsetForLine(blockOffset, false), false);
298             markerLogicalLeft = rightLineOffset - lineOffset + paddingStart() + borderStart() + m_marker->marginEnd();
299             m_marker->inlineBoxWrapper()->adjustLineDirectionPosition(markerLogicalLeft - markerOldLogicalLeft);
300             for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
301                 IntRect newLogicalVisualOverflowRect = box->logicalVisualOverflowRect(lineTop, lineBottom);
302                 IntRect newLogicalLayoutOverflowRect = box->logicalLayoutOverflowRect(lineTop, lineBottom);
303                 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalVisualOverflowRect.maxX() && !hitSelfPaintingLayer) {
304                     newLogicalVisualOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalVisualOverflowRect.x());
305                     if (box == root)
306                         adjustOverflow = true;
307                 }
308                 if (markerLogicalLeft + m_marker->logicalWidth() > newLogicalLayoutOverflowRect.maxX()) {
309                     newLogicalLayoutOverflowRect.setWidth(markerLogicalLeft + m_marker->logicalWidth() - newLogicalLayoutOverflowRect.x());
310                     if (box == root)
311                         adjustOverflow = true;
312                 }
313                 box->setOverflowFromLogicalRects(newLogicalLayoutOverflowRect, newLogicalVisualOverflowRect, lineTop, lineBottom);
314                 
315                 if (box->boxModelObject()->hasSelfPaintingLayer())
316                     hitSelfPaintingLayer = true;
317             }
318         }
319
320         if (adjustOverflow) {
321             IntRect markerRect(markerLogicalLeft + lineOffset, blockOffset, m_marker->width(), m_marker->height());
322             if (!style()->isHorizontalWritingMode())
323                 markerRect = markerRect.transposedRect();
324             RenderBox* o = m_marker;
325             bool propagateVisualOverflow = true;
326             bool propagateLayoutOverflow = true;
327             do {
328                 o = o->parentBox();
329                 if (o->hasOverflowClip())
330                     propagateVisualOverflow = false;
331                 if (o->isRenderBlock()) {
332                     if (propagateVisualOverflow)
333                         toRenderBlock(o)->addVisualOverflow(markerRect);
334                     if (propagateLayoutOverflow)
335                         toRenderBlock(o)->addLayoutOverflow(markerRect);
336                 }
337                 if (o->hasOverflowClip())
338                     propagateLayoutOverflow = false;
339                 if (o->hasSelfPaintingLayer())
340                     propagateVisualOverflow = false;
341                 markerRect.moveBy(-o->location());
342             } while (o != this && propagateVisualOverflow && propagateLayoutOverflow);
343         }
344     }
345 }
346
347 void RenderListItem::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
348 {
349     if (!logicalHeight())
350         return;
351
352     RenderBlock::paint(paintInfo, paintOffset);
353 }
354
355 const String& RenderListItem::markerText() const
356 {
357     if (m_marker)
358         return m_marker->text();
359     DEFINE_STATIC_LOCAL(String, staticNullString, ());
360     return staticNullString;
361 }
362
363 String RenderListItem::markerTextWithSuffix() const
364 {
365     if (!m_marker)
366         return String();
367
368     // Append the suffix for the marker in the right place depending
369     // on the direction of the text (right-to-left or left-to-right).
370
371     const String& markerText = m_marker->text();
372     const String markerSuffix = m_marker->suffix();
373     Vector<UChar> resultVector;
374
375     if (!m_marker->style()->isLeftToRightDirection())
376         resultVector.append(markerSuffix.characters(), markerSuffix.length());
377
378     resultVector.append(markerText.characters(), markerText.length());
379
380     if (m_marker->style()->isLeftToRightDirection())
381         resultVector.append(markerSuffix.characters(), markerSuffix.length());
382
383     return String::adopt(resultVector);
384 }
385
386 void RenderListItem::explicitValueChanged()
387 {
388     if (m_marker)
389         m_marker->setNeedsLayoutAndPrefWidthsRecalc();
390     Node* listNode = enclosingList(this);
391     RenderObject* listRenderer = 0;
392     if (listNode)
393         listRenderer = listNode->renderer();
394     for (RenderObject* renderer = this; renderer; renderer = renderer->nextInPreOrder(listRenderer))
395         if (renderer->isListItem()) {
396             RenderListItem* item = toRenderListItem(renderer);
397             if (!item->m_hasExplicitValue) {
398                 item->m_isValueUpToDate = false;
399                 if (RenderListMarker* marker = item->m_marker)
400                     marker->setNeedsLayoutAndPrefWidthsRecalc();
401             }
402         }
403 }
404
405 void RenderListItem::setExplicitValue(int value)
406 {
407     ASSERT(node());
408
409     if (m_hasExplicitValue && m_explicitValue == value)
410         return;
411     m_explicitValue = value;
412     m_value = value;
413     m_hasExplicitValue = true;
414     explicitValueChanged();
415 }
416
417 void RenderListItem::clearExplicitValue()
418 {
419     ASSERT(node());
420
421     if (!m_hasExplicitValue)
422         return;
423     m_hasExplicitValue = false;
424     m_isValueUpToDate = false;
425     explicitValueChanged();
426 }
427
428 void RenderListItem::updateListMarkerNumbers()
429 {
430     Node* listNode = enclosingList(this);
431     ASSERT(listNode && listNode->renderer());
432     if (!listNode || !listNode->renderer())
433         return;
434
435     RenderObject* list = listNode->renderer();
436     RenderObject* child = nextInPreOrder(list);
437     while (child) {
438         if (child->node() && isList(child->node())) {
439             // We've found a nested, independent list: nothing to do here.
440             child = child->nextInPreOrderAfterChildren(list);
441             continue;
442         }
443
444         if (child->isListItem()) {
445             RenderListItem* item = toRenderListItem(child);
446
447             if (!item->m_isValueUpToDate) {
448                 // If an item has been marked for update before, we can safely
449                 // assume that all the following ones have too.
450                 // This gives us the opportunity to stop here and avoid
451                 // marking the same nodes again.
452                 break;
453             }
454
455             item->updateValue();
456         }
457
458         child = child->nextInPreOrder(list);
459     }
460 }
461
462 } // namespace WebCore