Skip to content

Commit

Permalink
Safer create fragment at current node
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 29, 2024
1 parent 19d9599 commit 2769d75
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions html-api-debugger/html-api-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function get_supports(): array {
* @return string|null The normalized HTML or null if not supported.
*/
function get_normalized_html( string $html, array $options ): ?string {
$cfacn = new ReflectionMethod( WP_HTML_Processor::class, 'create_fragment_at_current_node' );
$cfacn->setAccessible( true );

if (
method_exists( WP_HTML_Processor::class, 'create_fragment_at_current_node' ) &&
$options['context_html']
Expand All @@ -40,7 +43,7 @@ function get_normalized_html( string $html, array $options ): ?string {
* @var WP_HTML_Processor|null $processor
* @disregard P1013
*/
$processor = $context_processor->create_fragment_at_current_node( $html );
$processor = $cfacn->invoke( $context_processor, $html );
}
} else {
$processor = WP_HTML_Processor::create_full_parser( $html );
Expand Down Expand Up @@ -73,6 +76,9 @@ function get_tree( string $html, array $options ): array {
$processor_bookmarks = new ReflectionProperty( WP_HTML_Processor::class, 'bookmarks' );
$processor_bookmarks->setAccessible( true );

$cfacn = new ReflectionMethod( WP_HTML_Processor::class, 'create_fragment_at_current_node' );
$cfacn->setAccessible( true );

$is_fragment_processor = false;

$compat_mode = 'BackCompat';
Expand Down Expand Up @@ -121,7 +127,7 @@ function get_tree( string $html, array $options ): array {
* @var WP_HTML_Processor|null $processor
* @disregard P1013
*/
$processor = $context_processor->create_fragment_at_current_node( $html );
$processor = $cfacn->invoke( $context_processor, $html );
}

if ( ! isset( $processor ) ) {
Expand Down

0 comments on commit 2769d75

Please sign in to comment.