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#ifndef ContainingBlockCache_h
22#define ContainingBlockCache_h
23
24#include "RenderBlock.h"
25
26namespace WebCore {
27
28static inline bool isContainingBlockCandidateForAbsolutelyPositionedObject(RenderObject* object)
29{
30 return object->style()->position() != StaticPosition
31 || (object->hasTransform() && object->isRenderBlock())
32#if ENABLE(SVG)
33 || object->isSVGForeignObject()
34#endif
35 || object->isRenderView();
36}
37
38static inline bool isNonRenderBlockInline(RenderObject* object)
39{
40 return (object->isInline() && !object->isReplaced()) || !object->isRenderBlock();
41}
42
43static inline RenderObject* containingBlockForFixedPosition(RenderObject* parent)
44{
45 RenderObject* object = parent;
46 while (object && !object->canContainFixedPositionObjects())
47 object = object->parent();
48 ASSERT(!object || !object->isAnonymousBlock());
49 return object;
50}
51
52static inline RenderObject* containingBlockForAbsolutePosition(RenderObject* parent)
53{
54 RenderObject* object = parent;
55 while (object && !isContainingBlockCandidateForAbsolutelyPositionedObject(object))
56 object = object->parent();
57
58 // For a relatively positioned inline, return its nearest non-anonymous containing block,
59 // not the inline itself, to avoid having a positioned objects list in all RenderInlines
60 // and use RenderBlock* as RenderObject::containingBlock's return type.
61 // Use RenderBlock::container() to obtain the inline.
62 if (object->isRenderInline())
63 object = object->containingBlock();
64
65 while (object && object->isAnonymousBlock())
66 object = object->containingBlock();
67
68 return object;
69}
70
71static inline RenderObject* containingBlockForObjectInFlow(RenderObject* parent)
72{
73 RenderObject* object = parent;
74 while (object && isNonRenderBlockInline(object))
75 object = object->parent();
76 return object;
77}
78
79class ContainingBlockCache
80{
81public:
82 // FIXME: We shouldn't have this constructor
83 ContainingBlockCache(RenderBlock* rootBlock)
84 : m_parentCache(0)
85 {
86 RenderObject* parent = rootBlock->parent();
87
88 // ContainingBlockCache should not be used on an orphaned tree.
89 m_containingBlockForFixedPosition.block = toRenderBlock(containingBlockForFixedPosition(parent));
90 m_containingBlockForAbsolutePosition.block = toRenderBlock(containingBlockForAbsolutePosition(parent));
91 m_containingBlockForInflowPosition.block = toRenderBlock(containingBlockForObjectInFlow(parent));
92 }
93
94 ContainingBlockCache(RenderBlock* block, const ContainingBlockCache& cache)
95 : m_containingBlockForFixedPosition(cache.m_containingBlockForFixedPosition)
96 , m_containingBlockForAbsolutePosition(cache.m_containingBlockForAbsolutePosition)
97 , m_parentCache(&cache)
98 {
99 if (block->canContainFixedPositionObjects())
100 m_containingBlockForFixedPosition.block = block;
101
102 if (isContainingBlockCandidateForAbsolutelyPositionedObject(block) && !block->isRenderInline() && !block->isAnonymousBlock())
103 m_containingBlockForFixedPosition.block = block;
104
105 m_containingBlockForInflowPosition.block = block;
106 }
107
108 RenderBlock* containingBlock(RenderBlock* block) const
109 {
110 EPosition position = block->style()->position();
111 if (position == FixedPosition)
112 return m_containingBlockForFixedPosition.block;
113 if (position == AbsolutePosition)
114 return m_containingBlockForAbsolutePosition.block;
115 return m_containingBlockForInflowPosition.block;
116 }
117
118 const ContainingBlockCache* parentCache() const { return m_parentCache; }
119
120private:
121 struct ContainingBlockInfo {
122 ContainingBlockInfo()
123 : block(0)
124 , cachedLeftOffset(false)
125 , cachedRightOffset(false)
126 { }
127
128 RenderBlock* block;
129 LayoutUnit leftOffset;
130 LayoutUnit rightOffset;
131 bool cachedLeftOffset : 1;
132 bool cachedRightOffset : 1;
133 };
134
135 ContainingBlockInfo m_containingBlockForFixedPosition;
136 ContainingBlockInfo m_containingBlockForAbsolutePosition;
137 ContainingBlockInfo m_containingBlockForInflowPosition;
138 const ContainingBlockCache* m_parentCache;
139};
140
141} // namespace WebCore
142
143#endif // ContainingBlockCache_h