Skip to content

Commit ee4c728

Browse files
author
Sravan S
authored
fix: Words-breaks happening mid word (#544)
Words were breaking mid word because all white spaces were converted into nbsps. CSS couldnt discern that nbsps are whitespaces, so wrapping didnt work well Fixes: https://sendbird.atlassian.net/browse/UIKIT-3889
1 parent a01cce8 commit ee4c728

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

src/modules/Message/utils/tokens/__tests__/tokenizeUtils.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,46 @@ describe('combineNearbyStrings', () => {
114114
});
115115

116116
describe('getWhiteSpacePreservedText', () => {
117-
it('should keep the leading and trailing white spaces', () => {
117+
it('should convert leading, trailing white space to nbsp', () => {
118118
const text = ' aaa ';
119119
const result = getWhiteSpacePreservedText(text);
120120
expect(result).toEqual('\u00A0aaa\u00A0');
121121
});
122+
123+
it('should convert leading, trailing white space to nbsp', () => {
124+
const text = ' aaa ';
125+
const result = getWhiteSpacePreservedText(text);
126+
expect(result).toEqual('\u00A0\u00A0aaa\u00A0\u00A0');
127+
});
128+
129+
it('should convert leading, trailing white space to nbsp, while preserving space in between', () => {
130+
const text = ' aaa cc dd ';
131+
const result = getWhiteSpacePreservedText(text);
132+
expect(result).toEqual('\u00A0aaa cc dd\u00A0');
133+
});
134+
122135
it('should keep the new lines', () => {
123136
const text = ' aaa\naa';
124137
const result = getWhiteSpacePreservedText(text);
125138
expect(result).toEqual('\u00A0aaa\naa');
126139
});
140+
141+
it('should add the padding after new lines', () => {
142+
const text = 'line1\n line2_with_prefix_space\n line3_with_prefix_space\nline4';
143+
const expected = 'line1\n\u00A0\u00A0line2_with_prefix_space\n\u00A0line3_with_prefix_space\nline4';
144+
const result = getWhiteSpacePreservedText(text);
145+
expect(result).toEqual(expected);
146+
});
147+
148+
it('should keep the tabs', () => {
149+
const text = ' aaa\taa';
150+
const result = getWhiteSpacePreservedText(text);
151+
expect(result).toEqual('\u00A0aaa\taa');
152+
});
153+
154+
it('should handle empty string', () => {
155+
const text = '';
156+
const result = getWhiteSpacePreservedText(text);
157+
expect(result).toEqual('');
158+
});
127159
});

src/modules/Message/utils/tokens/tokenize.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,27 @@ export function tokenizeMessage({
111111
* Don't need to use this util in DOM element since the white spaces will be kept as is,
112112
* but will need if the text is wrapped \w React.Fragement or </>
113113
* @link https://sendbird.slack.com/archives/GPGHESTL3/p1681180484341369
114+
* Or!!! -> convert any space or tab in leading/trailing to nbsp
115+
* to preserve the leading & trailing white spaces
114116
*/
115117
export function getWhiteSpacePreservedText(text: string): string {
116-
return text
117-
// convert any space or tab into the non-breaking space
118-
// to preserve the leading & trailing white spaces
119-
.replace(/([ \t]+)/g, (_, spaces) => '\u00A0'.repeat(spaces.length));
118+
const NON_BREAKING_SPACE = '\u00A0';
119+
// Split the input string into lines
120+
const lines = text.split('\n');
121+
122+
// Process each line and convert leading and trailing white spaces to "\u00A0"
123+
const processedLines = lines.map((line) => {
124+
const leadingWhitespace = line.match(/^\s*/)?.[0] || '';
125+
const trailingWhitespace = line.match(/\s*$/)?.[0] || '';
126+
127+
const convertedLeadingWhitespace = leadingWhitespace.replace(/ /g, NON_BREAKING_SPACE);
128+
const convertedTrailingWhitespace = trailingWhitespace.replace(/ /g, NON_BREAKING_SPACE);
129+
130+
return convertedLeadingWhitespace + line.trim() + convertedTrailingWhitespace;
131+
});
132+
133+
// Combine the processed lines into a new string with "\n"
134+
const result = processedLines.join('\n');
135+
136+
return result;
120137
}

src/ui/MessageContent/__tests__/__snapshots__/MessageContent.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ exports[`ui/MessageContent should do a snapshot test of the MessageContent DOM 1
5252
<div
5353
class="sendbird-message-content__middle__message-item-body sendbird-text-message-item-body incoming "
5454
>
55-
First second third
55+
First second third
5656
</div>
5757
</span>
5858
<span

src/ui/OpenchannelOGMessage/__tests__/__snapshots__/OpenchannelOGMessage.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ exports[`ui/OpenchannelOGMessage should do a snapshot test of the OpenchannelOGM
6161
<span
6262
class="sendbird-openchannel-og-message__top__right__description__message sendbird-label sendbird-label--body-1 sendbird-label--color-onbackground-1"
6363
>
64-
I am the Message
64+
I am the Message
6565
</span>
6666
</div>
6767
</div>

src/ui/TextMessageItemBody/__tests__/__snapshots__/TextMessageItemBody.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`ui/TextMessageItemBody should do a snapshot test of the TextMessageItem
88
<div
99
class="class-name-for-snapshot sendbird-text-message-item-body outgoing mouse-hover "
1010
>
11-
First second third fourth fifth
11+
First second third fourth fifth
1212
<span
1313
class="sendbird-text-message-item-body__message edited sendbird-label sendbird-label--body-1 sendbird-label--color-oncontent-2"
1414
>

src/ui/TextMessageItemBody/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface Props {
1919
}
2020

2121
export default function TextMessageItemBody({
22-
className,
22+
className = '',
2323
message,
2424
isByMe = false,
2525
mouseHover = false,

src/ui/Word/index.scss

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
.sendbird-word {
44
display: inline;
55
height: fit-content;
6-
.sendbird-word__url,
7-
.sendbird-word__normal {
8-
margin: 0 4px;
9-
display: inline-block;
10-
color: inherit;
11-
}
6+
}
7+
8+
.sendbird-word__url {
9+
margin: 0 4px;
10+
display: inline;
11+
color: inherit;
12+
word-break: break-all;
1213
}

0 commit comments

Comments
 (0)