Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/js/_enqueues/lib/zxcvbn-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} )();
2 changes: 1 addition & 1 deletion src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions typings/browser-globals/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading