Skip to content

Commit

Permalink
HTML API: Allow any fragment context.
Browse files Browse the repository at this point in the history
Previously, the fragment parser in WP_HTML_Processor has only allowed
creating a fragment with the `<body>` context. In this patch, any
context node is allowed.
  • Loading branch information
dmsnell committed Aug 2, 2024
1 parent f5ca994 commit a385685
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,27 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* @return static|null The created processor if successful, otherwise null.
*/
public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
if ( '<body>' !== $context || 'UTF-8' !== $encoding ) {
if ( 'UTF-8' !== $encoding ) {
return null;
}

$context_processor = new WP_HTML_Tag_Processor( $context );
if ( ! $context_processor->next_token() || '#tag' !== $context_processor->get_token_type() ) {
return null;
}

$context_tag = $context_processor->get_tag();
$context_attributes = array();
foreach ( $context_processor->get_attribute_names_with_prefix( '' ) as $name ) {
$context_attributes[ $name ] = $context_processor->get_attribute( $name );
}

if ( $context_processor->next_token() ) {
return null;
}

$processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE );
$processor->state->context_node = array( 'BODY', array() );
$processor->state->context_node = array( $context_tag, $context_attributes );
$processor->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
$processor->state->encoding = $encoding;
$processor->state->encoding_confidence = 'certain';
Expand Down

0 comments on commit a385685

Please sign in to comment.