Skip to content

Commit

Permalink
Allow ID attributes on elements that belong to an anchor block (#8124)
Browse files Browse the repository at this point in the history
Fixes #7335.
  • Loading branch information
pento authored Jul 25, 2018
1 parent 0fec0f3 commit 7e223fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/blocks/src/api/raw-handling/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe( 'removeInvalidHTML', () => {
expect( removeInvalidHTML( input, schema ) ).toBe( output );
} );

it( 'should remove id attributes', () => {
const input = '<p id="foo">test</p>';
const output = '<p>test</p>';
expect( removeInvalidHTML( input, schema ) ).toBe( output );
} );

it( 'should remove multiple attributes', () => {
const input = '<p class="test" id="test">test</p>';
const output = '<p>test</p>';
Expand Down
14 changes: 13 additions & 1 deletion packages/blocks/src/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { omit, mergeWith, includes, noop } from 'lodash';
* WordPress dependencies
*/
import { unwrap, insertAfter, remove } from '@wordpress/dom';
import { hasBlockSupport } from '..';

/**
* Browser dependencies
Expand Down Expand Up @@ -67,7 +68,18 @@ export function isPhrasingContent( node ) {
* @return {Object} A complete block content schema.
*/
export function getBlockContentSchema( transforms ) {
const schemas = transforms.map( ( { schema } ) => schema );
const schemas = transforms.map( ( { blockName, schema } ) => {
// If the block supports the "anchor" functionality, it needs to keep its ID attribute.
if ( hasBlockSupport( blockName, 'anchor' ) ) {
for ( const tag in schema ) {
if ( ! schema[ tag ].attributes ) {
schema[ tag ].attributes = [];
}
schema[ tag ].attributes.push( 'id' );
}
}
return schema;
} );

return mergeWith( {}, ...schemas, ( objValue, srcValue, key ) => {
if ( key === 'children' ) {
Expand Down

0 comments on commit 7e223fc

Please sign in to comment.