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

Blocks: Automatically generate a wrapper classname for blocks #1381

Merged
merged 7 commits into from
Jun 23, 2017
41 changes: 34 additions & 7 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* External dependencies
*/
import { isEmpty, map, reduce } from 'lodash';
import { isEmpty, map, reduce, kebabCase, isObject } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Component, createElement, renderToString } from 'element';
import { Component, createElement, renderToString, cloneElement, Children } from 'element';

/**
* Internal dependencies
Expand All @@ -16,14 +17,29 @@ import { getBlockType } from './registration';
import { parseBlockAttributes } from './parser';

/**
* Given a block's save render implementation and attributes, returns the
* Returns the block's default classname from its name
*
* @param {String} blockName The block name
* @return {string} The block's default class
*/
export function getBlockDefaultClassname( blockName ) {
// Drop the namespace "core/"" for core blocks only
const match = /^([a-z0-9-]+)\/([a-z0-9-]+)$/.exec( blockName );
const sanitizedBlockName = match[ 1 ] === 'core' ? match[ 2 ] : blockName;

return `wp-block-${ kebabCase( sanitizedBlockName ) }`;
}

/**
* Given a block type containg a save render implementation and attributes, returns the
* static markup to be saved.
*
* @param {Function|WPComponent} save Save render implementation
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {string} Save content
*/
export function getSaveContent( save, attributes ) {
export function getSaveContent( blockType, attributes ) {
const { save, className = getBlockDefaultClassname( blockType.name ) } = blockType;
let rawContent;

if ( save.prototype instanceof Component ) {
Expand All @@ -37,8 +53,19 @@ export function getSaveContent( save, attributes ) {
}
}

// Adding a generic classname
const addClassnameToElement = ( element ) => {
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to have a list of blocks where we don't want to apply the class (p and heading, to start).

Copy link
Contributor Author

@youknowriad youknowriad Jun 22, 2017

Choose a reason for hiding this comment

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

what about a block setting className that can be nullable and if not set we default to wp-block-core-quote

if ( ! element || ! isObject( element ) || ! className ) {
return element;
}

const updatedClassName = classnames( element.props.className, className );
return cloneElement( element, { className: updatedClassName } );
};
const contentWithClassname = Children.map( rawContent, addClassnameToElement );

// Otherwise, infer as element
return renderToString( rawContent );
return renderToString( contentWithClassname );
}

const escapeDoubleQuotes = value => value.replace( /"/g, '\"' );
Expand Down Expand Up @@ -107,7 +134,7 @@ function asNameValuePair( value, key ) {
export function serializeBlock( block ) {
const blockName = block.name;
const blockType = getBlockType( blockName );
const saveContent = getSaveContent( blockType.save, block.attributes );
const saveContent = getSaveContent( blockType, block.attributes );
const saveAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType ) );

const serializedAttributes = ! isEmpty( saveAttributes )
Expand Down
61 changes: 54 additions & 7 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ describe( 'block serializer', () => {
context( 'function save', () => {
it( 'should return string verbatim', () => {
const saved = getSaveContent(
( { attributes } ) => attributes.fruit,
{
save: ( { attributes } ) => attributes.fruit,
name: 'core/fruit',
},
{ fruit: 'Bananas' }
);

Expand All @@ -34,7 +37,48 @@ describe( 'block serializer', () => {

it( 'should return element as string if save returns element', () => {
const saved = getSaveContent(
( { attributes } ) => createElement( 'div', null, attributes.fruit ),
{
save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ),
name: 'core/fruit',
},
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( '<div class="wp-block-fruit">Bananas</div>' );
} );

it( 'should return use the namespace in the classname if it\' not a core block', () => {
const saved = getSaveContent(
{
save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ),
name: 'myplugin/fruit',
},
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( '<div class="wp-block-myplugin-fruit">Bananas</div>' );
} );

it( 'should overrides the className', () => {
const saved = getSaveContent(
{
save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ),
name: 'myplugin/fruit',
className: 'apples',
},
{ fruit: 'Bananas' }
);

expect( saved ).to.equal( '<div class="apples">Bananas</div>' );
} );

it( 'should not add a className if falsy', () => {
const saved = getSaveContent(
{
save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ),
name: 'myplugin/fruit',
className: false,
},
{ fruit: 'Bananas' }
);

Expand All @@ -45,10 +89,13 @@ describe( 'block serializer', () => {
context( 'component save', () => {
it( 'should return element as string', () => {
const saved = getSaveContent(
class extends Component {
render() {
return createElement( 'div', null, this.props.attributes.fruit );
}
{
save: class extends Component {
render() {
return createElement( 'div', null, this.props.attributes.fruit );
}
},
name: 'core/fruit',
},
{ fruit: 'Bananas' }
);
Expand Down Expand Up @@ -124,7 +171,7 @@ describe( 'block serializer', () => {
},
},
];
const expectedPostContent = '<!-- wp:core/test-block align="left" -->\n<p>Ribs & Chicken</p>\n<!-- /wp:core/test-block -->';
const expectedPostContent = '<!-- wp:core/test-block align="left" -->\n<p class="wp-block-test-block">Ribs & Chicken</p>\n<!-- /wp:core/test-block -->';

expect( serialize( blockList ) ).to.eql( expectedPostContent );
} );
Expand Down
2 changes: 2 additions & 0 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ registerBlockType( 'core/heading', {

category: 'common',

className: false,

attributes: {
content: children( 'h1,h2,h3,h4,h5,h6' ),
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
Expand Down
2 changes: 2 additions & 0 deletions blocks/library/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ registerBlockType( 'core/html', {

category: 'formatting',

className: false,

attributes: {
content: children(),
},
Expand Down
2 changes: 2 additions & 0 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ registerBlockType( 'core/list', {
values: children( 'ol,ul' ),
},

className: false,

transforms: {
from: [
{
Expand Down
2 changes: 2 additions & 0 deletions blocks/library/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ registerBlockType( 'core/text', {

category: 'common',

className: false,

attributes: {
content: query( 'p', children() ),
},
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-button-center.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:core/button align="center" -->
<div class="aligncenter"><a href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<div class="aligncenter wp-block-button"><a href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<!-- /wp:core/button -->
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-button-center.serialized.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- wp:core/button align="center" -->
<div class="aligncenter"><a href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<div class="aligncenter wp-block-button"><a href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<!-- /wp:core/button -->

2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-code.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/code -->
<pre><code>export default function MyButton() {
<pre class="wp-block-code"><code>export default function MyButton() {
return &lt;Button&gt;Click Me!&lt;/Button&gt;;
}</code></pre>
<!-- /wp:core/code -->
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-code.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/code -->
<pre><code>export default function MyButton() {
<pre class="wp-block-code"><code>export default function MyButton() {
return &lt;Button&gt;Click Me!&lt;/Button&gt;;
}</code></pre>
<!-- /wp:core/code -->
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/cover-image url="https://cldup.com/uuUqE_dXzy.jpg" -->
<section class="blocks-cover-image">
<section class="blocks-cover-image wp-block-cover-image">
<section class="cover-image" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg);">
<h2>Guten Berg!</h2>
</section>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embed.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embed url="https://example.com/" -->
<figure>
<figure class="wp-block-embed">
https://example.com/
<figcaption>Embedded content from an example URL</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embed.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embed url="https://example.com/" -->
<figure>
<figure class="wp-block-embed">
https://example.com/
<figcaption>Embedded content from an example URL</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedanimoto.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedanimoto url="https://animoto.com/" -->
<figure>
<figure class="wp-block-embedanimoto">
https://animoto.com/
<figcaption>Embedded content from animoto</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedanimoto.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedanimoto url="https://animoto.com/" -->
<figure>
<figure class="wp-block-embedanimoto">
https://animoto.com/
<figcaption>Embedded content from animoto</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedcloudup.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedcloudup url="https://cloudup.com/" -->
<figure>
<figure class="wp-block-embedcloudup">
https://cloudup.com/
<figcaption>Embedded content from cloudup</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedcloudup.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedcloudup url="https://cloudup.com/" -->
<figure>
<figure class="wp-block-embedcloudup">
https://cloudup.com/
<figcaption>Embedded content from cloudup</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedcollegehumor.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedcollegehumor url="https://collegehumor.com/" -->
<figure>
<figure class="wp-block-embedcollegehumor">
https://collegehumor.com/
<figcaption>Embedded content from collegehumor</figcaption>
</figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedcollegehumor url="https://collegehumor.com/" -->
<figure>
<figure class="wp-block-embedcollegehumor">
https://collegehumor.com/
<figcaption>Embedded content from collegehumor</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embeddailymotion.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embeddailymotion url="https://dailymotion.com/" -->
<figure>
<figure class="wp-block-embeddailymotion">
https://dailymotion.com/
<figcaption>Embedded content from dailymotion</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embeddailymotion.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embeddailymotion url="https://dailymotion.com/" -->
<figure>
<figure class="wp-block-embeddailymotion">
https://dailymotion.com/
<figcaption>Embedded content from dailymotion</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedfacebook.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedfacebook url="https://facebook.com/" -->
<figure>
<figure class="wp-block-embedfacebook">
https://facebook.com/
<figcaption>Embedded content from facebook</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedfacebook.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedfacebook url="https://facebook.com/" -->
<figure>
<figure class="wp-block-embedfacebook">
https://facebook.com/
<figcaption>Embedded content from facebook</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedflickr.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedflickr url="https://flickr.com/" -->
<figure>
<figure class="wp-block-embedflickr">
https://flickr.com/
<figcaption>Embedded content from flickr</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedflickr.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedflickr url="https://flickr.com/" -->
<figure>
<figure class="wp-block-embedflickr">
https://flickr.com/
<figcaption>Embedded content from flickr</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedfunnyordie.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedfunnyordie url="https://funnyordie.com/" -->
<figure>
<figure class="wp-block-embedfunnyordie">
https://funnyordie.com/
<figcaption>Embedded content from funnyordie</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedfunnyordie.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedfunnyordie url="https://funnyordie.com/" -->
<figure>
<figure class="wp-block-embedfunnyordie">
https://funnyordie.com/
<figcaption>Embedded content from funnyordie</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedhulu.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedhulu url="https://hulu.com/" -->
<figure>
<figure class="wp-block-embedhulu">
https://hulu.com/
<figcaption>Embedded content from hulu</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedhulu.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedhulu url="https://hulu.com/" -->
<figure>
<figure class="wp-block-embedhulu">
https://hulu.com/
<figcaption>Embedded content from hulu</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedimgur.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedimgur url="https://imgur.com/" -->
<figure>
<figure class="wp-block-embedimgur">
https://imgur.com/
<figcaption>Embedded content from imgur</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedimgur.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedimgur url="https://imgur.com/" -->
<figure>
<figure class="wp-block-embedimgur">
https://imgur.com/
<figcaption>Embedded content from imgur</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedinstagram.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedinstagram url="https://instagram.com/" -->
<figure>
<figure class="wp-block-embedinstagram">
https://instagram.com/
<figcaption>Embedded content from instagram</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedinstagram.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedinstagram url="https://instagram.com/" -->
<figure>
<figure class="wp-block-embedinstagram">
https://instagram.com/
<figcaption>Embedded content from instagram</figcaption>
</figure>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embedissuu.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/embedissuu url="https://issuu.com/" -->
<figure>
<figure class="wp-block-embedissuu">
https://issuu.com/
<figcaption>Embedded content from issuu</figcaption>
</figure>
Expand Down
Loading