From 7e223fcd8c72411921bf6b25ef07663057ea8d2f Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 25 Jul 2018 14:53:20 +1000 Subject: [PATCH] Allow ID attributes on elements that belong to an anchor block (#8124) Fixes #7335. --- packages/blocks/src/api/raw-handling/test/utils.js | 6 ++++++ packages/blocks/src/api/raw-handling/utils.js | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/blocks/src/api/raw-handling/test/utils.js b/packages/blocks/src/api/raw-handling/test/utils.js index b8c2d2f46e673..3cbdc68a5e268 100644 --- a/packages/blocks/src/api/raw-handling/test/utils.js +++ b/packages/blocks/src/api/raw-handling/test/utils.js @@ -109,6 +109,12 @@ describe( 'removeInvalidHTML', () => { expect( removeInvalidHTML( input, schema ) ).toBe( output ); } ); + it( 'should remove id attributes', () => { + const input = '

test

'; + const output = '

test

'; + expect( removeInvalidHTML( input, schema ) ).toBe( output ); + } ); + it( 'should remove multiple attributes', () => { const input = '

test

'; const output = '

test

'; diff --git a/packages/blocks/src/api/raw-handling/utils.js b/packages/blocks/src/api/raw-handling/utils.js index 5b233201eb98e..3aa3e438c2e51 100644 --- a/packages/blocks/src/api/raw-handling/utils.js +++ b/packages/blocks/src/api/raw-handling/utils.js @@ -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 @@ -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' ) {