Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core CSS support for root padding #42034

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for global styles
andrewserong committed Jul 1, 2022
commit 694d71aa6563d6648c9bea8ec33da93087d82679
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { __EXPERIMENTAL_ELEMENTS as ELEMENTS } from '@wordpress/blocks';
* Internal dependencies
*/
import {
getLayoutStyles,
getNodesWithSettings,
getNodesWithStyles,
toCustomProperties,
@@ -450,4 +451,144 @@ describe( 'global styles renderer', () => {
);
} );
} );

describe( 'getLayoutStyles', () => {
const layoutDefinitionsTree = {
settings: {
layout: {
definitions: {
default: {
name: 'default',
slug: 'flow',
className: 'is-layout-flow',
baseStyles: [
{
selector: ' > .alignleft',
rules: {
float: 'left',
'margin-inline-start': '0',
'margin-inline-end': '2em',
},
},
{
selector: ' > .alignright',
rules: {
float: 'right',
'margin-inline-start': '2em',
'margin-inline-end': '0',
},
},
{
selector: ' > .aligncenter',
rules: {
'margin-left': 'auto !important',
'margin-right': 'auto !important',
},
},
],
spacingStyles: [
{
selector: ' > *',
rules: {
'margin-block-start': '0',
'margin-block-end': '0',
},
},
{
selector: ' > * + *',
rules: {
'margin-block-start': null,
'margin-block-end': '0',
},
},
],
},
flex: {
name: 'flex',
slug: 'flex',
className: 'is-layout-flex',
baseStyles: [
{
selector: '',
rules: {
display: 'flex',
'flex-wrap': 'wrap',
'align-items': 'center',
},
},
{
selector: ' > *',
rules: {
margin: '0',
},
},
],
spacingStyles: [
{
selector: '',
rules: {
gap: null,
},
},
],
},
},
},
},
};

it( 'should return fallback gap flex layout style if block styles are enabled and blockGap is disabled', () => {
const hasBlockGapSupport = false;
const hasBlockStylesSupport = true;
const style = { spacing: { blockGap: '12px' } };

const layoutStyles = getLayoutStyles(
layoutDefinitionsTree,
style,
'wp-block-test',
hasBlockGapSupport,
hasBlockStylesSupport
);

expect( layoutStyles ).toEqual(
'wp-block-test.is-layout-flex { gap: 0.5em; }'
);
} );

it( 'should return fallback gap layout styles if blockGap is enabled, but there is no blockGap value', () => {
const hasBlockGapSupport = true;
const hasBlockStylesSupport = true;
const style = {};

const layoutStyles = getLayoutStyles(
layoutDefinitionsTree,
style,
'body',
hasBlockGapSupport,
hasBlockStylesSupport
);

expect( layoutStyles ).toEqual(
'body .is-layout-flow > * { margin-block-start: 0; margin-block-end: 0; }body .is-layout-flow > * + * { margin-block-start: 0.5em; margin-block-end: 0; }body .is-layout-flex { gap: 0.5em; }body { --wp--style--block-gap: 0.5em; }body .is-layout-flow > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em }body .is-layout-flow > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0 }body .is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important }body .is-layout-flex { display: flex; flex-wrap: wrap; align-items: center }body .is-layout-flex > * { margin: 0 }'
);
} );

it( 'should return real gap layout style if blockGap is enabled, and base styles', () => {
const hasBlockGapSupport = true;
const hasBlockStylesSupport = true;
const style = { spacing: { blockGap: '12px' } };

const layoutStyles = getLayoutStyles(
layoutDefinitionsTree,
style,
'body',
hasBlockGapSupport,
hasBlockStylesSupport
);

expect( layoutStyles ).toEqual(
'body .is-layout-flow > * { margin-block-start: 0; margin-block-end: 0; }body .is-layout-flow > * + * { margin-block-start: 12px; margin-block-end: 0; }body .is-layout-flex { gap: 12px; }body { --wp--style--block-gap: 12px; }body .is-layout-flow > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em }body .is-layout-flow > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0 }body .is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important }body .is-layout-flex { display: flex; flex-wrap: wrap; align-items: center }body .is-layout-flex > * { margin: 0 }'
);
} );
} );
} );
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ function getStylesDeclarations( blockStyles = {} ) {
* @param {boolean} hasBlockStylesSupport Whether or not the theme opts-in to `wp-block-styles` support.
* @return {string} Generated CSS rules for the layout styles.
*/
function getLayoutStyles(
export function getLayoutStyles(
tree,
style,
selector,
@@ -261,7 +261,7 @@ function getLayoutStyles(
tree?.settings?.layout?.definitions
) {
gapValue = hasBlockGapSupport
? getGapCSSValue( gapValue, '0.5em' )
? getGapCSSValue( gapValue, '0.5em' ) || '0.5em'
: '0.5em';
Object.values( tree.settings.layout.definitions ).forEach(
( { className, name, spacingStyles } ) => {
@@ -297,7 +297,7 @@ function getLayoutStyles(
}`;
ruleset += `${ combinedSelector } { ${ declarations.join(
'; '
) } }`;
) }; }`;
}
} );
}