OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/IntersectionGeometry.h" | 5 #include "core/layout/IntersectionGeometry.h" |
6 | 6 |
7 #include "core/dom/Element.h" | |
8 #include "core/dom/ElementRareData.h" | |
9 #include "core/frame/FrameView.h" | 7 #include "core/frame/FrameView.h" |
10 #include "core/frame/LocalFrame.h" | 8 #include "core/frame/LocalFrame.h" |
9 #include "core/html/HTMLFrameOwnerElement.h" | |
11 #include "core/layout/LayoutBox.h" | 10 #include "core/layout/LayoutBox.h" |
12 #include "core/layout/LayoutView.h" | 11 #include "core/layout/LayoutView.h" |
13 #include "core/layout/api/LayoutAPIShim.h" | 12 #include "core/layout/api/LayoutAPIShim.h" |
14 #include "core/layout/api/LayoutViewItem.h" | 13 #include "core/layout/api/LayoutViewItem.h" |
15 #include "core/paint/PaintLayer.h" | 14 #include "core/paint/PaintLayer.h" |
16 | 15 |
17 namespace blink { | 16 namespace blink { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 bool isContainingBlockChainDescendant(LayoutObject* descendant, | 20 bool isContainingBlockChainDescendant(LayoutObject* descendant, |
22 LayoutObject* ancestor) { | 21 LayoutObject* ancestor) { |
23 LocalFrame* ancestorFrame = ancestor->document().frame(); | 22 LocalFrame* ancestorFrame = ancestor->document().frame(); |
24 LocalFrame* descendantFrame = descendant->document().frame(); | 23 LocalFrame* descendantFrame = descendant->document().frame(); |
24 if (!ancestorFrame) | |
25 return false; | |
26 | |
27 while (descendantFrame && descendantFrame != ancestorFrame) { | |
ojan
2016/12/10 01:18:26
When is descendantFrame != ancestorFrame without t
szager1
2016/12/10 01:47:11
If the observer was created with an explicit root,
ojan
2016/12/12 19:53:31
Do we deliver intersection records for that? I tho
szager1
2016/12/12 20:16:11
As discussed in-person, this function should not w
| |
28 HTMLFrameOwnerElement* owner = descendant->document().localOwner(); | |
29 if (!owner) | |
30 return false; | |
31 descendant = owner->layoutObject(); | |
32 if (!descendant) | |
33 return false; | |
34 descendantFrame = descendant->document().frame(); | |
35 } | |
36 | |
37 if (!descendantFrame) | |
38 return false; | |
25 | 39 |
26 if (ancestor->isLayoutView()) | 40 if (ancestor->isLayoutView()) |
27 return descendantFrame && descendantFrame->tree().top() == ancestorFrame; | 41 return true; |
28 | |
29 if (ancestorFrame != descendantFrame) | |
30 return false; | |
31 | 42 |
32 while (descendant && descendant != ancestor) | 43 while (descendant && descendant != ancestor) |
33 descendant = descendant->containingBlock(); | 44 descendant = descendant->containingBlock(); |
34 return descendant; | 45 return descendant; |
35 } | 46 } |
36 | 47 |
37 void mapRectUpToDocument(LayoutRect& rect, | 48 void mapRectUpToDocument(LayoutRect& rect, |
38 const LayoutObject& layoutObject, | 49 const LayoutObject& descendant, |
39 const Document& document) { | 50 const Document& document) { |
40 FloatQuad mappedQuad = layoutObject.localToAbsoluteQuad( | 51 FloatQuad mappedQuad = descendant.localToAncestorQuad( |
41 FloatQuad(FloatRect(rect)), UseTransforms | ApplyContainerFlip); | 52 FloatQuad(FloatRect(rect)), document.layoutView(), |
53 UseTransforms | ApplyContainerFlip); | |
42 rect = LayoutRect(mappedQuad.boundingBox()); | 54 rect = LayoutRect(mappedQuad.boundingBox()); |
43 } | 55 } |
44 | 56 |
45 void mapRectDownToDocument(LayoutRect& rect, | 57 void mapRectDownToDocument(LayoutRect& rect, |
46 LayoutBoxModelObject& layoutObject, | 58 LayoutBoxModelObject* ancestor, |
47 const Document& document) { | 59 const Document& document) { |
48 FloatQuad mappedQuad = document.layoutView()->ancestorToLocalQuad( | 60 FloatQuad mappedQuad = document.layoutView()->ancestorToLocalQuad( |
49 &layoutObject, FloatQuad(FloatRect(rect)), | 61 ancestor, FloatQuad(FloatRect(rect)), |
50 UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries); | 62 UseTransforms | ApplyContainerFlip | TraverseDocumentBoundaries); |
51 rect = LayoutRect(mappedQuad.boundingBox()); | 63 rect = LayoutRect(mappedQuad.boundingBox()); |
52 } | 64 } |
53 | 65 |
54 LayoutUnit computeMargin(const Length& length, LayoutUnit referenceLength) { | 66 LayoutUnit computeMargin(const Length& length, LayoutUnit referenceLength) { |
55 if (length.type() == Percent) { | 67 if (length.type() == Percent) { |
56 return LayoutUnit( | 68 return LayoutUnit( |
57 static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0)); | 69 static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0)); |
58 } | 70 } |
59 DCHECK_EQ(length.type(), Fixed); | 71 DCHECK_EQ(length.type(), Fixed); |
60 return LayoutUnit(length.intValue()); | 72 return LayoutUnit(length.intValue()); |
61 } | 73 } |
62 | 74 |
75 LayoutView* localRootView(Element& element) { | |
76 LocalFrame* frame = element.document().frame(); | |
77 frame = frame ? frame->localFrameRoot() : nullptr; | |
ojan
2016/12/10 01:18:26
Can you create a new local variable here, e.g. fra
szager1
2016/12/10 01:47:11
Done.
| |
78 return frame ? frame->contentLayoutObject() : nullptr; | |
79 } | |
80 | |
63 } // namespace | 81 } // namespace |
64 | 82 |
65 IntersectionGeometry::IntersectionGeometry( | 83 IntersectionGeometry::IntersectionGeometry(Element* root, |
66 Node* root, | 84 Element& target, |
67 Element* target, | 85 const Vector<Length>& rootMargin, |
68 const Vector<Length>& rootMargin, | 86 bool shouldReportRootBounds) |
69 ReportRootBounds shouldReportRootBounds) | 87 : m_root(root ? root->layoutObject() : localRootView(target)), |
70 : m_root(root), | 88 m_target(target.layoutObject()), |
71 m_target(target), | |
72 m_rootMargin(rootMargin), | 89 m_rootMargin(rootMargin), |
73 m_shouldReportRootBounds(shouldReportRootBounds) { | 90 m_doesIntersect(0), |
74 DCHECK(m_target); | 91 m_shouldReportRootBounds(shouldReportRootBounds), |
75 DCHECK(rootMargin.isEmpty() || rootMargin.size() == 4); | 92 m_rootIsImplicit(root ? 0 : 1), |
ojan
2016/12/10 01:18:26
Does !root work? I feel like I haven't seen this t
szager1
2016/12/10 01:47:10
Switched to !root.
| |
76 } | 93 m_isValid(initializeGeometry(root, target)) {} |
77 | 94 |
78 IntersectionGeometry::~IntersectionGeometry() {} | 95 IntersectionGeometry::~IntersectionGeometry() {} |
79 | 96 |
80 Element* IntersectionGeometry::root() const { | 97 bool IntersectionGeometry::initializeGeometry(Element* root, Element& target) { |
ojan
2016/12/10 01:18:26
This bool return value isn't super clear.
How wou
szager1
2016/12/10 01:47:11
Done.
| |
81 if (m_root && !m_root->isDocumentNode()) | 98 DCHECK(m_rootMargin.isEmpty() || m_rootMargin.size() == 4); |
82 return toElement(m_root); | 99 if (root && !root->isConnected()) |
83 return nullptr; | 100 return false; |
84 } | 101 if (!m_root || !m_root->isBox()) |
85 | 102 return false; |
86 LayoutObject* IntersectionGeometry::getRootLayoutObject() const { | 103 if (!target.isConnected()) |
87 DCHECK(m_root); | 104 return false; |
88 if (m_root->isDocumentNode()) { | 105 if (!m_target || (!m_target->isBoxModelObject() && !m_target->isText())) |
89 return LayoutAPIShim::layoutObjectFrom( | 106 return false; |
90 toDocument(m_root)->layoutViewItem()); | 107 if (root && !isContainingBlockChainDescendant(m_target, m_root)) |
91 } | 108 return false; |
92 return toElement(m_root)->layoutObject(); | |
93 } | |
94 | |
95 void IntersectionGeometry::initializeGeometry() { | |
96 initializeTargetRect(); | 109 initializeTargetRect(); |
97 m_intersectionRect = m_targetRect; | 110 m_intersectionRect = m_targetRect; |
98 initializeRootRect(); | 111 initializeRootRect(); |
99 m_doesIntersect = true; | 112 return true; |
100 } | 113 } |
101 | 114 |
102 void IntersectionGeometry::initializeTargetRect() { | 115 void IntersectionGeometry::initializeTargetRect() { |
103 LayoutObject* targetLayoutObject = m_target->layoutObject(); | 116 m_targetRect = |
104 DCHECK(targetLayoutObject && targetLayoutObject->isBoxModelObject()); | 117 LayoutRect(toLayoutBoxModelObject(target())->borderBoundingBox()); |
105 m_targetRect = LayoutRect( | |
106 toLayoutBoxModelObject(targetLayoutObject)->borderBoundingBox()); | |
107 } | 118 } |
108 | 119 |
109 void IntersectionGeometry::initializeRootRect() { | 120 void IntersectionGeometry::initializeRootRect() { |
110 LayoutObject* rootLayoutObject = getRootLayoutObject(); | 121 // TODO(szager): In OOPIF, m_root will be the LayoutView of the |
111 if (rootLayoutObject->isLayoutView()) { | 122 // localFrameRoot(). Once viewport intersection support lands, |
112 m_rootRect = LayoutRect( | 123 // add a call to mapToVisualRectInAncestorSpace to map the rect up to |
113 toLayoutView(rootLayoutObject)->frameView()->visibleContentRect()); | 124 // top-level frame coordinates. |
114 } else if (rootLayoutObject->isBox() && rootLayoutObject->hasOverflowClip()) { | 125 if (m_root->isLayoutView()) { |
115 m_rootRect = LayoutRect(toLayoutBox(rootLayoutObject)->contentBoxRect()); | 126 m_rootRect = |
127 LayoutRect(toLayoutView(m_root)->frameView()->visibleContentRect()); | |
128 } else if (m_root->isBox() && m_root->hasOverflowClip()) { | |
129 m_rootRect = LayoutRect(toLayoutBox(m_root)->contentBoxRect()); | |
116 } else { | 130 } else { |
117 m_rootRect = LayoutRect( | 131 m_rootRect = |
118 toLayoutBoxModelObject(rootLayoutObject)->borderBoundingBox()); | 132 LayoutRect(toLayoutBoxModelObject(m_root)->borderBoundingBox()); |
119 } | 133 } |
120 applyRootMargin(); | 134 applyRootMargin(); |
121 } | 135 } |
122 | 136 |
123 void IntersectionGeometry::applyRootMargin() { | 137 void IntersectionGeometry::applyRootMargin() { |
124 if (m_rootMargin.isEmpty()) | 138 if (m_rootMargin.isEmpty()) |
125 return; | 139 return; |
126 | 140 |
127 // TODO(szager): Make sure the spec is clear that left/right margins are | 141 // TODO(szager): Make sure the spec is clear that left/right margins are |
128 // resolved against width and not height. | 142 // resolved against width and not height. |
129 LayoutUnit topMargin = computeMargin(m_rootMargin[0], m_rootRect.height()); | 143 LayoutUnit topMargin = computeMargin(m_rootMargin[0], m_rootRect.height()); |
130 LayoutUnit rightMargin = computeMargin(m_rootMargin[1], m_rootRect.width()); | 144 LayoutUnit rightMargin = computeMargin(m_rootMargin[1], m_rootRect.width()); |
131 LayoutUnit bottomMargin = computeMargin(m_rootMargin[2], m_rootRect.height()); | 145 LayoutUnit bottomMargin = computeMargin(m_rootMargin[2], m_rootRect.height()); |
132 LayoutUnit leftMargin = computeMargin(m_rootMargin[3], m_rootRect.width()); | 146 LayoutUnit leftMargin = computeMargin(m_rootMargin[3], m_rootRect.width()); |
133 | 147 |
134 m_rootRect.setX(m_rootRect.x() - leftMargin); | 148 m_rootRect.setX(m_rootRect.x() - leftMargin); |
135 m_rootRect.setWidth(m_rootRect.width() + leftMargin + rightMargin); | 149 m_rootRect.setWidth(m_rootRect.width() + leftMargin + rightMargin); |
136 m_rootRect.setY(m_rootRect.y() - topMargin); | 150 m_rootRect.setY(m_rootRect.y() - topMargin); |
137 m_rootRect.setHeight(m_rootRect.height() + topMargin + bottomMargin); | 151 m_rootRect.setHeight(m_rootRect.height() + topMargin + bottomMargin); |
138 } | 152 } |
139 | 153 |
140 void IntersectionGeometry::clipToRoot() { | 154 void IntersectionGeometry::clipToRoot() { |
141 // Map and clip rect into root element coordinates. | 155 // Map and clip rect into root element coordinates. |
142 // TODO(szager): the writing mode flipping needs a test. | 156 // TODO(szager): the writing mode flipping needs a test. |
143 LayoutBox* rootLayoutObject = toLayoutBox(getRootLayoutObject()); | 157 // TODO(szager): Once the OOPIF viewport intersection code lands, |
144 LayoutObject* targetLayoutObject = m_target->layoutObject(); | 158 // use nullptr for ancestor to map to the top frame. |
145 | 159 LayoutBox* ancestor = toLayoutBox(m_root); |
146 m_doesIntersect = targetLayoutObject->mapToVisualRectInAncestorSpace( | 160 m_doesIntersect = m_target->mapToVisualRectInAncestorSpace( |
147 rootLayoutObject, m_intersectionRect, EdgeInclusive); | 161 ancestor, m_intersectionRect, EdgeInclusive); |
148 if (rootLayoutObject->hasOverflowClip()) | 162 if (ancestor && ancestor->hasOverflowClip()) |
149 m_intersectionRect.move(-rootLayoutObject->scrolledContentOffset()); | 163 m_intersectionRect.move(-ancestor->scrolledContentOffset()); |
150 | |
151 if (!m_doesIntersect) | 164 if (!m_doesIntersect) |
152 return; | 165 return; |
153 LayoutRect rootClipRect(m_rootRect); | 166 LayoutRect rootClipRect(m_rootRect); |
154 rootLayoutObject->flipForWritingMode(rootClipRect); | 167 if (ancestor) |
168 ancestor->flipForWritingMode(rootClipRect); | |
155 m_doesIntersect &= m_intersectionRect.inclusiveIntersect(rootClipRect); | 169 m_doesIntersect &= m_intersectionRect.inclusiveIntersect(rootClipRect); |
156 } | 170 } |
157 | 171 |
158 void IntersectionGeometry::mapTargetRectToTargetFrameCoordinates() { | 172 void IntersectionGeometry::mapTargetRectToTargetFrameCoordinates() { |
159 LayoutObject& targetLayoutObject = *m_target->layoutObject(); | |
160 Document& targetDocument = m_target->document(); | 173 Document& targetDocument = m_target->document(); |
161 LayoutSize scrollPosition = | 174 LayoutSize scrollPosition = |
162 LayoutSize(targetDocument.view()->getScrollOffset()); | 175 LayoutSize(targetDocument.view()->getScrollOffset()); |
163 mapRectUpToDocument(m_targetRect, targetLayoutObject, targetDocument); | 176 mapRectUpToDocument(m_targetRect, *m_target, targetDocument); |
164 m_targetRect.move(-scrollPosition); | 177 m_targetRect.move(-scrollPosition); |
165 } | 178 } |
166 | 179 |
167 void IntersectionGeometry::mapRootRectToRootFrameCoordinates() { | 180 void IntersectionGeometry::mapRootRectToRootFrameCoordinates() { |
168 LayoutObject& rootLayoutObject = *getRootLayoutObject(); | 181 Document& rootDocument = m_root->document(); |
169 Document& rootDocument = rootLayoutObject.document(); | 182 if (!rootIsImplicit()) |
183 mapRectUpToDocument(m_rootRect, *m_root, rootDocument); | |
184 // TODO(szager): When OOPIF support lands, this scroll offset adjustment | |
185 // will probably be wrong. | |
170 LayoutSize scrollPosition = | 186 LayoutSize scrollPosition = |
171 LayoutSize(rootDocument.view()->getScrollOffset()); | 187 LayoutSize(rootDocument.view()->getScrollOffset()); |
172 mapRectUpToDocument(m_rootRect, rootLayoutObject, | |
173 rootLayoutObject.document()); | |
174 m_rootRect.move(-scrollPosition); | 188 m_rootRect.move(-scrollPosition); |
175 } | 189 } |
176 | 190 |
177 void IntersectionGeometry::mapRootRectToTargetFrameCoordinates() { | 191 void IntersectionGeometry::mapIntersectionRectToTargetFrameCoordinates() { |
178 LayoutObject& rootLayoutObject = *getRootLayoutObject(); | |
179 Document& targetDocument = m_target->document(); | 192 Document& targetDocument = m_target->document(); |
180 LayoutSize scrollPosition = | 193 if (rootIsImplicit()) { |
181 LayoutSize(targetDocument.view()->getScrollOffset()); | 194 LocalFrame* targetFrame = targetDocument.frame(); |
182 | 195 Frame* rootFrame = targetFrame->tree().top(); |
183 if (&targetDocument == &rootLayoutObject.document()) { | 196 LayoutSize scrollPosition = |
184 mapRectUpToDocument(m_intersectionRect, rootLayoutObject, targetDocument); | 197 LayoutSize(targetDocument.view()->getScrollOffset()); |
198 if (targetFrame != rootFrame) | |
199 mapRectDownToDocument(m_intersectionRect, nullptr, targetDocument); | |
200 m_intersectionRect.move(-scrollPosition); | |
185 } else { | 201 } else { |
186 mapRectDownToDocument(m_intersectionRect, | 202 LayoutSize scrollPosition = |
187 toLayoutBoxModelObject(rootLayoutObject), | 203 LayoutSize(targetDocument.view()->getScrollOffset()); |
188 targetDocument); | 204 mapRectUpToDocument(m_intersectionRect, *m_root, m_root->document()); |
205 m_intersectionRect.move(-scrollPosition); | |
189 } | 206 } |
190 | |
191 m_intersectionRect.move(-scrollPosition); | |
192 } | 207 } |
193 | 208 |
194 void IntersectionGeometry::computeGeometry() { | 209 void IntersectionGeometry::computeGeometry() { |
195 // In the first few lines here, before initializeGeometry is called, "return | 210 if (!isValid()) |
196 // true" effectively means "if the previous observed state was that root and | |
197 // target were intersecting, then generate a notification indicating that they | |
198 // are no longer intersecting." This happens, for example, when root or | |
199 // target is removed from the DOM tree and not reinserted before the next | |
200 // frame is generated, or display:none is set on the root or target. | |
201 if (!m_target->isConnected()) | |
202 return; | 211 return; |
203 Element* rootElement = root(); | |
204 if (rootElement && !rootElement->isConnected()) | |
205 return; | |
206 | |
207 LayoutObject* rootLayoutObject = getRootLayoutObject(); | |
208 if (!rootLayoutObject || !rootLayoutObject->isBoxModelObject()) | |
209 return; | |
210 // TODO(szager): Support SVG | |
211 LayoutObject* targetLayoutObject = m_target->layoutObject(); | |
212 if (!targetLayoutObject) | |
213 return; | |
214 if (!targetLayoutObject->isBoxModelObject() && !targetLayoutObject->isText()) | |
215 return; | |
216 if (!isContainingBlockChainDescendant(targetLayoutObject, rootLayoutObject)) | |
217 return; | |
218 | |
219 initializeGeometry(); | |
220 | |
221 clipToRoot(); | 212 clipToRoot(); |
222 | |
223 mapTargetRectToTargetFrameCoordinates(); | 213 mapTargetRectToTargetFrameCoordinates(); |
224 | |
225 if (m_doesIntersect) | 214 if (m_doesIntersect) |
226 mapRootRectToTargetFrameCoordinates(); | 215 mapIntersectionRectToTargetFrameCoordinates(); |
227 else | 216 else |
228 m_intersectionRect = LayoutRect(); | 217 m_intersectionRect = LayoutRect(); |
229 | |
230 // Small optimization: if we're not going to report root bounds, don't bother | 218 // Small optimization: if we're not going to report root bounds, don't bother |
231 // transforming them to the frame. | 219 // transforming them to the frame. |
232 if (m_shouldReportRootBounds == ReportRootBounds::kShouldReportRootBounds) | 220 if (shouldReportRootBounds()) |
233 mapRootRectToRootFrameCoordinates(); | 221 mapRootRectToRootFrameCoordinates(); |
234 } | 222 } |
235 | 223 |
236 DEFINE_TRACE(IntersectionGeometry) { | |
237 visitor->trace(m_root); | |
238 visitor->trace(m_target); | |
239 } | |
240 | |
241 } // namespace blink | 224 } // namespace blink |
OLD | NEW |