From b171b086bd5fe7f7644393d85cc6eb36af4a4564 Mon Sep 17 00:00:00 2001 From: Enrico Carraro Date: Tue, 13 Oct 2020 17:56:09 +0200 Subject: [PATCH 1/4] wp_get_script_tag() and wp_get_inline_script_tag() --- src/wp-includes/functions.php | 109 ++++++++++++ .../tests/functions/wpInlineScriptTag.php | 119 +++++++++++++ .../functions/wpSanitizeScriptAttributes.php | 157 ++++++++++++++++++ tests/phpunit/tests/functions/wpScriptTag.php | 94 +++++++++++ 4 files changed, 479 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpInlineScriptTag.php create mode 100644 tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php create mode 100644 tests/phpunit/tests/functions/wpScriptTag.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 537425d8981ab..87a5502d44d7b 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7773,3 +7773,112 @@ function is_php_version_compatible( $required ) { function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { return abs( (float) $expected - (float) $actual ) <= $precision; } + +/** + * Sanitizes an attributes array into an attributes string to be placed inside a `\n", wp_sanitize_script_attributes( $attributes ) ); +} + +/** + * Prints formatted `\n", wp_sanitize_script_attributes( $attributes ), $javascript ); +} + +/** + * Prints inline JavaScript wrapped in `\n", + wp_get_inline_script_tag( + $this->event_handler, + array( + 'type' => 'application/javascript', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + '\n", + wp_get_inline_script_tag( + $this->event_handler, + array( + 'type' => 'application/javascript', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + } + + function test_get_inline_script_tag_type_not_set() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + "\n", + wp_get_inline_script_tag( + $this->event_handler, + array( + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + } + + function test_get_inline_script_tag_unescaped_src() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + "\n", + wp_get_inline_script_tag( $this->event_handler ) + ); + + remove_theme_support( 'html5' ); + } + + function test_print_script_tag_prints_get_inline_script_tag() { + add_filter( + 'wp_script_attributes', + function ( $attributes ) { + if ( isset( $attributes['id'] ) && 'utils-js-extra' === $attributes['id'] ) { + $attributes['async'] = true; + } + return $attributes; + } + ); + + add_theme_support( 'html5', array( 'script' ) ); + + $attributes = array( + 'id' => 'utils-js-before', + 'nomodule' => true, + ); + + $this->assertSame( + wp_get_inline_script_tag( $this->event_handler, $attributes ), + get_echo( + 'wp_print_inline_script_tag', + array( + $this->event_handler, + $attributes, + ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + wp_get_inline_script_tag( $this->event_handler, $attributes ), + get_echo( + 'wp_print_inline_script_tag', + array( + $this->event_handler, + $attributes, + ) + ) + ); + } +} diff --git a/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php b/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php new file mode 100644 index 0000000000000..d6048f15b941c --- /dev/null +++ b/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php @@ -0,0 +1,157 @@ +assertSame( + ' type="application/javascript" src="https://DOMAIN.TLD/PATH/FILE.js" nomodule', + wp_sanitize_script_attributes( + array( + 'type' => 'application/javascript', + 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + ' src="https://DOMAIN.TLD/PATH/FILE.js" type="application/javascript" nomodule="nomodule"', + wp_sanitize_script_attributes( + array( + 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', + 'type' => 'application/javascript', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + } + + function test_sanitize_script_attributes_type_not_set() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + ' src="https://DOMAIN.TLD/PATH/FILE.js" nomodule', + wp_sanitize_script_attributes( + array( + 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + ' src="https://DOMAIN.TLD/PATH/FILE.js" nomodule="nomodule" type="text/javascript"', + wp_sanitize_script_attributes( + array( + 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + } + + + function test_sanitize_script_attributes_no_attributes() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + '', + wp_sanitize_script_attributes() + ); + + remove_theme_support( 'html5' ); + } + + function test_sanitize_script_attributes_relative_src() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + ' src="PATH/FILE.js" nomodule', + wp_sanitize_script_attributes( + array( + 'src' => 'PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + } + + + function test_sanitize_script_attributes_only_false_boolean_attributes() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + '', + wp_sanitize_script_attributes( + array( + 'async' => false, + 'nomodule' => false, + ) + ) + ); + + remove_theme_support( 'html5' ); + } + + function test_sanitize_script_attributes_only_true_boolean_attributes() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + ' async nomodule', + wp_sanitize_script_attributes( + array( + 'async' => true, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + } + + function test_sanitize_script_attributes_wp_script_attributes_filter() { + add_theme_support( 'html5', array( 'script' ) ); + + add_filter( + 'wp_script_attributes', + function( $attributes ) { + if ( isset( $attributes['id'] ) && 'utils-js-extra' === $attributes['id'] ) { + $attributes['async'] = true; + } + return $attributes; + } + ); + + $this->assertSame( + ' src="https://DOMAIN.TLD/PATH/FILE.js" id="utils-js-extra" async nomodule', + wp_sanitize_script_attributes( + array( + 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', + 'id' => 'utils-js-extra', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + } +} diff --git a/tests/phpunit/tests/functions/wpScriptTag.php b/tests/phpunit/tests/functions/wpScriptTag.php new file mode 100644 index 0000000000000..3c451f5359cad --- /dev/null +++ b/tests/phpunit/tests/functions/wpScriptTag.php @@ -0,0 +1,94 @@ +assertSame( + '' . "\n", + wp_get_script_tag( + array( + 'type' => 'application/javascript', + 'src' => 'https://localhost/PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + '' . "\n", + wp_get_script_tag( + array( + 'src' => 'https://localhost/PATH/FILE.js', + 'type' => 'application/javascript', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + } + + function test_get_script_tag_type_not_set() { + add_theme_support( 'html5', array( 'script' ) ); + + $this->assertSame( + '' . "\n", + wp_get_script_tag( + array( + 'src' => 'https://localhost/PATH/FILE.js', + 'async' => false, + 'nomodule' => true, + ) + ) + ); + + remove_theme_support( 'html5' ); + } + + function test_print_script_tag_prints_get_script_tag() { + add_filter( + 'wp_script_attributes', + function ( $attributes ) { + if ( isset( $attributes['id'] ) && 'utils-js-extra' === $attributes['id'] ) { + $attributes['async'] = true; + } + return $attributes; + } + ); + + add_theme_support( 'html5', array( 'script' ) ); + + $attributes = array( + 'src' => 'https://localhost/PATH/FILE.js', + 'id' => 'utils-js-extra', + 'nomodule' => true, + ); + + $this->assertSame( + wp_get_script_tag( $attributes ), + get_echo( + 'wp_print_script_tag', + array( $attributes ) + ) + ); + + remove_theme_support( 'html5' ); + + $this->assertSame( + wp_get_script_tag( $attributes ), + get_echo( + 'wp_print_script_tag', + array( $attributes ) + ) + ); + } +} From 9f0fccd2833bfc07c9de03a388e47e867456e224 Mon Sep 17 00:00:00 2001 From: Enrico Carraro Date: Fri, 23 Oct 2020 14:12:07 +0200 Subject: [PATCH 2/4] Move 'wp_script_attributes' filter out of wp_sanitize_script_attributes(). --- src/wp-includes/functions.php | 42 ++++++++++++------- .../functions/wpSanitizeScriptAttributes.php | 31 +------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 87a5502d44d7b..5ac8ec2147181 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7786,20 +7786,6 @@ function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { * @return string String made of sanitized `\n", wp_sanitize_script_attributes( $attributes ) ); } @@ -7862,6 +7862,20 @@ function wp_print_script_tag( $attributes ) { * @return string String containing inline JavaScript code wrapped around `\n", wp_sanitize_script_attributes( $attributes ), $javascript ); diff --git a/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php b/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php index d6048f15b941c..dd060ebd5daf4 100644 --- a/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php +++ b/tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php @@ -54,7 +54,7 @@ function test_sanitize_script_attributes_type_not_set() { remove_theme_support( 'html5' ); $this->assertSame( - ' src="https://DOMAIN.TLD/PATH/FILE.js" nomodule="nomodule" type="text/javascript"', + ' src="https://DOMAIN.TLD/PATH/FILE.js" nomodule="nomodule"', wp_sanitize_script_attributes( array( 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', @@ -71,7 +71,7 @@ function test_sanitize_script_attributes_no_attributes() { $this->assertSame( '', - wp_sanitize_script_attributes() + wp_sanitize_script_attributes( array() ) ); remove_theme_support( 'html5' ); @@ -127,31 +127,4 @@ function test_sanitize_script_attributes_only_true_boolean_attributes() { remove_theme_support( 'html5' ); } - function test_sanitize_script_attributes_wp_script_attributes_filter() { - add_theme_support( 'html5', array( 'script' ) ); - - add_filter( - 'wp_script_attributes', - function( $attributes ) { - if ( isset( $attributes['id'] ) && 'utils-js-extra' === $attributes['id'] ) { - $attributes['async'] = true; - } - return $attributes; - } - ); - - $this->assertSame( - ' src="https://DOMAIN.TLD/PATH/FILE.js" id="utils-js-extra" async nomodule', - wp_sanitize_script_attributes( - array( - 'src' => 'https://DOMAIN.TLD/PATH/FILE.js', - 'id' => 'utils-js-extra', - 'async' => false, - 'nomodule' => true, - ) - ) - ); - - remove_theme_support( 'html5' ); - } } From 7b36c46834abb2debfabc2f5fc6fa83accf6a38f Mon Sep 17 00:00:00 2001 From: Enrico Carraro Date: Fri, 23 Oct 2020 14:17:15 +0200 Subject: [PATCH 3/4] Restructure ifs in wp_sanitize_script_attributes(). --- src/wp-includes/functions.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 5ac8ec2147181..eadcc61411aec 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7785,23 +7785,22 @@ function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { * @param array $attributes Optional. Key-value pairs representing `