Skip to content

Commit ec5776b

Browse files
committed
HTML API: Allow any fragment context.
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.
1 parent a799101 commit ec5776b

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,27 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
293293
* @return static|null The created processor if successful, otherwise null.
294294
*/
295295
public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) {
296-
if ( '<body>' !== $context || 'UTF-8' !== $encoding ) {
296+
if ( 'UTF-8' !== $encoding ) {
297+
return null;
298+
}
299+
300+
$context_processor = new WP_HTML_Tag_Processor( $context );
301+
if ( ! $context_processor->next_token() || '#tag' !== $context_processor->get_token_type() ) {
302+
return null;
303+
}
304+
305+
$context_tag = $context_processor->get_tag();
306+
$context_attributes = array();
307+
foreach ( $context_processor->get_attribute_names_with_prefix( '' ) as $name ) {
308+
$context_attributes[ $name ] = $context_processor->get_attribute( $name );
309+
}
310+
311+
if ( $context_processor->next_token() ) {
297312
return null;
298313
}
299314

300315
$processor = new static( $html, self::CONSTRUCTOR_UNLOCK_CODE );
301-
$processor->state->context_node = array( 'BODY', array() );
316+
$processor->state->context_node = array( $context_tag, $context_attributes );
302317
$processor->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;
303318
$processor->state->encoding = $encoding;
304319
$processor->state->encoding_confidence = 'certain';

0 commit comments

Comments
 (0)