Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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 "config.h" |
| 32 | #include "FindInPageCoordinates.h" |
| 33 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 34 | #include "core/dom/Node.h" |
| 35 | #include "core/dom/Range.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 36 | #include "core/frame/LocalFrame.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 37 | #include "core/rendering/RenderBlock.h" |
| 38 | #include "core/rendering/RenderBox.h" |
| 39 | #include "core/rendering/RenderObject.h" |
| 40 | #include "core/rendering/RenderPart.h" |
| 41 | #include "core/rendering/RenderView.h" |
| 42 | #include "core/rendering/style/RenderStyle.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 43 | #include "platform/geometry/FloatPoint.h" |
| 44 | #include "platform/geometry/FloatQuad.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 45 | #include "platform/geometry/IntPoint.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 46 | |
| 47 | using namespace WebCore; |
| 48 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 49 | namespace blink { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 50 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 51 | static const RenderBlock* enclosingScrollableAncestor(const RenderObject* renderer) |
| 52 | { |
| 53 | ASSERT(!renderer->isRenderView()); |
| 54 | |
| 55 | // Trace up the containingBlocks until we reach either the render view or a scrollable object. |
| 56 | const RenderBlock* container = renderer->containingBlock(); |
| 57 | while (!container->hasOverflowClip() && !container->isRenderView()) |
| 58 | container = container->containingBlock(); |
| 59 | return container; |
| 60 | } |
| 61 | |
| 62 | static FloatRect toNormalizedRect(const FloatRect& absoluteRect, const RenderObject* renderer, const RenderBlock* container) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 63 | { |
| 64 | ASSERT(renderer); |
| 65 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 66 | ASSERT(container || renderer->isRenderView()); |
| 67 | if (!container) |
| 68 | return FloatRect(); |
| 69 | |
| 70 | // We want to normalize by the max layout overflow size instead of only the visible bounding box. |
| 71 | // Quads and their enclosing bounding boxes need to be used in order to keep results transform-friendly. |
| 72 | FloatPoint scrolledOrigin; |
| 73 | |
| 74 | // For overflow:scroll we need to get where the actual origin is independently of the scroll. |
| 75 | if (container->hasOverflowClip()) |
| 76 | scrolledOrigin = -IntPoint(container->scrolledContentOffset()); |
| 77 | |
| 78 | FloatRect overflowRect(scrolledOrigin, container->maxLayoutOverflow()); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 79 | FloatRect containerRect = container->localToAbsoluteQuad(FloatQuad(overflowRect)).enclosingBoundingBox(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 80 | |
| 81 | if (containerRect.isEmpty()) |
| 82 | return FloatRect(); |
| 83 | |
| 84 | // Make the coordinates relative to the container enclosing bounding box. |
| 85 | // Since we work with rects enclosing quad unions this is still transform-friendly. |
| 86 | FloatRect normalizedRect = absoluteRect; |
| 87 | normalizedRect.moveBy(-containerRect.location()); |
| 88 | |
| 89 | // Fixed positions do not make sense in this coordinate system, but need to leave consistent tickmarks. |
| 90 | // So, use their position when the view is not scrolled, like an absolute position. |
| 91 | if (renderer->style()->position() == FixedPosition && container->isRenderView()) |
| 92 | normalizedRect.move(-toRenderView(container)->frameView()->scrollOffsetForFixedPosition()); |
| 93 | |
| 94 | normalizedRect.scale(1 / containerRect.width(), 1 / containerRect.height()); |
| 95 | return normalizedRect; |
| 96 | } |
| 97 | |
| 98 | FloatRect findInPageRectFromAbsoluteRect(const FloatRect& inputRect, const RenderObject* baseRenderer) |
| 99 | { |
| 100 | if (!baseRenderer || inputRect.isEmpty()) |
| 101 | return FloatRect(); |
| 102 | |
| 103 | // Normalize the input rect to its container block. |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 104 | const RenderBlock* baseContainer = enclosingScrollableAncestor(baseRenderer); |
| 105 | FloatRect normalizedRect = toNormalizedRect(inputRect, baseRenderer, baseContainer); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 106 | |
| 107 | // Go up across frames. |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 108 | for (const RenderBox* renderer = baseContainer; renderer; ) { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 109 | |
| 110 | // Go up the render tree until we reach the root of the current frame (the RenderView). |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 111 | while (!renderer->isRenderView()) { |
| 112 | const RenderBlock* container = enclosingScrollableAncestor(renderer); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 113 | |
| 114 | // Compose the normalized rects. |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 115 | FloatRect normalizedBoxRect = toNormalizedRect(renderer->absoluteBoundingBoxRect(), renderer, container); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 116 | normalizedRect.scale(normalizedBoxRect.width(), normalizedBoxRect.height()); |
| 117 | normalizedRect.moveBy(normalizedBoxRect.location()); |
| 118 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 119 | renderer = container; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 122 | ASSERT(renderer->isRenderView()); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 123 | |
| 124 | // Jump to the renderer owning the frame, if any. |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 125 | renderer = renderer->frame() ? renderer->frame()->ownerRenderer() : 0; |
| 126 | } |
| 127 | |
| 128 | return normalizedRect; |
| 129 | } |
| 130 | |
| 131 | FloatRect findInPageRectFromRange(Range* range) |
| 132 | { |
| 133 | if (!range || !range->firstNode()) |
| 134 | return FloatRect(); |
| 135 | |
| 136 | return findInPageRectFromAbsoluteRect(RenderObject::absoluteBoundingBoxRectForRange(range), range->firstNode()->renderer()); |
| 137 | } |
| 138 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 139 | } // namespace blink |