Skip to content

Truly independent web browser

LibWeb: Spellcheck while the user types

sideshowbarkerb3f8d60d40a5
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index aad43e3c6f3..66bc40fedfc 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1071,6 +1071,8 @@ void Document::note_selection_changed()
{
m_selection_states_are_stale = true;
request_frame_for_pending_repaint();
+ if (m_spell_check_controller)
+ m_spell_check_controller->did_change_selection();
}
void Document::update_highlight_states_if_needed(Layout::BegunRead const& read)
diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp
index 57f5c055490..6b5bb259bb9 100644
--- a/Libraries/LibWeb/Editing/ExecCommand.cpp
+++ b/Libraries/LibWeb/Editing/ExecCommand.cpp
@@ -15,6 +15,7 @@
#include <LibWeb/Editing/Commands.h>
#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
+#include <LibWeb/Editing/SpellCheckController.h>
#include <LibWeb/Editing/VisiblePosition.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLLIElement.h>
@@ -248,6 +249,10 @@ WebIDL::ExceptionOr<bool> Document::exec_command_internal(Utf16FlyString const&
if (!command_result)
return false;
+ // A command the user ran by editing, rather than one a script called, is a user edit for spellchecking.
+ if (user_input_type.has_value() && tree_was_modified && affected_editing_host)
+ spell_check_controller().did_user_edit(*affected_editing_host);
+
// 7. If the action modified DOM tree, then fire an event named "input" at affected editing host using InputEvent,
// with its isTrusted and bubbles attributes initialized to true, inputType attribute initialized to the mapped
// value of command, and its data attribute initialized to null.
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.cpp b/Libraries/LibWeb/Editing/SpellCheckController.cpp
index f1335205155..e2139ecb848 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.cpp
+++ b/Libraries/LibWeb/Editing/SpellCheckController.cpp
@@ -5,16 +5,19 @@
*/
#include <AK/Utf16StringBuilder.h>
+#include <LibUnicode/Segmenter.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
#include <LibWeb/Editing/SpellCheckController.h>
#include <LibWeb/Editing/SpellingMarkers.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/Page/Page.h>
+#include <LibWeb/Selection/Selection.h>
namespace Web::Editing {
@@ -71,6 +74,22 @@ static bool is_separator(char16_t code_unit)
return code_unit == ' ' || code_unit == '\t' || code_unit == '\n' || code_unit == '\r' || code_unit == 0xa0;
}
+// The word-like segment containing offset, or ending or starting at it.
+static Optional<SpellCheckController::WordRange> word_touching(DOM::Text& text, size_t offset)
+{
+ auto& segmenter = text.word_segmenter();
+ for (auto start : { segmenter.previous_boundary(offset, Unicode::Segmenter::Inclusive::Yes), segmenter.previous_boundary(offset) }) {
+ if (!start.has_value())
+ continue;
+ auto end = segmenter.next_boundary(*start);
+ if (!end.has_value() || *end < offset)
+ continue;
+ if (segmenter.is_current_boundary_word_like())
+ return SpellCheckController::WordRange { text, *start, *end };
+ }
+ return {};
+}
+
// The first node in tree order that comes after node and all of its descendants.
static GC::Ptr<DOM::Node> next_after_subtree(DOM::Node& node)
{
@@ -107,6 +126,136 @@ bool SpellCheckController::owns(DOM::Text const& text) const
return text.is_connected() && &text.document() == m_document.ptr();
}
+Optional<SpellCheckController::TextPosition> SpellCheckController::caret_position() const
+{
+ auto focused = m_document->focused_area();
+ if (auto* text_control = as_if<HTML::FormAssociatedTextControlElement>(focused.ptr())) {
+ auto text = text_control->form_associated_element_to_text_node();
+ if (!text)
+ return {};
+ return TextPosition { *text, min<size_t>(text_control->selection_end(), text->length_in_utf16_code_units()) };
+ }
+ auto selection = m_document->get_selection();
+ if (!selection)
+ return {};
+ auto* text = as_if<DOM::Text>(selection->focus_node().ptr());
+ if (!text)
+ return {};
+ return TextPosition { *text, selection->focus_offset() };
+}
+
+void SpellCheckController::send_window_around(DOM::Text& text, size_t offset, RequestKind kind)
+{
+ if (!owns(text))
+ return;
+ auto paragraph = paragraph_containing(text, offset);
+ size_t paragraph_offset = 0;
+ Optional<size_t> caret;
+ for (auto const& piece : paragraph) {
+ if (piece.text.ptr() == &text && piece.node_start <= offset && offset <= piece.node_end) {
+ caret = paragraph_offset + (offset - piece.node_start);
+ break;
+ }
+ paragraph_offset += piece.node_end - piece.node_start;
+ }
+ if (!caret.has_value())
+ return;
+ auto half = TYPING_WINDOW_SIZE / 2;
+ send(make_request(paragraph, *caret > half ? *caret - half : 0, *caret + half, kind));
+}
+
+// Checking while typing: Every user edit queues one check of the paragraph around the caret per turn of the event
+// loop, and while one is in flight, the next waits for its answer.
+void SpellCheckController::did_user_edit(DOM::Node& node)
+{
+ if (!m_document->page().spell_checking_available() || !checkable_editable_for(node))
+ return;
+ // Committing a composition ends it, and did_end_input_method_composition() takes the check from there.
+ if (m_document->is_input_method_composing())
+ return;
+ schedule_typing_check();
+}
+
+void SpellCheckController::did_end_input_method_composition()
+{
+ if (m_document->page().spell_checking_available())
+ schedule_typing_check();
+}
+
+void SpellCheckController::schedule_typing_check()
+{
+ if (m_typing_check_scheduled)
+ return;
+ m_typing_check_scheduled = true;
+ HTML::queue_global_task(HTML::Task::Source::UserInteraction, HTML::relevant_global_object(m_document), GC::create_function(m_document->heap(), [document = GC::Weak<DOM::Document> { m_document }] {
+ if (auto strong_document = document.ptr()) {
+ strong_document->spell_check_controller().run_typing_check();
+ strong_document->page().notify_spell_check_progress();
+ }
+ }));
+}
+
+void SpellCheckController::run_typing_check()
+{
+ if (!exchange(m_typing_check_scheduled, false))
+ return;
+ if (m_typing_check_in_flight) {
+ m_typing_check_wanted = true;
+ return;
+ }
+ auto caret = caret_position();
+ if (!caret.has_value() || !owns(*caret->text) || !checkable_editable_for(*caret->text))
+ return;
+ // Right after an edit, the word touching the caret is still being typed. Its markers come off, and no answer flags
+ // it until the caret leaves it.
+ m_exempt_word = word_touching(*caret->text, caret->offset);
+ if (m_exempt_word.has_value())
+ m_document->spelling_markers().remove_overlapping(*caret->text, m_exempt_word->start, m_exempt_word->end);
+ send_window_around(*caret->text, caret->offset, RequestKind::Typing);
+}
+
+void SpellCheckController::did_change_selection()
+{
+ if (m_exempt_word.has_value())
+ schedule_exempt_word_check();
+}
+
+void SpellCheckController::schedule_exempt_word_check()
+{
+ if (m_exempt_word_check_scheduled)
+ return;
+ m_exempt_word_check_scheduled = true;
+ HTML::queue_global_task(HTML::Task::Source::UserInteraction, HTML::relevant_global_object(m_document), GC::create_function(m_document->heap(), [document = GC::Weak<DOM::Document> { m_document }] {
+ if (auto strong_document = document.ptr()) {
+ strong_document->spell_check_controller().run_exempt_word_check();
+ strong_document->page().notify_spell_check_progress();
+ }
+ }));
+}
+
+void SpellCheckController::run_exempt_word_check()
+{
+ if (!exchange(m_exempt_word_check_scheduled, false))
+ return;
+ if (!m_exempt_word.has_value())
+ return;
+ auto exempt = *m_exempt_word;
+ auto text = exempt.text.ptr();
+ if (!text || !owns(*text)) {
+ m_exempt_word.clear();
+ return;
+ }
+ // A caret still inside the same word, which typing may have lengthened, keeps it exempt.
+ if (auto caret = caret_position(); caret.has_value() && caret->text.ptr() == text.ptr()) {
+ if (auto word = word_touching(*text, caret->offset); word.has_value() && word->start <= exempt.start && exempt.start <= word->end) {
+ m_exempt_word = word;
+ return;
+ }
+ }
+ m_exempt_word.clear();
+ send_window_around(*text, min<size_t>(exempt.start, text->length_in_utf16_code_units()), RequestKind::Other);
+}
+
SpellCheckController::Paragraph SpellCheckController::paragraph_containing(DOM::Text& text, size_t offset) const
{
Paragraph paragraph;
@@ -160,7 +309,7 @@ Vector<GC::Weak<DOM::Text>> SpellCheckController::paragraphs_of(DOM::Node& edita
// Builds the request for a window of a paragraph's text, widened to whitespace so no word is cut. Text whose checking
// is disabled goes as spaces of the same length, so offsets stay put and the words on either side don't merge.
-SpellCheckController::Request SpellCheckController::make_request(Paragraph const& paragraph, size_t window_start, size_t window_end) const
+SpellCheckController::Request SpellCheckController::make_request(Paragraph const& paragraph, size_t window_start, size_t window_end, RequestKind kind) const
{
size_t length = 0;
for (auto const& piece : paragraph)
@@ -182,6 +331,7 @@ SpellCheckController::Request SpellCheckController::make_request(Paragraph const
++window_end;
Request request;
+ request.kind = kind;
request.window_end = window_end;
Utf16StringBuilder text;
size_t text_length = 0;
@@ -218,6 +368,8 @@ void SpellCheckController::send(Request request)
{
auto text = request.text;
++m_requests_in_flight;
+ if (request.kind == RequestKind::Typing)
+ m_typing_check_in_flight = true;
m_document->page().request_spell_check(text, GC::create_function(m_document->heap(), [document = GC::Weak<DOM::Document> { m_document }, request = move(request)](Vector<SpellingError> errors) {
auto strong_document = document.ptr();
if (!strong_document)
@@ -231,8 +383,19 @@ void SpellCheckController::did_receive_answer(Request const& request, Vector<Spe
{
--m_requests_in_flight;
apply(request, errors);
- m_existing_text_chunk_in_flight = false;
- send_next_existing_text_chunk();
+ switch (request.kind) {
+ case RequestKind::ExistingText:
+ m_existing_text_chunk_in_flight = false;
+ send_next_existing_text_chunk();
+ break;
+ case RequestKind::Typing:
+ m_typing_check_in_flight = false;
+ if (exchange(m_typing_check_wanted, false))
+ schedule_typing_check();
+ break;
+ case RequestKind::Other:
+ break;
+ }
}
// Checking existing text goes one chunk at a time: The next chunk goes out once the previous one's answer is in.
@@ -248,6 +411,7 @@ void SpellCheckController::send_next_existing_text_chunk()
};
Request chunk;
+ chunk.kind = RequestKind::ExistingText;
Utf16StringBuilder chunk_text;
size_t chunk_length = 0;
while (pass.next_paragraph < pass.paragraphs.size() && chunk_length < EXISTING_TEXT_CHUNK_SIZE) {
@@ -268,7 +432,7 @@ void SpellCheckController::send_next_existing_text_chunk()
auto room = EXISTING_TEXT_CHUNK_SIZE - chunk_length;
if (chunk_length > 0 && length - pass.next_offset > room)
break;
- auto request = make_request(paragraph, pass.next_offset, pass.next_offset + room);
+ auto request = make_request(paragraph, pass.next_offset, pass.next_offset + room, RequestKind::ExistingText);
if (request.window_end <= pass.next_offset) {
next_paragraph();
continue;
@@ -339,7 +503,18 @@ void SpellCheckController::apply(Request const& request, Vector<SpellingError> c
// Checking existing text: When a checkable editable gains focus, its whole text is checked, one chunk at a time.
void SpellCheckController::did_focus(GC::Ptr<DOM::Node> node)
{
+ // Focus moving ends the typing that came before. The word the user was typing is no longer being typed, so it gets
+ // checked now. And a typing check queued for an earlier edit no longer runs, since it would read a caret that edit
+ // didn't put there.
+ if (auto exempt = exchange(m_exempt_word, {}); exempt.has_value()) {
+ if (auto text = exempt->text.ptr(); text && owns(*text) && text_is_checkable(*text) && m_document->page().spell_checking_available())
+ send_window_around(*text, min<size_t>(exempt->start, text->length_in_utf16_code_units()), RequestKind::Other);
+ }
+ m_typing_check_scheduled = false;
+ m_typing_check_wanted = false;
+ m_exempt_word_check_scheduled = false;
m_existing_text_pass.clear();
+
if (!node || !m_document->page().spell_checking_available())
return;
auto editable = checkable_editable_for(*node);
@@ -351,7 +526,8 @@ void SpellCheckController::did_focus(GC::Ptr<DOM::Node> node)
bool SpellCheckController::is_settled() const
{
- return !m_existing_text_pass.has_value() && m_requests_in_flight == 0;
+ return !m_existing_text_pass.has_value() && m_requests_in_flight == 0 && !m_typing_check_scheduled
+ && !m_typing_check_wanted && !m_exempt_word_check_scheduled;
}
}
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.h b/Libraries/LibWeb/Editing/SpellCheckController.h
index 672cfb6eeca..ba7dee814ff 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.h
+++ b/Libraries/LibWeb/Editing/SpellCheckController.h
@@ -38,6 +38,10 @@ public:
explicit SpellCheckController(DOM::Document&);
void did_focus(GC::Ptr<DOM::Node>);
+ // A user edit changed the text of the editable that contains node.
+ void did_user_edit(DOM::Node&);
+ void did_end_input_method_composition();
+ void did_change_selection();
bool is_settled() const;
@@ -63,7 +67,14 @@ private:
bool checked { false };
};
+ enum class RequestKind : u8 {
+ ExistingText,
+ Typing,
+ Other,
+ };
+
struct Request {
+ RequestKind kind { RequestKind::Other };
Utf16String text;
Vector<RequestPiece> pieces;
// Where the window ended in the paragraph's text.
@@ -80,10 +91,22 @@ private:
size_t next_offset { 0 };
};
+ struct TextPosition {
+ GC::Ref<DOM::Text> text;
+ size_t offset { 0 };
+ };
+
bool owns(DOM::Text const&) const;
+ Optional<TextPosition> caret_position() const;
+ void send_window_around(DOM::Text&, size_t offset, RequestKind);
+ void schedule_typing_check();
+ void run_typing_check();
+ void schedule_exempt_word_check();
+ void run_exempt_word_check();
+
Paragraph paragraph_containing(DOM::Text&, size_t offset) const;
Vector<GC::Weak<DOM::Text>> paragraphs_of(DOM::Node& editable) const;
- Request make_request(Paragraph const&, size_t window_start, size_t window_end) const;
+ Request make_request(Paragraph const&, size_t window_start, size_t window_end, RequestKind) const;
void send(Request);
void send_next_existing_text_chunk();
void did_receive_answer(Request const&, Vector<SpellingError> const&);
@@ -96,6 +119,11 @@ private:
size_t m_requests_in_flight { 0 };
// The word the user is still typing, which no answer flags until the caret leaves it.
Optional<WordRange> m_exempt_word;
+ bool m_typing_check_scheduled { false };
+ bool m_typing_check_in_flight { false };
+ // A typing check wanted while another was in flight, which goes out once that one's answer is in.
+ bool m_typing_check_wanted { false };
+ bool m_exempt_word_check_scheduled { false };
};
}
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index ae1af89d34e..08c68fc43ea 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -22,6 +22,7 @@
#include <LibWeb/DOM/SubtreeInsertionScope.h>
#include <LibWeb/Editing/EditCommand.h>
#include <LibWeb/Editing/EditingHistory.h>
+#include <LibWeb/Editing/SpellCheckController.h>
#include <LibWeb/HTML/CustomElements/CustomElementReactionNames.h>
#include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
@@ -1310,6 +1311,7 @@ void FormAssociatedTextControlElement::handle_insert(Utf16FlyString const& input
data_for_input_event = Utf16String::from_utf16(data_for_insertion);
did_edit_text_node(input_type, data_for_input_event);
+ html_element.document().spell_check_controller().did_user_edit(html_element);
scroll_cursor_into_view();
}
@@ -1360,6 +1362,7 @@ void FormAssociatedTextControlElement::handle_delete(Utf16FlyString const& input
history->end_recording();
did_edit_text_node(input_type, {});
+ html_element.document().spell_check_controller().did_user_edit(html_element);
scroll_cursor_into_view();
}
@@ -1421,6 +1424,9 @@ void FormAssociatedTextControlElement::selection_was_changed(SelectionSource sou
VERIFY_NOT_REACHED();
}
+ if (auto* controller = element.document().spell_check_controller_if_exists())
+ controller->did_change_selection();
+
// NB: Called during selection change handling, layout may be stale. The render state finds the text's box as it
// applies the repaint, so the change reads nothing of it.
auto text_node = form_associated_element_to_text_node();
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index c97cba0d665..813427afb95 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -40,6 +40,7 @@
#include <LibWeb/Editing/ClipboardSerializer.h>
#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
+#include <LibWeb/Editing/SpellCheckController.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
@@ -6304,6 +6305,9 @@ void LocalNavigable::end_input_method_composition(Optional<Utf16View> final_text
if (!was_active)
return;
+ if (auto document = active_document())
+ document->spell_check_controller().did_end_input_method_composition();
+
// https://w3c.github.io/uievents/#event-type-compositionend
// A user agent MUST dispatch this event when a text composition system completes or cancels the current composition
// session, and the compositionend event MUST be dispatched after the control is updated.
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-last-word-on-focus-change.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-last-word-on-focus-change.txt
new file mode 100644
index 00000000000..2c28d667403
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-last-word-on-focus-change.txt
@@ -0,0 +1,2 @@
+while typing: (none)
+after focus moved on: 0-4
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-while-typing.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-while-typing.txt
new file mode 100644
index 00000000000..4774a9f9040
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-while-typing.txt
@@ -0,0 +1,9 @@
+typing the word: (none)
+after a space: 0-4
+half-typed second word: 0-4
+still typing it: 0-4
+caret moved away: 0-4 9-13
+typing inside a flagged word: 10-14
+during a composition: (none)
+after committing it: 0-4
+contenteditable: 0-7
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-last-word-on-focus-change.html b/Tests/LibWeb/Text/input/Editing/spellcheck-last-word-on-focus-change.html
new file mode 100644
index 00000000000..834e303fe28
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-last-word-on-focus-change.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="typed"></textarea>
+<textarea id="next"></textarea>
+<script>
+ promiseTest(async () => {
+ const typed = document.getElementById("typed");
+ internals.sendText(typed, "txet");
+ await internals.spellCheckSettled();
+ println(`while typing: ${internals.spellingErrorRanges(typed) || "(none)"}`);
+
+ // The word the user was typing gets checked once the user moves focus away from it.
+ const next = document.getElementById("next");
+ const rect = next.getBoundingClientRect();
+ internals.click(rect.left + rect.width / 2, rect.top + rect.height / 2);
+ await internals.spellCheckSettled();
+ println(`after focus moved on: ${internals.spellingErrorRanges(typed) || "(none)"}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-while-typing.html b/Tests/LibWeb/Text/input/Editing/spellcheck-while-typing.html
new file mode 100644
index 00000000000..483cd7c0b87
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-while-typing.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="area"></textarea>
+<textarea id="composed"></textarea>
+<div id="editable" contenteditable></div>
+<script>
+ promiseTest(async () => {
+ const area = document.getElementById("area");
+ const show = async (label, node) => {
+ await internals.spellCheckSettled();
+ println(`${label}: ${internals.spellingErrorRanges(node) || "(none)"}`);
+ };
+
+ internals.sendText(area, "txet");
+ await show("typing the word", area);
+ internals.sendText(area, " ");
+ await show("after a space", area);
+ internals.sendText(area, "fox txe");
+ await show("half-typed second word", area);
+ internals.sendText(area, "t");
+ await show("still typing it", area);
+ area.setSelectionRange(0, 0);
+ await show("caret moved away", area);
+ area.setSelectionRange(2, 2);
+ internals.sendText(area, "x");
+ await show("typing inside a flagged word", area);
+
+ const composed = document.getElementById("composed");
+ composed.focus();
+ internals.setMarkedTextFromInputMethod("txet");
+ await show("during a composition", composed);
+ internals.commitTextFromInputMethod("txet ");
+ await show("after committing it", composed);
+
+ const editable = document.getElementById("editable");
+ internals.sendText(editable, "speling ");
+ await show("contenteditable", editable.firstChild);
+ });
+</script>