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: Store attributes in unescaped JSON format #1213

Merged
merged 14 commits into from
Jun 26, 2017

Conversation

dmsnell
Copy link
Member

@dmsnell dmsnell commented Jun 16, 2017

@see #1088

Previously we borrowed the name="value" format of tag attributes from
HTML. This caused some headache with blocks because of the different
ways we are wanting to save data in the block comment headers, not the
least of which reason is due to how data must or need not be escaped.

In this change we overhaul the format of the data to allow for (mostly)
unescaped JSON data in the block comment headers. Minimal escaping
exists to prevent breaking the HTML comment and to prevent
poorly-implemented tools from messing up the comments as well.

@dmsnell dmsnell added the [Status] In Progress Tracking issues with work in progress label Jun 16, 2017
const o = {};
o[ key ] = untransformValue( value );
return o;
function maybeJSON( s ) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we report an error somehow?

Copy link
Member Author

Choose a reason for hiding this comment

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

right now the error is reported as a null value. many things, including the string null is valid JSON. in that sense, I would like to propose that we only accept JSON objects, which would dramatically simplify the parser and semantics and serializer.

that would mean null can also signify an error, or alternatively, on error we could send the string of the content which failed to parse.

@nb
Copy link
Member

nb commented Jun 16, 2017

Looks good, but it will break everything. Can we keep the old parser for a bit and use it as a fallback? On first save the format will be converted by the new serializer.

@dmsnell
Copy link
Member Author

dmsnell commented Jun 17, 2017

Looks good, but it will break everything. Can we keep the old parser for a bit and use it as a fallback? On first save the format will be converted by the new serializer.

thought this was why we are working on the prototype… 😉

@westonruter
Copy link
Member

Beware. I'm rebasing this now to remove the merge commit.

@westonruter
Copy link
Member

@dmsnell now that the plugin was just released on WordPress.org, the back-compat for attributes probably should be added.

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from 0b00150 to b6411b0 Compare June 17, 2017 14:17
@westonruter
Copy link
Member

Rebased. Former HEAD is 0b00150.

lib/blocks.php Outdated
@@ -102,7 +103,7 @@ function do_blocks( $content ) {
global $wp_registered_blocks;

// Extract the blocks from the post content.
$matcher = '/<!--\s*wp:([a-z](?:[a-z0-9\/]+)*)\s+((?:(?!-->).)*)\s*\/?-->(?:.*?<!--\s*\/wp:\g1\s+-->)?/';
$matcher = '/<!--\s*wp:([a-z](?:[a-z0-9\/]+)*)\s+((?:(?!-->).)*?)\s*\/?-->(?:.*?<!--\s*\/wp:\g1\s+-->)?/';
Copy link
Member Author

Choose a reason for hiding this comment

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

we need to be extremely careful with combining wildcards. those +)* are troublesome, and I don't see why we are introducing *? - was it not working without the ?? @westonruter

Copy link
Member

Choose a reason for hiding this comment

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

@dmsnell The reason for this is the matches for a block like <!-- wp:core/latestposts {"poststoshow":3} /-->. Given that example, without the change on this line it was resulting in a match like {"poststoshow":3} /, which naturally is invalid JSON. So I was debating whether to rtrim the / or to make the pattern matching non-greedy. I decided on the latter, but you think the former is better then I'm happy to change.

@dmsnell
Copy link
Member Author

dmsnell commented Jun 17, 2017

@nb @jasmussen and @westonruter - I'm a bit mixed on "this will break things" don't we want to block all new usages of named attributes? should we really be providing a fallback to protect usage of the alpha release?

@BE-Webdesign
Copy link
Contributor

BE-Webdesign commented Jun 17, 2017

Break it while we still can. I think the JSON pattern is a lot nicer.

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from b6411b0 to cf056f6 Compare June 19, 2017 03:59
@westonruter
Copy link
Member

Rebased again. Former HEAD is b6411b0.

Build has been fixed. Tests have been updated.

I'd like to get this merged so we can move forward with dynamic blocks.

@youknowriad
Copy link
Contributor

Yes, let's break the current blocks and release a plugin update quickly.

@nylen
Copy link
Member

nylen commented Jun 20, 2017

I think we should do this but I am a bit mixed on the breakage as well. I am happy to let someone else make the call here, I will just note that it shouldn't be too difficult to insert a temporary hack to convert string attributes to the new format.

@jasmussen
Copy link
Contributor

I can understand the empathy for breakage, but we can't let the fact that we released the plugin into the repository hold us from making these necessary improvements, or even moving fast. This breakage should be expected under the beta moniker, and we even have an alert box on the publish button that warns that this a beta plugin.

We need to stay nimble, and not worry too much about breakage, especially in this phase, or we might get mired in additional challenges.

@westonruter
Copy link
Member

I'll remove my back-compat PHP parsing and rebase.

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from 79f3684 to 0004aaf Compare June 20, 2017 19:48
@westonruter
Copy link
Member

Rebased. Former HEAD was 79f3684.

'<p>The goal of this new editor is to make adding rich content to WordPress simple and enjoyable. This whole post is composed of <em>pieces of content</em>—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.</p>',
'<!-- /wp:core/text -->',

'<!-- wp:core/text -->',
'<p>What you are reading now is a <strong>text block</strong>, the most basic block of all. The text block has its own controls to be moved freely around the post...</p>',
'<!-- /wp:core/text -->',

'<!-- wp:core/text align="right" -->',
'<!-- wp:core/text { "align": "right" } -->',
Copy link
Member

Choose a reason for hiding this comment

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

Since default serialization wouldn't include whitespace between braces and after colon, should we similarly not do so here?

Copy link
Member

Choose a reason for hiding this comment

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

Since the post-content.js here is written by hand, it seems useful to have more spaces for readability when editing.

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from bc9ee22 to 5d5c07a Compare June 21, 2017 16:31
@westonruter
Copy link
Member

Rebased to fix merge conflict. Former HEAD was bc9ee226.

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from 5d5c07a to 4dd0f22 Compare June 23, 2017 03:08
@westonruter
Copy link
Member

Re-re-rebased to fix merge conflicts. Former HEAD was 5d5c07a.

@mtias can this be merged?

@westonruter westonruter added [Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f and removed [Status] In Progress Tracking issues with work in progress labels Jun 23, 2017
@westonruter
Copy link
Member

Resolving conflicts and re-basing again…

@westonruter westonruter force-pushed the parser/unescaped-json-attributes branch from bdf86f0 to f5900f6 Compare June 25, 2017 06:49
@westonruter
Copy link
Member

westonruter commented Jun 25, 2017

Rebased. Former HEAD was bdf86f0.
Rebased. Former HEAD was f5900f6 (@dmsnell)

dmsnell and others added 13 commits June 26, 2017 18:17
Previously we borrowed the `name="value"` format of tag attributes from
HTML. This caused some headache with blocks because of the different
ways we are wanting to save data in the block comment headers, not the
least of which reason is due to how data must or need not be escaped.

In this change we overhaul the format of the data to allow for (mostly)
unescaped JSON data in the block comment headers. Minimal escaping
exists to prevent breaking the HTML comment and to prevent
poorly-implemented tools from messing up the comments as well.
@dmsnell dmsnell force-pushed the parser/unescaped-json-attributes branch from f5900f6 to f63bbca Compare June 26, 2017 16:17
@dmsnell dmsnell merged commit 49cfd17 into master Jun 26, 2017
@dmsnell dmsnell deleted the parser/unescaped-json-attributes branch June 26, 2017 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants