mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-06 00:25:12 +00:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user