Skip to content

Commit

Permalink
Footnotes: only replace attribute if footnotes were detected (#63935)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: sgomes <sergiomdgomes@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
4 people authored Aug 30, 2024
1 parent 90e6d26 commit 5b21c10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/core-data/src/footnotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export function updateFootnotesFromMeta( blocks, meta ) {
? RichTextData.fromHTMLString( value )
: new RichTextData( value );

let hasFootnotes = false;

richTextValue.replacements.forEach( ( replacement ) => {
if ( replacement.type === 'core/footnote' ) {
const id = replacement.attributes[ 'data-fn' ];
Expand All @@ -92,13 +94,16 @@ export function updateFootnotesFromMeta( blocks, meta ) {
replacement.innerHTML = toHTMLString( {
value: countValue,
} );
hasFootnotes = true;
}
} );

attributes[ key ] =
typeof value === 'string'
? richTextValue.toHTMLString()
: richTextValue;
if ( hasFootnotes ) {
attributes[ key ] =
typeof value === 'string'
? richTextValue.toHTMLString()
: richTextValue;
}
}

return attributes;
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/specs/editor/various/footnotes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,36 @@ test.describe( 'Footnotes', () => {
'1 ↩︎'
);
} );

test( 'should leave alone other block attributes', async ( {
editor,
page,
} ) => {
await page.evaluate( () => {
window.wp.blocks.registerBlockType( 'core/test-block-string', {
apiVersion: 3,
title: 'Block with string attribute',
attributes: { string: { type: 'string' } },
edit: () => null,
save: () => null,
} );
} );
await editor.insertBlock( {
name: 'core/test-block-string',
attributes: { string: 'a\nb' },
} );

await editor.insertBlock( { name: 'core/paragraph' } );
await page.keyboard.type( 'a' );
await editor.showBlockToolbar();
await editor.clickBlockToolbarButton( 'More' );
await page.locator( 'button:text("Footnote")' ).click();
await page.keyboard.type( '1' );

expect( ( await editor.getBlocks() )[ 0 ] ).toMatchObject( {
name: 'core/test-block-string',
// This should NOT be 'a<br>b'!
attributes: { string: 'a\nb' },
} );
} );
} );

0 comments on commit 5b21c10

Please sign in to comment.