Truly independent web browser
LibJS: Compare ASCII strings in localeCompare() before checking identity
Andreas Kling59ba72c8e53c
diff --git a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs b/Libraries/LibJS/Rust/src/runtime/string_prototype.rsindex 5780359a057..80155d8cc6c 100644--- a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/string_prototype.rs@@ -725,14 +725,15 @@ impl StringPrototype {{let (string, that_value) = (vm.this_value().as_string(), vm.argument(0).as_string());let (view, that_view) = (string.utf16_string_view(), that_value.utf16_string_view());- if view == that_view {- return Ok(Value::from_i32(0));- }+ // NB: The ASCII comparison finds identical strings equal too.if let (Utf16View::Ascii(view), Utf16View::Ascii(that_view)) = (view, that_view)&& let Some(result) = try_fast_ascii_string_compare(view, that_view){return Ok(Value::from_i32(result));}+ if view == that_view {+ return Ok(Value::from_i32(0));+ }// NB: The views are taken again after making the collator, which may collect garbage.let collator = realm.intrinsics().default_collator(vm);return Ok(Value::from_i32(compare_strings(diff --git a/Tests/LibJS/Runtime/builtins/String/String.prototype.localeCompare.js b/Tests/LibJS/Runtime/builtins/String/String.prototype.localeCompare.jsindex 8b4151983ba..6ec1a0bf58b 100644--- a/Tests/LibJS/Runtime/builtins/String/String.prototype.localeCompare.js+++ b/Tests/LibJS/Runtime/builtins/String/String.prototype.localeCompare.js@@ -64,3 +64,12 @@ test("ASCII strings compare like the collator", () => {for (const b of strings) expect(a.localeCompare(b)).toBe(collator.compare(a, b));}});++test("identical strings are equal", () => {+ const strings = ["", "foo123", "a\x01b", "\x00", "á", "\u{1F600}x"];+ for (const string of strings) {+ const copy = [...string].join("");+ expect(string.localeCompare(copy)).toBe(0);+ expect(copy.localeCompare(string)).toBe(0);+ }+});