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

Parser: Hide the core namespace in serialised data #2950

Merged
merged 7 commits into from
Oct 12, 2017
Merged
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
28 changes: 14 additions & 14 deletions blocks/api/paste/test/integration/apple-out.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p><strong>This is a </strong>title</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p><strong>This is a <em>heading</em></strong></p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/list -->
<!-- wp:list -->
<ul>
<li>A</li>
<li>Bulleted</li>
Expand All @@ -19,17 +19,17 @@
</ul>
<li>List</li>
</ul>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/list -->
<!-- wp:list -->
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/table -->
<!-- wp:table -->
<table class="wp-block-table">
<tbody>
<tr>
Expand Down Expand Up @@ -67,8 +67,8 @@
</tr>
</tbody>
</table>
<!-- /wp:core/table -->
<!-- /wp:table -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>An image: </p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->
36 changes: 18 additions & 18 deletions blocks/api/paste/test/integration/google-docs-out.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <strong>title</strong><br/></p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/heading -->
<!-- wp:heading -->
<h2>This is a <em>heading</em></h2>
<!-- /wp:core/heading -->
<!-- /wp:heading -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.<br/></p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/list -->
<!-- wp:list -->
<ul>
<li>A</li>
<li>Bulleted</li>
Expand All @@ -19,17 +19,17 @@ <h2>This is a <em>heading</em></h2>
</ul>
<li>List</li>
</ul>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/list -->
<!-- wp:list -->
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/table -->
<!-- wp:table -->
<table class="wp-block-table">
<tbody>
<tr>
Expand All @@ -49,17 +49,17 @@ <h2>This is a <em>heading</em></h2>
</tr>
</tbody>
</table>
<!-- /wp:core/table -->
<!-- /wp:table -->

<!-- wp:core/separator -->
<!-- wp:separator -->
<hr class="wp-block-separator" />
<!-- /wp:core/separator -->
<!-- /wp:separator -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>An image:<br/></p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/image -->
<!-- wp:image -->
<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/ID" /></figure>
<!-- /wp:core/image -->
<!-- /wp:image -->

28 changes: 14 additions & 14 deletions blocks/api/paste/test/integration/ms-word-online-out.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <em>heading</em> </p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/">link</a>. </p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/list -->
<!-- wp:list -->
<ul>
<li>A </li>
<li>Bulleted </li>
<li>Indented </li>
<li>List </li>
</ul>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/list -->
<!-- wp:list -->
<ol>
<li>One </li>
<li>Two </li>
<li>Three </li>
</ol>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/table -->
<!-- wp:table -->
<table class="wp-block-table">
<tbody>
<tr>
Expand All @@ -43,12 +43,12 @@
</tr>
</tbody>
</table>
<!-- /wp:core/table -->
<!-- /wp:table -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>An image: </p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/image -->
<!-- wp:image -->
<figure class="wp-block-image"><img src="data:image/jpeg;base64,###" /></figure>
<!-- /wp:core/image -->
<!-- /wp:image -->
40 changes: 20 additions & 20 deletions blocks/api/paste/test/integration/ms-word-out.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a title
</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a subtitle
</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/heading -->
<!-- wp:heading -->
<h1>This is a heading level 1</h1>
<!-- /wp:core/heading -->
<!-- /wp:heading -->

<!-- wp:core/heading -->
<!-- wp:heading -->
<h2>This is a heading level 2</h2>
<!-- /wp:core/heading -->
<!-- /wp:heading -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org/">link</a>.</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/list -->
<!-- wp:list -->
<ul>
<li>A</li>
<li>Bulleted
Expand All @@ -30,17 +30,17 @@ <h2>This is a heading level 2</h2>
</li>
<li>List</li>
</ul>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/list -->
<!-- wp:list -->
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<!-- /wp:core/list -->
<!-- /wp:list -->

<!-- wp:core/table -->
<!-- wp:table -->
<table class="wp-block-table">
<tbody>
<tr>
Expand Down Expand Up @@ -78,12 +78,12 @@ <h2>This is a heading level 2</h2>
</tr>
</tbody>
</table>
<!-- /wp:core/table -->
<!-- /wp:table -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>An image:</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/image -->
<!-- wp:image -->
<figure class="wp-block-image"><img src="" /></figure>
<!-- /wp:core/image -->
<!-- /wp:image -->
8 changes: 4 additions & 4 deletions blocks/api/paste/test/integration/plain-out.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>test<br/>test</p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->

