CounterNode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
  3. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include "config.h"
  22. #include "CounterNode.h"
  23. #include "RenderCounter.h"
  24. #include "RenderObject.h"
  25. #include <stdio.h>
  26. namespace WebCore {
  27. CounterNode::CounterNode(RenderObject* o, bool hasResetType, int value)
  28. : m_hasResetType(hasResetType)
  29. , m_value(value)
  30. , m_countInParent(0)
  31. , m_owner(o)
  32. , m_rootRenderer(0)
  33. , m_parent(0)
  34. , m_previousSibling(0)
  35. , m_nextSibling(0)
  36. , m_firstChild(0)
  37. , m_lastChild(0)
  38. {
  39. }
  40. CounterNode::~CounterNode()
  41. {
  42. // Ideally this would be an assert and this would never be reached. In reality this happens a lot
  43. // so we need to handle these cases. The node is still connected to the tree so we need to detach it.
  44. if (m_parent || m_previousSibling || m_nextSibling || m_firstChild || m_lastChild) {
  45. CounterNode* oldParent = 0;
  46. CounterNode* oldPreviousSibling = 0;
  47. // Instead of calling removeChild() we do this safely as the tree is likely broken if we get here.
  48. if (m_parent) {
  49. if (m_parent->m_firstChild == this)
  50. m_parent->m_firstChild = m_nextSibling;
  51. if (m_parent->m_lastChild == this)
  52. m_parent->m_lastChild = m_previousSibling;
  53. oldParent = m_parent;
  54. m_parent = 0;
  55. }
  56. if (m_previousSibling) {
  57. if (m_previousSibling->m_nextSibling == this)
  58. m_previousSibling->m_nextSibling = m_nextSibling;
  59. oldPreviousSibling = m_previousSibling;
  60. m_previousSibling = 0;
  61. }
  62. if (m_nextSibling) {
  63. if (m_nextSibling->m_previousSibling == this)
  64. m_nextSibling->m_previousSibling = oldPreviousSibling;
  65. m_nextSibling = 0;
  66. }
  67. if (m_firstChild) {
  68. // The node's children are reparented to the old parent.
  69. for (CounterNode* child = m_firstChild; child; ) {
  70. CounterNode* nextChild = child->m_nextSibling;
  71. CounterNode* nextSibling = 0;
  72. child->m_parent = oldParent;
  73. if (oldPreviousSibling) {
  74. nextSibling = oldPreviousSibling->m_nextSibling;
  75. child->m_previousSibling = oldPreviousSibling;
  76. oldPreviousSibling->m_nextSibling = child;
  77. child->m_nextSibling = nextSibling;
  78. nextSibling->m_previousSibling = child;
  79. oldPreviousSibling = child;
  80. }
  81. child = nextChild;
  82. }
  83. }
  84. }
  85. resetRenderers();
  86. }
  87. PassRefPtr<CounterNode> CounterNode::create(RenderObject* owner, bool hasResetType, int value)
  88. {
  89. return adoptRef(new CounterNode(owner, hasResetType, value));
  90. }
  91. CounterNode* CounterNode::nextInPreOrderAfterChildren(const CounterNode* stayWithin) const
  92. {
  93. if (this == stayWithin)
  94. return 0;
  95. const CounterNode* current = this;
  96. CounterNode* next;
  97. while (!(next = current->m_nextSibling)) {
  98. current = current->m_parent;
  99. if (!current || current == stayWithin)
  100. return 0;
  101. }
  102. return next;
  103. }
  104. CounterNode* CounterNode::nextInPreOrder(const CounterNode* stayWithin) const
  105. {
  106. if (CounterNode* next = m_firstChild)
  107. return next;
  108. return nextInPreOrderAfterChildren(stayWithin);
  109. }
  110. CounterNode* CounterNode::lastDescendant() const
  111. {
  112. CounterNode* last = m_lastChild;
  113. if (!last)
  114. return 0;
  115. while (CounterNode* lastChild = last->m_lastChild)
  116. last = lastChild;
  117. return last;
  118. }
  119. CounterNode* CounterNode::previousInPreOrder() const
  120. {
  121. CounterNode* previous = m_previousSibling;
  122. if (!previous)
  123. return m_parent;
  124. while (CounterNode* lastChild = previous->m_lastChild)
  125. previous = lastChild;
  126. return previous;
  127. }
  128. int CounterNode::computeCountInParent() const
  129. {
  130. int increment = actsAsReset() ? 0 : m_value;
  131. if (m_previousSibling)
  132. return m_previousSibling->m_countInParent + increment;
  133. ASSERT(m_parent->m_firstChild == this);
  134. return m_parent->m_value + increment;
  135. }
  136. void CounterNode::addRenderer(RenderCounter* value)
  137. {
  138. if (!value) {
  139. ASSERT_NOT_REACHED();
  140. return;
  141. }
  142. if (value->m_counterNode) {
  143. ASSERT_NOT_REACHED();
  144. value->m_counterNode->removeRenderer(value);
  145. }
  146. ASSERT(!value->m_nextForSameCounter);
  147. for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
  148. if (iterator == value) {
  149. ASSERT_NOT_REACHED();
  150. return;
  151. }
  152. }
  153. value->m_nextForSameCounter = m_rootRenderer;
  154. m_rootRenderer = value;
  155. if (value->m_counterNode != this) {
  156. if (value->m_counterNode) {
  157. ASSERT_NOT_REACHED();
  158. value->m_counterNode->removeRenderer(value);
  159. }
  160. value->m_counterNode = this;
  161. }
  162. }
  163. void CounterNode::removeRenderer(RenderCounter* value)
  164. {
  165. if (!value) {
  166. ASSERT_NOT_REACHED();
  167. return;
  168. }
  169. if (value->m_counterNode && value->m_counterNode != this) {
  170. ASSERT_NOT_REACHED();
  171. value->m_counterNode->removeRenderer(value);
  172. }
  173. RenderCounter* previous = 0;
  174. for (RenderCounter* iterator = m_rootRenderer;iterator; iterator = iterator->m_nextForSameCounter) {
  175. if (iterator == value) {
  176. if (previous)
  177. previous->m_nextForSameCounter = value->m_nextForSameCounter;
  178. else
  179. m_rootRenderer = value->m_nextForSameCounter;
  180. value->m_nextForSameCounter = 0;
  181. value->m_counterNode = 0;
  182. return;
  183. }
  184. previous = iterator;
  185. }
  186. ASSERT_NOT_REACHED();
  187. }
  188. void CounterNode::resetRenderers()
  189. {
  190. while (m_rootRenderer)
  191. m_rootRenderer->invalidate(); // This makes m_rootRenderer point to the next renderer if any since it disconnects the m_rootRenderer from this.
  192. }
  193. void CounterNode::resetThisAndDescendantsRenderers()
  194. {
  195. CounterNode* node = this;
  196. do {
  197. node->resetRenderers();
  198. node = node->nextInPreOrder(this);
  199. } while (node);
  200. }
  201. void CounterNode::recount()
  202. {
  203. for (CounterNode* node = this; node; node = node->m_nextSibling) {
  204. int oldCount = node->m_countInParent;
  205. int newCount = node->computeCountInParent();
  206. if (oldCount == newCount)
  207. break;
  208. node->m_countInParent = newCount;
  209. node->resetThisAndDescendantsRenderers();
  210. }
  211. }
  212. void CounterNode::insertAfter(CounterNode* newChild, CounterNode* refChild, const AtomicString& identifier)
  213. {
  214. ASSERT(newChild);
  215. ASSERT(!newChild->m_parent);
  216. ASSERT(!newChild->m_previousSibling);
  217. ASSERT(!newChild->m_nextSibling);
  218. // If the refChild is not our child we can not complete the request. This hardens against bugs in RenderCounter.
  219. // When renderers are reparented it may request that we insert counter nodes improperly.
  220. if (refChild && refChild->m_parent != this)
  221. return;
  222. if (newChild->m_hasResetType) {
  223. while (m_lastChild != refChild)
  224. RenderCounter::destroyCounterNode(m_lastChild->owner(), identifier);
  225. }
  226. CounterNode* next;
  227. if (refChild) {
  228. next = refChild->m_nextSibling;
  229. refChild->m_nextSibling = newChild;
  230. } else {
  231. next = m_firstChild;
  232. m_firstChild = newChild;
  233. }
  234. newChild->m_parent = this;
  235. newChild->m_previousSibling = refChild;
  236. if (next) {
  237. ASSERT(next->m_previousSibling == refChild);
  238. next->m_previousSibling = newChild;
  239. newChild->m_nextSibling = next;
  240. } else {
  241. ASSERT(m_lastChild == refChild);
  242. m_lastChild = newChild;
  243. }
  244. if (!newChild->m_firstChild || newChild->m_hasResetType) {
  245. newChild->m_countInParent = newChild->computeCountInParent();
  246. newChild->resetThisAndDescendantsRenderers();
  247. if (next)
  248. next->recount();
  249. return;
  250. }
  251. // The code below handles the case when a formerly root increment counter is loosing its root position
  252. // and therefore its children become next siblings.
  253. CounterNode* last = newChild->m_lastChild;
  254. CounterNode* first = newChild->m_firstChild;
  255. if (first) {
  256. ASSERT(last);
  257. newChild->m_nextSibling = first;
  258. if (m_lastChild == newChild)
  259. m_lastChild = last;
  260. first->m_previousSibling = newChild;
  261. // The case when the original next sibling of the inserted node becomes a child of
  262. // one of the former children of the inserted node is not handled as it is believed
  263. // to be impossible since:
  264. // 1. if the increment counter node lost it's root position as a result of another
  265. // counter node being created, it will be inserted as the last child so next is null.
  266. // 2. if the increment counter node lost it's root position as a result of a renderer being
  267. // inserted into the document's render tree, all its former children counters are attached
  268. // to children of the inserted renderer and hence cannot be in scope for counter nodes
  269. // attached to renderers that were already in the document's render tree.
  270. last->m_nextSibling = next;
  271. if (next) {
  272. ASSERT(next->m_previousSibling == newChild);
  273. next->m_previousSibling = last;
  274. } else
  275. m_lastChild = last;
  276. for (next = first; ; next = next->m_nextSibling) {
  277. next->m_parent = this;
  278. if (last == next)
  279. break;
  280. }
  281. }
  282. newChild->m_firstChild = 0;
  283. newChild->m_lastChild = 0;
  284. newChild->m_countInParent = newChild->computeCountInParent();
  285. newChild->resetRenderers();
  286. first->recount();
  287. }
  288. void CounterNode::removeChild(CounterNode* oldChild)
  289. {
  290. ASSERT(oldChild);
  291. ASSERT(!oldChild->m_firstChild);
  292. ASSERT(!oldChild->m_lastChild);
  293. CounterNode* next = oldChild->m_nextSibling;
  294. CounterNode* previous = oldChild->m_previousSibling;
  295. oldChild->m_nextSibling = 0;
  296. oldChild->m_previousSibling = 0;
  297. oldChild->m_parent = 0;
  298. if (previous)
  299. previous->m_nextSibling = next;
  300. else {
  301. ASSERT(m_firstChild == oldChild);
  302. m_firstChild = next;
  303. }
  304. if (next)
  305. next->m_previousSibling = previous;
  306. else {
  307. ASSERT(m_lastChild == oldChild);
  308. m_lastChild = previous;
  309. }
  310. if (next)
  311. next->recount();
  312. }
  313. #ifndef NDEBUG
  314. static void showTreeAndMark(const CounterNode* node)
  315. {
  316. const CounterNode* root = node;
  317. while (root->parent())
  318. root = root->parent();
  319. for (const CounterNode* current = root; current; current = current->nextInPreOrder()) {
  320. fprintf(stderr, "%c", (current == node) ? '*' : ' ');
  321. for (const CounterNode* parent = current; parent && parent != root; parent = parent->parent())
  322. fprintf(stderr, " ");
  323. fprintf(stderr, "%p %s: %d %d P:%p PS:%p NS:%p R:%p\n",
  324. current, current->actsAsReset() ? "reset____" : "increment", current->value(),
  325. current->countInParent(), current->parent(), current->previousSibling(),
  326. current->nextSibling(), current->owner());
  327. }
  328. fflush(stderr);
  329. }
  330. #endif
  331. } // namespace WebCore
  332. #ifndef NDEBUG
  333. void showCounterTree(const WebCore::CounterNode* counter)
  334. {
  335. if (counter)
  336. showTreeAndMark(counter);
  337. }
  338. #endif