diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index 7dcc965e4ba..5192676fd40 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -232,7 +232,12 @@ void HTMLParser::run(HTMLTokenizer::StopAtInsertionPoint stop_at_insertion_point
break;
}
}
-
+ // Set element as target element if url fragment matches its id.
+ auto url_fragment = m_document->url().fragment();
+ GC::Ptr target_element;
+ if (url_fragment.has_value() && (target_element = m_document->get_element_by_id(url_fragment.value()))){
+ m_document->set_target_element(target_element);
+ }
flush_character_insertions();
}
diff --git a/Tests/LibWeb/Ref/expected/iframe-target-element.html b/Tests/LibWeb/Ref/expected/iframe-target-element.html
new file mode 100644
index 00000000000..1e2c90837ea
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/iframe-target-element.html
@@ -0,0 +1,12 @@
+
+
+
+
\ No newline at end of file
diff --git a/Tests/LibWeb/Ref/input/iframe-target-element.html b/Tests/LibWeb/Ref/input/iframe-target-element.html
new file mode 100644
index 00000000000..32bc809c0d8
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/iframe-target-element.html
@@ -0,0 +1,12 @@
+
+
+
+
+
\ No newline at end of file