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
21#pragma once
22
23#include "RenderBlock.h"
24
25namespace WebCore {
26
27class LogicalSelectionOffsetCaches {
28public:
29 class ContainingBlockInfo {
30 public:
31 ContainingBlockInfo()
32 : m_hasFloatsOrFragmentedFlows(false)
33 , m_cachedLogicalLeftSelectionOffset(false)
34 , m_cachedLogicalRightSelectionOffset(false)
35 { }
36
37 void setBlock(RenderBlock* block, const LogicalSelectionOffsetCaches* cache, bool parentCacheHasFloatsOrFragmentedFlows = false)
38 {
39 m_block = block;
40 bool blockHasFloatsOrFragmentedFlows = m_block ? (m_block->containsFloats() || m_block->enclosingFragmentedFlow()) : false;
41 m_hasFloatsOrFragmentedFlows = parentCacheHasFloatsOrFragmentedFlows || m_hasFloatsOrFragmentedFlows || blockHasFloatsOrFragmentedFlows;
42 m_cache = cache;
43 m_cachedLogicalLeftSelectionOffset = false;
44 m_cachedLogicalRightSelectionOffset = false;
45 }
46
47 LayoutUnit logicalLeftSelectionOffset(RenderBlock& rootBlock, LayoutUnit position) const
48 {
49 ASSERT(m_cache);
50 if (m_hasFloatsOrFragmentedFlows || !m_cachedLogicalLeftSelectionOffset) {
51 m_cachedLogicalLeftSelectionOffset = true;
52 m_logicalLeftSelectionOffset = m_block ? m_block->logicalLeftSelectionOffset(rootBlock, position, *m_cache) : 0_lu;
53 } else
54 ASSERT(m_logicalLeftSelectionOffset == (m_block ? m_block->logicalLeftSelectionOffset(rootBlock, position, *m_cache) : 0_lu));
55 return m_logicalLeftSelectionOffset;
56 }
57
58 LayoutUnit logicalRightSelectionOffset(RenderBlock& rootBlock, LayoutUnit position) const
59 {
60 ASSERT(m_cache);
61 if (m_hasFloatsOrFragmentedFlows || !m_cachedLogicalRightSelectionOffset) {
62 m_cachedLogicalRightSelectionOffset = true;
63 m_logicalRightSelectionOffset = m_block ? m_block->logicalRightSelectionOffset(rootBlock, position, *m_cache) : 0_lu;
64 } else
65 ASSERT(m_logicalRightSelectionOffset == (m_block ? m_block->logicalRightSelectionOffset(rootBlock, position, *m_cache) : 0_lu));
66 return m_logicalRightSelectionOffset;
67 }
68
69 RenderBlock* block() const { return m_block; }
70 const LogicalSelectionOffsetCaches* cache() const { return m_cache; }
71 bool hasFloatsOrFragmentedFlows() const { return m_hasFloatsOrFragmentedFlows; }
72
73 private:
74 RenderBlock* m_block { nullptr };
75 const LogicalSelectionOffsetCaches* m_cache { nullptr };
76 bool m_hasFloatsOrFragmentedFlows : 1;
77 mutable bool m_cachedLogicalLeftSelectionOffset : 1;
78 mutable bool m_cachedLogicalRightSelectionOffset : 1;
79 mutable LayoutUnit m_logicalLeftSelectionOffset;
80 mutable LayoutUnit m_logicalRightSelectionOffset;
81
82 };
83
84 explicit LogicalSelectionOffsetCaches(RenderBlock& rootBlock)
85 {
86#if ENABLE(TEXT_SELECTION)
87 // FIXME: We should either move this assertion to the caller (if applicable) or structure the code
88 // such that we can remove this assertion.
89 ASSERT(rootBlock.isSelectionRoot());
90#endif
91 // LogicalSelectionOffsetCaches should not be used on an orphaned tree.
92 m_containingBlockForFixedPosition.setBlock(rootBlock.containingBlockForFixedPosition(), nullptr);
93 m_containingBlockForAbsolutePosition.setBlock(rootBlock.containingBlockForAbsolutePosition(), nullptr);
94 m_containingBlockForInflowPosition.setBlock(rootBlock.containingBlockForObjectInFlow(), nullptr);
95 }
96
97 LogicalSelectionOffsetCaches(RenderBlock& block, const LogicalSelectionOffsetCaches& cache)
98 : m_containingBlockForFixedPosition(cache.m_containingBlockForFixedPosition)
99 , m_containingBlockForAbsolutePosition(cache.m_containingBlockForAbsolutePosition)
100 {
101 if (block.canContainFixedPositionObjects())
102 m_containingBlockForFixedPosition.setBlock(&block, &cache, cache.m_containingBlockForFixedPosition.hasFloatsOrFragmentedFlows());
103
104 if (block.canContainAbsolutelyPositionedObjects() && !block.isRenderInline() && !block.isAnonymousBlock())
105 m_containingBlockForAbsolutePosition.setBlock(&block, &cache, cache.m_containingBlockForAbsolutePosition.hasFloatsOrFragmentedFlows());
106
107 m_containingBlockForInflowPosition.setBlock(&block, &cache, cache.m_containingBlockForInflowPosition.hasFloatsOrFragmentedFlows());
108 }
109
110 const ContainingBlockInfo& containingBlockInfo(RenderBlock& block) const
111 {
112 auto position = block.style().position();
113 if (position == PositionType::Fixed) {
114 ASSERT(block.containingBlock() == m_containingBlockForFixedPosition.block());
115 return m_containingBlockForFixedPosition;
116 }
117 if (position == PositionType::Absolute) {
118 ASSERT(block.containingBlock() == m_containingBlockForAbsolutePosition.block());
119 return m_containingBlockForAbsolutePosition;
120 }
121 ASSERT(block.containingBlock() == m_containingBlockForInflowPosition.block());
122 return m_containingBlockForInflowPosition;
123 }
124
125private:
126 ContainingBlockInfo m_containingBlockForFixedPosition;
127 ContainingBlockInfo m_containingBlockForAbsolutePosition;
128 ContainingBlockInfo m_containingBlockForInflowPosition;
129};
130
131} // namespace WebCore
132