Skip to content

Truly independent web browser

LibWeb: Drop spelling markers where checking turns off

sideshowbarker0b88939b927a
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 665dc3afa79..28e2e2dd813 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -77,6 +77,7 @@
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Editing/EditingHistory.h>
+#include <LibWeb/Editing/SpellCheckController.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
@@ -1336,6 +1337,12 @@ void Element::run_attribute_change_steps(Utf16FlyString const& local_name, Optio
if (old_value != value)
CSS::record_element_attribute_changed(*this, local_name, namespace_, old_value, value);
+ if (old_value != value
+ && local_name.is_one_of(HTML::AttributeNames::spellcheck, HTML::AttributeNames::readonly, HTML::AttributeNames::disabled, HTML::AttributeNames::type, HTML::AttributeNames::contenteditable)) {
+ if (auto* controller = document().spell_check_controller_if_exists())
+ controller->did_change_checking_state(*this);
+ }
+
if (old_value != value) {
// An SVG element's parsed attributes are layout input, published under its style node. Published here,
// after the change steps, rather than from SVGElement's: each SVG element runs its base class's steps before it
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.cpp b/Libraries/LibWeb/Editing/SpellCheckController.cpp
index e2139ecb848..0c6c6b1de03 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.cpp
+++ b/Libraries/LibWeb/Editing/SpellCheckController.cpp
@@ -524,6 +524,22 @@ void SpellCheckController::did_focus(GC::Ptr<DOM::Node> node)
send_next_existing_text_chunk();
}
+// https://html.spec.whatwg.org/multipage/interaction.html#spelling-and-grammar-checking
+void SpellCheckController::did_change_checking_state(DOM::Element& element)
+{
+ // If checking is disabled, the user agent should not indicate spelling or grammar errors for that text.
+ Vector<GC::Ref<DOM::Text>> unchecked;
+ m_document->spelling_markers().for_each_marked_text([&](DOM::Text& text, auto) {
+ if (element.is_shadow_including_inclusive_ancestor_of(text) && !text_is_checkable(text))
+ unchecked.append(text);
+ });
+ for (auto& text : unchecked)
+ m_document->spelling_markers().remove_all(text);
+
+ // NB: Checking that turns on starts no check of its own. A script turns it on, not the user, and a check it could
+ // start at will would let a page test any words it likes against the user's dictionary.
+}
+
bool SpellCheckController::is_settled() const
{
return !m_existing_text_pass.has_value() && m_requests_in_flight == 0 && !m_typing_check_scheduled
diff --git a/Libraries/LibWeb/Editing/SpellCheckController.h b/Libraries/LibWeb/Editing/SpellCheckController.h
index ba7dee814ff..2c1e0b436f5 100644
--- a/Libraries/LibWeb/Editing/SpellCheckController.h
+++ b/Libraries/LibWeb/Editing/SpellCheckController.h
@@ -42,6 +42,7 @@ public:
void did_user_edit(DOM::Node&);
void did_end_input_method_composition();
void did_change_selection();
+ void did_change_checking_state(DOM::Element&);
bool is_settled() const;
diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt
new file mode 100644
index 00000000000..6430b933b3e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-turned-off.txt
@@ -0,0 +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)
diff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
new file mode 100644
index 00000000000..372f132cf84
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-turned-off.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<textarea id="spell">txet</textarea>
+<textarea id="readonly">txet</textarea>
+<textarea id="disabled">txet</textarea>
+<input id="type" value="txet">
+<div id="editable" contenteditable>txet</div>
+<div id="later" contenteditable spellcheck="false">txet</div>
+<script>
+ promiseTest(async () => {
+ const ids = ["spell", "readonly", "disabled", "type", "editable", "later"];
+ const node = id => {
+ const element = document.getElementById(id);
+ return element.localName === "div" ? element.firstChild : element;
+ };
+ const show = label => {
+ for (const id of ids)
+ println(`${id} ${label}: ${internals.spellingErrorRanges(node(id)) || "(none)"}`);
+ };
+ for (const id of ids) {
+ document.getElementById(id).focus();
+ await internals.spellCheckSettled();
+ }
+ show("before");
+
+ 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");
+ later.focus();
+ later.spellcheck = true;
+ await internals.spellCheckSettled();
+ show("after");
+ });
+</script>