Skip to content

Truly independent web browser

LibWeb: Spellcheck an input only while it has focus

Andreas Klinge912427ba047
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.cpp b/Libraries/LibWeb/Editing/SpellCheckController.cpp
index fbbb2187116..7a8fb8f7654 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.cpp
+++ b/Libraries/LibWeb/Editing/SpellCheckController.cpp
@@ -75,6 +75,19 @@ static bool text_is_checkable(DOM::Text& text)
return parent && parent->is_editable_or_editing_host() && parent->spellcheck();
}
+// INTEROP: The value of an input is checked only while the input has focus, and its markers come off when focus leaves
+// it. A one-line field mostly holds names, addresses and such, so its errors only show while the user types.
+static bool is_checked_now(DOM::Text& text)
+{
+ if (!text_is_checkable(text))
+ return false;
+ auto editable = checkable_editable_for(text);
+ if (!is<HTML::HTMLInputElement>(*editable))
+ return true;
+ auto focused_area = text.document().focused_area();
+ return focused_area && checkable_editable_for(*focused_area) == editable;
+}
+
// A check only ever runs on the user's behalf. A check a page could start at will would let it test any words it likes
// against the user's dictionary.
static bool has_user_activation(DOM::Document& document)
@@ -485,7 +498,7 @@ void SpellCheckController::apply(Request const& request, Vector<SpellingError> c
auto text = piece.text.ptr();
// Text that left the document, or whose checking turned off, since the text was sent takes nothing from the
// answer.
- if (!text || !piece.checked || !owns(*text) || !text_is_checkable(*text))
+ if (!text || !piece.checked || !owns(*text) || !is_checked_now(*text))
continue;
// A node edited since the text was sent is dirty again, and the next check takes it.
auto data = text->data().utf16_view();
@@ -511,12 +524,22 @@ void SpellCheckController::apply(Request const& request, Vector<SpellingError> c
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.
+ // checked now, unless it is in an input that focus left. 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())
+ if (auto text = exempt->text.ptr(); text && owns(*text) && is_checked_now(*text) && m_document->page().spell_checking_available())
send_window_around(*text, min<size_t>(exempt->start, text->length_in_utf16_code_units()), RequestKind::Other);
}
+
+ // An input that focus left takes its markers with it.
+ GC::RootVector<GC::Ref<DOM::Text>> unfocused;
+ m_document->spelling_markers().for_each_marked_text([&](DOM::Text& text, auto) {
+ if (auto editable = checkable_editable_for(text); editable && is<HTML::HTMLInputElement>(*editable) && !is_checked_now(text))
+ unfocused.append(text);
+ });
+ for (auto& text : unfocused)
+ m_document->spelling_markers().remove_all(text);
+
m_typing_check_scheduled = false;
m_typing_check_wanted = false;
m_exempt_word_check_scheduled = false;
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-existing-text-on-focus.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-existing-text-on-focus.txt
index af07807c75b..8441f039349 100644
--- a/Tests/LibWeb/Text/expected/Editing/spellcheck-existing-text-on-focus.txt
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-existing-text-on-focus.txt
@@ -1,5 +1,5 @@
area: 4-8
-text: 0-5
+text: (none)
password: (none)
readonly: (none)
disabled: (none)
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-input-only-while-focused.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-input-only-while-focused.txt
index 4b963d0ad85..1e15fd8706e 100644
--- a/Tests/LibWeb/Text/expected/Editing/spellcheck-input-only-while-focused.txt
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-input-only-while-focused.txt
@@ -1,5 +1,5 @@
input while focused: 0-5
-input after focus left: 0-5
+input after focus left: (none)
textarea while focused: 0-5
-input left while its check was out: 0-5
+input left while its check was out: (none)
textarea after focus left: 0-5
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt
index 6430b933b3e..0a24c0597c6 100644
--- a/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt
@@ -1,12 +1,12 @@
spell before: 0-4
readonly before: 0-4
disabled before: 0-4
-type before: 0-4
editable before: 0-4
later before: (none)
spell after: (none)
readonly after: (none)
disabled after: (none)
-type after: (none)
editable after: (none)
later after: (none)
+type before: 0-4
+type after: (none)
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
index e2e290fcd3e..cc9d4ddc10d 100644
--- a/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
@@ -10,7 +10,7 @@
promiseTest(async () => {
// Checks run only on the user's behalf, so the user focuses each field with a click.
const clickOn = (element, rect = element.getBoundingClientRect()) => internals.click(rect.left + rect.width / 2, rect.top + rect.height / 2);
- const ids = ["spell", "readonly", "disabled", "type", "editable", "later"];
+ const ids = ["spell", "readonly", "disabled", "editable", "later"];
const node = id => {
const element = document.getElementById(id);
return element.localName === "div" ? element.firstChild : element;
@@ -28,7 +28,6 @@
document.getElementById("spell").spellcheck = false;
document.getElementById("readonly").readOnly = true;
document.getElementById("disabled").disabled = true;
- document.getElementById("type").type = "password";
document.getElementById("editable").contentEditable = "false";
// Script turning checking on starts no check, so a page can't test words against the user's dictionary at will.
const later = document.getElementById("later");
@@ -36,5 +35,14 @@
later.spellcheck = true;
await internals.spellCheckSettled();
show("after");
+
+ // An input shows its errors only while it has focus, so its checking turns off while it has focus.
+ const type = document.getElementById("type");
+ clickOn(type);
+ await internals.spellCheckSettled();
+ println(`type before: ${internals.spellingErrorRanges(type) || "(none)"}`);
+ type.type = "password";
+ await internals.spellCheckSettled();
+ println(`type after: ${internals.spellingErrorRanges(type) || "(none)"}`);
});
</script>