Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,96 @@ describe( 'getAllValue', () => {
expect( getAllValue( undefined ) ).toBe( undefined );
} );
} );

describe( 'when provided complex CSS values (clamp, min, max, calc)', () => {
it( 'should preserve clamp values when all corners have the same clamp value', () => {
const clampValue = 'clamp(1rem, 2vw, 3rem)';
const values = {
bottomLeft: clampValue,
bottomRight: clampValue,
topLeft: clampValue,
topRight: clampValue,
};
expect( getAllValue( values ) ).toBe( clampValue );
} );

it( 'should preserve min() values when all corners have the same min value', () => {
const minValue = 'min(10px, 5vw)';
const values = {
bottomLeft: minValue,
bottomRight: minValue,
topLeft: minValue,
topRight: minValue,
};
expect( getAllValue( values ) ).toBe( minValue );
} );

it( 'should preserve max() values when all corners have the same max value', () => {
const maxValue = 'max(20px, 10vw)';
const values = {
bottomLeft: maxValue,
bottomRight: maxValue,
topLeft: maxValue,
topRight: maxValue,
};
expect( getAllValue( values ) ).toBe( maxValue );
} );

it( 'should preserve calc() values when all corners have the same calc value', () => {
const calcValue = 'calc(100% - 20px)';
const values = {
bottomLeft: calcValue,
bottomRight: calcValue,
topLeft: calcValue,
topRight: calcValue,
};
expect( getAllValue( values ) ).toBe( calcValue );
} );

it( 'should return undefined when complex CSS values are mixed', () => {
const values = {
bottomLeft: 'clamp(1rem, 2vw, 3rem)',
bottomRight: 'clamp(1rem, 2vw, 3rem)',
topLeft: 'min(10px, 5vw)',
topRight: 'clamp(1rem, 2vw, 3rem)',
};
expect( getAllValue( values ) ).toBe( undefined );
} );

it( 'should return undefined when complex CSS values are mixed with simple values', () => {
const values = {
bottomLeft: 'clamp(1rem, 2vw, 3rem)',
bottomRight: 'clamp(1rem, 2vw, 3rem)',
topLeft: '2px',
topRight: 'clamp(1rem, 2vw, 3rem)',
};
expect( getAllValue( values ) ).toBe( undefined );
} );

it( 'should preserve string values that cannot be parsed at all (no numeric prefix)', () => {
// Values with no numeric prefix cannot be parsed, so they should be preserved
const unparseableValue = 'apples';
const values = {
bottomLeft: unparseableValue,
bottomRight: unparseableValue,
topLeft: unparseableValue,
topRight: unparseableValue,
};
expect( getAllValue( values ) ).toBe( unparseableValue );
} );

it( 'should parse numeric prefix from partially parseable values', () => {
// Values with numeric prefix get parsed, so "32apples" becomes "32"
const partiallyParseableValue = '32apples';
const values = {
bottomLeft: partiallyParseableValue,
bottomRight: partiallyParseableValue,
topLeft: partiallyParseableValue,
topRight: partiallyParseableValue,
};
expect( getAllValue( values ) ).toBe( '32' );
} );
} );
} );

describe( 'hasMixedValues', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ export function getAllValue( values = {} ) {
return values;
}

const parsedQuantitiesAndUnits = Object.values( values ).map( ( value ) =>
parseQuantityAndUnitFromRawValue( value )
);
const parsedQuantitiesAndUnits = Object.values( values ).map( ( value ) => {
const newValue = parseQuantityAndUnitFromRawValue( value );
if ( typeof value === 'string' && newValue[ 0 ] === undefined ) {
return [ value, '' ];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will return the string no matter what it is. I think that's okay since the value should match what's in the presets array in order for the steppers to work.

The Theme JSON class sanitizes the values on the backend.

https://github.com/WordPress/gutenberg/blob/trunk/lib/class-wp-theme-json-gutenberg.php#L3716

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a glance, this looks ok to me as well.

There's a possibility the values are values originating from the preset values but instead custom values entered by the user. These would be entered via the custom value inputs and should be safe simple CSS values.

The Theme JSON class does sanitize style values on the backend but that happens here:

https://github.com/WordPress/gutenberg/blob/trunk/lib/class-wp-theme-json-gutenberg.php#L3779

It might pay to dig a little deeper and confirm safety for the application of styles in the editor. If there's an issue there it wouldn't be unique to this fix or PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked a little closer and didn't see any obvious issues but it might pay to get a fresher set of eyes on it.

}
return newValue;
} );

const allValues = parsedQuantitiesAndUnits.map(
( value ) => value[ 0 ] ?? ''
Expand Down
Loading