Skip to content

Commit

Permalink
Merge pull request #30559 from HardikChoudhary24/fix/input-error
Browse files Browse the repository at this point in the history
input of '[' and other special characters fixed

(cherry picked from commit d651cab)
  • Loading branch information
neil-marcellini authored and OSBotify committed Oct 31, 2023
1 parent d3e8a1e commit 0416a66
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/libs/convertToLTRForComposer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ function hasRTLCharacters(text: string): boolean {

// Converts a given text to ensure it starts with the LTR (Left-to-Right) marker.
const convertToLTRForComposer: ConvertToLTRForComposer = (text) => {
// Ensure that the text starts with RTL characters if not we return the same text to avoid concatination with special character at the start which leads to unexpected behaviour for Emoji/Mention suggestions.
// Ensure that the text starts with RTL characters if not we return the same text to avoid concatination with special
// character at the start which leads to unexpected behaviour for Emoji/Mention suggestions.
if (!hasRTLCharacters(text)) {
// If text is empty string return empty string to avoid an empty draft due to special character.
if (text === '' || CONST.UNICODE.LTR.match(text)) {
return '';
}
return text;
return text.replace(CONST.UNICODE.LTR, '');
}

// Check if the text contains only spaces. If it does, we do not concatenate it with CONST.UNICODE.LTR,
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/ConvertToLTRForComposerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import convertToLTRForComposer from '@libs/convertToLTRForComposer';
import CONST from '@src/CONST';

describe('convertToLTRForComposer', () => {
test('Input without RTL characters remains unchanged', () => {
// Test when input contains no RTL characters
const inputText = 'Hello, world!';
const result = convertToLTRForComposer(inputText);
expect(result).toBe(inputText);
});

test('Input with RTL characters is prefixed with LTR marker', () => {
// Test when input contains RTL characters
const inputText = 'مثال';
const result = convertToLTRForComposer(inputText);
expect(result).toBe(`${CONST.UNICODE.LTR}${inputText}`);
});

test('Input with mixed RTL and LTR characters is prefixed with LTR marker', () => {
// Test when input contains mix of RTL and LTR characters
const inputText = 'مثال test ';
const result = convertToLTRForComposer(inputText);
expect(result).toBe(`${CONST.UNICODE.LTR}${inputText}`);
});

test('Input with only space remains unchanged', () => {
// Test when input contains only spaces
const inputText = ' ';
const result = convertToLTRForComposer(inputText);
expect(result).toBe(inputText);
});

test('Input with existing LTR marker remains unchanged', () => {
// Test when input already starts with the LTR marker
const inputText = `${CONST.UNICODE.LTR}مثال`;
const result = convertToLTRForComposer(inputText);
expect(result).toBe(inputText);
});

test('Input starting with native emojis remains unchanged', () => {
// Test when input starts with the native emojis
const inputText = '🧶';
const result = convertToLTRForComposer(inputText);
expect(result).toBe(inputText);
});

test('Input is empty', () => {
// Test when input is empty to check for draft comments
const inputText = '';
const result = convertToLTRForComposer(inputText);
expect(result.length).toBe(0);
});

test('input with special characters remains unchanged', () => {
// Test when input contains special characters
const specialCharacters = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '{', '}', '[', ']', '"', ':', ';', '<', '>', '?', '`', '~'];
specialCharacters.forEach((character) => {
const result = convertToLTRForComposer(character);
expect(result).toBe(character);
});
});
});

0 comments on commit 0416a66

Please sign in to comment.