summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-01-30 21:12:42 +0200
committerIdan Horowitz <idan.horowitz@gmail.com>2022-01-31 21:05:04 +0200
commit366468f1def4adef06f1f945d7fdf31bd612ddd1 (patch)
tree54a0e9d962c7f162df67aa4c4d660d62a567859a
parent96af50bbbaa0b70b5614952e7c16ae91defb2348 (diff)
downloadserenity-366468f1def4adef06f1f945d7fdf31bd612ddd1.zip
LibJS: Implement Intl %SegmentsPrototype%.containing
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp36
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js42
4 files changed, 79 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index a261f00a63..9f0bd564b4 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -105,6 +105,7 @@ namespace JS {
P(console) \
P(construct) \
P(constructor) \
+ P(containing) \
P(compare) \
P(copyWithin) \
P(cos) \
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp
index 6d5870ff7f..e48cdfb46e 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp
@@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Utf16View.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/SegmentIterator.h>
-#include <LibJS/Runtime/Intl/Segments.h>
#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
namespace JS::Intl {
@@ -25,6 +25,40 @@ void SegmentsPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(*vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
+ define_native_function(vm.names.containing, containing, 1, attr);
+}
+
+// 18.5.2.1 %SegmentsPrototype%.containing ( index ), https://tc39.es/ecma402/#sec-%segmentsprototype%.containing
+JS_DEFINE_NATIVE_FUNCTION(SegmentsPrototype::containing)
+{
+ // 1. Let segments be the this value.
+ // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
+ auto* segments = TRY(typed_this_object(global_object));
+
+ // 3. Let segmenter be segments.[[SegmentsSegmenter]].
+ auto const& segmenter = segments->segments_segmenter();
+
+ // 4. Let string be segments.[[SegmentsString]].
+ auto string = segments->segments_string();
+
+ // 5. Let len be the length of string.
+ auto length = string.length_in_code_units();
+
+ // 6. Let n be ? ToIntegerOrInfinity(index).
+ auto n = TRY(vm.argument(0).to_integer_or_infinity(global_object));
+
+ // 7. If n < 0 or n ≥ len, return undefined.
+ if (n < 0 || n >= length)
+ return js_undefined();
+
+ // 8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
+ auto start_index = find_boundary(segmenter, string, n, Direction::Before, segments->boundaries_cache());
+
+ // 9. Let endIndex be ! FindBoundary(segmenter, string, n, after).
+ auto end_index = find_boundary(segmenter, string, n, Direction::After, segments->boundaries_cache());
+
+ // 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex).
+ return create_segment_data_object(global_object, segmenter, string, start_index, end_index);
}
// 18.5.2.2 %SegmentsPrototype% [ @@iterator ] ( ), https://tc39.es/ecma402/#sec-%segmentsprototype%-@@iterator
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h
index 143a4870bb..f8f749a248 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h
@@ -20,6 +20,7 @@ public:
virtual ~SegmentsPrototype() override = default;
private:
+ JS_DECLARE_NATIVE_FUNCTION(containing);
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js
index 0369987c61..4c415c672b 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js
@@ -10,6 +10,48 @@ describe("correct behavior", () => {
);
});
+ test("returns segments object with valid containing method", () => {
+ const string = "hello friends!";
+ const graphemeSegmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
+ const graphemeSegments = graphemeSegmenter.segment(string);
+ const graphemeSegment0 = graphemeSegments.containing(0);
+ expect(graphemeSegment0.segment).toBe("h");
+ expect(graphemeSegment0.index).toBe(0);
+ expect(graphemeSegment0.input).toBe(string);
+ expect(graphemeSegment0.isWordLike).toBeUndefined();
+ const graphemeSegment5 = graphemeSegments.containing(5);
+ expect(graphemeSegment5.segment).toBe(" ");
+ expect(graphemeSegment5.index).toBe(5);
+ expect(graphemeSegment5.input).toBe(string);
+ expect(graphemeSegment5.isWordLike).toBeUndefined();
+
+ const wordSegmenter = new Intl.Segmenter("en", { granularity: "word" });
+ const wordSegments = wordSegmenter.segment(string);
+ const wordSegment0 = wordSegments.containing(0);
+ expect(wordSegment0.segment).toBe("hello");
+ expect(wordSegment0.index).toBe(0);
+ expect(wordSegment0.input).toBe(string);
+ // FIXME: expect(wordSegment0.isWordLike).toBeTrue();
+ const wordSegment5 = wordSegments.containing(5);
+ expect(wordSegment5.segment).toBe(" ");
+ expect(wordSegment5.index).toBe(5);
+ expect(wordSegment5.input).toBe(string);
+ expect(wordSegment5.isWordLike).toBeFalse();
+
+ const sentenceSegmenter = new Intl.Segmenter("en", { granularity: "sentence" });
+ const sentenceSegments = sentenceSegmenter.segment(string);
+ const sentenceSegment0 = sentenceSegments.containing(0);
+ expect(sentenceSegment0.segment).toBe(string);
+ expect(sentenceSegment0.index).toBe(0);
+ expect(sentenceSegment0.input).toBe(string);
+ expect(sentenceSegment0.isWordLike).toBeUndefined();
+ const sentenceSegment5 = sentenceSegments.containing(5);
+ expect(sentenceSegment5.segment).toBe(sentenceSegment0.segment);
+ expect(sentenceSegment5.index).toBe(sentenceSegment0.index);
+ expect(sentenceSegment5.input).toBe(sentenceSegment0.input);
+ expect(sentenceSegment5.isWordLike).toBe(sentenceSegment0.isWordLike);
+ });
+
test("returns segments object segment iterator", () => {
const segmenter = new Intl.Segmenter();
const segments = segmenter.segment("hello friends!");