Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ID attributes on any elements #8124

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a unit tests for headings (where we keep the attribute?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without moving things around, or copy/pasting code. Testing that functionality requires access to getRawTransformations(), which currently isn't exported.


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