Skip to content

Commit 1bd29b1

Browse files
committed
Scripts: Remove non-HTML5 script support.
Remove the following behaviors that are obsolete in HTML5: - CDATA wrappers around `SCRIPT` tag contents. - Conversion of boolean attributes to strings (attribute `async="async"` becomes `async`). HTML5 was released in 2008 and data suggests virtually all WordPress sites are served as HTML5. See #59883 for more details. Developed in #10660. Props jonsurrell, westonruter, azaozz, soyebsalar01, dmsnell. Fixes #64442. See #59883. git-svn-id: https://develop.svn.wordpress.org/trunk@61415 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7336bbe commit 1bd29b1

File tree

4 files changed

+62
-418
lines changed

4 files changed

+62
-418
lines changed

src/wp-includes/script-loader.php

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,10 +2210,8 @@ function _print_scripts() {
22102210
if ( $concat ) {
22112211
if ( ! empty( $wp_scripts->print_code ) ) {
22122212
echo "\n<script>\n";
2213-
echo "/* <![CDATA[ */\n"; // Not needed in HTML 5.
22142213
echo $wp_scripts->print_code;
22152214
echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) );
2216-
echo "/* ]]> */\n";
22172215
echo "</script>\n";
22182216
}
22192217

@@ -2871,8 +2869,7 @@ function wp_enqueue_editor_format_library_assets() {
28712869
* @return string String made of sanitized `<script>` tag attributes.
28722870
*/
28732871
function wp_sanitize_script_attributes( $attributes ) {
2874-
$html5_script_support = is_admin() || current_theme_supports( 'html5', 'script' );
2875-
$attributes_string = '';
2872+
$attributes_string = '';
28762873

28772874
/*
28782875
* If HTML5 script tag is supported, only the attribute name is added
@@ -2881,7 +2878,7 @@ function wp_sanitize_script_attributes( $attributes ) {
28812878
foreach ( $attributes as $attribute_name => $attribute_value ) {
28822879
if ( is_bool( $attribute_value ) ) {
28832880
if ( $attribute_value ) {
2884-
$attributes_string .= $html5_script_support ? ' ' . esc_attr( $attribute_name ) : sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_name ) );
2881+
$attributes_string .= ' ' . esc_attr( $attribute_name );
28852882
}
28862883
} else {
28872884
$attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
@@ -2944,56 +2941,6 @@ function wp_print_script_tag( $attributes ) {
29442941
* @return string String containing inline JavaScript code wrapped around `<script>` tag.
29452942
*/
29462943
function wp_get_inline_script_tag( $data, $attributes = array() ) {
2947-
/*
2948-
* XHTML extracts the contents of the SCRIPT element and then the XML parser
2949-
* decodes character references and other syntax elements. This can lead to
2950-
* misinterpretation of the script contents or invalid XHTML documents.
2951-
*
2952-
* Wrapping the contents in a CDATA section instructs the XML parser not to
2953-
* transform the contents of the SCRIPT element before passing them to the
2954-
* JavaScript engine.
2955-
*
2956-
* Example:
2957-
*
2958-
* <script>console.log('&hellip;');</script>
2959-
*
2960-
* In an HTML document this would print "&hellip;" to the console,
2961-
* but in an XHTML document it would print "…" to the console.
2962-
*
2963-
* <script>console.log('An image is <img> in HTML');</script>
2964-
*
2965-
* In an HTML document this would print "An image is <img> in HTML",
2966-
* but it's an invalid XHTML document because it interprets the `<img>`
2967-
* as an empty tag missing its closing `/`.
2968-
*
2969-
* @see https://www.w3.org/TR/xhtml1/#h-4.8
2970-
*/
2971-
if (
2972-
( ! current_theme_supports( 'html5', 'script' ) && ! is_admin() )
2973-
&& (
2974-
! isset( $attributes['type'] ) ||
2975-
'module' === $attributes['type'] ||
2976-
str_contains( $attributes['type'], 'javascript' ) ||
2977-
str_contains( $attributes['type'], 'ecmascript' ) ||
2978-
str_contains( $attributes['type'], 'jscript' ) ||
2979-
str_contains( $attributes['type'], 'livescript' )
2980-
)
2981-
) {
2982-
/*
2983-
* If the string `]]>` exists within the JavaScript it would break
2984-
* out of any wrapping CDATA section added here, so to start, it's
2985-
* necessary to escape that sequence which requires splitting the
2986-
* content into two CDATA sections wherever it's found.
2987-
*
2988-
* Note: it's only necessary to escape the closing `]]>` because
2989-
* an additional `<![CDATA[` leaves the contents unchanged.
2990-
*/
2991-
$data = str_replace( ']]>', ']]]]><![CDATA[>', $data );
2992-
2993-
// Wrap the entire escaped script inside a CDATA section.
2994-
$data = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $data );
2995-
}
2996-
29972944
$data = "\n" . trim( $data, "\n\r " ) . "\n";
29982945

29992946
/**

0 commit comments

Comments
 (0)