Skip to content

Commit 069dc53

Browse files
committed
Try: Allow queries to be passed to next_tag()
1 parent 42bd7a8 commit 069dc53

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,38 +246,35 @@ 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 disallows querying. This is to ensure the breadcrumbs can be tracked.
250-
* It will _always_ visit tag closers.
249+
* Unlike the base class, this subclass visits tag closers by default.
251250
*
252251
* @inheritDoc
253252
* @since 0.4.0
253+
* @since n.e.x.t Passing a $query is now allowed.
254254
*
255-
* @param array{tag_name?: string|null, match_offset?: int|null, class_name?: string|null, tag_closers?: string|null}|null $query Query, but only null is accepted for this subclass.
255+
* @param array{tag_name?: string|null, match_offset?: int|null, class_name?: string|null, tag_closers?: string|null}|null $query Query.
256256
* @return bool Whether a tag was matched.
257257
*
258258
* @throws InvalidArgumentException If attempting to pass a query.
259259
*/
260260
public function next_tag( $query = null ): bool {
261-
if ( null !== $query ) {
262-
throw new InvalidArgumentException( esc_html__( 'Processor subclass does not support queries.', 'optimization-detective' ) );
261+
if ( null === $query ) {
262+
// For back-compat with tag visitor which previously relied next_tag() always visiting tag closers.
263+
$query = array( array( 'tag_closers' => 'visit' ) );
263264
}
264-
265-
// Elements in the Admin Bar are not relevant for optimization, so this loop ensures that no tags in the Admin Bar are visited.
266-
do {
267-
$matched = parent::next_tag( array( 'tag_closers' => 'visit' ) );
268-
} while ( $matched && $this->is_admin_bar() );
269-
return $matched;
265+
return parent::next_tag( $query );
270266
}
271267

272268
/**
273269
* Finds the next open tag.
274270
*
275271
* @since 0.4.0
272+
* @deprecated
276273
*
277274
* @return bool Whether a tag was matched.
278275
*/
279276
public function next_open_tag(): bool {
280-
while ( $this->next_tag() ) {
277+
while ( $this->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
281278
if ( ! $this->is_tag_closer() ) {
282279
return true;
283280
}

plugins/optimization-detective/optimization.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ function od_optimize_template_output_buffer( string $buffer ): string {
355355

356356
do {
357357
// Never process anything inside NOSCRIPT since it will never show up in the DOM when scripting is enabled, and thus it can never be detected nor measured.
358-
if ( in_array( 'NOSCRIPT', $processor->get_breadcrumbs(), true ) ) {
358+
// Similarly, elements in the Admin Bar are not relevant for optimization, so this loop ensures that no tags in the Admin Bar are visited.
359+
if (
360+
in_array( 'NOSCRIPT', $processor->get_breadcrumbs(), true )
361+
||
362+
str_starts_with( $processor->get_xpath(), "/HTML/BODY/DIV[@id='wpadminbar']" )
363+
) {
359364
continue;
360365
}
361366

@@ -388,7 +393,7 @@ function od_optimize_template_output_buffer( string $buffer ): string {
388393
}
389394

390395
$visited_tag_state->reset();
391-
} while ( $processor->next_open_tag() );
396+
} while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) );
392397

393398
// Send any preload links in a Link response header and in a LINK tag injected at the end of the HEAD.
394399
if ( count( $link_collection ) > 0 ) {

plugins/optimization-detective/tests/test-cases/admin-bar/set-up.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ static function ( OD_Tag_Visitor_Registry $registry ) use ( $test_case ): void {
66
$registry->register(
77
'everything',
88
static function ( OD_Tag_Visitor_Context $context ) use ( $test_case ): void {
9-
$test_case->assertNotEquals( 'wpadminbar', $context->processor->get_attribute( 'id' ) );
9+
$test_case->assertStringNotContainsString( 'wpadminbar', $context->processor->get_xpath() );
10+
$test_case->assertNotEquals( 'wpadminbar', $context->processor->get_attribute( 'id' ), $context->processor->get_xpath() );
1011
$test_case->assertNull( $context->url_metrics_id, 'Expected url_metrics_id to be null since no od_url_metrics_post exists for this URL.' );
1112
}
1213
);

0 commit comments

Comments
 (0)