Skip to content

Commit

Permalink
fix: avoid while cycle in computeMaxFontSize for big Number run forev…
Browse files Browse the repository at this point in the history
…er when css rule applied (#20173)
  • Loading branch information
diegomedina248 committed May 25, 2022
1 parent b0c6935 commit 365acee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ function decreaseSizeUntil(
): number {
let size = startSize;
let dimension = computeDimension(size);

while (!condition(dimension)) {
size -= 1;

// Here if the size goes below zero most likely is because it
// has additional style applied in which case we assume the user
// knows what it's doing and we just let them use that.
// Visually it works, although it could have another
// check in place.
if (size < 0) {
size = startSize;
break;
}

dimension = computeDimension(size);
}

Expand Down Expand Up @@ -66,15 +78,15 @@ export default function computeMaxFontSize(
size = decreaseSizeUntil(
size,
computeDimension,
dim => dim.width <= maxWidth,
dim => dim.width > 0 && dim.width <= maxWidth,
);
}

if (maxHeight !== undefined && maxHeight !== null) {
size = decreaseSizeUntil(
size,
computeDimension,
dim => dim.height <= maxHeight,
dim => dim.height > 0 && dim.height <= maxHeight,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,14 @@ describe('computeMaxFontSize(input)', () => {
}),
).toEqual(25);
});
it('ensure idealFontSize is used if the maximum font size calculation goes below zero', () => {
expect(
computeMaxFontSize({
maxWidth: 5,
idealFontSize: 34,
text: SAMPLE_TEXT[0],
}),
).toEqual(34);
});
});
});

0 comments on commit 365acee

Please sign in to comment.