Skip to content

Commit

Permalink
Mobile - Fix parsing of css units for null matched values (#48484)
Browse files Browse the repository at this point in the history
* Mobile - Fix parsing of css units for null matched values

* Mobile - getPxFromCssUnit - Add new case to test
  • Loading branch information
Gerardo Pacheco authored Mar 2, 2023
1 parent a08861e commit 4945d3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/block-editor/src/utils/parse-css-unit-to-px.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,24 @@ function evalMathExpression( cssUnit ) {
// The following regex matches numbers that have a following unit
// E.g. 5.25rem, 1vw
const cssUnitsBits = cssUnit.match( /\d+\.?\d*[a-zA-Z]+|\.\d+[a-zA-Z]+/g );
for ( const unit of cssUnitsBits ) {
// Standardize the unit to px and extract the value.
const parsedUnit = parseUnit( getPxFromCssUnit( unit ) );
if ( ! parseFloat( parsedUnit.value ) ) {
errorFound = true;
// End early since we are dealing with a null value.
break;
if ( cssUnitsBits ) {
for ( const unit of cssUnitsBits ) {
// Standardize the unit to px and extract the value.
const parsedUnit = parseUnit( getPxFromCssUnit( unit ) );
if ( ! parseFloat( parsedUnit.value ) ) {
errorFound = true;
// End early since we are dealing with a null value.
break;
}
cssUnit = cssUnit.replace( unit, parsedUnit.value );
}
cssUnit = cssUnit.replace( unit, parsedUnit.value );
} else {
errorFound = true;
}

// For mixed math expressions wrapped within CSS expressions
if ( ! errorFound && cssUnit.match( /(max|min|clamp)/g ) ) {
const expressionsMatches = cssUnit.match( /(max|min|clamp)/g );
if ( ! errorFound && expressionsMatches ) {
const values = cssUnit.split( ',' );
for ( const currentValue of values ) {
// Check for nested calc() and remove them to calculate the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe( 'getPxFromCssUnit', () => {
'clamp(2.625rem, calc(2.625rem + ((1vw - 0.48rem) * 8.4135)), 3.25rem)',
'42px',
],
[ 'var:preset|font-size|medium', null ],
];

test.each( testData )( 'getPxFromCssUnit( %s )', ( unit, expected ) => {
Expand Down

1 comment on commit 4945d3f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 4945d3f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4311714359
📝 Reported issues:

Please sign in to comment.