diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-01-31 17:35:03 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-26 12:53:32 +0100 |
commit | d73fb7e10f58a82a8700809530226bc51e237562 (patch) | |
tree | 1b6a370a137baec095d78a905a70c545ce2a80c4 /Userland/Libraries/LibWeb | |
parent | 46ce50f74eb453178661a7e2a405b69b6fc51234 (diff) | |
download | serenity-d73fb7e10f58a82a8700809530226bc51e237562.zip |
LibWeb: Implement Range.set{Start,End}{Before,After}
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.cpp | 56 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Range.idl | 4 |
3 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp index 447e155659..dae53a5c32 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.cpp +++ b/Userland/Libraries/LibWeb/DOM/Range.cpp @@ -171,6 +171,62 @@ ExceptionOr<void> Range::set_end(Node& node, u32 offset) return set_start_or_end(node, offset, StartOrEnd::End); } +// https://dom.spec.whatwg.org/#dom-range-setstartbefore +ExceptionOr<void> Range::set_start_before(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. Set the start of this to boundary point (parent, node’s index). + return set_start_or_end(*parent, node.index(), StartOrEnd::Start); +} + +// https://dom.spec.whatwg.org/#dom-range-setstartafter +ExceptionOr<void> Range::set_start_after(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. Set the start of this to boundary point (parent, node’s index plus 1). + return set_start_or_end(*parent, node.index() + 1, StartOrEnd::Start); +} + +// https://dom.spec.whatwg.org/#dom-range-setendbefore +ExceptionOr<void> Range::set_end_before(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. Set the end of this to boundary point (parent, node’s index). + return set_start_or_end(*parent, node.index(), StartOrEnd::End); +} + +// https://dom.spec.whatwg.org/#dom-range-setendafter +ExceptionOr<void> Range::set_end_after(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. Set the end of this to boundary point (parent, node’s index plus 1). + return set_start_or_end(*parent, node.index() + 1, StartOrEnd::End); +} + 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 06fed39b5d..b145a30f98 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.h +++ b/Userland/Libraries/LibWeb/DOM/Range.h @@ -26,6 +26,10 @@ public: ExceptionOr<void> set_start(Node& node, u32 offset); ExceptionOr<void> set_end(Node& node, u32 offset); + ExceptionOr<void> set_start_before(Node& node); + ExceptionOr<void> set_start_after(Node& node); + ExceptionOr<void> set_end_before(Node& node); + ExceptionOr<void> set_end_after(Node& node); NonnullRefPtr<Range> inverted() const; NonnullRefPtr<Range> normalized() const; diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl index 4597df43c3..792abf3f6a 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.idl +++ b/Userland/Libraries/LibWeb/DOM/Range.idl @@ -10,6 +10,10 @@ interface Range : AbstractRange { undefined setStart(Node node, unsigned long offset); undefined setEnd(Node node, unsigned long offset); + undefined setStartBefore(Node node); + undefined setStartAfter(Node node); + undefined setEndBefore(Node node); + undefined setEndAfter(Node node); Range cloneRange(); |