2024-02-19 05:06:39 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Heap.h>
|
2024-02-19 05:06:39 +01:00
|
|
|
#include <LibWeb/Bindings/ResizeObserverSizePrototype.h>
|
2026-02-11 07:33:58 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2024-02-19 05:06:39 +01:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
|
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
|
|
|
|
#include <LibWeb/ResizeObserver/ResizeObserverSize.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::ResizeObserver {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(ResizeObserverSize);
|
2024-02-19 05:06:39 +01:00
|
|
|
|
|
|
|
|
void ResizeObserverSize::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(ResizeObserverSize);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2024-02-19 05:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://drafts.csswg.org/resize-observer-1/#calculate-box-size
|
2026-02-21 01:33:41 +01:00
|
|
|
ResizeObserverSize::RawSize ResizeObserverSize::compute_box_size(DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
|
2024-02-19 05:06:39 +01:00
|
|
|
{
|
2026-02-21 01:33:41 +01:00
|
|
|
RawSize size;
|
2024-02-19 05:06:39 +01:00
|
|
|
|
2026-02-21 01:33:41 +01:00
|
|
|
// FIXME: If target is an SVGGraphicsElement that does not have an associated CSS layout box:
|
2024-02-19 05:06:39 +01:00
|
|
|
// Otherwise:
|
2026-02-26 11:57:29 +01:00
|
|
|
// NB: Layout was up to date when observations were gathered, but a previous
|
|
|
|
|
// observer's callback may have invalidated it before we get here.
|
|
|
|
|
// This matches the behavior of all major browsers.
|
|
|
|
|
if (target.unsafe_paintable_box()) {
|
|
|
|
|
auto const& paintable_box = *target.unsafe_paintable_box();
|
2024-02-19 05:06:39 +01:00
|
|
|
switch (observed_box) {
|
|
|
|
|
case Bindings::ResizeObserverBoxOptions::BorderBox:
|
2026-02-21 01:33:41 +01:00
|
|
|
size.inline_size = paintable_box.border_box_width().to_double();
|
|
|
|
|
size.block_size = paintable_box.border_box_height().to_double();
|
2024-02-19 05:06:39 +01:00
|
|
|
break;
|
|
|
|
|
case Bindings::ResizeObserverBoxOptions::ContentBox:
|
2026-02-21 01:33:41 +01:00
|
|
|
size.inline_size = paintable_box.content_width().to_double();
|
|
|
|
|
size.block_size = paintable_box.content_height().to_double();
|
2024-02-19 05:06:39 +01:00
|
|
|
break;
|
|
|
|
|
case Bindings::ResizeObserverBoxOptions::DevicePixelContentBox: {
|
2024-03-10 08:41:18 +01:00
|
|
|
auto device_pixel_ratio = target.document().window()->device_pixel_ratio();
|
2026-02-21 01:33:41 +01:00
|
|
|
size.inline_size = paintable_box.border_box_width().to_double() * device_pixel_ratio;
|
|
|
|
|
size.block_size = paintable_box.border_box_height().to_double() * device_pixel_ratio;
|
2024-02-19 05:06:39 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 01:33:41 +01:00
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GC::Ref<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
|
|
|
|
|
{
|
|
|
|
|
auto raw = compute_box_size(target, observed_box);
|
|
|
|
|
auto computed_size = realm.create<ResizeObserverSize>(realm);
|
|
|
|
|
computed_size->set_inline_size(raw.inline_size);
|
|
|
|
|
computed_size->set_block_size(raw.block_size);
|
2024-02-19 05:06:39 +01:00
|
|
|
return computed_size;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 01:33:41 +01:00
|
|
|
bool ResizeObserverSize::equals(RawSize const& other) const
|
|
|
|
|
{
|
|
|
|
|
return m_inline_size == other.inline_size && m_block_size == other.block_size;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 05:06:39 +01:00
|
|
|
bool ResizeObserverSize::equals(ResizeObserverSize const& other) const
|
|
|
|
|
{
|
|
|
|
|
return m_inline_size == other.m_inline_size && m_block_size == other.m_block_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|