Skip to content

Truly independent web browser

LibWeb: Spellcheck only on the user’s behalf

sideshowbarker620aeb7f3cd4
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.cpp b/Libraries/LibWeb/Editing/SpellCheckController.cpp
index 0c6c6b1de03..08455a1bf86 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.cpp
+++ b/Libraries/LibWeb/Editing/SpellCheckController.cpp
@@ -16,6 +16,7 @@
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
+#include <LibWeb/HTML/Window.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Selection/Selection.h>
@@ -69,6 +70,14 @@ static bool text_is_checkable(DOM::Text& text)
return parent && parent->is_editable_or_editing_host() && parent->spellcheck();
}
+// 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)
+{
+ auto window = document.window();
+ return window && window->has_transient_activation();
+}
+
static bool is_separator(char16_t code_unit)
{
return code_unit == ' ' || code_unit == '\t' || code_unit == '\n' || code_unit == '\r' || code_unit == 0xa0;
@@ -168,7 +177,7 @@ void SpellCheckController::send_window_around(DOM::Text& text, size_t offset, Re
// 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))
+ if (!m_document->page().spell_checking_available() || !has_user_activation(m_document) || !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())
@@ -176,6 +185,7 @@ void SpellCheckController::did_user_edit(DOM::Node& node)
schedule_typing_check();
}
+// NB: Only the platform's input method commits a composition, so the user is behind it, with or without activation.
void SpellCheckController::did_end_input_method_composition()
{
if (m_document->page().spell_checking_available())
@@ -515,7 +525,7 @@ void SpellCheckController::did_focus(GC::Ptr<DOM::Node> node)
m_exempt_word_check_scheduled = false;
m_existing_text_pass.clear();
- if (!node || !m_document->page().spell_checking_available())
+ if (!node || !m_document->page().spell_checking_available() || !has_user_activation(m_document))
return;
auto editable = checkable_editable_for(*node);
if (!editable)
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-needs-user-activation.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-needs-user-activation.txt
new file mode 100644
index 00000000000..9f535425424
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-needs-user-activation.txt
@@ -0,0 +1,2 @@
+focused by script: (none)
+focused by a click: 4-8
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-existing-text-on-focus.html b/Tests/LibWeb/Text/input/Editing/spellcheck-existing-text-on-focus.html
index 3fcfce3d439..304eb581ee4 100644
--- a/Tests/LibWeb/Text/input/Editing/spellcheck-existing-text-on-focus.html
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-existing-text-on-focus.html
@@ -12,10 +12,12 @@
<textarea id="long"></textarea>
<script>
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);
document.getElementById("long").value = "word ".repeat(4000) + "txet";
const ids = ["area", "text", "password", "readonly", "disabled", "no-spellcheck", "astral", "long"];
for (const id of [...ids, "editable"]) {
- document.getElementById(id).focus();
+ clickOn(document.getElementById(id));
await internals.spellCheckSettled();
}
for (const id of ids)
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-needs-user-activation.html b/Tests/LibWeb/Text/input/Editing/spellcheck-needs-user-activation.html
new file mode 100644
index 00000000000..ad6ac79c60c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-needs-user-activation.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="scripted">one txet two</textarea>
+<textarea id="clicked">one txet two</textarea>
+<script>
+ promiseTest(async () => {
+ // A page can't start a check at will, so it can't test words against the user's dictionary: Focus from script
+ // checks nothing.
+ const scripted = document.getElementById("scripted");
+ scripted.focus();
+ await internals.spellCheckSettled();
+ println(`focused by script: ${internals.spellingErrorRanges(scripted) || "(none)"}`);
+
+ // Focus the user gives a field checks its existing text.
+ const clicked = document.getElementById("clicked");
+ const rect = clicked.getBoundingClientRect();
+ internals.click(rect.left + rect.width / 2, rect.top + rect.height / 2);
+ await internals.spellCheckSettled();
+ println(`focused by a click: ${internals.spellingErrorRanges(clicked) || "(none)"}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-stale-and-removed.html b/Tests/LibWeb/Text/input/Editing/spellcheck-stale-and-removed.html
index a248cda8d72..b5b954873b4 100644
--- a/Tests/LibWeb/Text/input/Editing/spellcheck-stale-and-removed.html
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-stale-and-removed.html
@@ -5,27 +5,31 @@
<iframe id="frame" srcdoc="<div contenteditable>txet</div>"></iframe>
<script>
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 frame = document.getElementById("frame");
if (!frame.contentDocument?.querySelector("div"))
await new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));
// A reply that comes back after the text changed is dropped.
const edited = document.getElementById("edited");
- edited.focus();
+ clickOn(edited);
edited.value = "one text two";
await internals.spellCheckSettled();
println(`edited before the reply: ${internals.spellingErrorRanges(edited) || "(none)"}`);
// An editable removed while its check is in flight gets no markers, and nothing crashes.
const removed = document.getElementById("removed");
- removed.focus();
+ clickOn(removed);
removed.remove();
await internals.spellCheckSettled();
println(`removed during the check: ${internals.spellingErrorRanges(removed) || "(none)"}`);
// A contenteditable in a same-origin iframe is checked by its own document.
const frameEditable = frame.contentDocument.querySelector("div");
- frameEditable.focus();
+ const frameRect = frame.getBoundingClientRect();
+ const editableRect = frameEditable.getBoundingClientRect();
+ clickOn(frame, new DOMRect(frameRect.left + frame.clientLeft + editableRect.left, frameRect.top + frame.clientTop + editableRect.top, editableRect.width, editableRect.height));
await internals.spellCheckSettled();
println(`iframe: ${internals.spellingErrorRanges(frameEditable.firstChild) || "(none)"}`);
});
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
index 372f132cf84..e2e290fcd3e 100644
--- a/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
@@ -8,6 +8,8 @@
<div id="later" contenteditable spellcheck="false">txet</div>
<script>
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 node = id => {
const element = document.getElementById(id);
@@ -18,7 +20,7 @@
println(`${id} ${label}: ${internals.spellingErrorRanges(node(id)) || "(none)"}`);
};
for (const id of ids) {
- document.getElementById(id).focus();
+ clickOn(document.getElementById(id));
await internals.spellCheckSettled();
}
show("before");