Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / FindInPageCoordinates.cpp
blobdc75505ca85abf55456668b537e210285591c5b3
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/FindInPageCoordinates.h"
34 #include "core/dom/Node.h"
35 #include "core/dom/Range.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"
47 namespace blink {
49 static const LayoutBlock* enclosingScrollableAncestor(const LayoutObject* layoutObject)
51 ASSERT(!layoutObject->isLayoutView());
53 // Trace up the containingBlocks until we reach either the layoutObject view or a scrollable object.
54 const LayoutBlock* container = layoutObject->containingBlock();
55 while (!container->hasOverflowClip() && !container->isLayoutView())
56 container = container->containingBlock();
57 return container;
60 static FloatRect toNormalizedRect(const FloatRect& absoluteRect, const LayoutObject* layoutObject, const LayoutBlock* container)
62 ASSERT(layoutObject);
64 ASSERT(container || layoutObject->isLayoutView());
65 if (!container)
66 return FloatRect();
68 // We want to normalize by the max layout overflow size instead of only the visible bounding box.
69 // Quads and their enclosing bounding boxes need to be used in order to keep results transform-friendly.
70 FloatPoint scrolledOrigin;
72 // For overflow:scroll we need to get where the actual origin is independently of the scroll.
73 if (container->hasOverflowClip())
74 scrolledOrigin = -IntPoint(container->scrolledContentOffset());
76 FloatRect overflowRect(scrolledOrigin, FloatSize(container->maxLayoutOverflow()));
77 FloatRect containerRect = container->localToAbsoluteQuad(FloatQuad(overflowRect)).enclosingBoundingBox();
79 if (containerRect.isEmpty())
80 return FloatRect();
82 // Make the coordinates relative to the container enclosing bounding box.
83 // Since we work with rects enclosing quad unions this is still transform-friendly.
84 FloatRect normalizedRect = absoluteRect;
85 normalizedRect.moveBy(-containerRect.location());
87 // Fixed positions do not make sense in this coordinate system, but need to leave consistent tickmarks.
88 // So, use their position when the view is not scrolled, like an absolute position.
89 if (layoutObject->style()->position() == FixedPosition && container->isLayoutView())
90 normalizedRect.moveBy(-toLayoutView(container)->frameView()->scrollPosition());
92 normalizedRect.scale(1 / containerRect.width(), 1 / containerRect.height());
93 return normalizedRect;
96 FloatRect findInPageRectFromAbsoluteRect(const FloatRect& inputRect, const LayoutObject* baseLayoutObject)
98 if (!baseLayoutObject || inputRect.isEmpty())
99 return FloatRect();
101 // Normalize the input rect to its container block.
102 const LayoutBlock* baseContainer = enclosingScrollableAncestor(baseLayoutObject);
103 FloatRect normalizedRect = toNormalizedRect(inputRect, baseLayoutObject, baseContainer);
105 // Go up across frames.
106 for (const LayoutBox* layoutObject = baseContainer; layoutObject; ) {
108 // Go up the layout tree until we reach the root of the current frame (the LayoutView).
109 while (!layoutObject->isLayoutView()) {
110 const LayoutBlock* container = enclosingScrollableAncestor(layoutObject);
112 // Compose the normalized rects.
113 FloatRect normalizedBoxRect = toNormalizedRect(layoutObject->absoluteBoundingBoxRect(), layoutObject, container);
114 normalizedRect.scale(normalizedBoxRect.width(), normalizedBoxRect.height());
115 normalizedRect.moveBy(normalizedBoxRect.location());
117 layoutObject = container;
120 ASSERT(layoutObject->isLayoutView());
122 // Jump to the layoutObject owning the frame, if any.
123 layoutObject = layoutObject->frame() ? layoutObject->frame()->ownerLayoutObject() : 0;
126 return normalizedRect;
129 FloatRect findInPageRectFromRange(Range* range)
131 if (!range || !range->firstNode())
132 return FloatRect();
134 return findInPageRectFromAbsoluteRect(LayoutObject::absoluteBoundingBoxRectForRange(range), range->firstNode()->layoutObject());
137 } // namespace blink