<!-- wp:core/paragraph -->
<!-- wp:paragraph -->
<p>test<br/></p>
<!-- /wp:core/paragraph -->
<!-- /wp:paragraph -->
13 changes: 12 additions & 1 deletion blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,18 @@ WP_Block_End
}

WP_Block_Name
= $(ASCII_Letter (ASCII_AlphaNumeric / "/" ASCII_AlphaNumeric)*)
= WP_Namespaced_Block_Name
/ WP_Core_Block_Name

WP_Namespaced_Block_Name
= $(ASCII_Letter ASCII_AlphaNumeric* "/" ASCII_Letter ASCII_AlphaNumeric*)

WP_Core_Block_Name
= type:$(ASCII_Letter ASCII_AlphaNumeric*)
{
/** <?php return "core/$type"; ?> **/
return 'core/' + type;
}

WP_Block_Attributes
= attrs:$("{" (!("}" WS+ """/"? "-->") .)* "}")
Expand Down
6 changes: 6 additions & 0 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export function registerBlockType( name, settings ) {
);
return;
}
if ( /[A-Z]+/.test( name ) ) {
console.error(
'Block names must not contain uppercase characters.'
);
return;
}
if ( ! /^[a-z0-9-]+\/[a-z0-9-]+$/.test( name ) ) {
console.error(
'Block names must contain a namespace prefix. Example: my-plugin/my-custom-block'
Expand Down
15 changes: 10 additions & 5 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,21 @@ export function getBlockContent( block ) {
/**
* Returns the content of a block, including comment delimiters.
*
* @param {String} blockName Block name
* @param {Object} attributes Block attributes
* @param {String} content Block save content
* @return {String} Comment-delimited block content
* @param {String} rawBlockName Block name
* @param {Object} attributes Block attributes
* @param {String} content Block save content
* @return {String} Comment-delimited block content
*/
export function getCommentDelimitedContent( blockName, attributes, content ) {
export function getCommentDelimitedContent( rawBlockName, attributes, content ) {
const serializedAttributes = ! isEmpty( attributes )
? serializeAttributes( attributes ) + ' '
: '';

// strip core blocks of their namespace prefix
const blockName = rawBlockName.startsWith( 'core/' )
? rawBlockName.slice( 5 )
: rawBlockName;

if ( ! content ) {
return `<!-- wp:${ blockName } ${ serializedAttributes }/-->`;
}
Expand Down
27 changes: 26 additions & 1 deletion blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe( 'block parser', () => {
);
expect( block.name ).toBe( 'core/unknown-block' );
expect( block.attributes.fruit ).toBe( 'Bananas' );
expect( block.attributes.content ).toContain( 'core/test-block' );
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is kind of weird, I don't understand why we were receiving core/test-block in the content attributes and why we're receiving wp:test-block right now. (Both seem odd if we look at the definition of the unknown block)

Copy link
Member Author

@pento pento Oct 11, 2017

Choose a reason for hiding this comment

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

This behaviour was added in #2513.

expect( block.attributes.content ).toContain( 'wp:test-block' );
} );

it( 'should fall back to the unknown type handler if block type not specified', () => {
Expand Down Expand Up @@ -333,6 +333,31 @@ describe( 'block parser', () => {
expect( typeof parsed[ 0 ].uid ).toBe( 'string' );
} );

it( 'should add the core namespace to un-namespaced blocks', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

const parsed = parse(
'<!-- wp:test-block -->\nRibs\n<!-- /wp:test-block -->'
);

expect( parsed ).toHaveLength( 1 );
expect( parsed[ 0 ].name ).toBe( 'core/test-block' );
} );

it( 'should ignore blocks with a bad namespace', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

setUnknownTypeHandlerName( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown/block -->Ribs<!-- /wp:core/unknown/block -->'
);
expect( parsed ).toHaveLength( 1 );
expect( parsed[ 0 ].name ).toBe( 'core/test-block' );
} );

it( 'should parse the post content, using unknown block handler', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', defaultBlockSettings );
Expand Down
6 changes: 6 additions & 0 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with uppercase characters', () => {
const block = registerBlockType( 'Core/Paragraph' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must not contain uppercase characters.' );
expect( block ).toBeUndefined();
} );

it( 'should accept valid block names', () => {
const block = registerBlockType( 'my-plugin/fancy-block-4', defaultBlockSettings );
expect( console.error ).not.toHaveBeenCalled();
Expand Down
Loading