NavigationDestination.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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::NonnullGCPtr<NavigationDestination> NavigationDestination::create(JS::Realm& realm)
  14. {
  15. return realm.heap().allocate<NavigationDestination>(realm, realm);
  16. }
  17. NavigationDestination::NavigationDestination(JS::Realm& realm)
  18. : Bindings::PlatformObject(realm)
  19. {
  20. }
  21. NavigationDestination::~NavigationDestination() = default;
  22. void NavigationDestination::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationDestinationPrototype>(realm, "NavigationDestination"));
  26. }
  27. void NavigationDestination::visit_edges(JS::Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_entry);
  31. }
  32. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-url
  33. WebIDL::ExceptionOr<String> NavigationDestination::url() const
  34. {
  35. // The url getter steps are to return this's URL, serialized.
  36. return TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(m_url.serialize()));
  37. }
  38. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-key
  39. String NavigationDestination::key() const
  40. {
  41. // The key getter steps are:
  42. // 1. If this's entry is null, then return the empty string.
  43. // 2. Return this's entry's key.
  44. return (m_entry == nullptr) ? String {} : m_entry->key();
  45. }
  46. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-id
  47. String NavigationDestination::id() const
  48. {
  49. // The id getter steps are:
  50. // 1. If this's entry is null, then return the empty string.
  51. // 2. Return this's entry's ID.
  52. return (m_entry == nullptr) ? String {} : m_entry->id();
  53. }
  54. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-index
  55. i64 NavigationDestination::index() const
  56. {
  57. // The index getter steps are:
  58. // 1. If this's entry is null, then return -1.
  59. // 2. Return this's entry's index.
  60. return (m_entry == nullptr) ? -1 : m_entry->index();
  61. }
  62. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-samedocument
  63. bool NavigationDestination::same_document() const
  64. {
  65. // The sameDocument getter steps are to return this's is same document.
  66. return m_is_same_document;
  67. }
  68. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-getstate
  69. WebIDL::ExceptionOr<JS::Value> NavigationDestination::get_state()
  70. {
  71. // The getState() method steps are to return StructuredDeserialize(this's state).
  72. return structured_deserialize(vm(), m_state, realm(), {});
  73. }
  74. }