Ports theme.json changes for beta 3.

- Add _wp_to_kebab_case function
- Add CSS Custom Properties within preset classes.

Props nosolosw.
See #53397.

git-svn-id: https://develop.svn.wordpress.org/trunk@51198 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jorge Costa 2021-06-22 09:49:13 +00:00
parent 098ab594fe
commit ab86a02401
4 changed files with 126 additions and 9 deletions

View File

@ -711,11 +711,11 @@ class WP_Theme_JSON {
foreach ( $preset['classes'] as $class ) {
foreach ( $preset_by_slug as $slug => $value ) {
$stylesheet .= self::to_ruleset(
self::append_to_selector( $selector, '.has-' . $slug . '-' . $class['class_suffix'] ),
self::append_to_selector( $selector, '.has-' . _wp_to_kebab_case( $slug ) . '-' . $class['class_suffix'] ),
array(
array(
'name' => $class['property_name'],
'value' => $value . ' !important',
'value' => 'var(--wp--preset--' . $preset['css_var_infix'] . '--' . _wp_to_kebab_case( $slug ) . ') !important',
),
)
);
@ -751,7 +751,7 @@ class WP_Theme_JSON {
$preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] );
foreach ( $preset_by_slug as $slug => $value ) {
$declarations[] = array(
'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $slug,
'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . _wp_to_kebab_case( $slug ),
'value' => $value,
);
}

View File

@ -4697,6 +4697,83 @@ function _wp_array_set( &$array, $path, $value = null ) {
$array[ $path[ $i ] ] = $value;
}
/**
* This function is trying to replicate what
* lodash's kebabCase (JS library) does in the client.
*
* The reason we need this function is that we do some processing
* in both the client and the server (e.g.: we generate
* preset classes from preset slugs) that needs to
* create the same output.
*
* We can't remove or update the client's library due to backward compatibility
* (some of the output of lodash's kebabCase is saved in the post content).
* We have to make the server behave like the client.
*
* Changes to this function should follow updates in the client
* with the same logic.
*
* @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L14369
* @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L278
* @link https://github.com/lodash-php/lodash-php/blob/master/src/String/kebabCase.php
* @link https://github.com/lodash-php/lodash-php/blob/master/src/internal/unicodeWords.php
*
* @param string $string The string to kebab-case.
*
* @return string kebab-cased-string.
*/
function _wp_to_kebab_case( $string ) {
//phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
// ignore the camelCase names for variables so the names are the same as lodash
// so comparing and porting new changes is easier.
/*
* Some notable things we've removed compared to the lodash version are:
*
* - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
* - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
*
*/
/** Used to compose unicode character classes. */
$rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff';
$rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf';
$rsPunctuationRange = '\\x{2000}-\\x{206f}';
$rsSpaceRange = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}';
$rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde';
$rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;
/** Used to compose unicode capture groups. */
$rsBreak = '[' . $rsBreakRange . ']';
$rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
$rsLower = '[' . $rsLowerRange . ']';
$rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
$rsUpper = '[' . $rsUpperRange . ']';
/** Used to compose unicode regexes. */
$rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
$rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
$rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])';
$rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])';
$regexp = '/' . implode(
'|',
array(
$rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
$rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
$rsUpper . '?' . $rsMiscLower . '+',
$rsUpper . '+',
$rsOrdUpper,
$rsOrdLower,
$rsDigits,
)
) . '/u';
preg_match_all( $regexp, str_replace( "'", '', $string ), $matches );
return strtolower( implode( '-', $matches[0] ) );
//phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
/**
* Determines if the variable is a numeric-indexed array.
*

View File

@ -0,0 +1,40 @@
<?php
/**
* Tests for the _wp_to_kebab_case() function
*
* @since 5.8.0
*
* @group functions.php
* @covers ::_wp_to_kebab_case
*/
class Tests_Functions_wpToKebabCase extends WP_UnitTestCase {
public function test_wp_to_kebab_case() {
$this->assertEquals( 'white', _wp_to_kebab_case( 'white' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white+black' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white:black' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white*black' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white.black' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white black' ) );
$this->assertEquals( 'white-black', _wp_to_kebab_case( 'white black' ) );
$this->assertEquals( 'white-to-black', _wp_to_kebab_case( 'white-to-black' ) );
$this->assertEquals( 'white-2-white', _wp_to_kebab_case( 'white2white' ) );
$this->assertEquals( 'white-2nd', _wp_to_kebab_case( 'white2nd' ) );
$this->assertEquals( 'white-2-ndcolor', _wp_to_kebab_case( 'white2ndcolor' ) );
$this->assertEquals( 'white-2nd-color', _wp_to_kebab_case( 'white2ndColor' ) );
$this->assertEquals( 'white-2nd-color', _wp_to_kebab_case( 'white2nd_color' ) );
$this->assertEquals( 'white-23-color', _wp_to_kebab_case( 'white23color' ) );
$this->assertEquals( 'white-23', _wp_to_kebab_case( 'white23' ) );
$this->assertEquals( '23-color', _wp_to_kebab_case( '23color' ) );
$this->assertEquals( 'white-4th', _wp_to_kebab_case( 'white4th' ) );
$this->assertEquals( 'font-2-xl', _wp_to_kebab_case( 'font2xl' ) );
$this->assertEquals( 'white-to-white', _wp_to_kebab_case( 'whiteToWhite' ) );
$this->assertEquals( 'white-t-owhite', _wp_to_kebab_case( 'whiteTOwhite' ) );
$this->assertEquals( 'whit-eto-white', _wp_to_kebab_case( 'WHITEtoWHITE' ) );
$this->assertEquals( '42', _wp_to_kebab_case( 42 ) );
$this->assertEquals( 'ive-done', _wp_to_kebab_case( "i've done" ) );
$this->assertEquals( 'ffffff', _wp_to_kebab_case( '#ffffff' ) );
$this->assertEquals( 'ffffff', _wp_to_kebab_case( '$ffffff' ) );
}
}

View File

@ -294,11 +294,11 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
);
$this->assertSame(
'body{--wp--preset--color--grey: grey;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
'body{--wp--preset--color--grey: grey;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet()
);
$this->assertSame(
'body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
'body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet( 'block_styles' )
);
$this->assertSame(
@ -329,7 +329,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
);
$this->assertSame(
'h1.has-white-color,h2.has-white-color,h3.has-white-color,h4.has-white-color,h5.has-white-color,h6.has-white-color{color: #fff !important;}h1.has-white-background-color,h2.has-white-background-color,h3.has-white-background-color,h4.has-white-background-color,h5.has-white-background-color,h6.has-white-background-color{background-color: #fff !important;}',
'h1.has-white-color,h2.has-white-color,h3.has-white-color,h4.has-white-color,h5.has-white-color,h6.has-white-color{color: var(--wp--preset--color--white) !important;}h1.has-white-background-color,h2.has-white-background-color,h3.has-white-background-color,h4.has-white-background-color,h5.has-white-background-color,h6.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}',
$theme_json->get_stylesheet( 'block_styles' )
);
}
@ -365,11 +365,11 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
);
$this->assertSame(
'.wp-block-group{--wp--preset--color--grey: grey;}.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: grey !important;}.wp-block-group.has-grey-background-color{background-color: grey !important;}',
'.wp-block-group{--wp--preset--color--grey: grey;}.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: var(--wp--preset--color--grey) !important;}.wp-block-group.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet()
);
$this->assertSame(
'.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: grey !important;}.wp-block-group.has-grey-background-color{background-color: grey !important;}',
'.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: var(--wp--preset--color--grey) !important;}.wp-block-group.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet( 'block_styles' )
);
}
@ -407,7 +407,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
);
$this->assertSame(
'body{--wp--preset--color--grey: grey;}p{background-color: blue;color: red;font-size: 12px;line-height: 1.3;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
'body{--wp--preset--color--grey: grey;}p{background-color: blue;color: red;font-size: 12px;line-height: 1.3;}.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}',
$theme_json->get_stylesheet()
);
}