@@ -5286,6 +5286,8 @@ public function seek( $bookmark_name ): bool {
52865286 // Flush any pending updates to the document before beginning.
52875287 $ this ->get_updated_html ();
52885288
5289+ echo 'Seeking to ' . $ bookmark_name . "\n" ;
5290+
52895291 $ actual_bookmark_name = "_ {$ bookmark_name }" ;
52905292 $ processor_started_at = $ this ->state ->current_token
52915293 ? $ this ->bookmarks [ $ this ->state ->current_token ->bookmark_name ]->start
@@ -5323,45 +5325,64 @@ public function seek( $bookmark_name ): bool {
53235325 * and computation time.
53245326 */
53255327 if ( 'backward ' === $ direction ) {
5326- /*
5327- * Instead of clearing the parser state and starting fresh, calling the stack methods
5328- * maintains the proper flags in the parser.
5329- */
5330- foreach ( $ this ->state ->stack_of_open_elements ->walk_up () as $ item ) {
5331- if ( 'context-node ' === $ item ->bookmark_name ) {
5332- break ;
5333- }
5328+ echo 'back: ' . "\n" ;
5329+ // Reset necessary parser state
5330+ $ this ->state ->frameset_ok = true ;
5331+ $ this ->state ->stack_of_template_insertion_modes = array ();
5332+ $ this ->state ->head_element = null ;
5333+ $ this ->state ->form_element = null ;
5334+ $ this ->state ->current_token = null ;
5335+ $ this ->element_queue = array ();
5336+ $ this ->current_element = null ;
5337+
5338+ if ( null === $ this ->context_node ) {
5339+ echo 'full parser ' . "\n" ;
5340+ /*
5341+ * In the case of a full parser, the processor needs to be
5342+ * reset to a clean state and start over.
5343+ */
5344+ $ this ->state ->stack_of_open_elements = new WP_HTML_Open_Elements ();
5345+ $ this ->state ->active_formatting_elements = new WP_HTML_Active_Formatting_Elements ();
53345346
5335- $ this ->state ->stack_of_open_elements ->remove_node ( $ item );
5336- }
5347+ $ this ->breadcrumbs = array ();
5348+ $ this ->bytes_already_parsed = 0 ;
5349+ $ this ->parser_state = self ::STATE_READY ;
5350+ } else {
5351+ /*
5352+ * In case the parser is a fragment parser, instead of clearing the
5353+ * parser state and starting fresh, calling the stack methods
5354+ * maintains the proper flags in the parser.
5355+ */
5356+ foreach ( $ this ->state ->stack_of_open_elements ->walk_up () as $ item ) {
5357+ if ( 'context-node ' === $ item ->bookmark_name ) {
5358+ break ;
5359+ }
53375360
5338- foreach ( $ this ->state ->active_formatting_elements ->walk_up () as $ item ) {
5339- if ( 'context-node ' === $ item ->bookmark_name ) {
5340- break ;
5361+ $ this ->state ->stack_of_open_elements ->remove_node ( $ item );
53415362 }
53425363
5343- $ this ->state ->active_formatting_elements ->remove_node ( $ item );
5344- }
5364+ foreach ( $ this ->state ->active_formatting_elements ->walk_up () as $ item ) {
5365+ if ( 'context-node ' === $ item ->bookmark_name ) {
5366+ break ;
5367+ }
53455368
5346- parent ::seek ( 'context-node ' );
5347- $ this ->state ->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY ;
5348- $ this ->state ->frameset_ok = true ;
5349- $ this ->element_queue = array ();
5350- $ this ->current_element = null ;
5369+ $ this ->state ->active_formatting_elements ->remove_node ( $ item );
5370+ }
53515371
5352- if ( isset ( $ this ->context_node ) ) {
5353- $ this ->breadcrumbs = array_slice ( $ this ->breadcrumbs , 0 , 2 );
5354- } else {
5355- $ this ->breadcrumbs = array ();
5372+ parent ::seek ( 'context-node ' );
5373+ $ this ->state ->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_IN_BODY ;
5374+ $ this ->breadcrumbs = array_slice ( $ this ->breadcrumbs , 0 , 2 );
53565375 }
53575376 }
53585377
5378+
53595379 // When moving forwards, reparse the document until reaching the same location as the original bookmark.
5360- if ( $ bookmark_starts_at === $ this ->bookmarks [ $ this ->state ->current_token ->bookmark_name ]->start ) {
5380+ if ( null !== $ this -> state -> current_token && $ bookmark_starts_at === $ this ->bookmarks [ $ this ->state ->current_token ->bookmark_name ]->start ) {
53615381 return true ;
53625382 }
53635383
53645384 while ( $ this ->next_token () ) {
5385+ echo 'bm starts at ' . $ bookmark_starts_at . ' and current token starts at ' . $ this ->bookmarks [ $ this ->state ->current_token ->bookmark_name ]->start . "\n" ;
53655386 if ( $ bookmark_starts_at === $ this ->bookmarks [ $ this ->state ->current_token ->bookmark_name ]->start ) {
53665387 while ( isset ( $ this ->current_element ) && WP_HTML_Stack_Event::POP === $ this ->current_element ->operation ) {
53675388 $ this ->current_element = array_shift ( $ this ->element_queue );
@@ -5370,6 +5391,7 @@ public function seek( $bookmark_name ): bool {
53705391 }
53715392 }
53725393
5394+ echo 'false ' . "\n" ;
53735395 return false ;
53745396 }
53755397
0 commit comments