Skip to content

Commit f6caa32

Browse files
committed
Add spawn_fragment_parser method
1 parent 292769d commit f6caa32

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,55 @@ function ( WP_HTML_Token $token ): void {
424424
};
425425
}
426426

427+
/**
428+
* Creates a fragment processor with the current node as its context element.
429+
*
430+
* @see https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm
431+
*
432+
* @param string $html Input HTML fragment to process.
433+
* @return static|null The created processor if successful, otherwise null.
434+
*/
435+
private function spawn_fragment_parser( string $html ): ?self {
436+
if ( $this->get_token_type() !== '#tag' ) {
437+
return null;
438+
}
439+
440+
/*
441+
* Prevent creating fragments at "self-contained" nodes.
442+
*
443+
* @see https://github.com/WordPress/wordpress-develop/pull/7141
444+
* @see https://github.com/WordPress/wordpress-develop/pull/7198
445+
*/
446+
if (
447+
'html' === $this->get_namespace() &&
448+
in_array( $this->get_tag(), array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true )
449+
) {
450+
return null;
451+
}
452+
453+
$fragment_processor = self::create_fragment( $html );
454+
$fragment_processor->compat_mode = $this->compat_mode;
455+
456+
// @todo The context element probably needs a namespace{
457+
$context_element = array( $this->get_tag(), array() );
458+
foreach ( $this->get_attribute_names_with_prefix( '' ) as $name => $value ) {
459+
$context_element[1][ $name ] = $value;
460+
}
461+
$fragment_processor->state->context_node = $context_element;
462+
463+
if ( 'TEMPLATE' === $context_element[0] ) {
464+
$fragment_processor->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE;
465+
}
466+
467+
$fragment_processor->reset_insertion_mode_appropriately();
468+
469+
// @todo Set the parser's form element pointer.
470+
471+
$fragment_processor->state->encoding_confidence = 'irrelevant';
472+
473+
return $fragment_processor;
474+
}
475+
427476
/**
428477
* Stops the parser and terminates its execution when encountering unsupported markup.
429478
*

0 commit comments

Comments
 (0)