Repository navigation
Font Size Picker: Allow non-integers as simple CSS values and in hints #36636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { isSimpleCssValue, splitValueAndUnitFromSize } from '../utils'; | ||
|
|
||
| const simpleCSSCases = [ | ||
| // Test integers and non-integers. | ||
| [ 1, true ], | ||
| [ 1.25, true ], | ||
| [ '123', true ], | ||
| [ '1.5', true ], | ||
| [ '0.75', true ], | ||
| // CSS unit tests. | ||
| [ '20px', true ], | ||
| [ '0.8em', true ], | ||
| [ '2rem', true ], | ||
| [ '1.4vw', true ], | ||
| [ '0.4vh', true ], | ||
| // Invalid negative values, | ||
| [ '-5px', false ], | ||
| // Complex CSS values that should fail. | ||
| [ 'abs(-10px)', false ], | ||
| [ 'calc(10px + 1)', false ], | ||
| [ 'clamp(2.5rem, 4vw, 3rem)', false ], | ||
| [ 'max(4.5em, 3vh)', false ], | ||
| [ 'min(10px, 1rem)', false ], | ||
| [ 'minmax(30px, auto)', false ], | ||
| [ 'var(--wp--font-size)', false ], | ||
| ]; | ||
|
|
||
| describe( 'isSimpleCssValue', () => { | ||
| test.each( simpleCSSCases )( | ||
| 'given %p as argument, returns %p', | ||
| ( cssValue, result ) => { | ||
| expect( isSimpleCssValue( cssValue ) ).toBe( result ); | ||
| } | ||
| ); | ||
| } ); | ||
|
|
||
| const splitValuesCases = [ | ||
| // Test integers and non-integers. | ||
| [ 1, '1', undefined ], | ||
| [ 1.25, '1.25', undefined ], | ||
| [ '123', '123', undefined ], | ||
| [ '1.5', '1.5', undefined ], | ||
| [ '0.75', '0.75', undefined ], | ||
| // Valid simple CSS values. | ||
| [ '20px', '20', 'px' ], | ||
| [ '0.8em', '0.8', 'em' ], | ||
| [ '2rem', '2', 'rem' ], | ||
| [ '1.4vw', '1.4', 'vw' ], | ||
| [ '0.4vh', '0.4', 'vh' ], | ||
| // Invalid negative values, | ||
| [ '-5px', undefined, undefined ], | ||
| // Complex CSS values that shouldn't parse. | ||
| [ 'abs(-15px)', undefined, undefined ], | ||
| [ 'calc(10px + 1)', undefined, undefined ], | ||
| [ 'clamp(2.5rem, 4vw, 3rem)', undefined, undefined ], | ||
| [ 'max(4.5em, 3vh)', undefined, undefined ], | ||
| [ 'min(10px, 1rem)', undefined, undefined ], | ||
| [ 'minmax(30px, auto)', undefined, undefined ], | ||
| [ 'var(--wp--font-size)', undefined, undefined ], | ||
| ]; | ||
|
|
||
| describe( 'splitValueAndUnitFromSize', () => { | ||
| test.each( splitValuesCases )( | ||
| 'given %p as argument, returns value = %p and unit = %p', | ||
| ( cssValue, expectedValue, expectedUnit ) => { | ||
| const [ value, unit ] = splitValueAndUnitFromSize( cssValue ); | ||
| expect( value ).toBe( expectedValue ); | ||
| expect( unit ).toBe( expectedUnit ); | ||
| } | ||
| ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,12 +22,13 @@ const CUSTOM_FONT_SIZE_OPTION = { | |
| * @return {[number, string]} An array with the numeric value and the unit if exists. | ||
| */ | ||
| export function splitValueAndUnitFromSize( size ) { | ||
| /** | ||
| * The first matched result is ignored as it's the left | ||
| * hand side of the capturing group in the regex. | ||
| */ | ||
| const [ , numericValue, unit ] = size.split( /(\d+)/ ); | ||
| return [ numericValue, unit ]; | ||
| const [ numericValue, unit ] = `${ size }`.match( /[\d\.]+|\D+/g ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the changes made in #36420, I believe that we should also apply the change on the JSDoc for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes here were slightly different to #36420. It honours what was documented in terms of the param accepting The In light of that, do you still believe I should restrict this?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You actually made a few good points — let's leave it as-is (and thank you for looking into it)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering why you changed from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, this wasn't a conscious choice. It came from when I copied the original change. On a closer look though, |
||
|
|
||
| if ( ! isNaN( parseFloat( numericValue ) ) && isFinite( numericValue ) ) { | ||
| return [ numericValue, unit ]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -38,7 +39,7 @@ export function splitValueAndUnitFromSize( size ) { | |
| * @return {boolean} Whether the value is a simple css value. | ||
| */ | ||
| export function isSimpleCssValue( value ) { | ||
| const sizeRegex = /^(?!0)\d+(px|em|rem|vw|vh|%)?$/i; | ||
| const sizeRegex = /^[\d\.]+(px|em|rem|vw|vh|%)?$/i; | ||
| return sizeRegex.test( value ); | ||
| } | ||
|
|
||
|
|
@@ -75,7 +76,7 @@ function getSelectOptions( optionsArray, disableCustomFontSizes ) { | |
| name, | ||
| size, | ||
| __experimentalHint: | ||
| size && isSimpleCssValue( size ) && parseInt( size ), | ||
| size && isSimpleCssValue( size ) && parseFloat( size ), | ||
| } ) ); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.