[Mojo] Fix conversion js strings to/from mojo String16 for Unicode.
- stringToMojoString16 - this method was pulling the wrong character codes for multi code point characters, resulting in improper output - instead, iterate over each code point to get the correct output. - mojoString16ToString - Instead of using to fromcodepoint, use fromCharCode since thats what mojo accepts, Tests are provided which convert emojis including graphemes back and forth. Change-Id: Iaa7bff70553a30f59772949aac4fb8ea31569ec1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5027656 Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org> Reviewed-by: Ken Rockot <rockot@google.com> Reviewed-by: Eshwar Stalin <estalin@chromium.org> Auto-Submit: David Pennington <dpenning@chromium.org> Commit-Queue: David Pennington <dpenning@chromium.org> Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com> Cr-Commit-Position: refs/heads/main@{#1225732}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
375eb24dd4
commit
dc05807ce7
@ -18,6 +18,19 @@ suite('MojoTypeUtilTest', () => {
|
||||
assertEquals(mojoString16ToString({data: [0x4f60, 0x597d]}), '你好');
|
||||
});
|
||||
|
||||
test('emojis', () => {
|
||||
assertEquals('❤️', mojoString16ToString(stringToMojoString16('❤️')));
|
||||
assertEquals(
|
||||
'👨👨👦',
|
||||
mojoString16ToString(stringToMojoString16('👨👨👦')));
|
||||
assertEquals('🇯🇵', mojoString16ToString(stringToMojoString16('🇯🇵')));
|
||||
assertEquals('🇺🇳', mojoString16ToString(stringToMojoString16('🇺🇳')));
|
||||
assertEquals(
|
||||
'👨👨👦🇯🇵👨👨👦a你❤️👨👨👦',
|
||||
mojoString16ToString(stringToMojoString16(
|
||||
'👨👨👦🇯🇵👨👨👦a你❤️👨👨👦')));
|
||||
});
|
||||
|
||||
test('Can convert strings to mojo Urls', () => {
|
||||
assertDeepEquals(stringToMojoUrl(''), {url: ''});
|
||||
assertDeepEquals(
|
||||
|
@ -5,12 +5,18 @@
|
||||
import {String16} from 'chrome://resources/mojo/mojo/public/mojom/base/string16.mojom-webui.js';
|
||||
import {Url} from 'chrome://resources/mojo/url/mojom/url.mojom-webui.js';
|
||||
|
||||
export function stringToMojoString16(s: string): String16 {
|
||||
return {data: Array.from(s, c => c.charCodeAt(0))};
|
||||
// Convert a javascript string into a Mojo String16.
|
||||
export function stringToMojoString16(str: string): String16 {
|
||||
const arr: number[] = [];
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
arr.push(str.charCodeAt(i));
|
||||
}
|
||||
return {data: arr};
|
||||
}
|
||||
|
||||
// Convert a Mojo String16 into a javascript string.
|
||||
export function mojoString16ToString(str16: String16): string {
|
||||
return str16.data.map((ch: number) => String.fromCodePoint(ch)).join('');
|
||||
return String.fromCharCode(...str16.data);
|
||||
}
|
||||
|
||||
// Note: This does not do any validation of the URL string.
|
||||
|
Reference in New Issue
Block a user