Skip to content

Commit e99f5f6

Browse files
committed
toLocaleLowerCase and toLocaleUpperCase do not throw on empty string
https://bugs.webkit.org/show_bug.cgi?id=250903 rdar://104568214 Reviewed by Alexey Shvayka. This patch aligns our toLocaleLowerCase and toLocaleUpperCase for invalid locale tag + empty string edge case to the spec. We should optimize empty string case only when locale is not specified. * JSTests/stress/empty-string-locale-case-convert.js: Added. (shouldThrow): * Source/JavaScriptCore/runtime/StringPrototype.cpp: (JSC::toLocaleCase): Canonical link: https://commits.webkit.org/259242@main
1 parent e73104b commit e99f5f6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function shouldThrow(func, errorMessage) {
2+
var errorThrown = false;
3+
var error = null;
4+
try {
5+
func();
6+
} catch (e) {
7+
errorThrown = true;
8+
error = e;
9+
}
10+
if (!errorThrown)
11+
throw new Error('not thrown');
12+
if (String(error) !== errorMessage)
13+
throw new Error(`bad error: ${String(error)}`);
14+
}
15+
16+
shouldThrow(() => {
17+
'a'.toLocaleLowerCase('');
18+
}, `RangeError: invalid language tag: `);
19+
shouldThrow(() => {
20+
'a'.toLocaleUpperCase('');
21+
}, `RangeError: invalid language tag: `);
22+
shouldThrow(() => {
23+
''.toLocaleLowerCase('');
24+
}, `RangeError: invalid language tag: `);
25+
shouldThrow(() => {
26+
''.toLocaleUpperCase('');
27+
}, `RangeError: invalid language tag: `);

Source/JavaScriptCore/runtime/StringPrototype.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,12 +1370,14 @@ static EncodedJSValue toLocaleCase(JSGlobalObject* globalObject, CallFrame* call
13701370
// 3. ReturnIfAbrupt(S).
13711371
RETURN_IF_EXCEPTION(scope, encodedJSValue());
13721372

1373+
JSValue localeValue = callFrame->argument(0);
1374+
13731375
// Optimization for empty strings.
1374-
if (s.isEmpty())
1376+
if (s.isEmpty() && localeValue.isUndefined())
13751377
return JSValue::encode(sVal);
13761378

13771379
// 4. Let requestedLocales be CanonicalizeLocaleList(locales).
1378-
Vector<String> requestedLocales = canonicalizeLocaleList(globalObject, callFrame->argument(0));
1380+
Vector<String> requestedLocales = canonicalizeLocaleList(globalObject, localeValue);
13791381

13801382
// 5. ReturnIfAbrupt(requestedLocales).
13811383
RETURN_IF_EXCEPTION(scope, encodedJSValue());

0 commit comments

Comments
 (0)