@@ -348,55 +348,41 @@ private function replace_html( string $block_content, string $attribute_name, $s
348348 * @param string $new_content New content to insert in the figcaption element.
349349 * @return bool Whether the inner content was properly replaced.
350350 */
351- public function set_content_between_figcaption_balanced_tags ( $ new_content ) {
352- /*
353- * THIS IS A STOP-GAP MEASURE NOT TO BE EMULATED.
354- *
355- * Check that the processor is paused on an opener tag.
356- *
357- */
358- if (
359- WP_HTML_Processor::STATE_MATCHED_TAG !== $ this ->parser_state ||
360- 'FIGCAPTION ' !== $ this ->get_tag () ||
361- $ this ->is_tag_closer ()
362- ) {
351+ public function set_figcaption_inner_text ( $ new_content ) {
352+ // Check that the processor is paused on an opener tag.
353+ if ( 'FIGCAPTION ' !== $ this ->get_tag () || $ this ->is_tag_closer () ) {
363354 return false ;
364355 }
365356
366- // Set position of the opener tag.
367- $ this ->set_bookmark ( 'opener_tag ' );
368-
369- /*
370- * This is a best-effort guess to visit the closer tag and check it exists.
371- * In the future, this code should rely on the HTML Processor for this kind of operation.
372- */
373- $ tag_name = $ this ->get_tag ();
374- if ( ! $ this ->next_tag (
375- array (
376- 'tag_name ' => $ tag_name ,
377- 'tag_closers ' => 'visit ' ,
378- )
379- ) || ! $ this ->is_tag_closer () ) {
357+ // Once this element closes the depth will be one shallower than it is now.
358+ $ depth = $ this ->get_current_depth ();
359+ while ( $ this ->next_token () && $ this ->get_current_depth () >= $ depth ) {
360+ // This is inside the FIGCAPTION element.
361+ }
362+
363+ if ( null !== $ this ->get_last_error () || $ this ->paused_at_incomplete_token () ) {
380364 return false ;
381365 }
382366
383- // Set position of the closer tag.
384- $ this ->set_bookmark ( 'closer_tag ' );
367+ $ this ->set_bookmark ( 'here ' );
385368
386- // Get opener and closer tag bookmarks.
387- $ opener_tag_bookmark = $ this ->bookmarks ['_opener_tag ' ];
388- $ closer_tag_bookmark = $ this ->bookmarks ['_closer_tag ' ];
369+ $ opening = $ this ->bookmarks [ $ this ->current_element ->token ->bookmark_name ];
370+ $ closing = $ this ->bookmarks ['_here ' ];
371+ $ start = $ opening ->start + $ opening ->length ;
372+
373+ $ this ->lexical_updates [] = new WP_HTML_Text_Replacement (
374+ $ start ,
375+ $ closing ->start - $ start ,
376+ wp_kses_post ( $ new_content )
377+ );
389378
390- // Appends the new content.
391- $ after_opener_tag = $ opener_tag_bookmark ->start + $ opener_tag_bookmark ->length ;
392- $ inner_content_length = $ closer_tag_bookmark ->start - $ after_opener_tag ;
393- $ this ->lexical_updates [] = new WP_HTML_Text_Replacement ( $ after_opener_tag , $ inner_content_length , $ new_content );
394379 return true ;
395380 }
396381 };
397- $ block_reader = $ bindings_processor ::create_fragment ( $ block_content );
382+
383+ $ block_reader = $ bindings_processor ::create_fragment ( $ block_content );
398384 if ( $ block_reader ->next_tag ( 'figcaption ' ) ) {
399- $ block_reader ->set_content_between_figcaption_balanced_tags ( wp_kses_post ( $ source_value ) );
385+ $ block_reader ->set_figcaption_inner_text ( $ source_value );
400386 }
401387 return $ block_reader ->get_updated_html ();
402388 }
0 commit comments