Skip to content

Commit

Permalink
test: minor adjustments and encoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rleeson committed Dec 20, 2024
1 parent 2b55b2a commit f857bd5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
18 changes: 10 additions & 8 deletions wp/headless-wp/includes/classes/Integrations/Gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ protected function bypass_block_attributes( string $block_name, WP_Block $block_
*/
public function process_block_with_dom_document_api( $html, $block_name, $block_attrs_serialized, $block, $block_instance ) {
try {
libxml_use_internal_errors( true );

return $this->bypass_block_attributes( $block_name, $block_instance )
? $this->process_dom_document_bypassed_block( $html )
: $this->process_dom_document_block( $html, $block_name, $block_attrs_serialized, $block, $block_instance );
Expand Down Expand Up @@ -125,7 +123,7 @@ public function process_dom_document_block(
array $block,
WP_Block $block_instance
): string {
$document = $this->read_decoded_dom_document( $html );
$document = $this->read_converted_dom_document( $html );

// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$root_node = $document->documentElement;
Expand Down Expand Up @@ -161,13 +159,13 @@ public function process_dom_document_block(
* @return string
*/
public function process_dom_document_bypassed_block( string $html ): string {
$document = $this->read_decoded_dom_document( "<body>{$html}</body>" );
$document = $this->read_converted_dom_document( "<body>{$html}</body>" );
$body = $document->getElementsByTagName( 'body' )->item( 0 );
$node_html = [];

// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach ( $body->childNodes as $child ) {
$block = new DOMDocument();
$block = new DOMDocument( '1.0', 'UTF-8' );
$block->appendChild( $block->importNode( $child, true ) );

$child_html = $block->saveHTML();
Expand All @@ -190,9 +188,13 @@ public function process_dom_document_bypassed_block( string $html ): string {
*
* @return DOMDocument
*/
protected function read_decoded_dom_document( string $html ) {
$document = new DomDocument( '1.0', 'UTF-8' );
$document->loadHTML( htmlspecialchars_decode( htmlentities( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED );
protected function read_converted_dom_document( string $html ) {
$converted_html = htmlspecialchars_decode( htmlentities( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) );
$document = new DomDocument( '1.0', 'UTF-8' );

libxml_use_internal_errors( true );
$document->loadHTML( $converted_html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED );
libxml_clear_errors();

// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( null === $document->documentElement ) {
Expand Down
34 changes: 34 additions & 0 deletions wp/headless-wp/tests/php/tests/TestGutenbergIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ public function test_render_classic_block() {
);
}

/**
* Test to ensure the parser handles both HTML and Multi-byte encodings properly
*
* @return void
*/
public function test_handle_multi_byte_html_encoding() {
[ 'html' => $html, 'parsed_block' => $block, 'instance' => $instance ] =
$this->core_render_block_from_markup(
<<<MARKUP
<!-- wp:paragraph -->
<p>The temperature is 23°C ☀️ (sun emoji) and © (copyright symbol). HTML entity for Degrees: &#176;.</p>
<!-- /wp:paragraph -->
MARKUP
);
$dom_expected = <<<RESULT
<p data-wp-block='{"dropCap":false}' data-wp-block-name="core/paragraph">The temperature is 23&deg;C &#9728;&#65039; (sun emoji) and &copy; (copyright symbol). HTML entity for Degrees: &deg;.</p>
RESULT;
$html_tag_api_expected = <<<RESULT
<p data-wp-block="{&quot;dropCap&quot;:false}" data-wp-block-name="core/paragraph">The temperature is 23&deg;C &#9728;&#65039; (sun emoji) and &copy; (copyright symbol). HTML entity for Degrees: &deg;.</p>
RESULT;

$dom_output = $this->parser->render_block( $html, $block, $instance );

$this->assertSame( trim( $dom_expected ), trim( $dom_output ), 'Gutenberg | DOM Document | Test HTML Encoding' );

add_filter( 'tenup_headless_wp_render_block_use_tag_processor', '__return_true' );

$html_api_output = $this->parser->render_block( $html, $block, $instance );

remove_filter( 'tenup_headless_wp_render_block_use_tag_processor', '__return_true' );

$this->assertSame( trim( $html_tag_api_expected ), trim( $html_api_output ), 'Gutenberg | HTML Tag API | Test HTML Encoding' );
}

/**
* Tests rendering classic block with the HTML tag api
*
Expand Down

0 comments on commit f857bd5

Please sign in to comment.