Skip to content

Commit

Permalink
SImplify font-weight validation
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Dec 8, 2021
1 parent 158642b commit 24afc2f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
12 changes: 1 addition & 11 deletions lib/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,7 @@ public function validate_font( $font ) {
}

// Check the font-weight.
if ( // Bail out if the font-weight is not a valid value.
( ! is_string( $font['font-weight'] ) && ! is_int( $font['font-weight'] ) ) ||
(
// Check if value is a single font-weight, formatted as a number.
! in_array( $font['font-weight'], array( 'normal', 'bold', 'bolder', 'lighter', 'inherit' ), true ) &&
// Check if value is a single font-weight, formatted as a number.
! preg_match( '/^(\d+)$/', $font['font-weight'], $matches ) &&
// Check if value is a range of font-weights, formatted as a number range.
! preg_match( '/^(\d+)\s+(\d+)$/', $font['font-weight'], $matches )
)
) {
if ( ! is_string( $font['font-weight'] ) && ! is_int( $font['font-weight'] ) ) {
trigger_error( __( 'Webfont font weight must be a properly formatted string or integer.', 'gutenberg' ) );
return false;
}
Expand Down
11 changes: 2 additions & 9 deletions phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,8 @@ public function test_validate_font() {
$this->assertNotEmpty( wp_webfonts()->validate_font( $font ) );

// Test font-weight.
$font_weights = array(
'invalid' => array( 'invalid', '', '100-900' ),
'valid' => array( 100, '100', '100 900', 'normal' ),
);
foreach ( $font_weights['invalid'] as $value ) {
$font['font-weight'] = $value;
$this->assertFalse( wp_webfonts()->validate_font( $font ) );
}
foreach ( $font_weights['valid'] as $value ) {
$font_weights = array( 100, '100', '100 900', 'normal' );
foreach ( $font_weights as $value ) {
$font['font-weight'] = $value;
$this->assertEquals( wp_webfonts()->validate_font( $font )['font-weight'], $value );
}
Expand Down

0 comments on commit 24afc2f

Please sign in to comment.