Skip to content

🎥 Make videos programmatically with React

remotion: Validate typed interpolation values

Jonny Burger4b63bfcd2ac9
diff --git a/packages/core/src/interpolate.ts b/packages/core/src/interpolate.ts
index d093a552fb..63adb4e76b 100644
--- a/packages/core/src/interpolate.ts
+++ b/packages/core/src/interpolate.ts
@@ -475,6 +475,12 @@ const parseStringInterpolationValue = (
}
if (parts.some((part) => transformOriginKeywords.has(part.toLowerCase()))) {
+ if (outputType !== undefined) {
+ throw new TypeError(
+ `Cannot interpolate "${output}" as ${outputType} because it is a transform-origin value`,
+ );
+ }
+
return parseTransformOriginValue(output, parts);
}
@@ -783,17 +789,16 @@ const interpolateString = ({
inputRange,
outputRange,
options,
+ outputType,
}: {
input: number;
inputRange: readonly number[];
outputRange: readonly (string | number)[];
options: InterpolateOptions | undefined;
+ outputType: Exclude<InterpolateOutputType, 'font-weight'> | undefined;
}): string => {
const initiallyParsedOutputRange = outputRange.map((output) =>
- parseStringInterpolationValue(
- output,
- options?.outputType === 'font-weight' ? undefined : options?.outputType,
- ),
+ parseStringInterpolationValue(output, outputType),
);
const hasAxisRotation = initiallyParsedOutputRange.some(
(parsed) => parsed.axisRotation,
@@ -948,19 +953,24 @@ const interpolateFontWeight = ({
options: InterpolateOptions | undefined;
}): number => {
const normalizedOutputRange = outputRange.map((output) => {
- if (output === 'normal') {
- return 400;
- }
+ if (typeof output === 'string') {
+ const lowerCaseOutput = output.toLowerCase();
+ if (lowerCaseOutput === 'normal') {
+ return 400;
+ }
- if (output === 'bold') {
- return 700;
+ if (lowerCaseOutput === 'bold') {
+ return 700;
+ }
}
+ const match =
+ typeof output === 'string' ? cssNumberRegex.exec(output) : null;
const value =
typeof output === 'number'
? output
- : cssNumberRegex.exec(output)?.[2] === undefined
- ? Number(output)
+ : match !== null && match[2] === undefined
+ ? Number(match[1])
: Number.NaN;
if (!Number.isFinite(value) || value < 1 || value > 1000) {
throw new TypeError(
@@ -1309,7 +1319,8 @@ export function interpolate(
throw new Error('outputRange must contain only numbers');
}
- if (options?.outputType === 'font-weight') {
+ const outputType = options?.outputType;
+ if (outputType === 'font-weight') {
if (
!outputRange.every(
(output) => typeof output === 'number' || typeof output === 'string',
@@ -1331,13 +1342,9 @@ export function interpolate(
const hasStringOutput = outputRange.some(
(output) => typeof output === 'string',
);
- if (
- options?.outputType !== undefined &&
- options.outputType !== 'scale' &&
- !hasStringOutput
- ) {
+ if (outputType !== undefined && outputType !== 'scale' && !hasStringOutput) {
throw new TypeError(
- `${options.outputType} outputRange must contain strings with the appropriate CSS units`,
+ `${outputType} outputRange must contain strings with the appropriate CSS units`,
);
}
@@ -1353,7 +1360,13 @@ export function interpolate(
}
try {
- return interpolateString({input, inputRange, outputRange, options});
+ return interpolateString({
+ input,
+ inputRange,
+ outputRange,
+ options,
+ outputType,
+ });
} catch (error) {
if (!outputRange.every((output) => typeof output === 'string')) {
throw error;
@@ -1361,12 +1374,7 @@ export function interpolate(
const hasNonNumericString = outputRange.some((output) => {
try {
- parseStringInterpolationValue(
- output,
- options?.outputType === 'font-weight'
- ? undefined
- : options?.outputType,
- );
+ parseStringInterpolationValue(output, outputType);
return false;
} catch (parseError) {
return parseError instanceof UnsupportedStringInterpolationValueError;
diff --git a/packages/core/src/test/interpolate.test.ts b/packages/core/src/test/interpolate.test.ts
index ff0920ca27..64f9f99661 100644
--- a/packages/core/src/test/interpolate.test.ts
+++ b/packages/core/src/test/interpolate.test.ts
@@ -98,6 +98,11 @@ test('Can interpolate font weights with an explicit output type', () => {
outputType: 'font-weight',
}),
).toBe(500);
+ expect(
+ interpolate(0.5, [0, 1], ['NORMAL', 'BOLD'], {
+ outputType: 'font-weight',
+ }),
+ ).toBe(550);
});
test('Font weight keywords require an explicit output type', () => {
@@ -122,6 +127,15 @@ test('Font weights must be absolute values in the CSS range', () => {
}),
/Expected "normal", "bold", or a number between 1 and 1000/,
);
+ for (const invalidWeight of ['1e3', '0x10', ' 100 ']) {
+ expectToThrow(
+ () =>
+ interpolate(0.5, [0, 1], [invalidWeight, 'bold'], {
+ outputType: 'font-weight',
+ }),
+ /Expected "normal", "bold", or a number between 1 and 1000/,
+ );
+ }
});
test('Explicit string output types validate the output range', () => {
@@ -144,6 +158,20 @@ test('Explicit string output types validate the output range', () => {
() => interpolate(0.5, [0, 1], [0, 90], {outputType: 'rotate'}),
/rotate outputRange must contain strings with the appropriate CSS units/,
);
+ expectToThrow(
+ () =>
+ interpolate(0.5, [0, 1], ['left top', 'right bottom'], {
+ outputType: 'rotate',
+ }),
+ /as rotate because it is a transform-origin value/,
+ );
+ expectToThrow(
+ () =>
+ interpolate(0.5, [0, 1], ['left top', 'right bottom'], {
+ outputType: 'translate',
+ }),
+ /as translate because it is a transform-origin value/,
+ );
});
test('Easing array with one keyframe accepts no entries', () => {
diff --git a/packages/docs/docs/interpolate.mdx b/packages/docs/docs/interpolate.mdx
index 689f126e99..5b02cdb381 100644
--- a/packages/docs/docs/interpolate.mdx
+++ b/packages/docs/docs/interpolate.mdx
@@ -322,6 +322,7 @@ For `[0, -1]` at progress `0.5`, the result is `-Math.sqrt(0.5)`.
Explicitly selects the output value syntax.
Accepts `'font-weight'`, `'scale'`, `'translate'`, `'rotate'`, or `'transform-origin'`.
Font weight keywords are only supported when this option is set to `'font-weight'`.
+Translate, rotate, and transform origin output ranges must use strings with the appropriate CSS units.
### posterize<AvailableFrom v="4.0.470" />