From 0e4a0ea9af195ca1390848fc32258bd7ebb1940c Mon Sep 17 00:00:00 2001 From: Igor Rozum Date: Wed, 18 Nov 2026 11:39:16 -0500 Subject: [PATCH] Quick/Bulk Edit: Make the cloned form field IDs unique on the posts list screen --- src/js/_enqueues/admin/inline-edit-post.js | 61 +++++++++++++++++++--- tests/e2e/specs/edit-posts.test.js | 46 ++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js index aaf4bcc5bc3ec..64db6a8a79faa 100644 --- a/src/js/_enqueues/admin/inline-edit-post.js +++ b/src/js/_enqueues/admin/inline-edit-post.js @@ -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 = { /** @@ -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(); @@ -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(''); // Populate fields in the quick edit window. diff --git a/tests/e2e/specs/edit-posts.test.js b/tests/e2e/specs/edit-posts.test.js index 282f0b1de26a4..d9ef96172267f 100644 --- a/tests/e2e/specs/edit-posts.test.js +++ b/tests/e2e/specs/edit-posts.test.js @@ -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, @@ -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,