@@ -428,13 +428,35 @@ function ( WP_HTML_Token $token ): void {
428
428
* Creates a fragment processor at the current node.
429
429
*
430
430
* 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 )`.
432
432
*
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>`.
435
435
*
436
436
* With a BODY context node results in the following tree:
437
437
*
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.
438
460
*
439
461
* @see https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm
440
462
*
@@ -462,6 +484,9 @@ public function create_fragment_at_current_node( string $html ) {
462
484
}
463
485
464
486
$ fragment_processor = static ::create_fragment ( $ html );
487
+ if ( null === $ fragment_processor ) {
488
+ return null ;
489
+ }
465
490
466
491
$ fragment_processor ->change_parsing_namespace (
467
492
$ this ->current_element ->token ->integration_node_type ? 'html ' : $ namespace
@@ -494,7 +519,9 @@ public function create_fragment_at_current_node( string $html ) {
494
519
*/
495
520
foreach ( $ this ->state ->stack_of_open_elements ->walk_up () as $ element ) {
496
521
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 ;
498
525
break ;
499
526
}
500
527
}
0 commit comments