diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-03 17:04:24 -0400 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-11-03 19:40:43 -0400 |
commit | 4067138702b68cbc5805ed7881e4c3bfdd688757 (patch) | |
tree | 26919713d0d249c4c4132254960de5a6d7711e84 /Userland/Services/WebContent | |
parent | d907fb304ea775179d85f235a29640787d0016c9 (diff) | |
download | serenity-4067138702b68cbc5805ed7881e4c3bfdd688757.zip |
LibWebView+WebConent: Add an IPC to get an element's selected state
Diffstat (limited to 'Userland/Services/WebContent')
-rw-r--r-- | Userland/Services/WebContent/ConnectionFromClient.cpp | 23 | ||||
-rw-r--r-- | Userland/Services/WebContent/ConnectionFromClient.h | 1 | ||||
-rw-r--r-- | Userland/Services/WebContent/WebContentServer.ipc | 1 |
3 files changed, 25 insertions, 0 deletions
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 <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); 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<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) |