@@ -560,6 +560,45 @@ public function get_raw_outer_markup() {
560560 return $ outer_markup ;
561561 }
562562
563+ /**
564+ * Replaces the raw HTML of the currently-matched tag with new HTML.
565+ * This replaces the entire contents of the tag including the tag itself.
566+ *
567+ * @param string $new_html
568+ * @return bool|null Whether the contents were updated.
569+ */
570+ public function set_raw_outer_markup ( $ new_html ) {
571+ if ( null === $ this ->get_tag () ) {
572+ return null ;
573+ }
574+
575+ $ this ->set_bookmark ( 'start ' );
576+ $ start_tag = $ this ->current_token ->node_name ;
577+
578+ if ( self ::is_void ( $ start_tag ) ) {
579+ $ this ->replace_using_bookmarks ( $ new_html , 'before ' , 'start ' , 'after ' , 'start ' );
580+ $ this ->release_bookmark ( 'start ' );
581+ return true ;
582+ }
583+
584+ $ found_tag = $ this ->step_until_tag_is_closed ();
585+ $ this ->set_bookmark ( 'end ' );
586+
587+ if ( $ found_tag ) {
588+ $ did_close = $ this ->get_tag () === $ start_tag && $ this ->is_tag_closer ();
589+ $ end_position = $ did_close ? 'after ' : 'before ' ;
590+ $ this ->replace_using_bookmarks ( $ new_html , 'before ' , 'start ' , $ end_position , 'end ' );
591+ } else {
592+ // If there's no closing tag then the outer markup continues to the end of the document.
593+ $ this ->replace_using_bookmark ( $ new_html , 'before ' , 'start ' );
594+ }
595+
596+ $ this ->seek ( 'start ' );
597+ $ this ->release_bookmark ( 'start ' );
598+ $ this ->release_bookmark ( 'end ' );
599+ return true ;
600+ }
601+
563602 /**
564603 * Steps through the HTML document and stop at the next tag, if any.
565604 *
@@ -940,17 +979,55 @@ private function step_until_tag_is_closed() {
940979 return $ found_tag ;
941980 }
942981
982+ /**
983+ * Replaces content in the HTML document from a bookmark to the end of the document.
984+ *
985+ * @since 6.4.0
986+ *
987+ * @param string $html New HTML to insert into document.
988+ * @param string $start_position "before" to clip before bookmark, "after" to clip after.
989+ * @param string $start_bookmark_name Bookmark name at which to start clipping.
990+ */
991+ private function replace_using_bookmark ( $ html , $ start_position , $ start_bookmark_name ) {
992+ $ start_bookmark = $ this ->bookmarks [ "_ {$ start_bookmark_name }" ];
993+ $ start_offset = 'before ' === $ start_position ? $ start_bookmark ->start : $ start_bookmark ->end + 1 ;
994+
995+ $ this ->lexical_updates [] = new WP_HTML_Text_Replacement ( $ start_offset , strlen ( $ this ->html ), $ html );
996+ $ this ->apply_attributes_updates ();
997+ }
998+
999+ /**
1000+ * Replaces content in the HTML document from one bookmark to another.
1001+ *
1002+ * @since 6.4.0
1003+ *
1004+ * @param string $html New HTML to insert into document.
1005+ * @param string $start_position "before" to clip before bookmark, "after" to clip after.
1006+ * @param string $start_bookmark_name Bookmark name at which to start clipping.
1007+ * @param string $end_position "before" to clip before bookmark, "after" to clip after.
1008+ * @param string $end_bookmark_name Bookmark name at which to end clipping.
1009+ */
1010+ private function replace_using_bookmarks ( $ html , $ start_position , $ start_bookmark_name , $ end_position , $ end_bookmark_name ) {
1011+ $ start_bookmark = $ this ->bookmarks [ "_ {$ start_bookmark_name }" ];
1012+ $ end_bookmark = $ this ->bookmarks [ "_ {$ end_bookmark_name }" ];
1013+ $ start_offset = 'before ' === $ start_position ? $ start_bookmark ->start : $ start_bookmark ->end + 1 ;
1014+ $ end_offset = 'before ' === $ end_position ? $ end_bookmark ->start : $ end_bookmark ->end + 1 ;
1015+
1016+ $ this ->lexical_updates [] = new WP_HTML_Text_Replacement ( $ start_offset , $ end_offset , $ html );
1017+ $ this ->apply_attributes_updates ();
1018+ }
1019+
9431020 /**
9441021 * Returns a substring of the input HTML document from a bookmark until the end.
9451022 *
9461023 * @since 6.4.0
9471024 *
948- * @param string $start_position "before" to clip before bookmark, "after" to clip after.
949- * @param string $start Bookmark name at which to start clipping.
1025+ * @param string $start_position "before" to clip before bookmark, "after" to clip after.
1026+ * @param string $start_bookmark_name Bookmark name at which to start clipping.
9501027 * @return string Clipped substring of input HTMl document.
9511028 */
952- private function substr_bookmark ( $ start_position , $ start ) {
953- $ start_bookmark = $ this ->bookmarks [ "_ {$ start }" ];
1029+ private function substr_bookmark ( $ start_position , $ start_bookmark_name ) {
1030+ $ start_bookmark = $ this ->bookmarks [ "_ {$ start_bookmark_name }" ];
9541031 $ start_offset = 'before ' === $ start_position ? $ start_bookmark ->start : $ start_bookmark ->end + 1 ;
9551032
9561033 return substr ( $ this ->html , $ start_offset );
@@ -961,15 +1038,15 @@ private function substr_bookmark( $start_position, $start ) {
9611038 *
9621039 * @since 6.4.0
9631040 *
964- * @param string $start_position "before" to clip before bookmark, "after" to clip after.
965- * @param string $start Bookmark name at which to start clipping.
966- * @param string $end_position "before" to clip before bookmark, "after" to clip after.
967- * @param string $end Bookmark name at which to end clipping.
1041+ * @param string $start_position "before" to clip before bookmark, "after" to clip after.
1042+ * @param string $start_bookmark_name Bookmark name at which to start clipping.
1043+ * @param string $end_position "before" to clip before bookmark, "after" to clip after.
1044+ * @param string $end_bookmark_name Bookmark name at which to end clipping.
9681045 * @return string Clipped substring of input HTMl document.
9691046 */
970- private function substr_bookmarks ( $ start_position , $ start , $ end_position , $ end ) {
971- $ start_bookmark = $ this ->bookmarks [ "_ {$ start }" ];
972- $ end_bookmark = $ this ->bookmarks [ "_ {$ end }" ];
1047+ private function substr_bookmarks ( $ start_position , $ start_bookmark_name , $ end_position , $ end_bookmark_name ) {
1048+ $ start_bookmark = $ this ->bookmarks [ "_ {$ start_bookmark_name }" ];
1049+ $ end_bookmark = $ this ->bookmarks [ "_ {$ end_bookmark_name }" ];
9731050 $ start_offset = 'before ' === $ start_position ? $ start_bookmark ->start : $ start_bookmark ->end + 1 ;
9741051 $ end_offset = 'before ' === $ end_position ? $ end_bookmark ->start : $ end_bookmark ->end + 1 ;
9751052
0 commit comments