ResizeObserverSize.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibWeb/Bindings/ResizeObserverSizePrototype.h>
  8. #include <LibWeb/HTML/Window.h>
  9. #include <LibWeb/Painting/PaintableBox.h>
  10. #include <LibWeb/ResizeObserver/ResizeObserverSize.h>
  11. namespace Web::ResizeObserver {
  12. JS_DEFINE_ALLOCATOR(ResizeObserverSize);
  13. void ResizeObserverSize::initialize(JS::Realm& realm)
  14. {
  15. Base::initialize(realm);
  16. WEB_SET_PROTOTYPE_FOR_INTERFACE(ResizeObserverSize);
  17. }
  18. // https://drafts.csswg.org/resize-observer-1/#calculate-box-size
  19. JS::NonnullGCPtr<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
  20. {
  21. // 1. Let computedSize be a new ResizeObserverSize object.
  22. auto computed_size = realm.heap().allocate<ResizeObserverSize>(realm, realm);
  23. // FIXME: 2. If target is an SVGGraphicsElement that does not have an associated CSS layout box:
  24. // Otherwise:
  25. if (target.paintable_box()) {
  26. auto const& paintable_box = *target.paintable_box();
  27. switch (observed_box) {
  28. case Bindings::ResizeObserverBoxOptions::BorderBox:
  29. // 1. Set computedSize’s inlineSize attribute to target’s border area inline length.
  30. computed_size->set_inline_size(paintable_box.border_box_width().to_double());
  31. // 2. Set computedSize’s blockSize attribute to target’s border area block length.
  32. computed_size->set_block_size(paintable_box.border_box_height().to_double());
  33. break;
  34. case Bindings::ResizeObserverBoxOptions::ContentBox:
  35. // 1. Set computedSize’s inlineSize attribute to target’s content area inline length.
  36. computed_size->set_inline_size(paintable_box.content_width().to_double());
  37. // 2. Set computedSize’s blockSize attribute to target’s content area block length.
  38. computed_size->set_block_size(paintable_box.content_height().to_double());
  39. break;
  40. case Bindings::ResizeObserverBoxOptions::DevicePixelContentBox: {
  41. auto device_pixel_ratio = target.document().window()->device_pixel_ratio();
  42. // 1. Set computedSize’s inlineSize attribute to target’s content area inline length, in integral device pixels.
  43. computed_size->set_inline_size(paintable_box.border_box_width().to_double() * device_pixel_ratio);
  44. // 2. Set computedSize’s blockSize attribute to target’s content area block length, in integral device pixels.
  45. computed_size->set_block_size(paintable_box.border_box_height().to_double() * device_pixel_ratio);
  46. break;
  47. }
  48. default:
  49. VERIFY_NOT_REACHED();
  50. }
  51. }
  52. // 3. Return computedSize.s
  53. return computed_size;
  54. }
  55. bool ResizeObserverSize::equals(ResizeObserverSize const& other) const
  56. {
  57. return m_inline_size == other.m_inline_size && m_block_size == other.m_block_size;
  58. }
  59. }