diff --git a/src/js/_enqueues/lib/zxcvbn-async.js b/src/js/_enqueues/lib/zxcvbn-async.js index 114cb5a0d698e..d974f775acc29 100644 --- a/src/js/_enqueues/lib/zxcvbn-async.js +++ b/src/js/_enqueues/lib/zxcvbn-async.js @@ -9,22 +9,26 @@ * script tag on the page. * * This makes sure zxcvbn isn't blocking loading the page as it is a big - * library. The source for zxcvbn is read from the _zxcvbnSettings global. + * library. The source for zxcvbn is read from the `_zxcvbnSettings` global. */ -(function() { - var async_load = function() { - var first, s; - s = document.createElement('script'); - s.src = _zxcvbnSettings.src; - s.type = 'text/javascript'; - s.async = true; - first = document.getElementsByTagName('script')[0]; - return first.parentNode.insertBefore(s, first); - }; +( () => { + const asyncLoad = () => { + if ( typeof _zxcvbnSettings === 'undefined' || ! _zxcvbnSettings.src ) { + return; + } - if (window.attachEvent != null) { - window.attachEvent('onload', async_load); - } else { - window.addEventListener('load', async_load, false); - } -}).call(this); + const script = document.createElement( 'script' ); + script.src = _zxcvbnSettings.src; + script.type = 'text/javascript'; + script.async = true; + + const first = document.getElementsByTagName( 'script' )[ 0 ]; + if ( ! first || ! first.parentNode ) { + return; + } + + first.parentNode.insertBefore( script, first ); + }; + + window.addEventListener( 'load', asyncLoad ); +} )(); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index b37f2d5717e0e..ef9b05b0a43a5 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1197,7 +1197,7 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'wp-playlist', "/wp-includes/js/mediaelement/wp-playlist$suffix.js", array( 'wp-util', 'backbone', 'mediaelement' ), false, 1 ); - $scripts->add( 'zxcvbn-async', "/wp-includes/js/zxcvbn-async$suffix.js", array(), '1.0' ); + $scripts->add( 'zxcvbn-async', "/wp-includes/js/zxcvbn-async$suffix.js" ); did_action( 'init' ) && $scripts->localize( 'zxcvbn-async', '_zxcvbnSettings', diff --git a/tsconfig.json b/tsconfig.json index 2a72ff4c550c2..710e3ddbb1d16 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -41,6 +41,7 @@ "src/js/_enqueues/lib/codemirror/javascript-lint.js", "src/js/_enqueues/lib/emoji-loader.js", "src/js/_enqueues/lib/language-chooser.js", + "src/js/_enqueues/lib/zxcvbn-async.js", "src/js/_enqueues/wp/code-editor.js", "src/js/_enqueues/wp/emoji.js", "src/js/_enqueues/wp/sanitize.js", diff --git a/typings/browser-globals/index.d.ts b/typings/browser-globals/index.d.ts index d79c4e8499f8b..271d212f99de6 100644 --- a/typings/browser-globals/index.d.ts +++ b/typings/browser-globals/index.d.ts @@ -12,3 +12,9 @@ * two conflict. Code which may run in either context therefore has to declare it. */ declare var WorkerGlobalScope: Function | undefined; + +/** + * Localized onto `zxcvbn-async` by `WP_Scripts::localize()`. + * Present only when that script is enqueued. + */ +declare var _zxcvbnSettings: { src: string } | undefined;