Changeset 56748
- Timestamp:
- 09/29/2023 07:45:53 PM (12 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpcs.xml.dist
r56741 r56748 172 172 <element value="nodeName"/> 173 173 <element value="nodeType"/> 174 <element value="nodeValue"/> 174 175 <element value="parentNode"/> 175 176 <element value="preserveWhiteSpace"/> -
trunk/src/wp-includes/class-wp-customize-manager.php
r56687 r56748 475 475 </script> 476 476 <?php 477 $message .= wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );477 $message .= wp_get_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 478 478 } 479 479 … … 2110 2110 </script> 2111 2111 <?php 2112 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );2112 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 2113 2113 } 2114 2114 … … 2231 2231 </script> 2232 2232 <?php 2233 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );2233 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 2234 2234 } 2235 2235 … … 5020 5020 </script> 5021 5021 <?php 5022 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );5022 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 5023 5023 } 5024 5024 -
trunk/src/wp-includes/functions.php
r56743 r56748 7620 7620 </script> 7621 7621 <?php 7622 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );7622 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 7623 7623 } 7624 7624 -
trunk/src/wp-includes/script-loader.php
r56739 r56748 2854 2854 } 2855 2855 2856 // Ensure markup is XHTML compatible if not HTML5. 2856 /* 2857 * XHTML extracts the contents of the SCRIPT element and then the XML parser 2858 * decodes character references and other syntax elements. This can lead to 2859 * misinterpretation of the script contents or invalid XHTML documents. 2860 * 2861 * Wrapping the contents in a CDATA section instructs the XML parser not to 2862 * transform the contents of the SCRIPT element before passing them to the 2863 * JavaScript engine. 2864 * 2865 * Example: 2866 * 2867 * <script>console.log('…');</script> 2868 * 2869 * In an HTML document this would print "…" to the console, 2870 * but in an XHTML document it would print "…" to the console. 2871 * 2872 * <script>console.log('An image is <img> in HTML');</script> 2873 * 2874 * In an HTML document this would print "An image is <img> in HTML", 2875 * but it's an invalid XHTML document because it interprets the `<img>` 2876 * as an empty tag missing its closing `/`. 2877 * 2878 * @see https://www.w3.org/TR/xhtml1/#h-4.8 2879 */ 2857 2880 if ( ! $is_html5 ) { 2858 $javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript ); // Escape any existing CDATA section. 2881 /* 2882 * If the string `]]>` exists within the JavaScript it would break 2883 * out of any wrapping CDATA section added here, so to start, it's 2884 * necessary to escape that sequence which requires splitting the 2885 * content into two CDATA sections wherever it's found. 2886 * 2887 * Note: it's only necessary to escape the closing `]]>` because 2888 * an additional `<![CDATA[` leaves the contents unchanged. 2889 */ 2890 $javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript ); 2891 2892 // Wrap the entire escaped script inside a CDATA section. 2859 2893 $javascript = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $javascript ); 2860 2894 } … … 3300 3334 return $editor_settings; 3301 3335 } 3336 3337 /** 3338 * Removes leading and trailing _empty_ script tags. 3339 * 3340 * This is a helper meant to be used for literal script tag construction 3341 * within `wp_get_inline_script_tag()` or `wp_print_inline_script_tag()`. 3342 * It removes the literal values of "<script>" and "</script>" from 3343 * around an inline script after trimming whitespace. Typlically this 3344 * is used in conjunction with output buffering, where `ob_get_clean()` 3345 * is passed as the `$contents` argument. 3346 * 3347 * Example: 3348 * 3349 * // Strips exact literal empty SCRIPT tags. 3350 * $js = '<script>sayHello();</script>; 3351 * 'sayHello();' === wp_remove_surrounding_empty_script_tags( $js ); 3352 * 3353 * // Otherwise if anything is different it warns in the JS console. 3354 * $js = '<script type="text/javascript">console.log( "hi" );</script>'; 3355 * 'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js ); 3356 * 3357 * @private 3358 * @since 6.4.0 3359 * 3360 * @see wp_print_inline_script_tag() 3361 * @see wp_get_inline_script_tag() 3362 * 3363 * @param string $contents Script body with manually created SCRIPT tag literals. 3364 * @return string Script body without surrounding script tag literals, or 3365 * original contents if both exact literals aren't present. 3366 */ 3367 function wp_remove_surrounding_empty_script_tags( $contents ) { 3368 $contents = trim( $contents ); 3369 $opener = '<SCRIPT>'; 3370 $closer = '</SCRIPT>'; 3371 3372 if ( 3373 strlen( $contents ) > strlen( $opener ) + strlen( $closer ) && 3374 strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener && 3375 strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer 3376 ) { 3377 return substr( $contents, strlen( $opener ), -strlen( $closer ) ); 3378 } else { 3379 $error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' ); 3380 _doing_it_wrong( __FUNCTION__, $error_message, '6.4' ); 3381 return sprintf( 'console.error(%s)', wp_json_encode( __( 'Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP.' ) . ' ' . $error_message ) ); 3382 } 3383 } -
trunk/src/wp-includes/theme-templates.php
r56687 r56748 206 206 </script> 207 207 <?php 208 $skip_link_script = str_replace( array( '<script>', '</script>' ), '',ob_get_clean() );208 $skip_link_script = wp_remove_surrounding_empty_script_tags( ob_get_clean() ); 209 209 $script_handle = 'wp-block-template-skip-link'; 210 210 wp_register_script( $script_handle, false ); -
trunk/src/wp-includes/theme.php
r56690 r56748 3801 3801 </script> 3802 3802 <?php 3803 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );3803 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 3804 3804 } 3805 3805 -
trunk/src/wp-includes/widgets/class-wp-widget-archives.php
r56687 r56748 121 121 </script> 122 122 <?php 123 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );123 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 124 124 } else { 125 125 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; -
trunk/src/wp-includes/widgets/class-wp-widget-categories.php
r56687 r56748 109 109 110 110 <?php 111 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );111 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 112 112 } else { 113 113 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; -
trunk/src/wp-login.php
r56687 r56748 106 106 <script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script> 107 107 <?php 108 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );108 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 109 109 } 110 110 … … 420 420 ob_start(); 421 421 ?> 422 <script >422 <script type="text/javascript"> 423 423 try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){} 424 424 if(typeof wpOnload==='function')wpOnload(); 425 425 </script> 426 426 <?php 427 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );427 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 428 428 } 429 429 … … 1363 1363 <script>setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script> 1364 1364 <?php 1365 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );1365 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 1366 1366 } 1367 1367 … … 1628 1628 </script> 1629 1629 <?php 1630 wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '',ob_get_clean() ) );1630 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) ); 1631 1631 } 1632 1632 -
trunk/tests/phpunit/tests/customize/manager.php
r56695 r56748 3136 3136 ob_start(); 3137 3137 $manager->remove_frameless_preview_messenger_channel(); 3138 $ output = ob_get_clean();3139 $this->assert StringContainsString( '<script', $output);3138 $processor = new WP_HTML_Tag_Processor( ob_get_clean() ); 3139 $this->assertTrue( $processor->next_tag( 'script' ), 'Failed to find expected SCRIPT element in output.' ); 3140 3140 } 3141 3141 -
trunk/tests/phpunit/tests/dependencies/scripts.php
r56687 r56748 261 261 wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null ); 262 262 $output = get_echo( 'wp_print_scripts' ); 263 $expected = str_replace( "'", '"', "<script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script>" ); 264 $this->assertStringContainsString( $expected, $output, 'Blocking dependents must force delayed dependencies to become blocking.' ); 263 $expected = <<<JS 264 <script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script> 265 <script id="dependent-script-a3-js" src="/dependent-script-a3.js" type="text/javascript"></script> 266 JS; 267 $this->assertEqualMarkup( $expected, $output, 'Blocking dependents must force delayed dependencies to become blocking.' ); 265 268 } 266 269 … … 2998 3001 } 2999 3002 3003 // Normalize other whitespace nodes. 3004 $xpath = new DOMXPath( $dom ); 3005 foreach ( $xpath->query( '//text()' ) as $node ) { 3006 /** @var DOMText $node */ 3007 if ( preg_match( '/^\s+$/', $node->nodeValue ) ) { 3008 $node->nodeValue = ' '; 3009 } 3010 } 3011 3000 3012 return $dom; 3001 3013 }
Note: See TracChangeset
for help on using the changeset viewer.