Make WordPress Core


Ignore:
Timestamp:
07/04/2020 04:52:49 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

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

    r47219 r48295  
    228228        }
    229229
    230         echo "<script{$this->type_attr}>\n";
     230        printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
    231231
    232232        // CDATA is not needed for HTML 5.
     
    299299
    300300        if ( $before_handle ) {
    301             $before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
     301            $before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
    302302        }
    303303
    304304        if ( $after_handle ) {
    305             $after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
     305            $after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
    306306        }
    307307
     
    367367        $translations = $this->print_translations( $handle, false );
    368368        if ( $translations ) {
    369             $translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
     369            $translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
    370370        }
    371371
     
    386386
    387387        $tag  = $translations . $cond_before . $before_handle;
    388         $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
     388        $tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) );
    389389        $tag .= $after_handle . $cond_after;
    390390
     
    459459
    460460        if ( $echo ) {
    461             printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
     461            printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
    462462        }
    463463
     
    596596
    597597        if ( $echo ) {
    598             printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
     598            printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
    599599        }
    600600
Note: See TracChangeset for help on using the changeset viewer.