LibHTML+Browser: Support scrolling to anchor with <a href="#foo">

This patch implements basic support for <a href="#foo"> fragment links.

To figure out where we actually want to scroll to, we have to do
something different based on the layout node's box type. So if it's a
regular LayoutBox we can just use the LayoutBox::position().

However, if it's an inline layout node, we use the position of the
first line box fragment in the containing block contributed by this
layout node or one of its descendants.
This commit is contained in:
Andreas Kling 2019-10-20 09:14:12 +02:00
parent 202dfbd6cd
commit c41bae3d54
Notes: sideshowbarker 2024-07-19 11:37:23 +09:00
8 changed files with 100 additions and 13 deletions

View file

@ -312,3 +312,27 @@ LayoutDocument* HtmlView::layout_root()
return nullptr;
return const_cast<LayoutDocument*>(document()->layout_node());
}
void HtmlView::scroll_to_anchor(const StringView& name)
{
HTMLAnchorElement* element = nullptr;
m_document->for_each_in_subtree([&](auto& node) {
if (!is<HTMLAnchorElement>(node))
return;
auto& anchor_element = to<HTMLAnchorElement>(node);
if (anchor_element.name() == name)
element = &anchor_element;
});
if (!element) {
dbg() << "HtmlView::scroll_to_anchor(): Anchor not found: '" << name << "'";
return;
}
if (!element->layout_node()) {
dbg() << "HtmlView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
return;
}
auto& layout_node = *element->layout_node();
scroll_into_view({ layout_node.box_type_agnostic_position(), visible_content_rect().size() }, true, true);
window()->set_override_cursor(GStandardCursor::None);
}