Skip to content

Commit

Permalink
Add spawn_fragment_parser method
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Sep 12, 2024
1 parent 199c0c6 commit c621467
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,55 @@ function ( WP_HTML_Token $token ): void {
};
}

/**
* Creates a fragment processor with the current node as its context element.
*
* @see https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm
*
* @param string $html Input HTML fragment to process.
* @return static|null The created processor if successful, otherwise null.
*/
private function spawn_fragment_parser( string $html ): ?self {
if ( $this->get_token_type() !== '#tag' ) {
return null;
}

/*
* Prevent creating fragments at "self-contained" nodes.
*
* @see https://github.com/WordPress/wordpress-develop/pull/7141
* @see https://github.com/WordPress/wordpress-develop/pull/7198
*/
if (
'html' === $this->get_namespace() &&
in_array( $this->get_tag(), array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true )
) {
return null;
}

$fragment_processor = self::create_fragment( $html );
$fragment_processor->compat_mode = $this->compat_mode;

// @todo The context element probably needs a namespace{
$context_element = array( $this->get_tag(), array() );
foreach ( $this->get_attribute_names_with_prefix( '' ) as $name => $value ) {
$context_element[1][ $name ] = $value;
}
$fragment_processor->state->context_node = $context_element;

if ( 'TEMPLATE' === $context_element[0] ) {
$fragment_processor->state->stack_of_template_insertion_modes[] = WP_HTML_Processor_State::INSERTION_MODE_IN_TEMPLATE;
}

$fragment_processor->reset_insertion_mode_appropriately();

// @todo Set the parser's form element pointer.

$fragment_processor->state->encoding_confidence = 'irrelevant';

return $fragment_processor;
}

/**
* Stops the parser and terminates its execution when encountering unsupported markup.
*
Expand Down

0 comments on commit c621467

Please sign in to comment.