Skip to content

Commit 76c9c76

Browse files
committed
Build/Test Tools: Ensure assertEqualHTML() recognizes whitespace text.
Ensure whitespace text nodes are correctly represented by `build_visual_html_tree()`. The `build_visual_html_tree()` function used by `assertEqualHTML()` would remove some leading whitespace from text nodes. Some whitespace-only text nodes were omitted from the tree. Developed in WordPress#10765. Follow-up to [60295]. Props jonsurrell, dmsnell, bernhard-reiter. Fixes #64531.
1 parent b9c0308 commit 76c9c76

6 files changed

Lines changed: 314 additions & 208 deletions

File tree

tests/phpunit/includes/build-visual-html-tree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static function ( $a, $b ) {
202202
case '#cdata-section':
203203
case '#text':
204204
$text_content = $processor->get_modifiable_text();
205-
if ( '' === trim( $text_content, " \f\t\r\n" ) ) {
205+
if ( '' === $text_content ) {
206206
break;
207207
}
208208
$was_text = true;
@@ -237,7 +237,7 @@ static function ( $a, $b ) {
237237
++$indent_level;
238238
}
239239

240-
// If they're no attributes, we're done here.
240+
// When no attributes are present, there’s nothing left to do.
241241
if ( empty( $block_attrs ) ) {
242242
break;
243243
}

tests/phpunit/tests/blocks/wpBlock.php

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -368,32 +368,39 @@ public function test_render_applies_dynamic_render_block_filter() {
368368
* @return array
369369
*/
370370
public function data_provider_test_render_enqueues_scripts_and_styles(): array {
371-
$block_markup = '
372-
<!-- wp:static -->
373-
<div class="static">
374-
<!-- wp:static-child -->
375-
<div class="static-child">First child</div>
376-
<!-- /wp:static-child -->
377-
<!-- wp:dynamic /-->
378-
<!-- wp:static-child -->
379-
<div class="static-child">Last child</div>
380-
<!-- /wp:static-child -->
381-
</div>
382-
<!-- /wp:static -->
383-
';
371+
$block_markup = <<<'HTML'
372+
<!-- wp:static -->
373+
<div class="static">
374+
<!-- wp:static-child -->
375+
<div class="static-child">First child</div>
376+
<!-- /wp:static-child -->
377+
<!-- wp:dynamic /-->
378+
<!-- wp:static-child -->
379+
<div class="static-child">Last child</div>
380+
<!-- /wp:static-child -->
381+
</div>
382+
<!-- /wp:static -->
383+
HTML;
384384

385385
// TODO: Add case where a dynamic block renders other blocks?
386386
return array(
387387
'all_printed' => array(
388388
'set_up' => null,
389389
'block_markup' => $block_markup,
390-
'expected_rendered_block' => '
391-
<div class="static">
392-
<div class="static-child">First child</div>
393-
<p class="dynamic">Hello World!</p>
394-
<div class="static-child">Last child</div>
395-
</div>
396-
',
390+
'expected_rendered_block' => <<<'HTML'
391+
392+
<div class="static">
393+
394+
<div class="static-child">First child</div>
395+
396+
<p class="dynamic">Hello World!</p>
397+
398+
<div class="static-child">Last child</div>
399+
400+
</div>
401+
402+
HTML
403+
,
397404
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
398405
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
399406
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
@@ -414,13 +421,20 @@ static function ( $content ) {
414421
);
415422
},
416423
'block_markup' => $block_markup,
417-
'expected_rendered_block' => '
418-
<div class="static">
419-
<div class="static-child">First child</div>
420-
<p class="dynamic filtered">Hello World!</p>
421-
<div class="static-child">Last child</div>
422-
</div>
423-
',
424+
'expected_rendered_block' => <<<'HTML'
425+
426+
<div class="static">
427+
428+
<div class="static-child">First child</div>
429+
430+
<p class="dynamic filtered">Hello World!</p>
431+
432+
<div class="static-child">Last child</div>
433+
434+
</div>
435+
436+
HTML
437+
,
424438
'expected_styles' => array( 'static-view-style', 'dynamic-extra', 'static-child-view-style', 'dynamic-view-style' ),
425439
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
426440
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
@@ -430,12 +444,20 @@ static function ( $content ) {
430444
add_filter( 'render_block_core/dynamic', '__return_empty_string' );
431445
},
432446
'block_markup' => $block_markup,
433-
'expected_rendered_block' => '
434-
<div class="static">
435-
<div class="static-child">First child</div>
436-
<div class="static-child">Last child</div>
437-
</div>
438-
',
447+
'expected_rendered_block' => <<<'HTML'
448+
449+
<div class="static">
450+
451+
<div class="static-child">First child</div>
452+
453+
454+
455+
<div class="static-child">Last child</div>
456+
457+
</div>
458+
459+
HTML
460+
,
439461
'expected_styles' => array( 'static-view-style', 'static-child-view-style' ),
440462
'expected_scripts' => array( 'static-view-script', 'static-child-view-script' ),
441463
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module' ),
@@ -456,12 +478,20 @@ static function ( $enqueue, $block_name ) {
456478
);
457479
},
458480
'block_markup' => $block_markup,
459-
'expected_rendered_block' => '
460-
<div class="static">
461-
<div class="static-child">First child</div>
462-
<div class="static-child">Last child</div>
463-
</div>
464-
',
481+
'expected_rendered_block' => <<<'HTML'
482+
483+
<div class="static">
484+
485+
<div class="static-child">First child</div>
486+
487+
488+
489+
<div class="static-child">Last child</div>
490+
491+
</div>
492+
493+
HTML
494+
,
465495
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
466496
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
467497
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
@@ -488,11 +518,16 @@ static function ( $content ) {
488518
add_filter( 'render_block_core/static-child', '__return_empty_string' );
489519
},
490520
'block_markup' => $block_markup,
491-
'expected_rendered_block' => '
492-
<div class="static">
493-
<p class="dynamic">Hello World!</p>
494-
</div>
495-
',
521+
'expected_rendered_block' => <<<'HTML'
522+
523+
<div class="static">
524+
525+
<p class="dynamic">Hello World!</p>
526+
527+
</div>
528+
529+
HTML
530+
,
496531
'expected_styles' => array( 'static-view-style', 'dynamic-view-style' ),
497532
'expected_scripts' => array( 'static-view-script', 'dynamic-view-script' ),
498533
'expected_script_modules' => array( 'static-view-script-module', 'dynamic-view-script-module' ),
@@ -512,12 +547,18 @@ static function ( $content ) {
512547
);
513548
},
514549
'block_markup' => $block_markup,
515-
'expected_rendered_block' => '
516-
<div class="static">
517-
<div class="static-child">First child</div>
518-
<p class="dynamic">Hello World!</p>
519-
</div>
520-
',
550+
'expected_rendered_block' => <<<'HTML'
551+
552+
<div class="static">
553+
554+
<div class="static-child">First child</div>
555+
556+
<p class="dynamic">Hello World!</p>
557+
558+
</div>
559+
560+
HTML
561+
,
521562
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
522563
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
523564
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
@@ -562,9 +603,8 @@ static function ( $content ) {
562603
);
563604
},
564605
'block_markup' => '<!-- wp:static --><div class="static"></div><!-- /wp:static -->',
565-
'expected_rendered_block' => '
566-
<div class="static yes-admin-bar-script-enqueued yes-admin-bar-style-enqueued"></div>
567-
',
606+
'expected_rendered_block' =>
607+
'<div class="static yes-admin-bar-script-enqueued yes-admin-bar-style-enqueued"></div>',
568608
'expected_styles' => array( 'static-view-style', 'admin-bar' ),
569609
'expected_scripts' => array( 'static-view-script', 'admin-bar' ),
570610
'expected_script_modules' => array( 'static-view-script-module' ),
@@ -668,7 +708,7 @@ public function test_render_enqueues_scripts_and_styles( ?Closure $set_up, strin
668708
);
669709

670710
// TODO: Why not use do_blocks() instead?
671-
$parsed_blocks = parse_blocks( trim( $block_markup ) );
711+
$parsed_blocks = parse_blocks( $block_markup );
672712
$parsed_block = $parsed_blocks[0];
673713
$context = array();
674714
$block = new WP_Block( $parsed_block, $context, $this->registry );
@@ -682,7 +722,7 @@ public function test_render_enqueues_scripts_and_styles( ?Closure $set_up, strin
682722
$expected_rendered_block,
683723
$rendered_block,
684724
'<body>',
685-
"Rendered block does not contain expected HTML:\n$rendered_block"
725+
'Rendered block does not contain expected HTML.'
686726
);
687727
}
688728

tests/phpunit/tests/build-visual-html-tree.php

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
*/
1010
class Tests_Build_Equivalent_HTML_Semantic_Tree extends WP_UnitTestCase {
1111
public function data_build_equivalent_html_semantic_tree() {
12-
$block_markup = <<<END
13-
<!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
14-
<hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
15-
<!-- /wp:separator -->
16-
END;
12+
$block_markup = <<<'HTML'
13+
<!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
14+
<hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
15+
<!-- /wp:separator -->
16+
HTML;
1717

18-
$tree_structure = <<<END
18+
$tree_structure = <<<'TREE'
1919
BLOCK["core/separator"]
2020
{
2121
"backgroundColor": "accent-1",
@@ -29,19 +29,55 @@ public function data_build_equivalent_html_semantic_tree() {
2929
}
3030
}
3131
}
32+
"
33+
"
3234
<hr>
3335
class="has-custom-classname is-style-default wp-block-separator"
3436
style="margin-top:50px;margin-bottom:50px;"
37+
"
38+
"
3539

36-
END;
40+
TREE;
3741

38-
return array(
39-
'Block delimiter' => array( $block_markup, $tree_structure ),
40-
);
42+
yield 'Block delimiter' => array( $block_markup, $tree_structure );
43+
44+
$block_markup = <<<'HTML'
45+
<!-- wp:example/block -->
46+
One
47+
<!-- wp:example/nested-void /-->
48+
Two
49+
<!-- wp:example/nested -->
50+
Three
51+
<!-- /wp:example/nested -->
52+
Four
53+
<!-- /wp:example/block -->
54+
HTML;
55+
56+
$tree_structure = <<<'TREE'
57+
BLOCK["example/block"]
58+
"
59+
One
60+
"
61+
BLOCK["example/nested-void"]
62+
"
63+
Two
64+
"
65+
BLOCK["example/nested"]
66+
"
67+
Three
68+
"
69+
"
70+
Four
71+
"
72+
73+
TREE;
74+
75+
yield 'Text nodes in blocks' => array( $block_markup, $tree_structure );
4176
}
4277

4378
/**
4479
* @ticket 63527
80+
* @ticket 64531
4581
*
4682
* @covers ::build_visual_html_tree
4783
*
@@ -141,4 +177,41 @@ public function test_build_equivalent_html_semantic_tree_with_non_equivalent_htm
141177

142178
$this->assertNotSame( $tree_expected, $tree_actual );
143179
}
180+
181+
/**
182+
* @ticket 64531
183+
*
184+
* @covers ::build_visual_html_tree
185+
*/
186+
public function test_spacing() {
187+
$html = <<<'HTML'
188+
<p> space-surrounded&#x20;</p>
189+
<p>&nbsp;nbsp-surrounded&#xA0;</p>
190+
<p>
191+
newline-surrounded&#xA;</p>
192+
<p>&#x9;tab-surrounded </p>
193+
<p>ok</p>
194+
HTML;
195+
196+
$expected = <<<TREE
197+
<p>
198+
" space-surrounded "
199+
"\n"
200+
<p>
201+
"\u{00A0}nbsp-surrounded\u{00A0}"
202+
"\n"
203+
<p>
204+
"\nnewline-surrounded\n"
205+
"\n"
206+
<p>
207+
"\ttab-surrounded\t"
208+
"\n"
209+
<p>
210+
"ok"
211+
212+
TREE;
213+
214+
$tree_result = build_visual_html_tree( $html, '<body>' );
215+
$this->assertSame( $expected, $tree_result );
216+
}
144217
}

0 commit comments

Comments
 (0)