Skip to content

Commit b97aa9b

Browse files
authored
Silence notices when searching for context nodes (#6)
1 parent d2af8ca commit b97aa9b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

html-api-debugger/html-api-integration.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ function get_normalized_html( string $html, array $options ): ?string {
3737
) {
3838
$context_processor = WP_HTML_Processor::create_full_parser( $options['context_html'] );
3939

40+
add_filter( 'doing_it_wrong_trigger_error', __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error', 10, 2 );
4041
while ( $context_processor->next_tag() ) {
4142
$context_processor->set_bookmark( 'final_node' );
4243
}
44+
remove_filter( 'doing_it_wrong_trigger_error', __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error', 10 );
4345
if ( $context_processor->has_bookmark( 'final_node' ) ) {
4446
$context_processor->seek( 'final_node' );
4547
/**
@@ -119,6 +121,7 @@ function get_tree( string $html, array $options ): array {
119121
) {
120122
$context_processor = WP_HTML_Processor::create_full_parser( $options['context_html'] );
121123

124+
add_filter( 'doing_it_wrong_trigger_error', __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error', 10, 2 );
122125
while ( $context_processor->next_token() ) {
123126
switch ( $context_processor->get_token_type() ) {
124127
case '#doctype':
@@ -140,6 +143,7 @@ function get_tree( string $html, array $options ): array {
140143
break;
141144
}
142145
}
146+
remove_filter( 'doing_it_wrong_trigger_error', __NAMESPACE__ . '\filter_doing_it_wrong_trigger_error', 10 );
143147

144148
if ( $document_title === null ) {
145149
$document_title = '';
@@ -473,6 +477,22 @@ function get_tree( string $html, array $options ): array {
473477
);
474478
}
475479

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+
476496
const NODE_TYPE_ELEMENT = 1;
477497
const NODE_TYPE_ATTRIBUTE = 2;
478498
const NODE_TYPE_TEXT = 3;

html-api-debugger/readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Please file issues and pull requests on the [GitHub repository](https://github.c
1515

1616
== Changelog ==
1717

18+
= 2.8 =
19+
* Silence _doing_it_wrong bookmark notices when providing from HTML context.
20+
1821
= 2.7 =
1922
* Preserve attribute value line breaks in tree views.
2023
* Prevent plugin module from being enqueued on all pages.

0 commit comments

Comments
 (0)