Skip to content

Commit a044651

Browse files
committed
Fix data-wp-each-child value assignment
1 parent 901a358 commit a044651

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@ private function data_wp_router_region_processor( WP_Interactivity_API_Directive
11961196
* `template` tag.
11971197
*
11981198
* @since 6.5.0
1199+
* @since 6.9.0 Include the list path in the rendered `data-wp-each-child` directives.
11991200
*
12001201
* @param WP_Interactivity_API_Directives_Processor $p The directives processor instance.
12011202
* @param string $mode Whether the processing is entering or exiting the tag.
@@ -1264,10 +1265,20 @@ private function data_wp_each_processor( WP_Interactivity_API_Directives_Process
12641265
return;
12651266
}
12661267

1267-
// Adds the `data-wp-each-child` to each top-level tag.
1268+
/*
1269+
* Adds the `data-wp-each-child` directive to each top-level tag
1270+
* rendered by this `data-wp-each` directive. The value is the
1271+
* `data-wp-each` directive's namespace and path.
1272+
*
1273+
* Nested `data-wp-each` directives could render
1274+
* `data-wp-each-child` elements at the top level as well, and
1275+
* they should be ignored.
1276+
*/
12681277
$i = new WP_Interactivity_API_Directives_Processor( $processed_item );
12691278
while ( $i->next_tag() ) {
1270-
$i->set_attribute( 'data-wp-each-child', $namespace_value . '::' . $path );
1279+
if ( ! $i->get_attribute( 'data-wp-each-child' ) ) {
1280+
$i->set_attribute( 'data-wp-each-child', $namespace_value . '::' . $path );
1281+
}
12711282
$i->next_balanced_tag_closer_tag();
12721283
}
12731284
$processed_content .= $i->get_updated_html();

tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,18 @@ public function test_wp_each_nested_template_tags() {
473473
'<span data-wp-text="myPlugin::context.item2"></span>' .
474474
'</template>' .
475475
'</template>' .
476-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">1</span>' .
477-
'<template data-wp-each-child data-wp-each--item2="myPlugin::state.list2">' .
476+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item1">1</span>' .
477+
'<template data-wp-each-child="myPlugin::state.list" data-wp-each--item2="myPlugin::state.list2">' .
478478
'<span data-wp-text="myPlugin::context.item2"></span>' .
479479
'</template>' .
480-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">3</span>' .
481-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">4</span>' .
482-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">2</span>' .
483-
'<template data-wp-each-child data-wp-each--item2="myPlugin::state.list2">' .
480+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">3</span>' .
481+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">4</span>' .
482+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item1">2</span>' .
483+
'<template data-wp-each-child="myPlugin::state.list" data-wp-each--item2="myPlugin::state.list2">' .
484484
'<span data-wp-text="myPlugin::context.item2"></span>' .
485485
'</template>' .
486-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">3</span>' .
487-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">4</span>' .
486+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">3</span>' .
487+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">4</span>' .
488488
'<div id="after-wp-each" data-wp-bind--id="myPlugin::state.after">Text</div>';
489489
$new = $this->interactivity->process_directives( $original );
490490
$this->assertSame( $expected, $new );
@@ -515,22 +515,22 @@ public function test_wp_each_directly_nested_template_tags() {
515515
'<span data-wp-text="myPlugin::context.item2"></span>' .
516516
'</template>' .
517517
'</template>' .
518-
'<template data-wp-each-child data-wp-each--item2="myPlugin::state.list2">' .
518+
'<template data-wp-each-child="myPlugin::state.list" data-wp-each--item2="myPlugin::state.list2">' .
519519
'<span data-wp-text="myPlugin::context.item1"></span>' .
520520
'<span data-wp-text="myPlugin::context.item2"></span>' .
521521
'</template>' .
522-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">1</span>' .
523-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">3</span>' .
524-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">1</span>' .
525-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">4</span>' .
526-
'<template data-wp-each-child data-wp-each--item2="myPlugin::state.list2">' .
522+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item1">1</span>' .
523+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">3</span>' .
524+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item1">1</span>' .
525+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">4</span>' .
526+
'<template data-wp-each-child="myPlugin::state.list" data-wp-each--item2="myPlugin::state.list2">' .
527527
'<span data-wp-text="myPlugin::context.item1"></span>' .
528528
'<span data-wp-text="myPlugin::context.item2"></span>' .
529529
'</template>' .
530-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">2</span>' .
531-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">3</span>' .
532-
'<span data-wp-each-child data-wp-text="myPlugin::context.item1">2</span>' .
533-
'<span data-wp-each-child data-wp-text="myPlugin::context.item2">4</span>' .
530+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item1">2</span>' .
531+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">3</span>' .
532+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item1">2</span>' .
533+
'<span data-wp-each-child="myPlugin::state.list2" data-wp-text="myPlugin::context.item2">4</span>' .
534534
'<div id="after-wp-each" data-wp-bind--id="myPlugin::state.after">Text</div>';
535535
$new = $this->interactivity->process_directives( $original );
536536
$this->assertSame( $expected, $new );
@@ -559,16 +559,16 @@ public function test_wp_each_nested_template_tags_using_previous_item_as_list()
559559
'<span data-wp-text="myPlugin::context.number"></span>' .
560560
'</template>' .
561561
'</template>' .
562-
'<template data-wp-each-child data-wp-each--number="myPlugin::context.list">' .
562+
'<template data-wp-each-child="myPlugin::state.list2" data-wp-each--number="myPlugin::context.list">' .
563563
'<span data-wp-text="myPlugin::context.number"></span>' .
564564
'</template>' .
565-
'<span data-wp-each-child data-wp-text="myPlugin::context.number">1</span>' .
566-
'<span data-wp-each-child data-wp-text="myPlugin::context.number">2</span>' .
567-
'<template data-wp-each-child data-wp-each--number="myPlugin::context.list">' .
565+
'<span data-wp-each-child="myPlugin::context.list" data-wp-text="myPlugin::context.number">1</span>' .
566+
'<span data-wp-each-child="myPlugin::context.list" data-wp-text="myPlugin::context.number">2</span>' .
567+
'<template data-wp-each-child="myPlugin::state.list2" data-wp-each--number="myPlugin::context.list">' .
568568
'<span data-wp-text="myPlugin::context.number"></span>' .
569569
'</template>' .
570-
'<span data-wp-each-child data-wp-text="myPlugin::context.number">3</span>' .
571-
'<span data-wp-each-child data-wp-text="myPlugin::context.number">4</span>' .
570+
'<span data-wp-each-child="myPlugin::context.list" data-wp-text="myPlugin::context.number">3</span>' .
571+
'<span data-wp-each-child="myPlugin::context.list" data-wp-text="myPlugin::context.number">4</span>' .
572572
'<div id="after-wp-each" data-wp-bind--id="myPlugin::state.after">Text</div>';
573573
$new = $this->interactivity->process_directives( $original );
574574
$this->assertSame( $expected, $new );
@@ -671,15 +671,15 @@ public function test_wp_each_doesnt_process_with_manual_server_directive_process
671671
'<template data-wp-each="myPlugin::state.list">' .
672672
'<span data-wp-text="myPlugin::context.item"></span>' .
673673
'</template>' .
674-
'<span data-wp-each-child data-wp-text="myPlugin::context.item">1</span>' .
675-
'<span data-wp-each-child data-wp-text="myPlugin::context.item">2</span>' .
674+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item">1</span>' .
675+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item">2</span>' .
676676
'<div data-wp-bind--id="myPlugin::state.after">Text</div>';
677677
$expected = '' .
678678
'<template data-wp-each="myPlugin::state.list">' .
679679
'<span data-wp-text="myPlugin::context.item"></span>' .
680680
'</template>' .
681-
'<span data-wp-each-child data-wp-text="myPlugin::context.item">1</span>' .
682-
'<span data-wp-each-child data-wp-text="myPlugin::context.item">2</span>' .
681+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item">1</span>' .
682+
'<span data-wp-each-child="myPlugin::state.list" data-wp-text="myPlugin::context.item">2</span>' .
683683
'<div id="after-wp-each" data-wp-bind--id="myPlugin::state.after">Text</div>';
684684
$new = $this->interactivity->process_directives( $original );
685685
$this->assertSame( $expected, $new );

0 commit comments

Comments
 (0)