Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/phpunit/includes/build-visual-html-tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static function ( $a, $b ) {
case '#cdata-section':
case '#text':
$text_content = $processor->get_modifiable_text();
if ( '' === trim( $text_content, " \f\t\r\n" ) ) {
if ( '' === $text_content ) {
break;
}
$was_text = true;
Expand Down Expand Up @@ -237,7 +237,7 @@ static function ( $a, $b ) {
++$indent_level;
}

// If they're no attributes, we're done here.
// When no attributes are present, there’s nothing left to do.
if ( empty( $block_attrs ) ) {
break;
}
Expand Down
150 changes: 95 additions & 55 deletions tests/phpunit/tests/blocks/wpBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,32 +368,39 @@ public function test_render_applies_dynamic_render_block_filter() {
* @return array
*/
public function data_provider_test_render_enqueues_scripts_and_styles(): array {
$block_markup = '
<!-- wp:static -->
<div class="static">
<!-- wp:static-child -->
<div class="static-child">First child</div>
<!-- /wp:static-child -->
<!-- wp:dynamic /-->
<!-- wp:static-child -->
<div class="static-child">Last child</div>
<!-- /wp:static-child -->
</div>
<!-- /wp:static -->
';
$block_markup = <<<'HTML'
<!-- wp:static -->
<div class="static">
<!-- wp:static-child -->
<div class="static-child">First child</div>
<!-- /wp:static-child -->
<!-- wp:dynamic /-->
<!-- wp:static-child -->
<div class="static-child">Last child</div>
<!-- /wp:static-child -->
</div>
<!-- /wp:static -->
HTML;

// TODO: Add case where a dynamic block renders other blocks?
return array(
'all_printed' => array(
'set_up' => null,
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<div class="static-child">First child</div>
<p class="dynamic">Hello World!</p>
<div class="static-child">Last child</div>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<div class="static-child">First child</div>

<p class="dynamic">Hello World!</p>

<div class="static-child">Last child</div>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
Expand All @@ -414,13 +421,20 @@ static function ( $content ) {
);
},
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<div class="static-child">First child</div>
<p class="dynamic filtered">Hello World!</p>
<div class="static-child">Last child</div>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<div class="static-child">First child</div>

<p class="dynamic filtered">Hello World!</p>

<div class="static-child">Last child</div>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'dynamic-extra', 'static-child-view-style', 'dynamic-view-style' ),
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
Expand All @@ -430,12 +444,20 @@ static function ( $content ) {
add_filter( 'render_block_core/dynamic', '__return_empty_string' );
},
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<div class="static-child">First child</div>
<div class="static-child">Last child</div>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<div class="static-child">First child</div>



<div class="static-child">Last child</div>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'static-child-view-style' ),
'expected_scripts' => array( 'static-view-script', 'static-child-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module' ),
Expand All @@ -456,12 +478,20 @@ static function ( $enqueue, $block_name ) {
);
},
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<div class="static-child">First child</div>
<div class="static-child">Last child</div>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<div class="static-child">First child</div>



<div class="static-child">Last child</div>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
Expand All @@ -488,11 +518,16 @@ static function ( $content ) {
add_filter( 'render_block_core/static-child', '__return_empty_string' );
},
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<p class="dynamic">Hello World!</p>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<p class="dynamic">Hello World!</p>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'dynamic-view-style' ),
'expected_scripts' => array( 'static-view-script', 'dynamic-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'dynamic-view-script-module' ),
Expand All @@ -512,12 +547,18 @@ static function ( $content ) {
);
},
'block_markup' => $block_markup,
'expected_rendered_block' => '
<div class="static">
<div class="static-child">First child</div>
<p class="dynamic">Hello World!</p>
</div>
',
'expected_rendered_block' => <<<'HTML'

<div class="static">

<div class="static-child">First child</div>

<p class="dynamic">Hello World!</p>

</div>

HTML
,
'expected_styles' => array( 'static-view-style', 'static-child-view-style', 'dynamic-view-style' ),
'expected_scripts' => array( 'static-view-script', 'static-child-view-script', 'dynamic-view-script' ),
'expected_script_modules' => array( 'static-view-script-module', 'static-child-view-script-module', 'dynamic-view-script-module' ),
Expand Down Expand Up @@ -562,9 +603,8 @@ static function ( $content ) {
);
},
'block_markup' => '<!-- wp:static --><div class="static"></div><!-- /wp:static -->',
'expected_rendered_block' => '
<div class="static yes-admin-bar-script-enqueued yes-admin-bar-style-enqueued"></div>
',
'expected_rendered_block' =>
'<div class="static yes-admin-bar-script-enqueued yes-admin-bar-style-enqueued"></div>',
'expected_styles' => array( 'static-view-style', 'admin-bar' ),
'expected_scripts' => array( 'static-view-script', 'admin-bar' ),
'expected_script_modules' => array( 'static-view-script-module' ),
Expand Down Expand Up @@ -668,7 +708,7 @@ public function test_render_enqueues_scripts_and_styles( ?Closure $set_up, strin
);

