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
48 changes: 48 additions & 0 deletions src/wp-includes/css-api/class-wp-css-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,54 @@ public function is_terminated(): bool {
return $this->is_terminated;
}

/**
* Reports whether the token ends with a `\` that starts an escape at the
* end of the input.
*
* CSS Syntax 3 reads that `\` as a parse error. In an identifier or a url
* it decodes to U+FFFD; in a string it decodes to nothing. An identifier
* that ends this way is otherwise complete, and is_terminated() returns
* true for it. A consumer that rejects input with parse errors can check
* this method.
*
* Example:
*
* ```php interactive
* $processor = new WP_CSS_Token_Processor( 'Inter\\' );
* $processor->next_token();
* $token = $processor->get_token();
* echo $token->get_value() . "\n";
* var_dump( $token->ends_with_eof_escape() );
* // Outputs:
* // Inter�
* // bool(true)
* ```
*
* @since 7.3.0
*
* @link https://www.w3.org/TR/css-syntax-3/#consume-escaped-code-point
*
* @return bool Whether the input ends with a `\` that starts an escape in this token.
*/
public function ends_with_eof_escape(): bool {
$end = $this->start + $this->length;
if (
WP_CSS_Token_Processor::TOKEN_COMMENT === $this->type ||
strlen( $this->css ) !== $end ||
'\\' !== $this->css[ $end - 1 ]
) {
return false;
}

// In a run of `\`, each second `\` is the character the one before it escapes.
$at = $end - 1;
while ( $at > $this->start && '\\' === $this->css[ $at - 1 ] ) {
--$at;
}

return 1 === ( $end - $at ) % 2;
}

/**
* Reports whether the token is whitespace or a comment.
*
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/fonts/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static function get_sanitization_schema() {
'preview' => 'sanitize_url',
'fontFace' => array(
array(
'fontFamily' => 'sanitize_text_field',
'fontFamily' => 'WP_Font_Utils::sanitize_font_family',
'fontStyle' => 'sanitize_text_field',
'fontWeight' => 'sanitize_text_field',
'src' => static function ( $value ) {
Expand Down
7 changes: 2 additions & 5 deletions src/wp-includes/fonts/class-wp-font-face-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,13 @@ private static function parse_settings( array $settings ) {
* parse and return the fist font from the list.
*
* @since 6.4.0
* @since 7.3.0 Returns the name as a quoted CSS string, or an empty string if the value is invalid.
*
* @param string $font_family Font family `fontFamily' to parse.
* @return string Font-family name.
*/
private static function maybe_parse_name_from_comma_separated_list( $font_family ) {
if ( str_contains( $font_family, ',' ) ) {
$font_family = explode( ',', $font_family )[0];
}

return trim( $font_family, "\"'" );
return WP_Font_Utils::get_font_face_family( $font_family );
}

/**
Expand Down
36 changes: 22 additions & 14 deletions src/wp-includes/fonts/class-wp-font-face.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Font Face generates and prints `@font-face` styles for given fonts.
*
* @since 6.4.0
* @since 7.3.0 Writes the font-family descriptor as a quoted CSS string.
*/
class WP_Font_Face {

Expand Down Expand Up @@ -82,8 +83,9 @@ public function generate_and_print( array $fonts ) {

/*
* The font-face CSS is contained within <style> tags and can only be interpreted
* as CSS in the browser. Using wp_strip_all_tags() is sufficient escaping
* to avoid malicious attempts to close </style> and open a <script>.
* as CSS in the browser. The font-family descriptor escapes `<` as a CSS escape,
* so a font name cannot close the style element. wp_strip_all_tags() removes any
* remaining markup from the other descriptors.
*/
$css = wp_strip_all_tags( $css );

Expand Down Expand Up @@ -146,6 +148,23 @@ private function validate_font_face_declarations( array $font_face ) {
return false;
}

/*
* Write the font-family descriptor as a quoted CSS string. The value
* can be CSS, such as `"ACME, Sans"`, or a plain name, such as
* `O'Reilly Sans`. The decoded name does not change.
*/
$font_face['font-family'] = WP_Font_Utils::get_font_face_family( $font_face['font-family'] );

if ( '' === $font_face['font-family'] ) {
// @todo replace with `wp_trigger_error()`.
_doing_it_wrong(
__METHOD__,
__( 'Font font-family must be a valid CSS font family value or a plain font name.' ),
'7.3.0'
);
return false;
}

// Make sure that local fonts have 'src' defined.
if ( empty( $font_face['src'] ) || ( ! is_string( $font_face['src'] ) && ! is_array( $font_face['src'] ) ) ) {
// @todo replace with `wp_trigger_error()`.
Expand Down Expand Up @@ -307,18 +326,7 @@ private function order_src( array $font_face ) {
private function build_font_face_css( array $font_face ) {
$css = '';

/*
* Wrap font-family in quotes if it contains spaces
* and is not already wrapped in quotes.
*/
if (
str_contains( $font_face['font-family'], ' ' ) &&
! str_contains( $font_face['font-family'], '"' ) &&
! str_contains( $font_face['font-family'], "'" )
) {
$font_face['font-family'] = '"' . $font_face['font-family'] . '"';
}

// The font-family is already a quoted CSS string. See ::validate_font_face_declarations().
foreach ( $font_face as $key => $value ) {
// Compile the "src" parameter.
if ( 'src' === $key ) {
Expand Down
Loading
Loading