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

Keep and handle image alignment classes for raw transforms #3724

Merged
merged 2 commits into from
Jan 3, 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
21 changes: 19 additions & 2 deletions blocks/api/raw-handling/strip-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ELEMENT_NODE } = window.Node;
/**
* Internal dependencies
*/
import { isAttributeWhitelisted } from './utils';
import { isAttributeWhitelisted, isClassWhitelisted } from './utils';

export default function( node ) {
if ( node.nodeType !== ELEMENT_NODE ) {
Expand All @@ -20,10 +20,27 @@ export default function( node ) {
const tag = node.nodeName.toLowerCase();

Array.from( node.attributes ).forEach( ( { name } ) => {
if ( isAttributeWhitelisted( tag, name ) ) {
if ( name === 'class' || isAttributeWhitelisted( tag, name ) ) {
return;
}

node.removeAttribute( name );
} );

const oldClasses = node.getAttribute( 'class' );

if ( ! oldClasses ) {
return;
}

const newClasses = oldClasses
.split( ' ' )
.filter( ( name ) => name && isClassWhitelisted( tag, name ) )
.join( ' ' );

if ( newClasses.length ) {
node.setAttribute( 'class', newClasses );
} else {
node.removeAttribute( 'class' );
}
}
4 changes: 4 additions & 0 deletions blocks/api/raw-handling/test/strip-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ describe( 'stripAttributes', () => {
it( 'should keep some attributes', () => {
equal( deepFilterHTML( '<a href="#keep">test</a>', [ stripAttributes ] ), '<a href="#keep">test</a>' );
} );

it( 'should keep some classes', () => {
equal( deepFilterHTML( '<img class="alignright test" src="">', [ stripAttributes ] ), '<img class="alignright" src="">' );
} );
} );
10 changes: 9 additions & 1 deletion blocks/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const inlineWrapperWhiteList = {
const whitelist = {
...inlineWhitelist,
...inlineWrapperWhiteList,
img: { attributes: [ 'src', 'alt' ] },
img: { attributes: [ 'src', 'alt' ], classes: [ 'alignleft', 'aligncenter', 'alignright', 'alignnone' ] },
Copy link
Contributor

Choose a reason for hiding this comment

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

Side thought: we should start to consider performance and do some measuring. Some of the predicates that consume this data could perhaps benefit from a more barebones switch construct, for instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you give an example?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I just had this benchmark in the back of my head.

figure: {},
blockquote: {},
hr: {},
Expand Down Expand Up @@ -100,6 +100,14 @@ export function isInline( node, tagName ) {
return !! inlineWhitelist[ nodeName ] || isInlineForTag( nodeName, tagName );
}

export function isClassWhitelisted( tag, name ) {
return (
whitelist[ tag ] &&
whitelist[ tag ].classes &&
whitelist[ tag ].classes.indexOf( name ) !== -1
);
}

export function isInlineWrapper( node ) {
return !! inlineWrapperWhiteList[ node.nodeName.toLowerCase() ];
}
Expand Down
13 changes: 11 additions & 2 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createMediaFromFile } from '@wordpress/utils';
*/
import './style.scss';
import './editor.scss';
import { registerBlockType, createBlock } from '../../api';
import { registerBlockType, createBlock, getBlockAttributes, getBlockType } from '../../api';
import ImageBlock from './block';

registerBlockType( 'core/image', {
Expand Down Expand Up @@ -63,13 +63,22 @@ registerBlockType( 'core/image', {
from: [
{
type: 'raw',
isMatch: ( node ) => {
isMatch( node ) {
const tag = node.nodeName.toLowerCase();
const hasText = !! node.textContent.trim();
const hasImage = node.querySelector( 'img' );

return tag === 'img' || ( hasImage && ! hasText ) || ( hasImage && tag === 'figure' );
},
transform( node ) {
const targetNode = node.parentNode.querySelector( 'figure,img' );
const matches = /align(left|center|right)/.exec( targetNode.className );
const align = matches ? matches[ 1 ] : undefined;
const blockType = getBlockType( 'core/image' );
const attributes = getBlockAttributes( blockType, targetNode.outerHTML, { align } );

return createBlock( 'core/image', attributes );
},
},
{
type: 'files',
Expand Down