Skip to content
Open
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
49 changes: 48 additions & 1 deletion src/wp-includes/fonts/class-wp-font-face.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ private function compile_src( array $value ) {
*
* @since 6.4.0
* @since 7.2.0 Quotes each axis tag and separates the axes with commas.
* @since 7.3.0 Escapes each axis tag and skips an axis whose tag or value is invalid.
*
* @link https://www.w3.org/TR/css-fonts-4/#font-variation-settings-def
*
* @param array $font_variation_settings Array of font variation settings, keyed by axis tag.
* @return string The CSS.
Expand All @@ -372,9 +375,53 @@ private function compile_variations( array $font_variation_settings ) {
$variations = array();

foreach ( $font_variation_settings as $tag => $value ) {
$variations[] = sprintf( '"%s" %s', $tag, $value );
$tag = (string) $tag;
$number = self::get_css_number( $value );

// An axis tag is four ASCII characters from U+0020 to U+007E.
if ( null === $number || 1 !== preg_match( '/\A[\x20-\x7E]{4}\z/', $tag ) ) {
continue;
}

$variations[] = WP_CSS_Builder::string( $tag ) . ' ' . $number;
}

return implode( ', ', $variations );
}

/**
* Returns the CSS text of a value that is one CSS number.
*
* Whitespace around the number is removed. Any other token, such as a
* unit, a function or a second number, makes the value invalid.
*
* @since 7.3.0
*
* @param mixed $value Value to read.
* @return string|null The number as CSS text, or null if the value is not one number.
*/
private static function get_css_number( $value ) {
if ( ! is_int( $value ) && ! is_float( $value ) && ! is_string( $value ) ) {
return null;
}

$processor = new WP_CSS_Token_Processor( (string) $value );
$number = null;

while ( $processor->next_token() ) {
$type = $processor->get_token_type();

if ( WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type ) {
continue;
}

if ( null !== $number || WP_CSS_Token_Processor::TOKEN_NUMBER !== $type ) {
return null;
}

$number = $processor->get_token()->get_value();
}

return $number;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,76 @@ public function test_should_generate_and_print_given_fonts( array $fonts, $expec
$output = get_echo( array( $font_face, 'generate_and_print' ), array( $fonts ) );
$this->assertEqualHTML( $expected_output, $output );
}

/**
* @ticket 66103
*
* @dataProvider data_should_print_font_variation_settings
*
* @param array $settings Font variation settings, keyed by axis tag.
* @param string|null $expected Expected descriptor value, or null if the descriptor is omitted.
*/
public function test_should_print_font_variation_settings( array $settings, $expected ) {
$font_face = new WP_Font_Face();
$fonts = array(
array(
array(
'font-family' => 'Inter',
'src' => array( 'https://example.org/font.woff2' ),
'font-variation-settings' => $settings,
),
),
);

$output = get_echo( array( $font_face, 'generate_and_print' ), array( $fonts ) );

if ( null === $expected ) {
$this->assertStringNotContainsString( 'font-variation-settings', $output );
} else {
$this->assertStringContainsString( 'font-variation-settings:' . $expected . ';}', $output );
}
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_should_print_font_variation_settings() {
return array(
'registered axes' => array(
array(
'wght' => 400,
'slnt' => -10,
),
'"wght" 400, "slnt" -10',
),
'a custom axis' => array( array( 'GRAD' => 0 ), '"GRAD" 0' ),
'a float value' => array( array( 'wght' => 412.5 ), '"wght" 412.5' ),
'a numeric string with whitespace' => array( array( 'wght' => ' 400 ' ), '"wght" 400' ),
'a numeric tag' => array( array( '1234' => 1 ), '"1234" 1' ),
'a tag with a quotation mark is escaped' => array( array( 'a"b;' => 1 ), '"a\\22 b\\3b " 1' ),
'a tag with an angle bracket is escaped' => array( array( '</a>' => 1 ), '"\\3c /a\\3e " 1' ),
'a tag that is not four characters' => array(
array(
'wdth' => 100,
'weight' => 400,
),
'"wdth" 100',
),
'a tag with a non-ASCII character' => array( array( 'wgh€' => 400 ), null ),
'a value with a unit' => array( array( 'wght' => '400px' ), null ),
'a value with a second declaration' => array( array( 'wght' => '400;color:red' ), null ),
'a value with a function' => array( array( 'wght' => 'calc(400)' ), null ),
'a value that is not a scalar' => array( array( 'wght' => array( 400 ) ), null ),
'a value that is not a number' => array(
array(
'wght' => 'bold',
'wdth' => 100,
),
'"wdth" 100',
),
'a value that is infinite' => array( array( 'wght' => INF ), null ),
);
}
}
Loading