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-scripts.php

    r45590 r46164  
    124124
    125125    /**
     126     * Holds a string which contains the type attribute for script tag.
     127     *
     128     * If the current theme does not declare HTML5 support for 'script',
     129     * then it initializes as `type='text/javascript'`.
     130     *
     131     * @since 5.3.0
     132     * @var string
     133     */
     134    private $type_attr = '';
     135
     136    /**
    126137     * Constructor.
    127138     *
     
    131142        $this->init();
    132143        add_action( 'init', array( $this, 'init' ), 0 );
     144
     145        if ( ! current_theme_supports( 'html5', 'script' ) ) {
     146            $this->type_attr = " type='text/javascript'";
     147        }
    133148    }
    134149
     
    206221        }
    207222
    208         echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5.
     223        echo "<script{$this->type_attr}>\n"; // CDATA and type="text/javascript" is not needed for HTML 5.
    209224        echo "/* <![CDATA[ */\n";
    210225        echo "$output\n";
     
    267282
    268283        if ( $before_handle ) {
    269             $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
     284            $before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
    270285        }
    271286
    272287        if ( $after_handle ) {
    273             $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
     288            $after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
    274289        }
    275290
    276291        if ( $before_handle || $after_handle ) {
    277             $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
     292            $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
    278293        } else {
    279294            $inline_script_tag = '';
     
    335350        $translations = $this->print_translations( $handle, false );
    336351        if ( $translations ) {
    337             $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
     352            $translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
    338353        }
    339354
     
    353368        }
    354369
    355         $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
     370        $tag  = $translations . $cond_before . $before_handle;
     371        $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
     372        $tag .= $after_handle . $cond_after;
    356373
    357374        /**
     
    423440
    424441        if ( $echo ) {
    425             printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     442            printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
    426443        }
    427444
     
    558575
    559576        if ( $echo ) {
    560             printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     577            printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
    561578        }
    562579
Note: See TracChangeset for help on using the changeset viewer.