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

Parsing: Enforce block naming requirements consistently #3521

Merged
merged 2 commits into from
Nov 17, 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
21 changes: 5 additions & 16 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,36 +283,25 @@ Block_Name
/ Core_Block_Name

Namespaced_Block_Name
= $(ASCII_Letter ASCII_AlphaNumeric* "/" ASCII_Letter ASCII_AlphaNumeric*)
= $( Block_Name_Part "/" Block_Name_Part )

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

Block_Name_Part
= $( [a-z][a-z0-9_-]* )

Block_Attributes
= attrs:$("{" (!("}" WS+ """/"? "-->") .)* "}")
{
/** <?php return json_decode( $attrs, true ); ?> **/
return maybeJSON( attrs );
}

ASCII_AlphaNumeric
= ASCII_Letter
/ ASCII_Digit
/ Special_Chars

ASCII_Letter
= [a-zA-Z]

ASCII_Digit
= [0-9]

Special_Chars
= [\-\_]

WS
= [ \t\r\n]

Expand Down
10 changes: 2 additions & 8 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,9 @@ export function registerBlockType( name, settings ) {
);
return;
}
if ( /[A-Z]+/.test( name ) ) {
if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.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'
'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block'
);
return;
}
Expand Down
14 changes: 10 additions & 4 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,31 @@ describe( 'blocks', () => {

it( 'should reject blocks without a namespace', () => {
const block = registerBlockType( 'doing-it-wrong' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix. Example: my-plugin/my-custom-block' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' );
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with too many namespaces', () => {
const block = registerBlockType( 'doing/it/wrong' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix. Example: my-plugin/my-custom-block' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' );
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with invalid characters', () => {
const block = registerBlockType( 'still/_doing_it_wrong' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix. Example: my-plugin/my-custom-block' );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' );
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( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' );
expect( block ).toBeUndefined();
} );

it( 'should reject blocks not starting with a letter', () => {
const block = registerBlockType( 'my-plugin/4-fancy-block', defaultBlockSettings );
expect( console.error ).toHaveBeenCalledWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' );
expect( block ).toBeUndefined();
} );

Expand Down
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__4-invalid-starting-letter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/4-invalid /-->
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__4-invalid-starting-letter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uid": "_uid_0",
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/4-invalid /-->"
},
"originalContent": "<!-- wp:core/4-invalid /-->"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"attrs": {},
"innerHTML": "<!-- wp:core/4-invalid /-->\n"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/4-invalid /-->
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__invalid-Capitals.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/invalid-Capitals /-->
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__invalid-Capitals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uid": "_uid_0",
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/invalid-Capitals /-->"
},
"originalContent": "<!-- wp:core/invalid-Capitals /-->"
}
]
6 changes: 6 additions & 0 deletions blocks/test/fixtures/core__invalid-Capitals.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"attrs": {},
"innerHTML": "<!-- wp:core/invalid-Capitals /-->\n"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/invalid-Capitals /-->
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__invalid-special.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/invalid-$special /-->
11 changes: 11 additions & 0 deletions blocks/test/fixtures/core__invalid-special.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"uid": "_uid_0",
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/invalid-$special /-->"
},
"originalContent": "<!-- wp:core/invalid-$special /-->"
}
]
6 changes: 6 additions & 0 deletions blocks/test/fixtures/core__invalid-special.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"attrs": {},
"innerHTML": "<!-- wp:core/invalid-$special /-->\n"
}
]
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__invalid-special.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:core/invalid-$special /-->
4 changes: 3 additions & 1 deletion docs/block-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ The name for a block is a unique string that identifies a block. Names have to b
registerBlockType( 'my-plugin/book', {} );
```

*Note:* this name is used on the comment delimiters as `<!-- wp:my-plugin/book -->`. Those blocks provided by core don't include a namespace when serialized.
*Note:* A block name can only contain lowercase alphanumeric characters and dashes, and must begin with a letter.

*Note:* This name is used on the comment delimiters as `<!-- wp:my-plugin/book -->`. Those blocks provided by core don't include a namespace when serialized.

### Block Configuration

Expand Down
Loading