1/*
2 * Copyright (C) 2019 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(RESIZE_OBSERVER)
29#include "ResizeObservation.h"
30
31#include "HTMLFrameOwnerElement.h"
32#include "RenderBox.h"
33#include "SVGElement.h"
34
35namespace WebCore {
36
37Ref<ResizeObservation> ResizeObservation::create(Element* target)
38{
39 return adoptRef(*new ResizeObservation(target));
40}
41
42ResizeObservation::ResizeObservation(Element* element)
43 : m_target(element)
44{
45}
46
47ResizeObservation::~ResizeObservation()
48{
49}
50
51void ResizeObservation::updateObservationSize(const LayoutSize& size)
52{
53 m_lastObservationSize = size;
54}
55
56LayoutSize ResizeObservation::computeObservedSize() const
57{
58 if (m_target->isSVGElement()) {
59 FloatRect svgRect;
60 if (downcast<SVGElement>(*m_target).getBoundingBox(svgRect))
61 return LayoutSize(svgRect.width(), svgRect.height());
62 }
63 if (m_target->renderBox())
64 return m_target->renderBox()->contentSize();
65 return LayoutSize();
66}
67
68LayoutPoint ResizeObservation::computeTargetLocation() const
69{
70 if (!m_target->isSVGElement()) {
71 if (auto box = m_target->renderBox())
72 return LayoutPoint(box->paddingLeft(), box->paddingTop());
73 }
74
75 return { };
76}
77
78FloatRect ResizeObservation::computeContentRect() const
79{
80 return FloatRect(FloatPoint(computeTargetLocation()), FloatSize(m_lastObservationSize));
81}
82
83bool ResizeObservation::elementSizeChanged(LayoutSize& currentSize) const
84{
85 currentSize = computeObservedSize();
86 return m_lastObservationSize != currentSize;
87}
88
89size_t ResizeObservation::targetElementDepth() const
90{
91 unsigned depth = 0;
92 for (Element* ownerElement = m_target; ownerElement; ownerElement = ownerElement->document().ownerElement()) {
93 for (Element* parent = ownerElement; parent; parent = parent->parentElement())
94 ++depth;
95 }
96
97 return depth;
98}
99
100} // namespace WebCore
101
102#endif // ENABLE(RESIZE_OBSERVER)
103