LibWeb: Begin implementing a Selenium-like method to get element text

Unfortunately, there isn't an exact spec method to get the rendered text
of an element, including its shadow DOM. The WebDriver spec requires
just doing exactly what Selenium does.

This patch does not implement this, but is a step in the right direction
as we will now handle text transforms.
This commit is contained in:
Timothy Flynn 2024-11-03 17:58:32 -05:00 committed by Tim Flynn
parent a4daf6f928
commit 64a8fcc4ef
Notes: github-actions[bot] 2024-11-04 01:43:42 +00:00
3 changed files with 18 additions and 3 deletions

View file

@ -516,6 +516,18 @@ bool is_shadow_root_detached(Web::DOM::ShadowRoot const& shadow_root)
return !shadow_root.document().is_active() || !shadow_root.host() || is_element_stale(*shadow_root.host());
}
// https://w3c.github.io/webdriver/#dfn-bot-dom-getvisibletext
String element_rendered_text(DOM::Node& node)
{
// FIXME: The spec does not define how to get the element's rendered text, other than to do exactly as Selenium does.
// This implementation is not sufficient, as we must also at least consider the shadow DOM.
if (!is<HTML::HTMLElement>(node))
return node.text_content().value_or(String {});
auto& element = static_cast<HTML::HTMLElement&>(node);
return element.inner_text();
}
// https://w3c.github.io/webdriver/#dfn-center-point
CSSPixelPoint in_view_center_point(DOM::Element const& element, CSSPixelRect viewport)
{

View file

@ -55,6 +55,8 @@ ErrorOr<JS::NonnullGCPtr<Web::DOM::ShadowRoot>, WebDriver::Error> deserialize_sh
ErrorOr<JS::NonnullGCPtr<Web::DOM::ShadowRoot>, Web::WebDriver::Error> get_known_shadow_root(HTML::BrowsingContext const&, StringView reference);
bool is_shadow_root_detached(Web::DOM::ShadowRoot const&);
String element_rendered_text(DOM::Node&);
CSSPixelPoint in_view_center_point(DOM::Element const& element, CSSPixelRect viewport);
}

View file

@ -1322,11 +1322,12 @@ Messages::WebDriverClient::GetElementTextResponse WebDriverConnection::get_eleme
// 3. Let element be the result of trying to get a known connected element with url variable element id.
auto element = WEBDRIVER_TRY(Web::WebDriver::get_known_element(current_browsing_context(), element_id));
// 4. Let rendered text be the result of performing implementation-specific steps whose result is exactly the same as the result of a Function.[[Call]](null, element) with bot.dom.getVisibleText as the this value.
auto rendered_text = element->text_content();
// 4. Let rendered text be the result of performing implementation-specific steps whose result is exactly the
// same as the result of a Function.[[Call]](null, element) with bot.dom.getVisibleText as the this value.
auto rendered_text = Web::WebDriver::element_rendered_text(element);
// 5. Return success with data rendered text.
async_driver_execution_complete({ rendered_text.value_or(String {}).to_byte_string() });
async_driver_execution_complete({ rendered_text.to_byte_string() });
});
return JsonValue {};