diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-01-31 19:10:12 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-26 12:53:32 +0100 |
commit | 386ee5ab17f42863485abef062ebb677c3ca3326 (patch) | |
tree | 5d6a81f572cff5a356b194bacb6901a698c772a7 /Userland | |
parent | 4c08757ff9a732313359039731a53e2271856183 (diff) | |
download | serenity-386ee5ab17f42863485abef062ebb677c3ca3326.zip |
LibWeb: Implement Range.intersectsNode
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.cpp | 27 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.idl | 2 |
3 files changed, 31 insertions, 0 deletions
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<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 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); + }; |