Quick/Bulk Edit: Fix undefined error when initializing UI Autocomplete 1.12.1 on non-existing element and then attempting to use the autocomplete instance.

Example: `jQuery( '#nonexisting' ).autocomplete().autocomplete( 'instance' ).something`.

Props _luigi, sabernhardt, donmhico, azaozz.
Fixes #51872.

git-svn-id: https://develop.svn.wordpress.org/trunk@49703 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2020-11-28 12:44:44 +00:00
parent f4525a2e93
commit b2d59e7c8d
2 changed files with 22 additions and 7 deletions

View File

@ -329,6 +329,11 @@ window.wp = window.wp || {};
textarea = $('textarea.tax_input_' + taxname, editRow),
comma = wp.i18n._x( ',', 'tag delimiter' ).trim();
// Ensure the textarea exists.
if ( ! textarea.length ) {
return;
}
terms.find( 'img' ).replaceWith( function() { return this.alt; } );
terms = terms.text();

View File

@ -38,6 +38,11 @@
var last;
var $element = $( this );
// Do not initialize if the element doesn't exist.
if ( ! $element.length ) {
return;
}
options = options || {};
var taxonomy = options.taxonomy || $element.attr( 'data-wp-taxonomy' ) || 'post_tag';
@ -146,13 +151,18 @@
$element.on( 'keydown', function() {
$element.removeAttr( 'aria-activedescendant' );
} )
.autocomplete( options )
.autocomplete( 'instance' )._renderItem = function( ul, item ) {
return $( '<li role="option" id="wp-tags-autocomplete-' + item.id + '">' )
.text( item.name )
.appendTo( ul );
};
} );
$element.autocomplete( options );
// Ensure the autocomplete instance exists.
if ( $element.autocomplete( 'instance' ) ) {
$element.autocomplete( 'instance' )._renderItem = function( ul, item ) {
return $( '<li role="option" id="wp-tags-autocomplete-' + item.id + '">' )
.text( item.name )
.appendTo( ul );
};
}
$element.attr( {
'role': 'combobox',