From af617a2a03383c6125cb2279721bc4e331437e28 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Sun, 11 Jun 2017 16:49:27 +0200 Subject: [PATCH 1/3] Parser: Add "void" block signature Sometimes a block doesn't need anything inside of the block comment tags as children. One example of this is the traditional "more tag," which only needs to indicate position. These changes introduce a new syntax element, the "void block," borrowing from the HTML5 nomenclature for HTML void elements. In this new element, there are no children. Since nothing changes on the JavaScript size this could be considered an optimization whereby any block with no children serializes to a self-closing block comment tag, saving some space and improving readability of the `post_content`. --- blocks/api/post.pegjs | 11 ++++++++++- post-content.js | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/blocks/api/post.pegjs b/blocks/api/post.pegjs index e53d9fa530524b..e45139e048e3e7 100644 --- a/blocks/api/post.pegjs +++ b/blocks/api/post.pegjs @@ -15,9 +15,18 @@ WP_Block_List = WP_Block* WP_Block - = WP_Block_Balanced + = WP_Block_Void + / WP_Block_Balanced / WP_Block_Html +WP_Block_Void + = "" + { return { + blockType: blockType, + attrs: attrs, + rawContent: '' + } } + WP_Block_Balanced = s:WP_Block_Start ts:(!WP_Block_End c:Any { return c })* e:WP_Block_End & { return s.blockType === e.blockType } { return { diff --git a/post-content.js b/post-content.js index 33b6cc98a13535..8317d2b64ae3a6 100644 --- a/post-content.js +++ b/post-content.js @@ -16,6 +16,8 @@ window._wpGutenbergPost = { '

... like this one, which is separate from the above and right aligned.

', '', + '', + '', '

Headings are separate blocks as well, which helps with the outline and organization of your content.

', '', From 127aecbe110b6d040c8a03ded384760c2bf27a3d Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Mon, 12 Jun 2017 21:23:06 +0200 Subject: [PATCH 2/3] Update serializer to use void block if no children exist for block --- blocks/api/serializer.js | 12 +++++++----- blocks/test/fixtures/core-latestposts.html | 2 +- .../test/fixtures/core-latestposts.serialized.html | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 9080e853f144fe..5ccd305befc454 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -82,17 +82,19 @@ export default function serialize( blocks ) { indent_inner_html: true, wrap_line_length: 0, }; + const blockAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType ) ); + + if ( ! saveContent ) { + return memo + '\n\n'; + } return memo + ( '' + - ( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) + + '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' + '' diff --git a/blocks/test/fixtures/core-latestposts.html b/blocks/test/fixtures/core-latestposts.html index cc09cb936f430a..811b2260df7382 100644 --- a/blocks/test/fixtures/core-latestposts.html +++ b/blocks/test/fixtures/core-latestposts.html @@ -1,2 +1,2 @@ - + diff --git a/blocks/test/fixtures/core-latestposts.serialized.html b/blocks/test/fixtures/core-latestposts.serialized.html index cc09cb936f430a..811b2260df7382 100644 --- a/blocks/test/fixtures/core-latestposts.serialized.html +++ b/blocks/test/fixtures/core-latestposts.serialized.html @@ -1,2 +1,2 @@ - + From 5ebe561b7e9803aed3050f4871ab06a2e4569cc2 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Mon, 12 Jun 2017 17:19:55 -0400 Subject: [PATCH 3/3] Use current default parser for full-content tests Also update the full-content fixtures with the resulting changes. --- blocks/test/fixtures/core-button-center.html | 2 +- blocks/test/fixtures/core-freeform.json | 7 ++++--- blocks/test/fixtures/core-freeform.serialized.html | 4 +++- blocks/test/fixtures/core-text-multi-paragraph.json | 1 + blocks/test/full-content.js | 7 ++----- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/blocks/test/fixtures/core-button-center.html b/blocks/test/fixtures/core-button-center.html index e19e0ed3fc4d6e..b80f97864c2935 100644 --- a/blocks/test/fixtures/core-button-center.html +++ b/blocks/test/fixtures/core-button-center.html @@ -1,3 +1,3 @@ - + diff --git a/blocks/test/fixtures/core-freeform.json b/blocks/test/fixtures/core-freeform.json index 09a99fbee8aa5d..c12160e9584178 100644 --- a/blocks/test/fixtures/core-freeform.json +++ b/blocks/test/fixtures/core-freeform.json @@ -4,13 +4,13 @@ "name": "core/freeform", "attributes": { "content": [ - "Testing freeform block with some", + "Testing freeform block with some\n", { "attributes": { "className": "wp-some-class" }, "children": [ - "HTML ", + "\n\tHTML ", { "attributes": { "style": { @@ -19,7 +19,8 @@ }, "children": "content", "type": "span" - } + }, + "\n" ], "type": "div" } diff --git a/blocks/test/fixtures/core-freeform.serialized.html b/blocks/test/fixtures/core-freeform.serialized.html index 4ce56fd27af8e9..d30114895dd4c6 100644 --- a/blocks/test/fixtures/core-freeform.serialized.html +++ b/blocks/test/fixtures/core-freeform.serialized.html @@ -1,5 +1,7 @@ Testing freeform block with some -
HTML content
+
+ HTML content +
diff --git a/blocks/test/fixtures/core-text-multi-paragraph.json b/blocks/test/fixtures/core-text-multi-paragraph.json index 4760696210f6b2..fd29811801eff6 100644 --- a/blocks/test/fixtures/core-text-multi-paragraph.json +++ b/blocks/test/fixtures/core-text-multi-paragraph.json @@ -15,6 +15,7 @@ "—somewhat similar to LEGO bricks—that you can move around and interact with. Move your cursor around and you'll notice the different blocks light up with outlines and arrows. Press the arrows to reposition blocks quickly, without fearing about losing things in the process of copying and pasting." ] }, + "\n", { "type": "p", "children": [ diff --git a/blocks/test/full-content.js b/blocks/test/full-content.js index 4bdf65b03bac7f..26ab2a09a60b4c 100644 --- a/blocks/test/full-content.js +++ b/blocks/test/full-content.js @@ -10,10 +10,7 @@ import { format } from 'util'; /** * Internal dependencies */ -import { - // parseWithGrammar, - parseWithTinyMCE, -} from '../api/parser'; +import parse from '../api/parser'; import serialize from '../api/serializer'; import { getBlockTypes } from '../api/registration'; @@ -94,7 +91,7 @@ describe( 'full post content fixture', () => { it( f, () => { const content = readFixtureFile( f + '.html' ); - const blocksActual = parseWithTinyMCE( content ); + const blocksActual = parse( content ); const blocksActualNormalized = normalizeParsedBlocks( blocksActual ); let blocksExpectedString = readFixtureFile( f + '.json' );