Make WordPress Core


Ignore:
Timestamp:
07/14/2026 05:06:03 AM (4 months ago)
Author:
ramonopoly
Message:

Style Engine: Preserve important state declarations

Before, state styles appended !important directly to CSS values before Style Engine sanitization. For gradient values, this caused safecss_filter_attr() to reject otherwise valid declarations, so responsive/state gradient CSS might not have been emitted

This commit stores !important as declaration metadata in the Style Engine, preserves it through rule generation and optimization, and appends it only after sanitization.

Props andrewserong, ramonopoly, wildworks.

Fixes #65561.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/style-engine/class-wp-style-engine-css-declarations.php

    r61516 r62720  
    2828
    2929        /**
     30         * CSS declaration options keyed by property name.
     31         *
     32         * @since 7.1.0
     33         *
     34         * @var array
     35         */
     36        protected $declaration_options = array();
     37
     38        /**
    3039         * Constructor for this object.
    3140         *
    … …  
    4756         *
    4857         * @since 6.1.0
     58         * @since 7.1.0 Added the `$options` parameter.
    4959         *
    5060         * @param string $property The CSS property.
    5161         * @param string $value    The CSS value.
    52          * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
    53          */
    54         public function add_declaration( $property, $value ) {
     62         * @param array  $options  {
     63         *     Optional. An array of options. Default empty array.
     64         *
     65         *     @type bool $important Whether to output the declaration with !important. Default false.
     66         * }
     67         * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
     68         */
     69        public function add_declaration( $property, $value, $options = array() ) {
    5570                // Sanitizes the property.
    5671                $property = $this->sanitize_property( $property );
    … …  
    7186                }
    7287
     88                $options = wp_parse_args(
     89                        $options,
     90                        array(
     91                                'important' => false,
     92                        )
     93                );
     94                $options = array_filter( $options );
     95
    7396                // Adds the declaration property/value pair.
    7497                $this->declarations[ $property ] = $value;
     98                if ( $options ) {
     99                        $this->declaration_options[ $property ] = $options;
     100                } else {
     101                        unset( $this->declaration_options[ $property ] );
     102                }
    75103
    76104                return $this;
    … …  
    87115        public function remove_declaration( $property ) {
    88116                unset( $this->declarations[ $property ] );
     117                unset( $this->declaration_options[ $property ] );
    89118                return $this;
    90119        }
    … …  
    132161
    133162        /**
     163         * Gets declaration options keyed by property name.
     164         *
     165         * @since 7.1.0
     166         *
     167         * @return array Declaration options keyed by property name.
     168         */
     169        public function get_declaration_options() {
     170                return $this->declaration_options;
     171        }
     172
     173        /**
    134174         * Filters a CSS property + value pair.
    135175         *
    136176         * @since 6.1.0
     177         * @since 7.1.0 Added the `$options` parameter.
    137178         *
    138179         * @param string $property The CSS property.
    … …  
    140181         * @param string $spacer   Optional. The spacer between the colon and the value.
    141182         *                         Default empty string.
     183         * @param array  $options  {
     184         *     Optional. An array of options. Default empty array.
     185         *
     186         *     @type bool $important Whether to output the declaration with !important. Default false.
     187         * }
    142188         * @return string The filtered declaration or an empty string.
    143189         */
    144         protected static function filter_declaration( $property, $value, $spacer = '' ) {
     190        protected static function filter_declaration( $property, $value, $spacer = '', $options = array() ) {
    145191                $filtered_value = wp_strip_all_tags( $value, true );
    146192                if ( '' !== $filtered_value ) {
    147                         return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
     193                        $options = wp_parse_args(
     194                                $options,
     195                                array(
     196                                        'important' => false,
     197                                )
     198                        );
     199
     200                        $filtered_declaration = safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
     201
     202                        // Only append !important in the presence of an option value and when sanitization returns a single declaration.
     203                        if ( true === $options['important'] && '' !== $filtered_declaration && ! str_contains( $filtered_declaration, ';' ) ) {
     204                                return "$filtered_declaration !important";
     205                        }
     206
     207                        return $filtered_declaration;
    148208                }
    149209                return '';
    … …  
    170230
    171231                foreach ( $declarations_array as $property => $value ) {
    172                         $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
     232                        $filtered_declaration = static::filter_declaration( $property, $value, $spacer, $this->declaration_options[ $property ] ?? array() );
    173233                        if ( $filtered_declaration ) {
    174234                                $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
Note: See TracChangeset for help on using the changeset viewer.