1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:07:44 +00:00

LibWeb: Implement Range.selectNode

This commit is contained in:
Luke Wilde 2022-01-31 18:33:41 +00:00 committed by Andreas Kling
parent a26f1b2ff9
commit dfdc2ddb9e
3 changed files with 34 additions and 0 deletions

View file

@ -314,6 +314,37 @@ ExceptionOr<i16> Range::compare_boundary_points(u16 how, Range const& source_ran
}
}
// https://dom.spec.whatwg.org/#concept-range-select
ExceptionOr<void> Range::select(Node& node)
{
// 1. Let parent be nodes parent.
auto* parent = node.parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent)
return InvalidNodeTypeError::create("Given node has no parent.");
// 3. Let index be nodes index.
auto index = node.index();
// 4. Set ranges start to boundary point (parent, index).
m_start_container = *parent;
m_start_offset = index;
// 5. Set ranges end to boundary point (parent, index plus 1).
m_end_container = *parent;
m_end_offset = index + 1;
return {};
}
// https://dom.spec.whatwg.org/#dom-range-selectnode
ExceptionOr<void> Range::select_node(Node& node)
{
// The selectNode(node) method steps are to select node within this.
return select(node);
}
NonnullRefPtr<Range> Range::clone_range() const
{
return adopt_ref(*new Range(const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset));