fix: avoid while cycle in computeMaxFontSize for big Number run forever when css rule applied (#20173)

This commit is contained in:
Diego Medina 2022-05-25 06:04:04 -04:00 committed by GitHub
parent b0c6935f06
commit 365acee663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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);
}
@ -66,7 +78,7 @@ export default function computeMaxFontSize(
size = decreaseSizeUntil(
size,
computeDimension,
dim => dim.width <= maxWidth,
dim => dim.width > 0 && dim.width <= maxWidth,
);
}
@ -74,7 +86,7 @@ export default function computeMaxFontSize(
size = decreaseSizeUntil(
size,
computeDimension,
dim => dim.height <= maxHeight,
dim => dim.height > 0 && dim.height <= maxHeight,
);
}

View File

@ -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);
});
});
});