Make WordPress Core

Changeset 58714

Timestamp:
07/12/2024 10:27:20 PM (4 weeks ago)
Author:
dmsnell
Message:

HTML API: Add context to Unsupported_Exception class for improved debugging.

The HTML Processor internally throws an exception when it reaches HTML
that it knows it cannot process, but this exception is not made
available to calling code. It can be useful to extract more knowledge
about why it gave up, especially for debugging purposes.

In this patch, more context is added to the WP_HTML_Unsupported_Exception
and the last exception is made available to calling code through a new
method, get_unsupported_exception().

Developed in https://github.com/WordPress/wordpress-develop/pull/6985
Discussed in https://core.trac.wordpress.org/ticket/61646

Props bernhard-reiter, dmsnell, jonsurrell.
See #61646.

Location:
trunk/src/wp-includes/html-api
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-processor.php

    r58713 r58714  
    190190
    191191    /**
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
    192203     * Releases a bookmark when PHP garbage-collects its wrapping WP_HTML_Token instance.
    193204     *
     
    386397
    387398    /**
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
    388438     * Returns the last error, if any.
    389439     *
     
    410460    public function get_last_error() {
    411461        return $this->last_error;
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
    412477    }
    413478
     
    842907                // This should be unreachable but PHP doesn't have total type checking on switch.
    843908                default:
    844                     $this->last_error = self::ERROR_UNSUPPORTED;
    845                     throw new WP_HTML_Unsupported_Exception( "Found unrecognized insertion mode '{$this->state->insertion_mode}'." );
     909                    $this->bail( "Unaware of the requested parsing mode: '{$this->state->insertion_mode}'." );
    846910            }
    847911        } catch ( WP_HTML_Unsupported_Exception $e ) {
     
    923987     */
    924988    private function step_initial() {
    925         $this->last_error = self::ERROR_UNSUPPORTED;
    926         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     989        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    927990    }
    928991
     
    9431006     */
    9441007    private function step_before_html() {
    945         $this->last_error = self::ERROR_UNSUPPORTED;
    946         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1008        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    9471009    }
    9481010
     
    9631025     */
    9641026    private function step_before_head() {
    965         $this->last_error = self::ERROR_UNSUPPORTED;
    966         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1027        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    9671028    }
    9681029
     
    9831044     */
    9841045    private function step_in_head() {
    985         $this->last_error = self::ERROR_UNSUPPORTED;
    986         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1046        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    9871047    }
    9881048
     
    10031063     */
    10041064    private function step_in_head_noscript() {
    1005         $this->last_error = self::ERROR_UNSUPPORTED;
    1006         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1065        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    10071066    }
    10081067
     
    10231082     */
    10241083    private function step_after_head() {
    1025         $this->last_error = self::ERROR_UNSUPPORTED;
    1026         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1084        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    10271085    }
    10281086
     
    14461504             */
    14471505            case '-BR':
    1448                 $this->last_error = self::ERROR_UNSUPPORTED;
    1449                 throw new WP_HTML_Unsupported_Exception( 'Closing BR tags require unimplemented special handling.' );
     1506                $this->bail( 'Closing BR tags require unimplemented special handling.' );
     1507                // This return required because PHPCS can't determine that the call to bail() throws.
     1508                return false;
    14501509
    14511510            /*
     
    16031662            case 'TR':
    16041663            case 'XMP':
    1605                 $this->last_error = self::ERROR_UNSUPPORTED;
    1606                 throw new WP_HTML_Unsupported_Exception( "Cannot process {$token_name} element." );
     1664                $this->bail( "Cannot process {$token_name} element." );
    16071665        }
    16081666
     
    16661724     */
    16671725    private function step_in_table() {
    1668         $this->last_error = self::ERROR_UNSUPPORTED;
    1669         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1726        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    16701727    }
    16711728
     
    16861743     */
    16871744    private function step_in_table_text() {
    1688         $this->last_error = self::ERROR_UNSUPPORTED;
    1689         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1745        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    16901746    }
    16911747
     
    17061762     */
    17071763    private function step_in_caption() {
    1708         $this->last_error = self::ERROR_UNSUPPORTED;
    1709         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1764        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    17101765    }
    17111766
     
    17261781     */
    17271782    private function step_in_column_group() {
    1728         $this->last_error = self::ERROR_UNSUPPORTED;
    1729         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1783        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    17301784    }
    17311785
     
    17461800     */
    17471801    private function step_in_table_body() {
    1748         $this->last_error = self::ERROR_UNSUPPORTED;
    1749         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1802        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    17501803    }
    17511804
     
    17661819     */
    17671820    private function step_in_row() {
    1768         $this->last_error = self::ERROR_UNSUPPORTED;
    1769         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1821        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    17701822    }
    17711823
     
    17861838     */
    17871839    private function step_in_cell() {
    1788         $this->last_error = self::ERROR_UNSUPPORTED;
    1789         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     1840        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    17901841    }
    17911842
     
    19872038     */
    19882039    private function step_in_select_in_table() {
    1989         $this->last_error = self::ERROR_UNSUPPORTED;
    1990         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2040        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    19912041    }
    19922042
     
    20072057     */
    20082058    private function step_in_template() {
    2009         $this->last_error = self::ERROR_UNSUPPORTED;
    2010         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2059        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    20112060    }
    20122061
     
    20272076     */
    20282077    private function step_after_body() {
    2029         $this->last_error = self::ERROR_UNSUPPORTED;
    2030         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2078        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    20312079    }
    20322080
     
    20472095     */
    20482096    private function step_in_frameset() {
    2049         $this->last_error = self::ERROR_UNSUPPORTED;
    2050         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2097        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    20512098    }
    20522099
     
    20672114     */
    20682115    private function step_after_frameset() {
    2069         $this->last_error = self::ERROR_UNSUPPORTED;
    2070         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2116        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    20712117    }
    20722118
     
    20872133     */
    20882134    private function step_after_after_body() {
    2089         $this->last_error = self::ERROR_UNSUPPORTED;
    2090         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2135        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    20912136    }
    20922137
     
    21072152     */
    21082153    private function step_after_after_frameset() {
    2109         $this->last_error = self::ERROR_UNSUPPORTED;
    2110         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2154        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    21112155    }
    21122156
     
    21272171     */
    21282172    private function step_in_foreign_content() {
    2129         $this->last_error = self::ERROR_UNSUPPORTED;
    2130         throw new WP_HTML_Unsupported_Exception( "No support for parsing in the '{$this->state->insertion_mode}' state." );
     2173        $this->bail( "No support for parsing in the '{$this->state->insertion_mode}' state." );
    21312174    }
    21322175
     
    28362879        }
    28372880
    2838         $this->last_error = self::ERROR_UNSUPPORTED;
    2839         throw new WP_HTML_Unsupported_Exception( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
     2881        $this->bail( 'Cannot reconstruct active formatting elements when advancing and rewinding is required.' );
    28402882    }
    28412883
     
    30733115            // > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
    30743116            if ( null === $formatting_element ) {
    3075                 $this->last_error = self::ERROR_UNSUPPORTED;
    3076                 throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when "any other end tag" is required.' );
     3117                $this->bail( 'Cannot run adoption agency when "any other end tag" is required.' );
    30773118            }
    30783119
     
    31263167            }
    31273168
    3128             $this->last_error = self::ERROR_UNSUPPORTED;
    3129             throw new WP_HTML_Unsupported_Exception( 'Cannot extract common ancestor in adoption agency algorithm.' );
    3130         }
    3131 
    3132         $this->last_error = self::ERROR_UNSUPPORTED;
    3133         throw new WP_HTML_Unsupported_Exception( 'Cannot run adoption agency when looping required.' );
     3169            $this->bail( 'Cannot extract common ancestor in adoption agency algorithm.' );
     3170        }
     3171
     3172        $this->bail( 'Cannot run adoption agency when looping required.' );
    31343173    }
    31353174
  • trunk/src/wp-includes/html-api/class-wp-html-unsupported-exception.php

    r56274 r58714  
    2222 *
    2323 * @since 6.4.0
     24
    2425 *
    2526 * @access private
     
    2829 */
    2930class WP_HTML_Unsupported_Exception extends Exception {
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
    3044
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
    31115}
Note: See TracChangeset for help on using the changeset viewer.