From 386ee5ab17f42863485abef062ebb677c3ca3326 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Mon, 31 Jan 2022 19:10:12 +0000 Subject: [PATCH] LibWeb: Implement Range.intersectsNode --- Userland/Libraries/LibWeb/DOM/Range.cpp | 27 +++++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Range.h | 2 ++ Userland/Libraries/LibWeb/DOM/Range.idl | 2 ++ 3 files changed, 31 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp index 0ba35e2499..7d4178f601 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.cpp +++ b/Userland/Libraries/LibWeb/DOM/Range.cpp @@ -421,4 +421,31 @@ NonnullRefPtr 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 node’s root is different from this’s root, return false. + if (&node.root() != &root()) + return false; + + // 2. Let parent be node’s parent. + auto* parent = node.parent(); + + // 3. If parent is null, return true. + if (!parent) + return true; + + // 4. Let offset be node’s 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; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h index 6b62b1d44c..2d2b986d34 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.h +++ b/Userland/Libraries/LibWeb/DOM/Range.h @@ -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&); diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl index bcede22cf8..17595288bf 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.idl +++ b/Userland/Libraries/LibWeb/DOM/Range.idl @@ -27,4 +27,6 @@ interface Range : AbstractRange { Range cloneRange(); undefined detach(); + boolean intersectsNode(Node node); + };