WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/18/2019 02:49:30 PM (9 months ago)
Author:
SergeyBiryukov
Message:

Script Loader: Introduce HTML5 support for scripts and styles.

When a theme declares HTML5 support for script and styles via add_theme_support( 'html5', array( 'script', 'style' ) ), the type="text/javascript" and type="text/css" attributes are omitted.

These attributes are unnecessary in HTML5 and cause warnings in the W3C Markup Validation Service.

Props sasiddiqui, swissspidy, knutsp, SergeyBiryukov.
See #42804.

File:
1 edited

Legend:

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

    r46088 r46164  
    102102
    103103    /**
     104     * Holds a string which contains the type attribute for style tag.
     105     *
     106     * If the current theme does not declare HTML5 support for 'style',
     107     * then it initializes as `type='text/css'`.
     108     *
     109     * @since 5.3.0
     110     * @var string
     111     */
     112    private $type_attr = '';
     113
     114    /**
    104115     * Constructor.
    105116     *
     
    115126         */
    116127        do_action_ref_array( 'wp_default_styles', array( &$this ) );
     128
     129        if ( ! current_theme_supports( 'html5', 'style' ) ) {
     130            $this->type_attr = " type='text/css'";
     131        }
    117132    }
    118133
     
    157172
    158173        if ( $inline_style ) {
    159             $inline_style_tag = sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
     174            $inline_style_tag = sprintf(
     175                "<style id='%s-inline-css'%s>\n%s\n</style>\n",
     176                esc_attr( $handle ),
     177                $this->type_attr,
     178                $inline_style
     179            );
    160180        } else {
    161181            $inline_style_tag = '';
     
    198218
    199219        $rel   = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
    200         $title = isset( $obj->extra['title'] ) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
    201 
    202         $tag = "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n";
     220        $title = isset( $obj->extra['title'] ) ? sprintf( "title='%s'", esc_attr( $obj->extra['title'] ) ) : '';
     221
     222        $tag = sprintf(
     223            "<link rel='%s' id='%s-css' %s href='%s'%s media='%s' />\n",
     224            $rel,
     225            $handle,
     226            $title,
     227            $href,
     228            $this->type_attr,
     229            $media
     230        );
    203231
    204232        /**
     
    224252            }
    225253
    226             $rtl_tag = "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n";
     254            $rtl_tag = sprintf(
     255                "<link rel='%s' id='%s-rtl-css' %s href='%s'%s media='%s' />\n",
     256                $rel,
     257                $handle,
     258                $title,
     259                $rtl_href,
     260                $this->type_attr,
     261                $media
     262            );
     263
    227264            /** This filter is documented in wp-includes/class.wp-styles.php */
    228265            $rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
     
    299336        }
    300337
    301         printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
     338        printf(
     339            "<style id='%s-inline-css'%s>\n%s\n</style>\n",
     340            esc_attr( $handle ),
     341            $this->type_attr,
     342            $output
     343        );
    302344
    303345        return true;
Note: See TracChangeset for help on using the changeset viewer.