mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +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:
parent
5c8b4c1b00
commit
4df5cd15a5
@ -12,6 +12,9 @@
|
||||
"require": {
|
||||
"php": ">=7.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-dom": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
|
||||
"squizlabs/php_codesniffer": "3.6.0",
|
||||
|
||||
@ -464,6 +464,8 @@ final class WP_Customize_Manager {
|
||||
),
|
||||
'error' => $ajax_message,
|
||||
);
|
||||
$message .= ob_get_clean();
|
||||
ob_start();
|
||||
?>
|
||||
<script>
|
||||
( function( api, settings ) {
|
||||
@ -472,7 +474,7 @@ final class WP_Customize_Manager {
|
||||
} )( wp.customize, <?php echo wp_json_encode( $settings ); ?> );
|
||||
</script>
|
||||
<?php
|
||||
$message .= ob_get_clean();
|
||||
$message .= wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
wp_die( $message );
|
||||
@ -2083,6 +2085,7 @@ final class WP_Customize_Manager {
|
||||
if ( ! $this->messenger_channel ) {
|
||||
return;
|
||||
}
|
||||
ob_start();
|
||||
?>
|
||||
<script>
|
||||
( function() {
|
||||
@ -2106,6 +2109,7 @@ final class WP_Customize_Manager {
|
||||
} )();
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2201,8 +2205,9 @@ final class WP_Customize_Manager {
|
||||
}
|
||||
}
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
|
||||
_wpCustomizeSettings.values = {};
|
||||
(function( v ) {
|
||||
@ -2225,6 +2230,7 @@ final class WP_Customize_Manager {
|
||||
})( _wpCustomizeSettings.values );
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -4976,8 +4982,9 @@ final class WP_Customize_Manager {
|
||||
}
|
||||
}
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
|
||||
_wpCustomizeSettings.initialClientTimestamp = _.now();
|
||||
_wpCustomizeSettings.controls = {};
|
||||
@ -5012,6 +5019,7 @@ final class WP_Customize_Manager {
|
||||
?>
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1559,7 +1559,7 @@ final class WP_Customize_Nav_Menus {
|
||||
$exports = array(
|
||||
'navMenuInstanceArgs' => $this->preview_nav_menu_instance_args,
|
||||
);
|
||||
printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) );
|
||||
wp_print_inline_script_tag( sprintf( 'var _wpCustomizePreviewNavMenusExports = %s;', wp_json_encode( $exports ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1310,12 +1310,9 @@ final class WP_Customize_Widgets {
|
||||
foreach ( $settings['registeredWidgets'] as &$registered_widget ) {
|
||||
unset( $registered_widget['callback'] ); // May not be JSON-serializeable.
|
||||
}
|
||||
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
var _wpWidgetCustomizerPreviewSettings = <?php echo wp_json_encode( $settings ); ?>;
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag(
|
||||
sprintf( 'var _wpWidgetCustomizerPreviewSettings = %s;', wp_json_encode( $settings ) )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -122,17 +122,6 @@ class WP_Scripts extends WP_Dependencies {
|
||||
*/
|
||||
public $default_dirs;
|
||||
|
||||
/**
|
||||
* Holds a string which contains the type attribute for script tag.
|
||||
*
|
||||
* If the active theme does not declare HTML5 support for 'script',
|
||||
* then it initializes as `type='text/javascript'`.
|
||||
*
|
||||
* @since 5.3.0
|
||||
* @var string
|
||||
*/
|
||||
private $type_attr = '';
|
||||
|
||||
/**
|
||||
* Holds a mapping of dependents (as handles) for a given script handle.
|
||||
* Used to optimize recursive dependency tree checks.
|
||||
@ -167,14 +156,6 @@ class WP_Scripts extends WP_Dependencies {
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public function init() {
|
||||
if (
|
||||
function_exists( 'is_admin' ) && ! is_admin()
|
||||
&&
|
||||
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
|
||||
) {
|
||||
$this->type_attr = " type='text/javascript'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires when the WP_Scripts instance is initialized.
|
||||
*
|
||||
@ -245,20 +226,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return $output;
|
||||
}
|
||||
|
||||
printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
|
||||
|
||||
// CDATA is not needed for HTML 5.
|
||||
if ( $this->type_attr ) {
|
||||
echo "/* <![CDATA[ */\n";
|
||||
}
|
||||
|
||||
echo "$output\n";
|
||||
|
||||
if ( $this->type_attr ) {
|
||||
echo "/* ]]> */\n";
|
||||
}
|
||||
|
||||
echo "</script>\n";
|
||||
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-extra" ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -335,7 +303,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
|
||||
$translations = $this->print_translations( $handle, false );
|
||||
if ( $translations ) {
|
||||
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
|
||||
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
|
||||
}
|
||||
|
||||
if ( $this->do_concat ) {
|
||||
@ -403,21 +371,24 @@ class WP_Scripts extends WP_Dependencies {
|
||||
}
|
||||
|
||||
/** This filter is documented in wp-includes/class-wp-scripts.php */
|
||||
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
|
||||
$src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) );
|
||||
|
||||
if ( ! $src ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$tag = $translations . $cond_before . $before_script;
|
||||
$tag .= sprintf(
|
||||
"<script%s src='%s' id='%s-js'%s%s></script>\n",
|
||||
$this->type_attr,
|
||||
$src, // Value is escaped above.
|
||||
esc_attr( $handle ),
|
||||
$strategy ? " {$strategy}" : '',
|
||||
$intended_strategy ? " data-wp-strategy='{$intended_strategy}'" : ''
|
||||
$attr = array(
|
||||
'src' => $src,
|
||||
'id' => "{$handle}-js",
|
||||
);
|
||||
if ( $strategy ) {
|
||||
$attr[ $strategy ] = true;
|
||||
}
|
||||
if ( $intended_strategy ) {
|
||||
$attr['data-wp-strategy'] = $intended_strategy;
|
||||
}
|
||||
$tag = $translations . $cond_before . $before_script;
|
||||
$tag .= wp_get_script_tag( $attr );
|
||||
$tag .= $after_script . $cond_after;
|
||||
|
||||
/**
|
||||
@ -720,7 +691,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
JS;
|
||||
|
||||
if ( $display ) {
|
||||
printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
|
||||
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
||||
@ -1366,7 +1366,7 @@ function wp_comment_form_unfiltered_html_nonce() {
|
||||
|
||||
if ( current_user_can( 'unfiltered_html' ) ) {
|
||||
wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
|
||||
echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
|
||||
wp_print_inline_script_tag( "(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -193,7 +193,7 @@ final class WP_Customize_Selective_Refresh {
|
||||
);
|
||||
|
||||
// Export data to JS.
|
||||
printf( '<script>var _customizePartialRefreshExports = %s;</script>', wp_json_encode( $exports ) );
|
||||
wp_print_inline_script_tag( sprintf( 'var _customizePartialRefreshExports = %s;', wp_json_encode( $exports ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -7655,6 +7655,7 @@ function wp_post_preview_js() {
|
||||
// Has to match the window name used in post_submit_meta_box().
|
||||
$name = 'wp-preview-' . (int) $post->ID;
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<script>
|
||||
( function() {
|
||||
@ -7670,6 +7671,7 @@ function wp_post_preview_js() {
|
||||
}());
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -2787,7 +2787,11 @@ function wp_sanitize_script_attributes( $attributes ) {
|
||||
*/
|
||||
function wp_get_script_tag( $attributes ) {
|
||||
if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
|
||||
$attributes['type'] = 'text/javascript';
|
||||
// Keep the type attribute as the first for legacy reasons (it has always been this way in core).
|
||||
$attributes = array_merge(
|
||||
array( 'type' => 'text/javascript' ),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Filters attributes to be added to a script tag.
|
||||
@ -2830,9 +2834,23 @@ function wp_print_script_tag( $attributes ) {
|
||||
* @return string String containing inline JavaScript code wrapped around `<script>` tag.
|
||||
*/
|
||||
function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
|
||||
if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
|
||||
$attributes['type'] = 'text/javascript';
|
||||
$is_html5 = current_theme_supports( 'html5', 'script' ) || is_admin();
|
||||
if ( ! isset( $attributes['type'] ) && ! $is_html5 ) {
|
||||
// Keep the type attribute as the first for legacy reasons (it has always been this way in core).
|
||||
$attributes = array_merge(
|
||||
array( 'type' => 'text/javascript' ),
|
||||
$attributes
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure markup is XHTML compatible if not HTML5.
|
||||
if ( ! $is_html5 ) {
|
||||
$javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript ); // Escape any existing CDATA section.
|
||||
$javascript = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $javascript );
|
||||
}
|
||||
|
||||
$javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";
|
||||
|
||||
/**
|
||||
* Filters attributes to be added to a script tag.
|
||||
*
|
||||
@ -2845,8 +2863,6 @@ function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
|
||||
*/
|
||||
$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $javascript );
|
||||
|
||||
$javascript = "\n" . trim( $javascript, "\n\r " ) . "\n";
|
||||
|
||||
return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $javascript );
|
||||
}
|
||||
|
||||
|
||||
@ -160,8 +160,9 @@ function the_block_template_skip_link() {
|
||||
wp_enqueue_style( $handle );
|
||||
|
||||
/**
|
||||
* Print the skip-link script.
|
||||
* Enqueue the skip-link script.
|
||||
*/
|
||||
ob_start();
|
||||
?>
|
||||
<script>
|
||||
( function() {
|
||||
@ -204,6 +205,11 @@ function the_block_template_skip_link() {
|
||||
}() );
|
||||
</script>
|
||||
<?php
|
||||
$skip_link_script = str_replace( array( '<script>', '</script>' ), '', ob_get_clean() );
|
||||
$script_handle = 'wp-block-template-skip-link';
|
||||
wp_register_script( $script_handle, false );
|
||||
wp_add_inline_script( $script_handle, $skip_link_script );
|
||||
wp_enqueue_script( $script_handle );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -3783,9 +3783,9 @@ function wp_customize_support_script() {
|
||||
$admin_origin = parse_url( admin_url() );
|
||||
$home_origin = parse_url( home_url() );
|
||||
$cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
|
||||
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
|
||||
ob_start();
|
||||
?>
|
||||
<script<?php echo $type_attr; ?>>
|
||||
<script>
|
||||
(function() {
|
||||
var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
|
||||
|
||||
@ -3801,6 +3801,7 @@ function wp_customize_support_script() {
|
||||
}());
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -100,8 +100,6 @@ class WP_Widget_Archives extends WP_Widget {
|
||||
$label = __( 'Select Post' );
|
||||
break;
|
||||
}
|
||||
|
||||
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
|
||||
?>
|
||||
|
||||
<option value=""><?php echo esc_html( $label ); ?></option>
|
||||
@ -109,8 +107,8 @@ class WP_Widget_Archives extends WP_Widget {
|
||||
|
||||
</select>
|
||||
|
||||
<script<?php echo $type_attr; ?>>
|
||||
/* <![CDATA[ */
|
||||
<?php ob_start(); ?>
|
||||
<script>
|
||||
(function() {
|
||||
var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
|
||||
function onSelectChange() {
|
||||
@ -120,9 +118,9 @@ class WP_Widget_Archives extends WP_Widget {
|
||||
}
|
||||
dropdown.onchange = onSelectChange;
|
||||
})();
|
||||
/* ]]> */
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
} else {
|
||||
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
|
||||
|
||||
|
||||
@ -92,11 +92,10 @@ class WP_Widget_Categories extends WP_Widget {
|
||||
|
||||
echo '</form>';
|
||||
|
||||
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
|
||||
ob_start();
|
||||
?>
|
||||
|
||||
<script<?php echo $type_attr; ?>>
|
||||
/* <![CDATA[ */
|
||||
<script>
|
||||
(function() {
|
||||
var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
|
||||
function onCatChange() {
|
||||
@ -106,10 +105,10 @@ class WP_Widget_Categories extends WP_Widget {
|
||||
}
|
||||
dropdown.onchange = onCatChange;
|
||||
})();
|
||||
/* ]]> */
|
||||
</script>
|
||||
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
} else {
|
||||
$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
|
||||
|
||||
|
||||
@ -101,9 +101,11 @@ function login_header( $title = 'Log In', $message = '', $wp_error = null ) {
|
||||
* but maybe better if it's not removable by plugins.
|
||||
*/
|
||||
if ( 'loggedout' === $wp_error->get_error_code() ) {
|
||||
ob_start();
|
||||
?>
|
||||
<script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -193,9 +195,10 @@ function login_header( $title = 'Log In', $message = '', $wp_error = null ) {
|
||||
?>
|
||||
</head>
|
||||
<body class="login no-js <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
|
||||
<script type="text/javascript">
|
||||
document.body.className = document.body.className.replace('no-js','js');
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( "document.body.className = document.body.className.replace('no-js','js');" );
|
||||
?>
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Fires in the login page header after the body tag is opened.
|
||||
@ -414,12 +417,14 @@ function login_footer( $input_id = '' ) {
|
||||
<?php
|
||||
|
||||
if ( ! empty( $input_id ) ) {
|
||||
ob_start();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
|
||||
if(typeof wpOnload==='function')wpOnload();
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -441,11 +446,7 @@ function login_footer( $input_id = '' ) {
|
||||
* @since 3.0.0
|
||||
*/
|
||||
function wp_shake_js() {
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
document.querySelector('form').classList.add('shake');
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( "document.querySelector('form').classList.add('shake');" );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1357,9 +1358,11 @@ switch ( $action ) {
|
||||
do_action( 'login_footer' );
|
||||
|
||||
if ( $customize_login ) {
|
||||
ob_start();
|
||||
?>
|
||||
<script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
|
||||
<script>setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
?>
|
||||
@ -1605,15 +1608,12 @@ switch ( $action ) {
|
||||
// Run `wpOnload()` if defined.
|
||||
$login_script .= "if ( typeof wpOnload === 'function' ) { wpOnload() }";
|
||||
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
<?php echo $login_script; ?>
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( $login_script );
|
||||
|
||||
if ( $interim_login ) {
|
||||
ob_start();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
( function() {
|
||||
try {
|
||||
var i, links = document.getElementsByTagName( 'a' );
|
||||
@ -1627,6 +1627,7 @@ switch ( $action ) {
|
||||
}());
|
||||
</script>
|
||||
<?php
|
||||
wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
|
||||
}
|
||||
|
||||
login_footer();
|
||||
|
||||
@ -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!' ); /* ]]> */" )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user