RenderRegion.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  21. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  26. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #include "RenderRegion.h"
  31. #include "FlowThreadController.h"
  32. #include "GraphicsContext.h"
  33. #include "HitTestResult.h"
  34. #include "IntRect.h"
  35. #include "LayoutRepainter.h"
  36. #include "PaintInfo.h"
  37. #include "Range.h"
  38. #include "RenderBoxRegionInfo.h"
  39. #include "RenderNamedFlowThread.h"
  40. #include "RenderView.h"
  41. #include "StyleResolver.h"
  42. #include <wtf/StackStats.h>
  43. using namespace std;
  44. namespace WebCore {
  45. RenderRegion::RenderRegion(Element* element, RenderFlowThread* flowThread)
  46. : RenderBlock(element)
  47. , m_flowThread(flowThread)
  48. , m_parentNamedFlowThread(0)
  49. , m_isValid(false)
  50. , m_hasCustomRegionStyle(false)
  51. , m_hasAutoLogicalHeight(false)
  52. , m_regionState(RegionUndefined)
  53. {
  54. }
  55. LayoutUnit RenderRegion::pageLogicalWidth() const
  56. {
  57. ASSERT(m_flowThread);
  58. return m_flowThread->isHorizontalWritingMode() ? contentWidth() : contentHeight();
  59. }
  60. LayoutUnit RenderRegion::pageLogicalHeight() const
  61. {
  62. ASSERT(m_flowThread);
  63. if (hasOverrideHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
  64. ASSERT(hasAutoLogicalHeight());
  65. return overrideLogicalContentHeight();
  66. }
  67. return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
  68. }
  69. // This method returns the maximum page size of a region with auto-height. This is the initial
  70. // height value for auto-height regions in the first layout phase of the parent named flow.
  71. LayoutUnit RenderRegion::maxPageLogicalHeight() const
  72. {
  73. ASSERT(m_flowThread);
  74. ASSERT(hasAutoLogicalHeight() && !m_flowThread->inConstrainedLayoutPhase());
  75. return style()->logicalMaxHeight().isUndefined() ? LayoutUnit::max() / 2 : computeReplacedLogicalHeightUsing(style()->logicalMaxHeight());
  76. }
  77. LayoutUnit RenderRegion::logicalHeightOfAllFlowThreadContent() const
  78. {
  79. ASSERT(m_flowThread);
  80. if (hasOverrideHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
  81. ASSERT(hasAutoLogicalHeight());
  82. return overrideLogicalContentHeight();
  83. }
  84. return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
  85. }
  86. LayoutRect RenderRegion::flowThreadPortionOverflowRect() const
  87. {
  88. return overflowRectForFlowThreadPortion(flowThreadPortionRect(), isFirstRegion(), isLastRegion());
  89. }
  90. LayoutRect RenderRegion::overflowRectForFlowThreadPortion(const LayoutRect& flowThreadPortionRect, bool isFirstPortion, bool isLastPortion) const
  91. {
  92. ASSERT(isValid());
  93. // FIXME: Would like to just use hasOverflowClip() but we aren't a block yet. When RenderRegion is eliminated and
  94. // folded into RenderBlock, switch to hasOverflowClip().
  95. bool clipX = style()->overflowX() != OVISIBLE;
  96. bool clipY = style()->overflowY() != OVISIBLE;
  97. bool isLastRegionWithRegionFragmentBreak = (isLastPortion && (style()->regionFragment() == BreakRegionFragment));
  98. if ((clipX && clipY) || isLastRegionWithRegionFragmentBreak)
  99. return flowThreadPortionRect;
  100. LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
  101. // Only clip along the flow thread axis.
  102. LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
  103. LayoutRect clipRect;
  104. if (m_flowThread->isHorizontalWritingMode()) {
  105. LayoutUnit minY = isFirstPortion ? (flowThreadOverflow.y() - outlineSize) : flowThreadPortionRect.y();
  106. LayoutUnit maxY = isLastPortion ? max(flowThreadPortionRect.maxY(), flowThreadOverflow.maxY()) + outlineSize : flowThreadPortionRect.maxY();
  107. LayoutUnit minX = clipX ? flowThreadPortionRect.x() : min(flowThreadPortionRect.x(), flowThreadOverflow.x() - outlineSize);
  108. LayoutUnit maxX = clipX ? flowThreadPortionRect.maxX() : max(flowThreadPortionRect.maxX(), (flowThreadOverflow.maxX() + outlineSize));
  109. clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
  110. } else {
  111. LayoutUnit minX = isFirstPortion ? (flowThreadOverflow.x() - outlineSize) : flowThreadPortionRect.x();
  112. LayoutUnit maxX = isLastPortion ? max(flowThreadPortionRect.maxX(), flowThreadOverflow.maxX()) + outlineSize : flowThreadPortionRect.maxX();
  113. LayoutUnit minY = clipY ? flowThreadPortionRect.y() : min(flowThreadPortionRect.y(), (flowThreadOverflow.y() - outlineSize));
  114. LayoutUnit maxY = clipY ? flowThreadPortionRect.maxY() : max(flowThreadPortionRect.y(), (flowThreadOverflow.maxY() + outlineSize));
  115. clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
  116. }
  117. return clipRect;
  118. }
  119. LayoutUnit RenderRegion::pageLogicalTopForOffset(LayoutUnit /* offset */) const
  120. {
  121. return flowThread()->isHorizontalWritingMode() ? flowThreadPortionRect().y() : flowThreadPortionRect().x();
  122. }
  123. bool RenderRegion::isFirstRegion() const
  124. {
  125. ASSERT(isValid());
  126. return m_flowThread->firstRegion() == this;
  127. }
  128. bool RenderRegion::isLastRegion() const
  129. {
  130. ASSERT(isValid());
  131. return m_flowThread->lastRegion() == this;
  132. }
  133. static bool shouldPaintRegionContentsInPhase(PaintPhase phase)
  134. {
  135. return phase == PaintPhaseBlockBackground
  136. || phase == PaintPhaseChildBlockBackground
  137. || phase == PaintPhaseSelection
  138. || phase == PaintPhaseTextClip;
  139. }
  140. void RenderRegion::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
  141. {
  142. if (style()->visibility() != VISIBLE)
  143. return;
  144. RenderBlock::paintObject(paintInfo, paintOffset);
  145. if (!isValid())
  146. return;
  147. // We do not want to paint a region's contents multiple times (for each paint phase of the region object).
  148. // Thus, we only paint the region's contents in certain phases.
  149. if (!shouldPaintRegionContentsInPhase(paintInfo.phase))
  150. return;
  151. // Delegate the painting of a region's contents to RenderFlowThread.
  152. // RenderFlowThread is a self painting layer because it's a positioned object.
  153. // RenderFlowThread paints its children, the collected objects.
  154. setRegionObjectsRegionStyle();
  155. m_flowThread->paintFlowThreadPortionInRegion(paintInfo, this, flowThreadPortionRect(), flowThreadPortionOverflowRect(), LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
  156. restoreRegionObjectsOriginalStyle();
  157. }
  158. // Hit Testing
  159. bool RenderRegion::hitTestContents(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
  160. {
  161. if (!isValid() || action != HitTestForeground)
  162. return false;
  163. LayoutRect boundsRect = borderBoxRectInRegion(locationInContainer.region());
  164. boundsRect.moveBy(accumulatedOffset);
  165. if (visibleToHitTesting() && locationInContainer.intersects(boundsRect)) {
  166. if (m_flowThread->hitTestFlowThreadPortionInRegion(this, flowThreadPortionRect(), flowThreadPortionOverflowRect(), request, result,
  167. locationInContainer, LayoutPoint(accumulatedOffset.x() + borderLeft() + paddingLeft(), accumulatedOffset.y() + borderTop() + paddingTop())))
  168. return true;
  169. }
  170. return false;
  171. }
  172. void RenderRegion::checkRegionStyle()
  173. {
  174. ASSERT(m_flowThread);
  175. bool customRegionStyle = false;
  176. // FIXME: Region styling doesn't work for pseudo elements.
  177. if (node()) {
  178. Element* regionElement = toElement(node());
  179. customRegionStyle = view()->document()->ensureStyleResolver()->checkRegionStyle(regionElement);
  180. }
  181. setHasCustomRegionStyle(customRegionStyle);
  182. m_flowThread->checkRegionsWithStyling();
  183. }
  184. void RenderRegion::incrementAutoLogicalHeightCount()
  185. {
  186. ASSERT(isValid());
  187. ASSERT(m_hasAutoLogicalHeight);
  188. m_flowThread->incrementAutoLogicalHeightRegions();
  189. }
  190. void RenderRegion::decrementAutoLogicalHeightCount()
  191. {
  192. ASSERT(isValid());
  193. m_flowThread->decrementAutoLogicalHeightRegions();
  194. }
  195. void RenderRegion::updateRegionHasAutoLogicalHeightFlag()
  196. {
  197. ASSERT(m_flowThread);
  198. if (!isValid())
  199. return;
  200. bool didHaveAutoLogicalHeight = m_hasAutoLogicalHeight;
  201. m_hasAutoLogicalHeight = shouldHaveAutoLogicalHeight();
  202. if (m_hasAutoLogicalHeight != didHaveAutoLogicalHeight) {
  203. if (m_hasAutoLogicalHeight)
  204. incrementAutoLogicalHeightCount();
  205. else {
  206. clearOverrideLogicalContentHeight();
  207. decrementAutoLogicalHeightCount();
  208. }
  209. }
  210. }
  211. bool RenderRegion::shouldHaveAutoLogicalHeight() const
  212. {
  213. bool hasSpecifiedEndpointsForHeight = style()->logicalTop().isSpecified() && style()->logicalBottom().isSpecified();
  214. bool hasAnchoredEndpointsForHeight = isOutOfFlowPositioned() && hasSpecifiedEndpointsForHeight;
  215. return style()->logicalHeight().isAuto() && !hasAnchoredEndpointsForHeight;
  216. }
  217. void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
  218. {
  219. RenderBlock::styleDidChange(diff, oldStyle);
  220. // If the region is not attached to any thread, there is no need to check
  221. // whether the region has region styling since no content will be displayed
  222. // into the region.
  223. if (!m_flowThread) {
  224. setHasCustomRegionStyle(false);
  225. return;
  226. }
  227. checkRegionStyle();
  228. updateRegionHasAutoLogicalHeightFlag();
  229. }
  230. void RenderRegion::layoutBlock(bool relayoutChildren, LayoutUnit)
  231. {
  232. StackStats::LayoutCheckPoint layoutCheckPoint;
  233. RenderBlock::layoutBlock(relayoutChildren);
  234. if (isValid()) {
  235. LayoutRect oldRegionRect(flowThreadPortionRect());
  236. if (!isHorizontalWritingMode())
  237. oldRegionRect = oldRegionRect.transposedRect();
  238. if (hasAutoLogicalHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
  239. m_flowThread->invalidateRegions();
  240. clearOverrideLogicalContentHeight();
  241. return;
  242. }
  243. if (!isRenderRegionSet() && (oldRegionRect.width() != pageLogicalWidth() || oldRegionRect.height() != pageLogicalHeight()))
  244. // This can happen even if we are in the inConstrainedLayoutPhase and it will trigger a pathological layout of the flow thread.
  245. m_flowThread->invalidateRegions();
  246. }
  247. // FIXME: We need to find a way to set up overflow properly. Our flow thread hasn't gotten a layout
  248. // yet, so we can't look to it for correct information. It's possible we could wait until after the RenderFlowThread
  249. // gets a layout, and then try to propagate overflow information back to the region, and then mark for a second layout.
  250. // That second layout would then be able to use the information from the RenderFlowThread to set up overflow.
  251. //
  252. // The big problem though is that overflow needs to be region-specific. We can't simply use the RenderFlowThread's global
  253. // overflow values, since then we'd always think any narrow region had huge overflow (all the way to the width of the
  254. // RenderFlowThread itself).
  255. //
  256. // We'll need to expand RenderBoxRegionInfo to also hold left and right overflow values.
  257. }
  258. void RenderRegion::repaintFlowThreadContent(const LayoutRect& repaintRect, bool immediate) const
  259. {
  260. repaintFlowThreadContentRectangle(repaintRect, immediate, flowThreadPortionRect(), flowThreadPortionOverflowRect(), contentBoxRect().location());
  261. }
  262. void RenderRegion::repaintFlowThreadContentRectangle(const LayoutRect& repaintRect, bool immediate, const LayoutRect& flowThreadPortionRect, const LayoutRect& flowThreadPortionOverflowRect, const LayoutPoint& regionLocation) const
  263. {
  264. ASSERT(isValid());
  265. // We only have to issue a repaint in this region if the region rect intersects the repaint rect.
  266. LayoutRect flippedFlowThreadPortionRect(flowThreadPortionRect);
  267. LayoutRect flippedFlowThreadPortionOverflowRect(flowThreadPortionOverflowRect);
  268. flowThread()->flipForWritingMode(flippedFlowThreadPortionRect); // Put the region rects into physical coordinates.
  269. flowThread()->flipForWritingMode(flippedFlowThreadPortionOverflowRect);
  270. LayoutRect clippedRect(repaintRect);
  271. clippedRect.intersect(flippedFlowThreadPortionOverflowRect);
  272. if (clippedRect.isEmpty())
  273. return;
  274. // Put the region rect into the region's physical coordinate space.
  275. clippedRect.setLocation(regionLocation + (clippedRect.location() - flippedFlowThreadPortionRect.location()));
  276. // Now switch to the region's writing mode coordinate space and let it repaint itself.
  277. flipForWritingMode(clippedRect);
  278. // Issue the repaint.
  279. repaintRectangle(clippedRect, immediate);
  280. }
  281. void RenderRegion::installFlowThread()
  282. {
  283. ASSERT(view());
  284. m_flowThread = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
  285. // By now the flow thread should already be added to the rendering tree,
  286. // so we go up the rendering parents and check that this region is not part of the same
  287. // flow that it actually needs to display. It would create a circular reference.
  288. RenderObject* parentObject = parent();
  289. m_parentNamedFlowThread = 0;
  290. for ( ; parentObject; parentObject = parentObject->parent()) {
  291. if (parentObject->isRenderNamedFlowThread()) {
  292. m_parentNamedFlowThread = toRenderNamedFlowThread(parentObject);
  293. // Do not take into account a region that links a flow with itself. The dependency
  294. // cannot change, so it is not worth adding it to the list.
  295. if (m_flowThread == m_parentNamedFlowThread)
  296. m_flowThread = 0;
  297. break;
  298. }
  299. }
  300. }
  301. void RenderRegion::attachRegion()
  302. {
  303. if (documentBeingDestroyed())
  304. return;
  305. // A region starts off invalid.
  306. setIsValid(false);
  307. // Initialize the flow thread reference and create the flow thread object if needed.
  308. // The flow thread lifetime is influenced by the number of regions attached to it,
  309. // and we are attaching the region to the flow thread.
  310. installFlowThread();
  311. if (!m_flowThread)
  312. return;
  313. // Only after adding the region to the thread, the region is marked to be valid.
  314. m_flowThread->addRegionToThread(this);
  315. // The region just got attached to the flow thread, lets check whether
  316. // it has region styling rules associated.
  317. checkRegionStyle();
  318. if (!isValid())
  319. return;
  320. m_hasAutoLogicalHeight = shouldHaveAutoLogicalHeight();
  321. if (hasAutoLogicalHeight())
  322. incrementAutoLogicalHeightCount();
  323. }
  324. void RenderRegion::detachRegion()
  325. {
  326. if (m_flowThread) {
  327. m_flowThread->removeRegionFromThread(this);
  328. if (hasAutoLogicalHeight())
  329. decrementAutoLogicalHeightCount();
  330. }
  331. m_flowThread = 0;
  332. }
  333. RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
  334. {
  335. ASSERT(isValid());
  336. return m_renderBoxRegionInfo.get(box);
  337. }
  338. RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
  339. bool containingBlockChainIsInset)
  340. {
  341. ASSERT(isValid());
  342. OwnPtr<RenderBoxRegionInfo>& boxInfo = m_renderBoxRegionInfo.add(box, nullptr).iterator->value;
  343. if (boxInfo)
  344. *boxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
  345. else
  346. boxInfo = adoptPtr(new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset));
  347. return boxInfo.get();
  348. }
  349. PassOwnPtr<RenderBoxRegionInfo> RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
  350. {
  351. return m_renderBoxRegionInfo.take(box);
  352. }
  353. void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
  354. {
  355. m_renderBoxRegionInfo.remove(box);
  356. }
  357. void RenderRegion::deleteAllRenderBoxRegionInfo()
  358. {
  359. m_renderBoxRegionInfo.clear();
  360. }
  361. LayoutUnit RenderRegion::logicalTopOfFlowThreadContentRect(const LayoutRect& rect) const
  362. {
  363. ASSERT(isValid());
  364. return flowThread()->isHorizontalWritingMode() ? rect.y() : rect.x();
  365. }
  366. LayoutUnit RenderRegion::logicalBottomOfFlowThreadContentRect(const LayoutRect& rect) const
  367. {
  368. ASSERT(isValid());
  369. return flowThread()->isHorizontalWritingMode() ? rect.maxY() : rect.maxX();
  370. }
  371. void RenderRegion::setRegionObjectsRegionStyle()
  372. {
  373. if (!hasCustomRegionStyle())
  374. return;
  375. // Start from content nodes and recursively compute the style in region for the render objects below.
  376. // If the style in region was already computed, used that style instead of computing a new one.
  377. RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
  378. const NamedFlowContentNodes& contentNodes = namedFlow->contentNodes();
  379. for (NamedFlowContentNodes::const_iterator iter = contentNodes.begin(), end = contentNodes.end(); iter != end; ++iter) {
  380. const Node* node = *iter;
  381. // The list of content nodes contains also the nodes with display:none.
  382. if (!node->renderer())
  383. continue;
  384. RenderObject* object = node->renderer();
  385. // If the content node does not flow any of its children in this region,
  386. // we do not compute any style for them in this region.
  387. if (!flowThread()->objectInFlowRegion(object, this))
  388. continue;
  389. // If the object has style in region, use that instead of computing a new one.
  390. RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(object);
  391. RefPtr<RenderStyle> objectStyleInRegion;
  392. bool objectRegionStyleCached = false;
  393. if (it != m_renderObjectRegionStyle.end()) {
  394. objectStyleInRegion = it->value.style;
  395. ASSERT(it->value.cached);
  396. objectRegionStyleCached = true;
  397. } else
  398. objectStyleInRegion = computeStyleInRegion(object);
  399. setObjectStyleInRegion(object, objectStyleInRegion, objectRegionStyleCached);
  400. computeChildrenStyleInRegion(object);
  401. }
  402. }
  403. void RenderRegion::restoreRegionObjectsOriginalStyle()
  404. {
  405. if (!hasCustomRegionStyle())
  406. return;
  407. RenderObjectRegionStyleMap temp;
  408. for (RenderObjectRegionStyleMap::iterator iter = m_renderObjectRegionStyle.begin(), end = m_renderObjectRegionStyle.end(); iter != end; ++iter) {
  409. RenderObject* object = const_cast<RenderObject*>(iter->key);
  410. RefPtr<RenderStyle> objectRegionStyle = object->style();
  411. RefPtr<RenderStyle> objectOriginalStyle = iter->value.style;
  412. object->setStyleInternal(objectOriginalStyle);
  413. bool shouldCacheRegionStyle = iter->value.cached;
  414. if (!shouldCacheRegionStyle) {
  415. // Check whether we should cache the computed style in region.
  416. unsigned changedContextSensitiveProperties = ContextSensitivePropertyNone;
  417. StyleDifference styleDiff = objectOriginalStyle->diff(objectRegionStyle.get(), changedContextSensitiveProperties);
  418. if (styleDiff < StyleDifferenceLayoutPositionedMovementOnly)
  419. shouldCacheRegionStyle = true;
  420. }
  421. if (shouldCacheRegionStyle) {
  422. ObjectRegionStyleInfo styleInfo;
  423. styleInfo.style = objectRegionStyle;
  424. styleInfo.cached = true;
  425. temp.set(object, styleInfo);
  426. }
  427. }
  428. m_renderObjectRegionStyle.swap(temp);
  429. }
  430. void RenderRegion::insertedIntoTree()
  431. {
  432. RenderBlock::insertedIntoTree();
  433. attachRegion();
  434. }
  435. void RenderRegion::willBeRemovedFromTree()
  436. {
  437. RenderBlock::willBeRemovedFromTree();
  438. detachRegion();
  439. }
  440. PassRefPtr<RenderStyle> RenderRegion::computeStyleInRegion(const RenderObject* object)
  441. {
  442. ASSERT(object);
  443. ASSERT(object->view());
  444. ASSERT(object->view()->document());
  445. ASSERT(!object->isAnonymous());
  446. ASSERT(object->node() && object->node()->isElementNode());
  447. // FIXME: Region styling fails for pseudo-elements because the renderers don't have a node.
  448. Element* element = toElement(object->node());
  449. RefPtr<RenderStyle> renderObjectRegionStyle = object->view()->document()->ensureStyleResolver()->styleForElement(element, 0, DisallowStyleSharing, MatchAllRules, this);
  450. return renderObjectRegionStyle.release();
  451. }
  452. void RenderRegion::computeChildrenStyleInRegion(const RenderObject* object)
  453. {
  454. for (RenderObject* child = object->firstChild(); child; child = child->nextSibling()) {
  455. RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(child);
  456. RefPtr<RenderStyle> childStyleInRegion;
  457. bool objectRegionStyleCached = false;
  458. if (it != m_renderObjectRegionStyle.end()) {
  459. childStyleInRegion = it->value.style;
  460. objectRegionStyleCached = true;
  461. } else {
  462. if (child->isAnonymous() || child->isInFlowRenderFlowThread())
  463. childStyleInRegion = RenderStyle::createAnonymousStyleWithDisplay(object->style(), child->style()->display());
  464. else if (child->isText())
  465. childStyleInRegion = RenderStyle::clone(object->style());
  466. else
  467. childStyleInRegion = computeStyleInRegion(child);
  468. }
  469. setObjectStyleInRegion(child, childStyleInRegion, objectRegionStyleCached);
  470. computeChildrenStyleInRegion(child);
  471. }
  472. }
  473. void RenderRegion::setObjectStyleInRegion(RenderObject* object, PassRefPtr<RenderStyle> styleInRegion, bool objectRegionStyleCached)
  474. {
  475. ASSERT(object->flowThreadContainingBlock());
  476. RefPtr<RenderStyle> objectOriginalStyle = object->style();
  477. object->setStyleInternal(styleInRegion);
  478. if (object->isBoxModelObject() && !object->hasBoxDecorations()) {
  479. bool hasBoxDecorations = object->isTableCell()
  480. || object->style()->hasBackground()
  481. || object->style()->hasBorder()
  482. || object->style()->hasAppearance()
  483. || object->style()->boxShadow();
  484. object->setHasBoxDecorations(hasBoxDecorations);
  485. }
  486. ObjectRegionStyleInfo styleInfo;
  487. styleInfo.style = objectOriginalStyle;
  488. styleInfo.cached = objectRegionStyleCached;
  489. m_renderObjectRegionStyle.set(object, styleInfo);
  490. }
  491. void RenderRegion::clearObjectStyleInRegion(const RenderObject* object)
  492. {
  493. ASSERT(object);
  494. m_renderObjectRegionStyle.remove(object);
  495. // Clear the style for the children of this object.
  496. for (RenderObject* child = object->firstChild(); child; child = child->nextSibling())
  497. clearObjectStyleInRegion(child);
  498. }
  499. void RenderRegion::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
  500. {
  501. if (!isValid()) {
  502. RenderBlock::computeIntrinsicLogicalWidths(minLogicalWidth, maxLogicalWidth);
  503. return;
  504. }
  505. minLogicalWidth = m_flowThread->minPreferredLogicalWidth();
  506. maxLogicalWidth = m_flowThread->maxPreferredLogicalWidth();
  507. }
  508. void RenderRegion::computePreferredLogicalWidths()
  509. {
  510. ASSERT(preferredLogicalWidthsDirty());
  511. if (!isValid()) {
  512. RenderBlock::computePreferredLogicalWidths();
  513. return;
  514. }
  515. // FIXME: Currently, the code handles only the <length> case for min-width/max-width.
  516. // It should also support other values, like percentage, calc or viewport relative.
  517. m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
  518. RenderStyle* styleToUse = style();
  519. if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() > 0)
  520. m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
  521. else
  522. computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
  523. if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
  524. m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
  525. m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
  526. }
  527. if (styleToUse->logicalMaxWidth().isFixed()) {
  528. m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
  529. m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
  530. }
  531. LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
  532. m_minPreferredLogicalWidth += borderAndPadding;
  533. m_maxPreferredLogicalWidth += borderAndPadding;
  534. setPreferredLogicalWidthsDirty(false);
  535. }
  536. void RenderRegion::getRanges(Vector<RefPtr<Range> >& rangeObjects) const
  537. {
  538. RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
  539. namedFlow->getRanges(rangeObjects, this);
  540. }
  541. void RenderRegion::updateLogicalHeight()
  542. {
  543. RenderBlock::updateLogicalHeight();
  544. if (!hasAutoLogicalHeight())
  545. return;
  546. // We want to update the logical height based on the computed override logical
  547. // content height only if the view is in the layout phase
  548. // in which all the auto logical height regions have their override logical height set.
  549. if (!m_flowThread->inConstrainedLayoutPhase())
  550. return;
  551. // There may be regions with auto logical height that during the prerequisite layout phase
  552. // did not have the chance to layout flow thread content. Because of that, these regions do not
  553. // have an overrideLogicalContentHeight computed and they will not be able to fragment any flow
  554. // thread content.
  555. if (!hasOverrideHeight())
  556. return;
  557. LayoutUnit newLogicalHeight = overrideLogicalContentHeight() + borderAndPaddingLogicalHeight();
  558. ASSERT(newLogicalHeight < LayoutUnit::max() / 2);
  559. if (newLogicalHeight > logicalHeight()) {
  560. setLogicalHeight(newLogicalHeight);
  561. // Recalculate position of the render block after new logical height is set.
  562. // (needed in absolute positioning case with bottom alignment for example)
  563. RenderBlock::updateLogicalHeight();
  564. }
  565. }
  566. } // namespace WebCore