From ecbd1376a6c322f20ccccc6133933d13fc3c3356 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 19 Jan 2024 19:01:29 +0000 Subject: [PATCH] HTML API: Add support for HR element. Adds support for the following HTML elements to the HTML Processor: - HR Previously, this element was not supported and the HTML Processor would bail when encountering it. Now, with this patch, it will proceed to parse an HTML document when encountering one. Developed in WordPress/wordpress-develop#5897 Props jonsurrell, dmsnell Fixes #60283 git-svn-id: https://develop.svn.wordpress.org/trunk@57314 602fd350-edb4-49c9-b593-d223f7449a82 --- .../html-api/class-wp-html-processor.php | 14 ++++++++++++-- tests/phpunit/tests/html-api/wpHtmlProcessor.php | 1 - .../html-api/wpHtmlProcessorBreadcrumbs.php | 1 - .../html-api/wpHtmlProcessorSemanticRules.php | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/html-api/class-wp-html-processor.php b/src/wp-includes/html-api/class-wp-html-processor.php index cce26a60c5350..910c1f24f1b18 100644 --- a/src/wp-includes/html-api/class-wp-html-processor.php +++ b/src/wp-includes/html-api/class-wp-html-processor.php @@ -109,7 +109,7 @@ * - Media elements: AUDIO, CANVAS, FIGCAPTION, FIGURE, IMG, MAP, PICTURE, VIDEO. * - Paragraph: P. * - Phrasing elements: ABBR, BDI, BDO, CITE, DATA, DEL, DFN, INS, MARK, OUTPUT, Q, SAMP, SUB, SUP, TIME, VAR. - * - Sectioning elements: ARTICLE, ASIDE, NAV, SECTION. + * - Sectioning elements: ARTICLE, ASIDE, HR, NAV, SECTION. * - Templating elements: SLOT. * - Text decoration: RUBY. * - Deprecated elements: ACRONYM, BLINK, CENTER, DIR, ISINDEX, MULTICOL, NEXTID, SPACER. @@ -941,6 +941,17 @@ private function step_in_body() { $this->reconstruct_active_formatting_elements(); $this->insert_html_element( $this->state->current_token ); return true; + + /* + * > A start tag whose tag name is "hr" + */ + case '+HR': + if ( $this->state->stack_of_open_elements->has_p_in_button_scope() ) { + $this->close_a_p_element(); + } + $this->insert_html_element( $this->state->current_token ); + $this->state->frameset_ok = false; + return true; } /* @@ -977,7 +988,6 @@ private function step_in_body() { case 'FRAME': case 'FRAMESET': case 'HEAD': - case 'HR': case 'HTML': case 'IFRAME': case 'INPUT': diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessor.php b/tests/phpunit/tests/html-api/wpHtmlProcessor.php index d9f1357b5c66f..d1f0767e9ce13 100644 --- a/tests/phpunit/tests/html-api/wpHtmlProcessor.php +++ b/tests/phpunit/tests/html-api/wpHtmlProcessor.php @@ -173,7 +173,6 @@ public function data_unsupported_special_in_body_tags() { 'FRAME' => array( 'FRAME' ), 'FRAMESET' => array( 'FRAMESET' ), 'HEAD' => array( 'HEAD' ), - 'HR' => array( 'HR' ), 'HTML' => array( 'HTML' ), 'IFRAME' => array( 'IFRAME' ), 'INPUT' => array( 'INPUT' ), diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php b/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php index 15d38d6f70c6c..c16d38146acb3 100644 --- a/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php +++ b/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php @@ -175,7 +175,6 @@ public function data_unsupported_elements() { 'FRAME', 'FRAMESET', 'HEAD', - 'HR', 'HTML', 'IFRAME', 'INPUT', diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php b/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php index c1adf9a71a3f8..ba9640f2efdc7 100644 --- a/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php +++ b/tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php @@ -224,6 +224,22 @@ public function test_in_body_button_with_button_in_scope_as_ancestor() { $this->assertSame( array( 'HTML', 'BODY', 'BUTTON' ), $p->get_breadcrumbs(), 'Failed to produce expected DOM nesting for third button.' ); } + /** + * Verifies that HR closes an open p tag + * + * @ticket 60283 + */ + public function test_in_body_hr_element_closes_open_p_tag() { + $processor = WP_HTML_Processor::create_fragment( '


' ); + + $processor->next_tag( 'HR' ); + $this->assertSame( + array( 'HTML', 'BODY', 'HR' ), + $processor->get_breadcrumbs(), + 'Expected HR to be a direct child of the BODY, having closed the open P element.' + ); + } + /** * Verifies that H1 through H6 elements close an open P element. *