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 1 commit
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
12 changes: 6 additions & 6 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ Block_Name
/ Core_Block_Name

Namespaced_Block_Name
= $(ASCII_Letter ASCII_AlphaNumeric* "/" ASCII_Letter ASCII_AlphaNumeric*)
= $(ASCII_LowercaseLetter ASCII_LowercaseAlphaNumeric* "/" ASCII_LowercaseLetter ASCII_LowercaseAlphaNumeric*)

Core_Block_Name
= type:$(ASCII_Letter ASCII_AlphaNumeric*)
= type:$(ASCII_LowercaseLetter ASCII_LowercaseAlphaNumeric*)
{
/** <?php return "core/$type"; ?> **/
return 'core/' + type;
Expand All @@ -299,13 +299,13 @@ Block_Attributes
return maybeJSON( attrs );
}

ASCII_AlphaNumeric
= ASCII_Letter
ASCII_LowercaseAlphaNumeric
= ASCII_LowercaseLetter
Copy link
Member

@dmsnell dmsnell Nov 16, 2017

Choose a reason for hiding this comment

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

this seems a bit verbose. what if instead we ripped out the whole ASCII nomenclature and just simplified down to something like Block_Name or Block_Name_Part - we're not using the other rules so we can get rid of them

Namespaced_Block_Name
  = $( Block_Name_Part "/" Block_Name_Part )

Core_Block_Name
  = type:Block_Name_Part { return 'core/' + type }

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

Copy link
Member Author

Choose a reason for hiding this comment

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

this seems a bit verbose. what if instead we ripped out the whole ASCII nomenclature and just simplified down to something like Block_Name or Block_Name_Part

Sounds good to me. Applied in df43eb4.

Copy link
Member

Choose a reason for hiding this comment

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

looks nice!

/ ASCII_Digit
/ Special_Chars

ASCII_Letter
= [a-zA-Z]
ASCII_LowercaseLetter
= [a-z]

ASCII_Digit
= [0-9]
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
28 changes: 14 additions & 14 deletions lib/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1363,13 +1363,13 @@ private function peg_parseNamespaced_Block_Name() {

$s0 = $this->peg_currPos;
$s1 = $this->peg_currPos;
$s2 = $this->peg_parseASCII_Letter();
$s2 = $this->peg_parseASCII_LowercaseLetter();
if ($s2 !== $this->peg_FAILED) {
$s3 = array();
$s4 = $this->peg_parseASCII_AlphaNumeric();
$s4 = $this->peg_parseASCII_LowercaseAlphaNumeric();
while ($s4 !== $this->peg_FAILED) {
$s3[] = $s4;
$s4 = $this->peg_parseASCII_AlphaNumeric();
$s4 = $this->peg_parseASCII_LowercaseAlphaNumeric();
}
if ($s3 !== $this->peg_FAILED) {
if ($this->input_substr($this->peg_currPos, 1) === $this->peg_c15) {
Expand All @@ -1382,13 +1382,13 @@ private function peg_parseNamespaced_Block_Name() {
}
}
if ($s4 !== $this->peg_FAILED) {
$s5 = $this->peg_parseASCII_Letter();
$s5 = $this->peg_parseASCII_LowercaseLetter();
if ($s5 !== $this->peg_FAILED) {
$s6 = array();
$s7 = $this->peg_parseASCII_AlphaNumeric();
$s7 = $this->peg_parseASCII_LowercaseAlphaNumeric();
while ($s7 !== $this->peg_FAILED) {
$s6[] = $s7;
$s7 = $this->peg_parseASCII_AlphaNumeric();
$s7 = $this->peg_parseASCII_LowercaseAlphaNumeric();
}
if ($s6 !== $this->peg_FAILED) {
$s2 = array($s2, $s3, $s4, $s5, $s6);
Expand Down Expand Up @@ -1427,13 +1427,13 @@ private function peg_parseCore_Block_Name() {
$s0 = $this->peg_currPos;
$s1 = $this->peg_currPos;
$s2 = $this->peg_currPos;
$s3 = $this->peg_parseASCII_Letter();
$s3 = $this->peg_parseASCII_LowercaseLetter();
if ($s3 !== $this->peg_FAILED) {
$s4 = array();
$s5 = $this->peg_parseASCII_AlphaNumeric();
$s5 = $this->peg_parseASCII_LowercaseAlphaNumeric();
while ($s5 !== $this->peg_FAILED) {
$s4[] = $s5;
$s5 = $this->peg_parseASCII_AlphaNumeric();
$s5 = $this->peg_parseASCII_LowercaseAlphaNumeric();
}
if ($s4 !== $this->peg_FAILED) {
$s3 = array($s3, $s4);
Expand Down Expand Up @@ -1718,9 +1718,9 @@ private function peg_parseBlock_Attributes() {
return $s0;
}

private function peg_parseASCII_AlphaNumeric() {
private function peg_parseASCII_LowercaseAlphaNumeric() {

$s0 = $this->peg_parseASCII_Letter();
$s0 = $this->peg_parseASCII_LowercaseLetter();
if ($s0 === $this->peg_FAILED) {
$s0 = $this->peg_parseASCII_Digit();
if ($s0 === $this->peg_FAILED) {
Expand All @@ -1731,7 +1731,7 @@ private function peg_parseASCII_AlphaNumeric() {
return $s0;
}

private function peg_parseASCII_Letter() {
private function peg_parseASCII_LowercaseLetter() {

if (Gutenberg_PEG_peg_char_class_test($this->peg_c22, $this->input_substr($this->peg_currPos, 1))) {
$s0 = $this->input_substr($this->peg_currPos, 1);
Expand Down Expand Up @@ -1888,8 +1888,8 @@ public function parse($input) {
$this->peg_c19 = "}";
$this->peg_c20 = array( "type" => "literal", "value" => "}", "description" => "\"}\"" );
$this->peg_c21 = "";
$this->peg_c22 = array(array(97,122), array(65,90));
$this->peg_c23 = array( "type" => "class", "value" => "[a-zA-Z]", "description" => "[a-zA-Z]" );
$this->peg_c22 = array(array(97,122));
$this->peg_c23 = array( "type" => "class", "value" => "[a-z]", "description" => "[a-z]" );
$this->peg_c24 = array(array(48,57));
$this->peg_c25 = array( "type" => "class", "value" => "[0-9]", "description" => "[0-9]" );
$this->peg_c26 = array(array(45,45), array(95,95));
Expand Down