Skip to content

Commit 35e6b14

Browse files
committed
fix infinite loop when there are no white space characters in text to wrap
1 parent 1a04c20 commit 35e6b14

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/render/src/shared/utils/pretty.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,32 +181,35 @@ export const wrapText = (
181181
maxLineLength: number,
182182
lineBreak: string,
183183
): string => {
184+
if (!text.includes(' ')) {
185+
return `${linePrefix}${text}`;
186+
}
184187
let wrappedText = linePrefix + text;
185-
let nextLineStartIndex = 0;
186-
while (wrappedText.length - nextLineStartIndex > maxLineLength) {
188+
let currentLineStartIndex = 0;
189+
while (wrappedText.length - currentLineStartIndex > maxLineLength) {
187190
const overflowingCharacterIndex = Math.min(
188-
nextLineStartIndex + maxLineLength - 1,
191+
currentLineStartIndex + maxLineLength - 1,
189192
wrappedText.length,
190193
);
191-
for (let i = overflowingCharacterIndex; i >= nextLineStartIndex; i--) {
194+
for (let i = overflowingCharacterIndex; i >= currentLineStartIndex; i--) {
192195
const char = wrappedText[i];
193196
if (char === ' ') {
194197
wrappedText =
195198
wrappedText.slice(0, i) +
196199
lineBreak +
197200
linePrefix +
198201
wrappedText.slice(i + 1);
199-
nextLineStartIndex = lineBreak.length + linePrefix.length + i;
202+
currentLineStartIndex = lineBreak.length + linePrefix.length + i;
200203
break;
201204
}
202-
if (i === nextLineStartIndex) {
203-
const nextSpaceIndex = wrappedText.indexOf(' ', nextLineStartIndex);
205+
if (i === currentLineStartIndex) {
206+
const nextSpaceIndex = wrappedText.indexOf(' ', currentLineStartIndex);
204207
wrappedText =
205208
wrappedText.slice(0, nextSpaceIndex) +
206209
lineBreak +
207210
linePrefix +
208211
wrappedText.slice(nextSpaceIndex + 1);
209-
nextLineStartIndex =
212+
currentLineStartIndex =
210213
lineBreak.length + linePrefix.length + nextSpaceIndex;
211214
}
212215
}

0 commit comments

Comments
 (0)