Script Loader: Add id attributes to script assets.

This commit adds a unique ID attribute to script loader generated <script> tags as well as related <script> tags for inline JavaScript, translations, or parameters.

This is a first step in adding support for lazy loading scripts and styles, but for now is only used to assist in debugging generated output.

Props dd32, spacedmonkey.
See #48654.

Built from https://develop.svn.wordpress.org/trunk@48295


git-svn-id: http://core.svn.wordpress.org/trunk@48064 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2020-07-04 16:54:02 +00:00
parent df320b33c2
commit 633c453fcc
2 changed files with 8 additions and 8 deletions

View File

@ -227,7 +227,7 @@ class WP_Scripts extends WP_Dependencies {
return $output;
}
echo "<script{$this->type_attr}>\n";
printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
// CDATA is not needed for HTML 5.
if ( $this->type_attr ) {
@ -298,11 +298,11 @@ class WP_Scripts extends WP_Dependencies {
$after_handle = $this->print_inline_script( $handle, 'after', false );
if ( $before_handle ) {
$before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
}
if ( $after_handle ) {
$after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
}
if ( $before_handle || $after_handle ) {
@ -366,7 +366,7 @@ class WP_Scripts extends WP_Dependencies {
$translations = $this->print_translations( $handle, false );
if ( $translations ) {
$translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
@ -385,7 +385,7 @@ class WP_Scripts extends WP_Dependencies {
}
$tag = $translations . $cond_before . $before_handle;
$tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
$tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) );
$tag .= $after_handle . $cond_after;
/**
@ -458,7 +458,7 @@ class WP_Scripts extends WP_Dependencies {
$output = trim( implode( "\n", $output ), "\n" );
if ( $echo ) {
printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
}
return $output;
@ -595,7 +595,7 @@ class WP_Scripts extends WP_Dependencies {
JS;
if ( $echo ) {
printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
}
return $output;

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-48294';
$wp_version = '5.5-alpha-48295';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.