DOMRectReadOnly.cpp 3.0 KB

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