Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: third_party/WebKit/Source/web/FindInPageCoordinates.cpp

Issue 2828493003: Move FindInPagesCoordinates.* from web/ -> core/editing/. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 Google Inc. 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "web/FindInPageCoordinates.h"
32
33 #include "core/dom/Node.h"
34 #include "core/dom/Range.h"
35 #include "core/frame/FrameView.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/layout/LayoutBlock.h"
38 #include "core/layout/LayoutBox.h"
39 #include "core/layout/LayoutObject.h"
40 #include "core/layout/LayoutPart.h"
41 #include "core/layout/LayoutView.h"
42 #include "core/style/ComputedStyle.h"
43 #include "platform/geometry/FloatPoint.h"
44 #include "platform/geometry/FloatQuad.h"
45 #include "platform/geometry/IntPoint.h"
46
47 namespace blink {
48
49 static const LayoutBlock* EnclosingScrollableAncestor(
50 const LayoutObject* layout_object) {
51 DCHECK(!layout_object->IsLayoutView());
52
53 // Trace up the containingBlocks until we reach either the layoutObject view
54 // or a scrollable object.
55 const LayoutBlock* container = layout_object->ContainingBlock();
56 while (!container->HasOverflowClip() && !container->IsLayoutView())
57 container = container->ContainingBlock();
58 return container;
59 }
60
61 static FloatRect ToNormalizedRect(const FloatRect& absolute_rect,
62 const LayoutObject* layout_object,
63 const LayoutBlock* container) {
64 DCHECK(layout_object);
65
66 DCHECK(container || layout_object->IsLayoutView());
67 if (!container)
68 return FloatRect();
69
70 // We want to normalize by the max layout overflow size instead of only the
71 // visible bounding box. Quads and their enclosing bounding boxes need to be
72 // used in order to keep results transform-friendly.
73 FloatPoint scrolled_origin;
74
75 // For overflow:scroll we need to get where the actual origin is independently
76 // of the scroll.
77 if (container->HasOverflowClip())
78 scrolled_origin = -IntPoint(container->ScrolledContentOffset());
79
80 FloatRect overflow_rect(scrolled_origin,
81 FloatSize(container->MaxLayoutOverflow()));
82 FloatRect container_rect =
83 container->LocalToAbsoluteQuad(FloatQuad(overflow_rect))
84 .EnclosingBoundingBox();
85
86 if (container_rect.IsEmpty())
87 return FloatRect();
88
89 // Make the coordinates relative to the container enclosing bounding box.
90 // Since we work with rects enclosing quad unions this is still
91 // transform-friendly.
92 FloatRect normalized_rect = absolute_rect;
93 normalized_rect.MoveBy(-container_rect.Location());
94
95 // Fixed positions do not make sense in this coordinate system, but need to
96 // leave consistent tickmarks. So, use their position when the view is not
97 // scrolled, like an absolute position.
98 if (layout_object->Style()->GetPosition() == EPosition::kFixed &&
99 container->IsLayoutView()) {
100 normalized_rect.Move(
101 -ToLayoutView(container)->GetFrameView()->GetScrollOffset());
102 }
103
104 normalized_rect.Scale(1 / container_rect.Width(),
105 1 / container_rect.Height());
106 return normalized_rect;
107 }
108
109 FloatRect FindInPageRectFromAbsoluteRect(
110 const FloatRect& input_rect,
111 const LayoutObject* base_layout_object) {
112 if (!base_layout_object || input_rect.IsEmpty())
113 return FloatRect();
114
115 // Normalize the input rect to its container block.
116 const LayoutBlock* base_container =
117 EnclosingScrollableAncestor(base_layout_object);
118 FloatRect normalized_rect =
119 ToNormalizedRect(input_rect, base_layout_object, base_container);
120
121 // Go up across frames.
122 for (const LayoutBox* layout_object = base_container; layout_object;) {
123 // Go up the layout tree until we reach the root of the current frame (the
124 // LayoutView).
125 while (!layout_object->IsLayoutView()) {
126 const LayoutBlock* container = EnclosingScrollableAncestor(layout_object);
127
128 // Compose the normalized rects.
129 FloatRect normalized_box_rect = ToNormalizedRect(
130 layout_object->AbsoluteBoundingBoxRect(), layout_object, container);
131 normalized_rect.Scale(normalized_box_rect.Width(),
132 normalized_box_rect.Height());
133 normalized_rect.MoveBy(normalized_box_rect.Location());
134
135 layout_object = container;
136 }
137
138 DCHECK(layout_object->IsLayoutView());
139
140 // Jump to the layoutObject owning the frame, if any.
141 layout_object = layout_object->GetFrame()
142 ? layout_object->GetFrame()->OwnerLayoutObject()
143 : 0;
144 }
145
146 return normalized_rect;
147 }
148
149 FloatRect FindInPageRectFromRange(Range* range) {
150 if (!range || !range->FirstNode())
151 return FloatRect();
152
153 return FindInPageRectFromAbsoluteRect(
154 LayoutObject::AbsoluteBoundingBoxRectForRange(range),
155 range->FirstNode()->GetLayoutObject());
156 }
157
158 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/FindInPageCoordinates.h ('k') | third_party/WebKit/Source/web/TextFinder.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698