Skip to content

Commit

Permalink
Fix empty heading anchor id leaving an empty id attribute on the head…
Browse files Browse the repository at this point in the history
…ing html tag (WordPress#11663)

This commit checks that the anchor id attribute is not an empty string before saving it as additional props. Also adds a test to cover this behaviour.
  • Loading branch information
andrewserong committed Nov 15, 2018
1 parent 4a54cc1 commit 8f9055b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) =>
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockSupport( blockType, 'anchor' ) ) {
if ( hasBlockSupport( blockType, 'anchor' ) && attributes.anchor !== '' ) {
extraProps.id = attributes.anchor;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/editor/src/hooks/test/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,17 @@ describe( 'anchor', () => {

expect( extraProps.id ).toBe( 'foo' );
} );

it( 'should have no id property if anchor attribute id is an empty string', () => {
const attributes = { anchor: '' };
const extraProps = getSaveContentExtraProps( {}, {
...blockSettings,
supports: {
anchor: true,
},
}, attributes );

expect( extraProps ).not.toHaveProperty( 'id' );
} );
} );
} );

0 comments on commit 8f9055b

Please sign in to comment.