@@ -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 }
0 commit comments