// TODO: Why not use do_blocks() instead?
$parsed_blocks = parse_blocks( trim( $block_markup ) );
$parsed_blocks = parse_blocks( $block_markup );
$parsed_block = $parsed_blocks[0];
$context = array();
$block = new WP_Block( $parsed_block, $context, $this->registry );
Expand All @@ -682,7 +722,7 @@ public function test_render_enqueues_scripts_and_styles( ?Closure $set_up, strin
$expected_rendered_block,
$rendered_block,
'<body>',
"Rendered block does not contain expected HTML:\n$rendered_block"
'Rendered block does not contain expected HTML.'
);
}

Expand Down
93 changes: 83 additions & 10 deletions tests/phpunit/tests/build-visual-html-tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
class Tests_Build_Equivalent_HTML_Semantic_Tree extends WP_UnitTestCase {
public function data_build_equivalent_html_semantic_tree() {
$block_markup = <<<END
<!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
<hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
<!-- /wp:separator -->
END;
$block_markup = <<<'HTML'
<!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
<hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
<!-- /wp:separator -->
HTML;

$tree_structure = <<<END
$tree_structure = <<<'TREE'
BLOCK["core/separator"]
{
"backgroundColor": "accent-1",
Expand All @@ -29,19 +29,55 @@ public function data_build_equivalent_html_semantic_tree() {
}
}
}
"
"
<hr>
class="has-custom-classname is-style-default wp-block-separator"
style="margin-top:50px;margin-bottom:50px;"
"
"

END;
TREE;

return array(
'Block delimiter' => array( $block_markup, $tree_structure ),
);
yield 'Block delimiter' => array( $block_markup, $tree_structure );

$block_markup = <<<'HTML'
<!-- wp:example/block -->
One
<!-- wp:example/nested-void /-->
Two
<!-- wp:example/nested -->
Three
<!-- /wp:example/nested -->
Four
<!-- /wp:example/block -->
HTML;

$tree_structure = <<<'TREE'
BLOCK["example/block"]
"
One
"
BLOCK["example/nested-void"]
"
Two
"
BLOCK["example/nested"]
"
Three
"
"
Four
"

TREE;

yield 'Text nodes in blocks' => array( $block_markup, $tree_structure );
}

/**
* @ticket 63527
* @ticket 64531
*
* @covers ::build_visual_html_tree
*
Expand Down Expand Up @@ -141,4 +177,41 @@ public function test_build_equivalent_html_semantic_tree_with_non_equivalent_htm

$this->assertNotSame( $tree_expected, $tree_actual );
}

/**
* @ticket 64531
*
* @covers ::build_visual_html_tree
*/
public function test_spacing() {
$html = <<<'HTML'
<p> space-surrounded&#x20;</p>
<p>&nbsp;nbsp-surrounded&#xA0;</p>
<p>
newline-surrounded&#xA;</p>
<p>&#x9;tab-surrounded </p>
<p>ok</p>
HTML;

$expected = <<<TREE
<p>
" space-surrounded "
"\n"
<p>
"\u{00A0}nbsp-surrounded\u{00A0}"
"\n"
<p>
"\nnewline-surrounded\n"
"\n"
<p>
"\ttab-surrounded\t"
"\n"
<p>
"ok"

TREE;

$tree_result = build_visual_html_tree( $html, '<body>' );
$this->assertSame( $expected, $tree_result );
}
}
Loading
Loading