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.

git-svn-id: https://develop.svn.wordpress.org/trunk@46164 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2019-09-18 14:49:30 +00:00
parent 0dbd752500
commit 5fdf48c0ec
13 changed files with 173 additions and 47 deletions

View File

@@ -122,6 +122,17 @@ class WP_Scripts extends WP_Dependencies {
*/
public $default_dirs;
/**
* Holds a string which contains the type attribute for script tag.
*
* If the current theme does not declare HTML5 support for 'script',
* then it initializes as `type='text/javascript'`.
*
* @since 5.3.0
* @var string
*/
private $type_attr = '';
/**
* Constructor.
*
@@ -130,6 +141,10 @@ class WP_Scripts extends WP_Dependencies {
public function __construct() {
$this->init();
add_action( 'init', array( $this, 'init' ), 0 );
if ( ! current_theme_supports( 'html5', 'script' ) ) {
$this->type_attr = " type='text/javascript'";
}
}
/**
@@ -205,7 +220,7 @@ class WP_Scripts extends WP_Dependencies {
return $output;
}
echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5.
echo "<script{$this->type_attr}>\n"; // CDATA and type="text/javascript" is not needed for HTML 5.
echo "/* <![CDATA[ */\n";
echo "$output\n";
echo "/* ]]> */\n";
@@ -266,15 +281,15 @@ class WP_Scripts extends WP_Dependencies {
$after_handle = $this->print_inline_script( $handle, 'after', false );
if ( $before_handle ) {
$before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
$before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
}
if ( $after_handle ) {
$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
$after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
}
if ( $before_handle || $after_handle ) {
$inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
} else {
$inline_script_tag = '';
}
@@ -334,7 +349,7 @@ class WP_Scripts extends WP_Dependencies {
$translations = $this->print_translations( $handle, false );
if ( $translations ) {
$translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
$translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
@@ -352,7 +367,9 @@ class WP_Scripts extends WP_Dependencies {
return true;
}
$tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
$tag = $translations . $cond_before . $before_handle;
$tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
$tag .= $after_handle . $cond_after;
/**
* Filters the HTML script tag of an enqueued script.
@@ -422,7 +439,7 @@ class WP_Scripts extends WP_Dependencies {
$output = trim( implode( "\n", $output ), "\n" );
if ( $echo ) {
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
}
return $output;
@@ -557,7 +574,7 @@ class WP_Scripts extends WP_Dependencies {
JS;
if ( $echo ) {
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
}
return $output;