Skip to content

Read single-quoted attributes on RichText and other component tags - #195

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/single-quoted-component-attributes
Nov 2, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/single-quoted-component-attributes

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

<RichText attribute='title' tag='h2' /> renders as an empty string, while the same tag with double quotes renders the value. The same applies to every attribute of a component tag written with single quotes or without quotes.

Cause

Block::replace_custom_tag() parses the attributes of the tag with

'/([a-zA-Z_][a-zA-Z0-9\-_:.]*)(?:\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^ \/>]+)))?/'

and then takes $match[2] ?? ( $match[3] ?? ( $match[4] ?? true ) ). When group 3 or 4 matches, preg_match_all() fills the skipped group 2 with '', not null, so ?? stops there. attribute becomes '', no value is resolved, and the tag is replaced with nothing. For tag='h2' the element would be <p>.

The other attribute parsers with the same pattern (Block_Tags, the list renderer, Render_Document) already skip empty groups, so only this one is affected.

Fix

PREG_UNMATCHED_AS_NULL on that preg_match_all() call. Unmatched groups are null, the existing fallback reaches the group that matched. A matched empty value (class="") is still '', a bare attribute (useBlockProps) is still true.

Tests

BlockTest::test_replace_custom_tag_reads_single_quoted_attributes renders the same RichText tag with double and single quotes and expects identical output. It fails before the change (single quotes give '') and passes after. tests/unit/block/BlockTest.php runs green locally (63 tests) against a local WordPress; the full suites are left to CI as per AGENTS.md.

@dnnsjsk
dnnsjsk merged commit 2d02366 into inline0:main Nov 2, 2026
@dnnsjsk

dnnsjsk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged and released in Blockstudio 7.7.11. The full CI and distribution checks passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants