Make WordPress Core

Changeset 48295


Ignore:
Timestamp:
07/04/2020 04:52:49 PM (4 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r48177 r48295  
    1818
    1919        $this->wp_scripts_print_translations_output  = <<<JS
    20 <script type='text/javascript'>
     20<script type='text/javascript' id='__HANDLE__-js-translations'>
    2121( function( domain, translations ) {
    2222    var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
     
    4747
    4848        $ver       = get_bloginfo( 'version' );
    49         $expected  = "<script type='text/javascript' src='http://example.com?ver=$ver'></script>\n";
    50         $expected .= "<script type='text/javascript' src='http://example.com?ver=$ver'></script>\n";
    51         $expected .= "<script type='text/javascript' src='http://example.com?ver=1.2'></script>\n";
    52         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     49        $expected  = "<script type='text/javascript' src='http://example.com?ver=$ver' id='no-deps-no-version-js'></script>\n";
     50        $expected .= "<script type='text/javascript' src='http://example.com?ver=$ver' id='empty-deps-no-version-js'></script>\n";
     51        $expected .= "<script type='text/javascript' src='http://example.com?ver=1.2' id='empty-deps-version-js'></script>\n";
     52        $expected .= "<script type='text/javascript' src='http://example.com' id='empty-deps-null-version-js'></script>\n";
    5353
    5454        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    7070
    7171        $ver      = get_bloginfo( 'version' );
    72         $expected = "<script src='http://example.com?ver=$ver'></script>\n";
     72        $expected = "<script src='http://example.com?ver=$ver' id='empty-deps-no-version-js'></script>\n";
    7373
    7474        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    9191        // Try with an HTTP reference.
    9292        wp_enqueue_script( 'jquery-http', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' );
    93         $expected .= "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver'></script>\n";
     93        $expected .= "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver' id='jquery-http-js'></script>\n";
    9494
    9595        // Try with an HTTPS reference.
    9696        wp_enqueue_script( 'jquery-https', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' );
    97         $expected .= "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver'></script>\n";
     97        $expected .= "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver' id='jquery-https-js'></script>\n";
    9898
    9999        // Try with an automatic protocol reference (//).
    100100        wp_enqueue_script( 'jquery-doubleslash', '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' );
    101         $expected .= "<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver'></script>\n";
     101        $expected .= "<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver' id='jquery-doubleslash-js'></script>\n";
    102102
    103103        // Try with a local resource and an automatic protocol reference (//).
    104104        $url = '//my_plugin/script.js';
    105105        wp_enqueue_script( 'plugin-script', $url );
    106         $expected .= "<script type='text/javascript' src='$url?ver=$ver'></script>\n";
     106        $expected .= "<script type='text/javascript' src='$url?ver=$ver' id='plugin-script-js'></script>\n";
    107107
    108108        // Try with a bad protocol.
    109109        wp_enqueue_script( 'jquery-ftp', 'ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' );
    110         $expected .= "<script type='text/javascript' src='{$wp_scripts->base_url}ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver'></script>\n";
     110        $expected .= "<script type='text/javascript' src='{$wp_scripts->base_url}ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=$ver' id='jquery-ftp-js'></script>\n";
    111111
    112112        // Go!
     
    151151        wp_enqueue_script( 'test-only-data', 'example.com', array(), null );
    152152        wp_script_add_data( 'test-only-data', 'data', 'testing' );
    153         $expected  = "<script type='text/javascript'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n";
    154         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     153        $expected  = "<script type='text/javascript' id='test-only-data-js-extra'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n";
     154        $expected .= "<script type='text/javascript' src='http://example.com' id='test-only-data-js'></script>\n";
    155155
    156156        // Go!
     
    170170        wp_enqueue_script( 'test-only-conditional', 'example.com', array(), null );
    171171        wp_script_add_data( 'test-only-conditional', 'conditional', 'gt IE 7' );
    172         $expected = "<!--[if gt IE 7]>\n<script type='text/javascript' src='http://example.com'></script>\n<![endif]-->\n";
     172        $expected = "<!--[if gt IE 7]>\n<script type='text/javascript' src='http://example.com' id='test-only-conditional-js'></script>\n<![endif]-->\n";
    173173
    174174        // Go!
     
    189189        wp_script_add_data( 'test-conditional-with-data', 'data', 'testing' );
    190190        wp_script_add_data( 'test-conditional-with-data', 'conditional', 'lt IE 9' );
    191         $expected  = "<!--[if lt IE 9]>\n<script type='text/javascript'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n<![endif]-->\n";
    192         $expected .= "<!--[if lt IE 9]>\n<script type='text/javascript' src='http://example.com'></script>\n<![endif]-->\n";
     191        $expected  = "<!--[if lt IE 9]>\n<script type='text/javascript' id='test-conditional-with-data-js-extra'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n<![endif]-->\n";
     192        $expected .= "<!--[if lt IE 9]>\n<script type='text/javascript' src='http://example.com' id='test-conditional-with-data-js'></script>\n<![endif]-->\n";
    193193
    194194        // Go!
     
    208208        wp_enqueue_script( 'test-invalid', 'example.com', array(), null );
    209209        wp_script_add_data( 'test-invalid', 'invalid', 'testing' );
    210         $expected = "<script type='text/javascript' src='http://example.com'></script>\n";
     210        $expected = "<script type='text/javascript' src='http://example.com' id='test-invalid-js'></script>\n";
    211211
    212212        // Go!
     
    231231     */
    232232    function test_wp_register_script_with_handle_without_source() {
    233         $expected  = "<script type='text/javascript' src='http://example.com?ver=1'></script>\n";
    234         $expected .= "<script type='text/javascript' src='http://example.com?ver=2'></script>\n";
     233        $expected  = "<script type='text/javascript' src='http://example.com?ver=1' id='handle-one-js'></script>\n";
     234        $expected .= "<script type='text/javascript' src='http://example.com?ver=2' id='handle-two-js'></script>\n";
    235235
    236236        wp_register_script( 'handle-one', 'http://example.com', array(), 1 );
     
    320320        $footer = get_echo( 'wp_print_footer_scripts' );
    321321
    322         $expected_header  = "<script type='text/javascript' src='/child-footer.js'></script>\n";
    323         $expected_header .= "<script type='text/javascript' src='/child-head.js'></script>\n";
    324         $expected_footer  = "<script type='text/javascript' src='/parent.js'></script>\n";
     322        $expected_header  = "<script type='text/javascript' src='/child-footer.js' id='child-footer-js'></script>\n";
     323        $expected_header .= "<script type='text/javascript' src='/child-head.js' id='child-head-js'></script>\n";
     324        $expected_footer  = "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
    325325
    326326        $this->assertEquals( $expected_header, $header );
     
    341341        $footer = get_echo( 'wp_print_footer_scripts' );
    342342
    343         $expected_header  = "<script type='text/javascript' src='/child-head.js'></script>\n";
    344         $expected_footer  = "<script type='text/javascript' src='/child-footer.js'></script>\n";
    345         $expected_footer .= "<script type='text/javascript' src='/parent.js'></script>\n";
     343        $expected_header  = "<script type='text/javascript' src='/child-head.js' id='child-head-js'></script>\n";
     344        $expected_footer  = "<script type='text/javascript' src='/child-footer.js' id='child-footer-js'></script>\n";
     345        $expected_footer .= "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
    346346
    347347        $this->assertEquals( $expected_header, $header );
     
    367367        $footer = get_echo( 'wp_print_footer_scripts' );
    368368
    369         $expected_header  = "<script type='text/javascript' src='/child-head.js'></script>\n";
    370         $expected_header .= "<script type='text/javascript' src='/grandchild-head.js'></script>\n";
    371         $expected_header .= "<script type='text/javascript' src='/child2-head.js'></script>\n";
    372         $expected_header .= "<script type='text/javascript' src='/parent-header.js'></script>\n";
    373 
    374         $expected_footer  = "<script type='text/javascript' src='/child-footer.js'></script>\n";
    375         $expected_footer .= "<script type='text/javascript' src='/child2-footer.js'></script>\n";
    376         $expected_footer .= "<script type='text/javascript' src='/parent-footer.js'></script>\n";
     369        $expected_header  = "<script type='text/javascript' src='/child-head.js' id='child-head-js'></script>\n";
     370        $expected_header .= "<script type='text/javascript' src='/grandchild-head.js' id='grandchild-head-js'></script>\n";
     371        $expected_header .= "<script type='text/javascript' src='/child2-head.js' id='child2-head-js'></script>\n";
     372        $expected_header .= "<script type='text/javascript' src='/parent-header.js' id='parent-header-js'></script>\n";
     373
     374        $expected_footer  = "<script type='text/javascript' src='/child-footer.js' id='child-footer-js'></script>\n";
     375        $expected_footer .= "<script type='text/javascript' src='/child2-footer.js' id='child2-footer-js'></script>\n";
     376        $expected_footer .= "<script type='text/javascript' src='/parent-footer.js' id='parent-footer-js'></script>\n";
    377377
    378378        $this->assertEquals( $expected_header, $header );
     
    404404        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
    405405
    406         $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    407         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     406        $expected  = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     407        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
    408408
    409409        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    417417        wp_add_inline_script( 'test-example', 'console.log("after");' );
    418418
    419         $expected  = "<script type='text/javascript' src='http://example.com'></script>\n";
    420         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     419        $expected  = "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     420        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    421421
    422422        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    431431        wp_add_inline_script( 'test-example', 'console.log("after");' );
    432432
    433         $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    434         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    435         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     433        $expected  = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     434        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     435        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    436436
    437437        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    446446        wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
    447447
    448         $expected = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
     448        $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
    449449
    450450        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    459459        wp_add_inline_script( 'test-example', 'console.log("after");' );
    460460
    461         $expected = "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     461        $expected = "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    462462
    463463        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    473473        wp_add_inline_script( 'test-example', 'console.log("after");' );
    474474
    475         $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    476         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     475        $expected  = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     476        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    477477
    478478        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    489489        wp_add_inline_script( 'test-example', 'console.log("after");' );
    490490
    491         $expected  = "<script type='text/javascript'>\nconsole.log(\"before\");\nconsole.log(\"before\");\n</script>\n";
    492         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    493         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\nconsole.log(\"after\");\n</script>\n";
     491        $expected  = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\nconsole.log(\"before\");\n</script>\n";
     492        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     493        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\nconsole.log(\"after\");\n</script>\n";
    494494
    495495        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    505505        wp_add_inline_script( 'test-example', 'console.log("after");' );
    506506
    507         $expected  = "<script type='text/javascript'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
    508         $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    509         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    510         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     507        $expected  = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
     508        $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     509        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     510        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    511511
    512512        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    530530
    531531        $ver       = get_bloginfo( 'version' );
    532         $expected  = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
    533         $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
    534         $expected .= "<script type='text/javascript'>\nconsole.log(\"before two\");\n</script>\n";
    535         $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
    536         $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
     532        $expected  = "<script type='text/javascript' id='one-js-before'>\nconsole.log(\"before one\");\n</script>\n";
     533        $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}' id='one-js'></script>\n";
     534        $expected .= "<script type='text/javascript' id='two-js-before'>\nconsole.log(\"before two\");\n</script>\n";
     535        $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}' id='two-js'></script>\n";
     536        $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}' id='three-js'></script>\n";
    537537
    538538        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    555555
    556556        $ver       = get_bloginfo( 'version' );
    557         $expected  = "<script type='text/javascript'>\nconsole.log(\"before one\");\n</script>\n";
    558         $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}'></script>\n";
    559         $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
    560         $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
     557        $expected  = "<script type='text/javascript' id='one-js-before'>\nconsole.log(\"before one\");\n</script>\n";
     558        $expected .= "<script type='text/javascript' src='/directory/one.js?ver={$ver}' id='one-js'></script>\n";
     559        $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}' id='two-js'></script>\n";
     560        $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}' id='three-js'></script>\n";
    561561
    562562        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    582582        $ver       = get_bloginfo( 'version' );
    583583        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one&amp;ver={$ver}'></script>\n";
    584         $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}'></script>\n";
    585         $expected .= "<script type='text/javascript'>\nconsole.log(\"after two\");\n</script>\n";
    586         $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}'></script>\n";
    587         $expected .= "<script type='text/javascript'>\nconsole.log(\"after three\");\n</script>\n";
    588         $expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}'></script>\n";
     584        $expected .= "<script type='text/javascript' src='/directory/two.js?ver={$ver}' id='two-js'></script>\n";
     585        $expected .= "<script type='text/javascript' id='two-js-after'>\nconsole.log(\"after two\");\n</script>\n";
     586        $expected .= "<script type='text/javascript' src='/directory/three.js?ver={$ver}' id='three-js'></script>\n";
     587        $expected .= "<script type='text/javascript' id='three-js-after'>\nconsole.log(\"after three\");\n</script>\n";
     588        $expected .= "<script type='text/javascript' src='/directory/four.js?ver={$ver}' id='four-js'></script>\n";
    589589
    590590        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    601601
    602602        $expected_localized  = "<!--[if gte IE 9]>\n";
    603         $expected_localized .= "<script type='text/javascript'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
     603        $expected_localized .= "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
    604604        $expected_localized .= "<![endif]-->\n";
    605605
    606606        $expected  = "<!--[if gte IE 9]>\n";
    607         $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    608         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    609         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     607        $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     608        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     609        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    610610        $expected .= "<![endif]-->\n";
    611611
     
    634634        $ver       = get_bloginfo( 'version' );
    635635        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
    636         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    637         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     636        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     637        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    638638
    639639        wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     
    660660        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
    661661        $expected .= "<!--[if gte IE 9]>\n";
    662         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    663         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     662        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     663        $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
    664664        $expected .= "<![endif]-->\n";
    665665
     
    688688        $ver       = get_bloginfo( 'version' );
    689689        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
    690         $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    691         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     690        $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     691        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
    692692
    693693        wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     
    714714        $ver       = get_bloginfo( 'version' );
    715715        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$ver}'></script>\n";
    716         $expected .= "<script type='text/javascript'>\nconsole.log(\"before\");\n</script>\n";
    717         $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
    718         $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/vendor/wp-polyfill.min.js'></script>\n";
    719         $expected .= "<script type='text/javascript'>\n";
     716        $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
     717        $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
     718        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/vendor/wp-polyfill.min.js' id='wp-polyfill-js'></script>\n";
     719        $expected .= "<script type='text/javascript' id='wp-polyfill-js-after'>\n";
    720720        $expected .= "( 'fetch' in window ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-fetch.min.js\"></scr' + 'ipt>' );( document.contains ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-node-contains.min.js\"></scr' + 'ipt>' );( window.DOMRect ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-dom-rect.min.js\"></scr' + 'ipt>' );( window.URL && window.URL.prototype && window.URLSearchParams ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-url.min.js\"></scr' + 'ipt>' );( window.FormData && window.FormData.prototype.keys ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-formdata.min.js\"></scr' + 'ipt>' );( Element.prototype.matches && Element.prototype.closest ) || document.write( '<script src=\"http://example.org/wp-includes/js/dist/vendor/wp-polyfill-element-closest.min.js\"></scr' + 'ipt>' );\n";
    721721        $expected .= "</script>\n";
    722         $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/dom-ready.min.js'></script>\n";
    723         $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/i18n.min.js'></script>\n";
    724         $expected .= "<script type='text/javascript'>\n";
     722        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/dom-ready.min.js' id='wp-dom-ready-js'></script>\n";
     723        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/i18n.min.js' id='wp-i18n-js'></script>\n";
     724        $expected .= "<script type='text/javascript' id='wp-a11y-js-translations'>\n";
    725725        $expected .= "( function( domain, translations ) {\n";
    726726        $expected .= "  var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;\n";
     
    729729        $expected .= "} )( \"default\", { \"locale_data\": { \"messages\": { \"\": {} } } } );\n";
    730730        $expected .= "</script>\n";
    731         $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/a11y.min.js'></script>\n";
    732         $expected .= "<script type='text/javascript' src='http://example2.com'></script>\n";
    733         $expected .= "<script type='text/javascript'>\nconsole.log(\"after\");\n</script>\n";
     731        $expected .= "<script type='text/javascript' src='/wp-includes/js/dist/a11y.min.js' id='wp-a11y-js'></script>\n";
     732        $expected .= "<script type='text/javascript' src='http://example2.com' id='test-example2-js'></script>\n";
     733        $expected .= "<script type='text/javascript' id='test-example2-js-after'>\nconsole.log(\"after\");\n</script>\n";
    734734
    735735        wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
     
    768768        $wp_scripts->do_concat = true;
    769769
    770         $expected_tail  = "<script type='text/javascript' src='/customize-dependency.js'></script>\n";
    771         $expected_tail .= "<script type='text/javascript'>\n";
     770        $expected_tail  = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
     771        $expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
    772772        $expected_tail .= "tryCustomizeDependency()\n";
    773773        $expected_tail .= "</script>\n";
     
    780780        $print_scripts .= get_echo( '_print_scripts' );
    781781
    782         $tail = substr( $print_scripts, strrpos( $print_scripts, "<script type='text/javascript' src='/customize-dependency.js'>" ) );
     782        $tail = substr( $print_scripts, strrpos( $print_scripts, "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'>" ) );
    783783        $this->assertEquals( $expected_tail, $tail );
    784784    }
     
    800800
    801801        $ver       = get_bloginfo( 'version' );
    802         $expected  = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}'></script>\n";
    803         $expected .= "<script type='text/javascript'>\nconsole.log(\"after one\");\n</script>\n";
    804         $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}'></script>\n";
    805         $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
    806         $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
     802        $expected  = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$ver}' id='one-js'></script>\n";
     803        $expected .= "<script type='text/javascript' id='one-js-after'>\nconsole.log(\"after one\");\n</script>\n";
     804        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$ver}' id='two-js'></script>\n";
     805        $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}' id='three-js'></script>\n";
     806        $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}' id='four-js'></script>\n";
    807807
    808808        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    826826        $ver       = get_bloginfo( 'version' );
    827827        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one,two&amp;ver={$ver}'></script>\n";
    828         $expected .= "<script type='text/javascript'>\nconsole.log(\"before three\");\n</script>\n";
    829         $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
    830         $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
     828        $expected .= "<script type='text/javascript' id='three-js-before'>\nconsole.log(\"before three\");\n</script>\n";
     829        $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}' id='three-js'></script>\n";
     830        $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}' id='four-js'></script>\n";
    831831
    832832        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     
    841841        wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
    842842
    843         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     843        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    844844        $expected .= str_replace(
    845845            array(
    846846                '__DOMAIN__',
     847                '__HANDLE__',
    847848                '__JSON_TRANSLATIONS__',
    848849            ),
    849850            array(
    850851                'default',
     852                'test-example',
    851853                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
    852854            ),
    853855            $this->wp_scripts_print_translations_output
    854856        );
    855         $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     857        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
    856858
    857859        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    866868        wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' );
    867869
    868         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     870        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    869871        $expected .= str_replace(
    870872            array(
    871873                '__DOMAIN__',
     874                '__HANDLE__',
    872875                '__JSON_TRANSLATIONS__',
    873876            ),
    874877            array(
    875878                'internationalized-plugin',
     879                'plugin-example',
    876880                file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
    877881            ),
    878882            $this->wp_scripts_print_translations_output
    879883        );
    880         $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
     884        $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js' id='plugin-example-js'></script>\n";
    881885
    882886        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    891895        wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' );
    892896
    893         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     897        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    894898        $expected .= str_replace(
    895899            array(
    896900                '__DOMAIN__',
     901                '__HANDLE__',
    897902                '__JSON_TRANSLATIONS__',
    898903            ),
    899904            array(
    900905                'internationalized-theme',
     906                'theme-example',
    901907                file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
    902908            ),
    903909            $this->wp_scripts_print_translations_output
    904910        );
    905         $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
     911        $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js' id='theme-example-js'></script>\n";
    906912
    907913        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    916922        wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' );
    917923
    918         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     924        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    919925        $expected .= str_replace(
    920926            array(
    921927                '__DOMAIN__',
     928                '__HANDLE__',
    922929                '__JSON_TRANSLATIONS__',
    923930            ),
    924931            array(
    925932                'admin',
     933                'script-handle',
    926934                file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ),
    927935            ),
    928936            $this->wp_scripts_print_translations_output
    929937        );
    930         $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
     938        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='script-handle-js'></script>\n";
    931939
    932940        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    956964        wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' );
    957965
    958         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     966        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    959967        $expected .= str_replace(
    960968            array(
    961969                '__DOMAIN__',
     970                '__HANDLE__',
    962971                '__JSON_TRANSLATIONS__',
    963972            ),
    964973            array(
    965974                'admin',
     975                'test-example',
    966976                '{ "locale_data": { "messages": { "": {} } } }',
    967977            ),
    968978            $this->wp_scripts_print_translations_output
    969979        );
    970         $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
     980        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='test-example-js'></script>\n";
    971981
    972982        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    983993        wp_enqueue_script( 'test-example' );
    984994
    985         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     995        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    986996        $expected .= str_replace(
    987997            array(
    988998                '__DOMAIN__',
     999                '__HANDLE__',
    9891000                '__JSON_TRANSLATIONS__',
    9901001            ),
    9911002            array(
    9921003                'default',
     1004                'test-example',
    9931005                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
    9941006            ),
    9951007            $this->wp_scripts_print_translations_output
    9961008        );
    997         $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     1009        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
    9981010
    9991011        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
     
    10101022        wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
    10111023
    1012         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     1024        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
    10131025        $expected .= str_replace(
    10141026            array(
    10151027                '__DOMAIN__',
     1028                '__HANDLE__',
    10161029                '__JSON_TRANSLATIONS__',
    10171030            ),
    10181031            array(
    10191032                'default',
     1033                'test-dependency',
    10201034                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
    10211035            ),
    10221036            $this->wp_scripts_print_translations_output
    10231037        );
    1024         $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    1025         $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";
     1038        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-dependency-js'></script>\n";
     1039        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js' id='test-example-js'></script>\n";
    10261040
    10271041        $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
Note: See TracChangeset for help on using the changeset viewer.