From 394cd774678e96f59724698296771aefc0342f34 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 21 Mar 2022 16:29:19 +0100 Subject: LibWeb: Implement stringifier for DOM Range :^) --- Userland/Libraries/LibWeb/DOM/Range.cpp | 30 ++++++++++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Range.h | 2 ++ Userland/Libraries/LibWeb/DOM/Range.idl | 2 ++ 3 files changed, 34 insertions(+) (limited to 'Userland/Libraries/LibWeb/DOM') diff --git a/Userland/Libraries/LibWeb/DOM/Range.cpp b/Userland/Libraries/LibWeb/DOM/Range.cpp index a989de8432..ce20127b3c 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.cpp +++ b/Userland/Libraries/LibWeb/DOM/Range.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace Web::DOM { @@ -498,4 +499,33 @@ ExceptionOr Range::compare_point(Node const& node, u32 offset) const return 0; } +// https://dom.spec.whatwg.org/#dom-range-stringifier +String Range::to_string() const +{ + // 1. Let s be the empty string. + StringBuilder builder; + + // 2. If this’s start node is this’s end node and it is a Text node, + // then return the substring of that Text node’s data beginning at this’s start offset and ending at this’s end offset. + if (start_container() == end_container() && is(*start_container())) + return static_cast(*start_container()).data().substring(start_offset(), end_offset()); + + // 3. If this’s start node is a Text node, then append the substring of that node’s data from this’s start offset until the end to s. + if (is(*start_container())) + builder.append(static_cast(*start_container()).data().substring_view(start_offset())); + + // 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s. + for (Node const* node = start_container()->next_in_pre_order(); node && node != end_container(); node = node->next_in_pre_order()) { + if (is(*node)) + builder.append(static_cast(*node).data()); + } + + // 5. If this’s end node is a Text node, then append the substring of that node’s data from its start until this’s end offset to s. + if (is(*end_container())) + builder.append(static_cast(*end_container()).data().substring_view(0, end_offset())); + + // 6. Return s. + return builder.to_string(); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Range.h b/Userland/Libraries/LibWeb/DOM/Range.h index 64047345d1..9fd7562135 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.h +++ b/Userland/Libraries/LibWeb/DOM/Range.h @@ -61,6 +61,8 @@ public: ExceptionOr is_point_in_range(Node const&, u32 offset) const; ExceptionOr compare_point(Node const&, u32 offset) const; + String to_string() const; + private: explicit Range(Document&); diff --git a/Userland/Libraries/LibWeb/DOM/Range.idl b/Userland/Libraries/LibWeb/DOM/Range.idl index 900c9ceec0..966de1cbe6 100644 --- a/Userland/Libraries/LibWeb/DOM/Range.idl +++ b/Userland/Libraries/LibWeb/DOM/Range.idl @@ -32,4 +32,6 @@ interface Range : AbstractRange { boolean intersectsNode(Node node); + stringifier; + }; -- cgit v1.2.3