Make WordPress Core


Ignore:
Timestamp:
12/30/2025 01:01:11 PM (3 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/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;
Note: See TracChangeset for help on using the changeset viewer.