History.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/HTML/History.h>
  8. namespace Web::HTML {
  9. JS::NonnullGCPtr<History> History::create(HTML::Window& window, DOM::Document& document)
  10. {
  11. return *window.heap().allocate<History>(window.realm(), window, document);
  12. }
  13. History::History(HTML::Window& window, DOM::Document& document)
  14. : PlatformObject(window.realm())
  15. , m_associated_document(document)
  16. {
  17. set_prototype(&window.cached_web_prototype("History"));
  18. }
  19. History::~History() = default;
  20. void History::visit_edges(Cell::Visitor& visitor)
  21. {
  22. Base::visit_edges(visitor);
  23. visitor.visit(m_associated_document.ptr());
  24. }
  25. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  26. DOM::ExceptionOr<void> History::push_state(JS::Value data, String const&, String const& url)
  27. {
  28. // NOTE: The second parameter of this function is intentionally unused.
  29. return shared_history_push_replace_state(data, url, IsPush::Yes);
  30. }
  31. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  32. DOM::ExceptionOr<void> History::replace_state(JS::Value data, String const&, String const& url)
  33. {
  34. // NOTE: The second parameter of this function is intentionally unused.
  35. return shared_history_push_replace_state(data, url, IsPush::No);
  36. }
  37. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  38. DOM::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value, String const&, IsPush)
  39. {
  40. // 1. Let document be history's associated Document. (NOTE: Not necessary)
  41. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  42. if (!m_associated_document->is_fully_active())
  43. return DOM::SecurityError::create(global_object(), "Cannot perform pushState or replaceState on a document that isn't fully active.");
  44. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  45. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  46. dbgln("FIXME: Implement shared_history_push_replace_state.");
  47. return {};
  48. // FIXME: Add the rest of the spec steps once they're added.
  49. }
  50. }