<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56748] trunk: Script Loader: Harden removal of script tag wrappers.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56748">56748</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56748","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-29 19:45:53 +0000 (Fri, 29 Sep 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Script Loader: Harden removal of script tag wrappers.
* Add `wp_remove_surrounding_empty_script_tags()` to more precisely remove script tag wrappers and warn when doing it wrong.
* Add clarifying comments for XML escaping logic in `wp_get_inline_script_tag()`.
* Leverage `WP_HTML_Tag_Processor` in `test_remove_frameless_preview_messenger_channel`.
* Reuse `assertEqualMarkup` in `test_blocking_dependent_with_delayed_dependency`.
* Normalize whitespace in `parse_markup_fragment` for `assertEqualMarkup`.
Follow-up to <a href="https://core.trac.wordpress.org/changeset/56687">[56687]</a>.
Props dmsnell, westonruter, flixos90.
See <a href="https://core.trac.wordpress.org/ticket/58664">#58664</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkphpcsxmldist">trunk/phpcs.xml.dist</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesthemetemplatesphp">trunk/src/wp-includes/theme-templates.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
<li><a href="#trunksrcwpincludeswidgetsclasswpwidgetarchivesphp">trunk/src/wp-includes/widgets/class-wp-widget-archives.php</a></li>
<li><a href="#trunksrcwpincludeswidgetsclasswpwidgetcategoriesphp">trunk/src/wp-includes/widgets/class-wp-widget-categories.php</a></li>
<li><a href="#trunksrcwploginphp">trunk/src/wp-login.php</a></li>
<li><a href="#trunktestsphpunittestscustomizemanagerphp">trunk/tests/phpunit/tests/customize/manager.php</a></li>
<li><a href="#trunktestsphpunittestsdependenciesscriptsphp">trunk/tests/phpunit/tests/dependencies/scripts.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsdependencieswpRemoveSurroundingEmptyScriptTagsphp">trunk/tests/phpunit/tests/dependencies/wpRemoveSurroundingEmptyScriptTags.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkphpcsxmldist"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/phpcs.xml.dist</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/phpcs.xml.dist 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/phpcs.xml.dist 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -171,6 +171,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="lastChild"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="nodeName"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="nodeType"/>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <element value="nodeValue"/>
</ins><span class="cx" style="display: block; padding: 0 10px"> <element value="parentNode"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="preserveWhiteSpace"/>
</span><span class="cx" style="display: block; padding: 0 10px"> <element value="textContent"/>
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/class-wp-customize-manager.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -474,7 +474,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } )( wp.customize, <?php echo wp_json_encode( $settings ); ?> );
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $message .= wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $message .= wp_get_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_die( $message );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2109,7 +2109,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } )();
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2230,7 +2230,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> })( _wpCustomizeSettings.values );
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5019,7 +5019,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/functions.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7619,7 +7619,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }());
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/script-loader.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2853,9 +2853,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Ensure markup is XHTML compatible if not HTML5.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * XHTML extracts the contents of the SCRIPT element and then the XML parser
+ * decodes character references and other syntax elements. This can lead to
+ * misinterpretation of the script contents or invalid XHTML documents.
+ *
+ * Wrapping the contents in a CDATA section instructs the XML parser not to
+ * transform the contents of the SCRIPT element before passing them to the
+ * JavaScript engine.
+ *
+ * Example:
+ *
+ * <script>console.log('…');</script>
+ *
+ * In an HTML document this would print "…" to the console,
+ * but in an XHTML document it would print "…" to the console.
+ *
+ * <script>console.log('An image is <img> in HTML');</script>
+ *
+ * In an HTML document this would print "An image is <img> in HTML",
+ * but it's an invalid XHTML document because it interprets the `<img>`
+ * as an empty tag missing its closing `/`.
+ *
+ * @see https://www.w3.org/TR/xhtml1/#h-4.8
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $is_html5 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript ); // Escape any existing CDATA section.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * If the string `]]>` exists within the JavaScript it would break
+ * out of any wrapping CDATA section added here, so to start, it's
+ * necessary to escape that sequence which requires splitting the
+ * content into two CDATA sections wherever it's found.
+ *
+ * Note: it's only necessary to escape the closing `]]>` because
+ * an additional `<![CDATA[` leaves the contents unchanged.
+ */
+ $javascript = str_replace( ']]>', ']]]]><![CDATA[>', $javascript );
+
+ // Wrap the entire escaped script inside a CDATA section.
</ins><span class="cx" style="display: block; padding: 0 10px"> $javascript = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $javascript );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3299,3 +3333,51 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $editor_settings;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Removes leading and trailing _empty_ script tags.
+ *
+ * This is a helper meant to be used for literal script tag construction
+ * within `wp_get_inline_script_tag()` or `wp_print_inline_script_tag()`.
+ * It removes the literal values of "<script>" and "</script>" from
+ * around an inline script after trimming whitespace. Typlically this
+ * is used in conjunction with output buffering, where `ob_get_clean()`
+ * is passed as the `$contents` argument.
+ *
+ * Example:
+ *
+ * // Strips exact literal empty SCRIPT tags.
+ * $js = '<script>sayHello();</script>;
+ * 'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );
+ *
+ * // Otherwise if anything is different it warns in the JS console.
+ * $js = '<script type="text/javascript">console.log( "hi" );</script>';
+ * 'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
+ *
+ * @private
+ * @since 6.4.0
+ *
+ * @see wp_print_inline_script_tag()
+ * @see wp_get_inline_script_tag()
+ *
+ * @param string $contents Script body with manually created SCRIPT tag literals.
+ * @return string Script body without surrounding script tag literals, or
+ * original contents if both exact literals aren't present.
+ */
+function wp_remove_surrounding_empty_script_tags( $contents ) {
+ $contents = trim( $contents );
+ $opener = '<SCRIPT>';
+ $closer = '</SCRIPT>';
+
+ if (
+ strlen( $contents ) > strlen( $opener ) + strlen( $closer ) &&
+ strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener &&
+ strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer
+ ) {
+ return substr( $contents, strlen( $opener ), -strlen( $closer ) );
+ } else {
+ $error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' );
+ _doing_it_wrong( __FUNCTION__, $error_message, '6.4' );
+ return sprintf( 'console.error(%s)', wp_json_encode( __( 'Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP.' ) . ' ' . $error_message ) );
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesthemetemplatesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme-templates.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme-templates.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/theme-templates.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -205,7 +205,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }() );
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $skip_link_script = str_replace( array( '<script>', '</script>' ), '', ob_get_clean() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $skip_link_script = wp_remove_surrounding_empty_script_tags( ob_get_clean() );
</ins><span class="cx" style="display: block; padding: 0 10px"> $script_handle = 'wp-block-template-skip-link';
</span><span class="cx" style="display: block; padding: 0 10px"> wp_register_script( $script_handle, false );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_add_inline_script( $script_handle, $skip_link_script );
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/theme.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3800,7 +3800,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }());
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludeswidgetsclasswpwidgetarchivesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/widgets/class-wp-widget-archives.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/widgets/class-wp-widget-archives.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/widgets/class-wp-widget-archives.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,7 +120,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> })();
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludeswidgetsclasswpwidgetcategoriesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/widgets/class-wp-widget-categories.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/widgets/class-wp-widget-categories.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-includes/widgets/class-wp-widget-categories.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -108,7 +108,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwploginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-login.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-login.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/src/wp-login.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -105,7 +105,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> <script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -419,12 +419,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $input_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> ob_start();
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <script>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <script type="text/javascript">
</ins><span class="cx" style="display: block; padding: 0 10px"> try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
</span><span class="cx" style="display: block; padding: 0 10px"> if(typeof wpOnload==='function')wpOnload();
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1362,7 +1362,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> <script>setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); ?>', channel: 'login' }).send('login') }, 1000 );</script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1627,7 +1627,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }());
</span><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_print_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> login_footer();
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/manager.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/tests/phpunit/tests/customize/manager.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3135,8 +3135,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $manager = new WP_Customize_Manager( array( 'messenger_channel' => 'preview-0' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> ob_start();
</span><span class="cx" style="display: block; padding: 0 10px"> $manager->remove_frameless_preview_messenger_channel();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = ob_get_clean();
- $this->assertStringContainsString( '<script', $output );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $processor = new WP_HTML_Tag_Processor( ob_get_clean() );
+ $this->assertTrue( $processor->next_tag( 'script' ), 'Failed to find expected SCRIPT element in output.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsdependenciesscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/dependencies/scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dependencies/scripts.php 2023-09-29 17:11:21 UTC (rev 56747)
+++ trunk/tests/phpunit/tests/dependencies/scripts.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -260,8 +260,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'main-script-a3', '/main-script-a3.js', array(), null, compact( 'strategy' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'dependent-script-a3', '/dependent-script-a3.js', array( 'main-script-a3' ), null );
</span><span class="cx" style="display: block; padding: 0 10px"> $output = get_echo( 'wp_print_scripts' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $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.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = <<<JS
+ <script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script>
+ <script id="dependent-script-a3-js" src="/dependent-script-a3.js" type="text/javascript"></script>
+JS;
+ $this->assertEqualMarkup( $expected, $output, 'Blocking dependents must force delayed dependencies to become blocking.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2997,6 +3000,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Normalize other whitespace nodes.
+ $xpath = new DOMXPath( $dom );
+ foreach ( $xpath->query( '//text()' ) as $node ) {
+ /** @var DOMText $node */
+ if ( preg_match( '/^\s+$/', $node->nodeValue ) ) {
+ $node->nodeValue = ' ';
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $dom;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsdependencieswpRemoveSurroundingEmptyScriptTagsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/dependencies/wpRemoveSurroundingEmptyScriptTags.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dependencies/wpRemoveSurroundingEmptyScriptTags.php (rev 0)
+++ trunk/tests/phpunit/tests/dependencies/wpRemoveSurroundingEmptyScriptTags.php 2023-09-29 19:45:53 UTC (rev 56748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,78 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test wp_remove_surrounding_empty_script_tags().
+ *
+ * @group dependencies
+ * @group scripts
+ * @ticket 58664
+ * @covers ::wp_remove_surrounding_empty_script_tags
+ */
+class Tests_Functions_wpRemoveSurroundingEmptyScriptTags extends WP_UnitTestCase {
+
+ /**
+ * Data provider for test.
+ *
+ * @return array
+ */
+ public function get_data_to_test_wp_remove_surrounding_empty_script_tags() {
+ $error_js = 'console.error("Function wp_remove_surrounding_empty_script_tags() used incorrectly in PHP. Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.")';
+ return array(
+ 'basic_case' => array(
+ '<script>alert("hello")</script>',
+ 'alert("hello")',
+ false,
+ ),
+ 'BASIC_CASE' => array(
+ '<SCRIPT>alert("hello")</SCRIPT>',
+ 'alert("hello")',
+ false,
+ ),
+ 'whitespace_basic_case' => array(
+ ' <script>alert("hello")</script> ',
+ 'alert("hello")',
+ false,
+ ),
+ 'missing_tags' => array(
+ 'alert("hello")',
+ $error_js,
+ true,
+ ),
+ 'missing_start_tag' => array(
+ 'alert("hello")</script>',
+ $error_js,
+ true,
+ ),
+ 'missing_end_tag' => array(
+ '<script>alert("hello")',
+ $error_js,
+ true,
+ ),
+ 'erroneous attributes' => array(
+ '<script type="text/javascript">alert("hello")</script>',
+ $error_js,
+ true,
+ ),
+ );
+ }
+
+ /**
+ * Test scenarios for wp_remove_surrounding_empty_script_tags().
+ *
+ * @dataProvider get_data_to_test_wp_remove_surrounding_empty_script_tags
+ *
+ * @param string $input Input.
+ * @param string $expected Expected.
+ * @param bool $expect_doing_it_wrong Whether input is _doing_it_wrong().
+ */
+ public function test_wp_remove_surrounding_empty_script_tags( $input, $expected, $expect_doing_it_wrong ) {
+ if ( $expect_doing_it_wrong ) {
+ $this->setExpectedIncorrectUsage( 'wp_remove_surrounding_empty_script_tags' );
+ }
+
+ $this->assertSame(
+ $expected,
+ wp_remove_surrounding_empty_script_tags( $input )
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/dependencies/wpRemoveSurroundingEmptyScriptTags.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>