Skip to content

Truly independent web browser

LibWeb: Preserve directive state in session history

Sam Atkins36f045750ce1
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index d967388c2ac..2c5dad642d0 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -7253,13 +7253,33 @@ void Document::update_for_history_step_application(NonnullRefPtr<HTML::SessionHi
// 1. Let oldURL be document's latest entry's URL.
auto old_url = m_latest_entry ? m_latest_entry->url() : URL::URL {};
- // 2. Set document's latest entry to entry.
+ // https://wicg.github.io/scroll-to-text-fragment/#applying-directives-to-a-document
+ // 2. If document's latest entry's directive state is not entry's directive state then:
+ auto latest_entry_directive_state = [&] -> RefPtr<HTML::DirectiveState> {
+ if (m_latest_entry)
+ return m_latest_entry->directive_state();
+ if (previous_entry_for_activation)
+ return previous_entry_for_activation->directive_state();
+
+ return nullptr;
+ }();
+ if (!latest_entry_directive_state || latest_entry_directive_state.ptr() != entry->directive_state().ptr()) {
+ // 1. Let fragment directive be entry's directive state's value.
+ auto const& fragment_directive = entry->directive_state()->value();
+
+ // 2. Set document's pending text directives to the result of parsing fragment directive.
+ m_pending_text_directives = fragment_directive.has_value()
+ ? Optional<Vector<HTML::TextDirective>> { HTML::parse_the_fragment_directive(*fragment_directive) }
+ : Optional<Vector<HTML::TextDirective>> { Vector<HTML::TextDirective> {} };
+ }
+
+ // 3. Set document's latest entry to entry.
m_latest_entry = entry;
- // 3. Restore the history object state given document and entry.
+ // 4. Restore the history object state given document and entry.
restore_the_history_object_state(entry);
- // 4. If documentIsNew is false, then:
+ // 5. If documentIsNew is false, then:
if (!document_is_new) {
// NOTE: Not in the spec, but otherwise document's url won't be updated in case of a same-document back/forward navigation.
set_url(entry->url());
@@ -7302,7 +7322,7 @@ void Document::update_for_history_step_application(NonnullRefPtr<HTML::SessionHi
}
}
- // 5. Otherwise:
+ // 6. Otherwise:
else {
// 1. Assert: entriesForNavigationAPI is given.
VERIFY(entries_for_navigation_api.has_value());
diff --git a/Libraries/LibWeb/HTML/DirectiveState.h b/Libraries/LibWeb/HTML/DirectiveState.h
new file mode 100644
index 00000000000..7ce4358ab92
--- /dev/null
+++ b/Libraries/LibWeb/HTML/DirectiveState.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/RefCounted.h>
+#include <AK/String.h>
+#include <LibWeb/HTML/CrossProcessId.h>
+
+namespace Web::HTML {
+
+// https://wicg.github.io/scroll-to-text-fragment/#the-fragment-directive
+// Directive state holds the value of the fragment directive at the time the session history entry
+// was created and is used to invoke directives, such as text highlighting, whenever the entry is
+// traversed.
+class DirectiveState final : public RefCounted<DirectiveState> {
+public:
+ static NonnullRefPtr<DirectiveState> create(CrossProcessId cross_process_id, Optional<String> value = {})
+ {
+ return adopt_ref(*new DirectiveState(cross_process_id, move(value)));
+ }
+
+ Optional<String> const& value() const { return m_value; }
+ void set_value(Optional<String> value) { m_value = move(value); }
+
+ CrossProcessId cross_process_id() const { return m_cross_process_id; }
+
+private:
+ DirectiveState(CrossProcessId cross_process_id, Optional<String> value)
+ : m_value(move(value))
+ , m_cross_process_id(cross_process_id)
+ {
+ }
+
+ // value, the fragment directive ASCII string or null, initially null.
+ Optional<String> m_value;
+
+ // AD-HOC: Preserve object identity when entries cross process boundaries.
+ CrossProcessId m_cross_process_id;
+};
+
+}
diff --git a/Libraries/LibWeb/HTML/TextDirective.h b/Libraries/LibWeb/HTML/TextDirective.h
index 2f6b9568b32..c6b9babeb18 100644
--- a/Libraries/LibWeb/HTML/TextDirective.h
+++ b/Libraries/LibWeb/HTML/TextDirective.h
@@ -11,6 +11,7 @@
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibURL/URL.h>
+#include <LibWeb/Export.h>
namespace Web::HTML {
@@ -21,9 +22,9 @@ struct TextDirective {
Optional<Utf16String> suffix;
};
-Optional<String> remove_the_fragment_directive(URL::URL&);
-Optional<Utf16String> percent_decode_a_text_directive_term(Optional<StringView> term);
-Optional<TextDirective> parse_a_text_directive(StringView text_directive_value);
-Vector<TextDirective> parse_the_fragment_directive(StringView fragment_directive);
+WEB_API Optional<String> remove_the_fragment_directive(URL::URL&);
+WEB_API Optional<Utf16String> percent_decode_a_text_directive_term(Optional<StringView> term);
+WEB_API Optional<TextDirective> parse_a_text_directive(StringView text_directive_value);
+WEB_API Vector<TextDirective> parse_the_fragment_directive(StringView fragment_directive);
}
diff --git a/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.h b/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.h
index 41ff066f177..fb669b83587 100644
--- a/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.h
+++ b/Libraries/LibWebCommon/HTML/NavigationPopulationRequest.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/Optional.h>
+#include <AK/String.h>
#include <AK/Utf16String.h>
#include <LibIPC/Forward.h>
#include <LibWebCommon/Bindings/Navigation.h>
@@ -41,6 +42,8 @@ struct NavigationStartRequest {
StorageSerializationRecord navigation_api_state;
Utf16String navigation_api_key;
Utf16String navigation_api_id;
+ CrossProcessId directive_state_id;
+ Optional<String> directive_state_value;
};
// The serializable inputs needed to continue the navigate algorithm in the process selected to
diff --git a/Libraries/LibWebView/SessionHistorySnapshotStorage.cpp b/Libraries/LibWebView/SessionHistorySnapshotStorage.cpp
index 6c8e85c8ccd..e3868826999 100644
--- a/Libraries/LibWebView/SessionHistorySnapshotStorage.cpp
+++ b/Libraries/LibWebView/SessionHistorySnapshotStorage.cpp
@@ -504,6 +504,7 @@ static ErrorOr<void> validate_entry_within_column_caps(Web::HTML::SessionHistory
TRY(validate_text_column_within_cap(entry.url.serialize(), totals));
TRY(validate_text_column_within_cap(entry.navigation_api_key, totals));
TRY(validate_text_column_within_cap(entry.navigation_api_id, totals));
+ TRY(validate_text_column_within_cap(entry.directive_state_value, totals));
TRY(validate_text_column_within_cap(document_state.navigable_target_name, totals));
if (document_state.about_base_url.has_value())
TRY(validate_text_column_within_cap(document_state.about_base_url->serialize(), totals));
@@ -548,6 +549,7 @@ ErrorOr<SessionHistorySnapshotStatements> prepare_session_history_snapshot_state
history_id, entry_ordinal, step, url, document_state_namespace_id, document_state_local_id,
classic_state, navigation_state,
navigation_api_key, navigation_api_id, scroll_restoration_mode,
+ directive_state_namespace_id, directive_state_local_id, directive_state_value,
scroll_x_raw, scroll_y_raw,
origin_kind, origin_nonce, origin_scheme, origin_host, origin_port, origin_domain,
initiator_origin_kind, initiator_origin_nonce, initiator_origin_scheme,
@@ -558,6 +560,7 @@ ErrorOr<SessionHistorySnapshotStatements> prepare_session_history_snapshot_state
:history_id, :entry_ordinal, :step, :url, :document_state_namespace_id, :document_state_local_id,
:classic_state, :navigation_state,
:navigation_api_key, :navigation_api_id, :scroll_restoration_mode,
+ :directive_state_namespace_id, :directive_state_local_id, :directive_state_value,
:scroll_x_raw, :scroll_y_raw,
:origin_kind, :origin_nonce, :origin_scheme, :origin_host, :origin_port, :origin_domain,
:initiator_origin_kind, :initiator_origin_nonce, :initiator_origin_scheme,
@@ -607,6 +610,7 @@ ErrorOr<SessionHistorySnapshotStatements> prepare_session_history_snapshot_state
SELECT history_id, entry_ordinal, step, url, document_state_namespace_id, document_state_local_id,
classic_state, navigation_state,
navigation_api_key, navigation_api_id, scroll_restoration_mode,
+ directive_state_namespace_id, directive_state_local_id, directive_state_value,
scroll_x_raw, scroll_y_raw,
origin_kind, origin_nonce, origin_scheme, origin_host, origin_port, origin_domain,
initiator_origin_kind, initiator_origin_nonce, initiator_origin_scheme,
@@ -805,6 +809,9 @@ static ErrorOr<void> store_history_entries(Database::Database& database, Session
TRY(bind("navigation_api_key"sv, entry.navigation_api_key));
TRY(bind("navigation_api_id"sv, entry.navigation_api_id));
TRY(bind("scroll_restoration_mode"sv, encode_scroll_restoration_mode(entry.scroll_restoration_mode)));
+ TRY(bind("directive_state_namespace_id"sv, encode_cross_process_id_component(entry.directive_state_id.namespace_id)));
+ TRY(bind("directive_state_local_id"sv, encode_cross_process_id_component(entry.directive_state_id.local_id)));
+ TRY(bind("directive_state_value"sv, entry.directive_state_value));
TRY(bind("scroll_x_raw"sv, scroll.map([](auto const& point) { return point.x().raw_value(); })));
TRY(bind("scroll_y_raw"sv, scroll.map([](auto const& point) { return point.y().raw_value(); })));
TRY(bind("origin_kind"sv, origin.kind));
@@ -900,6 +907,9 @@ struct EntryRow {
Utf16String navigation_api_key;
Utf16String navigation_api_id;
i64 scroll_restoration_mode { 0 };
+ i64 directive_state_namespace_id { 0 };
+ i64 directive_state_local_id { 0 };
+ Optional<String> directive_state_value;
Optional<i32> scroll_x_raw;
Optional<i32> scroll_y_raw;
PersistedOrigin origin;
@@ -1203,6 +1213,8 @@ static ErrorOr<Web::HTML::SessionHistoryEntryDescriptor> build_entry(EntryRow&&
entry.navigation_api_key = row.navigation_api_key;
entry.navigation_api_id = row.navigation_api_id;
entry.scroll_restoration_mode = scroll_restoration_mode;
+ entry.directive_state_id = Web::HTML::CrossProcessId { .namespace_id = decode_cross_process_id_component(row.directive_state_namespace_id), .local_id = decode_cross_process_id_component(row.directive_state_local_id) };
+ entry.directive_state_value = move(row.directive_state_value);
if (row.scroll_x_raw.has_value())
entry.scroll_position_data.viewport_scroll_position = Web::CSSPixelPoint { Web::CSSPixels::from_raw(*row.scroll_x_raw), Web::CSSPixels::from_raw(*row.scroll_y_raw) };
return entry;
@@ -1304,6 +1316,9 @@ ErrorOr<SessionHistorySnapshot> load_session_history_snapshot(Database::Database
row.navigation_api_key = TRY(read_snapshot_utf16_text(result_row, "navigation_api_key"sv, totals));
row.navigation_api_id = TRY(read_snapshot_utf16_text(result_row, "navigation_api_id"sv, totals));
row.scroll_restoration_mode = TRY(result_row.read_integer<i64>("scroll_restoration_mode"sv));
+ row.directive_state_namespace_id = TRY(result_row.read_integer<i64>("directive_state_namespace_id"sv));
+ row.directive_state_local_id = TRY(result_row.read_integer<i64>("directive_state_local_id"sv));
+ row.directive_state_value = TRY(read_optional_snapshot_text(result_row, "directive_state_value"sv, totals));
row.scroll_x_raw = TRY(result_row.read_optional_integer<i32>("scroll_x_raw"sv));
row.scroll_y_raw = TRY(result_row.read_optional_integer<i32>("scroll_y_raw"sv));
row.origin = {
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index b560fff0356..b27e706a857 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -528,6 +528,7 @@ Text/input/wpt-import/content-security-policy/unsafe-eval/eval-scripts-setTimeou
Text/input/wpt-import/content-security-policy/unsafe-eval/eval-scripts-setTimeout-setInterval-policy-added-later.html
; scroll-to-text-fragment tests all rely on loading/fetching other pages.
+Text/input/scroll-to-text-fragment/history.html
Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive.html
Text/input/wpt-import/scroll-to-text-fragment/force-load-at-top.html
Text/input/wpt-import/scroll-to-text-fragment/iframe-target.html
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment/history.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/history.txt
new file mode 100644
index 00000000000..e9ed41afe8f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/history.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Fragment directives remain hidden across same-document history operations
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/force-load-at-top.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/force-load-at-top.txt
index 829bbe600f1..776d13995a3 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/force-load-at-top.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/force-load-at-top.txt
@@ -5,10 +5,10 @@ Found 8 tests
4 Pass
4 Fail
Pass force-load-at-top must block scroll on load from text fragment.
-Pass force-load-at-top must block scroll on load from text fragment with element fallback.
+Fail force-load-at-top must block scroll on load from text fragment with element fallback.
Fail force-load-at-top must block scroll on load from element fragment.
Fail force-load-at-top must block scroll on load from history scroll restoration.
Fail no-force-load-at-top must not block scroll on load from text fragment.
-Fail no-force-load-at-top must not block scroll on load from text fragment with element fallback.
+Pass no-force-load-at-top must not block scroll on load from text fragment with element fallback.
Pass no-force-load-at-top must not block scroll on load from element fragment.
Pass no-force-load-at-top must not block scroll on load from history scroll restoration.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
index 2aa89708e11..48ccd211649 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-force-load-at-top.txt
@@ -2,9 +2,9 @@ Harness status: OK
Found 4 tests
-1 Pass
-3 Fail
+3 Pass
+1 Fail
Pass Basic element fragment navigation
Fail Basic text directive navigation
-Fail Basic element id fallback
-Fail Malformed text directive element id fallback
\ No newline at end of file
+Pass Basic element id fallback
+Pass Malformed text directive element id fallback
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
index 2aa89708e11..48ccd211649 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests-no-force-load-at-top.txt
@@ -2,9 +2,9 @@ Harness status: OK
Found 4 tests
-1 Pass
-3 Fail
+3 Pass
+1 Fail
Pass Basic element fragment navigation
Fail Basic text directive navigation
-Fail Basic element id fallback
-Fail Malformed text directive element id fallback
\ No newline at end of file
+Pass Basic element id fallback
+Pass Malformed text directive element id fallback
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
index 2aa89708e11..48ccd211649 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/same-document-tests.txt
@@ -2,9 +2,9 @@ Harness status: OK
Found 4 tests
-1 Pass
-3 Fail
+3 Pass
+1 Fail
Pass Basic element fragment navigation
Fail Basic text directive navigation
-Fail Basic element id fallback
-Fail Malformed text directive element id fallback
\ No newline at end of file
+Pass Basic element id fallback
+Pass Malformed text directive element id fallback
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment.txt
index cb9d996c195..67e011704e8 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/scroll-to-text-fragment.txt
@@ -2,14 +2,14 @@ Harness status: OK
Found 44 tests
-1 Pass
-43 Fail
+16 Pass
+28 Fail
Pass Test navigation with fragment: Empty hash should scroll to top.
-Fail Test navigation with fragment: Text directive with invalid syntax (context terms without "-") should not parse as a text directive.
-Fail Test navigation with fragment: Text directive with invalid syntax (only prefix, no start text) should not parse as a text directive.
-Fail Test navigation with fragment: Text directive with invalid syntax (only suffix, no start text) should not parse as a text directive.
-Fail Test navigation with fragment: Generic fragment directive with existing element fragment should scroll to element.
-Fail Test navigation with fragment: Uppercase TEXT directive should not parse as a text directive.
+Pass Test navigation with fragment: Text directive with invalid syntax (context terms without "-") should not parse as a text directive.
+Pass Test navigation with fragment: Text directive with invalid syntax (only prefix, no start text) should not parse as a text directive.
+Pass Test navigation with fragment: Text directive with invalid syntax (only suffix, no start text) should not parse as a text directive.
+Pass Test navigation with fragment: Generic fragment directive with existing element fragment should scroll to element.
+Pass Test navigation with fragment: Uppercase TEXT directive should not parse as a text directive.
Fail Test navigation with fragment: Exact text with no context should match text.
Fail Test navigation with fragment: Case-insensitive search with no context should match text.
Fail Test navigation with fragment: Exact text with prefix should match text.
@@ -20,31 +20,31 @@ Fail Test navigation with fragment: Text range with no context should match text
Fail Test navigation with fragment: Text range with prefix should match text.
Fail Test navigation with fragment: Text range with suffix should match text.
Fail Test navigation with fragment: Text range with prefix and suffix should match text.
-Fail Test navigation with fragment: Text range with non-matching endText should not match.
-Fail Test navigation with fragment: Text range with non-matching startText should not match.
-Fail Test navigation with fragment: Text range with prefix and nonmatching suffix should not match.
-Fail Test navigation with fragment: Text range with nonmatching prefix and matching suffix should not match.
+Pass Test navigation with fragment: Text range with non-matching endText should not match.
+Pass Test navigation with fragment: Text range with non-matching startText should not match.
+Pass Test navigation with fragment: Text range with prefix and nonmatching suffix should not match.
+Pass Test navigation with fragment: Text range with nonmatching prefix and matching suffix should not match.
Fail Test navigation with fragment: Exact text with percent encoded spaces should match text.
-Fail Test navigation with fragment: Non-whole-word exact text with spaces should not match.
+Pass Test navigation with fragment: Non-whole-word exact text with spaces should not match.
Fail Test navigation with fragment: Fragment directive with percent encoded syntactical characters "&,-" should match text.
Fail Test navigation with fragment: Fragment directive with percent encoded non-ASCII unicode character should match text.
Fail Test navigation with fragment: Fragment directive with all TextMatchChars should match text.
Fail Test navigation with fragment: Multiple matching exact texts should match text.
-Fail Test navigation with fragment: Multiple non-whole-word exact texts should not match.
+Pass Test navigation with fragment: Multiple non-whole-word exact texts should not match.
Fail Test navigation with fragment: A non-matching text directive followed by a matching text directive should match and scroll into view the second text directive.
Fail Test navigation with fragment: Text directive followed by non-text directive should match text.
Fail Test navigation with fragment: Multiple text directives and a non-text directive should match text.
Fail Test navigation with fragment: Text directive with existing element fragment should match and scroll into view text.
Fail Test navigation with fragment: Text directive with nonexistent element fragment should match and scroll into view text.
-Fail Test navigation with fragment: Non-matching text directive with existing element fragment should scroll to element.
-Fail Test navigation with fragment: Non-matching text directive with nonexistent element fragment should not match and not scroll.
+Pass Test navigation with fragment: Non-matching text directive with existing element fragment should scroll to element.
+Pass Test navigation with fragment: Non-matching text directive with nonexistent element fragment should not match and not scroll.
Fail Test navigation with fragment: Multiple match text directive disambiguated by prefix should match the prefixed text.
Fail Test navigation with fragment: Multiple match text directive disambiguated by suffix should match the suffixed text.
Fail Test navigation with fragment: Multiple match text directive disambiguated by prefix and suffix should match the text with the given context.
Fail Test navigation with fragment: Text directive should match when context terms are separated by node boundaries.
Fail Test navigation with fragment: Text directive should match text within shadow DOM.
-Fail Test navigation with fragment: Text directive should not scroll to hidden text.
-Fail Test navigation with fragment: Text directive should not scroll to display none text.
+Pass Test navigation with fragment: Text directive should not scroll to hidden text.
+Pass Test navigation with fragment: Text directive should not scroll to display none text.
Fail Test navigation with fragment: Text directive should horizontally scroll into view.
Fail Test navigation with fragment: Text directive should horizontally scroll into view within a wide line.
Fail Test navigation with fragment: Text directive that spans a range larger than the viewport should scroll the start into view.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment/history.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment/history.html
new file mode 100644
index 00000000000..0f005e4b3ea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment/history.html
@@ -0,0 +1,36 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Fragment directive session history</title>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#the-fragment-directive">
+<script src="../wpt-import/resources/testharness.js"></script>
+<script src="../wpt-import/resources/testharnessreport.js"></script>
+<script>
+promise_test(async t => {
+ history.replaceState(null, "", "#base");
+
+ let hashchangeCount = 0;
+ window.addEventListener("hashchange", () => ++hashchangeCount);
+
+ location.hash = "#base:~:text=first";
+ await new Promise(resolve => t.step_timeout(resolve, 0));
+ assert_equals(location.hash, "#base", "a directive-only navigation exposes the stripped fragment");
+ assert_equals(hashchangeCount, 0, "a directive-only navigation does not fire hashchange");
+
+ const changedHash = new Promise(resolve => window.addEventListener("hashchange", resolve, { once: true }));
+ location.hash = "#other:~:";
+ await changedHash;
+ assert_equals(location.hash, "#other", "a trailing-empty directive is stripped");
+ assert_equals(hashchangeCount, 1, "changing the stripped fragment fires hashchange");
+
+ history.pushState({ entry: "push" }, "", "#push:~:text=second");
+ assert_equals(location.hash, "#push", "pushState stores a stripped URL");
+
+ history.replaceState({ entry: "replace" }, "", "#replace:~:");
+ assert_equals(location.hash, "#replace", "replaceState stores a stripped trailing-empty URL");
+
+ const traversed = new Promise(resolve => window.addEventListener("popstate", resolve, { once: true }));
+ history.back();
+ await traversed;
+ assert_equals(location.hash, "#other", "traversal exposes the stripped history URL");
+}, "Fragment directives remain hidden across same-document history operations");
+</script>
diff --git a/Tests/LibWebView/TestApplyHistoryStep.cpp b/Tests/LibWebView/TestApplyHistoryStep.cpp
index 111d4110032..7e6f2665047 100644
--- a/Tests/LibWebView/TestApplyHistoryStep.cpp
+++ b/Tests/LibWebView/TestApplyHistoryStep.cpp
@@ -100,6 +100,8 @@ static Web::HTML::SessionHistoryEntryDescriptor entry(i32 step, StringView url)
.navigation_api_id = Utf16String::from_utf8(url),
.scroll_restoration_mode = Web::HTML::ScrollRestorationMode::Auto,
.scroll_position_data = {},
+ .directive_state_id = { 3, 2000 + static_cast<u64>(step) },
+ .directive_state_value = {},
};
}
diff --git a/Tests/LibWebView/TestSessionHistory.cpp b/Tests/LibWebView/TestSessionHistory.cpp
index c202f56fea3..b4e332dc109 100644
--- a/Tests/LibWebView/TestSessionHistory.cpp
+++ b/Tests/LibWebView/TestSessionHistory.cpp
@@ -100,6 +100,8 @@ static Web::HTML::SessionHistoryEntryDescriptor create_test_entry(i32 step, URL:
.navigation_api_id = {},
.scroll_restoration_mode = Web::HTML::ScrollRestorationMode::Auto,
.scroll_position_data = {},
+ .directive_state_id = test_document_state_id(2000 + static_cast<u64>(step)),
+ .directive_state_value = {},
};
}
@@ -119,6 +121,8 @@ static Web::HTML::PendingSessionHistoryEntryDescriptor pending_entry(Web::HTML::
.navigation_api_id = move(entry.navigation_api_id),
.scroll_restoration_mode = entry.scroll_restoration_mode,
.scroll_position_data = move(entry.scroll_position_data),
+ .directive_state_id = entry.directive_state_id,
+ .directive_state_value = move(entry.directive_state_value),
};
}
@@ -134,6 +138,8 @@ static Web::HTML::SessionHistoryEntryDescriptor entry_with_step(Web::HTML::Pendi
.navigation_api_id = move(entry.navigation_api_id),
.scroll_restoration_mode = entry.scroll_restoration_mode,
.scroll_position_data = move(entry.scroll_position_data),
+ .directive_state_id = entry.directive_state_id,
+ .directive_state_value = move(entry.directive_state_value),
};
}
@@ -230,6 +236,8 @@ static Web::HTML::PendingSessionHistoryEntryDescriptor pending_entry(StringView
.navigation_api_id = move(descriptor.navigation_api_id),
.scroll_restoration_mode = descriptor.scroll_restoration_mode,
.scroll_position_data = move(descriptor.scroll_position_data),
+ .directive_state_id = descriptor.directive_state_id,
+ .directive_state_value = move(descriptor.directive_state_value),
};
}
@@ -244,6 +252,8 @@ static Web::HTML::SameDocumentNavigationEntry same_document_entry(Web::HTML::Ses
.navigation_api_id = move(entry.navigation_api_id),
.scroll_restoration_mode = entry.scroll_restoration_mode,
.scroll_position_data = move(entry.scroll_position_data),
+ .directive_state_id = entry.directive_state_id,
+ .directive_state_value = move(entry.directive_state_value),
};
}
@@ -263,6 +273,60 @@ static void run_canonical_history_operation(WebView::CanonicalTraversable& trave
VERIFY(queue_promise->is_resolved());
}
+TEST_CASE(display_url_includes_fragment_directive_state)
+{
+ auto entry = create_test_entry(0, parse_url("https://example.test/#feature-detectability"sv));
+ entry.directive_state_value = "text=For-,feature%20detectability"_string;
+
+ EXPECT_EQ(
+ WebView::display_url_for_session_history_entry(entry),
+ parse_url("https://example.test/#feature-detectability:~:text=For-,feature%20detectability"sv));
+
+ entry.directive_state_value.clear();
+ EXPECT_EQ(
+ WebView::display_url_for_session_history_entry(entry),
+ parse_url("https://example.test/#feature-detectability"sv));
+}
+
+TEST_CASE(replace_navigation_updates_every_shared_directive_state_descriptor)
+{
+ Core::EventLoop event_loop;
+ WebView::CanonicalTraversable traversable;
+ traversable.set_id({ 9, 1 });
+ auto shared_directive_state_id = test_document_state_id(3000);
+
+ auto first_entry = entry(0, "https://example.test/first"sv);
+ first_entry.navigation_api_key = "first-key"_utf16;
+ first_entry.directive_state_id = shared_directive_state_id;
+ first_entry.directive_state_value = "text=old"_string;
+
+ auto second_entry = entry(1, "https://example.test/second"sv);
+ second_entry.navigation_api_key = "second-key"_utf16;
+ second_entry.directive_state_id = shared_directive_state_id;
+ second_entry.directive_state_value = "text=old"_string;
+
+ EXPECT(traversable.initialize_session_history_for_testing({ first_entry, second_entry }, { 0, 1 }, 1));
+
+ auto target_entry = second_entry;
+ target_entry.directive_state_value = "text=new"_string;
+ auto request = Web::FinalizeSameDocumentNavigationHistoryOperationParameters {
+ .navigable_id = traversable.id(),
+ .target_entry = same_document_entry(move(target_entry)),
+ .entry_to_replace = Web::HTML::SessionHistoryEntryIdentity {
+ .document_state_id = second_entry.document_state.id,
+ .navigation_api_id = second_entry.navigation_api_id,
+ },
+ .previous_entry_persisted_state = {},
+ .history_handling = Web::HTML::HistoryHandlingBehavior::Replace,
+ .user_involvement = Web::HTML::UserNavigationInvolvement::None,
+ };
+ auto operation_result = run_canonical_history_operation(traversable, move(request));
+ EXPECT_EQ(operation_result.result, Web::HTML::HistoryStepResult::Applied);
+
+ EXPECT_EQ(traversable.session_history().entry_at(0)->directive_state_value, "text=new"sv);
+ EXPECT_EQ(traversable.session_history().entry_at(1)->directive_state_value, "text=new"sv);
+}
+
static void expect_entry(WebView::TraversableSessionHistory const& history, size_t index, i32 expected_step, StringView expected_url)
{
auto* entry = history.entry_at(index);
diff --git a/Tests/LibWebView/TestSessionStore.cpp b/Tests/LibWebView/TestSessionStore.cpp
index ad2fb35f02c..fae3788ac5a 100644
--- a/Tests/LibWebView/TestSessionStore.cpp
+++ b/Tests/LibWebView/TestSessionStore.cpp
@@ -28,6 +28,8 @@ static Web::HTML::SessionHistoryEntryDescriptor make_entry(i32 step, StringView
entry.document_state.history_policy_container = Web::HTML::DocumentStateClient::Tag;
entry.classic_history_api_state = Web::HTML::StorageSerializationRecord { MUST(ByteBuffer::copy({ &state_byte, 1 })) };
entry.navigation_api_state = Web::HTML::StorageSerializationRecord { MUST(ByteBuffer::copy({ &state_byte, 1 })) };
+ entry.directive_state_id = { 2, document_state_id };
+ entry.directive_state_value = "text=target"_string;
return entry;
}