-
Notifications
You must be signed in to change notification settings - Fork 3.3k
wp_get_script_tag() and wp_get_inline_script_tag() #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b171b08
wp_get_script_tag() and wp_get_inline_script_tag()
enricocarraro 9f0fccd
Move 'wp_script_attributes' filter out of wp_sanitize_script_attribut…
enricocarraro 7b36c46
Restructure ifs in wp_sanitize_script_attributes().
enricocarraro 6762f5b
Docs: correct DocBlocks for wp_get_script_tag and related functions.
enricocarraro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Test wp_get_inline_script_tag() and wp_print_inline_script_tag(). | ||
| * | ||
| * @group functions.php | ||
| */ | ||
| class Tests_Functions_wpInlineScriptTag extends WP_UnitTestCase { | ||
|
|
||
| private $event_handler = <<<'JS' | ||
| document.addEventListener( 'DOMContentLoaded', function () { | ||
| document.getElementById( 'elementID' ) | ||
| .addEventListener( 'click', function( event ) { | ||
| event.preventDefault(); | ||
| }); | ||
| }); | ||
| JS; | ||
|
|
||
| function get_inline_script_tag_type_set() { | ||
| add_theme_support( 'html5', array( 'script' ) ); | ||
|
|
||
| $this->assertSame( | ||
| '<script type="application/javascript" nomodule>' . "\n{$this->event_handler}\n</script>\n", | ||
| wp_get_inline_script_tag( | ||
| $this->event_handler, | ||
| array( | ||
| 'type' => 'application/javascript', | ||
| 'async' => false, | ||
| 'nomodule' => true, | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| remove_theme_support( 'html5' ); | ||
|
|
||
| $this->assertSame( | ||
| '<script type="application/javascript" nomodule>' . "\n{$this->event_handler}\n</script>\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( | ||
| "<script nomodule>\n{$this->event_handler}\n</script>\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( | ||
| "<script>\n{$this->event_handler}\n</script>\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, | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
tests/phpunit/tests/functions/wpSanitizeScriptAttributes.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Test wp_sanitize_script_attributes(). | ||
| * | ||
| * @group functions.php | ||
| */ | ||
| class Tests_Functions_wpSanitizeScriptAttributes extends WP_UnitTestCase { | ||
|
|
||
| function test_sanitize_script_attributes_type_set() { | ||
| add_theme_support( 'html5', array( 'script' ) ); | ||
|
|
||
| $this->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"', | ||
| 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( array() ) | ||
| ); | ||
|
|
||
| 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' ); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.