Skip to content

Block Supports: Allow CSS custom properties in block gap sanitization - #82817

Open
noruzzamans wants to merge 2 commits into
WordPress:trunkfrom
noruzzamans:fix/grid-themejson-blockgap-preset-82747
Open

noruzzamans wants to merge 2 commits into
WordPress:trunkfrom
noruzzamans:fix/grid-themejson-blockgap-preset-82747

Conversation

@noruzzamans

@noruzzamans noruzzamans commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

What?

Closes #82747

Allows valid CSS custom property references (such as var(--wp--preset--spacing--32)) in gutenberg_sanitize_block_gap_value().

Why?

When a theme defines a preset block gap in theme.json (such as styles.spacing.blockGap: "var:preset|spacing|32"):

  1. gutenberg_get_global_styles() resolves the preset to its CSS variable format: var(--wp--preset--spacing--32).
  2. Candidate values for the global block gap in gutenberg_render_layout_support_flag() are passed through gutenberg_sanitize_block_gap_value().
  3. Because gutenberg_sanitize_block_gap_value() disallows parentheses (, ) to prevent CSS injection, the resolved preset was rejected and returned null.
  4. As a result, the Grid layout calculation in gutenberg_get_layout_style() fell back to 0.5em instead of using the theme's preset gap value.
  5. In Grid blocks with columnCount and minimumColumnWidth, the column calculation formula max(min(..., 100%), (100% - (gap * (n - 1))) / n) was computed against 0.5em, causing grid items to overflow or stack into a single column.

How?

  • Updated gutenberg_sanitize_block_gap_value() in lib/block-supports/layout.php to accept valid CSS custom property references matching /^var\(--[a-zA-Z0-9_-]+\)$/.
  • Added unit test cases in phpunit/block-supports/layout-test.php covering gutenberg_sanitize_block_gap_value() with valid and invalid CSS variables, including nested array handling.
  • Added a test case in data_gutenberg_get_layout_style() verifying that Grid layout with a preset CSS variable fallback correctly computes the grid-template-columns declaration.

Testing Instructions

  1. Run the PHPUnit test suite:
    npm run wp-env-test run -- --env-cwd='wp-content/plugins/gutenberg' wordpress vendor/bin/phpunit -c phpunit.xml.dist --filter test_sanitize_block_gap_value
    npm run wp-env-test run -- --env-cwd='wp-content/plugins/gutenberg' wordpress vendor/bin/phpunit -c phpunit.xml.dist --filter test_gutenberg_get_layout_style
  2. Verify all tests pass.
  3. Test with a block theme setting a preset block gap in theme.json:
    "styles": {
      "spacing": {
        "blockGap": "var:preset|spacing|32"
      }
    }
  4. Insert a Grid block with columnCount: 2 and minimumColumnWidth: "20rem".
  5. Check the rendered front-end CSS output for the grid container; verify grid-template-columns uses var(--wp--preset--spacing--32) rather than falling back to 0.5em.

Use of AI Tools

AI assistance: Yes
Model(s): Gemini 3.8 Flash High

Assisted by Gemini 3.8 Flash High to investigate the root cause in layout.php and compose the PHPUnit test cases.

Ensures gutenberg_sanitize_block_gap_value() accepts valid CSS custom property references such as var(--wp--preset--spacing--*). This fixes an issue where Grid layout calculations fell back to 0.5em when using global theme.json blockGap presets on capped columns.

Fixes WordPress#82747.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: kadamwhite <kadamwhite@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Layout Layout block support, its UI controls, and style output. labels Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Layout Layout block support, its UI controls, and style output. [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid layout ignores theme.json blockGap presets when computing capped columns

2 participants