diff --git a/dom/html/TextControlState.cpp b/dom/html/TextControlState.cpp
index 8f5e6d0c6186..82adc0efd4d9 100644
--- a/dom/html/TextControlState.cpp
+++ b/dom/html/TextControlState.cpp
@@ -2488,6 +2488,12 @@ void TextControlState::UnbindFromFrame(nsTextControlFrame* aFrame) {
DebugOnly ok = SetValue(value, ValueSetterOption::ByInternalAPI);
// TODO Find something better to do if this fails...
NS_WARNING_ASSERTION(ok, "SetValue() couldn't allocate memory");
+ // And mark the selection as dirty to make sure the selection will be
+ // restored properly in RestoreSelectionState. See bug 1993351.
+ if (IsSelectionCached()) {
+ SelectionProperties& props = GetSelectionProperties();
+ props.SetIsDirty();
+ }
}
}
diff --git a/testing/web-platform/tests/html/semantics/forms/the-textarea-element/selection-after-whitespace-change.html b/testing/web-platform/tests/html/semantics/forms/the-textarea-element/selection-after-whitespace-change.html
new file mode 100644
index 000000000000..a1b568b8ae2b
--- /dev/null
+++ b/testing/web-platform/tests/html/semantics/forms/the-textarea-element/selection-after-whitespace-change.html
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+