Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD for PR #42892] trim leading and trailing CR LF during paste into composer #41265

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,7 @@ const CONST = {
CARD_SECURITY_CODE: /^[0-9]{3,4}$/,
CARD_EXPIRATION_DATE: /^(0[1-9]|1[0-2])([^0-9])?([0-9]{4}|([0-9]{2}))$/,
ROOM_NAME: /^#[\p{Ll}0-9-]{1,100}$/u,
TRAILING_CRLF: /^[\r\n]+|[\r\n]+$/g,

// eslint-disable-next-line max-len, no-misleading-character-class
EMOJI: /[\p{Extended_Pictographic}\u200d\u{1f1e6}-\u{1f1ff}\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}\u20E3\uFE0F]|[#*0-9]\uFE0F?\u20E3/gu,
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useHtmlPaste/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useNavigation} from '@react-navigation/native';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import {useCallback, useEffect} from 'react';
import StringUtils from '@libs/StringUtils';
import type UseHtmlPaste from './types';

const insertByCommand = (text: string) => {
Expand All @@ -12,7 +13,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
if (selection?.rangeCount) {
const range = selection.getRangeAt(0);
range.deleteContents();
const node = document.createTextNode(text);
const node = document.createTextNode(StringUtils.sanitizeTrailingNewline(text));
range.insertNode(node);

// Move caret to the end of the newly inserted text node.
Expand Down
11 changes: 10 additions & 1 deletion src/libs/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function sanitizeString(str: string): string {
return _.deburr(str).toLowerCase().replaceAll(CONST.REGEX.NON_ALPHABETIC_AND_NON_LATIN_CHARS, '');
}

/**
* Remove trailing Carriage Return and Line feed
* @param text - Input text to be sanitized
* @returns Sanitized string
*/
function sanitizeTrailingNewline(text: string): string {
return text.replace(CONST.REGEX.TRAILING_CRLF, '');
}

/**
* Check if the string would be empty if all invisible characters were removed.
*/
Expand Down Expand Up @@ -96,4 +105,4 @@ function lineBreaksToSpaces(text = '') {
return text.replace(CONST.REGEX.LINE_BREAK, ' ');
}

export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeCRLF, getAcronym, lineBreaksToSpaces};
export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeCRLF, getAcronym, lineBreaksToSpaces, sanitizeTrailingNewline};
14 changes: 14 additions & 0 deletions tests/unit/StringUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ describe('StringUtils', () => {
expect(acronym).toBe('HEO');
});
});

describe('sanitizeTrailingNewline', () => {
test('Test trailing newline replacement', () => {
const text = 'Test string.\r\n';
const sanitizedText = 'Test string.';
expect(StringUtils.sanitizeTrailingNewline(text)).toBe(sanitizedText);
});

test('Test leading and trailing newline replacement with multiline text', () => {
const text = '\r\nTest\r\nstring.\r\n';
const sanitizedText = 'Test\r\nstring.';
expect(StringUtils.sanitizeTrailingNewline(text)).toBe(sanitizedText);
});
})
});
Loading