History.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. History::History(DOM::Document& document)
  10. : m_associated_document(document)
  11. {
  12. }
  13. History::~History() = default;
  14. // https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate
  15. DOM::ExceptionOr<void> History::push_state(JS::Value data, String const&, String const& url)
  16. {
  17. // NOTE: The second parameter of this function is intentionally unused.
  18. return shared_history_push_replace_state(data, url, IsPush::Yes);
  19. }
  20. // https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate
  21. DOM::ExceptionOr<void> History::replace_state(JS::Value data, String const&, String const& url)
  22. {
  23. // NOTE: The second parameter of this function is intentionally unused.
  24. return shared_history_push_replace_state(data, url, IsPush::No);
  25. }
  26. // https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps
  27. DOM::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value, String const&, IsPush)
  28. {
  29. // 1. Let document be history's associated Document. (NOTE: Not necessary)
  30. // 2. If document is not fully active, then throw a "SecurityError" DOMException.
  31. if (!m_associated_document.is_fully_active())
  32. return DOM::SecurityError::create("Cannot perform pushState or replaceState on a document that isn't fully active.");
  33. // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer,
  34. // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.)
  35. dbgln("FIXME: Implement shared_history_push_replace_state.");
  36. return {};
  37. // FIXME: Add the rest of the spec steps once they're added.
  38. }
  39. }