Skip to content

Commit

Permalink
Keep and handle image alignment classes for raw transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 29, 2017
1 parent 50cdcf4 commit 5d4f9a2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
12 changes: 7 additions & 5 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { getBlockType } from './registration';
import { getBlockAttributes } from './parser';

/**
* Returns a block object given its type and attributes.
*
* @param {String} name Block name
* @param {Object} blockAttributes Block attributes
* @return {Object} Block object
* @param {String} name Block name
* @param {Object} blockAttributes Block attributes
* @param {?String} innerHTML Block HTML
* @return {Object} Block object
*/
export function createBlock( name, blockAttributes = {} ) {
export function createBlock( name, blockAttributes = {}, innerHTML ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );

Expand All @@ -52,7 +54,7 @@ export function createBlock( name, blockAttributes = {} ) {
uid: uuid(),
name,
isValid: true,
attributes,
attributes: innerHTML ? getBlockAttributes( blockType, innerHTML, attributes ) : attributes,
};
}

Expand Down
20 changes: 18 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,26 @@ 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 ) => {
return name && isClassWhitelisted( tag, name );
} );

if ( newClasses.length ) {
node.setAttribute( 'class', newClasses.join( ' ' ) );
} 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 @@ -34,7 +34,7 @@ const inlineWrapperWhiteList = {
const whitelist = {
...inlineWhitelist,
...inlineWrapperWhiteList,
img: { attributes: [ 'src', 'alt' ] },
img: { attributes: [ 'src', 'alt' ], classes: [ 'alignleft', 'aligncenter', 'alignright', 'alignnone' ] },
figure: {},
blockquote: {},
hr: {},
Expand Down Expand Up @@ -63,6 +63,14 @@ export function isAttributeWhitelisted( tag, attribute ) {
);
}

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

export function isInline( node ) {
return !! inlineWhitelist[ node.nodeName.toLowerCase() ];
}
Expand Down
8 changes: 8 additions & 0 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ registerBlockType( 'core/image', {

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 );

return createBlock( 'core/image', {
align: matches ? matches[ 1 ] : undefined,
}, targetNode.outerHTML );
},
},
{
type: 'files',
Expand Down

0 comments on commit 5d4f9a2

Please sign in to comment.