LibWebView+WebConent: Add an IPC to get an element's selected state

This commit is contained in:
Timothy Flynn 2022-11-03 17:04:24 -04:00 committed by Tim Flynn
parent d907fb304e
commit 4067138702
Notes: sideshowbarker 2024-07-17 04:48:37 +09:00
5 changed files with 31 additions and 0 deletions

View file

@ -530,6 +530,11 @@ Optional<Vector<i32>> OutOfProcessWebView::query_selector_all(i32 start_node_id,
return client().query_selector_all(start_node_id, selector);
}
bool OutOfProcessWebView::is_element_selected(i32 element_id)
{
return client().is_element_selected(element_id);
}
Optional<String> OutOfProcessWebView::get_element_attribute(i32 element_id, String const& name)
{
return client().get_element_attribute(element_id, name);

View file

@ -65,6 +65,7 @@ public:
Optional<i32> get_document_element();
Optional<Vector<i32>> query_selector_all(i32 start_node_id, String const& selector);
bool is_element_selected(i32 element_id);
Optional<String> get_element_attribute(i32 element_id, String const& name);
Optional<String> get_element_property(i32 element_id, String const& name);
String get_active_documents_type();

View file

@ -28,6 +28,8 @@
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Storage.h>
#include <LibWeb/HTML/Window.h>
@ -501,6 +503,27 @@ static Optional<Web::DOM::Element&> find_element_by_id(i32 element_id)
return verify_cast<Web::DOM::Element>(*node);
}
Messages::WebContentServer::IsElementSelectedResponse ConnectionFromClient::is_element_selected(i32 element_id)
{
auto element = find_element_by_id(element_id);
if (!element.has_value())
return { false };
bool selected = false;
if (is<Web::HTML::HTMLInputElement>(*element)) {
auto& input = dynamic_cast<Web::HTML::HTMLInputElement&>(*element);
using enum Web::HTML::HTMLInputElement::TypeAttributeState;
if (input.type_state() == Checkbox || input.type_state() == RadioButton)
selected = input.checked();
} else if (is<Web::HTML::HTMLOptionElement>(*element)) {
selected = dynamic_cast<Web::HTML::HTMLOptionElement&>(*element).selected();
}
return { selected };
}
Messages::WebContentServer::GetElementAttributeResponse ConnectionFromClient::get_element_attribute(i32 element_id, String const& name)
{
auto element = find_element_by_id(element_id);

View file

@ -85,6 +85,7 @@ private:
virtual Messages::WebContentServer::GetDocumentElementResponse get_document_element() override;
virtual Messages::WebContentServer::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override;
virtual Messages::WebContentServer::IsElementSelectedResponse is_element_selected(i32 element_id) override;
virtual Messages::WebContentServer::GetElementAttributeResponse get_element_attribute(i32 element_id, String const& name) override;
virtual Messages::WebContentServer::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override;
virtual Messages::WebContentServer::GetActiveDocumentsTypeResponse get_active_documents_type() override;

View file

@ -42,6 +42,7 @@ endpoint WebContentServer
get_document_element() => (Optional<i32> node_id)
query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids)
is_element_selected(i32 element_id) => (bool selected)
get_element_attribute(i32 element_id, String name) => (Optional<String> attribute)
get_element_property(i32 element_id, String name) => (Optional<String> property)
get_active_documents_type() => (String type)