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