DOMRectReadOnly.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Geometry/DOMRectReadOnly.h>
  9. #include <LibWeb/HTML/StructuredSerialize.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::Geometry {
  12. JS_DEFINE_ALLOCATOR(DOMRectReadOnly);
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> DOMRectReadOnly::construct_impl(JS::Realm& realm, double x, double y, double width, double height)
  14. {
  15. return realm.heap().allocate<DOMRectReadOnly>(realm, realm, x, y, width, height);
  16. }
  17. // https://drafts.fxtf.org/geometry/#create-a-domrect-from-the-dictionary
  18. JS::NonnullGCPtr<DOMRectReadOnly> DOMRectReadOnly::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other)
  19. {
  20. auto& realm = *vm.current_realm();
  21. return realm.heap().allocate<DOMRectReadOnly>(realm, realm, other.x, other.y, other.width, other.height);
  22. }
  23. JS::NonnullGCPtr<DOMRectReadOnly> DOMRectReadOnly::create(JS::Realm& realm)
  24. {
  25. return realm.heap().allocate<DOMRectReadOnly>(realm, realm);
  26. }
  27. DOMRectReadOnly::DOMRectReadOnly(JS::Realm& realm, double x, double y, double width, double height)
  28. : PlatformObject(realm)
  29. , m_rect(x, y, width, height)
  30. {
  31. }
  32. DOMRectReadOnly::DOMRectReadOnly(JS::Realm& realm)
  33. : PlatformObject(realm)
  34. {
  35. }
  36. DOMRectReadOnly::~DOMRectReadOnly() = default;
  37. void DOMRectReadOnly::initialize(JS::Realm& realm)
  38. {
  39. Base::initialize(realm);
  40. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMRectReadOnly);
  41. }
  42. // https://drafts.fxtf.org/geometry/#structured-serialization
  43. WebIDL::ExceptionOr<void> DOMRectReadOnly::serialization_steps(HTML::SerializationRecord& serialized, bool)
  44. {
  45. // 1. Set serialized.[[X]] to value’s x coordinate.
  46. HTML::serialize_primitive_type(serialized, this->x());
  47. // 2. Set serialized.[[Y]] to value’s y coordinate.
  48. HTML::serialize_primitive_type(serialized, this->y());
  49. // 3. Set serialized.[[Width]] to value’s width.
  50. HTML::serialize_primitive_type(serialized, this->width());
  51. // 4. Set serialized.[[Height]] to value’s height.
  52. HTML::serialize_primitive_type(serialized, this->height());
  53. return {};
  54. }
  55. // https://drafts.fxtf.org/geometry/#structured-serialization
  56. WebIDL::ExceptionOr<void> DOMRectReadOnly::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position)
  57. {
  58. // 1. Set value’s x coordinate to serialized.[[X]].
  59. auto x = HTML::deserialize_primitive_type<double>(serialized, position);
  60. // 2. Set value’s y coordinate to serialized.[[Y]].
  61. auto y = HTML::deserialize_primitive_type<double>(serialized, position);
  62. // 3. Set value’s width to serialized.[[Width]].
  63. auto width = HTML::deserialize_primitive_type<double>(serialized, position);
  64. // 4. Set value’s height to serialized.[[Height]].
  65. auto height = HTML::deserialize_primitive_type<double>(serialized, position);
  66. m_rect = { x, y, width, height };
  67. return {};
  68. }
  69. }