1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:08:10 +00:00

LibWeb: Implement Range.intersectsNode

This commit is contained in:
Luke Wilde 2022-01-31 19:10:12 +00:00 committed by Andreas Kling
parent 4c08757ff9
commit 386ee5ab17
3 changed files with 31 additions and 0 deletions

View file

@ -421,4 +421,31 @@ NonnullRefPtr<Node> Range::common_ancestor_container() const
return container;
}
// https://dom.spec.whatwg.org/#dom-range-intersectsnode
bool Range::intersects_node(Node const& node) const
{
// 1. If nodes root is different from thiss root, return false.
if (&node.root() != &root())
return false;
// 2. Let parent be nodes parent.
auto* parent = node.parent();
// 3. If parent is null, return true.
if (!parent)
return true;
// 4. Let offset be nodes index.
auto offset = node.index();
// 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset);
auto relative_position_to_start = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset + 1, m_start_container, m_start_offset);
if (relative_position_to_end == RelativeBoundaryPointPosition::Before && relative_position_to_start == RelativeBoundaryPointPosition::After)
return true;
// 6. Return false.
return false;
}
}

View file

@ -57,6 +57,8 @@ public:
// Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
}
bool intersects_node(Node const&) const;
private:
explicit Range(Document&);

View file

@ -27,4 +27,6 @@ interface Range : AbstractRange {
Range cloneRange();
undefined detach();
boolean intersectsNode(Node node);
};