[FIX] Add support to non ascii url characters on ios devices (#4364)

* add support to non ascii url caracters on ios

* rename module and add tests

* remove last slice
This commit is contained in:
Gleidson Daniel Silva 2022-07-28 10:36:40 -03:00 committed by GitHub
parent 376c8e8252
commit a965465af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 2 deletions

View File

@ -34,3 +34,4 @@ export * from './userPreferencesMethods';
export * from './crashReport';
export * from './parseSettings';
export * from './subscribeRooms';
export * from './serializeAsciiUrl';

View File

@ -0,0 +1,30 @@
import { serializeAsciiUrl } from '.';
const ASCIIUrl = 'https://чат24.рф';
const NonASCIIUrl = 'open.rocket.chat';
const ASCIIUrlSerialized = 'https://xn--24-6kc6exa.xn--p1ai';
describe('Serialize ASCII url on ios', () => {
jest.mock('react-native', () => ({ Platform: { OS: 'ios' } }));
test('ASCII url', () => {
const result = serializeAsciiUrl(ASCIIUrl);
expect(result).toBe(ASCIIUrlSerialized);
});
test('Non ASCII url', () => {
const result = serializeAsciiUrl(NonASCIIUrl);
expect(result).toBe(NonASCIIUrl);
});
});
describe('Serialize ASCII url on android', () => {
jest.mock('react-native', () => ({ Platform: { OS: 'android' } }));
// By default android converts ASCII addresses
// test('ASCII url', () => {
// const result = serializeAsciiUrl(ASCIIUrl);
// expect(result).toBe('filename.png');
// });
test('Non ASCII url', () => {
const result = serializeAsciiUrl(NonASCIIUrl);
expect(result).toBe(NonASCIIUrl);
});
});

View File

@ -0,0 +1,13 @@
import * as uri from 'uri-js';
import { isIOS } from './helpers';
export const serializeAsciiUrl = (url: string): string => {
let newUrl = url;
const ascii = /^[ -~\t\n\r]+$/;
if (isIOS && !ascii.test(newUrl)) {
newUrl = uri.serialize(uri.parse(url));
newUrl = newUrl.charAt(newUrl.length - 1) === '/' ? newUrl.slice(0, -1) : newUrl;
}
return newUrl;
};

View File

@ -31,6 +31,7 @@ import { moderateScale, verticalScale } from './scaling';
import SSLPinning from '../../lib/methods/helpers/sslPinning';
import sharedStyles from '../Styles';
import ServerInput from './ServerInput';
import { serializeAsciiUrl } from '../../lib/methods';
const styles = StyleSheet.create({
onboardingImage: {
@ -257,8 +258,7 @@ class NewServerView extends React.Component<INewServerViewProps, INewServerViewS
url = `https://${url}`;
}
}
return url.replace(/\/+$/, '').replace(/\\/g, '/');
return serializeAsciiUrl(url.replace(/\/+$/, '').replace(/\\/g, '/'));
};
uriToPath = (uri: string) => uri.replace('file://', '');

View File

@ -133,6 +133,7 @@
"rn-root-view": "1.0.3",
"semver": "7.3.5",
"ua-parser-js": "^1.0.2",
"uri-js": "^4.4.1",
"url-parse": "1.5.10",
"use-deep-compare-effect": "1.6.1",
"xregexp": "5.0.2"

View File

@ -18170,6 +18170,13 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"
uri-js@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"
integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==
dependencies:
punycode "^2.1.0"
urix@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"