From b487f99fc3680f10df6874d913636612bc771e9d Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 23 Jul 2018 16:19:37 +1000 Subject: [PATCH 1/2] Allow ID attributes on any elements. --- packages/blocks/src/api/raw-handling/test/utils.js | 10 ++++++++-- packages/blocks/src/api/raw-handling/utils.js | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/blocks/src/api/raw-handling/test/utils.js b/packages/blocks/src/api/raw-handling/test/utils.js index 2d83a9a25e4884..e7342a12fe30d3 100644 --- a/packages/blocks/src/api/raw-handling/test/utils.js +++ b/packages/blocks/src/api/raw-handling/test/utils.js @@ -109,14 +109,20 @@ describe( 'removeInvalidHTML', () => { expect( removeInvalidHTML( input, schema ) ).toBe( output ); } ); + it( 'should keep id attributes', () => { + const input = '

test

'; + const output = '

test

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

test

'; + const input = '

test

'; const output = '

test

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

test test

'; + const input = '

test test

'; const output = '

test test

'; expect( removeInvalidHTML( input, schema ) ).toBe( output ); } ); diff --git a/packages/blocks/src/api/raw-handling/utils.js b/packages/blocks/src/api/raw-handling/utils.js index 9e7f093f3691a1..158b8fd9064a91 100644 --- a/packages/blocks/src/api/raw-handling/utils.js +++ b/packages/blocks/src/api/raw-handling/utils.js @@ -197,7 +197,7 @@ function cleanNodeList( nodeList, doc, schema, inline ) { if ( node.hasAttributes() ) { // Strip invalid attributes. Array.from( node.attributes ).forEach( ( { name } ) => { - if ( name !== 'class' && ! includes( attributes, name ) ) { + if ( name !== 'class' && name !== 'id' && ! includes( attributes, name ) ) { node.removeAttribute( name ); } } ); From 98e418fca418914c25323edd8b59f023d61d9492 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 24 Jul 2018 14:18:21 +1000 Subject: [PATCH 2/2] Only allow blocks that support the anchor feature to keep their ID. --- .../blocks/src/api/raw-handling/test/utils.js | 8 ++++---- packages/blocks/src/api/raw-handling/utils.js | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/blocks/src/api/raw-handling/test/utils.js b/packages/blocks/src/api/raw-handling/test/utils.js index e7342a12fe30d3..dfa073a6e5a53a 100644 --- a/packages/blocks/src/api/raw-handling/test/utils.js +++ b/packages/blocks/src/api/raw-handling/test/utils.js @@ -109,20 +109,20 @@ describe( 'removeInvalidHTML', () => { expect( removeInvalidHTML( input, schema ) ).toBe( output ); } ); - it( 'should keep id attributes', () => { + it( 'should remove id attributes', () => { const input = '

test

'; - const output = '

test

'; + const output = '

test

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

test

'; + const input = '

test

'; const output = '

test

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

test test

'; + const input = '

test test

'; const output = '

test test

'; expect( removeInvalidHTML( input, schema ) ).toBe( output ); } ); diff --git a/packages/blocks/src/api/raw-handling/utils.js b/packages/blocks/src/api/raw-handling/utils.js index 158b8fd9064a91..c26be68df98a4f 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' ) { @@ -197,7 +209,7 @@ function cleanNodeList( nodeList, doc, schema, inline ) { if ( node.hasAttributes() ) { // Strip invalid attributes. Array.from( node.attributes ).forEach( ( { name } ) => { - if ( name !== 'class' && name !== 'id' && ! includes( attributes, name ) ) { + if ( name !== 'class' && ! includes( attributes, name ) ) { node.removeAttribute( name ); } } );