Skip to content

Commit

Permalink
Raw handling: preserve image ID (#6638)
Browse files Browse the repository at this point in the history
* Raw handling: preserve image ID

* Address feedback
  • Loading branch information
ellatrix authored May 11, 2018
1 parent d70ec18 commit 0ab0deb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions blocks/api/raw-handling/test/integration/one-image-out.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" /></figure>
<!-- wp:image {"id":5114} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114" /></figure>
<!-- /wp:image -->
8 changes: 4 additions & 4 deletions blocks/api/raw-handling/test/integration/two-images-out.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- wp:image -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" /></figure>
<!-- wp:image {"id":5114} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114" /></figure>
<!-- /wp:image -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-05-2017-17-52-09-9-300x248.gif" alt="" /></figure>
<!-- wp:image {"id":5109} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-05-2017-17-52-09-9-300x248.gif" alt="" class="wp-image-5109" /></figure>
<!-- /wp:image -->
26 changes: 18 additions & 8 deletions blocks/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { omit, mergeWith, includes } from 'lodash';
import { omit, mergeWith, includes, noop } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -204,13 +204,23 @@ function cleanNodeList( nodeList, doc, schema, inline ) {

// Strip invalid classes.
if ( node.classList.length ) {
const newClasses = classes.filter( ( name ) =>
node.classList.contains( name )
);

if ( newClasses.length ) {
node.setAttribute( 'class', newClasses.join( ' ' ) );
} else {
const mattchers = classes.map( ( item ) => {
if ( typeof item === 'string' ) {
return ( className ) => className === item;
} else if ( item instanceof RegExp ) {
return ( className ) => item.test( className );
}

return noop;
} );

Array.from( node.classList ).forEach( ( name ) => {
if ( ! mattchers.some( ( isMatch ) => isMatch( name ) ) ) {
node.classList.remove( name );
}
} );

if ( ! node.classList.length ) {
node.removeAttribute( 'class' );
}
}
Expand Down
13 changes: 9 additions & 4 deletions core-blocks/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const blockAttributes = {
const imageSchema = {
img: {
attributes: [ 'src', 'alt' ],
classes: [ 'alignleft', 'aligncenter', 'alignright', 'alignnone' ],
classes: [ 'alignleft', 'aligncenter', 'alignright', 'alignnone', /^wp-image-\d+$/ ],
},
};

Expand Down Expand Up @@ -100,10 +100,15 @@ export const settings = {
isMatch: ( node ) => node.nodeName === 'FIGURE' && !! node.querySelector( 'img' ),
schema,
transform: ( node ) => {
const matches = /align(left|center|right)/.exec( node.className );
const align = matches ? matches[ 1 ] : undefined;
// Search both figure and image classes. Alignment could be
// set on either. ID is set on the image.
const className = node.className + ' ' + node.querySelector( 'img' ).className;
const alignMatches = /(?:^|\s)align(left|center|right)(?:$|\s)/.exec( className );
const align = alignMatches ? alignMatches[ 1 ] : undefined;
const idMatches = /(?:^|\s)wp-image-(\d+)(?:$|\s)/.exec( className );
const id = idMatches ? idMatches[ 1 ] : undefined;
const blockType = getBlockType( 'core/image' );
const attributes = getBlockAttributes( blockType, node.outerHTML, { align } );
const attributes = getBlockAttributes( blockType, node.outerHTML, { align, id } );
return createBlock( 'core/image', attributes );
},
},
Expand Down

0 comments on commit 0ab0deb

Please sign in to comment.