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: Replace js-beautify with element beautification #6716

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 3 additions & 19 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { isEmpty, reduce, isObject, castArray, startsWith } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';

/**
* WordPress dependencies
Expand Down Expand Up @@ -117,7 +116,8 @@ export function getSaveElement( blockType, attributes, innerBlocks = [] ) {
* @return {string} Save content.
*/
export function getSaveContent( blockType, attributes, innerBlocks ) {
return renderToString( getSaveElement( blockType, attributes, innerBlocks ) );
const element = getSaveElement( blockType, attributes, innerBlocks );
return renderToString( element, { beautify: true } );
}

/**
Expand Down Expand Up @@ -172,22 +172,6 @@ export function serializeAttributes( attrs ) {
.replace( /&/g, '\\u0026' ); // ibid
}

/**
* Returns HTML markup processed by a markup beautifier configured for use in
* block serialization.
*
* @param {string} content Original HTML.
*
* @return {string} Beautiful HTML.
*/
export function getBeautifulContent( content ) {
return beautifyHtml( content, {
indent_inner_html: true,
indent_with_tabs: true,
wrap_line_length: 0,
} );
}

/**
* Given a block object, returns the Block's Inner HTML markup.
*
Expand All @@ -211,7 +195,7 @@ export function getBlockContent( block ) {
} catch ( error ) {}
}

return getUnknownTypeHandlerName() === block.name || ! saveContent ? saveContent : getBeautifulContent( saveContent );
return saveContent;
}

/**
Expand Down
13 changes: 2 additions & 11 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { createElement, Component } from '@wordpress/element';
*/
import serialize, {
getCommentAttributes,
getBeautifulContent,
getSaveContent,
serializeAttributes,
getCommentDelimitedContent,
Expand Down Expand Up @@ -36,14 +35,6 @@ describe( 'block serializer', () => {
} );
} );

describe( 'getBeautifulContent()', () => {
it( 'returns beautiful content', () => {
const content = getBeautifulContent( '<div><div>Beautiful</div></div>' );

expect( content ).toBe( '<div>\n\t<div>Beautiful</div>\n</div>' );
} );
} );

describe( 'getSaveContent()', () => {
describe( 'function save', () => {
it( 'should return element as string if save returns element', () => {
Expand Down Expand Up @@ -311,11 +302,11 @@ describe( 'block serializer', () => {
const block = {
name: 'core/chicken',
attributes: {
content: 'chicken',
content: 'chicken\nribs',
},
isValid: true,
};
expect( getBlockContent( block ) ).toBe( 'chicken' );
expect( getBlockContent( block ) ).toBe( 'chicken\nribs' );
} );
} );
} );
2 changes: 1 addition & 1 deletion core-blocks/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function getEmbedBlockSettings( { title, description, icon, category = 'embed',

return (
<figure className={ embedClassName }>
{ `\n${ url }\n` /* URL needs to be on its own line. */ }
{ `\n\t${ url }` /* URL needs to be on its own line. */ }
{ caption && caption.length > 0 && <RichText.Content tagName="figcaption" value={ caption } /> }
</figure>
);
Expand Down
4 changes: 3 additions & 1 deletion core-blocks/test/fixtures/core__audio.serialized.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- wp:audio {"align":"right"} -->
<figure class="wp-block-audio alignright"><audio controls src="https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3"></audio></figure>
<figure class="wp-block-audio alignright">
<audio controls src="https://media.simplecast.com/episodes/audio/80564/draft-podcast-51-livePublish2.mp3"></audio>
</figure>
<!-- /wp:audio -->
26 changes: 12 additions & 14 deletions core-blocks/test/fixtures/core__columns.serialized.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<!-- wp:columns {"columns":3} -->
<div class="wp-block-columns has-3-columns">
<!-- wp:paragraph {"layout":"column-1"} -->
<p class="layout-column-1">Column One, Paragraph One</p>
<!-- /wp:paragraph -->
<div class="wp-block-columns has-3-columns"><!-- wp:paragraph {"layout":"column-1"} -->
<p class="layout-column-1">Column One, Paragraph One</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"layout":"column-1"} -->
<p class="layout-column-1">Column One, Paragraph Two</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"layout":"column-1"} -->
<p class="layout-column-1">Column One, Paragraph Two</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"layout":"column-2"} -->
<p class="layout-column-2">Column Two, Paragraph One</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"layout":"column-2"} -->
<p class="layout-column-2">Column Two, Paragraph One</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"layout":"column-3"} -->
<p class="layout-column-3">Column Three, Paragraph One</p>
<!-- /wp:paragraph -->
</div>
<!-- wp:paragraph {"layout":"column-3"} -->
<p class="layout-column-3">Column Three, Paragraph One</p>
<!-- /wp:paragraph --></div>
<!-- /wp:columns -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:preformatted -->
<pre class="wp-block-preformatted">Some <em>preformatted</em> text...<br/>And more!</pre>
<pre class="wp-block-preformatted">Some <em>preformatted</em> text...<br />And more!</pre>
<!-- /wp:preformatted -->
3 changes: 2 additions & 1 deletion core-blocks/test/fixtures/core__pullquote.serialized.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- wp:pullquote -->
<blockquote class="wp-block-pullquote alignnone">
<p>Testing pullquote block...</p><cite>...with a caption</cite></blockquote>
<p>Testing pullquote block...</p><cite>...with a caption</cite>
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated. Ugh, maybe we should wrap cite elements in a footer or something?

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe we should wrap cite elements in a footer or something?

