Skip to content

Commit 58531a8

Browse files
matiasbenedettoyouknowriad
authored andcommitted
Font Library: adds the ability to use generic() in font family names. (#59103)
* adds the ability to use generic in font families as the examples from CSS level 4 spec. Example: generic(kai) * update comment
1 parent 3d276af commit 58531a8

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php

+8-12
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,26 @@ class WP_Font_Utils {
2828
* @link https://www.w3.org/TR/css-fonts-4/#font-family-prop
2929
*
3030
* @since 6.5.0
31-
* @access private
32-
*
33-
* @see sanitize_font_family()
3431
*
3532
* @param string $item A font family name.
36-
* @return string The font family name with surrounding quotes if necessary.
33+
* @return string The font family name with surrounding quotes, if necessary.
3734
*/
3835
private static function maybe_add_quotes( $item ) {
39-
// Match any non alphabetic characters (a-zA-Z), dashes -, or parenthesis ().
40-
$regex = '/[^a-zA-Z\-()]+/';
36+
// Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens).
37+
$regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/';
4138
$item = trim( $item );
4239
if ( preg_match( $regex, $item ) ) {
43-
// Removes leading and trailing quotes.
44-
$item = preg_replace( '/^["\']|["\']$/', '', $item );
45-
return "\"$item\"";
40+
$item = trim( $item, "\"'" );
41+
return '"' . $item . '"';
4642
}
4743
return $item;
4844
}
4945

5046
/**
5147
* Sanitizes and formats font family names.
5248
*
53-
* - Applies `sanitize_text_field`
54-
* - Adds surrounding quotes to names that special
49+
* - Applies `sanitize_text_field`.
50+
* - Adds surrounding quotes to names containing any characters that are not alphabetic or dashes.
5551
*
5652
* It follows the recommendations from the CSS Fonts Module Level 4.
5753
* @link https://www.w3.org/TR/css-fonts-4/#font-family-prop
@@ -69,7 +65,7 @@ public static function sanitize_font_family( $font_family ) {
6965
return '';
7066
}
7167

72-
$output = trim( sanitize_text_field( $font_family ) );
68+
$output = sanitize_text_field( $font_family );
7369
$formatted_items = array();
7470
if ( str_contains( $output, ',' ) ) {
7571
$items = explode( ',', $output );

packages/edit-site/src/components/global-styles/font-library-modal/utils/preview-styles.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ function extractFontWeights( fontFaces ) {
4242
*
4343
* Example:
4444
* formatFontFamily( "Open Sans, Font+Name, sans-serif" ) => '"Open Sans", "Font+Name", sans-serif'
45-
* formatFontFamily( "'Open Sans', sans-serif" ) => '"Open Sans", sans-serif'
45+
* formatFontFamily( "'Open Sans', generic(kai), sans-serif" ) => '"Open Sans", sans-serif'
4646
* formatFontFamily( "DotGothic16, Slabo 27px, serif" ) => '"DotGothic16","Slabo 27px",serif'
4747
* formatFontFamily( "Mine's, Moe's Typography" ) => `"mine's","Moe's Typography"`
4848
*/
4949
export function formatFontFamily( input ) {
50-
// Matchs any non alphabetic characters (a-zA-Z), dashes - , or parenthesis ()
51-
const regex = /[^a-zA-Z\-()]+/;
50+
// Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens).
51+
const regex = /^(?!generic\([ a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/;
5252
const output = input.trim();
5353

5454
const formatItem = ( item ) => {

packages/edit-site/src/components/global-styles/font-library-modal/utils/test/preview-styles.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ describe( 'formatFontFamily', () => {
172172
'"Abril Fatface", Times, serif'
173173
);
174174
} );
175+
176+
it( 'should not add quotes to generic names', () => {
177+
expect(
178+
formatFontFamily(
179+
'Paren(thesis)Font, generic(kai), generic(fasongsong), generic( abc ), Helvetica Neue'
180+
)
181+
).toBe(
182+
'"Paren(thesis)Font", generic(kai), generic(fasongsong), generic( abc ), "Helvetica Neue"'
183+
);
184+
} );
175185
} );
176186

177187
describe( 'formatFontFaceName', () => {

0 commit comments

Comments
 (0)