-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
There are currently two parsers being used: one in PegJS (in JS) and regular expressions (in the PHP do_blocks
and parse_block_attributes
functions). Has there been any investigation into re-using the PegJS grammar directly in PHP? For example, php-pegjs. This would be important as the attributes get more complex, such as in implementing dynamic blocks (widgets, #1011).
In that regard, the serialization logic is currently limited to strings that lack any special characters. If a block attempts to serialize a string that includes -->
or "
then the parser would break. Likewise, if an attribute contains an object then it currently gets encoded as [object Object]
and if it is an integer or a boolean, then it gets converted into strings.
I'm also aware of #988, but it doesn't change how attributes get serialized. To me it seems like there needs to be more robust handling of serializing attribute values that need special encoding. While the goal of being human-readable is important, it's probably less important than having data that isn't easily corrupted. This problem was tackled in the Shortcake plugin by URL-encoding such values: wp-shortcake/shortcake#496
Nevertheless, to preserve the original attribute value's data type as well as to escape the value during serialization, perhaps the value should be serialized as JSON and then URL-encoded. This is the approach that the JS Widgets plugin took when porting making widgets available as shortcodes: xwp/wp-js-widgets#32
The attributes encoded with JSON could be prefixed with encoded:
. For example:
<!-- wp:example/foo encoded:bar="%7B%22baz%22%3A%5B%22qux%22%5D%7D" --><!-- /wp:example/foo -->
If the parser can be made more robust than to just rely on regular expressions, however, then we could just encode straight JSON as the values.
<!-- wp:example/foo bar={"baz":["qux"]} --><!-- /wp:example/foo -->
Note here that no double-quotes are used for the value because it's encoding an object. Similarly, integers could be unquoted and as well as booleans:
<!-- wp:example/foo someString="a \"great\" quote" --><!-- /wp:example/foo -->
<!-- wp:example/foo someNumber=1.243513 --><!-- /wp:example/foo -->
<!-- wp:example/foo someBool=false --><!-- /wp:example/foo -->
<!-- wp:example/foo someArray=[1,2,3] --><!-- /wp:example/foo -->