NavigationDestination.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/NavigationDestinationPrototype.h>
  10. #include <LibWeb/HTML/NavigationDestination.h>
  11. #include <LibWeb/HTML/NavigationHistoryEntry.h>
  12. namespace Web::HTML {
  13. JS_DEFINE_ALLOCATOR(NavigationDestination);
  14. JS::NonnullGCPtr<NavigationDestination> NavigationDestination::create(JS::Realm& realm)
  15. {
  16. return realm.heap().allocate<NavigationDestination>(realm, realm);
  17. }
  18. NavigationDestination::NavigationDestination(JS::Realm& realm)
  19. : Bindings::PlatformObject(realm)
  20. {
  21. }
  22. NavigationDestination::~NavigationDestination() = default;
  23. void NavigationDestination::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(NavigationDestination);
  27. }
  28. void NavigationDestination::visit_edges(JS::Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_entry);
  32. }
  33. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-url
  34. WebIDL::ExceptionOr<String> NavigationDestination::url() const
  35. {
  36. // The url getter steps are to return this's URL, serialized.
  37. return TRY_OR_THROW_OOM(vm(), String::from_byte_string(m_url.serialize()));
  38. }
  39. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-key
  40. String NavigationDestination::key() const
  41. {
  42. // The key getter steps are:
  43. // 1. If this's entry is null, then return the empty string.
  44. // 2. Return this's entry's key.
  45. return (m_entry == nullptr) ? String {} : m_entry->key();
  46. }
  47. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-id
  48. String NavigationDestination::id() const
  49. {
  50. // The id getter steps are:
  51. // 1. If this's entry is null, then return the empty string.
  52. // 2. Return this's entry's ID.
  53. return (m_entry == nullptr) ? String {} : m_entry->id();
  54. }
  55. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-index
  56. i64 NavigationDestination::index() const
  57. {
  58. // The index getter steps are:
  59. // 1. If this's entry is null, then return -1.
  60. // 2. Return this's entry's index.
  61. return (m_entry == nullptr) ? -1 : m_entry->index();
  62. }
  63. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-samedocument
  64. bool NavigationDestination::same_document() const
  65. {
  66. // The sameDocument getter steps are to return this's is same document.
  67. return m_is_same_document;
  68. }
  69. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-getstate
  70. WebIDL::ExceptionOr<JS::Value> NavigationDestination::get_state()
  71. {
  72. // The getState() method steps are to return StructuredDeserialize(this's state).
  73. return structured_deserialize(vm(), m_state, realm(), {});
  74. }
  75. }