[wp-trac] [WordPress Trac] #62290: Unable to seek to any bookmark in HTML Processor

WordPress Trac noreply at wordpress.org
Thu Oct 24 16:59:28 UTC 2024


#62290: Unable to seek to any bookmark in HTML Processor
--------------------------+---------------------
 Reporter:  westonruter   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.7.1
Component:  HTML API      |     Version:  6.6
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+---------------------

Comment (by westonruter):

 Replying to [comment:2 mi5t4n]:
 > Running above code on the current trunk (6.8-alpha-59274-src), does not
 throw any exceptions.

 You're right. I guess I didn't re-test in trunk when I switched to
 `create_fragment` since `create_full_parser` didn't exist in 6.5. Or I
 didn't re-test in trunk when I removed the root HTML element.

 The following does fail for me in trunk (6.8-alpha-59287):

 {{{#!php
 <?php
 $bookmark_name = 'the-bookmark';

 $html = <<<HTML
 <html lang="en">
         <head>
                 <meta charset="utf-8">
                 <title>...</title>
         </head>
         <body>
                 <div id="$bookmark_name"></div>
         </body>
 </html>
 HTML;

 try {
         $processor = WP_HTML_Processor::create_full_parser( $html );
         while ( $processor->next_tag() ) {
                 if ( 'DIV' === $processor->get_tag() ) {
                         if ( ! $processor->set_bookmark( $bookmark_name )
 ) {
                                 throw new Exception( "Failed to set
 bookmark" );
                         }
                 }
         }
         if ( ! $processor->has_bookmark( $bookmark_name ) ) {
                 throw new Exception( "Unexpectedly has_bookmark returned
 false." );
         }
         if ( ! $processor->seek( $bookmark_name ) ) {
                 throw new Exception( "Failed to seek to bookmark." );
         }
         printf( "PASS (WP %s)\n", get_bloginfo( 'version' ) );
 } catch ( Exception $e ) {
         printf( "FAIL: %s (WP %s)\n", $e->getMessage(), get_bloginfo(
 'version' ) );
 }
 }}}

 I get:

 {{{
 Notice: Function WP_HTML_Tag_Processor::seek was called
 <strong>incorrectly</strong>. Unknown bookmark name. Please see <a
 href="https://developer.wordpress.org/advanced-administration/debug/debug-
 wordpress/">Debugging in WordPress</a> for more information. (This message
 was added in version 6.2.0.) in /var/www/html/wp-includes/functions.php on
 line 6087
 FAIL: Failed to seek to bookmark. (WP 6.8-alpha-59287)
 }}}

 If I replace `create_full_parser` with `create_fragment` I also get a
 failure but this time with PHP warnings:

 {{{
 Warning: Attempt to read property "node_name" on null in /var/www/html/wp-
 includes/html-api/class-wp-html-processor.php on line 5518
 Warning: Undefined variable $node in /var/www/html/wp-includes/html-api
 /class-wp-html-processor.php on line 2991
 FAIL: Failed to seek to bookmark. (WP 6.8-alpha-59287)
 }}}

 But then if I use `create_fragment` ''and'' I reduce the `$html` down to
 just `<div id="$bookmark_name"></div>` then at that point it passes.

 So it seems there are issues with seeking to bookmarks when a full HTML
 document is passed into either `create_fragment` or `create_full_parser`.

 @mi5t4n Please confirm whether you're seeing the same.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62290#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list