LibWeb: Set element as target element if url fragment matches its id

This commit is contained in:
MFMF-EGY 2024-11-21 05:44:27 +02:00
parent 135daeb8bb
commit b56b15e03a
3 changed files with 30 additions and 1 deletions

View file

@ -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<DOM::Element> 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();
}

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<iframe id="iframe"></iframe>
<script>
const IFRAME_DATA_SRC = `data:text/html,
<p id="foo">This should be green.</p>
<style>%23foo { color: green; }<\/style>#foo`.replace('\n', '');
const iframe = document.getElementById("iframe");
iframe.src = IFRAME_DATA_SRC;
</script>

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<link rel="match" href="../expected/iframe-target-element.html" />
<iframe id="iframe"></iframe>
<script>
const IFRAME_DATA_SRC = `data:text/html,
<style>:target { color: green; }<\/style>
<p id="foo">This should be green.</p>#foo`.replace('\n', '');
const iframe = document.getElementById("iframe");
iframe.src = IFRAME_DATA_SRC;
</script>