| 1 | | Work in progress |
| 2 | | Backport from gutenberg |
| | 1 | Add Block Bindings support for the List Item block's `content` attribute. |
| | 2 | |
| | 3 | This needs more than registering the attribute, because a List Item stores its rich text and a nested List in the same `<li>`: |
| | 4 | |
| | 5 | {{{ |
| | 6 | <!-- wp:list-item --> |
| | 7 | <li>My text |
| | 8 | <!-- wp:list --> |
| | 9 | <ul class="wp-block-list"><!-- wp:list-item --> |
| | 10 | <li>Nested item</li> |
| | 11 | <!-- /wp:list-item --></ul> |
| | 12 | <!-- /wp:list --></li> |
| | 13 | <!-- /wp:list-item --> |
| | 14 | }}} |
| | 15 | |
| | 16 | `WP_Block::replace_html()` replaces everything between the selector's opening and closing tag for a rich-text attribute. It has no awareness that part of that content came from inner blocks, so binding the list item `content` overwrites the nested list: |
| | 17 | |
| | 18 | {{{ |
| | 19 | <li>Bound value</li> |
| | 20 | }}} |
| | 21 | |
| | 22 | '''The fix:''' a block's own rich text always renders before its inner blocks, so the replacement only needs to run up to where the first inner block begins. `WP_Block::render()` already builds the output by walking `inner_content`, so it knows each inner block's byte offset and can hand those to the replacement, which stops at the first inner block inside the selector. |
| | 23 | |
| | 24 | This is general — there is no list-item-specific code in the render path, so it works for any block that places an inner block inside a bound rich-text element. Blocks without inner blocks are unaffected. |
| | 25 | |
| | 26 | Because it uses real inner-block positions, it also tells apart raw `<ul>` markup typed into the text (part of the rich text, replaced) from an actual nested block (preserved). |
| | 27 | |
| | 28 | `core/list-item` is the only core block today whose bound rich-text selector contains an inner block, so this also adds `content` to its supported binding attributes. The other multi-rich-text blocks (table, pullquote, quote, file, gallery) keep their inner content outside the bound selector and are unaffected. |
| | 29 | |
| | 30 | |
| | 31 | Steps to reproduce: |
| | 32 | 1. Register a binding source returning a plain string. |
| | 33 | 2. Insert a List, add a list item with a nested list under it. |
| | 34 | 3. Bind the outer list item's `content` to the source. |
| | 35 | 4. View the post on the front end. |
| | 36 | |
| | 37 | Expected: the bound text replaces the list item's own text, the nested list stays. Actual (before patch): the nested list is dropped. |
| | 38 | |
| | 39 | Patch and unit tests in Gutenberg's PR [https://github.com/WordPress/wordpress-develop/pull/12113], which supersedes the registration-only patch in PR [https://github.com/WordPress/wordpress-develop/pull/12084]. |
| | 40 | |
| | 41 | Companion Gutenberg compatibility for WordPress 6.9/7.0 in WordPress/gutenberg#78991. |