Skip to content

Commit

Permalink
HTML API: Improve private method name used by `WP_HTML_Processor::nex…
Browse files Browse the repository at this point in the history
…t_token()`.

This renames the private `_next_token` method to `next_visitable_token`. It also removes irrelevant assertions from the unit test.

Follow-up to [59285].

Props dmsnell, jonsurrell, westonruter.
See #62269.


git-svn-id: https://develop.svn.wordpress.org/trunk@59364 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
westonruter committed Nov 6, 2024
1 parent 7b94f09 commit 8ad5281
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 141 deletions.
19 changes: 12 additions & 7 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function next_tag( $query = null ): bool {
* @return bool Whether a token was parsed.
*/
public function next_token(): bool {
return $this->_next_token();
return $this->next_visitable_token();
}

/**
Expand All @@ -627,13 +627,18 @@ public function next_token(): bool {
* semantic rules for text nodes. For access to the raw tokens consider using
* WP_HTML_Tag_Processor instead.
*
* @since 6.7.1 Added for internal support; do not use.
* Note that this method may call itself recursively. This is why it is not
* implemented as {@see WP_HTML_Processor::next_token()}, which instead calls
* this method similarly to how {@see WP_HTML_Tag_Processor::next_token()}
* calls the {@see WP_HTML_Tag_Processor::base_class_next_token()} method.
*
* @since 6.7.1 Added for internal support.
*
* @access private
*
* @return bool
*/
private function _next_token(): bool {
private function next_visitable_token(): bool {
$this->current_element = null;

if ( isset( $this->last_error ) ) {
Expand All @@ -651,7 +656,7 @@ private function _next_token(): bool {
* tokens works in the meantime and isn't obviously wrong.
*/
if ( empty( $this->element_queue ) && $this->step() ) {
return $this->_next_token();
return $this->next_visitable_token();
}

// Process the next event on the queue.
Expand All @@ -662,7 +667,7 @@ private function _next_token(): bool {
continue;
}

return empty( $this->element_queue ) ? false : $this->_next_token();
return empty( $this->element_queue ) ? false : $this->next_visitable_token();
}

$is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation;
Expand All @@ -673,7 +678,7 @@ private function _next_token(): bool {
* the breadcrumbs.
*/
if ( 'root-node' === $this->current_element->token->bookmark_name ) {
return $this->_next_token();
return $this->next_visitable_token();
}

// Adjust the breadcrumbs for this event.
Expand All @@ -685,7 +690,7 @@ private function _next_token(): bool {

// Avoid sending close events for elements which don't expect a closing.
if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) {
return $this->_next_token();
return $this->next_visitable_token();
}

return true;
Expand Down
88 changes: 0 additions & 88 deletions tests/phpunit/data/html-api/html-xpath-generating-processor.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/phpunit/data/html-api/token-counting-html-processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

class Token_Counting_HTML_Processor extends WP_HTML_Processor {

/**
* List of tokens that have already been seen.
*
* @var array<string, int>
*/
public $token_seen_count = array();

/**
* Gets next token.
*
* @return bool Whether next token was matched.
*/
public function next_token(): bool {
$result = parent::next_token();

if ( $this->get_token_type() === '#tag' ) {
$token_name = ( $this->is_tag_closer() ? '-' : '+' ) . $this->get_tag();
} else {
$token_name = $this->get_token_name();
}

if ( ! isset( $this->token_seen_count[ $token_name ] ) ) {
$this->token_seen_count[ $token_name ] = 1;
} else {
++$this->token_seen_count[ $token_name ];
}

return $result;
}

}
51 changes: 5 additions & 46 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,17 +925,6 @@ public function data_html_processor_with_extended_next_token() {
'-HTML' => 1,
'' => 1,
),
'expected_xpaths' => array(
0 => '/*[1][self::HTML]',
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
2 => '/*[1][self::HTML]/*[1][self::HEAD]/*[1][self::META]',
3 => '/*[1][self::HTML]/*[1][self::HEAD]/*[2][self::TITLE]',
4 => '/*[1][self::HTML]/*[2][self::BODY]',
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::H1]',
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::P]',
8 => '/*[1][self::HTML]/*[2][self::BODY]/*[4][self::FOOTER]',
),
),

'multiple_tag_instances' => array(
Expand Down Expand Up @@ -972,19 +961,6 @@ public function data_html_processor_with_extended_next_token() {
'-HTML' => 1,
'' => 1,
),
'expected_xpaths' => array(
0 => '/*[1][self::HTML]',
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
2 => '/*[1][self::HTML]/*[2][self::BODY]',
3 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::H1]',
4 => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::P]',
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::P]',
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[4][self::P]',
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]',
8 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[1][self::LI]',
9 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[2][self::LI]',
10 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[3][self::LI]',
),
),

'extreme_nested_formatting' => array(
Expand Down Expand Up @@ -1021,41 +997,24 @@ public function data_html_processor_with_extended_next_token() {
'-HTML' => 1,
'' => 1,
),
'expected_xpaths' => array(
0 => '/*[1][self::HTML]',
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
2 => '/*[1][self::HTML]/*[2][self::BODY]',
3 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]',
4 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]',
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]',
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]',
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]/*[1][self::I]',
8 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]/*[1][self::I]/*[1][self::B]',
9 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]/*[1][self::I]/*[1][self::B]/*[1][self::U]',
),
),
);
}

/**
* Ensures that subclasses to WP_HTML_Processor can do bookkeeping by extending the next_token() method.
*
* @ticket ?
* @ticket 62269
* @dataProvider data_html_processor_with_extended_next_token
*/
public function test_ensure_next_token_method_extensibility( $html, $expected_token_counts, $expected_xpaths ) {
require_once DIR_TESTDATA . '/html-api/html-xpath-generating-processor.php';
public function test_ensure_next_token_method_extensibility( $html, $expected_token_counts ) {
require_once DIR_TESTDATA . '/html-api/token-counting-html-processor.php';

$processor = HTML_XPath_Generating_Processor::create_full_parser( $html );
$actual_xpaths = array();
$processor = Token_Counting_HTML_Processor::create_full_parser( $html );
while ( $processor->next_tag() ) {
if ( ! $processor->is_tag_closer() ) {
$processor->set_attribute( 'xpath', $processor->get_xpath() );
$actual_xpaths[] = $processor->get_xpath();
}
continue;
}

$this->assertEquals( $expected_token_counts, $processor->token_seen_count, 'Snapshot: ' . var_export( $processor->token_seen_count, true ) );
$this->assertEquals( $expected_xpaths, $actual_xpaths, 'Snapshot: ' . var_export( $actual_xpaths, true ) );
}
}

0 comments on commit 8ad5281

Please sign in to comment.