Skip to content

Commit 7a7411a

Browse files
committed
Soft-deprecate next_open_tag()
1 parent 2c6271b commit 7a7411a

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

plugins/optimization-detective/class-od-html-tag-processor.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,13 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor {
246246
/**
247247
* Finds the next tag.
248248
*
249-
* Unlike the base class, this subclass visits tag closers by default.
249+
* Unlike the base class, this subclass currently visits tag closers by default.
250+
* However, for the 1.0.0 release this method will behave the same as the method in
251+
* the base class, where it skips tag closers by default.
250252
*
251253
* @inheritDoc
252254
* @since 0.4.0
253-
* @since n.e.x.t Passing a $query is now allowed. In a future release, this will default to skipping tag closers.
255+
* @since n.e.x.t Passing a $query is now allowed. In the 1.0.0 release, this will default to skipping tag closers.
254256
*
255257
* @param array{tag_name?: string|null, match_offset?: int|null, class_name?: string|null, tag_closers?: string|null}|null $query Query.
256258
* @return bool Whether a tag was matched.
@@ -269,7 +271,10 @@ public function next_tag( $query = null ): bool {
269271
/**
270272
* Finds the next open tag.
271273
*
274+
* This method will soon be equivalent to calling {@see self::next_tag()} without passing any `$query`.
275+
*
272276
* @since 0.4.0
277+
* @deprecated n.e.x.t Use {@see self::next_tag()} instead.
273278
*
274279
* @return bool Whether a tag was matched.
275280
*/

plugins/optimization-detective/tests/test-class-od-html-tag-processor.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,42 @@ public function test_next_tag_without_query(): void {
577577
$this->assertFalse( $p->next_tag() );
578578
}
579579

580+
/**
581+
* Test next_open_tag().
582+
*
583+
* @covers ::next_open_tag
584+
*/
585+
public function test_next_open_tag(): void {
586+
$html = '
587+
<!DOCTYPE html>
588+
<html>
589+
<head>
590+
<title></title>
591+
</head>
592+
<body></body>
593+
</html>
594+
';
595+
596+
$p = new OD_HTML_Tag_Processor( $html );
597+
$this->assertTrue( $p->next_open_tag() ); // @phpstan-ignore method.deprecated
598+
$this->assertSame( 'HTML', $p->get_tag() );
599+
$this->assertFalse( $p->is_tag_closer() );
600+
601+
$this->assertTrue( $p->next_open_tag() ); // @phpstan-ignore method.deprecated
602+
$this->assertSame( 'HEAD', $p->get_tag() );
603+
$this->assertFalse( $p->is_tag_closer() );
604+
605+
$this->assertTrue( $p->next_open_tag() ); // @phpstan-ignore method.deprecated
606+
$this->assertSame( 'TITLE', $p->get_tag() );
607+
$this->assertFalse( $p->is_tag_closer() );
608+
609+
$this->assertTrue( $p->next_open_tag() ); // @phpstan-ignore method.deprecated
610+
$this->assertSame( 'BODY', $p->get_tag() );
611+
$this->assertFalse( $p->is_tag_closer() );
612+
613+
$this->assertFalse( $p->next_open_tag() ); // @phpstan-ignore method.deprecated
614+
}
615+
580616
/**
581617
* Test expects_closer().
582618
*
@@ -632,7 +668,7 @@ public function test_append_head_and_body_html(): void {
632668
$saw_head = false;
633669
$saw_body = false;
634670
$did_seek = false;
635-
while ( $processor->next_open_tag() ) {
671+
while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) ) {
636672
$this->assertStringNotContainsString( $head_injected, $processor->get_updated_html(), 'Only expecting end-of-head injection once document was finalized.' );
637673
$this->assertStringNotContainsString( $body_injected, $processor->get_updated_html(), 'Only expecting end-of-body injection once document was finalized.' );
638674
$tag = $processor->get_tag();
@@ -708,7 +744,7 @@ public function test_get_updated_html_when_out_of_bookmarks(): void {
708744

709745
$saw_head = false;
710746
$saw_body = false;
711-
while ( $processor->next_open_tag() ) {
747+
while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) ) {
712748
$tag = $processor->get_tag();
713749
if ( 'HEAD' === $tag ) {
714750
$saw_head = true;
@@ -732,7 +768,7 @@ public function test_get_updated_html_when_out_of_bookmarks(): void {
732768
*/
733769
public function test_html_tag_processor_wrapper_methods(): void {
734770
$processor = new OD_HTML_Tag_Processor( '<html lang="en" class="foo" dir="ltr" data-novalue></html>' );
735-
while ( $processor->next_open_tag() ) {
771+
while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) ) {
736772
$open_tag = $processor->get_tag();
737773
if ( 'HTML' === $open_tag ) {
738774
$processor->set_attribute( 'lang', 'es' );
@@ -787,7 +823,7 @@ public function test_bookmarking_and_seeking(): void {
787823
$this->assertSame( 0, $last_cursor_move_count );
788824

789825
$bookmarks = array();
790-
while ( $processor->next_open_tag() ) {
826+
while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) ) {
791827
$this_cursor_move_count = $processor->get_cursor_move_count();
792828
$this->assertGreaterThan( $last_cursor_move_count, $this_cursor_move_count );
793829
$last_cursor_move_count = $this_cursor_move_count;

0 commit comments

Comments
 (0)