summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2022-01-31 18:33:41 +0000
committerAndreas Kling <kling@serenityos.org>2022-02-26 12:53:32 +0100
commitdfdc2ddb9eb45aa305a13511eed9dd417455e5a8 (patch)
tree0caa7bf3b54fee0df10c0c3696baad9b1a49e01f /Userland/Libraries/LibWeb/DOM
parenta26f1b2ff953f441438b4415b7aa722ec523d2d0 (diff)
downloadserenity-dfdc2ddb9eb45aa305a13511eed9dd417455e5a8.zip
LibWeb: Implement Range.selectNode
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.cpp31
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.h2
-rw-r--r--Userland/Libraries/LibWeb/DOM/Range.idl1
3 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp
index 9c4c25ca8d..e30c2b519e 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Range.cpp
@@ -314,6 +314,37 @@ ExceptionOr<i16> Range::compare_boundary_points(u16 how, Range const& source_ran
}
}
+// https://dom.spec.whatwg.org/#concept-range-select
+ExceptionOr<void> Range::select(Node& node)
+{
+ // 1. Let parent be node’s parent.
+ auto* parent = node.parent();
+
+ // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
+ if (!parent)
+ return InvalidNodeTypeError::create("Given node has no parent.");
+
+ // 3. Let index be node’s index.
+ auto index = node.index();
+
+ // 4. Set range’s start to boundary point (parent, index).
+ m_start_container = *parent;
+ m_start_offset = index;
+
+ // 5. Set range’s end to boundary point (parent, index plus 1).
+ m_end_container = *parent;
+ m_end_offset = index + 1;
+
+ return {};
+}
+
+// https://dom.spec.whatwg.org/#dom-range-selectnode
+ExceptionOr<void> Range::select_node(Node& node)
+{
+ // The selectNode(node) method steps are to select node within this.
+ return select(node);
+}
+
NonnullRefPtr<Range> Range::clone_range() const
{
return adopt_ref(*new Range(const_cast<Node&>(*m_start_container), m_start_offset, const_cast<Node&>(*m_end_container), m_end_offset));
diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h
index e23814e428..9b10cf2f4c 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.h
+++ b/Userland/Libraries/LibWeb/DOM/Range.h
@@ -30,6 +30,7 @@ public:
ExceptionOr<void> set_start_after(Node& node);
ExceptionOr<void> set_end_before(Node& node);
ExceptionOr<void> set_end_after(Node& node);
+ ExceptionOr<void> select_node(Node& node);
// https://dom.spec.whatwg.org/#dom-range-start_to_start
enum HowToCompareBoundaryPoints : u16 {
@@ -61,6 +62,7 @@ private:
};
ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end);
+ ExceptionOr<void> select(Node& node);
};
}
diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl
index 0faaddfcc9..d5fb24f1a7 100644
--- a/Userland/Libraries/LibWeb/DOM/Range.idl
+++ b/Userland/Libraries/LibWeb/DOM/Range.idl
@@ -14,6 +14,7 @@ interface Range : AbstractRange {
undefined setStartAfter(Node node);
undefined setEndBefore(Node node);
undefined setEndAfter(Node node);
+ undefined selectNode(Node node);
const unsigned short START_TO_START = 0;
const unsigned short START_TO_END = 1;