Skip to content

Truly independent web browser

LibWeb: Forget the undo history of a text control when its value is set

Andreas Kling2b8841dde8c6
diff --git a/Libraries/LibWeb/Editing/EditingHistory.cpp b/Libraries/LibWeb/Editing/EditingHistory.cpp
index 5b57cc88b71..b174dfe2e38 100644
--- a/Libraries/LibWeb/Editing/EditingHistory.cpp
+++ b/Libraries/LibWeb/Editing/EditingHistory.cpp
@@ -432,6 +432,15 @@ void EditingHistory::notify_state_if_changed(DOM::Document& document)
document.page().client().page_did_update_editing_history_state(new_can_undo, new_can_redo);
}
+void EditingHistory::forget_steps_for(DOM::Node& editing_host)
+{
+ if (m_open_step && m_open_step->editing_host().ptr() == &editing_host)
+ m_open_step = nullptr;
+ m_undo_stack.remove_all_matching([&](GC::Ref<UndoStep> const& step) { return step->editing_host().ptr() == &editing_host; });
+ m_redo_stack.remove_all_matching([&](GC::Ref<UndoStep> const& step) { return step->editing_host().ptr() == &editing_host; });
+ notify_state_if_changed(editing_host.document());
+}
+
void EditingHistory::prune_steps_for_disconnected_hosts()
{
// INTEROP: Blink erases the undo steps owned by a root editable element when it is removed
diff --git a/Libraries/LibWeb/Editing/EditingHistory.h b/Libraries/LibWeb/Editing/EditingHistory.h
index 8cbab0c1616..404253fc5ab 100644
--- a/Libraries/LibWeb/Editing/EditingHistory.h
+++ b/Libraries/LibWeb/Editing/EditingHistory.h
@@ -175,6 +175,9 @@ public:
GC::Ptr<UndoStep> next_undo_step();
GC::Ptr<UndoStep> next_redo_step();
+ // Forgets the undo and redo steps of an editing host, e.g. when a script sets the value of a text control.
+ void forget_steps_for(DOM::Node& editing_host);
+
// Called whenever the document selection changes; ends typing coalescence unless the change
// came from an editing command or from history application itself.
void selection_changed();
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 72d424b50b4..10f576868d1 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -39,6 +39,7 @@
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOMURL/DOMURL.h>
+#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/Dates.h>
@@ -742,6 +743,14 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_relevant_value(Utf16View value)
return {};
}
+WebIDL::ExceptionOr<void> HTMLInputElement::set_value_binding(Utf16View value)
+{
+ // INTEROP: Browsers forget the undo history of a text control when a script sets its value.
+ if (auto history = document().editing_history_if_exists())
+ history->forget_steps_for(*this);
+ return set_value(value);
+}
+
WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)
{
switch (value_attribute_mode()) {
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h
index 4b7fe362bcb..584a14f055d 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.h
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h
@@ -92,6 +92,8 @@ public:
virtual Utf16String form_value() const override { return value(); }
virtual Optional<Utf16String> optional_value() const override;
WebIDL::ExceptionOr<void> set_value(Utf16View);
+ Utf16String value_binding() const { return value(); }
+ WebIDL::ExceptionOr<void> set_value_binding(Utf16View);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
virtual Utf16String relevant_value() const override;
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Libraries/LibWeb/HTML/HTMLInputElement.idl
index 81d7bbeeb5e..c4c24d6ac39 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.idl
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.idl
@@ -37,7 +37,7 @@ interface HTMLInputElement : HTMLElement {
[CEReactions, Reflect] attribute boolean switch;
[CEReactions] attribute DOMString type;
[CEReactions, Reflect=value] attribute DOMString defaultValue;
- [CEReactions, LegacyNullToEmptyString] attribute DOMString value;
+ [CEReactions, LegacyNullToEmptyString, ImplementedAs=value_binding] attribute DOMString value;
[ImplementedAs=value_as_date_object] attribute object? valueAsDate;
attribute unrestricted double valueAsNumber;
[CEReactions] attribute unsigned long width;
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
index 76abb67d913..9f073fa0b6e 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp
@@ -20,6 +20,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
+#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -171,6 +172,14 @@ Utf16String HTMLTextAreaElement::value() const
return api_value();
}
+void HTMLTextAreaElement::set_value_binding(Utf16View value)
+{
+ // INTEROP: Browsers forget the undo history of a text control when a script sets its value.
+ if (auto history = document().editing_history_if_exists())
+ history->forget_steps_for(*this);
+ set_value(value);
+}
+
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
void HTMLTextAreaElement::set_value(Utf16View value)
{
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
index 13e238925fa..41b06ec9e20 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h
@@ -85,6 +85,8 @@ public:
Utf16String value() const;
virtual Utf16String form_value() const override { return value(); }
void set_value(Utf16View);
+ Utf16String value_binding() const { return value(); }
+ void set_value_binding(Utf16View);
// https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-api-value-3
Utf16String api_value() const;
diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.idl b/Libraries/LibWeb/HTML/HTMLTextAreaElement.idl
index abd04a65735..cf3b465478e 100644
--- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.idl
+++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.idl
@@ -19,7 +19,7 @@ interface HTMLTextAreaElement : HTMLElement {
readonly attribute DOMString type;
[CEReactions] attribute DOMString defaultValue;
- [LegacyNullToEmptyString] attribute DOMString value;
+ [LegacyNullToEmptyString, ImplementedAs=value_binding] attribute DOMString value;
readonly attribute unsigned long textLength;
readonly attribute boolean willValidate;