Skip to content

Truly independent web browser

LibWebView: Replace in place when a traversal leaves the replaced entry

Tim Ledbetter293ee18d76cb
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index dc519676825..0f5ca9445f9 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -2257,11 +2257,22 @@ void CanonicalTraversable::finalize_a_same_document_navigation(HistoryOperation&
return;
}
+ auto replaces_current_entry = target_navigable->current_session_history_entry_is(**entry_to_replace);
+
// 5.1. Replace entryToReplace with targetEntry in targetEntries.
// 5.2. Set targetEntry's step to entryToReplace's step.
auto replaced = m_session_history.replace_session_history_entry(*target_navigable, **entry_to_replace, *target_entry);
VERIFY(replaced);
+ // AD-HOC: A traversal that runs while this replacement waits can move targetNavigable away from
+ // entryToReplace. Applying the history step would then move targetNavigable to targetEntry, so
+ // the replacement happens in place only.
+ if (!replaces_current_entry) {
+ m_session_history.update_directive_state_value(target_entry->directive_state_id, target_entry->directive_state_value);
+ finish_history_operation(operation.operation_id);
+ return;
+ }
+
// 5.3. Set targetStep to traversable's current session history step.
target_step = current_step;
}
diff --git a/Tests/LibWeb/Text/expected/navigation/iframe-replace-state-racing-traversal.txt b/Tests/LibWeb/Text/expected/navigation/iframe-replace-state-racing-traversal.txt
new file mode 100644
index 00000000000..b7a75d17d01
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/navigation/iframe-replace-state-racing-traversal.txt
@@ -0,0 +1,2 @@
+After going back: #a
+After going forward: #replaced
diff --git a/Tests/LibWeb/Text/input/navigation/iframe-replace-state-racing-traversal.html b/Tests/LibWeb/Text/input/navigation/iframe-replace-state-racing-traversal.html
new file mode 100644
index 00000000000..00cf01738db
--- /dev/null
+++ b/Tests/LibWeb/Text/input/navigation/iframe-replace-state-racing-traversal.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe srcdoc=""></iframe>
+<script>
+ function popstate(window) {
+ return new Promise(resolve => window.addEventListener("popstate", resolve, { once: true }));
+ }
+
+ function hashchange(window) {
+ return new Promise(resolve => window.addEventListener("hashchange", resolve, { once: true }));
+ }
+
+ promiseTest(async () => {
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ const frame = document.querySelector("iframe").contentWindow;
+
+ frame.location.hash = "a";
+ await hashchange(frame);
+ frame.location.hash = "b";
+ await hashchange(frame);
+
+ // The replacement runs while the traversal back is still under way. It replaces the forward entry, so going
+ // forward again should reach the replaced entry.
+ let traversed = popstate(frame);
+ frame.history.go(-1);
+ frame.history.replaceState(null, "", "about:srcdoc#replaced");
+ await traversed;
+ println(`After going back: ${frame.location.hash}`);
+
+ traversed = popstate(frame);
+ history.go(1);
+ await traversed;
+ println(`After going forward: ${frame.location.hash}`);
+ });
+</script>