diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index a91be2c4357..ec2d9e64bde 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -18,6 +18,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -321,8 +322,14 @@ void HTMLParser::the_end(GC::Ref document, GC::Ptr pa
(void)document->scripts_to_execute_when_parsing_has_finished().take_first();
}
- // FIXME: Spec bug: https://github.com/whatwg/html/issues/10914
- document->scroll_to_the_fragment();
+ // AD-HOC: We need to scroll to the fragment on page load somewhere.
+ // But a script that ran in step 5 above may have scrolled the page already,
+ // so only do this if there is an actual fragment to avoid resetting the scroll position unexpectedly.
+ // Spec bug: https://github.com/whatwg/html/issues/10914
+ auto indicated_part = document->determine_the_indicated_part();
+ if (indicated_part.has() && indicated_part.get() != nullptr) {
+ document->scroll_to_the_fragment();
+ }
// 6. Queue a global task on the DOM manipulation task source given the Document's relevant global object to run the following substeps:
queue_global_task(HTML::Task::Source::DOMManipulation, *document, GC::create_function(heap, [document] {
diff --git a/Tests/LibWeb/Text/expected/scroll-during-page-load.txt b/Tests/LibWeb/Text/expected/scroll-during-page-load.txt
new file mode 100644
index 00000000000..11ee4c4e3dd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-during-page-load.txt
@@ -0,0 +1 @@
+Scroll position: 200px
diff --git a/Tests/LibWeb/Text/input/scroll-during-page-load.html b/Tests/LibWeb/Text/input/scroll-during-page-load.html
new file mode 100644
index 00000000000..99ae5909169
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-during-page-load.html
@@ -0,0 +1,12 @@
+
+
+
+
+