Skip to content
Closed
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
41 changes: 32 additions & 9 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3715,22 +3715,45 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
}
}
}
if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) {
/*
* Elements can be styled outside any breakpoint, inside one, or both,
* so collect the names from all of those places before looping. An
* element styled only inside a breakpoint still needs a node.
*/
$block_node = $theme_json['styles']['blocks'][ $name ] ?? array();
$element_names = array_keys( $block_node['elements'] ?? array() );
foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
$element_names = array_merge(
$element_names,
array_keys( $block_node[ $breakpoint ]['elements'] ?? array() )
);
}
$element_names = array_unique( $element_names );

if ( ! empty( $element_names ) ) {
foreach ( $element_names as $element ) {
$element_path = array( 'styles', 'blocks', $name, 'elements', $element );
if ( $include_node_paths_only ) {
$nodes[] = array(
'path' => $element_path,
);
if ( isset( $block_node['elements'][ $element ] ) ) {
$nodes[] = array(
'path' => $element_path,
);
}
continue;
}

if ( ! isset( $selectors[ $name ]['elements'][ $element ] ) ) {
continue;
}

$element_selector = $selectors[ $name ]['elements'][ $element ];

$nodes[] = array(
'path' => $element_path,
'selector' => $element_selector,
);
if ( isset( $block_node['elements'][ $element ] ) ) {
$nodes[] = array(
'path' => $element_path,
'selector' => $element_selector,
);
}

// Responsive element nodes: one node per breakpoint that has
// styles for this element. Cascade: a{} → @media{a{}}
Expand Down
112 changes: 112 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,118 @@ public function test_get_styles_for_block_outputs_responsive_block_gap_after_def
$this->assertLessThan( strpos( $actual_styles, $mobile_gap ), strpos( $actual_styles, $default_gap ) );
}

/**
* @ticket 65829
*/
public function test_get_stylesheet_renders_element_styles_defined_only_in_a_breakpoint() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'red',
),
),
),
),
),
),
),
)
);

$expected = '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button))){color: red;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65829
*/
public function test_get_stylesheet_renders_element_pseudo_styles_defined_only_in_a_breakpoint() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
':hover' => array(
'color' => array(
'text' => 'red',
),
),
),
),
),
),
),
),
)
);

$expected = '@media (width <= 480px){:root :where(.wp-block-group a:where(:not(.wp-element-button)):hover){color: red;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65829
*/
public function test_get_stylesheet_renders_element_styles_defined_only_in_separate_breakpoints() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/group' => array(
'@mobile' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'red',
),
),
),
),
'@tablet' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'blue',
),
),
),
),
),
),
),
)
);

$link_selector = ':root :where(.wp-block-group a:where(:not(.wp-element-button)))';
$expected = '@media (width <= 480px){' . $link_selector . '{color: red;}}' .
'@media (480px < width <= 782px){' . $link_selector . '{color: blue;}}';

$this->assertSame(
$expected,
$theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) )
);
}

/**
* @ticket 65164
*/
Expand Down
Loading