Skip to content

Commit 8f8390b

Browse files
authored
fix: remove lookbehind pattern in getWhiteSpacePreservedText (#512)
### Description Of Changes Resolves [UIKIT-3792](https://sendbird.atlassian.net/browse/UIKIT-3792) Updated a Regex in `getWhiteSpacePreservedText` util not to be crashed in Safari. See: https://caniuse.com/js-regexp-lookbehind https://stackoverflow.com/a/51568859
1 parent 2002bfc commit 8f8390b

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/smart-components/Message/utils/tokens/__tests__/tokenizeUtils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ describe('getWhiteSpacePreservedText', () => {
107107
it('should keep the leading and trailing white spaces', () => {
108108
const text = ' aaa ';
109109
const result = getWhiteSpacePreservedText(text);
110-
expect(result).toEqual(' aaa\u00A0');
110+
expect(result).toEqual('\u00A0aaa\u00A0');
111111
});
112112
it('should keep the new lines', () => {
113113
const text = ' aaa\naa';
114114
const result = getWhiteSpacePreservedText(text);
115-
expect(result).toEqual(' aaa\naa');
115+
expect(result).toEqual('\u00A0aaa\naa');
116116
});
117117
});

src/smart-components/Message/utils/tokens/tokenize.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ export function tokenizeMessage({
100100
}
101101

102102
/**
103-
* To preserve the original text which has
104-
* leading & trailing white spaces & new-lines in the middle
103+
* Don't need to use this util in DOM element since the white spaces will be kept as is,
104+
* but will need if the text is wrapped \w React.Fragement or </>
105105
* @link https://sendbird.slack.com/archives/GPGHESTL3/p1681180484341369
106106
*/
107107
export function getWhiteSpacePreservedText(text: string): string {
108108
return text
109109
// convert any space or tab into the non-breaking space
110110
// to preserve the leading & trailing white spaces
111-
.replace(/(?<!^)[ \t]+/g, '\u00A0')
112-
// and keep the new line as well
113-
.replace(/\n/g, '\n');
111+
.replace(/([ \t]+)/g, (_, spaces) => '\u00A0'.repeat(spaces.length))
114112
}

0 commit comments

Comments
 (0)