Would the benefit there be merely to put cite on its own line?

Copy link
Member

Choose a reason for hiding this comment

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

To me it doesn't make sense to have phrasing content next to block level content (cite and p on the same level). I guess that's just a personal thing.

</blockquote>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!-- wp:pullquote -->
<blockquote class="wp-block-pullquote alignnone">
<p>Paragraph <strong>one</strong></p>
<p>Paragraph two</p><cite>by whomever</cite></blockquote>
<p>Paragraph two</p><cite>by whomever</cite>
</blockquote>
<!-- /wp:pullquote -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- wp:quote -->
<blockquote class="wp-block-quote">
<p>The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.</p><cite>Matt Mullenweg, 2017</cite></blockquote>
<p>The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.</p><cite>Matt Mullenweg, 2017</cite>
</blockquote>
<!-- /wp:quote -->
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- wp:quote {"style":2} -->
<blockquote class="wp-block-quote is-large">
<p>There is no greater agony than bearing an untold story inside you.</p><cite>Maya Angelou</cite></blockquote>
<p>There is no greater agony than bearing an untold story inside you.</p><cite>Maya Angelou</cite>
</blockquote>
<!-- /wp:quote -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:paragraph -->
<p>This is an old-style text block. Changed to <code>paragraph</code> in #2135.</p>
<p>This is an old-style text block. Changed to <code>paragraph</code> in #2135.</p>
Copy link
Member

Choose a reason for hiding this comment

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

Interesting. So beautify is also dropping multiple spaces?

Copy link
Member Author

@aduth aduth May 23, 2018

Choose a reason for hiding this comment

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

Interesting. So beautify is also dropping multiple spaces?

Yes, presumably because in a browser there would be no rendered difference (whitespace collapsing).

<!-- /wp:paragraph -->
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core__verse.serialized.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:verse -->
<pre class="wp-block-verse">A <em>verse</em>…<br/>And more!</pre>
<pre class="wp-block-verse">A <em>verse</em>…<br />And more!</pre>
<!-- /wp:verse -->
4 changes: 3 additions & 1 deletion core-blocks/test/fixtures/core__video.serialized.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- wp:video -->
<figure class="wp-block-video"><video controls src="https://awesome-fake.video/file.mp4"></video></figure>
<figure class="wp-block-video">
<video controls src="https://awesome-fake.video/file.mp4"></video>
Copy link
Member

Choose a reason for hiding this comment

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

This is nice!

</figure>
<!-- /wp:video -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

exports[`InnerBlocks should force serialize for invalid block with inner blocks 1`] = `
"<!-- wp:test-block -->
<p>Invalid
<!-- wp:test-block -->
<p></p>
<!-- /wp:test-block -->
</p>
<p>Invalid<!-- wp:test-block -->
<p></p>
<!-- /wp:test-block --></p>
<!-- /wp:test-block -->"
`;

Expand Down
7 changes: 6 additions & 1 deletion editor/components/inner-blocks/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getSaveElement,
registerBlockType,
serialize,
parse,
unregisterBlockType,
} from '@wordpress/blocks';
import { renderToString } from '@wordpress/element';
Expand Down Expand Up @@ -106,6 +107,10 @@ describe( 'InnerBlocks', () => {
block.isValid = false;
block.originalContent = 'Original';

expect( serialize( block ) ).toMatchSnapshot();
const serialized = serialize( block );
expect( serialized ).toMatchSnapshot();
// Ensure beautification doesn't impact (trailing, leading whitespace)
// re-parsed content:
expect( parse( serialized )[ 0 ].attributes.content ).toEqual( block.attributes.content );
} );
} );
60 changes: 7 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"eslint-plugin-wordpress": "git://github.com/WordPress-Coding-Standards/eslint-plugin-wordpress.git#1774343f6226052a46b081e01db3fca8793cc9f1",
"hpq": "1.2.0",
"jquery": "3.2.1",
"js-beautify": "1.6.14",
"lerna": "2.11.0",
"lodash": "4.17.5",
"memize": "1.0.5",
Expand Down
Loading