-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2950 +/- ##
==========================================
+ Coverage 34.42% 34.47% +0.04%
==========================================
Files 196 196
Lines 5783 5787 +4
Branches 1021 1023 +2
==========================================
+ Hits 1991 1995 +4
Misses 3206 3206
Partials 586 586
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -223,7 +223,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' ); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
I am in favor—proposed this to both @aduth and @pento. Let's discuss the approach a bit more, though. The goal is to keep the markup as clean as possible, and In terms of registering a block, it still makes sense to keep Some thoughts on implementation:
If this makes sense, where do we do this on the server? Handling it on the grammar increases its complexity, but also prevents us from having to handle it on the client and the server. |
Also worth keeping the |
Re-adding
I'm wondering if |
@@ -226,7 +226,7 @@ describe( 'block serializer', () => { | |||
'' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to add tests for the non-core/
namespaced case now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
I like the idea of cleaner markup, but the introduced inconsistency is a bit worrying, at least with the challenge it poses the implementation. The fact that #2950 (comment) is mentioned at all serves as a reminder that intentional inconsistencies can be difficult to maintain.
What would be the reason to avoid introducing this in the grammar? It seems appealing to normalize this as early as possible. @pento's suggestion of interacting with blocks as a class object seems like the next best thing for normalizing. |
It seems like knowledge the parser shouldn't have to care about, but I'm ok with trying it there if we can retain clarity. |
blocks/api/post.pegjs
Outdated
blockName = 'core/' + blockName; | ||
} | ||
return blockName; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we're going to encode this here we might want to consider making these separate tokens in the grammar instead of adding more actual custom code in the parser
WP_Block_Name
= WP_Core_Block_Name
/ WP_Extra_Block_Name
WP_Core_Block_Name
= name:$(ASCII_Letter ASCII_AlphaNumeric*)
{ /** <?php return "core/$name" ?> */ return 'core/' + name }
WP_Extra_Block_Name
= $(ASCII_Letter (ASCII_AlphaNumeric / "/" ASCII_AlphaNumeric)*)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shuffled it around a bit in b925d43. This also explicitly parses zero or one /
s.
Is this Did we have people complain about the If someone were to look at the demo block and see all core blocks, what impression would they get that there is a necessary namespace prefix for their own blocks? |
I see it as a QoL improvement to what we save. It is of no semantic value to the user and adds confusion—"what is this core thing?". These are also valid terms in database searches, so keeping it as minimal as possible is good. I'd even be ok with dropping it entirely from the picture, but I think it holds some value in the context of registration. I like the idea of a different token: |
Incidentally, I think we can look at demoting |
On that note, seems like |
8898728
to
374605a
Compare
…s in the name. This was never allowed, but now has an explicit error message for it.
|
blocks/api/post.pegjs
Outdated
namespace = 'core/'; | ||
} | ||
return namespace + name; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're still missing some of the value of the parser: a separation of code from grammar. The more we can eliminate processing from inside of here the more declarative it is and the easier it will be to recreate in other contexts.
WP_Block_Name
= $(WP_Block_Namespace "/" WP_Namespaced_Block_name)
/ name:$(WP_Namespaced_Block_Name)
{ /** <?php return "core/$name" ?> */ return 'core/' + name }
Although here I miss some of the semantic focusing we had with a separate token for core vs. extra blocks. In this case we're making some imperative logical decisions based on the presence of a /
instead of stating two branches in a parse tree based on the identity of the tokens.
WP_Block_Type
= WP_Namespaced_Block_Type
/ WP_Core_Block_type
WP_Namespaced_Block_Type
= $(ASCII_Letter ASCII_AlphaNumeric* "/" ASCII_Letter ASCII_AlphaNumeric*)
WP_Core_Block_Type
= type:$(ASCII_Letter ASCII_AlphaNumeric*)
{ /** <?php return "core/$type" ?> */ return 'core/' + type }
In this construction we still do have some basic processing but it's nothing more than adding a core/
prefix - no conditionals are happening - instead they end up in the generated parser. We can talk about optimizations if they arise, but on the other hand, this is generating a parser which memoizes failed branches so we have a very low penalty for backing up and trying the second branch.
The ultimate goal is generating a grammar with zero code in it - of course we aren't there now, but it's good to highly vet new code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see what you're saying. That's a much nicer way of doing it, added in 5ebaf95.
Also, add a test that the parser parses non-namespaced block names.
blocks/api/serializer.js
Outdated
@@ -178,6 +178,10 @@ export function getCommentDelimitedContent( blockName, attributes, content ) { | |||
? serializeAttributes( attributes ) + ' ' | |||
: ''; | |||
|
|||
if ( blockName.startsWith( 'core/' ) ) { | |||
blockName = blockName.substr( 5 ); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you be happy using a style that doesn't mutate our variables? that is, instead of changing mid-function we start with a parameter and acknowledge that it needs processing via an appropriate name, then binding once the final value for that name?
getCommentDelimitedContent( rawBlockName, attributes, content ) {
…
// strip core blocks of their namespace prefix
const blockName = rawBlockName.startsWith( 'core/' )
? rawBlockName.substr( 5 )
: rawBlockName;
…
}
also, there's nothing wrong with it, though it seems like I'm not as used to seeing String.prototype.substr()
as frequently as String.prototype.slice()
- and I think the behaviors are the same when only one parameter is supplied. thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, I'm cool with these changes.
I probably defaulted to substr()
because of PHP, but slice()
is clearly the default option in the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @pento - I'm a bit intimidated asking for a style change, so thank for being so gracious with it!
@mtias did we have people experience QoL issues when working with the |
How so? You can easily search for Regarding the developer experience, that is a separate aspect. We are already treating core blocks differently in the className generation. For creating blocks, we have warnings if you don't use a namespace. If we have a tool to generate blocks from something like the command line, we'd handle it there as well. Basically, |
oh yeah, good point!
If this message is discoverable and clearly written then that alleviates my concern. I would want to make that a priority though so when someone starts playing around they don't have to go looking around for why their thing is broken. on that note, and marginally related to this point: I recently realized that it was confusing to me what "category"s were available and what "icon"s - would be cool to have more interactive/helpful warnings "from the system" while developing - a live linter for stuff like this and more |
Minor inconsistency I noticed - the parser allows for |
there wasn't a decision about this; it's there because we didn't have a norm. let's follow-up in a separate PR so we can establish what actually ought to be there. let's find some way to document that rule somewhere other than the code itself, keep from more "the implementation is the specification" good catch @pento |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me from reviewing the grammar - I love having more people working on it - thanks a bunch for doing this @pento!
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
* master: (45 commits) Parser: Hide the core namespace in serialised data (WordPress#2950) fix: Undefined index warning (WordPress#2982) navigation via keydown PHPCS improvements (WordPress#2914) Update/html block description (WordPress#2917) Framework: Merge EditorSettingsProvider into EditorProvider Framework: Move the store initialization to a dedicated component Rotate header ellipsis to match block ellipsis add/459: Added support for ins, sub and sup Refresh nonce with heartbeat (WordPress#2790) Paste: add table support Bump version to 1.4.0. (WordPress#2954) Blocks: The custom classname should be persisted properly in the block's comment Editor: Fix the scroll position when reordering blocks Polish spacing in block ellipsis menu (WordPress#2955) Editor: HTML Editor per block (WordPress#2797) Show most frequently used blocks next to inserter (WordPress#2877) add/459: Added light grey background to selected inline boundaries Extend inline boundaries to other elements Move markdown fix to own plugin-compatibility file ...
Description
To make the serialised data format a little easier to read, this PR removes the
core/
namespace from serialised core blocks.Implementation Notes
This is a sizeable PR for a relatively small change. We don't allow block registration without a namespace, so
serializer.js
can simply remove thecore/
namespace from any blocks that have it.Similarly, to maintain backwards compatibility with blocks stored prior to this change,
post.pegjs
can simply assume that any block name without a namespace should have thecore/
namespace.Everything else is updated tests, and the freshly generated
parser.php
.