Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3d51183
Initial commit: add typography to style engine
ramonjd Apr 6, 2022
24e9f57
Adding whitespace to inline output
ramonjd Apr 7, 2022
f257752
Introduce spacing to inline styles
ramonjd Apr 7, 2022
70f32de
Removing generating styles based on an options path since we're not u…
ramonjd Apr 7, 2022
9b7b31a
Perform kebab case normalization in the style engine.
ramonjd Apr 13, 2022
75ce88b
Remove unused arg $options
ramonjd Apr 13, 2022
b9d9ba4
Refactor style engine so that `generate()` will return an array of cs…
ramonjd Apr 14, 2022
c4ae1d3
Remove 'inline' as an option until we need something different
ramonjd Apr 14, 2022
510f6bb
Only add the required classnames if there's a valid style value or cl…
ramonjd Apr 14, 2022
86b8880
Removed options arg until we need it. Thanks, linter.
ramonjd Apr 14, 2022
e7affed
Refactor generate to accept string CSS preset property values (for no…
ramonjd Apr 19, 2022
c3d314e
Updated color and typography to use the preset value for the style en…
ramonjd Apr 19, 2022
44ddde4
No short ternaries says the linter. Booooh!
ramonjd Apr 19, 2022
df6077f
Cleaning up. Reinstating deprecated function so we don't forget to de…
ramonjd Apr 20, 2022
e3f4b11
Wherefore art thou, vile linter!
ramonjd Apr 20, 2022
4f9502f
Updating inline docs
ramonjd Apr 22, 2022
65cf61c
Created public interface method gutenberg_style_engine_generate
ramonjd Apr 26, 2022
d328e64
Linter, thy foul stench offends me!
ramonjd Apr 26, 2022
951ea74
Using the correct preset property keys for colors and gradients ensur…
ramonjd Apr 26, 2022
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
Prev Previous commit
Next Next commit
Created public interface method gutenberg_style_engine_generate
  • Loading branch information
ramonjd committed Apr 26, 2022
commit 65cf61c5c9afd27fd6c1db7afcb40cad42172b35
3 changes: 1 addition & 2 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}

$attributes = array();
$style_engine = gutenberg_get_style_engine();
$styles = $style_engine->generate( array( 'color' => $color_block_styles ) );
$styles = gutenberg_style_engine_generate( array( 'color' => $color_block_styles ) );

if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
Expand Down
3 changes: 1 addition & 2 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
return $attributes;
}

$style_engine = gutenberg_get_style_engine();
$skip_padding = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
$skip_margin = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
$spacing_block_styles = array();
$spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
$spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null;
$styles = $style_engine->generate(
$styles = gutenberg_style_engine_generate(
array( 'spacing' => $spacing_block_styles )
);

Expand Down
5 changes: 2 additions & 3 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['letterSpacing'], 'letter-spacing' );
}

$attributes = array();
$style_engine = gutenberg_get_style_engine();
$styles = $style_engine->generate( array( 'typography' => $typography_block_styles ) );
$attributes = array();
$styles = gutenberg_style_engine_generate( array( 'typography' => $typography_block_styles ) );

if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
Expand Down
26 changes: 20 additions & 6 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*
* Consolidates rendering block styles to reduce duplication and streamline
* CSS styles generation.
*
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
* This is a low-level API that may need to do breaking changes. Please, use gutenberg_style_engine_get_styles instead.
*
* @access private
*/
class WP_Style_Engine {
/**
Expand Down Expand Up @@ -169,10 +174,9 @@ protected static function get_classnames( $style_value, $style_definition ) {
* @return array An array of CSS rules.
*/
protected static function get_css( $style_value, $style_definition ) {
$css = array();
// Low-specificity check to see if the value is a CSS preset.
if ( is_string( $style_value ) && strpos( $style_value, 'var:' ) !== false ) {
return $css;
return array();
}

// If required in the future, style definitions could define a callable `value_func` to generate custom CSS rules.
Expand Down Expand Up @@ -269,12 +273,22 @@ protected static function get_css_rules( $style_value, $style_property ) {
}

/**
* This function returns the Style Engine instance.
* Global public interface method to WP_Style_Engine->generate.
*
* Returns an CSS ruleset.
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
* @param array $block_styles An array of styles from a block's attributes.
*
* @return WP_Style_Engine
* @return array|null array(
* 'styles' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag.
* 'classnames' => (string) Classnames separated by a space.
* );
*/
function wp_get_style_engine() {
function wp_style_engine_generate( $block_styles ) {
if ( class_exists( 'WP_Style_Engine' ) ) {
return WP_Style_Engine::get_instance();
$style_engine = WP_Style_Engine::get_instance();
return $style_engine->generate( $block_styles );
}
return null;
}
3 changes: 1 addition & 2 deletions packages/style-engine/phpunit/class-wp-style-engine-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
* @dataProvider data_generate_styles_fixtures
*/
function test_generate_styles( $block_styles, $expected_output ) {
$style_engine = wp_get_style_engine();
$generated_styles = $style_engine->generate( $block_styles );
$generated_styles = wp_style_engine_generate( $block_styles );
$this->assertSame( $expected_output, $generated_styles );
}

Expand Down