浏览代码

LibJS: Use Unicode normalization within String.prototype.normalize

Timothy Flynn 2 年之前
父节点
当前提交
7fc03e8967

+ 4 - 2
Userland/Libraries/LibJS/Runtime/StringPrototype.cpp

@@ -28,6 +28,7 @@
 #include <LibJS/Runtime/Value.h>
 #include <LibLocale/Locale.h>
 #include <LibUnicode/CharacterTypes.h>
+#include <LibUnicode/Normalize.h>
 #include <string.h>
 
 namespace JS {
@@ -850,8 +851,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::normalize)
     if (!form.is_one_of("NFC"sv, "NFD"sv, "NFKC"sv, "NFKD"sv))
         return vm.throw_completion<RangeError>(ErrorType::InvalidNormalizationForm, form);
 
-    // FIXME: 6. Let ns be the String value that is the result of normalizing S into the normalization form named by f as specified in https://unicode.org/reports/tr15/.
-    auto ns = string;
+    // 6. Let ns be the String value that is the result of normalizing S into the normalization form named by f as specified in https://unicode.org/reports/tr15/.
+    auto unicode_form = Unicode::normalization_form_from_string(form);
+    auto ns = Unicode::normalize(string, unicode_form);
 
     // 7. return ns.
     return js_string(vm, move(ns));

+ 2 - 3
Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.normalize.js

@@ -36,8 +36,7 @@ test("Invalid object throws", () => {
     );
 });
 
-// Tests below here are skipped due to the function being a stub at the moment
-test.skip("Normalization works", () => {
+test("Normalization works", () => {
     var s = "\u1E9B\u0323";
 
     expect(s.normalize("NFC")).toBe("\u1E9B\u0323");
@@ -46,7 +45,7 @@ test.skip("Normalization works", () => {
     expect(s.normalize("NFKD")).toBe("\u0073\u0323\u0307");
 });
 
-test.skip("Default parameter is NFC", () => {
+test("Default parameter is NFC", () => {
     var s = "\u1E9B\u0323";
 
     expect(s.normalize("NFC")).toBe(s.normalize());