@@ -37,9 +37,11 @@ function get_normalized_html( string $html, array $options ): ?string {
37
37
) {
38
38
$ context_processor = WP_HTML_Processor::create_full_parser ( $ options ['context_html ' ] );
39
39
40
+ add_filter ( 'doing_it_wrong_trigger_error ' , __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error ' , 10 , 2 );
40
41
while ( $ context_processor ->next_tag () ) {
41
42
$ context_processor ->set_bookmark ( 'final_node ' );
42
43
}
44
+ remove_filter ( 'doing_it_wrong_trigger_error ' , __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error ' , 10 );
43
45
if ( $ context_processor ->has_bookmark ( 'final_node ' ) ) {
44
46
$ context_processor ->seek ( 'final_node ' );
45
47
/**
@@ -119,6 +121,7 @@ function get_tree( string $html, array $options ): array {
119
121
) {
120
122
$ context_processor = WP_HTML_Processor::create_full_parser ( $ options ['context_html ' ] );
121
123
124
+ add_filter ( 'doing_it_wrong_trigger_error ' , __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error ' , 10 , 2 );
122
125
while ( $ context_processor ->next_token () ) {
123
126
switch ( $ context_processor ->get_token_type () ) {
124
127
case '#doctype ' :
@@ -140,6 +143,7 @@ function get_tree( string $html, array $options ): array {
140
143
break ;
141
144
}
142
145
}
146
+ remove_filter ( 'doing_it_wrong_trigger_error ' , __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error ' , 10 );
143
147
144
148
if ( $ document_title === null ) {
145
149
$ document_title = '' ;
@@ -473,6 +477,22 @@ function get_tree( string $html, array $options ): array {
473
477
);
474
478
}
475
479
480
+ /**
481
+ * Prevent set_bookmark on virtual context nodes from triggering a doing_it_wrong error.
482
+ * A context like `<body>` would trigger due to virtual nodes like `<head>` being created
483
+ * and bookmarked.
484
+ * Remove the noise when searching for context nodes.
485
+ *
486
+ * @param bool $trigger_error Whether to trigger a doing_it_wrong error.
487
+ * @param string $function_name The name of the function being called.
488
+ */
489
+ function filter_doing_it_wrong_trigger_error ( $ trigger_error , $ function_name ) {
490
+ if ( $ function_name === 'WP_HTML_Processor::set_bookmark ' ) {
491
+ return false ;
492
+ }
493
+ return $ trigger_error ;
494
+ }
495
+
476
496
const NODE_TYPE_ELEMENT = 1 ;
477
497
const NODE_TYPE_ATTRIBUTE = 2 ;
478
498
const NODE_TYPE_TEXT = 3 ;
0 commit comments