From bd61b0bf236ed9c04bbbb50fd9b34d1b94970c3c Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Tue, 16 Jul 2024 16:00:38 +0000 Subject: [PATCH] HTML API: Add tests confirming comment behavior in HTML Processor. There was a bug-fix late in the 6.6 cycle in the HTML Processor which resolved an issue with the wrong name being reported when paused at processing-instruction look-alike invalid comments, but no tests were added to enforce the correct behaviors. This patch adds the missing tests. Developed in https://github.com/WordPress/wordpress-develop/pull/6765 Discussed in https://core.trac.wordpress.org/ticket/61530 Follow-up to [58304], [58558]. Props dmsnell, jonsurrell. See #61530. git-svn-id: https://develop.svn.wordpress.org/trunk@58734 602fd350-edb4-49c9-b593-d223f7449a82 --- .../html-api/wpHtmlProcessorComments.php | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/phpunit/tests/html-api/wpHtmlProcessorComments.php diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessorComments.php b/tests/phpunit/tests/html-api/wpHtmlProcessorComments.php new file mode 100644 index 0000000000000..f02631bc56222 --- /dev/null +++ b/tests/phpunit/tests/html-api/wpHtmlProcessorComments.php @@ -0,0 +1,76 @@ +next_token(); + + $this->assertSame( '#comment', $processor->get_token_name() ); + $this->assertSame( $expected_comment_type, $processor->get_comment_type() ); + $this->assertSame( $expected_modifiable_text, $processor->get_modifiable_text() ); + $this->assertSame( $expected_tag, $processor->get_tag() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_comments() { + return array( + 'Normative comment' => array( '', WP_HTML_Processor::COMMENT_AS_HTML_COMMENT, ' A comment. ' ), + 'Abruptly closed comment' => array( '', WP_HTML_Processor::COMMENT_AS_ABRUPTLY_CLOSED_COMMENT, '' ), + 'Invalid HTML comment !' => array( '', WP_HTML_Processor::COMMENT_AS_INVALID_HTML, ' Bang opener ' ), + 'Invalid HTML comment ?' => array( '', WP_HTML_Processor::COMMENT_AS_INVALID_HTML, ' Question opener ' ), + 'CDATA comment' => array( '', WP_HTML_Processor::COMMENT_AS_CDATA_LOOKALIKE, ' cdata body ' ), + 'Processing instriction comment' => array( '', WP_HTML_Processor::COMMENT_AS_PI_NODE_LOOKALIKE, ' Instruction body. ', 'pi-target' ), + 'Processing instriction php' => array( '', WP_HTML_Processor::COMMENT_AS_PI_NODE_LOOKALIKE, ' const HTML_COMMENT = true; ', 'php' ), + ); + } + + /** + * Ensures that different types of comments are processed correctly. + * + * @ticket 61530 + * + * @dataProvider data_funky_comments + */ + public function test_funky_comment( string $html, string $expected_modifiable_text ) { + $processor = WP_HTML_Processor::create_fragment( $html ); + $processor->next_token(); + + $this->assertSame( '#funky-comment', $processor->get_token_name() ); + $this->assertSame( $expected_modifiable_text, $processor->get_modifiable_text() ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_funky_comments() { + return array( + 'Funky comment # (empty)' => array( '', '#' ), + 'Funky comment #' => array( '', '# foo' ), + 'Funky comment •' => array( '', '• bar' ), + ); + } +}