Skip to content
Open
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
61 changes: 55 additions & 6 deletions src/js/_enqueues/admin/inline-edit-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,48 @@ window.wp = window.wp || {};
*/
( function( $, wp ) {

/**
* Prefixes the "id" attributes of cloned elements and updates the references
* to them within the clone.
*
* @since 7.2.0
*
* @param {jQuery} $container Cloned markup to update references in.
* @param {jQuery} $elements Elements within the container to rename.
* @param {string} prefix Prefix to add to each ID.
*/
function prefixClonedIds( $container, $elements, prefix ) {
/**
* Replaces one ID in a space-separated list of IDs.
*
* @param {string} value Space-separated IDs.
* @param {string} oldId ID to replace.
* @param {string} newId Replacement ID.
*
* @return {string} The updated list.
*/
function replaceToken( value, oldId, newId ) {
return value.split( /\s+/ ).map( function( token ) {
return token === oldId ? newId : token;
} ).join( ' ' );
}

$elements.each( function() {
var oldId = this.id,
newId = prefix + oldId;

this.id = newId;

$container.find( '[for="' + oldId + '"]' ).attr( 'for', newId );
$container.find( '[aria-describedby~="' + oldId + '"]' ).attr( 'aria-describedby', function( i, value ) {
return replaceToken( value, oldId, newId );
} );
$container.find( '[aria-labelledby~="' + oldId + '"]' ).attr( 'aria-labelledby', function( i, value ) {
return replaceToken( value, oldId, newId );
} );
} );
}

window.inlineEditPost = {

/**
Expand Down Expand Up @@ -131,18 +173,18 @@ window.wp = window.wp || {};
inlineEditPost.edit( this );
});

// Clone quick edit categories for the bulk editor.
var beCategories = $( '#inline-edit fieldset.inline-edit-categories' ).clone();
// Clone quick edit categories and tags for the bulk editor.
var beCategories = $( '#inline-edit fieldset.inline-edit-categories' ).clone(),
beTags = $( '#inline-edit .inline-edit-tags-wrap' ).clone();

// Make "id" attributes globally unique.
beCategories.find( '*[id]' ).each( function() {
this.id = 'bulk-edit-' + this.id;
});
prefixClonedIds( beCategories, beCategories.find( '[id]' ), 'bulk-edit-' );
prefixClonedIds( beTags, beTags.find( '[id]' ), 'bulk-edit-' );

$('#bulk-edit').find('fieldset:first').after(
beCategories
).siblings( 'fieldset:last' ).prepend(
$( '#inline-edit .inline-edit-tags-wrap' ).clone()
beTags
);

$('select[name="_status"] option[value="future"]', bulkRow).remove();
Expand Down Expand Up @@ -357,6 +399,13 @@ window.wp = window.wp || {};
$( 'td', editRow ).find('#quick-edit-legend').removeAttr('id');
$( 'td', editRow ).find('p[id^="quick-edit-"]').removeAttr('id');

// Make the IDs of core fields unique; the hidden template keeps the originals.
prefixClonedIds(
editRow,
editRow.find( 'input[name="ss"][id], input[name="_inline_edit"][id], select[name="post_parent"][id], .inline-edit-categories [id], .inline-edit-tags-wrap [id]' ),
'quick-edit-'
);

$(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');

// Populate fields in the quick edit window.
Expand Down
46 changes: 46 additions & 0 deletions tests/e2e/specs/edit-posts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ test.describe( 'Edit Posts', () => {
);
}

async function getDuplicateIds( page ) {
return page.evaluate( () => {
const counts = {};
document.querySelectorAll( '[id]' ).forEach( ( { id } ) => {
counts[ id ] = ( counts[ id ] || 0 ) + 1;
} );
return Object.keys( counts )
.filter( ( id ) => counts[ id ] > 1 )
.sort();
} );
}

test( 'displays a message in the posts table when a search finds no posts',async ( {
admin,
page,
Expand Down Expand Up @@ -140,6 +152,40 @@ test.describe( 'Edit Posts', () => {
).toBeVisible();
} );

test( 'does not render duplicate HTML IDs in the Quick Edit and Bulk Edit forms', async ( {
admin,
page,
postFactory,
} ) => {
const title = postFactory.uniqueTitle( 'Edit Posts duplicate IDs' );
const post = await postFactory.create( { title, status: 'publish' } );
await visitPostList( admin, title );

const listTable = page.getByRole( 'table', { name: 'Table ordered by' } );
const row = postRow( listTable, post.id );
const tagsDescription = 'Separate tags with commas';

expect( await getDuplicateIds( page ) ).toEqual( [] );

await row.getByRole( 'link', { name: title, exact: true } ).hover();
await row.getByRole( 'button', { name: 'Quick Edit' } ).click();
expect( await getDuplicateIds( page ) ).toEqual( [] );
await expect(
page.locator( `#edit-${ post.id } textarea[name="tax_input[post_tag]"]` )
).toHaveAccessibleDescription( tagsDescription );

await page.getByRole( 'button', { name: 'Cancel' } ).click();
await row.getByRole( 'checkbox' ).check();
await bulkActionSelect( page ).selectOption( 'edit' );
await bulkActionApply( page ).click();
expect( await getDuplicateIds( page ) ).toEqual( [] );
await expect(
page
.getByRole( 'region', { name: 'Bulk Edit', exact: true } )
.locator( 'textarea[name="tax_input[post_tag]"]' )
).toHaveAccessibleDescription( tagsDescription );
} );

test( 'allows an existing post to be deleted using the Trash button', async ( {
admin,
page,
Expand Down
Loading