Skip to content

Commit

Permalink
Parser: Allow empty attributes in default parsers
Browse files Browse the repository at this point in the history
Since the introduction of the default parser in #8083 we have had a
subtle bug in the parsing which failed when empty attributes were
specified in a block's comment delimiter - `{}`

The absense of attributes was fine but _empty_ attributes were a
failure. This is due to using `+?` in the RegExp tokenizer instead of
using `*?` (which allows for no inner content in the JSON string).

This patch updates the quantifier to restore functionality and fix the
bug. This didn't appear in practice because we don't intentionally set
`{}` as the attributes - the serializer drops it altogther, and our
tests didn't catch it for similar reasons.
  • Loading branch information
dmsnell committed Nov 9, 2018
1 parent e0305d7 commit 707d2b2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/block-serialization-default-parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ function next_token() {
* match back in PHP to see which one it was.
*/
$has_match = preg_match(
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:[^}]+|}+(?=})|(?!}\s+-->).)+?}\s+)?(?<void>\/)?-->/s',
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:[^}]+|}+(?=})|(?!}\s+-->).)*?}\s+)?(?<void>\/)?-->/s',
$this->document,
$matches,
PREG_OFFSET_CAPTURE,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-serialization-default-parser/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let document;
let offset;
let output;
let stack;
const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:[^}]+|}+(?=})|(?!}\s+-->)[^])+?}\s+)?(\/)?-->/g;
const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:[^}]+|}+(?=})|(?!}\s+-->)[^])*?}\s+)?(\/)?-->/g;

function Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {
return {
Expand Down

0 comments on commit 707d2b2

Please sign in to comment.