From ed3a9249ffc967fce071a0050f0bf9a59997f344 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 16 Aug 2024 15:31:40 -0700 Subject: [PATCH 1/5] Introduce get_cursor_move_count() to use instead of get_seek_count() and get_next_token_count() --- .../class-od-html-tag-processor.php | 36 +++++++++++++- .../optimization-detective/optimization.php | 7 ++- .../test-class-od-html-tag-processor.php | 47 ++++++++++++++----- 3 files changed, 72 insertions(+), 18 deletions(-) diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index 3a550b7b2b..dcadab5f54 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -179,11 +179,24 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor { private $buffered_text_replacements = array(); /** - * Count for the number of times next_token() was called + * Count for the number of times that the cursor was successfully moved. + * + * @since n.e.x.t + * @var int + * @see self::next_token() + * @see self::seek() + */ + private $cursor_move_count = 0; + + /** + * Count for the number of times next_token() was called. + * + * The method that uses this is deprecated and it will be removed in a future release. * * @since 0.4.1 * @var int * @see self::next_token() + * @see self::get_next_token_count() */ private $next_token_count = 0; @@ -258,7 +271,8 @@ public function expects_closer( ?string $tag_name = null ): bool { */ public function next_token(): bool { $this->current_xpath = null; // Clear cache. - ++$this->next_token_count; + ++$this->next_token_count; // TODO: Remove when the deprecated get_next_token_count() method is removed. + ++$this->cursor_move_count; if ( ! parent::next_token() ) { $this->open_stack_tags = array(); $this->open_stack_indices = array(); @@ -338,15 +352,30 @@ public function next_token(): bool { return true; } + /** + * Gets the number of times the cursor has moved. + * + * @since n.e.x.t + * @see self::next_token() + * @see self::seek() + * + * @return int Count of times the cursor has moved. + */ + public function get_cursor_move_count(): int { + return $this->cursor_move_count; + } + /** * Gets the number of times next_token() was called. * * @since 0.4.1 * @see self::next_token() + * @deprecated Use {@see self::get_cursor_move_count()} instead. * * @return int Count of next_token() calls. */ public function get_next_token_count(): int { + _deprecated_function( __METHOD__, 'Optimization Detective n.e.x.t', __CLASS__ . '::get_cursor_move_count()' ); return $this->next_token_count; } @@ -429,6 +458,7 @@ public function seek( $bookmark_name ): bool { if ( $result ) { $this->open_stack_tags = $this->bookmarked_open_stacks[ $bookmark_name ]['tags']; $this->open_stack_indices = $this->bookmarked_open_stacks[ $bookmark_name ]['indices']; + ++$this->cursor_move_count; } return $result; } @@ -438,10 +468,12 @@ public function seek( $bookmark_name ): bool { * * @since 0.4.1 * @see self::seek() + * @deprecated Use {@see self::get_cursor_move_count()} instead. * * @return int Count of seek() calls. */ public function get_seek_count(): int { + _deprecated_function( __METHOD__, 'Optimization Detective n.e.x.t', __CLASS__ . '::get_cursor_move_count()' ); return $this->seek_count; } diff --git a/plugins/optimization-detective/optimization.php b/plugins/optimization-detective/optimization.php index 66a97591f2..8e4d997506 100644 --- a/plugins/optimization-detective/optimization.php +++ b/plugins/optimization-detective/optimization.php @@ -219,13 +219,12 @@ function od_optimize_template_output_buffer( string $buffer ): string { $processor->set_bookmark( $current_tag_bookmark ); // TODO: Should we break if this returns false? foreach ( $visitors as $visitor ) { - $seek_count = $processor->get_seek_count(); - $next_token_count = $processor->get_next_token_count(); - $did_visit = $visitor( $tag_visitor_context ) || $did_visit; + $cursor_move_count = $processor->get_cursor_move_count(); + $did_visit = $visitor( $tag_visitor_context ) || $did_visit; // If the visitor traversed HTML tags, we need to go back to this tag so that in the next iteration any // relevant tag visitors may apply, in addition to properly setting the data-od-xpath on this tag below. - if ( $seek_count !== $processor->get_seek_count() || $next_token_count !== $processor->get_next_token_count() ) { + if ( $cursor_move_count !== $processor->get_cursor_move_count() ) { $processor->seek( $current_tag_bookmark ); // TODO: Should this break out of the optimization loop if it returns false? } } diff --git a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php index 744f0447c4..7067dba6da 100644 --- a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php +++ b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php @@ -492,7 +492,8 @@ public function test_html_tag_processor_wrapper_methods(): void { */ public function test_bookmarking_and_seeking(): void { $processor = new OD_HTML_Tag_Processor( - ' + trim( + ' @@ -507,14 +508,19 @@ public function test_bookmarking_and_seeking(): void { - ' + ' + ) ); $actual_figure_contents = array(); - $this->assertSame( 0, $processor->get_seek_count() ); + $last_cursor_move_count = $processor->get_cursor_move_count(); + $this->assertSame( 0, $last_cursor_move_count ); $bookmarks = array(); while ( $processor->next_open_tag() ) { + $this_cursor_move_count = $processor->get_cursor_move_count(); + $this->assertGreaterThan( $last_cursor_move_count, $this_cursor_move_count ); + $last_cursor_move_count = $this_cursor_move_count; if ( 'FIGURE' === $processor->get_tag() && @@ -573,7 +579,6 @@ public function test_bookmarking_and_seeking(): void { 'depth' => $processor->get_current_depth(), ); } - $this->assertSame( count( $bookmarks ), $processor->get_seek_count() ); $this->assertSame( $expected_figure_contents, $sought_actual_contents ); @@ -597,11 +602,11 @@ public function test_bookmarking_and_seeking(): void { } /** - * Test get_seek_count. + * Test get_cursor_move_count(). * - * @covers ::get_seek_count + * @covers ::get_cursor_move_count */ - public function test_get_next_token_count(): void { + public function test_get_cursor_move_count(): void { $processor = new OD_HTML_Tag_Processor( trim( ' @@ -612,20 +617,38 @@ public function test_get_next_token_count(): void { ' ) ); - $this->assertSame( 0, $processor->get_next_token_count() ); + $this->assertSame( 0, $processor->get_cursor_move_count() ); $this->assertTrue( $processor->next_tag() ); $this->assertSame( 'HTML', $processor->get_tag() ); - $this->assertSame( 1, $processor->get_next_token_count() ); + $this->assertTrue( $processor->set_bookmark( 'document_root' ) ); + $this->assertSame( 1, $processor->get_cursor_move_count() ); $this->assertTrue( $processor->next_tag() ); $this->assertSame( 'HEAD', $processor->get_tag() ); - $this->assertSame( 3, $processor->get_next_token_count() ); // Note that next_token() call #2 was for the whitespace between and . + $this->assertSame( 3, $processor->get_cursor_move_count() ); // Note that next_token() call #2 was for the whitespace between and . $this->assertTrue( $processor->next_tag() ); $this->assertSame( 'HEAD', $processor->get_tag() ); $this->assertTrue( $processor->is_tag_closer() ); - $this->assertSame( 4, $processor->get_next_token_count() ); + $this->assertSame( 4, $processor->get_cursor_move_count() ); + $this->assertTrue( $processor->next_tag() ); + $this->assertSame( 'BODY', $processor->get_tag() ); + $this->assertSame( 6, $processor->get_cursor_move_count() ); // Note that next_token() call #5 was for the whitespace between and . $this->assertTrue( $processor->next_tag() ); $this->assertSame( 'BODY', $processor->get_tag() ); - $this->assertSame( 6, $processor->get_next_token_count() ); // Note that next_token() call #5 was for the whitespace between and . + $this->assertTrue( $processor->is_tag_closer() ); + $this->assertSame( 7, $processor->get_cursor_move_count() ); + $this->assertTrue( $processor->next_tag() ); + $this->assertSame( 'HTML', $processor->get_tag() ); + $this->assertTrue( $processor->is_tag_closer() ); + $this->assertSame( 9, $processor->get_cursor_move_count() ); // Note that next_token() call #8 was for the whitespace between and . + $this->assertFalse( $processor->next_tag() ); + $this->assertSame( 10, $processor->get_cursor_move_count() ); + $this->assertFalse( $processor->next_tag() ); + $this->assertSame( 11, $processor->get_cursor_move_count() ); + $this->assertTrue( $processor->seek( 'document_root' ) ); + $this->assertSame( 13, $processor->get_cursor_move_count() ); // Incremented by two because seek() was called in addition to next_token(). + $this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' ); + $this->assertFalse( $processor->seek( 'does_not_exist' ) ); + $this->assertSame( 13, $processor->get_cursor_move_count() ); // The bookmark does not exist so no change. } /** From 0eb7883b87848f871f461f513588d64dfd09721f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 16 Aug 2024 15:37:49 -0700 Subject: [PATCH 2/5] Eliminate unnecessary cursor_move_count increment in seek() since already incremented in next_token() --- .../optimization-detective/class-od-html-tag-processor.php | 1 - .../tests/test-class-od-html-tag-processor.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index dcadab5f54..365f0bc961 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -458,7 +458,6 @@ public function seek( $bookmark_name ): bool { if ( $result ) { $this->open_stack_tags = $this->bookmarked_open_stacks[ $bookmark_name ]['tags']; $this->open_stack_indices = $this->bookmarked_open_stacks[ $bookmark_name ]['indices']; - ++$this->cursor_move_count; } return $result; } diff --git a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php index 7067dba6da..c16402b1ee 100644 --- a/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php +++ b/plugins/optimization-detective/tests/test-class-od-html-tag-processor.php @@ -645,10 +645,10 @@ public function test_get_cursor_move_count(): void { $this->assertFalse( $processor->next_tag() ); $this->assertSame( 11, $processor->get_cursor_move_count() ); $this->assertTrue( $processor->seek( 'document_root' ) ); - $this->assertSame( 13, $processor->get_cursor_move_count() ); // Incremented by two because seek() was called in addition to next_token(). + $this->assertSame( 12, $processor->get_cursor_move_count() ); $this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' ); $this->assertFalse( $processor->seek( 'does_not_exist' ) ); - $this->assertSame( 13, $processor->get_cursor_move_count() ); // The bookmark does not exist so no change. + $this->assertSame( 12, $processor->get_cursor_move_count() ); // The bookmark does not exist so no change. } /** From f4d0f8c9889dc9a8eef25da0c6e4256a80625aa6 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 20 Aug 2024 13:47:05 -0700 Subject: [PATCH 3/5] Remove deprecated code Co-authored-by: adamsilverstein --- .../class-od-html-tag-processor.php | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index 365f0bc961..8ef56319b7 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -188,18 +188,6 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor { */ private $cursor_move_count = 0; - /** - * Count for the number of times next_token() was called. - * - * The method that uses this is deprecated and it will be removed in a future release. - * - * @since 0.4.1 - * @var int - * @see self::next_token() - * @see self::get_next_token_count() - */ - private $next_token_count = 0; - /** * Finds the next tag. * @@ -271,7 +259,6 @@ public function expects_closer( ?string $tag_name = null ): bool { */ public function next_token(): bool { $this->current_xpath = null; // Clear cache. - ++$this->next_token_count; // TODO: Remove when the deprecated get_next_token_count() method is removed. ++$this->cursor_move_count; if ( ! parent::next_token() ) { $this->open_stack_tags = array(); @@ -365,20 +352,6 @@ public function get_cursor_move_count(): int { return $this->cursor_move_count; } - /** - * Gets the number of times next_token() was called. - * - * @since 0.4.1 - * @see self::next_token() - * @deprecated Use {@see self::get_cursor_move_count()} instead. - * - * @return int Count of next_token() calls. - */ - public function get_next_token_count(): int { - _deprecated_function( __METHOD__, 'Optimization Detective n.e.x.t', __CLASS__ . '::get_cursor_move_count()' ); - return $this->next_token_count; - } - /** * Updates or creates a new attribute on the currently matched tag with the passed value. * @@ -462,20 +435,6 @@ public function seek( $bookmark_name ): bool { return $result; } - /** - * Gets the number of times seek() was called. - * - * @since 0.4.1 - * @see self::seek() - * @deprecated Use {@see self::get_cursor_move_count()} instead. - * - * @return int Count of seek() calls. - */ - public function get_seek_count(): int { - _deprecated_function( __METHOD__, 'Optimization Detective n.e.x.t', __CLASS__ . '::get_cursor_move_count()' ); - return $this->seek_count; - } - /** * Sets a bookmark in the HTML document. * From 48b06cca34a4e0214f1bf4e117e2a31663af5e63 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 20 Aug 2024 13:54:44 -0700 Subject: [PATCH 4/5] Add missing since tags --- .../optimization-detective/class-od-html-tag-processor.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index 8ef56319b7..952d58cd7c 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -378,7 +378,9 @@ public function set_attribute( $name, $value ): bool { // phpcs:ignore SlevomatC /** * Sets a meta attribute. * - * All meta attributes are prefixed with 'data-od-'. + * All meta attributes are prefixed with data-od-. + * + * @since 0.4.0 * * @param string $name Meta attribute name. * @param string|true $value Value. @@ -557,6 +559,8 @@ public function append_body_html( string $html ): void { /** * Returns the string representation of the HTML Tag Processor. * + * @since 0.4.0 + * * @return string The processed HTML. */ public function get_updated_html(): string { From 5b683b591814f3b95d06e2bb5297382989383731 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 21 Aug 2024 13:21:52 -0700 Subject: [PATCH 5/5] Clarify comment for $cursor_move_count --- plugins/optimization-detective/class-od-html-tag-processor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index 952d58cd7c..900f0a5b1b 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -179,7 +179,7 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor { private $buffered_text_replacements = array(); /** - * Count for the number of times that the cursor was successfully moved. + * Count for the number of times that the cursor was moved. * * @since n.e.x.t * @var int