summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2022-02-01 18:45:34 +0000
committerAndreas Kling <kling@serenityos.org>2022-02-26 12:53:32 +0100
commit62b76e06582b3d620813182e09ec8d0a23284544 (patch)
treeb92910a9973c3d77b141294f55cfc401c12b5f6f /Userland/Libraries/LibWeb
parent386ee5ab17f42863485abef062ebb677c3ca3326 (diff)
downloadserenity-62b76e06582b3d620813182e09ec8d0a23284544.zip
LibWeb: Implement Range.isPointInRange
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.cpp25
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.h1
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.idl2
3 files changed, 28 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp
index 7d4178f601..28b7097efe 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Range.cpp
@@ -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 node’s root is different from this’s 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 node’s 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;
+}
+
}
diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h
index 2d2b986d34..0b28978e8c 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.h
+++ b/Userland/Libraries/LibWeb/DOM/Range.h
@@ -58,6 +58,7 @@ public:
}
bool intersects_node(Node const&) const;
+ ExceptionOr<bool> is_point_in_range(Node const&, u32 offset) const;
private:
explicit Range(Document&);
diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl
index 17595288bf..750ed462e1 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.idl
+++ b/Userland/Libraries/LibWeb/DOM/Range.idl
@@ -27,6 +27,8 @@ interface Range : AbstractRange {
Range cloneRange();
undefined detach();
+ boolean isPointInRange(Node node, unsigned long offset);
+
boolean intersectsNode(Node node);
};