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/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
Note: See TracChangeset for help on using the changeset viewer.