Skip to content

Commit

Permalink
Bug 1881078 - Add Services.intl.stringHasRTLChars . r=jfkthame.
Browse files Browse the repository at this point in the history
  • Loading branch information
nchevobbe committed Feb 23, 2024
1 parent c40b60b commit 9dd2d5f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions toolkit/components/mozintl/MozIntlHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MozIntlHelper.h"
#include "nsBidiUtils.h"
#include "nsJSUtils.h"
#include "jsapi.h"
#include "js/experimental/Intl.h" // JS::AddMozDateTimeFormatConstructor
#include "js/PropertyAndElement.h" // JS_DefineFunctions
Expand Down Expand Up @@ -94,3 +96,20 @@ MozIntlHelper::AddDisplayNamesConstructor(JS::Handle<JS::Value> val,

return NS_OK;
}

NS_IMETHODIMP
MozIntlHelper::StringHasRTLChars(JS::Handle<JS::Value> str, JSContext* cx,
bool* res) {
if (!str.isString()) {
return NS_ERROR_INVALID_ARG;
}

nsAutoJSString string;
if (!string.init(cx, str)) {
return NS_ERROR_FAILURE;
}

*res = HasRTLChars(
Span(static_cast<const char16_t*>(string.get()), string.Length()));
return NS_OK;
}
2 changes: 2 additions & 0 deletions toolkit/components/mozintl/mozIMozIntl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ interface mozIMozIntl : nsISupports
*/
jsval getScriptDirection(in jsval locale);

boolean stringHasRTLChars(in jsval str);

readonly attribute jsval Collator;
readonly attribute jsval DateTimeFormat;
readonly attribute jsval DisplayNames;
Expand Down
2 changes: 2 additions & 0 deletions toolkit/components/mozintl/mozIMozIntlHelper.idl
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ interface mozIMozIntlHelper : nsISupports
* dayPeriod: a string from the set {"am", "pm"}.
*/
[implicit_jscontext] void addDisplayNamesConstructor(in jsval intlObject);

[implicit_jscontext] boolean stringHasRTLChars(in jsval str);
};
4 changes: 4 additions & 0 deletions toolkit/components/mozintl/mozIntl.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@ export class MozIntl {
return "ltr";
}

stringHasRTLChars(str) {
return mozIntlHelper.stringHasRTLChars(str);
}

get DateTimeFormat() {
if (!this._cache.hasOwnProperty("DateTimeFormat")) {
mozIntlHelper.addDateTimeFormatConstructor(this._cache);
Expand Down
21 changes: 21 additions & 0 deletions toolkit/components/mozintl/test/test_mozintl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function run_test() {
test_rtf_formatBestUnit();
test_datetimeformat();
test_getLanguageDirection();
test_stringHasRTLChars();

ok(true);
}
Expand Down Expand Up @@ -181,3 +182,23 @@ function test_getLanguageDirection() {
equal(Services.intl.getScriptDirection("en-US"), "ltr");
equal(Services.intl.getScriptDirection("fr"), "ltr");
}

function test_stringHasRTLChars() {
equal(Services.intl.stringHasRTLChars(""), false);
equal(Services.intl.stringHasRTLChars("a"), false);
equal(Services.intl.stringHasRTLChars("أهلا"), true);
equal(Services.intl.stringHasRTLChars(">\u202e<"), true);

const invalidArgs = [undefined, null, false, 42, {}];
for (const invalidArg of invalidArgs) {
try {
Services.intl.stringHasRTLChars(invalidArg);
ok(
false,
`stringHasRTLChars should throw when called with ${invalidArg}`
);
} catch (e) {
ok(true, `stringHasRTLChars throws when called with ${invalidArg}`);
}
}
}

0 comments on commit 9dd2d5f

Please sign in to comment.