Make WordPress Core

Changeset 61418


Ignore:
Timestamp:
12/30/2025 01:01:11 PM (4 weeks ago)
Author:
jonsurrell
Message:

Use the HTML API to generate style tags.

The HTML API escapes <style> tag contents to ensure the correct HTML structure. Common HTML escaping is unsuitable for <style> tags because they contain "raw text." The additional safety allows other restrictions, such as rejecting content with <>, to be relaxed or removed because the resulting tag will be well-formed.

Developed in https://github.com/WordPress/wordpress-develop/pull/10656.

Props jonsurrell, westonruter, dmsnell, ramonopoly, soyebsalar01, drw158, sabernhardt.
See #64418.

Location:
trunk/src/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-styles.php

    r61411 r61418  
    159159
    160160        if ( $inline_style ) {
    161             $inline_style_tag = sprintf(
    162                 "<style id='%s-inline-css'>\n%s\n</style>\n",
    163                 esc_attr( $handle ),
    164                 $inline_style
    165             );
     161            $processor = new WP_HTML_Tag_Processor( '<style></style>' );
     162            $processor->next_tag();
     163            $processor->set_attribute( 'id', "{$handle}-inline-css" );
     164            $processor->set_modifiable_text( "\n{$inline_style}\n" );
     165            $inline_style_tag = "{$processor->get_updated_html()}\n";
    166166        } else {
    167167            $inline_style_tag = '';
     
    337337        }
    338338
    339         printf(
    340             "<style id='%s-inline-css'>\n%s\n</style>\n",
    341             esc_attr( $handle ),
    342             $output
    343         );
     339        $processor = new WP_HTML_Tag_Processor( '<style></style>' );
     340        $processor->next_tag();
     341        $processor->set_attribute( 'id', "{$handle}-inline-css" );
     342        $processor->set_modifiable_text( "\n{$output}\n" );
     343        echo "{$processor->get_updated_html()}\n";
    344344
    345345        return true;
  • trunk/src/wp-includes/fonts/class-wp-font-face.php

    r61411 r61418  
    9393        }
    9494
    95         printf( $this->get_style_element(), $css );
     95        $processor = new WP_HTML_Tag_Processor( '<style class="wp-fonts-local"></style>' );
     96        $processor->next_tag();
     97        $processor->set_modifiable_text( "\n{$css}\n" );
     98        echo "{$processor->get_updated_html()}\n";
    9699    }
    97100
     
    195198
    196199    /**
    197      * Gets the style element for wrapping the `@font-face` CSS.
    198      *
    199      * @since 6.4.0
    200      *
    201      * @return string The style element.
    202      */
    203     private function get_style_element() {
    204         return "<style class='wp-fonts-local'>\n%s\n</style>\n";
    205     }
    206 
    207     /**
    208200     * Gets the `@font-face` CSS styles for locally-hosted font files.
    209201     *
  • trunk/src/wp-includes/script-loader.php

    r61416 r61418  
    24142414
    24152415        if ( ! empty( $wp_styles->print_code ) ) {
    2416             echo "<style>\n";
    2417             echo $wp_styles->print_code;
    2418             echo sprintf( "\n/*# sourceURL=%s */", rawurlencode( $concat_source_url ) );
    2419             echo "\n</style>\n";
     2416            $processor = new WP_HTML_Tag_Processor( '<style></style>' );
     2417            $processor->next_tag();
     2418            $style_tag_contents = "\n{$wp_styles->print_code}\n"
     2419                . sprintf( "/*# sourceURL=%s */\n", rawurlencode( $concat_source_url ) );
     2420            $processor->set_modifiable_text( $style_tag_contents );
     2421            echo "{$processor->get_updated_html()}\n";
    24202422        }
    24212423    }
     
    31723174        $action_hook_name,
    31733175        static function () use ( $style ) {
    3174             echo "<style>$style</style>\n";
     3176            $processor = new WP_HTML_Tag_Processor( '<style></style>' );
     3177            $processor->next_tag();
     3178            $processor->set_modifiable_text( $style );
     3179            echo "{$processor->get_updated_html()}\n";
    31753180        },
    31763181        $priority
  • trunk/src/wp-includes/theme.php

    r61411 r61418  
    19511951        $style .= $image . $position . $size . $repeat . $attachment;
    19521952    }
    1953     ?>
    1954 <style<?php echo $type_attr; ?> id="custom-background-css">
    1955 body.custom-background { <?php echo trim( $style ); ?> }
    1956 </style>
    1957     <?php
     1953
     1954    $processor = new WP_HTML_Tag_Processor( "<style{$type_attr} id=\"custom-background-css\"></style>" );
     1955    $processor->next_tag();
     1956
     1957    $style_tag_content = 'body.custom-background { ' . trim( $style ) . ' }';
     1958    $processor->set_modifiable_text( "\n{$style_tag_content}\n" );
     1959    echo "{$processor->get_updated_html()}\n";
    19581960}
    19591961
     
    19651967function wp_custom_css_cb() {
    19661968    $styles = wp_get_custom_css();
    1967     if ( $styles || is_customize_preview() ) :
    1968         $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
    1969         ?>
    1970         <style<?php echo $type_attr; ?> id="wp-custom-css">
    1971             <?php
    1972             // Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
    1973             echo strip_tags( $styles );
    1974             ?>
    1975         </style>
    1976         <?php
    1977     endif;
     1969    if ( ! $styles && ! is_customize_preview() ) {
     1970        return;
     1971    }
     1972
     1973    $processor = new WP_HTML_Tag_Processor( '<style></style>' );
     1974    $processor->next_tag();
     1975    if ( ! current_theme_supports( 'html5', 'style' ) ) {
     1976        $processor->set_attribute( 'type', 'text/css' );
     1977    }
     1978    $processor->set_attribute( 'id', 'wp-custom-css' );
     1979    $processor->set_modifiable_text( "\n{$styles}\n" );
     1980    echo "{$processor->get_updated_html()}\n";
    19781981}
    19791982
Note: See TracChangeset for help on using the changeset viewer.