diff --git a/plugins/optimization-detective/class-od-html-tag-processor.php b/plugins/optimization-detective/class-od-html-tag-processor.php index 3a550b7b2b..900f0a5b1b 100644 --- a/plugins/optimization-detective/class-od-html-tag-processor.php +++ b/plugins/optimization-detective/class-od-html-tag-processor.php @@ -179,13 +179,14 @@ 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 moved. * - * @since 0.4.1 + * @since n.e.x.t * @var int * @see self::next_token() + * @see self::seek() */ - private $next_token_count = 0; + private $cursor_move_count = 0; /** * Finds the next tag. @@ -258,7 +259,7 @@ 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->cursor_move_count; if ( ! parent::next_token() ) { $this->open_stack_tags = array(); $this->open_stack_indices = array(); @@ -339,15 +340,16 @@ public function next_token(): bool { } /** - * Gets the number of times next_token() was called. + * Gets the number of times the cursor has moved. * - * @since 0.4.1 + * @since n.e.x.t * @see self::next_token() + * @see self::seek() * - * @return int Count of next_token() calls. + * @return int Count of times the cursor has moved. */ - public function get_next_token_count(): int { - return $this->next_token_count; + public function get_cursor_move_count(): int { + return $this->cursor_move_count; } /** @@ -376,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. @@ -433,18 +437,6 @@ public function seek( $bookmark_name ): bool { return $result; } - /** - * Gets the number of times seek() was called. - * - * @since 0.4.1 - * @see self::seek() - * - * @return int Count of seek() calls. - */ - public function get_seek_count(): int { - return $this->seek_count; - } - /** * Sets a bookmark in the HTML document. * @@ -567,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 { 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..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 @@ -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 {