1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/**
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
* Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderListItem.h"
#include "CachedImage.h"
#include "HTMLNames.h"
#include "HTMLOListElement.h"
#include "RenderListMarker.h"
#include "RenderView.h"
#include <wtf/StdLibExtras.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderListItem::RenderListItem(Node* node)
: RenderBlock(node)
, m_marker(0)
, m_hasExplicitValue(false)
, m_isValueUpToDate(false)
, m_notInList(false)
{
setInline(false);
}
void RenderListItem::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (style()->listStyleType() != NoneListStyle
|| (style()->listStyleImage() && !style()->listStyleImage()->errorOccurred())) {
RefPtr<RenderStyle> newStyle = RenderStyle::create();
// The marker always inherits from the list item, regardless of where it might end
// up (e.g., in some deeply nested line box). See CSS3 spec.
newStyle->inheritFrom(style());
if (!m_marker)
m_marker = new (renderArena()) RenderListMarker(this);
m_marker->setStyle(newStyle.release());
} else if (m_marker) {
m_marker->destroy();
m_marker = 0;
}
}
void RenderListItem::destroy()
{
if (m_marker) {
m_marker->destroy();
m_marker = 0;
}
RenderBlock::destroy();
}
static bool isList(Node* node)
{
return (node->hasTagName(ulTag) || node->hasTagName(olTag));
}
static Node* enclosingList(Node* node)
{
Node* parent = node->parentNode();
for (Node* n = parent; n; n = n->parentNode())
if (isList(n))
return n;
// If there's no actual <ul> or <ol> list element, then our parent acts as
// our list for purposes of determining what other list items should be
// numbered as part of the same list.
return parent;
}
static Node* enclosingList(const RenderObject* renderer)
{
Node* node = renderer->node();
if (node)
return enclosingList(node);
renderer = renderer->parent();
while (renderer && !renderer->node())
renderer = renderer->parent();
node = renderer->node();
if (isList(node))
return node;
return enclosingList(node);
}
static RenderListItem* previousListItem(Node* list, const RenderListItem* item)
{
for (RenderObject* renderer = item->previousInPreOrder(); renderer != list->renderer(); renderer = renderer->previousInPreOrder()) {
if (!renderer->isListItem())
continue;
Node* otherList = enclosingList(renderer);
// This item is part of our current list, so it's what we're looking for.
if (list == otherList)
return toRenderListItem(renderer);
// We found ourself inside another list; lets skip the rest of it.
// Use nextInPreOrder() here because the other list itself may actually
// be a list item itself. We need to examine it, so we do this to counteract
// the previousInPreOrder() that will be done by the loop.
if (otherList)
renderer = otherList->renderer()->nextInPreOrder();
}
return 0;
}
inline int RenderListItem::calcValue() const
{
if (m_hasExplicitValue)
return m_explicitValue;
Node* list = enclosingList(this);
// FIXME: This recurses to a possible depth of the length of the list.
// That's not good -- we need to change this to an iterative algorithm.
if (RenderListItem* previousItem = previousListItem(list, this))
return previousItem->value() + 1;
if (list && list->hasTagName(olTag))
return static_cast<HTMLOListElement*>(list)->start();
return 1;
}
void RenderListItem::updateValueNow() const
{
m_value = calcValue();
m_isValueUpToDate = true;
}
bool RenderListItem::isEmpty() const
{
return lastChild() == m_marker;
}
static RenderObject* getParentOfFirstLineBox(RenderBlock* curr, RenderObject* marker)
{
RenderObject* firstChild = curr->firstChild();
if (!firstChild)
return 0;
for (RenderObject* currChild = firstChild; currChild; currChild = currChild->nextSibling()) {
if (currChild == marker)
continue;
if (currChild->isInline() && (!currChild->isRenderInline() || curr->generatesLineBoxesForInlineChild(currChild)))
return curr;
if (currChild->isFloating() || currChild->isPositioned())
continue;
if (currChild->isTable() || !currChild->isRenderBlock())
break;
if (curr->isListItem() && currChild->style()->htmlHacks() && currChild->node() &&
(currChild->node()->hasTagName(ulTag)|| currChild->node()->hasTagName(olTag)))
break;
RenderObject* lineBox = getParentOfFirstLineBox(toRenderBlock(currChild), marker);
if (lineBox)
return lineBox;
}
return 0;
}
void RenderListItem::updateValue()
{
if (!m_hasExplicitValue) {
m_isValueUpToDate = false;
if (m_marker)
m_marker->setNeedsLayoutAndPrefWidthsRecalc();
}
}
static RenderObject* firstNonMarkerChild(RenderObject* parent)
{
RenderObject* result = parent->firstChild();
while (result && result->isListMarker())
result = result->nextSibling();
return result;
}
void RenderListItem::updateMarkerLocation()
{
// Sanity check the location of our marker.
if (m_marker) {
RenderObject* markerPar = m_marker->parent();
RenderObject* lineBoxParent = getParentOfFirstLineBox(this, m_marker);
if (!lineBoxParent) {
// If the marker is currently contained inside an anonymous box,
// then we are the only item in that anonymous box (since no line box
// parent was found). It's ok to just leave the marker where it is
// in this case.
if (markerPar && markerPar->isAnonymousBlock())
lineBoxParent = markerPar;
else
lineBoxParent = this;
}
if (markerPar != lineBoxParent || m_marker->prefWidthsDirty()) {
// Removing and adding the marker can trigger repainting in
// containers other than ourselves, so we need to disable LayoutState.
view()->disableLayoutState();
updateFirstLetter();
m_marker->remove();
if (!lineBoxParent)
lineBoxParent = this;
lineBoxParent->addChild(m_marker, firstNonMarkerChild(lineBoxParent));
if (m_marker->prefWidthsDirty())
m_marker->calcPrefWidths();
view()->enableLayoutState();
}
}
}
void RenderListItem::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
updateMarkerLocation();
RenderBlock::calcPrefWidths();
}
void RenderListItem::layout()
{
ASSERT(needsLayout());
updateMarkerLocation();
RenderBlock::layout();
}
void RenderListItem::positionListMarker()
{
if (m_marker && !m_marker->isInside() && m_marker->inlineBoxWrapper()) {
int markerOldX = m_marker->x();
int yOffset = 0;
int xOffset = 0;
for (RenderBox* o = m_marker->parentBox(); o != this; o = o->parentBox()) {
yOffset += o->y();
xOffset += o->x();
}
bool adjustOverflow = false;
int markerXPos;
RootInlineBox* root = m_marker->inlineBoxWrapper()->root();
// FIXME: Inline flows in the line box hierarchy that have self-painting layers should act as cutoff points
// and really shouldn't keep propagating overflow up. This won't really break anything other than repainting
// not being as tight as it could be though.
if (style()->direction() == LTR) {
int leftLineOffset = leftRelOffset(yOffset, leftOffset(yOffset, false), false);
markerXPos = leftLineOffset - xOffset - paddingLeft() - borderLeft() + m_marker->marginLeft();
m_marker->inlineBoxWrapper()->adjustPosition(markerXPos - markerOldX, 0);
for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
if (markerXPos < box->leftLayoutOverflow()) {
box->setHorizontalOverflowPositions(markerXPos, box->rightLayoutOverflow(), box->leftVisualOverflow(), box->rightVisualOverflow());
if (box == root)
adjustOverflow = true;
}
}
} else {
int rightLineOffset = rightRelOffset(yOffset, rightOffset(yOffset, false), false);
markerXPos = rightLineOffset - xOffset + paddingRight() + borderRight() + m_marker->marginLeft();
m_marker->inlineBoxWrapper()->adjustPosition(markerXPos - markerOldX, 0);
for (InlineFlowBox* box = m_marker->inlineBoxWrapper()->parent(); box; box = box->parent()) {
if (markerXPos + m_marker->width() > box->rightLayoutOverflow()) {
box->setHorizontalOverflowPositions(box->leftLayoutOverflow(), markerXPos + m_marker->width(), box->leftVisualOverflow(), box->rightVisualOverflow());
if (box == root)
adjustOverflow = true;
}
}
}
if (adjustOverflow) {
IntRect markerRect(markerXPos + xOffset, yOffset, m_marker->width(), m_marker->height());
RenderBox* o = m_marker;
do {
o = o->parentBox();
if (o->isRenderBlock())
toRenderBlock(o)->addLayoutOverflow(markerRect);
markerRect.move(-o->x(), -o->y());
} while (o != this && !o->hasSelfPaintingLayer());
}
}
}
void RenderListItem::paint(PaintInfo& paintInfo, int tx, int ty)
{
if (!height())
return;
RenderBlock::paint(paintInfo, tx, ty);
}
const String& RenderListItem::markerText() const
{
if (m_marker)
return m_marker->text();
DEFINE_STATIC_LOCAL(String, staticNullString, ());
return staticNullString;
}
void RenderListItem::explicitValueChanged()
{
if (m_marker)
m_marker->setNeedsLayoutAndPrefWidthsRecalc();
Node* listNode = enclosingList(node());
RenderObject* listRenderer = 0;
if (listNode)
listRenderer = listNode->renderer();
for (RenderObject* renderer = this; renderer; renderer = renderer->nextInPreOrder(listRenderer))
if (renderer->isListItem()) {
RenderListItem* item = toRenderListItem(renderer);
if (!item->m_hasExplicitValue) {
item->m_isValueUpToDate = false;
if (RenderListMarker* marker = item->m_marker)
marker->setNeedsLayoutAndPrefWidthsRecalc();
}
}
}
void RenderListItem::setExplicitValue(int value)
{
ASSERT(node());
if (m_hasExplicitValue && m_explicitValue == value)
return;
m_explicitValue = value;
m_value = value;
m_hasExplicitValue = true;
explicitValueChanged();
}
void RenderListItem::clearExplicitValue()
{
ASSERT(node());
if (!m_hasExplicitValue)
return;
m_hasExplicitValue = false;
m_isValueUpToDate = false;
explicitValueChanged();
}
} // namespace WebCore
|