LogicalSelectionOffsetCaches.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #ifndef LogicalSelectionOffsetCaches_h
  21. #define LogicalSelectionOffsetCaches_h
  22. #include "RenderBlock.h"
  23. namespace WebCore {
  24. static inline bool isContainingBlockCandidateForAbsolutelyPositionedObject(RenderObject* object)
  25. {
  26. return object->style()->position() != StaticPosition
  27. || (object->hasTransform() && object->isRenderBlock())
  28. #if ENABLE(SVG)
  29. || object->isSVGForeignObject()
  30. #endif
  31. || object->isRenderView();
  32. }
  33. static inline bool isNonRenderBlockInline(RenderObject* object)
  34. {
  35. return (object->isInline() && !object->isReplaced()) || !object->isRenderBlock();
  36. }
  37. static inline RenderBlock* containingBlockForFixedPosition(RenderObject* parent)
  38. {
  39. RenderObject* object = parent;
  40. while (object && !object->canContainFixedPositionObjects())
  41. object = object->parent();
  42. ASSERT(!object || !object->isAnonymousBlock());
  43. return toRenderBlock(object);
  44. }
  45. static inline RenderBlock* containingBlockForAbsolutePosition(RenderObject* parent)
  46. {
  47. RenderObject* object = parent;
  48. while (object && !isContainingBlockCandidateForAbsolutelyPositionedObject(object))
  49. object = object->parent();
  50. // For a relatively positioned inline, return its nearest non-anonymous containing block,
  51. // not the inline itself, to avoid having a positioned objects list in all RenderInlines
  52. // and use RenderBlock* as RenderObject::containingBlock's return type.
  53. // Use RenderBlock::container() to obtain the inline.
  54. if (object && !object->isRenderBlock())
  55. object = object->containingBlock();
  56. while (object && object->isAnonymousBlock())
  57. object = object->containingBlock();
  58. return toRenderBlock(object);
  59. }
  60. static inline RenderBlock* containingBlockForObjectInFlow(RenderObject* parent)
  61. {
  62. RenderObject* object = parent;
  63. while (object && isNonRenderBlockInline(object))
  64. object = object->parent();
  65. return toRenderBlock(object);
  66. }
  67. class LogicalSelectionOffsetCaches {
  68. public:
  69. class ContainingBlockInfo {
  70. public:
  71. ContainingBlockInfo()
  72. : m_block(0)
  73. , m_cache(0)
  74. , m_hasFloatsOrFlowThreads(false)
  75. , m_cachedLogicalLeftSelectionOffset(false)
  76. , m_cachedLogicalRightSelectionOffset(false)
  77. { }
  78. void setBlock(RenderBlock* block, const LogicalSelectionOffsetCaches* cache)
  79. {
  80. m_block = block;
  81. m_hasFloatsOrFlowThreads = m_hasFloatsOrFlowThreads || m_block->containsFloats() || m_block->flowThreadContainingBlock();
  82. m_cache = cache;
  83. m_cachedLogicalLeftSelectionOffset = false;
  84. m_cachedLogicalRightSelectionOffset = false;
  85. }
  86. RenderBlock* block() const { return m_block; }
  87. const LogicalSelectionOffsetCaches* cache() const { return m_cache; }
  88. LayoutUnit logicalLeftSelectionOffset(RenderBlock* rootBlock, LayoutUnit position) const
  89. {
  90. ASSERT(m_cache);
  91. if (m_hasFloatsOrFlowThreads || !m_cachedLogicalLeftSelectionOffset) {
  92. m_cachedLogicalLeftSelectionOffset = true;
  93. m_logicalLeftSelectionOffset = m_block->logicalLeftSelectionOffset(rootBlock, position, *m_cache);
  94. } else
  95. ASSERT(m_logicalLeftSelectionOffset == m_block->logicalLeftSelectionOffset(rootBlock, position, *m_cache));
  96. return m_logicalLeftSelectionOffset;
  97. }
  98. LayoutUnit logicalRightSelectionOffset(RenderBlock* rootBlock, LayoutUnit position) const
  99. {
  100. ASSERT(m_cache);
  101. if (m_hasFloatsOrFlowThreads || !m_cachedLogicalRightSelectionOffset) {
  102. m_cachedLogicalRightSelectionOffset = true;
  103. m_logicalRightSelectionOffset = m_block->logicalRightSelectionOffset(rootBlock, position, *m_cache);
  104. } else
  105. ASSERT(m_logicalRightSelectionOffset == m_block->logicalRightSelectionOffset(rootBlock, position, *m_cache));
  106. return m_logicalRightSelectionOffset;
  107. }
  108. private:
  109. RenderBlock* m_block;
  110. const LogicalSelectionOffsetCaches* m_cache;
  111. bool m_hasFloatsOrFlowThreads : 1;
  112. mutable bool m_cachedLogicalLeftSelectionOffset : 1;
  113. mutable bool m_cachedLogicalRightSelectionOffset : 1;
  114. mutable LayoutUnit m_logicalLeftSelectionOffset;
  115. mutable LayoutUnit m_logicalRightSelectionOffset;
  116. };
  117. LogicalSelectionOffsetCaches(RenderBlock* rootBlock)
  118. {
  119. ASSERT(rootBlock->isSelectionRoot());
  120. RenderObject* parent = rootBlock->parent();
  121. // LogicalSelectionOffsetCaches should not be used on an orphaned tree.
  122. m_containingBlockForFixedPosition.setBlock(containingBlockForFixedPosition(parent), 0);
  123. m_containingBlockForAbsolutePosition.setBlock(containingBlockForAbsolutePosition(parent), 0);
  124. m_containingBlockForInflowPosition.setBlock(containingBlockForObjectInFlow(parent), 0);
  125. }
  126. LogicalSelectionOffsetCaches(RenderBlock* block, const LogicalSelectionOffsetCaches& cache)
  127. : m_containingBlockForFixedPosition(cache.m_containingBlockForFixedPosition)
  128. , m_containingBlockForAbsolutePosition(cache.m_containingBlockForAbsolutePosition)
  129. {
  130. if (block->canContainFixedPositionObjects())
  131. m_containingBlockForFixedPosition.setBlock(block, &cache);
  132. if (isContainingBlockCandidateForAbsolutelyPositionedObject(block) && !block->isRenderInline() && !block->isAnonymousBlock())
  133. m_containingBlockForFixedPosition.setBlock(block, &cache);
  134. m_containingBlockForInflowPosition.setBlock(block, &cache);
  135. }
  136. const ContainingBlockInfo& containingBlockInfo(RenderBlock* block) const
  137. {
  138. EPosition position = block->style()->position();
  139. if (position == FixedPosition) {
  140. ASSERT(block->containingBlock() == m_containingBlockForFixedPosition.block());
  141. return m_containingBlockForFixedPosition;
  142. }
  143. if (position == AbsolutePosition) {
  144. ASSERT(block->containingBlock() == m_containingBlockForAbsolutePosition.block());
  145. return m_containingBlockForAbsolutePosition;
  146. }
  147. ASSERT(block->containingBlock() == m_containingBlockForInflowPosition.block());
  148. return m_containingBlockForInflowPosition;
  149. }
  150. private:
  151. ContainingBlockInfo m_containingBlockForFixedPosition;
  152. ContainingBlockInfo m_containingBlockForAbsolutePosition;
  153. ContainingBlockInfo m_containingBlockForInflowPosition;
  154. };
  155. } // namespace WebCore
  156. #endif // LogicalSelectionOffsetCaches_h