Skip to content

Truly independent web browser

LibWeb+LibWebView+WebContent: Check spelling in the UI process

sideshowbarker0d105db997df
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index df49b052082..28412131416 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -2608,6 +2608,28 @@ Utf16String Internals::spelling_error_ranges(GC::Ref<DOM::Node> node)
return Utf16String::from_utf8(builder.string_view());
}
+GC::Ref<WebIDL::Promise> Internals::check_spelling_for_testing(Utf16String const& text)
+{
+ auto& realm = window().principal_realm();
+ auto promise = WebIDL::create_promise_for(window());
+ auto& page = this->page();
+ if (!page.spell_checking_available()) {
+ WebIDL::reject_promise(realm, promise, JS::PrimitiveString::create(vm(), "Spellchecking is unavailable"_utf16));
+ return promise;
+ }
+ page.request_spell_check(text, GC::create_function(heap(), [&realm, promise](Vector<SpellingError> errors) {
+ HTML::TemporaryExecutionContext execution_context { realm };
+ StringBuilder builder;
+ for (auto const& error : errors) {
+ if (!builder.is_empty())
+ builder.append(' ');
+ builder.appendff("{}-{}", error.offset, error.offset + error.length);
+ }
+ WebIDL::resolve_promise(realm, promise, JS::PrimitiveString::create(realm.vm(), Utf16String::from_utf8(builder.string_view())));
+ }));
+ return promise;
+}
+
GC::Ptr<JS::Object> Internals::hit_test_result(double x, double y)
{
auto& realm = HTML::relevant_realm(window());
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index e05437b28b2..6fd599f6009 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -84,6 +84,7 @@ public:
WebIDL::ExceptionOr<void> add_spelling_error_for_testing(GC::Ref<DOM::Node>, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end);
Utf16String spelling_error_ranges(GC::Ref<DOM::Node>);
+ GC::Ref<WebIDL::Promise> check_spelling_for_testing(Utf16String const& text);
void send_text(HTML::HTMLElement&, Utf16String const&, WebIDL::UnsignedShort modifiers);
void send_text_through_ui_process(Utf16String const&);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 23cb411752d..999ff9bd505 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -47,6 +47,7 @@ interface Internals {
// Spellchecking
undefined addSpellingErrorForTesting(Node node, unsigned long start, unsigned long end);
DOMString spellingErrorRanges(Node node);
+ Promise<DOMString> checkSpellingForTesting(DOMString text);
const unsigned short MOD_NONE = 0;
const unsigned short MOD_ALT = 1;
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 4d7aa46ea79..525fdf68d18 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -131,6 +131,7 @@ void Page::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_window_rect_observer);
visitor.visit(m_on_pending_dialog_closed);
visitor.visit(m_pending_clipboard_requests);
+ visitor.visit(m_pending_spell_check_requests);
visitor.visit(m_emulated_position_data);
visitor.visit(m_emulated_position_data_observers);
visitor.visit(m_offscreen_canvases_pending_placeholder_commit);
@@ -1262,6 +1263,19 @@ void Page::retrieved_clipboard_entries(u64 request_id, Vector<Clipboard::SystemC
(*request)->function()(move(items));
}
+void Page::request_spell_check(Utf16String const& text, SpellCheckRequest request)
+{
+ auto request_id = m_next_spell_check_request_id++;
+ m_pending_spell_check_requests.set(request_id, request);
+ client().page_did_request_spell_check(request_id, text);
+}
+
+void Page::did_check_spelling(u64 request_id, Vector<SpellingError> errors)
+{
+ if (auto request = m_pending_spell_check_requests.take(request_id); request.has_value())
+ (*request)->function()(move(errors));
+}
+
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
void Page::set_emulated_position_data(Geolocation::EmulatedPositionData data)
{
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index ab788508e02..8500b87d164 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -88,6 +88,7 @@
#include <LibWebCommon/Page/PageId.h>
#include <LibWebCommon/Page/PendingDialog.h>
#include <LibWebCommon/Page/QueuedInputEvent.h>
+#include <LibWebCommon/Page/SpellingError.h>
#include <LibWebCommon/Page/ViewportIsFullscreen.h>
#include <LibWebCommon/PixelUnits.h>
#include <LibWebCommon/StorageAPI/StorageEndpoint.h>
@@ -298,6 +299,13 @@ public:
void request_clipboard_entries(ClipboardRequest);
void retrieved_clipboard_entries(u64 request_id, Vector<Clipboard::SystemClipboardItem>);
+ using SpellCheckRequest = GC::Ref<GC::Function<void(Vector<SpellingError>)>>;
+ bool spell_checking_available() const { return m_spell_checking_available; }
+ void set_spell_checking_available(bool available) { m_spell_checking_available = available; }
+ void request_spell_check(Utf16String const& text, SpellCheckRequest);
+ void did_check_spelling(u64 request_id, Vector<SpellingError>);
+ bool has_pending_spell_checks() const { return !m_pending_spell_check_requests.is_empty(); }
+
using GeolocationPositionResult = Variant<Geolocation::CoordinatesData, Geolocation::GeolocationPositionError::ErrorCode>;
using GeolocationPositionCallback = GC::Ref<GC::Function<void(GeolocationPositionResult)>>;
enum class GeolocationRequestType : u8 {
@@ -505,6 +513,10 @@ private:
HashMap<u64, ClipboardRequest> m_pending_clipboard_requests;
u64 m_next_clipboard_request_id { 0 };
+ bool m_spell_checking_available { false };
+ HashMap<u64, SpellCheckRequest> m_pending_spell_check_requests;
+ u64 m_next_spell_check_request_id { 0 };
+
struct PendingGeolocationRequest {
GeolocationPositionCallback callback;
GeolocationRequestType type;
@@ -807,6 +819,7 @@ public:
virtual void page_did_insert_clipboard_item(Clipboard::SystemClipboardItem const&, [[maybe_unused]] StringView presentation_style) { }
virtual void page_did_request_clipboard_entries([[maybe_unused]] u64 request_id) { }
+ virtual void page_did_request_spell_check([[maybe_unused]] u64 request_id, [[maybe_unused]] Utf16String const& text) { }
virtual void page_did_request_primary_paste() { }
virtual void page_did_complete_paste_action() { }
virtual void page_did_update_primary_selection(Utf16String const&) { }
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index 3b7d01138d3..5537de16c57 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -47,6 +47,7 @@ set(SOURCES
Page/DragEvent.cpp
Page/InputEvent.cpp
Page/MediaContextMenu.cpp
+ Page/SpellingError.cpp
PixelUnits.cpp
ReferrerPolicy/ReferrerPolicy.cpp
StorageAPI/StorageEndpoint.cpp
diff --git a/Libraries/LibWebCommon/Page/SpellingError.cpp b/Libraries/LibWebCommon/Page/SpellingError.cpp
new file mode 100644
index 00000000000..a756f5060d2
--- /dev/null
+++ b/Libraries/LibWebCommon/Page/SpellingError.cpp
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+#include <LibWebCommon/Page/SpellingError.h>
+
+template<>
+ErrorOr<void> IPC::encode(Encoder& encoder, Web::SpellingError const& error)
+{
+ TRY(encoder.encode(error.offset));
+ TRY(encoder.encode(error.length));
+ return {};
+}
+
+template<>
+ErrorOr<Web::SpellingError> IPC::decode(Decoder& decoder)
+{
+ auto offset = TRY(decoder.decode<u32>());
+ auto length = TRY(decoder.decode<u32>());
+ return Web::SpellingError { offset, length };
+}
diff --git a/Libraries/LibWebCommon/Page/SpellingError.h b/Libraries/LibWebCommon/Page/SpellingError.h
new file mode 100644
index 00000000000..3d745c43f91
--- /dev/null
+++ b/Libraries/LibWebCommon/Page/SpellingError.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+#include <LibIPC/Forward.h>
+#include <LibWebCommon/Export.h>
+
+namespace Web {
+
+// A misspelled run of a checked text, in UTF-16 code units.
+struct SpellingError {
+ u32 offset { 0 };
+ u32 length { 0 };
+
+ bool operator==(SpellingError const&) const = default;
+};
+
+// The page context menu's spelling items a test can ask the UI process to activate.
+enum class SpellingMenuActionForTesting : u8 {
+ None,
+ Guess,
+ IgnoreSpelling,
+ LearnSpelling,
+};
+
+}
+
+namespace IPC {
+
+template<>
+WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::SpellingError const&);
+
+template<>
+WEBCOMMON_API ErrorOr<Web::SpellingError> decode(Decoder&);
+
+}
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 6e4c26315dc..2fd4f46d19e 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -62,6 +62,7 @@
#include <LibWebView/RequestServerManager.h>
#include <LibWebView/SessionStore.h>
#include <LibWebView/SiteCompatibility.h>
+#include <LibWebView/SpellChecker.h>
#include <LibWebView/TabPerformanceMonitor.h>
#include <LibWebView/URL.h>
#include <LibWebView/UserAgent.h>
@@ -2853,6 +2854,11 @@ void Application::toggle_bookmark_for_view(ViewImplementation& view)
});
}
+void Application::set_spell_checker(OwnPtr<SpellChecker> spell_checker)
+{
+ m_spell_checker = move(spell_checker);
+}
+
ErrorOr<NonnullRawPtr<Core::GeolocationProvider>> Application::ensure_geolocation_provider()
{
if (!m_geolocation_provider)
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 37dcc80e8b2..3453ef4a958 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -101,6 +101,9 @@ public:
static BrowserOptions const& browser_options() { return the().m_browser_options; }
static RequestServerOptions const& request_server_options() { return the().m_request_server_options; }
static WebContentOptions& web_content_options() { return the().m_web_content_options; }
+
+ SpellChecker* spell_checker() { return m_spell_checker.ptr(); }
+ void set_spell_checker(OwnPtr<SpellChecker>);
static FontService& font_service() { return *the().m_font_service; }
static FontServiceHost& font_service_host() { return *the().m_font_service_host; }
JsonValue const& site_compatibility_data() const { return m_site_compatibility_data; }
@@ -624,6 +627,7 @@ private:
bool request_server_client_may_use_cookies_in(int client_id, URL::URL const&, Optional<HTTP::Cookie::PartitionContext> const&) const;
OwnPtr<Core::GeolocationProvider> m_geolocation_provider;
+ OwnPtr<SpellChecker> m_spell_checker;
OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
Core::EventLoop* m_event_loop { nullptr };
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index 69e821badef..3309f9cfa40 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -60,6 +60,7 @@ set(SOURCES
Settings.cpp
SiteCompatibility.cpp
StorageJar.cpp
+ TestSpellChecker.cpp
URL.cpp
UserAgent.cpp
Utilities.cpp
diff --git a/Libraries/LibWebView/Forward.h b/Libraries/LibWebView/Forward.h
index d80bd9efbff..02121e6a1a5 100644
--- a/Libraries/LibWebView/Forward.h
+++ b/Libraries/LibWebView/Forward.h
@@ -51,6 +51,7 @@ class RequestServerSiteBindings;
class SessionStore;
class Settings;
class SettingsUI;
+class SpellChecker;
class StorageJar;
class TraversableSessionHistory;
class ViewImplementation;
diff --git a/Libraries/LibWebView/SpellChecker.h b/Libraries/LibWebView/SpellChecker.h
new file mode 100644
index 00000000000..92936b74c56
--- /dev/null
+++ b/Libraries/LibWebView/SpellChecker.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+#include <AK/Utf16String.h>
+#include <AK/Vector.h>
+#include <LibWebCommon/Page/SpellingError.h>
+#include <LibWebView/Export.h>
+
+namespace WebView {
+
+// The platform spellchecker the UI process asks on behalf of every page. A document tag scopes what ignore() hides to
+// one tab.
+class WEBVIEW_API SpellChecker {
+ AK_ALLOC_WITH_KMALLOC;
+
+public:
+ using DocumentTag = i64;
+
+ virtual ~SpellChecker() = default;
+
+ virtual DocumentTag create_document_tag() = 0;
+ virtual void close_document_tag(DocumentTag) = 0;
+
+ // Asynchronous; on_complete runs on the main thread.
+ virtual void check(DocumentTag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) = 0;
+
+ virtual Vector<Utf16String> guesses(DocumentTag, Utf16String const& word) = 0;
+ virtual void ignore(DocumentTag, Utf16String const& word) = 0;
+ virtual void learn(DocumentTag, Utf16String const& word) = 0;
+};
+
+}
diff --git a/Libraries/LibWebView/TestSpellChecker.cpp b/Libraries/LibWebView/TestSpellChecker.cpp
new file mode 100644
index 00000000000..c3d741f6eae
--- /dev/null
+++ b/Libraries/LibWebView/TestSpellChecker.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/AnyOf.h>
+#include <AK/CharacterTypes.h>
+#include <LibCore/EventLoop.h>
+#include <LibWebView/TestSpellChecker.h>
+
+namespace WebView {
+
+static constexpr Array s_misspelled_words {
+ u"txet"sv,
+ u"Txet"sv,
+ u"quikc"sv,
+ u"wrod"sv,
+ u"speling"sv,
+ u"lernword"sv,
+};
+
+static Vector<Utf16String> guesses_for(Utf16View word)
+{
+ auto guesses = [](std::initializer_list<StringView> words) {
+ Vector<Utf16String> result;
+ for (auto word : words)
+ result.append(Utf16String::from_utf8(word));
+ return result;
+ };
+ if (word == u"txet"sv)
+ return guesses({ "text"sv, "teat"sv, "tet"sv });
+ if (word == u"Txet"sv)
+ return guesses({ "Text"sv, "Teat"sv, "Tet"sv });
+ if (word == u"wrod"sv)
+ return guesses({ "word"sv, "rod"sv, "wrong"sv, "wood"sv, "ford"sv, "prod"sv });
+ if (word == u"speling"sv)
+ return guesses({ "spelling"sv, "spieling"sv });
+ if (word == u"lernword"sv)
+ return guesses({ "learnword"sv });
+ return {};
+}
+
+static bool is_word_code_unit(char16_t code_unit)
+{
+ return is_ascii_alpha(code_unit) || code_unit == '\'';
+}
+
+SpellChecker::DocumentTag TestSpellChecker::create_document_tag()
+{
+ return m_next_document_tag++;
+}
+
+void TestSpellChecker::close_document_tag(DocumentTag tag)
+{
+ m_accepted_words.remove(tag);
+}
+
+bool TestSpellChecker::is_misspelled(DocumentTag tag, Utf16View word) const
+{
+ if (!any_of(s_misspelled_words, [&](auto misspelled) { return misspelled == word; }))
+ return false;
+ auto accepted = m_accepted_words.get(tag);
+ return !accepted.has_value() || !accepted->contains(Utf16String::from_utf16(word));
+}
+
+void TestSpellChecker::check(DocumentTag tag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete)
+{
+ Vector<Web::SpellingError> errors;
+ auto view = text.utf16_view();
+ size_t index = 0;
+ while (index < view.length_in_code_units()) {
+ if (!is_word_code_unit(view.code_unit_at(index))) {
+ ++index;
+ continue;
+ }
+ auto start = index;
+ while (index < view.length_in_code_units() && is_word_code_unit(view.code_unit_at(index)))
+ ++index;
+ if (is_misspelled(tag, view.substring_view(start, index - start)))
+ errors.append({ static_cast<u32>(start), static_cast<u32>(index - start) });
+ }
+
+ // Answer on a later turn of the event loop, as platform spellcheckers do.
+ Core::deferred_invoke([on_complete = move(on_complete), errors = move(errors)]() mutable {
+ on_complete(move(errors));
+ });
+}
+
+Vector<Utf16String> TestSpellChecker::guesses(DocumentTag, Utf16String const& word)
+{
+ return guesses_for(word.utf16_view());
+}
+
+void TestSpellChecker::ignore(DocumentTag tag, Utf16String const& word)
+{
+ m_accepted_words.ensure(tag).set(word);
+}
+
+void TestSpellChecker::learn(DocumentTag tag, Utf16String const& word)
+{
+ m_accepted_words.ensure(tag).set(word);
+}
+
+}
diff --git a/Libraries/LibWebView/TestSpellChecker.h b/Libraries/LibWebView/TestSpellChecker.h
new file mode 100644
index 00000000000..2ae05ec15d1
--- /dev/null
+++ b/Libraries/LibWebView/TestSpellChecker.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/HashTable.h>
+#include <LibWebView/SpellChecker.h>
+
+namespace WebView {
+
+// A spellchecker with a fixed word list, so tests give the same answers on every platform.
+class WEBVIEW_API TestSpellChecker final : public SpellChecker {
+public:
+ virtual DocumentTag create_document_tag() override;
+ virtual void close_document_tag(DocumentTag) override;
+ virtual void check(DocumentTag, Utf16String const& text, Function<void(Vector<Web::SpellingError>)> on_complete) override;
+ virtual Vector<Utf16String> guesses(DocumentTag, Utf16String const& word) override;
+ virtual void ignore(DocumentTag, Utf16String const& word) override;
+ virtual void learn(DocumentTag, Utf16String const& word) override;
+
+private:
+ bool is_misspelled(DocumentTag, Utf16View word) const;
+
+ DocumentTag m_next_document_tag { 1 };
+ // AD-HOC: A platform spellchecker learns a word for every app. Learned words stay with the tab's tag here, so
+ // views that run tests side by side never see each other's.
+ HashMap<DocumentTag, HashTable<Utf16String>> m_accepted_words;
+};
+
+}
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index b43690913c1..9d2b8beef80 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -99,6 +99,7 @@ ViewImplementation::~ViewImplementation()
if (!m_window_handle.is_empty())
Application::the().notify_webdriver_window_closed(m_window_handle);
+ reset_spell_checking();
all_views().remove(m_view_id);
if (m_top_level_traversable) {
@@ -1132,6 +1133,7 @@ void ViewImplementation::send_preferences_to_page(WebContentPage& page)
send_browsing_behavior(page);
send_autoplay_settings(page);
send_global_privacy_control(page);
+ page.async_set_spell_checking_available(Application::the().spell_checker() != nullptr);
send_geolocation_emulated_position(page);
}
@@ -4031,6 +4033,36 @@ void ViewImplementation::reject_pending_selection_requests()
reject_requests(m_pending_cut_selected_text_requests);
}
+SpellChecker::DocumentTag ViewImplementation::spell_document_tag()
+{
+ auto* spell_checker = Application::the().spell_checker();
+ VERIFY(spell_checker);
+ if (!m_spell_document_tag.has_value())
+ m_spell_document_tag = spell_checker->create_document_tag();
+ return *m_spell_document_tag;
+}
+
+void ViewImplementation::did_request_spell_check(Badge<WebContentPage>, WebContentPage& requesting_page, u64 request_id, Utf16String text)
+{
+ auto* spell_checker = Application::the().spell_checker();
+ if (!spell_checker) {
+ requesting_page.async_did_check_spelling(request_id, {});
+ return;
+ }
+ spell_checker->check(spell_document_tag(), text, [page = NonnullRefPtr { requesting_page }, request_id](Vector<Web::SpellingError> errors) {
+ if (page->is_open())
+ page->async_did_check_spelling(request_id, move(errors));
+ });
+}
+
+// Forgets what the user told the spellchecker to ignore or learn in this tab.
+void ViewImplementation::reset_spell_checking()
+{
+ if (auto* spell_checker = Application::the().spell_checker(); spell_checker && m_spell_document_tag.has_value())
+ spell_checker->close_document_tag(*m_spell_document_tag);
+ m_spell_document_tag.clear();
+}
+
void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target)
{
auto request_id = ++m_context_menu_request_id;
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 1040b19bb47..941116629a7 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -75,6 +75,7 @@
#include <LibWebView/SessionHistory.h>
#include <LibWebView/SessionStore.h>
#include <LibWebView/Settings.h>
+#include <LibWebView/SpellChecker.h>
#include <LibWebView/TabPerformanceStats.h>
#include <LibWebView/WebContentClient.h>
#include <LibWebView/WebContentPage.h>
@@ -529,6 +530,9 @@ public:
Menu& bookmark_folder_context_menu() { return *m_bookmark_folder_context_menu; }
void did_request_page_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target);
+
+ void did_request_spell_check(Badge<WebContentPage>, WebContentPage& requesting_page, u64 request_id, Utf16String text);
+ void reset_spell_checking();
void did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor);
void did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<Gfx::ShareableBitmap> bitmap);
void did_request_media_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::MediaContextMenu menu, Web::HTML::PreparedNavigationDescriptor);
@@ -701,6 +705,9 @@ protected:
RefPtr<Menu> m_media_context_menu;
u64 m_context_menu_request_id { 0 };
+ SpellChecker::DocumentTag spell_document_tag();
+ Optional<SpellChecker::DocumentTag> m_spell_document_tag;
+
RefPtr<Menu> m_bookmarks_bar_context_menu;
RefPtr<Menu> m_bookmark_context_menu;
RefPtr<Menu> m_bookmark_folder_context_menu;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index e0c32c66f56..b4313490431 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1865,6 +1865,11 @@ void WebContentPage::did_request_clipboard_entries(u64 request_id)
async_retrieved_clipboard_entries(request_id, items);
}
+void WebContentPage::did_request_spell_check(u64 request_id, Utf16String text)
+{
+ view().did_request_spell_check({}, *this, request_id, move(text));
+}
+
void WebContentPage::did_request_set_system_focus(bool has_system_focus)
{
traversable().set_has_system_focus(has_system_focus, *this);
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index 5d0d60a1559..29ff613a4d8 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -291,6 +291,7 @@ private:
virtual void did_change_storage_item(Web::StorageAPI::StorageEndpointType storage_endpoint, String url, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value) override;
virtual void did_update_indexed_database(String update) override;
virtual void did_request_clipboard_entries(u64 request_id) override;
+ virtual void did_request_spell_check(u64 request_id, Utf16String text) override;
virtual void did_request_set_system_focus(bool has_system_focus) override;
virtual void did_request_set_system_visibility_state(Web::HTML::VisibilityState visibility_state) override;
virtual void did_request_navigation_start(Web::HTML::CrossProcessId navigable_id, Web::NavigationTarget target, URL::URL url, Utf16String navigation_id, Optional<Web::HTML::NavigationStartRequest> start_request) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 7a69cc91273..64197bd34de 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3338,6 +3338,18 @@ void ConnectionFromClient::retrieved_clipboard_entries(Web::PageId page_id, u64
page->page().retrieved_clipboard_entries(request_id, move(items));
}
+void ConnectionFromClient::set_spell_checking_available(Web::PageId page_id, bool available)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().set_spell_checking_available(available);
+}
+
+void ConnectionFromClient::did_check_spelling(Web::PageId page_id, u64 request_id, Vector<Web::SpellingError> errors)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().did_check_spelling(request_id, move(errors));
+}
+
void ConnectionFromClient::did_delete_all_cookies(Web::PageId page_id, u64 request_id)
{
if (auto page = this->page(page_id); page.has_value())
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index e44a8737237..86ddbff240b 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -238,6 +238,7 @@ private:
virtual void set_preferred_languages(Web::PageId page_id, Vector<String>) override;
virtual void set_browsing_behavior(Web::PageId page_id, WebView::BrowsingBehavior) override;
virtual void set_enable_global_privacy_control(Web::PageId page_id, bool) override;
+ virtual void set_spell_checking_available(Web::PageId page_id, bool available) override;
virtual void set_geolocation_emulated_position(Web::PageId page_id, WebView::GeolocationPositionData, Optional<u16> error_code) override;
virtual void geolocation_position_response(Web::PageId page_id, u64 request_id, WebView::GeolocationPositionData, Optional<u16> error_code) override;
virtual void permission_request_completed(Web::PageId page_id, u64 request_id, bool granted) override;
@@ -270,6 +271,7 @@ private:
virtual void select_dropdown_closed(Web::PageId page_id, Optional<u32> selected_item_id) override;
virtual void retrieved_clipboard_entries(Web::PageId page_id, u64 request_id, Vector<Web::Clipboard::SystemClipboardItem>) override;
+ virtual void did_check_spelling(Web::PageId page_id, u64 request_id, Vector<Web::SpellingError> errors) override;
virtual void toggle_media_play_state(Web::PageId page_id) override;
virtual void toggle_media_mute_state(Web::PageId page_id) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index e3bc61ea6c3..6011a380710 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1869,6 +1869,11 @@ void PageClient::page_did_request_clipboard_entries(u64 request_id)
client().async_did_request_clipboard_entries(m_id, request_id);
}
+void PageClient::page_did_request_spell_check(u64 request_id, Utf16String const& text)
+{
+ client().async_did_request_spell_check(m_id, request_id, text);
+}
+
void PageClient::page_did_request_primary_paste()
{
client().async_did_request_primary_paste(m_id);
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 0187880d995..09a90422c9e 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -344,6 +344,7 @@ private:
virtual void page_did_change_background_color(Gfx::Color color) override;
virtual void page_did_insert_clipboard_item(Web::Clipboard::SystemClipboardItem const&, StringView presentation_style) override;
virtual void page_did_request_clipboard_entries(u64 request_id) override;
+ virtual void page_did_request_spell_check(u64 request_id, Utf16String const& text) override;
virtual void page_did_request_primary_paste() override;
virtual void page_did_complete_paste_action() override;
virtual void page_did_update_primary_selection(Utf16String const&) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index cdbdc55ffa0..65ea2d7d729 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -58,6 +58,7 @@
#include <LibWebCommon/Page/PageId.h>
#include <LibWebCommon/Page/PendingDialog.h>
#include <LibWebCommon/Page/ScreenWakeLockState.h>
+#include <LibWebCommon/Page/SpellingError.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWebCommon/StorageAPI/StorageEndpoint.h>
#include <LibWebCommon/WebDriver/Response.h>
@@ -232,6 +233,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_insert_clipboard_item(Web::PageId page_id, Web::Clipboard::SystemClipboardItem item, String presentation_style) =|
did_request_clipboard_entries(Web::PageId page_id, u64 request_id) =|
+ did_request_spell_check(Web::PageId page_id, u64 request_id, Utf16String text) =|
did_request_primary_paste(Web::PageId page_id) =|
did_update_primary_selection(Web::PageId page_id, String text) =|
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index ac355d0be84..02ce5d49df9 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -46,6 +46,7 @@
#include <LibWebCommon/Page/NavigationTarget.h>
#include <LibWebCommon/Page/PageId.h>
#include <LibWebCommon/Page/PendingDialog.h>
+#include <LibWebCommon/Page/SpellingError.h>
#include <LibWebCommon/Page/ViewportIsFullscreen.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWebCommon/StorageAPI/StorageEndpoint.h>
@@ -244,6 +245,7 @@ endpoint WebContentServer routed by Web::PageId page_id
set_preferred_languages(Web::PageId page_id, Vector<String> preferred_languages) =|
set_browsing_behavior(Web::PageId page_id, WebView::BrowsingBehavior browsing_behavior) =|
set_enable_global_privacy_control(Web::PageId page_id, bool enable) =|
+ set_spell_checking_available(Web::PageId page_id, bool available) =|
set_geolocation_emulated_position(Web::PageId page_id, WebView::GeolocationPositionData position, Optional<u16> error_code) =|
geolocation_position_response(Web::PageId page_id, u64 request_id, WebView::GeolocationPositionData position, Optional<u16> error_code) =|
permission_request_completed(Web::PageId page_id, u64 request_id, bool granted) =|
@@ -275,6 +277,7 @@ endpoint WebContentServer routed by Web::PageId page_id
select_dropdown_closed(Web::PageId page_id, Optional<u32> selected_item_id) =|
retrieved_clipboard_entries(Web::PageId page_id, u64 request_id, Vector<Web::Clipboard::SystemClipboardItem> items) =|
+ did_check_spelling(Web::PageId page_id, u64 request_id, Vector<Web::SpellingError> errors) =|
toggle_media_play_state(Web::PageId page_id) =|
toggle_media_mute_state(Web::PageId page_id) =|
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-request-round-trip.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-request-round-trip.txt
new file mode 100644
index 00000000000..d0621df8034
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-request-round-trip.txt
@@ -0,0 +1,5 @@
+"nothing wrong here": ""
+"one txet two": "4-8"
+"Txet and quikc": "0-4 9-14"
+"😀 txet": "3-7"
+"txet's": ""
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-request-round-trip.html b/Tests/LibWeb/Text/input/Editing/spellcheck-request-round-trip.html
new file mode 100644
index 00000000000..25cad7fce08
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-request-round-trip.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ for (const text of ["nothing wrong here", "one txet two", "Txet and quikc", "\u{1F600} txet", "txet's"])
+ println(`${JSON.stringify(text)}: "${await internals.checkSpellingForTesting(text)}"`);
+ });
+</script>
diff --git a/Tests/LibWeb/test-web/Application.cpp b/Tests/LibWeb/test-web/Application.cpp
index 3c1b37cb41d..8960bfcf5f3 100644
--- a/Tests/LibWeb/test-web/Application.cpp
+++ b/Tests/LibWeb/test-web/Application.cpp
@@ -13,6 +13,7 @@
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
+#include <LibWebView/TestSpellChecker.h>
namespace TestWeb {
@@ -30,6 +31,8 @@ Application::Application(Optional<ByteString> ladybird_binary_path)
if (Core::Environment::has("CLAUDECODE"sv) || Core::Environment::has("CODEX_SANDBOX"sv))
quiet = true;
+
+ set_spell_checker(make<WebView::TestSpellChecker>());
}
Application::~Application()
diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cpp
index 60135e031a4..09cb61093be 100644
--- a/Tests/LibWeb/test-web/main.cpp
+++ b/Tests/LibWeb/test-web/main.cpp
@@ -1250,6 +1250,9 @@ static ErrorOr<int> run_tests(Core::AnonymousBuffer const& theme, Web::DevicePix
view->disconnect_child_crash_handlers();
view->close_child_web_views();
+ // A test's ignored and learned words live in this process, so they outlive a crashed WebContent.
+ view->reset_spell_checking();
+
// Don't try to reset state if WebContent crashed - it's gone
if (test_result != TestResult::Crashed) {
view->clear_content_blockers();