From d241ab3b55b06fed59e7ea03318e9f872399b390 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sat, 4 Jul 2020 16:52:49 +0000 Subject: [PATCH] Script Loader: Add id attributes to script assets. This commit adds a unique ID attribute to script loader generated \n", $this->type_attr, $before_handle ); + $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); } if ( $after_handle ) { - $after_handle = sprintf( "\n%s\n\n", $this->type_attr, $after_handle ); + $after_handle = sprintf( "\n%s\n\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( "\n%s\n\n", $this->type_attr, $translations ); + $translations = sprintf( "\n%s\n\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( "\n", $this->type_attr, $src ); + $tag .= sprintf( "\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( "\n%s\n\n", $this->type_attr, $output ); + printf( "\n%s\n\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( "\n%s\n\n", $this->type_attr, $output ); + printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $output ); } return $output; diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 0251bbfc20..ede0830511 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -17,7 +17,7 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); $this->wp_scripts_print_translations_output = << +\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -69,7 +69,7 @@ JS; wp_enqueue_script( 'empty-deps-no-version', 'example.com' ); $ver = get_bloginfo( 'version' ); - $expected = "\n"; + $expected = "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -90,24 +90,24 @@ JS; // Try with an HTTP reference. wp_enqueue_script( 'jquery-http', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ); - $expected .= "\n"; + $expected .= "\n"; // Try with an HTTPS reference. wp_enqueue_script( 'jquery-https', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ); - $expected .= "\n"; + $expected .= "\n"; // Try with an automatic protocol reference (//). wp_enqueue_script( 'jquery-doubleslash', '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ); - $expected .= "\n"; + $expected .= "\n"; // Try with a local resource and an automatic protocol reference (//). $url = '//my_plugin/script.js'; wp_enqueue_script( 'plugin-script', $url ); - $expected .= "\n"; + $expected .= "\n"; // Try with a bad protocol. wp_enqueue_script( 'jquery-ftp', 'ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ); - $expected .= "\n"; + $expected .= "\n"; // Go! $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -150,8 +150,8 @@ JS; // Enqueue and add data. wp_enqueue_script( 'test-only-data', 'example.com', array(), null ); wp_script_add_data( 'test-only-data', 'data', 'testing' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; // Go! $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -169,7 +169,7 @@ JS; // Enqueue and add conditional comments. wp_enqueue_script( 'test-only-conditional', 'example.com', array(), null ); wp_script_add_data( 'test-only-conditional', 'conditional', 'gt IE 7' ); - $expected = "\n"; + $expected = "\n"; // Go! $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -188,8 +188,8 @@ JS; wp_enqueue_script( 'test-conditional-with-data', 'example.com', array(), null ); wp_script_add_data( 'test-conditional-with-data', 'data', 'testing' ); wp_script_add_data( 'test-conditional-with-data', 'conditional', 'lt IE 9' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; // Go! $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -207,7 +207,7 @@ JS; // Enqueue and add an invalid key. wp_enqueue_script( 'test-invalid', 'example.com', array(), null ); wp_script_add_data( 'test-invalid', 'invalid', 'testing' ); - $expected = "\n"; + $expected = "\n"; // Go! $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); @@ -230,8 +230,8 @@ JS; * @ticket 35229 */ function test_wp_register_script_with_handle_without_source() { - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; wp_register_script( 'handle-one', 'http://example.com', array(), 1 ); wp_register_script( 'handle-two', 'http://example.com', array(), 2 ); @@ -319,9 +319,9 @@ JS; $header = get_echo( 'wp_print_head_scripts' ); $footer = get_echo( 'wp_print_footer_scripts' ); - $expected_header = "\n"; - $expected_header .= "\n"; - $expected_footer = "\n"; + $expected_header = "\n"; + $expected_header .= "\n"; + $expected_footer = "\n"; $this->assertEquals( $expected_header, $header ); $this->assertEquals( $expected_footer, $footer ); @@ -340,9 +340,9 @@ JS; $header = get_echo( 'wp_print_head_scripts' ); $footer = get_echo( 'wp_print_footer_scripts' ); - $expected_header = "\n"; - $expected_footer = "\n"; - $expected_footer .= "\n"; + $expected_header = "\n"; + $expected_footer = "\n"; + $expected_footer .= "\n"; $this->assertEquals( $expected_header, $header ); $this->assertEquals( $expected_footer, $footer ); @@ -366,14 +366,14 @@ JS; $header = get_echo( 'wp_print_head_scripts' ); $footer = get_echo( 'wp_print_footer_scripts' ); - $expected_header = "\n"; - $expected_header .= "\n"; - $expected_header .= "\n"; - $expected_header .= "\n"; + $expected_header = "\n"; + $expected_header .= "\n"; + $expected_header .= "\n"; + $expected_header .= "\n"; - $expected_footer = "\n"; - $expected_footer .= "\n"; - $expected_footer .= "\n"; + $expected_footer = "\n"; + $expected_footer .= "\n"; + $expected_footer .= "\n"; $this->assertEquals( $expected_header, $header ); $this->assertEquals( $expected_footer, $footer ); @@ -403,8 +403,8 @@ JS; wp_enqueue_script( 'test-example', 'example.com', array(), null ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -416,8 +416,8 @@ JS; wp_enqueue_script( 'test-example', 'example.com', array(), null ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -430,9 +430,9 @@ JS; wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -445,7 +445,7 @@ JS; wp_enqueue_script( 'test-example' ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); - $expected = "\n"; + $expected = "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -458,7 +458,7 @@ JS; wp_enqueue_script( 'test-example' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; + $expected = "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -472,8 +472,8 @@ JS; wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -488,9 +488,9 @@ JS; wp_add_inline_script( 'test-example', 'console.log("after");' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -504,10 +504,10 @@ JS; wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -529,11 +529,11 @@ JS; wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); $ver = get_bloginfo( 'version' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -554,10 +554,10 @@ JS; wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); $ver = get_bloginfo( 'version' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -581,11 +581,11 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -600,13 +600,13 @@ JS; $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php. $expected_localized = "\n"; $expected = "\n"; wp_enqueue_script( 'test-example', 'example.com', array(), null ); @@ -633,8 +633,8 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("after");' ); @@ -659,8 +659,8 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -687,8 +687,8 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); @@ -713,24 +713,24 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); @@ -767,8 +767,8 @@ JS; $wp_scripts->base_url = ''; $wp_scripts->do_concat = true; - $expected_tail = "\n"; - $expected_tail .= "\n"; + $expected_tail .= "\n"; @@ -779,7 +779,7 @@ JS; $print_scripts = get_echo( 'wp_print_scripts' ); $print_scripts .= get_echo( '_print_scripts' ); - $tail = substr( $print_scripts, strrpos( $print_scripts, "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -825,9 +825,9 @@ JS; $ver = get_bloginfo( 'version' ); $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -840,19 +840,21 @@ JS; wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null ); wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'default', + 'test-example', file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -865,19 +867,21 @@ JS; wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null ); wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'internationalized-plugin', + 'plugin-example', file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -890,19 +894,21 @@ JS; wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null ); wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'internationalized-theme', + 'theme-example', file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -915,19 +921,21 @@ JS; wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null ); wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'admin', + 'script-handle', file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -955,19 +963,21 @@ JS; wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null ); wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'admin', + 'test-example', '{ "locale_data": { "messages": { "": {} } } }', ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -982,19 +992,21 @@ JS; wp_enqueue_script( 'test-example' ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'default', + 'test-example', file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1009,20 +1021,22 @@ JS; wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null ); - $expected = "\n"; + $expected = "\n"; $expected .= str_replace( array( '__DOMAIN__', + '__HANDLE__', '__JSON_TRANSLATIONS__', ), array( 'default', + 'test-dependency', file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ), ), $this->wp_scripts_print_translations_output ); - $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); }