Make WordPress Core


Ignore:
Timestamp:
12/30/2025 01:01:11 PM (2 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.