Skip to content

Commit 2703671

Browse files
committed
Merge branch 'html-api/add-spawn-fragment-parser-method' into block-bindings/html-api-private-set-inner-html
2 parents 548cc42 + 05801bb commit 2703671

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,35 @@ function ( WP_HTML_Token $token ): void {
428428
* Creates a fragment processor at the current node.
429429
*
430430
* HTML Fragment parsing always happens with a context node. HTML Fragment Processors can be
431-
* instantiated with a `BODY` context node via `WP_HTML_Processor::create_fragment()`.
431+
* instantiated with a `BODY` context node via `WP_HTML_Processor::create_fragment( $html )`.
432432
*
433-
* The context node may impact how a fragment of HTML is parsed. For example, when parsing
434-
* `<rect />A</rect>B`:
433+
* The context node may impact how a fragment of HTML is parsed. For example, consider the HTML
434+
* fragment `<td />Inside TD?</td>`.
435435
*
436436
* With a BODY context node results in the following tree:
437437
*
438+
* └─#text Inside TD?
439+
*
440+
* Notice that the `<td>` tags are completely ignored.
441+
*
442+
* Compare that with an SVG context node that produces the following tree:
443+
*
444+
* ├─svg:td
445+
* └─#text Inside TD?
446+
*
447+
* Here, a `td` node in the `svg` namespace is created, and its self-closing flag is respected.
448+
* This is a peculiarity of parsing HTML in foreign content like SVG.
449+
*
450+
* Finally, consider the tree produced with a TABLE context node:
451+
*
452+
* └─TBODY
453+
* └─TR
454+
* └─TD
455+
* └─#text Inside TD?
456+
*
457+
* These examples demonstrate how important the context node may be when processing an HTML
458+
* fragment. Special care must be taken when processing fragments that are expected to appear
459+
* in specific contexts. SVG and TABLE are good examples, but there are others.
438460
*
439461
* @see https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm
440462
*
@@ -462,6 +484,9 @@ public function create_fragment_at_current_node( string $html ) {
462484
}
463485

464486
$fragment_processor = static::create_fragment( $html );
487+
if ( null === $fragment_processor ) {
488+
return null;
489+
}
465490

466491
$fragment_processor->change_parsing_namespace(
467492
$this->current_element->token->integration_node_type ? 'html' : $namespace
@@ -494,7 +519,9 @@ public function create_fragment_at_current_node( string $html ) {
494519
*/
495520
foreach ( $this->state->stack_of_open_elements->walk_up() as $element ) {
496521
if ( 'FORM' === $element->node_name && 'html' === $element->namespace ) {
497-
$fragment_processor->state->form_element = $element;
522+
$fragment_processor->state->form_element = clone $element;
523+
$fragment_processor->state->form_element->bookmark_name = null;
524+
$fragment_processor->state->form_element->on_destroy = null;
498525
break;
499526
}
500527
}

tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private static function build_tree_representation( ?string $fragment_context, st
213213
if ( null !== $parent_processor->get_last_error() ) {
214214
throw new Exception( $parent_processor->get_last_error() );
215215
}
216-
$processor = $parent_processor->spawn_fragment_parser( $html );
216+
$processor = $parent_processor->create_fragment_at_current_node( $html );
217217
}
218218

219219
if ( null === $processor ) {
@@ -226,11 +226,6 @@ private static function build_tree_representation( ?string $fragment_context, st
226226
}
227227
}
228228

229-
/*
230-
* The fragment parser will start in 2 levels deep at: html > body > [position]
231-
* and requires adjustment to initial parameters.
232-
* The full parser will not.
233-
*/
234229
$output = '';
235230
$indent_level = 0;
236231
$was_text = null;

0 commit comments

Comments
 (0)