Contexts.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/TraversableNavigable.h>
  10. #include <LibWeb/HTML/WindowProxy.h>
  11. #include <LibWeb/WebDriver/Contexts.h>
  12. namespace Web::WebDriver {
  13. // https://w3c.github.io/webdriver/#dfn-windowproxy-reference-object
  14. JsonObject window_proxy_reference_object(HTML::WindowProxy const& window)
  15. {
  16. // 1. Let identifier be the web window identifier if the associated browsing context of window is a top-level browsing context.
  17. // Otherwise let it be the web frame identifier.
  18. // NOTE: We look at the active browsing context's active document's node navigable instead.
  19. // Because a Browsing context's top-level traversable is this navigable's top level traversable.
  20. // Ref: https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable
  21. auto traversable_navigable = window.associated_browsing_context()->active_document()->navigable()->traversable_navigable();
  22. auto identifier = traversable_navigable->is_top_level_traversable()
  23. ? WEB_WINDOW_IDENTIFIER
  24. : WEB_FRAME_IDENTIFIER;
  25. // 2. Return a JSON Object initialized with the following properties:
  26. JsonObject object;
  27. // identifier
  28. // Associated window handle of the window’s browsing context.
  29. object.set(identifier, traversable_navigable->window_handle().to_byte_string());
  30. return object;
  31. }
  32. }