Skip to content

Commit c621467

Browse files
committed
Add spawn_fragment_parser method
1 parent 199c0c6 commit c621467

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
@@ -422,6 +422,55 @@ function ( WP_HTML_Token $token ): void {
422422
};
423423
}
424424

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

0 commit comments

Comments
 (0)