LibWeb+WebContent: Partially implement WebDriver's JSON deserialize AO

In particular, we need to convert web element references to the actual
element. The AO isn't fully implemented because we will need to work out
mixing JsonValue types with JS value types, which currently isn't very
straight forward with our JSON clone algorithm.
This commit is contained in:
Timothy Flynn 2024-09-29 11:44:17 -04:00 committed by Andreas Kling
commit 94c243acd6
Notes: github-actions[bot] 2024-10-01 09:03:42 +00:00
3 changed files with 60 additions and 5 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -44,6 +44,23 @@ JsonObject web_element_reference_object(Web::DOM::Node const& element)
return object;
}
// https://w3c.github.io/webdriver/#dfn-deserialize-a-web-element
ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, WebDriver::Error> deserialize_web_element(JsonObject const& object)
{
// 1. If object has no own property web element identifier, return error with error code invalid argument.
if (!object.has_string(web_element_identifier))
return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Object is not a web element");
// 2. Let reference be the result of getting the web element identifier property from object.
auto reference = extract_web_element_reference(object);
// 3. Let element be the result of trying to get a known element with session and reference.
auto* element = TRY(get_known_connected_element(reference));
// 4. Return success with data element.
return *element;
}
ByteString extract_web_element_reference(JsonObject const& object)
{
return object.get_byte_string(web_element_identifier).release_value();