Skip to content

Commit 8ad5281

Browse files
committed
HTML API: Improve private method name used by WP_HTML_Processor::next_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
1 parent 7b94f09 commit 8ad5281

File tree

4 files changed

+52
-141
lines changed

4 files changed

+52
-141
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ public function next_tag( $query = null ): bool {
616616
* @return bool Whether a token was parsed.
617617
*/
618618
public function next_token(): bool {
619-
return $this->_next_token();
619+
return $this->next_visitable_token();
620620
}
621621

622622
/**
@@ -627,13 +627,18 @@ public function next_token(): bool {
627627
* semantic rules for text nodes. For access to the raw tokens consider using
628628
* WP_HTML_Tag_Processor instead.
629629
*
630-
* @since 6.7.1 Added for internal support; do not use.
630+
* Note that this method may call itself recursively. This is why it is not
631+
* implemented as {@see WP_HTML_Processor::next_token()}, which instead calls
632+
* this method similarly to how {@see WP_HTML_Tag_Processor::next_token()}
633+
* calls the {@see WP_HTML_Tag_Processor::base_class_next_token()} method.
634+
*
635+
* @since 6.7.1 Added for internal support.
631636
*
632637
* @access private
633638
*
634639
* @return bool
635640
*/
636-
private function _next_token(): bool {
641+
private function next_visitable_token(): bool {
637642
$this->current_element = null;
638643

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

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

665-
return empty( $this->element_queue ) ? false : $this->_next_token();
670+
return empty( $this->element_queue ) ? false : $this->next_visitable_token();
666671
}
667672

668673
$is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation;
@@ -673,7 +678,7 @@ private function _next_token(): bool {
673678
* the breadcrumbs.
674679
*/
675680
if ( 'root-node' === $this->current_element->token->bookmark_name ) {
676-
return $this->_next_token();
681+
return $this->next_visitable_token();
677682
}
678683

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

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

691696
return true;

tests/phpunit/data/html-api/html-xpath-generating-processor.php

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
class Token_Counting_HTML_Processor extends WP_HTML_Processor {
4+
5+
/**
6+
* List of tokens that have already been seen.
7+
*
8+
* @var array<string, int>
9+
*/
10+
public $token_seen_count = array();
11+
12+
/**
13+
* Gets next token.
14+
*
15+
* @return bool Whether next token was matched.
16+
*/
17+
public function next_token(): bool {
18+
$result = parent::next_token();
19+
20+
if ( $this->get_token_type() === '#tag' ) {
21+
$token_name = ( $this->is_tag_closer() ? '-' : '+' ) . $this->get_tag();
22+
} else {
23+
$token_name = $this->get_token_name();
24+
}
25+
26+
if ( ! isset( $this->token_seen_count[ $token_name ] ) ) {
27+
$this->token_seen_count[ $token_name ] = 1;
28+
} else {
29+
++$this->token_seen_count[ $token_name ];
30+
}
31+
32+
return $result;
33+
}
34+
35+
}

tests/phpunit/tests/html-api/wpHtmlProcessor.php

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -925,17 +925,6 @@ public function data_html_processor_with_extended_next_token() {
925925
'-HTML' => 1,
926926
'' => 1,
927927
),
928-
'expected_xpaths' => array(
929-
0 => '/*[1][self::HTML]',
930-
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
931-
2 => '/*[1][self::HTML]/*[1][self::HEAD]/*[1][self::META]',
932-
3 => '/*[1][self::HTML]/*[1][self::HEAD]/*[2][self::TITLE]',
933-
4 => '/*[1][self::HTML]/*[2][self::BODY]',
934-
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::H1]',
935-
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::IMG]',
936-
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::P]',
937-
8 => '/*[1][self::HTML]/*[2][self::BODY]/*[4][self::FOOTER]',
938-
),
939928
),
940929

941930
'multiple_tag_instances' => array(
@@ -972,19 +961,6 @@ public function data_html_processor_with_extended_next_token() {
972961
'-HTML' => 1,
973962
'' => 1,
974963
),
975-
'expected_xpaths' => array(
976-
0 => '/*[1][self::HTML]',
977-
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
978-
2 => '/*[1][self::HTML]/*[2][self::BODY]',
979-
3 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::H1]',
980-
4 => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::P]',
981-
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[3][self::P]',
982-
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[4][self::P]',
983-
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]',
984-
8 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[1][self::LI]',
985-
9 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[2][self::LI]',
986-
10 => '/*[1][self::HTML]/*[2][self::BODY]/*[5][self::UL]/*[3][self::LI]',
987-
),
988964
),
989965

990966
'extreme_nested_formatting' => array(
@@ -1021,41 +997,24 @@ public function data_html_processor_with_extended_next_token() {
1021997
'-HTML' => 1,
1022998
'' => 1,
1023999
),
1024-
'expected_xpaths' => array(
1025-
0 => '/*[1][self::HTML]',
1026-
1 => '/*[1][self::HTML]/*[1][self::HEAD]',
1027-
2 => '/*[1][self::HTML]/*[2][self::BODY]',
1028-
3 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]',
1029-
4 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]',
1030-
5 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]',
1031-
6 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]',
1032-
7 => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::P]/*[1][self::STRONG]/*[1][self::EM]/*[1][self::STRIKE]/*[1][self::I]',
1033-
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]',
1034-
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]',
1035-
),
10361000
),
10371001
);
10381002
}
10391003

10401004
/**
10411005
* Ensures that subclasses to WP_HTML_Processor can do bookkeeping by extending the next_token() method.
10421006
*
1043-
* @ticket ?
1007+
* @ticket 62269
10441008
* @dataProvider data_html_processor_with_extended_next_token
10451009
*/
1046-
public function test_ensure_next_token_method_extensibility( $html, $expected_token_counts, $expected_xpaths ) {
1047-
require_once DIR_TESTDATA . '/html-api/html-xpath-generating-processor.php';
1010+
public function test_ensure_next_token_method_extensibility( $html, $expected_token_counts ) {
1011+
require_once DIR_TESTDATA . '/html-api/token-counting-html-processor.php';
10481012

1049-
$processor = HTML_XPath_Generating_Processor::create_full_parser( $html );
1050-
$actual_xpaths = array();
1013+
$processor = Token_Counting_HTML_Processor::create_full_parser( $html );
10511014
while ( $processor->next_tag() ) {
1052-
if ( ! $processor->is_tag_closer() ) {
1053-
$processor->set_attribute( 'xpath', $processor->get_xpath() );
1054-
$actual_xpaths[] = $processor->get_xpath();
1055-
}
1015+
continue;
10561016
}
10571017

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

0 commit comments

Comments
 (0)