From 4067138702b68cbc5805ed7881e4c3bfdd688757 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 3 Nov 2022 17:04:24 -0400 Subject: [PATCH] LibWebView+WebConent: Add an IPC to get an element's selected state --- .../LibWebView/OutOfProcessWebView.cpp | 5 ++++ .../LibWebView/OutOfProcessWebView.h | 1 + .../WebContent/ConnectionFromClient.cpp | 23 +++++++++++++++++++ .../WebContent/ConnectionFromClient.h | 1 + .../Services/WebContent/WebContentServer.ipc | 1 + 5 files changed, 31 insertions(+) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index d7ccbaeac7..464eba8cb9 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -530,6 +530,11 @@ Optional> 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 OutOfProcessWebView::get_element_attribute(i32 element_id, String const& name) { return client().get_element_attribute(element_id, name); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 934c865f29..9d2e072223 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -65,6 +65,7 @@ public: Optional get_document_element(); Optional> query_selector_all(i32 start_node_id, String const& selector); + bool is_element_selected(i32 element_id); Optional get_element_attribute(i32 element_id, String const& name); Optional get_element_property(i32 element_id, String const& name); String get_active_documents_type(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 73c1c9a935..5a153fae41 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include #include @@ -501,6 +503,27 @@ static Optional find_element_by_id(i32 element_id) return verify_cast(*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(*element)) { + auto& input = dynamic_cast(*element); + using enum Web::HTML::HTMLInputElement::TypeAttributeState; + + if (input.type_state() == Checkbox || input.type_state() == RadioButton) + selected = input.checked(); + } else if (is(*element)) { + selected = dynamic_cast(*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); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index c6cbb114c4..07d65bb16a 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -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; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 88c6841dfc..e5b4a4aa6e 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -42,6 +42,7 @@ endpoint WebContentServer get_document_element() => (Optional node_id) query_selector_all(i32 start_node_id, String selector) => (Optional> elements_ids) + is_element_selected(i32 element_id) => (bool selected) get_element_attribute(i32 element_id, String name) => (Optional attribute) get_element_property(i32 element_id, String name) => (Optional property) get_active_documents_type() => (String type)