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

LibWeb: Implement Range.isPointInRange

This commit is contained in:
Luke Wilde 2022-02-01 18:45:34 +00:00 committed by Andreas Kling
parent 386ee5ab17
commit 62b76e0658
3 changed files with 28 additions and 0 deletions

View file

@ -448,4 +448,29 @@ bool Range::intersects_node(Node const& node) const
return false;
}
// https://dom.spec.whatwg.org/#dom-range-ispointinrange
ExceptionOr<bool> Range::is_point_in_range(Node const& node, u32 offset) const
{
// 1. If nodes root is different from thiss root, return false.
if (&node.root() != &root())
return false;
// 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if (is<DocumentType>(node))
return InvalidNodeTypeError::create("Node cannot be a DocumentType.");
// 3. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException.
if (offset > node.length())
return IndexSizeError::create(String::formatted("Node does not contain a child at offset {}", offset));
// 4. If (node, offset) is before start or after end, return false.
auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset);
auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset);
if (relative_position_to_start == RelativeBoundaryPointPosition::Before || relative_position_to_end == RelativeBoundaryPointPosition::After)
return false;
// 5. Return true.
return true;
}
}