mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Script Loader: Use wp_get_script_tag() and wp_get_inline_script_tag()/wp_print_inline_script_tag() helper functions to output scripts on the frontend and login screen.
Using script tag helper functions allows plugins to employ the `wp_script_attributes` and `wp_inline_script_attributes` filters to inject the `nonce` attribute to apply Content Security Policy (e.g. Strict CSP). Use of helper functions also simplifies logic in `WP_Scripts`. * Update `wp_get_inline_script_tag()` to wrap inline script in CDATA blocks for XHTML-compatibility when not using HTML5. * Ensure the `type` attribute is printed first in `wp_get_inline_script_tag()` for back-compat. * Wrap existing `<script>` tags in output buffering to retain IDE supports. * In `wp_get_inline_script_tag()`, append the newline to `$javascript` before it is passed into the `wp_inline_script_attributes` filter so that the CSP hash can be computed properly. * In `the_block_template_skip_link()`, opt to enqueue the inline script rather than print it. * Add `ext-php` to `composer.json` under `suggest` as previously it was an undeclared dependency for running PHPUnit tests. * Update tests to rely on `DOMDocument` to compare script markup, normalizing unsemantic differences. Props westonruter, spacedmonkey, flixos90, 10upsimon, dmsnell, mukesh27, joemcgill, swissspidy, azaozz. Fixes #58664. See #39941. git-svn-id: https://develop.svn.wordpress.org/trunk@56687 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -3136,7 +3136,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
||||
ob_start();
|
||||
$manager->remove_frameless_preview_messenger_channel();
|
||||
$output = ob_get_clean();
|
||||
$this->assertStringContainsString( '<script>', $output );
|
||||
$this->assertStringContainsString( '<script', $output );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,11 +42,13 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase {
|
||||
|
||||
$this->wp_scripts_print_translations_output = <<<JS
|
||||
<script type='text/javascript' id='__HANDLE__-js-translations'>
|
||||
/* <![CDATA[ */
|
||||
( function( domain, translations ) {
|
||||
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
|
||||
localeData[""].domain = domain;
|
||||
wp.i18n.setLocaleData( localeData, domain );
|
||||
} )( "__DOMAIN__", __JSON_TRANSLATIONS__ );
|
||||
/* ]]> */
|
||||
</script>
|
||||
JS;
|
||||
$this->wp_scripts_print_translations_output .= "\n";
|
||||
@@ -77,7 +79,7 @@ JS;
|
||||
$expected .= "<script type='text/javascript' src='http://example.com?ver=1.2' id='empty-deps-version-js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com' id='empty-deps-null-version-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
@@ -118,12 +120,12 @@ JS;
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='http://example.org/ms-isa-1.js' id='ms-isa-1-js' data-wp-strategy='{$strategy}'></script>\n";
|
||||
$expected .= wp_get_inline_script_tag(
|
||||
"console.log(\"after one\");\n",
|
||||
'console.log("after one");',
|
||||
array(
|
||||
'id' => 'ms-isa-1-js-after',
|
||||
)
|
||||
);
|
||||
$this->assertSame( $expected, $output, 'Inline scripts in the "after" position, that are attached to a deferred main script, are failing to print/execute.' );
|
||||
$this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "after" position, that are attached to a deferred main script, are failing to print/execute.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,13 +148,13 @@ JS;
|
||||
|
||||
$expected = "<script type='text/javascript' src='http://example.org/ms-insa-3.js' id='ms-insa-3-js'></script>\n";
|
||||
$expected .= wp_get_inline_script_tag(
|
||||
"console.log(\"after one\");\n",
|
||||
'console.log("after one");',
|
||||
array(
|
||||
'id' => 'ms-insa-3-js-after',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame( $expected, $output, 'Inline scripts in the "after" position, that are attached to a blocking main script, are failing to print/execute.' );
|
||||
$this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "after" position, that are attached to a blocking main script, are failing to print/execute.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,7 +182,7 @@ JS;
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
|
||||
$expected = wp_get_inline_script_tag(
|
||||
"console.log(\"before first\");\n",
|
||||
'console.log("before first");',
|
||||
array(
|
||||
'id' => 'ds-i1-1-js-before',
|
||||
)
|
||||
@@ -189,7 +191,7 @@ JS;
|
||||
$expected .= "<script type='text/javascript' src='http://example.org/ds-i1-2.js' id='ds-i1-2-js' $strategy data-wp-strategy='{$strategy}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.org/ds-i1-3.js' id='ds-i1-3-js' $strategy data-wp-strategy='{$strategy}'></script>\n";
|
||||
$expected .= wp_get_inline_script_tag(
|
||||
"console.log(\"before last\");\n",
|
||||
'console.log("before last");',
|
||||
array(
|
||||
'id' => 'ms-i1-1-js-before',
|
||||
'type' => 'text/javascript',
|
||||
@@ -197,7 +199,7 @@ JS;
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='http://example.org/ms-i1-1.js' id='ms-i1-1-js' {$strategy} data-wp-strategy='{$strategy}'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $output, 'Inline scripts in the "before" position, that are attached to a deferred main script, are failing to print/execute.' );
|
||||
$this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "before" position, that are attached to a deferred main script, are failing to print/execute.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,7 +217,7 @@ JS;
|
||||
wp_enqueue_script( 'main-script-a1', '/main-script-a1.js', array(), null, array( 'strategy' => 'async' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-a1.js' id='main-script-a1-js' async data-wp-strategy='async'></script>\n";
|
||||
$this->assertSame( $expected, $output, 'Scripts enqueued with an async loading strategy are failing to have the async attribute applied to the script handle when being printed.' );
|
||||
$this->assertEqualMarkup( $expected, $output, 'Scripts enqueued with an async loading strategy are failing to have the async attribute applied to the script handle when being printed.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -235,9 +237,10 @@ JS;
|
||||
public function test_delayed_dependent_with_blocking_dependency( $strategy ) {
|
||||
wp_enqueue_script( 'dependency-script-a2', '/dependency-script-a2.js', array(), null );
|
||||
wp_enqueue_script( 'main-script-a2', '/main-script-a2.js', array( 'dependency-script-a2' ), null, compact( 'strategy' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-a2.js' id='main-script-a2-js' {$strategy} data-wp-strategy='{$strategy}'></script>";
|
||||
$this->assertStringContainsString( $expected, $output, 'Dependents of a blocking dependency are free to have any strategy.' );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script id='dependency-script-a2-js' src='/dependency-script-a2.js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/main-script-a2.js' id='main-script-a2-js' {$strategy} data-wp-strategy='{$strategy}'></script>";
|
||||
$this->assertEqualMarkup( $expected, $output, 'Dependents of a blocking dependency are free to have any strategy.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,7 +260,7 @@ JS;
|
||||
wp_enqueue_script( 'main-script-a3', '/main-script-a3.js', array(), null, compact( 'strategy' ) );
|
||||
wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script>";
|
||||
$expected = str_replace( "'", '"', "<script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script>" );
|
||||
$this->assertStringContainsString( $expected, $output, 'Blocking dependents must force delayed dependencies to become blocking.' );
|
||||
}
|
||||
|
||||
@@ -275,11 +278,12 @@ JS;
|
||||
* @param string $strategy Strategy.
|
||||
*/
|
||||
public function test_delayed_dependent_with_blocking_dependency_not_enqueued( $strategy ) {
|
||||
$this->add_html5_script_theme_support();
|
||||
wp_enqueue_script( 'main-script-a4', '/main-script-a4.js', array(), null, compact( 'strategy' ) );
|
||||
// This dependent is registered but not enqueued, so it should not factor into the eligible loading strategy.
|
||||
wp_register_script( 'dependent-script-a4', '/dependent-script-a4.js', array( 'main-script-a4' ), null );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-a4.js' id='main-script-a4-js' {$strategy} data-wp-strategy='{$strategy}'></script>";
|
||||
$expected = str_replace( "'", '"', "<script src='/main-script-a4.js' id='main-script-a4-js' {$strategy} data-wp-strategy='{$strategy}'></script>" );
|
||||
$this->assertStringContainsString( $expected, $output, 'Only enqueued dependents should affect the eligible strategy.' );
|
||||
}
|
||||
|
||||
@@ -964,9 +968,10 @@ HTML
|
||||
* @covers ::wp_enqueue_script
|
||||
*/
|
||||
public function test_loading_strategy_with_defer_having_no_dependents_nor_dependencies() {
|
||||
$this->add_html5_script_theme_support();
|
||||
wp_enqueue_script( 'main-script-d1', 'http://example.com/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='http://example.com/main-script-d1.js' id='main-script-d1-js' defer data-wp-strategy='defer'></script>\n";
|
||||
$expected = str_replace( "'", '"', "<script src='http://example.com/main-script-d1.js' id='main-script-d1-js' defer data-wp-strategy='defer'></script>\n" );
|
||||
$this->assertStringContainsString( $expected, $output, 'Expected defer, as there is no dependent or dependency' );
|
||||
}
|
||||
|
||||
@@ -980,12 +985,13 @@ HTML
|
||||
* @covers ::wp_enqueue_script
|
||||
*/
|
||||
public function test_loading_strategy_with_defer_dependent_and_varied_dependencies() {
|
||||
$this->add_html5_script_theme_support();
|
||||
wp_enqueue_script( 'dependency-script-d2-1', 'http://example.com/dependency-script-d2-1.js', array(), null, array( 'strategy' => 'defer' ) );
|
||||
wp_enqueue_script( 'dependency-script-d2-2', 'http://example.com/dependency-script-d2-2.js', array(), null );
|
||||
wp_enqueue_script( 'dependency-script-d2-3', 'http://example.com/dependency-script-d2-3.js', array( 'dependency-script-d2-2' ), null, array( 'strategy' => 'defer' ) );
|
||||
wp_enqueue_script( 'main-script-d2', 'http://example.com/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='http://example.com/main-script-d2.js' id='main-script-d2-js' defer data-wp-strategy='defer'></script>\n";
|
||||
$expected = '<script src="http://example.com/main-script-d2.js" id="main-script-d2-js" defer data-wp-strategy="defer"></script>';
|
||||
$this->assertStringContainsString( $expected, $output, 'Expected defer, as all dependencies are either deferred or blocking' );
|
||||
}
|
||||
|
||||
@@ -999,12 +1005,13 @@ HTML
|
||||
* @covers ::wp_enqueue_script
|
||||
*/
|
||||
public function test_loading_strategy_with_all_defer_dependencies() {
|
||||
$this->add_html5_script_theme_support();
|
||||
wp_enqueue_script( 'main-script-d3', 'http://example.com/main-script-d3.js', array(), null, array( 'strategy' => 'defer' ) );
|
||||
wp_enqueue_script( 'dependent-script-d3-1', 'http://example.com/dependent-script-d3-1.js', array( 'main-script-d3' ), null, array( 'strategy' => 'defer' ) );
|
||||
wp_enqueue_script( 'dependent-script-d3-2', 'http://example.com/dependent-script-d3-2.js', array( 'dependent-script-d3-1' ), null, array( 'strategy' => 'defer' ) );
|
||||
wp_enqueue_script( 'dependent-script-d3-3', 'http://example.com/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='http://example.com/main-script-d3.js' id='main-script-d3-js' defer data-wp-strategy='defer'></script>\n";
|
||||
$expected = '<script src="http://example.com/main-script-d3.js" id="main-script-d3-js" defer data-wp-strategy="defer"></script>';
|
||||
$this->assertStringContainsString( $expected, $output, 'Expected defer, as all dependents have defer loading strategy' );
|
||||
}
|
||||
|
||||
@@ -1029,7 +1036,7 @@ HTML
|
||||
$expected .= "<script type='text/javascript' src='/dependent-script-d4-2.js' id='dependent-script-d4-2-js' defer data-wp-strategy='async'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/dependent-script-d4-3.js' id='dependent-script-d4-3-js' defer data-wp-strategy='defer'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $output, 'Scripts registered as defer but that have dependents that are async are expected to have said dependents deferred.' );
|
||||
$this->assertEqualMarkup( $expected, $output, 'Scripts registered as defer but that have dependents that are async are expected to have said dependents deferred.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1049,7 +1056,7 @@ HTML
|
||||
wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null );
|
||||
wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-d4.js' id='main-script-d4-js' data-wp-strategy='defer'></script>\n";
|
||||
$expected = str_replace( "'", '"', "<script type='text/javascript' src='/main-script-d4.js' id='main-script-d4-js' data-wp-strategy='defer'></script>\n" );
|
||||
$this->assertStringContainsString( $expected, $output, 'Scripts registered as defer but that have all dependents with no strategy, should become blocking (no strategy).' );
|
||||
}
|
||||
|
||||
@@ -1067,12 +1074,14 @@ HTML
|
||||
wp_enqueue_script( 'main-script-b1', '/main-script-b1.js', array(), null );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-b1.js' id='main-script-b1-js'></script>\n";
|
||||
$expected = str_replace( "'", '"', $expected );
|
||||
$this->assertSame( $expected, $output, 'Scripts registered with a "blocking" strategy, and who have no dependencies, should have no loading strategy attributes printed.' );
|
||||
|
||||
// strategy args not set.
|
||||
wp_enqueue_script( 'main-script-b2', '/main-script-b2.js', array(), null, array() );
|
||||
$output = get_echo( 'wp_print_scripts' );
|
||||
$expected = "<script type='text/javascript' src='/main-script-b2.js' id='main-script-b2-js'></script>\n";
|
||||
$expected = str_replace( "'", '"', $expected );
|
||||
$this->assertSame( $expected, $output, 'Scripts registered with no strategy assigned, and who have no dependencies, should have no loading strategy attributes printed.' );
|
||||
}
|
||||
|
||||
@@ -1099,7 +1108,7 @@ HTML
|
||||
$expected_header .= "<script type='text/javascript' src='/enqueue-header-old.js' id='enqueue-header-old-js'></script>\n";
|
||||
$expected_header .= "<script type='text/javascript' src='/enqueue-header-new.js' id='enqueue-header-new-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected_header, $actual_header, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
|
||||
$this->assertEqualMarkup( $expected_header, $actual_header, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
|
||||
$this->assertEmpty( $actual_footer, 'Expected footer to be empty since all scripts were for head.' );
|
||||
}
|
||||
|
||||
@@ -1127,7 +1136,7 @@ HTML
|
||||
$expected_footer .= "<script type='text/javascript' src='/enqueue-footer-new.js' id='enqueue-footer-new-js'></script>\n";
|
||||
|
||||
$this->assertEmpty( $actual_header, 'Expected header to be empty since all scripts targeted footer.' );
|
||||
$this->assertSame( $expected_footer, $actual_footer, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
|
||||
$this->assertEqualMarkup( $expected_footer, $actual_footer, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1246,7 +1255,7 @@ HTML
|
||||
wp_register_script( 'invalid-strategy', '/defaults.js', array(), null, array( 'strategy' => 'random-strategy' ) );
|
||||
wp_enqueue_script( 'invalid-strategy' );
|
||||
|
||||
$this->assertSame(
|
||||
$this->assertEqualMarkup(
|
||||
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
|
||||
get_echo( 'wp_print_scripts' )
|
||||
);
|
||||
@@ -1271,7 +1280,7 @@ HTML
|
||||
wp_script_add_data( 'invalid-strategy', 'strategy', 'random-strategy' );
|
||||
wp_enqueue_script( 'invalid-strategy' );
|
||||
|
||||
$this->assertSame(
|
||||
$this->assertEqualMarkup(
|
||||
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
|
||||
get_echo( 'wp_print_scripts' )
|
||||
);
|
||||
@@ -1292,7 +1301,7 @@ HTML
|
||||
public function test_script_strategy_doing_it_wrong_via_enqueue() {
|
||||
wp_enqueue_script( 'invalid-strategy', '/defaults.js', array(), null, array( 'strategy' => 'random-strategy' ) );
|
||||
|
||||
$this->assertSame(
|
||||
$this->assertEqualMarkup(
|
||||
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
|
||||
get_echo( 'wp_print_scripts' )
|
||||
);
|
||||
@@ -1330,7 +1339,7 @@ HTML
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep,two-concat-dep,three-concat-dep&ver={$wp_version}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-js' defer data-wp-strategy='defer'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with a "defer" loading strategy. Deferred scripts should not be part of the script concat loading query.' );
|
||||
$this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with a "defer" loading strategy. Deferred scripts should not be part of the script concat loading query.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1365,7 +1374,7 @@ HTML
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&ver={$wp_version}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/main-script.js' id='main-async-script-1-js' async data-wp-strategy='async'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with an "async" loading strategy. Async scripts should not be part of the script concat loading query.' );
|
||||
$this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with an "async" loading strategy. Async scripts should not be part of the script concat loading query.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1404,7 +1413,7 @@ HTML
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=one-concat-dep-2,two-concat-dep-2,three-concat-dep-2,four-concat-dep-2,five-concat-dep-2,six-concat-dep-2&ver={$wp_version}'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/main-script.js' id='deferred-script-2-js' defer data-wp-strategy='defer'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered as deferred after other blocking scripts are registered. Deferred scripts should not be part of the script concat loader query string. ' );
|
||||
$this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered as deferred after other blocking scripts are registered. Deferred scripts should not be part of the script concat loader query string. ' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1412,7 +1421,6 @@ HTML
|
||||
*/
|
||||
public function test_wp_enqueue_script_with_html5_support_does_not_contain_type_attribute() {
|
||||
global $wp_version;
|
||||
add_theme_support( 'html5', array( 'script' ) );
|
||||
|
||||
$GLOBALS['wp_scripts'] = new WP_Scripts();
|
||||
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
|
||||
@@ -1421,7 +1429,7 @@ HTML
|
||||
|
||||
$expected = "<script src='http://example.com?ver={$wp_version}' id='empty-deps-no-version-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1460,7 +1468,7 @@ HTML
|
||||
$expected .= "<script type='text/javascript' src='{$wp_scripts->base_url}ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver={$wp_version}' id='jquery-ftp-js'></script>\n";
|
||||
|
||||
// Go!
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
@@ -1503,7 +1511,7 @@ HTML
|
||||
$expected .= "<script type='text/javascript' src='http://example.com' id='test-only-data-js'></script>\n";
|
||||
|
||||
// Go!
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
@@ -1521,7 +1529,7 @@ HTML
|
||||
$expected = "<!--[if gt IE 7]>\n<script type='text/javascript' src='http://example.com' id='test-only-conditional-js'></script>\n<![endif]-->\n";
|
||||
|
||||
// Go!
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
@@ -1539,9 +1547,10 @@ HTML
|
||||
wp_script_add_data( 'test-conditional-with-data', 'conditional', 'lt IE 9' );
|
||||
$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";
|
||||
$expected .= "<!--[if lt IE 9]>\n<script type='text/javascript' src='http://example.com' id='test-conditional-with-data-js'></script>\n<![endif]-->\n";
|
||||
$expected = str_replace( "'", '"', $expected );
|
||||
|
||||
// Go!
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
@@ -1559,10 +1568,10 @@ HTML
|
||||
$expected = "<script type='text/javascript' src='http://example.com' id='test-invalid-js'></script>\n";
|
||||
|
||||
// Go!
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
|
||||
// No scripts left to print.
|
||||
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( '', get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1588,7 +1597,7 @@ HTML
|
||||
|
||||
wp_enqueue_script( 'handle-three' );
|
||||
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1676,8 +1685,8 @@ HTML
|
||||
$expected_header .= "<script type='text/javascript' src='/child-head.js' id='child-head-js'></script>\n";
|
||||
$expected_footer = "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertSame( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
$this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1697,8 +1706,8 @@ HTML
|
||||
$expected_footer = "<script type='text/javascript' src='/child-footer.js' id='child-footer-js'></script>\n";
|
||||
$expected_footer .= "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertSame( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
$this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1728,8 +1737,8 @@ HTML
|
||||
$expected_footer .= "<script type='text/javascript' src='/child2-footer.js' id='child2-footer-js'></script>\n";
|
||||
$expected_footer .= "<script type='text/javascript' src='/parent-footer.js' id='parent-footer-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertSame( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
$this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
|
||||
$this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1952,12 +1961,14 @@ HTML
|
||||
$expected_localized = "<!--[if gte IE 9]>\n";
|
||||
$expected_localized .= "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
|
||||
$expected_localized .= "<![endif]-->\n";
|
||||
$expected_localized = str_replace( "'", '"', $expected_localized );
|
||||
|
||||
$expected = "<!--[if gte IE 9]>\n";
|
||||
$expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
|
||||
$expected .= "<![endif]-->\n";
|
||||
$expected = str_replace( "'", '"', $expected );
|
||||
|
||||
wp_enqueue_script( 'test-example', 'example.com', array(), null );
|
||||
wp_localize_script( 'test-example', 'testExample', array( 'foo' => 'bar' ) );
|
||||
@@ -2124,7 +2135,8 @@ HTML
|
||||
_print_scripts();
|
||||
$print_scripts = $this->getActualOutput();
|
||||
|
||||
$tail = substr( $print_scripts, strrpos( $print_scripts, "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'>" ) );
|
||||
$tail = substr( $print_scripts, strrpos( $print_scripts, '<script type="text/javascript" src="/customize-dependency.js" id="customize-dependency-js">' ) );
|
||||
|
||||
$this->assertEqualMarkup( $expected_tail, $tail );
|
||||
}
|
||||
|
||||
@@ -2304,7 +2316,7 @@ HTML
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2331,7 +2343,7 @@ HTML
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js' id='plugin-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2358,7 +2370,7 @@ HTML
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js' id='theme-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2385,7 +2397,7 @@ HTML
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='script-handle-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2415,7 +2427,7 @@ HTML
|
||||
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='test-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2444,7 +2456,7 @@ HTML
|
||||
);
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2474,7 +2486,7 @@ HTML
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-dependency-js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js' id='test-example-js'></script>\n";
|
||||
|
||||
$this->assertSameIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2863,7 +2875,7 @@ HTML
|
||||
$expected = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {$expected};\n/* ]]> */\n</script>\n";
|
||||
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
$this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2928,7 +2940,7 @@ HTML
|
||||
$expected .= "<script type='text/javascript' src='/plugins/wp-i18n.js' id='wp-i18n-js'></script>\n";
|
||||
$expected .= "<script type='text/javascript' src='/default/common.js' id='common-js'></script>\n";
|
||||
|
||||
$this->assertSame( $expected, $print_scripts );
|
||||
$this->assertEqualMarkup( $expected, $print_scripts );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2967,7 +2979,7 @@ HTML
|
||||
* Parse an HTML markup fragment.
|
||||
*
|
||||
* @param string $markup Markup.
|
||||
* @return DOMElement Body element wrapping supplied markup fragment.
|
||||
* @return DOMDocument Document containing the normalized markup fragment.
|
||||
*/
|
||||
protected function parse_markup_fragment( $markup ) {
|
||||
$dom = new DOMDocument();
|
||||
@@ -2985,21 +2997,60 @@ HTML
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
return $dom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert markup is equal.
|
||||
* Assert markup is equal after normalizing script tags.
|
||||
*
|
||||
* @param string $expected Expected markup.
|
||||
* @param string $actual Actual markup.
|
||||
* @param string $message Message.
|
||||
*/
|
||||
protected function assertEqualMarkup( $expected, $actual, $message = '' ) {
|
||||
$expected_dom = $this->parse_markup_fragment( $expected );
|
||||
$actual_dom = $this->parse_markup_fragment( $actual );
|
||||
foreach ( array( $expected_dom, $actual_dom ) as $dom ) {
|
||||
$xpath = new DOMXPath( $dom );
|
||||
/** @var DOMElement $script */
|
||||
|
||||
// Normalize type attribute. When missing, it defaults to text/javascript.
|
||||
foreach ( $xpath->query( '//script[ not( @type ) ]' ) as $script ) {
|
||||
$script->setAttribute( 'type', 'text/javascript' );
|
||||
}
|
||||
|
||||
// Normalize script contents to remove CDATA wrapper.
|
||||
foreach ( $xpath->query( '//script[ contains( text(), "<![CDATA[" ) ]' ) as $script ) {
|
||||
$script->textContent = str_replace(
|
||||
array(
|
||||
"/* <![CDATA[ */\n",
|
||||
"\n/* ]]> */",
|
||||
),
|
||||
'',
|
||||
$script->textContent
|
||||
);
|
||||
}
|
||||
|
||||
// Normalize XHTML-compatible boolean attributes to HTML5 ones.
|
||||
foreach ( array( 'async', 'defer' ) as $attribute ) {
|
||||
foreach ( iterator_to_array( $xpath->query( "//script[ @{$attribute} = '{$attribute}' ]" ) ) as $script ) {
|
||||
$script->removeAttribute( $attribute );
|
||||
$script->setAttributeNode( $dom->createAttribute( $attribute ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
$this->parse_markup_fragment( $expected ),
|
||||
$this->parse_markup_fragment( $actual ),
|
||||
$expected_dom->getElementsByTagName( 'body' )->item( 0 ),
|
||||
$actual_dom->getElementsByTagName( 'body' )->item( 0 ),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds html5 script theme support.
|
||||
*/
|
||||
protected function add_html5_script_theme_support() {
|
||||
add_theme_support( 'html5', array( 'script' ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,4 +119,18 @@ JS;
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that CDATA wrapper duplication is handled.
|
||||
*
|
||||
* @ticket 58664
|
||||
*/
|
||||
public function test_get_inline_script_tag_with_duplicated_cdata_wrappers() {
|
||||
remove_theme_support( 'html5' );
|
||||
|
||||
$this->assertSame(
|
||||
"<script type=\"text/javascript\">\n/* <![CDATA[ */\n/* <![CDATA[ */ console.log( 'Hello World!' ); /* ]]]]><![CDATA[> */\n/* ]]> */\n</script>\n",
|
||||
wp_get_inline_script_tag( "/* <![CDATA[ */ console.log( 'Hello World!' ); /* ]]> */" )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user