RenderListItem.cpp 19 KB

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