1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +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));

View file

@ -30,6 +30,7 @@ public:
ExceptionOr<void> set_start_after(Node& node);
ExceptionOr<void> set_end_before(Node& node);
ExceptionOr<void> set_end_after(Node& node);
ExceptionOr<void> select_node(Node& node);
// https://dom.spec.whatwg.org/#dom-range-start_to_start
enum HowToCompareBoundaryPoints : u16 {
@ -61,6 +62,7 @@ private:
};
ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
ExceptionOr<void> select(Node& node);
};
}

View file

@ -14,6 +14,7 @@ interface Range : AbstractRange {
undefined setStartAfter(Node node);
undefined setEndBefore(Node node);
undefined setEndAfter(Node node);
undefined selectNode(Node node);
const unsigned short START_TO_START = 0;
const unsigned short START_TO_END = 1;