Repository navigation
Add an opt-in "join" to "set" for list values - #124
Contributolo wants to merge 2 commits into
Conversation
Extension "set" rules evaluate their template once per entry of a list value (multiple select, checkbox, token). For class and style every entry adds a class or a declaration, but any other attribute was written with WP_HTML_Tag_Processor::set_attribute() once per entry, so only the last entry survived: choosing two languages rendered data-langs="de". The editor filter overwrote its wrapper props the same way. The evaluated values of a list are now collected and written once, separated by single spaces like an HTML token list, so selectors such as [data-langs~="en"] match every chosen entry. Empty and repeated results are skipped, and a list without any result writes nothing, as an empty list already did. Single values and the class and style handling are unchanged. In the editor, each list entry is now also the field's value while its template is evaluated, as render_blocks() already does. Without that, token lists showed "a,b" and lists with returnFormat "both" kept the unresolved placeholder in the editor.
dnnsjsk
left a comment
There was a problem hiding this comment.
Joining values solves the data-langs example, but the implementation applies the new semantics to every attribute except class/style. Single-valued attributes are affected too: a valid existing set binding targeting href with values /first/ and /second/ now emits href="/first/ /second/" instead of the previous /second/. IDs and other single-valued attributes have the same compatibility problem. I verified the new join_list_values() returns the combined URL string, and both render_blocks() and the editor path assign that result unconditionally.
Please limit aggregation to attributes with defined token-list semantics or make the joining policy explicit/opt-in, preserving existing behavior for single-valued URL/ID bindings. Add server and editor regressions for href/id as well as the intended data/token-list attributes. This is a behavior change beyond the data attribute example, so I am holding the current version out of 7.7.2.
The previous commit joined the evaluated values of a list for every
attribute other than class and style. That changed existing bindings to
single-valued attributes: two links bound to href rendered
href="/first/%20/second/" instead of "/second/", and an id received two
tokens.
Joining is now opt-in per set entry with "join": true. Without it, every
entry of a list overwrites the attribute and the last one wins, in
render_blocks() and in the editor, as before. With it, the results of
all entries are written once, separated by single spaces; empty and
repeated results are skipped, and nothing is written when no result
remains. "join" has no effect on class and style, which add one class or
declaration per entry either way, or on a field that returns a single
value. The option is documented and declared in the extension schema.
The editor still evaluates each list entry as the field's value, as
render_blocks() does. It now also reduces stored token entries
({ value, label }) to the return format like the server, so token lists
no longer render "[object Object]" in the editor.
Server regressions cover href, id, data attributes, tokens and
returnFormat "both" with and without join, empty and repeated results,
class and style. An e2e test compares the editor wrapper (its DOM and
the props the extension filter passes) with the frontend for the same
stored values, before and after a checkbox is changed in the inspector.
|
Thanks for the review, agreed. Reworked in 47ea601:
Unchanged and the same on main: the server sorts checkbox selections into option order while the editor keeps the stored order, so with options chosen out of order the editor can show a different last entry or token order than the frontend. I can send that as a separate PR if you want it. I also updated the title and description of this PR to the opt-in behavior. |
|
Included in Blockstudio 7.7.4. The opt-in join behavior for extension set rules has shipped, with matching editor/frontend behavior and regression coverage. Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution. |
Extension
setrules evaluate their template once per entry of a list value (selectwithmultiple,checkbox,token). Forclassandstyleevery entry adds a class or declaration, but any other attribute is written withWP_HTML_Tag_Processor::set_attribute()once per entry, so only the last entry survives. That is right for single-valued attributes likehreforid, but a token list such asdata-langswith two chosen languages rendersdata-langs="de".This adds an opt-in
jointo asetentry:With
"join": true, the evaluated values of a list are written once, separated by single spaces like an HTML token list, so[data-langs~="en"]matches every chosen entry. Empty and repeated results are skipped, and nothing is written when no result remains. Withoutjoin(or with"join": false) every attribute behaves as on main: the last entry wins.joinhas no effect onclassandstyleor on single values.In the editor, each list entry is now also the field's value while its template is evaluated, which is what
render_blocks()already does, and stored token entries ({ value, label }) are reduced to the return format liketransform_option_attribute(). On main the editor wrote the unresolved placeholder forreturnFormat: "both"lists and[object Object],[object Object]for token fields while the frontend wrote the last entry; editor and frontend now match.Changes
Extensions::render_blocks(): opt-in join for attributes other than class and style; helperExtensions::join_list_values()src/blocks/filters/extensions.tsx: the same opt-in throughjoinListValues()(src/blocks/utils/join-list-values.ts), per-entry evaluation, token reductionschemas/extend.json:setitems withattribute,value,join(checksum updated)Tests
tests/unit/extensions/ExtensionSetTest.php(18 tests): href and id with two values (last wins), data attribute without join and withjoin: false,returnFormat: "both"and tokens with and without join, repeated and empty results, empty list, class and style with and without join, single value. On main 5 fail (the join cases), on the first commit of this PR 6 fail (the cases without join)tests/unit/extensions/ExtensionsTest.php:join_list_values()tests/client/join-list-values.test.ts:joinListValues()tests/e2e/types/other/extension-set.tswith the fixtureextensions/set-list.jsononcore/verse: frontend, editor canvas and thewrapperPropsof the extension filter for the same stored values, before and after checking another option and savingChecked locally in WordPress Playground CLI: the new e2e test,
types/text.ts, the extensions, field, render, block and attribute-builder unit suites,npm run check, PHPCS and PHPStan. The bundle inincludes/admin/assetsis not rebuilt in this PR.