From 753aab9ba36f89849b653c5472d9462c5d00f44b Mon Sep 17 00:00:00 2001 From: James Nylen Date: Mon, 12 Jun 2017 20:49:17 -0400 Subject: [PATCH 01/18] Introduce `php-pegjs` Add `php-pegjs` and downgrade `pegjs` version accordingly Add script to generate PHP parser Update PEG grammar with PHP and JS code Run syntax check against php-pegjs parser Exclude generated PHP parser from phpcs checks Fix lint errors --- .gitignore | 1 + .travis.yml | 6 +++ bin/create-php-parser.js | 17 ++++++ blocks/api/post.pegjs | 112 +++++++++++++++++++++++++++++++-------- package.json | 2 + phpcs.xml | 1 + 6 files changed, 116 insertions(+), 23 deletions(-) create mode 100755 bin/create-php-parser.js diff --git a/.gitignore b/.gitignore index 9d58efe7906a66..f29182bf5c7f0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules build +lib/parser.php gutenberg.pot .vscode *.log diff --git a/.travis.yml b/.travis.yml index 9861d4b2e3d6d6..aa0004d671e5ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,8 +50,14 @@ before_script: script: - | if [[ ! -z "$WP_VERSION" ]] ; then + # Run the build because otherwise there will be a bunch of warnings about + # failed `stat` calls from `filemtime()`. npm install || exit 1 npm run build || exit 1 + # Syntax check for php-pegjs parser + node bin/create-php-parser.js || exit 1 + php lib/parser.php || exit 1 + # Run PHPUnit tests phpunit || exit 1 WP_MULTISITE=1 phpunit || exit 1 fi diff --git a/bin/create-php-parser.js b/bin/create-php-parser.js new file mode 100755 index 00000000000000..6660ced9086631 --- /dev/null +++ b/bin/create-php-parser.js @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +const pegjs = require( 'pegjs' ); +const phppegjs = require( 'php-pegjs' ); +const fs = require( 'fs' ); +const path = require( 'path' ); + +const peg = fs.readFileSync( 'blocks/api/post.pegjs', 'utf8' ); +const parser = pegjs.buildParser( + peg, + { plugins: [ phppegjs ] } +); + +fs.writeFileSync( + path.join( __dirname, '..', 'lib', 'parser.php' ), + parser +); diff --git a/blocks/api/post.pegjs b/blocks/api/post.pegjs index d3e6a62761addc..2bb8ec7df799c1 100644 --- a/blocks/api/post.pegjs +++ b/blocks/api/post.pegjs @@ -1,5 +1,10 @@ { +/** **/ + function maybeJSON( s ) { try { return JSON.parse( s ); @@ -22,25 +27,63 @@ WP_Block / WP_Block_Html WP_Block_Void - = "" - { return { - blockName: blockName, - attrs: attrs, - rawContent: '' - } } + = "" + { + /** $blockName, + 'attrs' => $attrs, + 'rawContent' => '', + ); + ?> **/ + + return { + blockName: blockName, + attrs: attrs, + rawContent: '' + }; + } WP_Block_Balanced - = s:WP_Block_Start ts:(!WP_Block_End c:Any { return c })* e:WP_Block_End - & { return s.blockName === e.blockName } - { return { - blockName: s.blockName, - attrs: s.attrs, - rawContent: ts.join( '' ), - } } + = s:WP_Block_Start ts:(!WP_Block_End c:Any { + /** **/ + return c; + })* e:WP_Block_End & { + /** **/ + return s.blockName === e.blockName; + } + { + /** $s['blockName'], + 'attrs' => $s['attrs'], + 'rawContent' => implode( '', $ts ), + ); + ?> **/ + + return { + blockName: s.blockName, + attrs: s.attrs, + rawContent: ts.join( '' ) + }; + } WP_Block_Html - = ts:(!WP_Block_Balanced !WP_Block_Void c:Any { return c })+ + = ts:(!WP_Block_Balanced !WP_Block_Void c:Any { + /** **/ + return c; + })+ { + /** array(), + 'rawContent' => implode( '', $ts ), + ); + ?> **/ + return { attrs: {}, rawContent: ts.join( '' ) @@ -48,24 +91,47 @@ WP_Block_Html } WP_Block_Start - = "" - { return { - blockName: blockName, - attrs: attrs - } } + = "" + { + /** $blockName, + 'attrs' => $attrs, + ); + ?> **/ + + return { + blockName: blockName, + attrs: attrs + }; + } WP_Block_End = "" - { return { - blockName: blockName - } } + { + /** $blockName, + ); + ?> **/ + + return { + blockName: blockName + }; + } WP_Block_Name = $(ASCII_Letter (ASCII_AlphaNumeric / "/" ASCII_AlphaNumeric)*) WP_Block_Attributes = attrs:$("{" (!("}" WS+ """/"? "-->") .)* "}") - { return maybeJSON( attrs ) } + { + /** **/ + return maybeJSON( attrs ); + } ASCII_AlphaNumeric = ASCII_Letter diff --git a/package.json b/package.json index 2b63e2e7f842d5..258e3e54b1f9ab 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,8 @@ "lodash": "^4.17.4", "moment": "^2.18.1", "moment-timezone": "^0.5.13", + "pegjs": "0.8.0", + "php-pegjs": "git+https://github.com/nylen/php-pegjs.git#add/php-blocks", "react": "^15.5.4", "react-autosize-textarea": "^0.4.2", "react-click-outside": "^2.3.0", diff --git a/phpcs.xml b/phpcs.xml index 561c325c689e8d..9086dccef78637 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -10,6 +10,7 @@ gutenberg.php ./lib + ./lib/parser.php ./phpunit ./bin From 2533fc8618655d860f77761f371dd76e51731d7d Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 22 Jun 2017 12:05:35 -0500 Subject: [PATCH 02/18] Change php-pegjs to phpegjs (forked version) --- bin/create-php-parser.js | 7 ++++--- package.json | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bin/create-php-parser.js b/bin/create-php-parser.js index 6660ced9086631..1f3298bad6c7db 100755 --- a/bin/create-php-parser.js +++ b/bin/create-php-parser.js @@ -1,14 +1,15 @@ #!/usr/bin/env node const pegjs = require( 'pegjs' ); -const phppegjs = require( 'php-pegjs' ); +const phpegjs = require( 'phpegjs' ); const fs = require( 'fs' ); const path = require( 'path' ); const peg = fs.readFileSync( 'blocks/api/post.pegjs', 'utf8' ); -const parser = pegjs.buildParser( + +const parser = pegjs.generate( peg, - { plugins: [ phppegjs ] } + { plugins: [ phpegjs ] } ); fs.writeFileSync( diff --git a/package.json b/package.json index 258e3e54b1f9ab..62f026bb4a11a3 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "lodash": "^4.17.4", "moment": "^2.18.1", "moment-timezone": "^0.5.13", - "pegjs": "0.8.0", - "php-pegjs": "git+https://github.com/nylen/php-pegjs.git#add/php-blocks", + "pegjs": "0.10.0", + "phpegjs": "1.0.0-beta1", "react": "^15.5.4", "react-autosize-textarea": "^0.4.2", "react-click-outside": "^2.3.0", From 01e661fbf5d045edfe8b54e692506160b5339525 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 23 Jun 2017 03:01:16 -0500 Subject: [PATCH 03/18] Add parser output to full-content test fixtures --- blocks/test/fixtures/README.md | 17 +++++----- .../fixtures/core-button-center.parsed.json | 13 ++++++++ blocks/test/fixtures/core-code.parsed.json | 11 +++++++ blocks/test/fixtures/core-embed.parsed.json | 13 ++++++++ .../fixtures/core-embedanimoto.parsed.json | 13 ++++++++ .../fixtures/core-embedcloudup.parsed.json | 13 ++++++++ .../core-embedcollegehumor.parsed.json | 13 ++++++++ .../core-embeddailymotion.parsed.json | 13 ++++++++ .../fixtures/core-embedfacebook.parsed.json | 13 ++++++++ .../fixtures/core-embedflickr.parsed.json | 13 ++++++++ .../fixtures/core-embedfunnyordie.parsed.json | 13 ++++++++ .../test/fixtures/core-embedhulu.parsed.json | 13 ++++++++ .../test/fixtures/core-embedimgur.parsed.json | 13 ++++++++ .../fixtures/core-embedinstagram.parsed.json | 13 ++++++++ .../test/fixtures/core-embedissuu.parsed.json | 13 ++++++++ .../core-embedkickstarter.parsed.json | 13 ++++++++ .../fixtures/core-embedmeetupcom.parsed.json | 13 ++++++++ .../fixtures/core-embedmixcloud.parsed.json | 13 ++++++++ .../core-embedphotobucket.parsed.json | 13 ++++++++ .../fixtures/core-embedpolldaddy.parsed.json | 13 ++++++++ .../fixtures/core-embedreddit.parsed.json | 13 ++++++++ .../core-embedreverbnation.parsed.json | 13 ++++++++ .../fixtures/core-embedscreencast.parsed.json | 13 ++++++++ .../fixtures/core-embedscribd.parsed.json | 13 ++++++++ .../fixtures/core-embedslideshare.parsed.json | 13 ++++++++ .../fixtures/core-embedsmugmug.parsed.json | 13 ++++++++ .../fixtures/core-embedsoundcloud.parsed.json | 13 ++++++++ .../fixtures/core-embedspeaker.parsed.json | 13 ++++++++ .../fixtures/core-embedspotify.parsed.json | 13 ++++++++ .../test/fixtures/core-embedted.parsed.json | 13 ++++++++ .../fixtures/core-embedtumblr.parsed.json | 13 ++++++++ .../fixtures/core-embedtwitter.parsed.json | 13 ++++++++ .../fixtures/core-embedvideopress.parsed.json | 13 ++++++++ .../test/fixtures/core-embedvimeo.parsed.json | 13 ++++++++ .../test/fixtures/core-embedvine.parsed.json | 13 ++++++++ .../fixtures/core-embedwordpress.parsed.json | 13 ++++++++ .../core-embedwordpresstv.parsed.json | 13 ++++++++ .../fixtures/core-embedyoutube.parsed.json | 13 ++++++++ .../test/fixtures/core-freeform.parsed.json | 11 +++++++ blocks/test/fixtures/core-gallery.parsed.json | 11 +++++++ .../fixtures/core-heading-h2-em.parsed.json | 11 +++++++ .../test/fixtures/core-heading-h2.parsed.json | 11 +++++++ .../core-image-center-caption.parsed.json | 13 ++++++++ blocks/test/fixtures/core-image.parsed.json | 11 +++++++ .../fixtures/core-latestposts.parsed.json | 13 ++++++++ blocks/test/fixtures/core-list-ul.parsed.json | 11 +++++++ .../fixtures/core-preformatted.parsed.json | 11 +++++++ .../test/fixtures/core-pullquote.parsed.json | 11 +++++++ .../fixtures/core-quote-style-1.parsed.json | 13 ++++++++ .../fixtures/core-quote-style-2.parsed.json | 13 ++++++++ .../test/fixtures/core-separator.parsed.json | 11 +++++++ blocks/test/fixtures/core-table.parsed.json | 11 +++++++ .../core-text-align-right.parsed.json | 13 ++++++++ blocks/test/full-content.js | 31 ++++++++++++++++--- 54 files changed, 691 insertions(+), 11 deletions(-) create mode 100644 blocks/test/fixtures/core-button-center.parsed.json create mode 100644 blocks/test/fixtures/core-code.parsed.json create mode 100644 blocks/test/fixtures/core-embed.parsed.json create mode 100644 blocks/test/fixtures/core-embedanimoto.parsed.json create mode 100644 blocks/test/fixtures/core-embedcloudup.parsed.json create mode 100644 blocks/test/fixtures/core-embedcollegehumor.parsed.json create mode 100644 blocks/test/fixtures/core-embeddailymotion.parsed.json create mode 100644 blocks/test/fixtures/core-embedfacebook.parsed.json create mode 100644 blocks/test/fixtures/core-embedflickr.parsed.json create mode 100644 blocks/test/fixtures/core-embedfunnyordie.parsed.json create mode 100644 blocks/test/fixtures/core-embedhulu.parsed.json create mode 100644 blocks/test/fixtures/core-embedimgur.parsed.json create mode 100644 blocks/test/fixtures/core-embedinstagram.parsed.json create mode 100644 blocks/test/fixtures/core-embedissuu.parsed.json create mode 100644 blocks/test/fixtures/core-embedkickstarter.parsed.json create mode 100644 blocks/test/fixtures/core-embedmeetupcom.parsed.json create mode 100644 blocks/test/fixtures/core-embedmixcloud.parsed.json create mode 100644 blocks/test/fixtures/core-embedphotobucket.parsed.json create mode 100644 blocks/test/fixtures/core-embedpolldaddy.parsed.json create mode 100644 blocks/test/fixtures/core-embedreddit.parsed.json create mode 100644 blocks/test/fixtures/core-embedreverbnation.parsed.json create mode 100644 blocks/test/fixtures/core-embedscreencast.parsed.json create mode 100644 blocks/test/fixtures/core-embedscribd.parsed.json create mode 100644 blocks/test/fixtures/core-embedslideshare.parsed.json create mode 100644 blocks/test/fixtures/core-embedsmugmug.parsed.json create mode 100644 blocks/test/fixtures/core-embedsoundcloud.parsed.json create mode 100644 blocks/test/fixtures/core-embedspeaker.parsed.json create mode 100644 blocks/test/fixtures/core-embedspotify.parsed.json create mode 100644 blocks/test/fixtures/core-embedted.parsed.json create mode 100644 blocks/test/fixtures/core-embedtumblr.parsed.json create mode 100644 blocks/test/fixtures/core-embedtwitter.parsed.json create mode 100644 blocks/test/fixtures/core-embedvideopress.parsed.json create mode 100644 blocks/test/fixtures/core-embedvimeo.parsed.json create mode 100644 blocks/test/fixtures/core-embedvine.parsed.json create mode 100644 blocks/test/fixtures/core-embedwordpress.parsed.json create mode 100644 blocks/test/fixtures/core-embedwordpresstv.parsed.json create mode 100644 blocks/test/fixtures/core-embedyoutube.parsed.json create mode 100644 blocks/test/fixtures/core-freeform.parsed.json create mode 100644 blocks/test/fixtures/core-gallery.parsed.json create mode 100644 blocks/test/fixtures/core-heading-h2-em.parsed.json create mode 100644 blocks/test/fixtures/core-heading-h2.parsed.json create mode 100644 blocks/test/fixtures/core-image-center-caption.parsed.json create mode 100644 blocks/test/fixtures/core-image.parsed.json create mode 100644 blocks/test/fixtures/core-latestposts.parsed.json create mode 100644 blocks/test/fixtures/core-list-ul.parsed.json create mode 100644 blocks/test/fixtures/core-preformatted.parsed.json create mode 100644 blocks/test/fixtures/core-pullquote.parsed.json create mode 100644 blocks/test/fixtures/core-quote-style-1.parsed.json create mode 100644 blocks/test/fixtures/core-quote-style-2.parsed.json create mode 100644 blocks/test/fixtures/core-separator.parsed.json create mode 100644 blocks/test/fixtures/core-table.parsed.json create mode 100644 blocks/test/fixtures/core-text-align-right.parsed.json diff --git a/blocks/test/fixtures/README.md b/blocks/test/fixtures/README.md index b17018f85c88c2..5321b7f4a037d7 100644 --- a/blocks/test/fixtures/README.md +++ b/blocks/test/fixtures/README.md @@ -6,11 +6,13 @@ and serialization logic. Each test is made up of three fixture files: 1. `fixture-name.html`: The initial post content. -2. `fixture-name.json`: The **expected** parsed representation of the block(s) - inside the post content, along with their attributes and any nested content. - The contents of this file are compared against the **actual** parsed block - object(s). -3. `fixture-name.serialized.html`: The **expected** result of calling +2. `fixture-name.parsed.json`: The **expected** output of the PEG parser for + this content (checked against the **actual** output of both the JS and PHP + versions of the parser). +3. `fixture-name.json`: The **expected** representation of the block(s) inside + the post content, along with their attributes and any nested content. The + contents of this file are compared against the **actual** block object(s). +4. `fixture-name.serialized.html`: The **expected** result of calling `serialize` on the parsed block object(s). The contents of this file are compared against the **actual** re-serialized post content. This final step simulates opening and re-saving a post. @@ -22,8 +24,9 @@ the parsing and serialization of that block. These fixtures must be named like 1. `core-image.html` (or `core-image-specific-test-name.html`). Must contain a `` block. -2. `core-image.json` (or `core-image-specific-test-name.html`). -3. `core-image.serialized.html` (or +2. `core-image.parsed.json` (or `core-image-specific-test-name.parsed.json`). +3. `core-image.json` (or `core-image-specific-test-name.json`). +4. `core-image.serialized.html` (or `core-image-specific-test-name.serialized.html`). Ideally all important attributes and features of the block should be tested diff --git a/blocks/test/fixtures/core-button-center.parsed.json b/blocks/test/fixtures/core-button-center.parsed.json new file mode 100644 index 00000000000000..cd020c352f0380 --- /dev/null +++ b/blocks/test/fixtures/core-button-center.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/button", + "attrs": { + "align": "center" + }, + "rawContent": "\n\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-code.parsed.json b/blocks/test/fixtures/core-code.parsed.json new file mode 100644 index 00000000000000..97bdae206d3142 --- /dev/null +++ b/blocks/test/fixtures/core-code.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/code", + "attrs": {}, + "rawContent": "\n
export default function MyButton() {\n\treturn <Button>Click Me!</Button>;\n}
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embed.parsed.json b/blocks/test/fixtures/core-embed.parsed.json new file mode 100644 index 00000000000000..0d734256c9a0b8 --- /dev/null +++ b/blocks/test/fixtures/core-embed.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embed", + "attrs": { + "url": "https://example.com/" + }, + "rawContent": "\n
\n https://example.com/\n
Embedded content from an example URL
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedanimoto.parsed.json b/blocks/test/fixtures/core-embedanimoto.parsed.json new file mode 100644 index 00000000000000..c0b095ddf03778 --- /dev/null +++ b/blocks/test/fixtures/core-embedanimoto.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedanimoto", + "attrs": { + "url": "https://animoto.com/" + }, + "rawContent": "\n
\n https://animoto.com/\n
Embedded content from animoto
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedcloudup.parsed.json b/blocks/test/fixtures/core-embedcloudup.parsed.json new file mode 100644 index 00000000000000..a5b905ba6b2787 --- /dev/null +++ b/blocks/test/fixtures/core-embedcloudup.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedcloudup", + "attrs": { + "url": "https://cloudup.com/" + }, + "rawContent": "\n
\n https://cloudup.com/\n
Embedded content from cloudup
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedcollegehumor.parsed.json b/blocks/test/fixtures/core-embedcollegehumor.parsed.json new file mode 100644 index 00000000000000..1ea7fe1c308a67 --- /dev/null +++ b/blocks/test/fixtures/core-embedcollegehumor.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedcollegehumor", + "attrs": { + "url": "https://collegehumor.com/" + }, + "rawContent": "\n
\n https://collegehumor.com/\n
Embedded content from collegehumor
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embeddailymotion.parsed.json b/blocks/test/fixtures/core-embeddailymotion.parsed.json new file mode 100644 index 00000000000000..116c47ca87ab79 --- /dev/null +++ b/blocks/test/fixtures/core-embeddailymotion.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embeddailymotion", + "attrs": { + "url": "https://dailymotion.com/" + }, + "rawContent": "\n
\n https://dailymotion.com/\n
Embedded content from dailymotion
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedfacebook.parsed.json b/blocks/test/fixtures/core-embedfacebook.parsed.json new file mode 100644 index 00000000000000..9748ecffcdaade --- /dev/null +++ b/blocks/test/fixtures/core-embedfacebook.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedfacebook", + "attrs": { + "url": "https://facebook.com/" + }, + "rawContent": "\n
\n https://facebook.com/\n
Embedded content from facebook
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedflickr.parsed.json b/blocks/test/fixtures/core-embedflickr.parsed.json new file mode 100644 index 00000000000000..0e29813f495e1a --- /dev/null +++ b/blocks/test/fixtures/core-embedflickr.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedflickr", + "attrs": { + "url": "https://flickr.com/" + }, + "rawContent": "\n
\n https://flickr.com/\n
Embedded content from flickr
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedfunnyordie.parsed.json b/blocks/test/fixtures/core-embedfunnyordie.parsed.json new file mode 100644 index 00000000000000..f187b9afb40ec7 --- /dev/null +++ b/blocks/test/fixtures/core-embedfunnyordie.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedfunnyordie", + "attrs": { + "url": "https://funnyordie.com/" + }, + "rawContent": "\n
\n https://funnyordie.com/\n
Embedded content from funnyordie
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedhulu.parsed.json b/blocks/test/fixtures/core-embedhulu.parsed.json new file mode 100644 index 00000000000000..dbd6907efca23e --- /dev/null +++ b/blocks/test/fixtures/core-embedhulu.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedhulu", + "attrs": { + "url": "https://hulu.com/" + }, + "rawContent": "\n
\n https://hulu.com/\n
Embedded content from hulu
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedimgur.parsed.json b/blocks/test/fixtures/core-embedimgur.parsed.json new file mode 100644 index 00000000000000..5b816b79555752 --- /dev/null +++ b/blocks/test/fixtures/core-embedimgur.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedimgur", + "attrs": { + "url": "https://imgur.com/" + }, + "rawContent": "\n
\n https://imgur.com/\n
Embedded content from imgur
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedinstagram.parsed.json b/blocks/test/fixtures/core-embedinstagram.parsed.json new file mode 100644 index 00000000000000..279808a9182c63 --- /dev/null +++ b/blocks/test/fixtures/core-embedinstagram.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedinstagram", + "attrs": { + "url": "https://instagram.com/" + }, + "rawContent": "\n
\n https://instagram.com/\n
Embedded content from instagram
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedissuu.parsed.json b/blocks/test/fixtures/core-embedissuu.parsed.json new file mode 100644 index 00000000000000..72eb263bb9307a --- /dev/null +++ b/blocks/test/fixtures/core-embedissuu.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedissuu", + "attrs": { + "url": "https://issuu.com/" + }, + "rawContent": "\n
\n https://issuu.com/\n
Embedded content from issuu
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedkickstarter.parsed.json b/blocks/test/fixtures/core-embedkickstarter.parsed.json new file mode 100644 index 00000000000000..a2d3100d7795b9 --- /dev/null +++ b/blocks/test/fixtures/core-embedkickstarter.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedkickstarter", + "attrs": { + "url": "https://kickstarter.com/" + }, + "rawContent": "\n
\n https://kickstarter.com/\n
Embedded content from kickstarter
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedmeetupcom.parsed.json b/blocks/test/fixtures/core-embedmeetupcom.parsed.json new file mode 100644 index 00000000000000..5e9bcfddf922f4 --- /dev/null +++ b/blocks/test/fixtures/core-embedmeetupcom.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedmeetupcom", + "attrs": { + "url": "https://meetupcom.com/" + }, + "rawContent": "\n
\n https://meetupcom.com/\n
Embedded content from meetupcom
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedmixcloud.parsed.json b/blocks/test/fixtures/core-embedmixcloud.parsed.json new file mode 100644 index 00000000000000..3ac3bafb51ca3e --- /dev/null +++ b/blocks/test/fixtures/core-embedmixcloud.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedmixcloud", + "attrs": { + "url": "https://mixcloud.com/" + }, + "rawContent": "\n
\n https://mixcloud.com/\n
Embedded content from mixcloud
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedphotobucket.parsed.json b/blocks/test/fixtures/core-embedphotobucket.parsed.json new file mode 100644 index 00000000000000..1ce02d240b07b7 --- /dev/null +++ b/blocks/test/fixtures/core-embedphotobucket.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedphotobucket", + "attrs": { + "url": "https://photobucket.com/" + }, + "rawContent": "\n
\n https://photobucket.com/\n
Embedded content from photobucket
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedpolldaddy.parsed.json b/blocks/test/fixtures/core-embedpolldaddy.parsed.json new file mode 100644 index 00000000000000..1f7144beef8f5e --- /dev/null +++ b/blocks/test/fixtures/core-embedpolldaddy.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedpolldaddy", + "attrs": { + "url": "https://polldaddy.com/" + }, + "rawContent": "\n
\n https://polldaddy.com/\n
Embedded content from polldaddy
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedreddit.parsed.json b/blocks/test/fixtures/core-embedreddit.parsed.json new file mode 100644 index 00000000000000..7f80f72d64a393 --- /dev/null +++ b/blocks/test/fixtures/core-embedreddit.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedreddit", + "attrs": { + "url": "https://reddit.com/" + }, + "rawContent": "\n
\n https://reddit.com/\n
Embedded content from reddit
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedreverbnation.parsed.json b/blocks/test/fixtures/core-embedreverbnation.parsed.json new file mode 100644 index 00000000000000..729ca8cfa32f02 --- /dev/null +++ b/blocks/test/fixtures/core-embedreverbnation.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedreverbnation", + "attrs": { + "url": "https://reverbnation.com/" + }, + "rawContent": "\n
\n https://reverbnation.com/\n
Embedded content from reverbnation
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedscreencast.parsed.json b/blocks/test/fixtures/core-embedscreencast.parsed.json new file mode 100644 index 00000000000000..a57dfd4c907bc7 --- /dev/null +++ b/blocks/test/fixtures/core-embedscreencast.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedscreencast", + "attrs": { + "url": "https://screencast.com/" + }, + "rawContent": "\n
\n https://screencast.com/\n
Embedded content from screencast
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedscribd.parsed.json b/blocks/test/fixtures/core-embedscribd.parsed.json new file mode 100644 index 00000000000000..3233bd9ca3cf96 --- /dev/null +++ b/blocks/test/fixtures/core-embedscribd.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedscribd", + "attrs": { + "url": "https://scribd.com/" + }, + "rawContent": "\n
\n https://scribd.com/\n
Embedded content from scribd
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedslideshare.parsed.json b/blocks/test/fixtures/core-embedslideshare.parsed.json new file mode 100644 index 00000000000000..05fcacb9ca36be --- /dev/null +++ b/blocks/test/fixtures/core-embedslideshare.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedslideshare", + "attrs": { + "url": "https://slideshare.com/" + }, + "rawContent": "\n
\n https://slideshare.com/\n
Embedded content from slideshare
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedsmugmug.parsed.json b/blocks/test/fixtures/core-embedsmugmug.parsed.json new file mode 100644 index 00000000000000..776d15c154714b --- /dev/null +++ b/blocks/test/fixtures/core-embedsmugmug.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedsmugmug", + "attrs": { + "url": "https://smugmug.com/" + }, + "rawContent": "\n
\n https://smugmug.com/\n
Embedded content from smugmug
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedsoundcloud.parsed.json b/blocks/test/fixtures/core-embedsoundcloud.parsed.json new file mode 100644 index 00000000000000..9d1b3986079ede --- /dev/null +++ b/blocks/test/fixtures/core-embedsoundcloud.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedsoundcloud", + "attrs": { + "url": "https://soundcloud.com/" + }, + "rawContent": "\n
\n https://soundcloud.com/\n
Embedded content from soundcloud
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedspeaker.parsed.json b/blocks/test/fixtures/core-embedspeaker.parsed.json new file mode 100644 index 00000000000000..e890aa7a4f4c96 --- /dev/null +++ b/blocks/test/fixtures/core-embedspeaker.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedspeaker", + "attrs": { + "url": "https://speaker.com/" + }, + "rawContent": "\n
\n https://speaker.com/\n
Embedded content from speaker
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedspotify.parsed.json b/blocks/test/fixtures/core-embedspotify.parsed.json new file mode 100644 index 00000000000000..375c209a131b47 --- /dev/null +++ b/blocks/test/fixtures/core-embedspotify.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedspotify", + "attrs": { + "url": "https://spotify.com/" + }, + "rawContent": "\n
\n https://spotify.com/\n
Embedded content from spotify
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedted.parsed.json b/blocks/test/fixtures/core-embedted.parsed.json new file mode 100644 index 00000000000000..c5bd17ffbdd7ca --- /dev/null +++ b/blocks/test/fixtures/core-embedted.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedted", + "attrs": { + "url": "https://ted.com/" + }, + "rawContent": "\n
\n https://ted.com/\n
Embedded content from ted
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedtumblr.parsed.json b/blocks/test/fixtures/core-embedtumblr.parsed.json new file mode 100644 index 00000000000000..397025f1920cbf --- /dev/null +++ b/blocks/test/fixtures/core-embedtumblr.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedtumblr", + "attrs": { + "url": "https://tumblr.com/" + }, + "rawContent": "\n
\n https://tumblr.com/\n
Embedded content from tumblr
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedtwitter.parsed.json b/blocks/test/fixtures/core-embedtwitter.parsed.json new file mode 100644 index 00000000000000..d794a60a801528 --- /dev/null +++ b/blocks/test/fixtures/core-embedtwitter.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedtwitter", + "attrs": { + "url": "https://twitter.com/automattic" + }, + "rawContent": "\n
\n https://twitter.com/automattic\n
We are Automattic
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedvideopress.parsed.json b/blocks/test/fixtures/core-embedvideopress.parsed.json new file mode 100644 index 00000000000000..ef20f744c7f315 --- /dev/null +++ b/blocks/test/fixtures/core-embedvideopress.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedvideopress", + "attrs": { + "url": "https://videopress.com/" + }, + "rawContent": "\n
\n https://videopress.com/\n
Embedded content from videopress
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedvimeo.parsed.json b/blocks/test/fixtures/core-embedvimeo.parsed.json new file mode 100644 index 00000000000000..29b1fc473aeb44 --- /dev/null +++ b/blocks/test/fixtures/core-embedvimeo.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedvimeo", + "attrs": { + "url": "https://vimeo.com/" + }, + "rawContent": "\n
\n https://vimeo.com/\n
Embedded content from vimeo
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedvine.parsed.json b/blocks/test/fixtures/core-embedvine.parsed.json new file mode 100644 index 00000000000000..341842e5f2c39c --- /dev/null +++ b/blocks/test/fixtures/core-embedvine.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedvine", + "attrs": { + "url": "https://vine.com/" + }, + "rawContent": "\n
\n https://vine.com/\n
Embedded content from vine
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedwordpress.parsed.json b/blocks/test/fixtures/core-embedwordpress.parsed.json new file mode 100644 index 00000000000000..936b23941f30ea --- /dev/null +++ b/blocks/test/fixtures/core-embedwordpress.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedwordpress", + "attrs": { + "url": "https://wordpress.com/" + }, + "rawContent": "\n
\n https://wordpress.com/\n
Embedded content from WordPress
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedwordpresstv.parsed.json b/blocks/test/fixtures/core-embedwordpresstv.parsed.json new file mode 100644 index 00000000000000..7fde2e5caa180a --- /dev/null +++ b/blocks/test/fixtures/core-embedwordpresstv.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedwordpresstv", + "attrs": { + "url": "https://wordpresstv.com/" + }, + "rawContent": "\n
\n https://wordpresstv.com/\n
Embedded content from wordpresstv
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embedyoutube.parsed.json b/blocks/test/fixtures/core-embedyoutube.parsed.json new file mode 100644 index 00000000000000..4fcbe733b388af --- /dev/null +++ b/blocks/test/fixtures/core-embedyoutube.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/embedyoutube", + "attrs": { + "url": "https://youtube.com/" + }, + "rawContent": "\n
\n https://youtube.com/\n
Embedded content from youtube
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-freeform.parsed.json b/blocks/test/fixtures/core-freeform.parsed.json new file mode 100644 index 00000000000000..f91546825d3fc3 --- /dev/null +++ b/blocks/test/fixtures/core-freeform.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/freeform", + "attrs": {}, + "rawContent": "\nTesting freeform block with some\n
\n\tHTML content\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-gallery.parsed.json b/blocks/test/fixtures/core-gallery.parsed.json new file mode 100644 index 00000000000000..287dc97f533a9d --- /dev/null +++ b/blocks/test/fixtures/core-gallery.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/gallery", + "attrs": {}, + "rawContent": "\n
\n\t\n\t\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-heading-h2-em.parsed.json b/blocks/test/fixtures/core-heading-h2-em.parsed.json new file mode 100644 index 00000000000000..171d877a3b6a1a --- /dev/null +++ b/blocks/test/fixtures/core-heading-h2-em.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/heading", + "attrs": {}, + "rawContent": "\n

The Inserter Tool

\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-heading-h2.parsed.json b/blocks/test/fixtures/core-heading-h2.parsed.json new file mode 100644 index 00000000000000..57936edd43d115 --- /dev/null +++ b/blocks/test/fixtures/core-heading-h2.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/heading", + "attrs": {}, + "rawContent": "\n

A picture is worth a thousand words, or so the saying goes

\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-image-center-caption.parsed.json b/blocks/test/fixtures/core-image-center-caption.parsed.json new file mode 100644 index 00000000000000..93a717c98a0a59 --- /dev/null +++ b/blocks/test/fixtures/core-image-center-caption.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/image", + "attrs": { + "align": "center" + }, + "rawContent": "\n
Give it a try. Press the "really wide" button on the image toolbar.
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-image.parsed.json b/blocks/test/fixtures/core-image.parsed.json new file mode 100644 index 00000000000000..9e4faa2d35e0a5 --- /dev/null +++ b/blocks/test/fixtures/core-image.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/image", + "attrs": {}, + "rawContent": "\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-latestposts.parsed.json b/blocks/test/fixtures/core-latestposts.parsed.json new file mode 100644 index 00000000000000..bfc039ba0d1b8a --- /dev/null +++ b/blocks/test/fixtures/core-latestposts.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/latestposts", + "attrs": { + "poststoshow": "5" + }, + "rawContent": "" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-list-ul.parsed.json b/blocks/test/fixtures/core-list-ul.parsed.json new file mode 100644 index 00000000000000..f0489633e24fb6 --- /dev/null +++ b/blocks/test/fixtures/core-list-ul.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/list", + "attrs": {}, + "rawContent": "\n
  • Text & Headings
  • Images & Videos
  • Galleries
  • Embeds, like YouTube, Tweets, or other WordPress posts.
  • Layout blocks, like Buttons, Hero Images, Separators, etc.
  • And Lists like this one of course :)
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-preformatted.parsed.json b/blocks/test/fixtures/core-preformatted.parsed.json new file mode 100644 index 00000000000000..27ff693cc851dd --- /dev/null +++ b/blocks/test/fixtures/core-preformatted.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/preformatted", + "attrs": {}, + "rawContent": "\n
Some preformatted text...
And more!
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-pullquote.parsed.json b/blocks/test/fixtures/core-pullquote.parsed.json new file mode 100644 index 00000000000000..2479d2e6eb1451 --- /dev/null +++ b/blocks/test/fixtures/core-pullquote.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/pullquote", + "attrs": {}, + "rawContent": "\n
\n

Testing pullquote block...

...with a caption
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-quote-style-1.parsed.json b/blocks/test/fixtures/core-quote-style-1.parsed.json new file mode 100644 index 00000000000000..1e0cb03a581f5a --- /dev/null +++ b/blocks/test/fixtures/core-quote-style-1.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/quote", + "attrs": { + "style": "1" + }, + "rawContent": "\n

The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.

Matt Mullenweg, 2017
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-quote-style-2.parsed.json b/blocks/test/fixtures/core-quote-style-2.parsed.json new file mode 100644 index 00000000000000..281ad3b6200467 --- /dev/null +++ b/blocks/test/fixtures/core-quote-style-2.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/quote", + "attrs": { + "style": "2" + }, + "rawContent": "\n

There is no greater agony than bearing an untold story inside you.

Maya Angelou
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-separator.parsed.json b/blocks/test/fixtures/core-separator.parsed.json new file mode 100644 index 00000000000000..61b0f7500268b6 --- /dev/null +++ b/blocks/test/fixtures/core-separator.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/separator", + "attrs": {}, + "rawContent": "\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-table.parsed.json b/blocks/test/fixtures/core-table.parsed.json new file mode 100644 index 00000000000000..3aedba808dff7f --- /dev/null +++ b/blocks/test/fixtures/core-table.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/table", + "attrs": {}, + "rawContent": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
VersionMusicianDate
.70No musician chosen.May 27, 2003
1.0Miles DavisJanuary 3, 2004
Lots of versions skipped, see the full list
4.4Clifford BrownDecember 8, 2015
4.5Coleman HawkinsApril 12, 2016
4.6Pepper AdamsAugust 16, 2016
4.7Sarah VaughanDecember 6, 2016
\n" + }, + { + "attrs": {}, + "rawContent": "\n\n" + } +] diff --git a/blocks/test/fixtures/core-text-align-right.parsed.json b/blocks/test/fixtures/core-text-align-right.parsed.json new file mode 100644 index 00000000000000..b4c6287f3514c0 --- /dev/null +++ b/blocks/test/fixtures/core-text-align-right.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/text", + "attrs": { + "align": "right" + }, + "rawContent": "\n

... like this one, which is separate from the above and right aligned.

\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/full-content.js b/blocks/test/full-content.js index 7a16290ddb872e..dd8cb93bc70340 100644 --- a/blocks/test/full-content.js +++ b/blocks/test/full-content.js @@ -11,15 +11,17 @@ import { format } from 'util'; * Internal dependencies */ import parse from '../api/parser'; +import { parse as grammarParse } from '../api/post.pegjs'; import serialize from '../api/serializer'; import { getBlockTypes } from '../api/registration'; const fixturesDir = path.join( __dirname, 'fixtures' ); -// We expect 3 different types of files for each fixture: -// - fixture.html - original content -// - fixture.json - blocks structure -// - fixture.serialized.html - re-serialized content +// We expect 4 different types of files for each fixture: +// - fixture.html : original content +// - fixture.parsed.json : parser output +// - fixture.json : blocks structure +// - fixture.serialized.html : re-serialized content // Get the "base" name for each fixture first. const fileBasenames = uniq( fs.readdirSync( fixturesDir ) @@ -91,6 +93,27 @@ describe( 'full post content fixture', () => { it( f, () => { const content = readFixtureFile( f + '.html' ); + const parserOutputActual = grammarParse( content ); + let parserOutputExpectedString = readFixtureFile( f + '.parsed.json' ); + + if ( ! parserOutputExpectedString ) { + if ( process.env.GENERATE_MISSING_FIXTURES ) { + parserOutputExpectedString = JSON.stringify( + parserOutputActual, + null, + 4 + ) + '\n'; + writeFixtureFile( f + '.parsed.json', parserOutputExpectedString ); + } else { + throw new Error( + 'Missing fixture file: ' + f + '.parsed.json' + ); + } + } + + const parserOutputExpected = JSON.parse( parserOutputExpectedString ); + expect( parserOutputActual ).to.eql( parserOutputExpected ); + const blocksActual = parse( content ); const blocksActualNormalized = normalizeParsedBlocks( blocksActual ); let blocksExpectedString = readFixtureFile( f + '.json' ); From b6337f702948a6247feb2b11a3d49d01163263fa Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 23 Jun 2017 07:38:52 -0500 Subject: [PATCH 04/18] Run tests against PHP parser during Travis CI builds Or using the following commands (TODO - add a npm script for this?): ``` bin/create-php-parser.js RUN_PARSER_TESTS=1 phpunit ``` --- .travis.yml | 2 +- phpcs.xml | 6 +++ phpunit/class-parsing-test.php | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 phpunit/class-parsing-test.php diff --git a/.travis.yml b/.travis.yml index aa0004d671e5ba..cf0ded3b24726a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -58,7 +58,7 @@ script: node bin/create-php-parser.js || exit 1 php lib/parser.php || exit 1 # Run PHPUnit tests - phpunit || exit 1 + RUN_PARSER_TESTS=1 phpunit || exit 1 WP_MULTISITE=1 phpunit || exit 1 fi - | diff --git a/phpcs.xml b/phpcs.xml index 9086dccef78637..a471c290142a70 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -40,4 +40,10 @@ phpunit/* + + phpunit/* + + + phpunit/* + diff --git a/phpunit/class-parsing-test.php b/phpunit/class-parsing-test.php new file mode 100644 index 00000000000000..9ddbacda438f27 --- /dev/null +++ b/phpunit/class-parsing-test.php @@ -0,0 +1,68 @@ +parse( $html ); + + error_log( json_encode( compact( 'html', 'result' ) ) ); + + $this->assertEquals( $parsed, $result ); + } +} From c88b9f43124031065ed4fe7a7de7603de4c1fb96 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 23 Jun 2017 07:50:16 -0500 Subject: [PATCH 05/18] Update parser fixture files after rebase --- blocks/test/fixtures/core-button-center.parsed.json | 2 +- blocks/test/fixtures/core-code.parsed.json | 4 ++-- blocks/test/fixtures/core-cover-image.parsed.json | 13 +++++++++++++ blocks/test/fixtures/core-embed.parsed.json | 2 +- blocks/test/fixtures/core-embedanimoto.parsed.json | 2 +- blocks/test/fixtures/core-embedcloudup.parsed.json | 2 +- .../fixtures/core-embedcollegehumor.parsed.json | 2 +- .../test/fixtures/core-embeddailymotion.parsed.json | 2 +- blocks/test/fixtures/core-embedfacebook.parsed.json | 2 +- blocks/test/fixtures/core-embedflickr.parsed.json | 2 +- .../test/fixtures/core-embedfunnyordie.parsed.json | 2 +- blocks/test/fixtures/core-embedhulu.parsed.json | 2 +- blocks/test/fixtures/core-embedimgur.parsed.json | 2 +- .../test/fixtures/core-embedinstagram.parsed.json | 2 +- blocks/test/fixtures/core-embedissuu.parsed.json | 2 +- .../test/fixtures/core-embedkickstarter.parsed.json | 2 +- .../test/fixtures/core-embedmeetupcom.parsed.json | 2 +- blocks/test/fixtures/core-embedmixcloud.parsed.json | 2 +- .../test/fixtures/core-embedphotobucket.parsed.json | 2 +- .../test/fixtures/core-embedpolldaddy.parsed.json | 2 +- blocks/test/fixtures/core-embedreddit.parsed.json | 2 +- .../fixtures/core-embedreverbnation.parsed.json | 2 +- .../test/fixtures/core-embedscreencast.parsed.json | 2 +- blocks/test/fixtures/core-embedscribd.parsed.json | 2 +- .../test/fixtures/core-embedslideshare.parsed.json | 2 +- blocks/test/fixtures/core-embedsmugmug.parsed.json | 2 +- .../test/fixtures/core-embedsoundcloud.parsed.json | 2 +- blocks/test/fixtures/core-embedspeaker.parsed.json | 2 +- blocks/test/fixtures/core-embedspotify.parsed.json | 2 +- blocks/test/fixtures/core-embedted.parsed.json | 2 +- blocks/test/fixtures/core-embedtumblr.parsed.json | 2 +- blocks/test/fixtures/core-embedtwitter.parsed.json | 2 +- .../test/fixtures/core-embedvideopress.parsed.json | 2 +- blocks/test/fixtures/core-embedvimeo.parsed.json | 2 +- blocks/test/fixtures/core-embedvine.parsed.json | 2 +- .../test/fixtures/core-embedwordpress.parsed.json | 2 +- .../test/fixtures/core-embedwordpresstv.parsed.json | 2 +- blocks/test/fixtures/core-embedyoutube.parsed.json | 2 +- blocks/test/fixtures/core-freeform.parsed.json | 2 +- blocks/test/fixtures/core-gallery.parsed.json | 4 ++-- blocks/test/fixtures/core-heading-h2-em.parsed.json | 2 +- blocks/test/fixtures/core-heading-h2.parsed.json | 2 +- blocks/test/fixtures/core-html.parsed.json | 11 +++++++++++ .../fixtures/core-image-center-caption.parsed.json | 2 +- blocks/test/fixtures/core-image.parsed.json | 4 ++-- blocks/test/fixtures/core-latestposts.parsed.json | 2 +- blocks/test/fixtures/core-list-ul.parsed.json | 2 +- blocks/test/fixtures/core-preformatted.parsed.json | 4 ++-- blocks/test/fixtures/core-pullquote.parsed.json | 4 ++-- blocks/test/fixtures/core-quote-style-1.parsed.json | 2 +- blocks/test/fixtures/core-quote-style-2.parsed.json | 2 +- blocks/test/fixtures/core-separator.parsed.json | 4 ++-- blocks/test/fixtures/core-table.parsed.json | 4 ++-- 53 files changed, 82 insertions(+), 58 deletions(-) create mode 100644 blocks/test/fixtures/core-cover-image.parsed.json create mode 100644 blocks/test/fixtures/core-html.parsed.json diff --git a/blocks/test/fixtures/core-button-center.parsed.json b/blocks/test/fixtures/core-button-center.parsed.json index cd020c352f0380..e19d7f4991ae7e 100644 --- a/blocks/test/fixtures/core-button-center.parsed.json +++ b/blocks/test/fixtures/core-button-center.parsed.json @@ -4,7 +4,7 @@ "attrs": { "align": "center" }, - "rawContent": "\n\n" + "rawContent": "\n\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-code.parsed.json b/blocks/test/fixtures/core-code.parsed.json index 97bdae206d3142..127ba413fcb3cd 100644 --- a/blocks/test/fixtures/core-code.parsed.json +++ b/blocks/test/fixtures/core-code.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/code", - "attrs": {}, - "rawContent": "\n
export default function MyButton() {\n\treturn <Button>Click Me!</Button>;\n}
\n" + "attrs": null, + "rawContent": "\n
export default function MyButton() {\n\treturn <Button>Click Me!</Button>;\n}
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-cover-image.parsed.json b/blocks/test/fixtures/core-cover-image.parsed.json new file mode 100644 index 00000000000000..437dffbb6c5b9a --- /dev/null +++ b/blocks/test/fixtures/core-cover-image.parsed.json @@ -0,0 +1,13 @@ +[ + { + "blockName": "core/cover-image", + "attrs": { + "url": "https://cldup.com/uuUqE_dXzy.jpg" + }, + "rawContent": "\n
\n\t
\n\t\t

Guten Berg!

\n\t
\n
\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-embed.parsed.json b/blocks/test/fixtures/core-embed.parsed.json index 0d734256c9a0b8..d0b20272ef818f 100644 --- a/blocks/test/fixtures/core-embed.parsed.json +++ b/blocks/test/fixtures/core-embed.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://example.com/" }, - "rawContent": "\n
\n https://example.com/\n
Embedded content from an example URL
\n
\n" + "rawContent": "\n
\n https://example.com/\n
Embedded content from an example URL
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedanimoto.parsed.json b/blocks/test/fixtures/core-embedanimoto.parsed.json index c0b095ddf03778..44b9baad3d40d7 100644 --- a/blocks/test/fixtures/core-embedanimoto.parsed.json +++ b/blocks/test/fixtures/core-embedanimoto.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://animoto.com/" }, - "rawContent": "\n
\n https://animoto.com/\n
Embedded content from animoto
\n
\n" + "rawContent": "\n
\n https://animoto.com/\n
Embedded content from animoto
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedcloudup.parsed.json b/blocks/test/fixtures/core-embedcloudup.parsed.json index a5b905ba6b2787..7454c9510d5b38 100644 --- a/blocks/test/fixtures/core-embedcloudup.parsed.json +++ b/blocks/test/fixtures/core-embedcloudup.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://cloudup.com/" }, - "rawContent": "\n
\n https://cloudup.com/\n
Embedded content from cloudup
\n
\n" + "rawContent": "\n
\n https://cloudup.com/\n
Embedded content from cloudup
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedcollegehumor.parsed.json b/blocks/test/fixtures/core-embedcollegehumor.parsed.json index 1ea7fe1c308a67..fe0823b1b4950f 100644 --- a/blocks/test/fixtures/core-embedcollegehumor.parsed.json +++ b/blocks/test/fixtures/core-embedcollegehumor.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://collegehumor.com/" }, - "rawContent": "\n
\n https://collegehumor.com/\n
Embedded content from collegehumor
\n
\n" + "rawContent": "\n
\n https://collegehumor.com/\n
Embedded content from collegehumor
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embeddailymotion.parsed.json b/blocks/test/fixtures/core-embeddailymotion.parsed.json index 116c47ca87ab79..23f1346941de4a 100644 --- a/blocks/test/fixtures/core-embeddailymotion.parsed.json +++ b/blocks/test/fixtures/core-embeddailymotion.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://dailymotion.com/" }, - "rawContent": "\n
\n https://dailymotion.com/\n
Embedded content from dailymotion
\n
\n" + "rawContent": "\n
\n https://dailymotion.com/\n
Embedded content from dailymotion
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedfacebook.parsed.json b/blocks/test/fixtures/core-embedfacebook.parsed.json index 9748ecffcdaade..ea29a1cd1c28fe 100644 --- a/blocks/test/fixtures/core-embedfacebook.parsed.json +++ b/blocks/test/fixtures/core-embedfacebook.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://facebook.com/" }, - "rawContent": "\n
\n https://facebook.com/\n
Embedded content from facebook
\n
\n" + "rawContent": "\n
\n https://facebook.com/\n
Embedded content from facebook
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedflickr.parsed.json b/blocks/test/fixtures/core-embedflickr.parsed.json index 0e29813f495e1a..8bd79af05b2c69 100644 --- a/blocks/test/fixtures/core-embedflickr.parsed.json +++ b/blocks/test/fixtures/core-embedflickr.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://flickr.com/" }, - "rawContent": "\n
\n https://flickr.com/\n
Embedded content from flickr
\n
\n" + "rawContent": "\n
\n https://flickr.com/\n
Embedded content from flickr
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedfunnyordie.parsed.json b/blocks/test/fixtures/core-embedfunnyordie.parsed.json index f187b9afb40ec7..e3d487cc5e189a 100644 --- a/blocks/test/fixtures/core-embedfunnyordie.parsed.json +++ b/blocks/test/fixtures/core-embedfunnyordie.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://funnyordie.com/" }, - "rawContent": "\n
\n https://funnyordie.com/\n
Embedded content from funnyordie
\n
\n" + "rawContent": "\n
\n https://funnyordie.com/\n
Embedded content from funnyordie
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedhulu.parsed.json b/blocks/test/fixtures/core-embedhulu.parsed.json index dbd6907efca23e..77d9a1d429f7a8 100644 --- a/blocks/test/fixtures/core-embedhulu.parsed.json +++ b/blocks/test/fixtures/core-embedhulu.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://hulu.com/" }, - "rawContent": "\n
\n https://hulu.com/\n
Embedded content from hulu
\n
\n" + "rawContent": "\n
\n https://hulu.com/\n
Embedded content from hulu
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedimgur.parsed.json b/blocks/test/fixtures/core-embedimgur.parsed.json index 5b816b79555752..50a3c2e8009719 100644 --- a/blocks/test/fixtures/core-embedimgur.parsed.json +++ b/blocks/test/fixtures/core-embedimgur.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://imgur.com/" }, - "rawContent": "\n
\n https://imgur.com/\n
Embedded content from imgur
\n
\n" + "rawContent": "\n
\n https://imgur.com/\n
Embedded content from imgur
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedinstagram.parsed.json b/blocks/test/fixtures/core-embedinstagram.parsed.json index 279808a9182c63..b01513853fc616 100644 --- a/blocks/test/fixtures/core-embedinstagram.parsed.json +++ b/blocks/test/fixtures/core-embedinstagram.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://instagram.com/" }, - "rawContent": "\n
\n https://instagram.com/\n
Embedded content from instagram
\n
\n" + "rawContent": "\n
\n https://instagram.com/\n
Embedded content from instagram
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedissuu.parsed.json b/blocks/test/fixtures/core-embedissuu.parsed.json index 72eb263bb9307a..f7bd209d42dd4e 100644 --- a/blocks/test/fixtures/core-embedissuu.parsed.json +++ b/blocks/test/fixtures/core-embedissuu.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://issuu.com/" }, - "rawContent": "\n
\n https://issuu.com/\n
Embedded content from issuu
\n
\n" + "rawContent": "\n
\n https://issuu.com/\n
Embedded content from issuu
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedkickstarter.parsed.json b/blocks/test/fixtures/core-embedkickstarter.parsed.json index a2d3100d7795b9..28b0b92b520649 100644 --- a/blocks/test/fixtures/core-embedkickstarter.parsed.json +++ b/blocks/test/fixtures/core-embedkickstarter.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://kickstarter.com/" }, - "rawContent": "\n
\n https://kickstarter.com/\n
Embedded content from kickstarter
\n
\n" + "rawContent": "\n
\n https://kickstarter.com/\n
Embedded content from kickstarter
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedmeetupcom.parsed.json b/blocks/test/fixtures/core-embedmeetupcom.parsed.json index 5e9bcfddf922f4..bbb34253900853 100644 --- a/blocks/test/fixtures/core-embedmeetupcom.parsed.json +++ b/blocks/test/fixtures/core-embedmeetupcom.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://meetupcom.com/" }, - "rawContent": "\n
\n https://meetupcom.com/\n
Embedded content from meetupcom
\n
\n" + "rawContent": "\n
\n https://meetupcom.com/\n
Embedded content from meetupcom
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedmixcloud.parsed.json b/blocks/test/fixtures/core-embedmixcloud.parsed.json index 3ac3bafb51ca3e..ad662365ce6121 100644 --- a/blocks/test/fixtures/core-embedmixcloud.parsed.json +++ b/blocks/test/fixtures/core-embedmixcloud.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://mixcloud.com/" }, - "rawContent": "\n
\n https://mixcloud.com/\n
Embedded content from mixcloud
\n
\n" + "rawContent": "\n
\n https://mixcloud.com/\n
Embedded content from mixcloud
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedphotobucket.parsed.json b/blocks/test/fixtures/core-embedphotobucket.parsed.json index 1ce02d240b07b7..cebb753a390411 100644 --- a/blocks/test/fixtures/core-embedphotobucket.parsed.json +++ b/blocks/test/fixtures/core-embedphotobucket.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://photobucket.com/" }, - "rawContent": "\n
\n https://photobucket.com/\n
Embedded content from photobucket
\n
\n" + "rawContent": "\n
\n https://photobucket.com/\n
Embedded content from photobucket
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedpolldaddy.parsed.json b/blocks/test/fixtures/core-embedpolldaddy.parsed.json index 1f7144beef8f5e..c3458df58e2f7f 100644 --- a/blocks/test/fixtures/core-embedpolldaddy.parsed.json +++ b/blocks/test/fixtures/core-embedpolldaddy.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://polldaddy.com/" }, - "rawContent": "\n
\n https://polldaddy.com/\n
Embedded content from polldaddy
\n
\n" + "rawContent": "\n
\n https://polldaddy.com/\n
Embedded content from polldaddy
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedreddit.parsed.json b/blocks/test/fixtures/core-embedreddit.parsed.json index 7f80f72d64a393..52ea635e03ee3c 100644 --- a/blocks/test/fixtures/core-embedreddit.parsed.json +++ b/blocks/test/fixtures/core-embedreddit.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://reddit.com/" }, - "rawContent": "\n
\n https://reddit.com/\n
Embedded content from reddit
\n
\n" + "rawContent": "\n
\n https://reddit.com/\n
Embedded content from reddit
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedreverbnation.parsed.json b/blocks/test/fixtures/core-embedreverbnation.parsed.json index 729ca8cfa32f02..76fad52e4d8f3d 100644 --- a/blocks/test/fixtures/core-embedreverbnation.parsed.json +++ b/blocks/test/fixtures/core-embedreverbnation.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://reverbnation.com/" }, - "rawContent": "\n
\n https://reverbnation.com/\n
Embedded content from reverbnation
\n
\n" + "rawContent": "\n
\n https://reverbnation.com/\n
Embedded content from reverbnation
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedscreencast.parsed.json b/blocks/test/fixtures/core-embedscreencast.parsed.json index a57dfd4c907bc7..51369759381f6e 100644 --- a/blocks/test/fixtures/core-embedscreencast.parsed.json +++ b/blocks/test/fixtures/core-embedscreencast.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://screencast.com/" }, - "rawContent": "\n
\n https://screencast.com/\n
Embedded content from screencast
\n
\n" + "rawContent": "\n
\n https://screencast.com/\n
Embedded content from screencast
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedscribd.parsed.json b/blocks/test/fixtures/core-embedscribd.parsed.json index 3233bd9ca3cf96..320c2c7e503e2d 100644 --- a/blocks/test/fixtures/core-embedscribd.parsed.json +++ b/blocks/test/fixtures/core-embedscribd.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://scribd.com/" }, - "rawContent": "\n
\n https://scribd.com/\n
Embedded content from scribd
\n
\n" + "rawContent": "\n
\n https://scribd.com/\n
Embedded content from scribd
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedslideshare.parsed.json b/blocks/test/fixtures/core-embedslideshare.parsed.json index 05fcacb9ca36be..ce0bce0a3f9582 100644 --- a/blocks/test/fixtures/core-embedslideshare.parsed.json +++ b/blocks/test/fixtures/core-embedslideshare.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://slideshare.com/" }, - "rawContent": "\n
\n https://slideshare.com/\n
Embedded content from slideshare
\n
\n" + "rawContent": "\n
\n https://slideshare.com/\n
Embedded content from slideshare
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedsmugmug.parsed.json b/blocks/test/fixtures/core-embedsmugmug.parsed.json index 776d15c154714b..21cac8cb4fe0af 100644 --- a/blocks/test/fixtures/core-embedsmugmug.parsed.json +++ b/blocks/test/fixtures/core-embedsmugmug.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://smugmug.com/" }, - "rawContent": "\n
\n https://smugmug.com/\n
Embedded content from smugmug
\n
\n" + "rawContent": "\n
\n https://smugmug.com/\n
Embedded content from smugmug
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedsoundcloud.parsed.json b/blocks/test/fixtures/core-embedsoundcloud.parsed.json index 9d1b3986079ede..db37defc1515b9 100644 --- a/blocks/test/fixtures/core-embedsoundcloud.parsed.json +++ b/blocks/test/fixtures/core-embedsoundcloud.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://soundcloud.com/" }, - "rawContent": "\n
\n https://soundcloud.com/\n
Embedded content from soundcloud
\n
\n" + "rawContent": "\n
\n https://soundcloud.com/\n
Embedded content from soundcloud
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedspeaker.parsed.json b/blocks/test/fixtures/core-embedspeaker.parsed.json index e890aa7a4f4c96..c13de136b978d4 100644 --- a/blocks/test/fixtures/core-embedspeaker.parsed.json +++ b/blocks/test/fixtures/core-embedspeaker.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://speaker.com/" }, - "rawContent": "\n
\n https://speaker.com/\n
Embedded content from speaker
\n
\n" + "rawContent": "\n
\n https://speaker.com/\n
Embedded content from speaker
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedspotify.parsed.json b/blocks/test/fixtures/core-embedspotify.parsed.json index 375c209a131b47..36bf2b6053d089 100644 --- a/blocks/test/fixtures/core-embedspotify.parsed.json +++ b/blocks/test/fixtures/core-embedspotify.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://spotify.com/" }, - "rawContent": "\n
\n https://spotify.com/\n
Embedded content from spotify
\n
\n" + "rawContent": "\n
\n https://spotify.com/\n
Embedded content from spotify
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedted.parsed.json b/blocks/test/fixtures/core-embedted.parsed.json index c5bd17ffbdd7ca..17900eb25b1fc8 100644 --- a/blocks/test/fixtures/core-embedted.parsed.json +++ b/blocks/test/fixtures/core-embedted.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://ted.com/" }, - "rawContent": "\n
\n https://ted.com/\n
Embedded content from ted
\n
\n" + "rawContent": "\n
\n https://ted.com/\n
Embedded content from ted
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedtumblr.parsed.json b/blocks/test/fixtures/core-embedtumblr.parsed.json index 397025f1920cbf..3bf297e741fa68 100644 --- a/blocks/test/fixtures/core-embedtumblr.parsed.json +++ b/blocks/test/fixtures/core-embedtumblr.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://tumblr.com/" }, - "rawContent": "\n
\n https://tumblr.com/\n
Embedded content from tumblr
\n
\n" + "rawContent": "\n
\n https://tumblr.com/\n
Embedded content from tumblr
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedtwitter.parsed.json b/blocks/test/fixtures/core-embedtwitter.parsed.json index d794a60a801528..0efad073a2118d 100644 --- a/blocks/test/fixtures/core-embedtwitter.parsed.json +++ b/blocks/test/fixtures/core-embedtwitter.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://twitter.com/automattic" }, - "rawContent": "\n
\n https://twitter.com/automattic\n
We are Automattic
\n
\n" + "rawContent": "\n
\n https://twitter.com/automattic\n
We are Automattic
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedvideopress.parsed.json b/blocks/test/fixtures/core-embedvideopress.parsed.json index ef20f744c7f315..4a4415cd827c43 100644 --- a/blocks/test/fixtures/core-embedvideopress.parsed.json +++ b/blocks/test/fixtures/core-embedvideopress.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://videopress.com/" }, - "rawContent": "\n
\n https://videopress.com/\n
Embedded content from videopress
\n
\n" + "rawContent": "\n
\n https://videopress.com/\n
Embedded content from videopress
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedvimeo.parsed.json b/blocks/test/fixtures/core-embedvimeo.parsed.json index 29b1fc473aeb44..03ce8fe0948897 100644 --- a/blocks/test/fixtures/core-embedvimeo.parsed.json +++ b/blocks/test/fixtures/core-embedvimeo.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://vimeo.com/" }, - "rawContent": "\n
\n https://vimeo.com/\n
Embedded content from vimeo
\n
\n" + "rawContent": "\n
\n https://vimeo.com/\n
Embedded content from vimeo
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedvine.parsed.json b/blocks/test/fixtures/core-embedvine.parsed.json index 341842e5f2c39c..fdc9ce13c8f6d5 100644 --- a/blocks/test/fixtures/core-embedvine.parsed.json +++ b/blocks/test/fixtures/core-embedvine.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://vine.com/" }, - "rawContent": "\n
\n https://vine.com/\n
Embedded content from vine
\n
\n" + "rawContent": "\n
\n https://vine.com/\n
Embedded content from vine
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedwordpress.parsed.json b/blocks/test/fixtures/core-embedwordpress.parsed.json index 936b23941f30ea..8db36fb91bc1f0 100644 --- a/blocks/test/fixtures/core-embedwordpress.parsed.json +++ b/blocks/test/fixtures/core-embedwordpress.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://wordpress.com/" }, - "rawContent": "\n
\n https://wordpress.com/\n
Embedded content from WordPress
\n
\n" + "rawContent": "\n
\n https://wordpress.com/\n
Embedded content from WordPress
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedwordpresstv.parsed.json b/blocks/test/fixtures/core-embedwordpresstv.parsed.json index 7fde2e5caa180a..44b786c2feae71 100644 --- a/blocks/test/fixtures/core-embedwordpresstv.parsed.json +++ b/blocks/test/fixtures/core-embedwordpresstv.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://wordpresstv.com/" }, - "rawContent": "\n
\n https://wordpresstv.com/\n
Embedded content from wordpresstv
\n
\n" + "rawContent": "\n
\n https://wordpresstv.com/\n
Embedded content from wordpresstv
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-embedyoutube.parsed.json b/blocks/test/fixtures/core-embedyoutube.parsed.json index 4fcbe733b388af..7a4e7575102222 100644 --- a/blocks/test/fixtures/core-embedyoutube.parsed.json +++ b/blocks/test/fixtures/core-embedyoutube.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://youtube.com/" }, - "rawContent": "\n
\n https://youtube.com/\n
Embedded content from youtube
\n
\n" + "rawContent": "\n
\n https://youtube.com/\n
Embedded content from youtube
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-freeform.parsed.json b/blocks/test/fixtures/core-freeform.parsed.json index f91546825d3fc3..276442f4c7864f 100644 --- a/blocks/test/fixtures/core-freeform.parsed.json +++ b/blocks/test/fixtures/core-freeform.parsed.json @@ -1,7 +1,7 @@ [ { "blockName": "core/freeform", - "attrs": {}, + "attrs": null, "rawContent": "\nTesting freeform block with some\n
\n\tHTML content\n
\n" }, { diff --git a/blocks/test/fixtures/core-gallery.parsed.json b/blocks/test/fixtures/core-gallery.parsed.json index 287dc97f533a9d..8dae7ead309f97 100644 --- a/blocks/test/fixtures/core-gallery.parsed.json +++ b/blocks/test/fixtures/core-gallery.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/gallery", - "attrs": {}, - "rawContent": "\n
\n\t\n\t\n
\n" + "attrs": null, + "rawContent": "\n\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-heading-h2-em.parsed.json b/blocks/test/fixtures/core-heading-h2-em.parsed.json index 171d877a3b6a1a..3920297cb1ac9c 100644 --- a/blocks/test/fixtures/core-heading-h2-em.parsed.json +++ b/blocks/test/fixtures/core-heading-h2-em.parsed.json @@ -1,7 +1,7 @@ [ { "blockName": "core/heading", - "attrs": {}, + "attrs": null, "rawContent": "\n

The Inserter Tool

\n" }, { diff --git a/blocks/test/fixtures/core-heading-h2.parsed.json b/blocks/test/fixtures/core-heading-h2.parsed.json index 57936edd43d115..a6d515e0ac0f13 100644 --- a/blocks/test/fixtures/core-heading-h2.parsed.json +++ b/blocks/test/fixtures/core-heading-h2.parsed.json @@ -1,7 +1,7 @@ [ { "blockName": "core/heading", - "attrs": {}, + "attrs": null, "rawContent": "\n

A picture is worth a thousand words, or so the saying goes

\n" }, { diff --git a/blocks/test/fixtures/core-html.parsed.json b/blocks/test/fixtures/core-html.parsed.json new file mode 100644 index 00000000000000..ae94db5e9027e1 --- /dev/null +++ b/blocks/test/fixtures/core-html.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/html", + "attrs": null, + "rawContent": "\n

Some HTML code

\nThis text will scroll from right to left\n" + }, + { + "attrs": {}, + "rawContent": "\n" + } +] diff --git a/blocks/test/fixtures/core-image-center-caption.parsed.json b/blocks/test/fixtures/core-image-center-caption.parsed.json index 93a717c98a0a59..dbc7b92dd38660 100644 --- a/blocks/test/fixtures/core-image-center-caption.parsed.json +++ b/blocks/test/fixtures/core-image-center-caption.parsed.json @@ -4,7 +4,7 @@ "attrs": { "align": "center" }, - "rawContent": "\n
Give it a try. Press the "really wide" button on the image toolbar.
\n" + "rawContent": "\n
Give it a try. Press the "really wide" button on the image toolbar.
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-image.parsed.json b/blocks/test/fixtures/core-image.parsed.json index 9e4faa2d35e0a5..e948d85add5d5c 100644 --- a/blocks/test/fixtures/core-image.parsed.json +++ b/blocks/test/fixtures/core-image.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/image", - "attrs": {}, - "rawContent": "\n
\n" + "attrs": null, + "rawContent": "\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-latestposts.parsed.json b/blocks/test/fixtures/core-latestposts.parsed.json index bfc039ba0d1b8a..dd1b8c6e2ba28c 100644 --- a/blocks/test/fixtures/core-latestposts.parsed.json +++ b/blocks/test/fixtures/core-latestposts.parsed.json @@ -2,7 +2,7 @@ { "blockName": "core/latestposts", "attrs": { - "poststoshow": "5" + "poststoshow": 5 }, "rawContent": "" }, diff --git a/blocks/test/fixtures/core-list-ul.parsed.json b/blocks/test/fixtures/core-list-ul.parsed.json index f0489633e24fb6..c29157168e32c7 100644 --- a/blocks/test/fixtures/core-list-ul.parsed.json +++ b/blocks/test/fixtures/core-list-ul.parsed.json @@ -1,7 +1,7 @@ [ { "blockName": "core/list", - "attrs": {}, + "attrs": null, "rawContent": "\n
  • Text & Headings
  • Images & Videos
  • Galleries
  • Embeds, like YouTube, Tweets, or other WordPress posts.
  • Layout blocks, like Buttons, Hero Images, Separators, etc.
  • And Lists like this one of course :)
\n" }, { diff --git a/blocks/test/fixtures/core-preformatted.parsed.json b/blocks/test/fixtures/core-preformatted.parsed.json index 27ff693cc851dd..b0854646ed594e 100644 --- a/blocks/test/fixtures/core-preformatted.parsed.json +++ b/blocks/test/fixtures/core-preformatted.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/preformatted", - "attrs": {}, - "rawContent": "\n
Some preformatted text...
And more!
\n" + "attrs": null, + "rawContent": "\n
Some preformatted text...
And more!
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-pullquote.parsed.json b/blocks/test/fixtures/core-pullquote.parsed.json index 2479d2e6eb1451..6261a0064c05ea 100644 --- a/blocks/test/fixtures/core-pullquote.parsed.json +++ b/blocks/test/fixtures/core-pullquote.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/pullquote", - "attrs": {}, - "rawContent": "\n
\n

Testing pullquote block...

...with a caption
\n
\n" + "attrs": null, + "rawContent": "\n
\n

Testing pullquote block...

...with a caption
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-quote-style-1.parsed.json b/blocks/test/fixtures/core-quote-style-1.parsed.json index 1e0cb03a581f5a..9af35a7fe4be22 100644 --- a/blocks/test/fixtures/core-quote-style-1.parsed.json +++ b/blocks/test/fixtures/core-quote-style-1.parsed.json @@ -4,7 +4,7 @@ "attrs": { "style": "1" }, - "rawContent": "\n

The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.

Matt Mullenweg, 2017
\n" + "rawContent": "\n

The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.

Matt Mullenweg, 2017
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-quote-style-2.parsed.json b/blocks/test/fixtures/core-quote-style-2.parsed.json index 281ad3b6200467..5988b9863e722b 100644 --- a/blocks/test/fixtures/core-quote-style-2.parsed.json +++ b/blocks/test/fixtures/core-quote-style-2.parsed.json @@ -4,7 +4,7 @@ "attrs": { "style": "2" }, - "rawContent": "\n

There is no greater agony than bearing an untold story inside you.

Maya Angelou
\n" + "rawContent": "\n

There is no greater agony than bearing an untold story inside you.

Maya Angelou
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-separator.parsed.json b/blocks/test/fixtures/core-separator.parsed.json index 61b0f7500268b6..9dbc62b9dbd849 100644 --- a/blocks/test/fixtures/core-separator.parsed.json +++ b/blocks/test/fixtures/core-separator.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/separator", - "attrs": {}, - "rawContent": "\n
\n" + "attrs": null, + "rawContent": "\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-table.parsed.json b/blocks/test/fixtures/core-table.parsed.json index 3aedba808dff7f..714f17be0caa70 100644 --- a/blocks/test/fixtures/core-table.parsed.json +++ b/blocks/test/fixtures/core-table.parsed.json @@ -1,8 +1,8 @@ [ { "blockName": "core/table", - "attrs": {}, - "rawContent": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
VersionMusicianDate
.70No musician chosen.May 27, 2003
1.0Miles DavisJanuary 3, 2004
Lots of versions skipped, see the full list
4.4Clifford BrownDecember 8, 2015
4.5Coleman HawkinsApril 12, 2016
4.6Pepper AdamsAugust 16, 2016
4.7Sarah VaughanDecember 6, 2016
\n" + "attrs": null, + "rawContent": "\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
VersionMusicianDate
.70No musician chosen.May 27, 2003
1.0Miles DavisJanuary 3, 2004
Lots of versions skipped, see the full list
4.4Clifford BrownDecember 8, 2015
4.5Coleman HawkinsApril 12, 2016
4.6Pepper AdamsAugust 16, 2016
4.7Sarah VaughanDecember 6, 2016
\n" }, { "attrs": {}, From 21c2ac34c2ee81986b3ee5febe0152b2be9e5b1a Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 27 Jun 2017 19:04:57 +0200 Subject: [PATCH 06/18] Decode block JSON attributes as PHP arrays, not StdClass objects --- blocks/api/post.pegjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/api/post.pegjs b/blocks/api/post.pegjs index 2bb8ec7df799c1..97d6372e514cae 100644 --- a/blocks/api/post.pegjs +++ b/blocks/api/post.pegjs @@ -129,7 +129,7 @@ WP_Block_Name WP_Block_Attributes = attrs:$("{" (!("}" WS+ """/"? "-->") .)* "}") { - /** **/ + /** **/ return maybeJSON( attrs ); } From 15b99664b54a00f9208c4063537622a49ee047cf Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 27 Jun 2017 19:05:44 +0200 Subject: [PATCH 07/18] Remove error logging and clarify variable name --- phpunit/class-parsing-test.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/phpunit/class-parsing-test.php b/phpunit/class-parsing-test.php index 9ddbacda438f27..b934324e425d5d 100644 --- a/phpunit/class-parsing-test.php +++ b/phpunit/class-parsing-test.php @@ -55,14 +55,12 @@ function test_parser_output( $html_filename, $parsed_json_filename ) { } } - $html = file_get_contents( $html_filename ); - $parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); + $html = file_get_contents( $html_filename ); + $expected_parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); $parser = new PhpPegJs\Parser; $result = $parser->parse( $html ); - error_log( json_encode( compact( 'html', 'result' ) ) ); - - $this->assertEquals( $parsed, $result ); + $this->assertEquals( $expected_parsed, $result ); } } From e29c4139f8d5d9e23852ca15749d1f1836fbe195 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 27 Jun 2017 19:11:03 +0200 Subject: [PATCH 08/18] Upgrade `phpegjs` to 1.0.0-beta2 Fix all the things (except PHP 5.2 support). --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 62f026bb4a11a3..188d7be6a41055 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "moment": "^2.18.1", "moment-timezone": "^0.5.13", "pegjs": "0.10.0", - "phpegjs": "1.0.0-beta1", + "phpegjs": "1.0.0-beta2", "react": "^15.5.4", "react-autosize-textarea": "^0.4.2", "react-click-outside": "^2.3.0", From 8eb22a7c5a5b28a1b9f11cce3947e719117d5009 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 27 Jun 2017 19:20:25 +0200 Subject: [PATCH 09/18] Use a different method of skipping the PHP parser tests Otherwise PHPUnit 4.5.0 (used with PHP 5.6 Travis build) complains about a suite with no tests in it. --- phpunit/class-parsing-test.php | 90 +++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/phpunit/class-parsing-test.php b/phpunit/class-parsing-test.php index b934324e425d5d..12e267b224f569 100644 --- a/phpunit/class-parsing-test.php +++ b/phpunit/class-parsing-test.php @@ -5,62 +5,62 @@ * @package Gutenberg */ -class Parsing_Test extends WP_UnitTestCase { - protected static $fixtures_dir; +if ( getenv( 'RUN_PARSER_TESTS' ) ) { + // Use the RUN_PARSER_TESTS environment variable to run these tests. - function parsing_test_filenames() { - if ( ! getenv( 'RUN_PARSER_TESTS' ) ) { - // Use the RUN_PARSER_TESTS environment variable to run these tests. - return array(); - } - self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/blocks/test/fixtures'; + class Parsing_Test extends WP_UnitTestCase { + protected static $fixtures_dir; - require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php'; + function parsing_test_filenames() { + self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/blocks/test/fixtures'; - $fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE ); - $fixture_filenames = array_values( array_unique( array_map( - array( $this, 'clean_fixture_filename' ), - $fixture_filenames - ) ) ); + require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php'; - return array_map( - array( $this, 'pass_parser_fixture_filenames' ), - $fixture_filenames - ); - } + $fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE ); + $fixture_filenames = array_values( array_unique( array_map( + array( $this, 'clean_fixture_filename' ), + $fixture_filenames + ) ) ); - function clean_fixture_filename( $filename ) { - $filename = basename( $filename ); - $filename = preg_replace( '/\..+$/', '', $filename ); - return $filename; - } + return array_map( + array( $this, 'pass_parser_fixture_filenames' ), + $fixture_filenames + ); + } - function pass_parser_fixture_filenames( $filename ) { - return array( - "$filename.html", - "$filename.parsed.json", - ); - } + function clean_fixture_filename( $filename ) { + $filename = basename( $filename ); + $filename = preg_replace( '/\..+$/', '', $filename ); + return $filename; + } + + function pass_parser_fixture_filenames( $filename ) { + return array( + "$filename.html", + "$filename.parsed.json", + ); + } - /** - * @dataProvider parsing_test_filenames - */ - function test_parser_output( $html_filename, $parsed_json_filename ) { - $html_filename = self::$fixtures_dir . '/' . $html_filename; - $parsed_json_filename = self::$fixtures_dir . '/' . $parsed_json_filename; + /** + * @dataProvider parsing_test_filenames + */ + function test_parser_output( $html_filename, $parsed_json_filename ) { + $html_filename = self::$fixtures_dir . '/' . $html_filename; + $parsed_json_filename = self::$fixtures_dir . '/' . $parsed_json_filename; - foreach ( array( $html_filename, $parsed_json_filename ) as $filename ) { - if ( ! file_exists( $filename ) ) { - throw new Exception( "Missing fixture file: '$filename'" ); + foreach ( array( $html_filename, $parsed_json_filename ) as $filename ) { + if ( ! file_exists( $filename ) ) { + throw new Exception( "Missing fixture file: '$filename'" ); + } } - } - $html = file_get_contents( $html_filename ); - $expected_parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); + $html = file_get_contents( $html_filename ); + $expected_parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); - $parser = new PhpPegJs\Parser; - $result = $parser->parse( $html ); + $parser = new PhpPegJs\Parser; + $result = $parser->parse( $html ); - $this->assertEquals( $expected_parsed, $result ); + $this->assertEquals( $expected_parsed, $result ); + } } } From 8f8d6496853559844dd21674f28dfaabde8469d8 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Tue, 27 Jun 2017 19:27:42 +0200 Subject: [PATCH 10/18] Make phpcs happy again --- phpcs.xml | 5 +++++ phpunit/class-parsing-test.php | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index a471c290142a70..bc8bf43defd5ba 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -24,6 +24,11 @@ gutenberg.php + + + * + + phpunit/* diff --git a/phpunit/class-parsing-test.php b/phpunit/class-parsing-test.php index 12e267b224f569..b6bf5360dd9929 100644 --- a/phpunit/class-parsing-test.php +++ b/phpunit/class-parsing-test.php @@ -5,9 +5,12 @@ * @package Gutenberg */ +// To run these tests, set the RUN_PARSER_TESTS environment variable to a +// truthy value. if ( getenv( 'RUN_PARSER_TESTS' ) ) { - // Use the RUN_PARSER_TESTS environment variable to run these tests. - + /** + * Tests for the PHP parser generated from our PEG grammar by `phpegjs`. + */ class Parsing_Test extends WP_UnitTestCase { protected static $fixtures_dir; From dcb968a5c48772dac78209abee5a76354b307b5d Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 28 Jun 2017 17:35:01 +0200 Subject: [PATCH 11/18] Upgrade `phpegjs` to 1.0.0-beta3 Adds support for generating a parser compatible with PHP 5.2. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 188d7be6a41055..f8eb2be5e3658c 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "moment": "^2.18.1", "moment-timezone": "^0.5.13", "pegjs": "0.10.0", - "phpegjs": "1.0.0-beta2", + "phpegjs": "1.0.0-beta3", "react": "^15.5.4", "react-autosize-textarea": "^0.4.2", "react-click-outside": "^2.3.0", From dd0f20546446d2846f275301d43e3d92d5add69e Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 28 Jun 2017 17:38:04 +0200 Subject: [PATCH 12/18] Do not use namespaces in generated PHP parser --- bin/create-php-parser.js | 8 +++++++- phpunit/class-parsing-test.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/create-php-parser.js b/bin/create-php-parser.js index 1f3298bad6c7db..fda773d0dd3f0c 100755 --- a/bin/create-php-parser.js +++ b/bin/create-php-parser.js @@ -9,7 +9,13 @@ const peg = fs.readFileSync( 'blocks/api/post.pegjs', 'utf8' ); const parser = pegjs.generate( peg, - { plugins: [ phpegjs ] } + { + plugins: [ phpegjs ], + phpegjs: { + parserNamespace: null, + parserGlobalNamePrefix: 'Gutenberg_PEG_', + }, + } ); fs.writeFileSync( diff --git a/phpunit/class-parsing-test.php b/phpunit/class-parsing-test.php index b6bf5360dd9929..6b2f48209a3ff1 100644 --- a/phpunit/class-parsing-test.php +++ b/phpunit/class-parsing-test.php @@ -60,7 +60,7 @@ function test_parser_output( $html_filename, $parsed_json_filename ) { $html = file_get_contents( $html_filename ); $expected_parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); - $parser = new PhpPegJs\Parser; + $parser = new Gutenberg_PEG_Parser; $result = $parser->parse( $html ); $this->assertEquals( $expected_parsed, $result ); From eff5ee44f9a64b9e89cf9eec8186b9d90665c95b Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 29 Jun 2017 18:07:52 +0200 Subject: [PATCH 13/18] Add generated PHP parser to repository This will simplify the process of loading the plugin. Otherwise we'd have to add this step to `npm run dev` and a couple of other places. --- .gitignore | 1 - lib/parser.php | 1303 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1303 insertions(+), 1 deletion(-) create mode 100644 lib/parser.php diff --git a/.gitignore b/.gitignore index f29182bf5c7f0f..9d58efe7906a66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ node_modules build -lib/parser.php gutenberg.pot .vscode *.log diff --git a/lib/parser.php b/lib/parser.php new file mode 100644 index 00000000000000..b135f0870852e2 --- /dev/null +++ b/lib/parser.php @@ -0,0 +1,1303 @@ +expected = $expected; + $this->found = $found; + $this->grammarOffset = $offset; + $this->grammarLine = $line; + $this->grammarColumn = $column; + $this->name = "Gutenberg_PEG_SyntaxError"; + } +};} + +class Gutenberg_PEG_Parser { + + + private $peg_currPos = 0; + private $peg_reportedPos = 0; + private $peg_cachedPos = 0; + private $peg_cachedPosDetails = array('line' => 1, 'column' => 1, 'seenCR' => false ); + private $peg_maxFailPos = 0; + private $peg_maxFailExpected = array(); + private $peg_silentFails = 0; + private $input = ""; + + + private function cleanup_state(){ + $this->peg_currPos = 0; + $this->peg_reportedPos = 0; + $this->peg_cachedPos = 0; + $this->peg_cachedPosDetails = array('line' => 1, 'column' => 1, 'seenCR' => false ); + $this->peg_maxFailPos = 0; + $this->peg_maxFailExpected = array(); + $this->peg_silentFails = 0; + $this->input = ""; + + } + + + private function text() { + return substr($this->input, $this->peg_reportedPos, $this->peg_reportedPos + $this->peg_currPos); + } + + private function offset() { + return $this->peg_reportedPos; + } + + private function line() { + $compute_pd = $this->peg_computePosDetails($this->peg_reportedPos); + return $compute_pd["line"]; + } + + private function column() { + $compute_pd = $this->peg_computePosDetails($this->peg_reportedPos); + return $compute_pd["column"]; + } + + private function expected($description) { + throw $this->peg_buildException( + null, + array(array("type" => "other", "description" => $description )), + $this->peg_reportedPos + ); + } + + private function error($message) { + throw $this->peg_buildException($message, null, $this->peg_reportedPos); + } + + private function peg_advancePos(&$details, $startPos, $endPos) { + for ($p = $startPos; $p < $endPos; $p++) { + $ch = mb_substr($this->input, $p, 1, "UTF-8"); + if ($ch === "\n") { + if (!$details["seenCR"]) { $details["line"]++; } + $details["column"] = 1; + $details["seenCR"] = false; + } else if ($ch === "\r" || $ch === "\u2028" || $ch === "\u2029") { + $details["line"]++; + $details["column"] = 1; + $details["seenCR"] = true; + } else { + $details["column"]++; + $details["seenCR"] = false; + } + } + } + + private function peg_computePosDetails($pos) { + if ($this->peg_cachedPos !== $pos) { + if ($this->peg_cachedPos > $pos) { + $this->peg_cachedPos = 0; + $this->peg_cachedPosDetails = array( "line" => 1, "column" => 1, "seenCR" => false ); + } + $this->peg_advancePos($this->peg_cachedPosDetails, $this->peg_cachedPos, $pos); + $this->peg_cachedPos = $pos; + } + + return $this->peg_cachedPosDetails; + } + + private function peg_fail($expected) { + if ($this->peg_currPos < $this->peg_maxFailPos) { return; } + + if ($this->peg_currPos > $this->peg_maxFailPos) { + $this->peg_maxFailPos = $this->peg_currPos; + $this->peg_maxFailExpected = array(); + } + + $this->peg_maxFailExpected[] = $expected; + } + + private function peg_buildException_expectedComparator($a, $b) { + if ($a["description"] < $b["description"]) { + return -1; + } else if ($a["description"] > $b["description"]) { + return 1; + } else { + return 0; + } + } + + private function peg_buildException($message, $expected, $pos) { + $posDetails = $this->peg_computePosDetails($pos); + $found = $pos < mb_strlen($this->input, "UTF-8") ? mb_substr($this->input, $pos, 1, "UTF-8") : null; + + if ($expected !== null) { + usort($expected, array($this, "peg_buildException_expectedComparator")); + $i = 1; + while ($i < count($expected)) { + if ($expected[$i - 1] === $expected[$i]) { + array_splice($expected, $i, 1); + } else { + $i++; + } + } + } + + if ($message === null) { + $expectedDescs = array_fill(0, count($expected), null); + + for ($i = 0; $i < count($expected); $i++) { + $expectedDescs[$i] = $expected[$i]["description"]; + } + + $expectedDesc = count($expected) > 1 + ? join(", ", array_slice($expectedDescs, 0, -1)) + . " or " + . $expectedDescs[count($expected) - 1] + : $expectedDescs[0]; + + $foundDesc = $found ? json_encode($found) : "end of input"; + + $message = "Expected " . $expectedDesc . " but " . $foundDesc . " found."; + } + + return new Gutenberg_PEG_SyntaxError( + $message, + $expected, + $found, + $pos, + $posDetails["line"], + $posDetails["column"] + ); + } + + private $peg_FAILED; + private $peg_c0; + private $peg_c1; + private $peg_c2; + private $peg_c3; + private $peg_c4; + private $peg_c5; + private $peg_c6; + private $peg_c7; + private $peg_c8; + private $peg_c9; + private $peg_c10; + private $peg_c11; + private $peg_c12; + private $peg_c13; + private $peg_c14; + private $peg_c15; + private $peg_c16; + private $peg_c17; + private $peg_c18; + private $peg_c19; + private $peg_c20; + private $peg_c21; + private $peg_c22; + private $peg_c23; + private $peg_c24; + private $peg_c25; + private $peg_c26; + private $peg_c27; + private $peg_c28; + private $peg_c29; + + private function peg_f0($blockName, $a) { return $a; } + private function peg_f1($blockName, $attrs) { + return array( + 'blockName' => $blockName, + 'attrs' => $attrs, + 'rawContent' => '', + ); + } + private function peg_f2($s, $c) { return $c; } + private function peg_f3($s, $ts, $e) { return $s['blockName'] === $e['blockName']; } + private function peg_f4($s, $ts, $e) { + return array( + 'blockName' => $s['blockName'], + 'attrs' => $s['attrs'], + 'rawContent' => implode( '', $ts ), + ); + } + private function peg_f5($c) { return $c; } + private function peg_f6($ts) { + return array( + 'attrs' => array(), + 'rawContent' => implode( '', $ts ), + ); + } + private function peg_f7($blockName, $attrs) { + return array( + 'blockName' => $blockName, + 'attrs' => $attrs, + ); + } + private function peg_f8($blockName) { + return array( + 'blockName' => $blockName, + ); + } + private function peg_f9($attrs) { return json_decode( $attrs, true ); } + + private function peg_parseDocument() { + + $s0 = $this->peg_parseWP_Block_List(); + + return $s0; + } + + private function peg_parseWP_Block_List() { + + $s0 = array(); + $s1 = $this->peg_parseWP_Block(); + while ($s1 !== $this->peg_FAILED) { + $s0[] = $s1; + $s1 = $this->peg_parseWP_Block(); + } + + return $s0; + } + + private function peg_parseWP_Block() { + + $s0 = $this->peg_parseWP_Block_Void(); + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_parseWP_Block_Balanced(); + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_parseWP_Block_Html(); + } + } + + return $s0; + } + + private function peg_parseWP_Block_Void() { + + $s0 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 4, "UTF-8") === $this->peg_c0) { + $s1 = $this->peg_c0; + $this->peg_currPos += 4; + } else { + $s1 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c1); } + } + if ($s1 !== $this->peg_FAILED) { + $s2 = array(); + $s3 = $this->peg_parseWS(); + if ($s3 !== $this->peg_FAILED) { + while ($s3 !== $this->peg_FAILED) { + $s2[] = $s3; + $s3 = $this->peg_parseWS(); + } + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c2) { + $s3 = $this->peg_c2; + $this->peg_currPos += 3; + } else { + $s3 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c3); } + } + if ($s3 !== $this->peg_FAILED) { + $s4 = $this->peg_parseWP_Block_Name(); + if ($s4 !== $this->peg_FAILED) { + $s5 = array(); + $s6 = $this->peg_parseWS(); + if ($s6 !== $this->peg_FAILED) { + while ($s6 !== $this->peg_FAILED) { + $s5[] = $s6; + $s6 = $this->peg_parseWS(); + } + } else { + $s5 = $this->peg_FAILED; + } + if ($s5 !== $this->peg_FAILED) { + $s6 = $this->peg_currPos; + $s7 = $this->peg_parseWP_Block_Attributes(); + if ($s7 !== $this->peg_FAILED) { + $s8 = array(); + $s9 = $this->peg_parseWS(); + if ($s9 !== $this->peg_FAILED) { + while ($s9 !== $this->peg_FAILED) { + $s8[] = $s9; + $s9 = $this->peg_parseWS(); + } + } else { + $s8 = $this->peg_FAILED; + } + if ($s8 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s6; + $s7 = $this->peg_f0($s4, $s7); + $s6 = $s7; + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + if ($s6 === $this->peg_FAILED) { + $s6 = null; + } + if ($s6 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 4, "UTF-8") === $this->peg_c4) { + $s7 = $this->peg_c4; + $this->peg_currPos += 4; + } else { + $s7 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c5); } + } + if ($s7 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f1($s4, $s6); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + + return $s0; + } + + private function peg_parseWP_Block_Balanced() { + + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseWP_Block_Start(); + if ($s1 !== $this->peg_FAILED) { + $s2 = array(); + $s3 = $this->peg_currPos; + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseWP_Block_End(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + $s5 = $this->peg_parseAny(); + if ($s5 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s3; + $s4 = $this->peg_f2($s1, $s5); + $s3 = $s4; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + while ($s3 !== $this->peg_FAILED) { + $s2[] = $s3; + $s3 = $this->peg_currPos; + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseWP_Block_End(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + $s5 = $this->peg_parseAny(); + if ($s5 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s3; + $s4 = $this->peg_f2($s1, $s5); + $s3 = $s4; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } + if ($s2 !== $this->peg_FAILED) { + $s3 = $this->peg_parseWP_Block_End(); + if ($s3 !== $this->peg_FAILED) { + $this->peg_reportedPos = $this->peg_currPos; + $s4 = $this->peg_f3($s1, $s2, $s3); + if ($s4) { + $s4 = null; + } else { + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f4($s1, $s2, $s3); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + + return $s0; + } + + private function peg_parseWP_Block_Html() { + + $s0 = $this->peg_currPos; + $s1 = array(); + $s2 = $this->peg_currPos; + $s3 = $this->peg_currPos; + $this->peg_silentFails++; + $s4 = $this->peg_parseWP_Block_Balanced(); + $this->peg_silentFails--; + if ($s4 === $this->peg_FAILED) { + $s3 = null; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + if ($s3 !== $this->peg_FAILED) { + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseWP_Block_Void(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + $s5 = $this->peg_parseAny(); + if ($s5 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s2; + $s3 = $this->peg_f5($s5); + $s2 = $s3; + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + while ($s2 !== $this->peg_FAILED) { + $s1[] = $s2; + $s2 = $this->peg_currPos; + $s3 = $this->peg_currPos; + $this->peg_silentFails++; + $s4 = $this->peg_parseWP_Block_Balanced(); + $this->peg_silentFails--; + if ($s4 === $this->peg_FAILED) { + $s3 = null; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + if ($s3 !== $this->peg_FAILED) { + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseWP_Block_Void(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + $s5 = $this->peg_parseAny(); + if ($s5 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s2; + $s3 = $this->peg_f5($s5); + $s2 = $s3; + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } + } else { + $s1 = $this->peg_FAILED; + } + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f6($s1); + } + $s0 = $s1; + + return $s0; + } + + private function peg_parseWP_Block_Start() { + + $s0 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 4, "UTF-8") === $this->peg_c0) { + $s1 = $this->peg_c0; + $this->peg_currPos += 4; + } else { + $s1 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c1); } + } + if ($s1 !== $this->peg_FAILED) { + $s2 = array(); + $s3 = $this->peg_parseWS(); + if ($s3 !== $this->peg_FAILED) { + while ($s3 !== $this->peg_FAILED) { + $s2[] = $s3; + $s3 = $this->peg_parseWS(); + } + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c2) { + $s3 = $this->peg_c2; + $this->peg_currPos += 3; + } else { + $s3 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c3); } + } + if ($s3 !== $this->peg_FAILED) { + $s4 = $this->peg_parseWP_Block_Name(); + if ($s4 !== $this->peg_FAILED) { + $s5 = array(); + $s6 = $this->peg_parseWS(); + if ($s6 !== $this->peg_FAILED) { + while ($s6 !== $this->peg_FAILED) { + $s5[] = $s6; + $s6 = $this->peg_parseWS(); + } + } else { + $s5 = $this->peg_FAILED; + } + if ($s5 !== $this->peg_FAILED) { + $s6 = $this->peg_currPos; + $s7 = $this->peg_parseWP_Block_Attributes(); + if ($s7 !== $this->peg_FAILED) { + $s8 = array(); + $s9 = $this->peg_parseWS(); + if ($s9 !== $this->peg_FAILED) { + while ($s9 !== $this->peg_FAILED) { + $s8[] = $s9; + $s9 = $this->peg_parseWS(); + } + } else { + $s8 = $this->peg_FAILED; + } + if ($s8 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s6; + $s7 = $this->peg_f0($s4, $s7); + $s6 = $s7; + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + if ($s6 === $this->peg_FAILED) { + $s6 = null; + } + if ($s6 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c6) { + $s7 = $this->peg_c6; + $this->peg_currPos += 3; + } else { + $s7 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c7); } + } + if ($s7 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f7($s4, $s6); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + + return $s0; + } + + private function peg_parseWP_Block_End() { + + $s0 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 4, "UTF-8") === $this->peg_c0) { + $s1 = $this->peg_c0; + $this->peg_currPos += 4; + } else { + $s1 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c1); } + } + if ($s1 !== $this->peg_FAILED) { + $s2 = array(); + $s3 = $this->peg_parseWS(); + if ($s3 !== $this->peg_FAILED) { + while ($s3 !== $this->peg_FAILED) { + $s2[] = $s3; + $s3 = $this->peg_parseWS(); + } + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 4, "UTF-8") === $this->peg_c8) { + $s3 = $this->peg_c8; + $this->peg_currPos += 4; + } else { + $s3 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c9); } + } + if ($s3 !== $this->peg_FAILED) { + $s4 = $this->peg_parseWP_Block_Name(); + if ($s4 !== $this->peg_FAILED) { + $s5 = array(); + $s6 = $this->peg_parseWS(); + if ($s6 !== $this->peg_FAILED) { + while ($s6 !== $this->peg_FAILED) { + $s5[] = $s6; + $s6 = $this->peg_parseWS(); + } + } else { + $s5 = $this->peg_FAILED; + } + if ($s5 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c6) { + $s6 = $this->peg_c6; + $this->peg_currPos += 3; + } else { + $s6 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c7); } + } + if ($s6 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f8($s4); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + + return $s0; + } + + private function peg_parseWP_Block_Name() { + + $s0 = $this->peg_currPos; + $s1 = $this->peg_currPos; + $s2 = $this->peg_parseASCII_Letter(); + if ($s2 !== $this->peg_FAILED) { + $s3 = array(); + $s4 = $this->peg_parseASCII_AlphaNumeric(); + if ($s4 === $this->peg_FAILED) { + $s4 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c10) { + $s5 = $this->peg_c10; + $this->peg_currPos++; + } else { + $s5 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c11); } + } + if ($s5 !== $this->peg_FAILED) { + $s6 = $this->peg_parseASCII_AlphaNumeric(); + if ($s6 !== $this->peg_FAILED) { + $s5 = array($s5, $s6); + $s4 = $s5; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + } + while ($s4 !== $this->peg_FAILED) { + $s3[] = $s4; + $s4 = $this->peg_parseASCII_AlphaNumeric(); + if ($s4 === $this->peg_FAILED) { + $s4 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c10) { + $s5 = $this->peg_c10; + $this->peg_currPos++; + } else { + $s5 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c11); } + } + if ($s5 !== $this->peg_FAILED) { + $s6 = $this->peg_parseASCII_AlphaNumeric(); + if ($s6 !== $this->peg_FAILED) { + $s5 = array($s5, $s6); + $s4 = $s5; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + } + } + if ($s3 !== $this->peg_FAILED) { + $s2 = array($s2, $s3); + $s1 = $s2; + } else { + $this->peg_currPos = $s1; + $s1 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s1; + $s1 = $this->peg_FAILED; + } + if ($s1 !== $this->peg_FAILED) { + $s0 = mb_substr($this->input, $s0, $this->peg_currPos - $s0, "UTF-8"); + } else { + $s0 = $s1; + } + + return $s0; + } + + private function peg_parseWP_Block_Attributes() { + + $s0 = $this->peg_currPos; + $s1 = $this->peg_currPos; + $s2 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c12) { + $s3 = $this->peg_c12; + $this->peg_currPos++; + } else { + $s3 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c13); } + } + if ($s3 !== $this->peg_FAILED) { + $s4 = array(); + $s5 = $this->peg_currPos; + $s6 = $this->peg_currPos; + $this->peg_silentFails++; + $s7 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c14) { + $s8 = $this->peg_c14; + $this->peg_currPos++; + } else { + $s8 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c15); } + } + if ($s8 !== $this->peg_FAILED) { + $s9 = array(); + $s10 = $this->peg_parseWS(); + if ($s10 !== $this->peg_FAILED) { + while ($s10 !== $this->peg_FAILED) { + $s9[] = $s10; + $s10 = $this->peg_parseWS(); + } + } else { + $s9 = $this->peg_FAILED; + } + if ($s9 !== $this->peg_FAILED) { + $s10 = $this->peg_c16; + if ($s10 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c10) { + $s11 = $this->peg_c10; + $this->peg_currPos++; + } else { + $s11 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c11); } + } + if ($s11 === $this->peg_FAILED) { + $s11 = null; + } + if ($s11 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c6) { + $s12 = $this->peg_c6; + $this->peg_currPos += 3; + } else { + $s12 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c7); } + } + if ($s12 !== $this->peg_FAILED) { + $s8 = array($s8, $s9, $s10, $s11, $s12); + $s7 = $s8; + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + $this->peg_silentFails--; + if ($s7 === $this->peg_FAILED) { + $s6 = null; + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + if ($s6 !== $this->peg_FAILED) { + if (mb_strlen($this->input, "UTF-8") > $this->peg_currPos) { + $s7 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s7 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c17); } + } + if ($s7 !== $this->peg_FAILED) { + $s6 = array($s6, $s7); + $s5 = $s6; + } else { + $this->peg_currPos = $s5; + $s5 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s5; + $s5 = $this->peg_FAILED; + } + while ($s5 !== $this->peg_FAILED) { + $s4[] = $s5; + $s5 = $this->peg_currPos; + $s6 = $this->peg_currPos; + $this->peg_silentFails++; + $s7 = $this->peg_currPos; + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c14) { + $s8 = $this->peg_c14; + $this->peg_currPos++; + } else { + $s8 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c15); } + } + if ($s8 !== $this->peg_FAILED) { + $s9 = array(); + $s10 = $this->peg_parseWS(); + if ($s10 !== $this->peg_FAILED) { + while ($s10 !== $this->peg_FAILED) { + $s9[] = $s10; + $s10 = $this->peg_parseWS(); + } + } else { + $s9 = $this->peg_FAILED; + } + if ($s9 !== $this->peg_FAILED) { + $s10 = $this->peg_c16; + if ($s10 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c10) { + $s11 = $this->peg_c10; + $this->peg_currPos++; + } else { + $s11 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c11); } + } + if ($s11 === $this->peg_FAILED) { + $s11 = null; + } + if ($s11 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 3, "UTF-8") === $this->peg_c6) { + $s12 = $this->peg_c6; + $this->peg_currPos += 3; + } else { + $s12 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c7); } + } + if ($s12 !== $this->peg_FAILED) { + $s8 = array($s8, $s9, $s10, $s11, $s12); + $s7 = $s8; + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s7; + $s7 = $this->peg_FAILED; + } + $this->peg_silentFails--; + if ($s7 === $this->peg_FAILED) { + $s6 = null; + } else { + $this->peg_currPos = $s6; + $s6 = $this->peg_FAILED; + } + if ($s6 !== $this->peg_FAILED) { + if (mb_strlen($this->input, "UTF-8") > $this->peg_currPos) { + $s7 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s7 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c17); } + } + if ($s7 !== $this->peg_FAILED) { + $s6 = array($s6, $s7); + $s5 = $s6; + } else { + $this->peg_currPos = $s5; + $s5 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s5; + $s5 = $this->peg_FAILED; + } + } + if ($s4 !== $this->peg_FAILED) { + if (mb_substr($this->input, $this->peg_currPos, 1, "UTF-8") === $this->peg_c14) { + $s5 = $this->peg_c14; + $this->peg_currPos++; + } else { + $s5 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c15); } + } + if ($s5 !== $this->peg_FAILED) { + $s3 = array($s3, $s4, $s5); + $s2 = $s3; + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s2; + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + $s1 = mb_substr($this->input, $s1, $this->peg_currPos - $s1, "UTF-8"); + } else { + $s1 = $s2; + } + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f9($s1); + } + $s0 = $s1; + + return $s0; + } + + private function peg_parseASCII_AlphaNumeric() { + + $s0 = $this->peg_parseASCII_Letter(); + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_parseASCII_Digit(); + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_parseSpecial_Chars(); + } + } + + return $s0; + } + + private function peg_parseASCII_Letter() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c18, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c19); } + } + + return $s0; + } + + private function peg_parseASCII_Digit() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c20, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c21); } + } + + return $s0; + } + + private function peg_parseSpecial_Chars() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c22, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c23); } + } + + return $s0; + } + + private function peg_parseWS() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c24, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c25); } + } + + return $s0; + } + + private function peg_parseNewline() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c26, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c27); } + } + + return $s0; + } + + private function peg_parse_() { + + if (Gutenberg_PEG_peg_regex_test($this->peg_c28, mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"))) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c29); } + } + + return $s0; + } + + private function peg_parse__() { + + $s0 = array(); + $s1 = $this->peg_parse_(); + if ($s1 !== $this->peg_FAILED) { + while ($s1 !== $this->peg_FAILED) { + $s0[] = $s1; + $s1 = $this->peg_parse_(); + } + } else { + $s0 = $this->peg_FAILED; + } + + return $s0; + } + + private function peg_parseAny() { + + if (mb_strlen($this->input, "UTF-8") > $this->peg_currPos) { + $s0 = mb_substr($this->input, $this->peg_currPos, 1, "UTF-8"); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { $this->peg_fail($this->peg_c17); } + } + + return $s0; + } + + public function parse($input) { + $arguments = func_get_args(); + $options = count($arguments) > 1 ? $arguments[1] : array(); + $this->cleanup_state(); + $this->input = $input; + $old_regex_encoding = mb_regex_encoding(); + mb_regex_encoding("UTF-8"); + + $this->peg_FAILED = new stdClass; + $this->peg_c0 = ""; + $this->peg_c5 = array( "type" => "literal", "value" => "/-->", "description" => "\"/-->\"" ); + $this->peg_c6 = "-->"; + $this->peg_c7 = array( "type" => "literal", "value" => "-->", "description" => "\"-->\"" ); + $this->peg_c8 = "/wp:"; + $this->peg_c9 = array( "type" => "literal", "value" => "/wp:", "description" => "\"/wp:\"" ); + $this->peg_c10 = "/"; + $this->peg_c11 = array( "type" => "literal", "value" => "/", "description" => "\"/\"" ); + $this->peg_c12 = "{"; + $this->peg_c13 = array( "type" => "literal", "value" => "{", "description" => "\"{\"" ); + $this->peg_c14 = "}"; + $this->peg_c15 = array( "type" => "literal", "value" => "}", "description" => "\"}\"" ); + $this->peg_c16 = ""; + $this->peg_c17 = array("type" => "any", "description" => "any character" ); + $this->peg_c18 = "/^[a-zA-Z]/"; + $this->peg_c19 = array( "type" => "class", "value" => "[a-zA-Z]", "description" => "[a-zA-Z]" ); + $this->peg_c20 = "/^[0-9]/"; + $this->peg_c21 = array( "type" => "class", "value" => "[0-9]", "description" => "[0-9]" ); + $this->peg_c22 = "/^[-_]/"; + $this->peg_c23 = array( "type" => "class", "value" => "[-_]", "description" => "[-_]" ); + $this->peg_c24 = "/^[ \\t\\r\\n]/"; + $this->peg_c25 = array( "type" => "class", "value" => "[ \t\r\n]", "description" => "[ \t\r\n]" ); + $this->peg_c26 = "/^[\\r\\n]/"; + $this->peg_c27 = array( "type" => "class", "value" => "[\r\n]", "description" => "[\r\n]" ); + $this->peg_c28 = "/^[ \\t]/"; + $this->peg_c29 = array( "type" => "class", "value" => "[ \t]", "description" => "[ \t]" ); + + $peg_startRuleFunctions = array( 'Document' => array($this, "peg_parseDocument") ); + $peg_startRuleFunction = array($this, "peg_parseDocument"); + if (isset($options["startRule"])) { + if (!(isset($peg_startRuleFunctions[$options["startRule"]]))) { + throw new Exception("Can't start parsing from rule \"" + $options["startRule"] + "\"."); + } + + $peg_startRuleFunction = $peg_startRuleFunctions[$options["startRule"]]; + } + + /* BEGIN initializer code */ + + // The `maybeJSON` function is not needed in PHP because its return semantics + // are the same as `json_decode` + + /* END initializer code */ + + $peg_result = call_user_func($peg_startRuleFunction); + + mb_regex_encoding($old_regex_encoding); + if ($peg_result !== $this->peg_FAILED && $this->peg_currPos === mb_strlen($input, "UTF-8")) { + return $peg_result; + } else { + if ($peg_result !== $this->peg_FAILED && $this->peg_currPos < mb_strlen($input, "UTF-8")) { + $this->peg_fail(array("type" => "end", "description" => "end of input" )); + } + + throw $this->peg_buildException(null, $this->peg_maxFailExpected, $this->peg_maxFailPos); + } + } + +}; \ No newline at end of file From 4c4fe438926f3d1c09d8c223d62d92cb8ab649ae Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 29 Jun 2017 18:15:59 +0200 Subject: [PATCH 14/18] During Travis build, make sure PHP parser is up to date --- .travis.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cf0ded3b24726a..7d1a864c1a2d07 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,8 +54,16 @@ script: # failed `stat` calls from `filemtime()`. npm install || exit 1 npm run build || exit 1 - # Syntax check for php-pegjs parser + # Make sure phpegjs parser is up to date node bin/create-php-parser.js || exit 1 + if ! git diff --quiet --exit-code lib/parser.php; then + echo 'ERROR: The PEG parser has been updated, but the generated PHP version' + echo ' (lib/parser.php) has not. Run `bin/create-php-parser.js` and' + echo ' commit the resulting changes to resolve this.' + sleep .2 # Otherwise Travis doesn't want to print the whole message + exit 1 + fi + # Check parser syntax php lib/parser.php || exit 1 # Run PHPUnit tests RUN_PARSER_TESTS=1 phpunit || exit 1 From b95043799b4d2efbe8a09c6a6d3ef57f466598dd Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 29 Jun 2017 18:32:46 +0200 Subject: [PATCH 15/18] Fix a few test fixtures that needed to change after rebase --- blocks/test/fixtures/core-cover-image.parsed.json | 2 +- blocks/test/fixtures/core-gallery.parsed.json | 2 +- blocks/test/fixtures/core-latestposts.parsed.json | 3 ++- blocks/test/fixtures/core-pullquote.parsed.json | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/blocks/test/fixtures/core-cover-image.parsed.json b/blocks/test/fixtures/core-cover-image.parsed.json index 437dffbb6c5b9a..21afba0f6d0039 100644 --- a/blocks/test/fixtures/core-cover-image.parsed.json +++ b/blocks/test/fixtures/core-cover-image.parsed.json @@ -4,7 +4,7 @@ "attrs": { "url": "https://cldup.com/uuUqE_dXzy.jpg" }, - "rawContent": "\n
\n\t
\n\t\t

Guten Berg!

\n\t
\n
\n" + "rawContent": "\n
\n\t
\n\t\t

Guten Berg!

\n\t
\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-gallery.parsed.json b/blocks/test/fixtures/core-gallery.parsed.json index 8dae7ead309f97..31f38109c032b0 100644 --- a/blocks/test/fixtures/core-gallery.parsed.json +++ b/blocks/test/fixtures/core-gallery.parsed.json @@ -2,7 +2,7 @@ { "blockName": "core/gallery", "attrs": null, - "rawContent": "\n\n" + "rawContent": "\n
\n\t\n\t\n
\n" }, { "attrs": {}, diff --git a/blocks/test/fixtures/core-latestposts.parsed.json b/blocks/test/fixtures/core-latestposts.parsed.json index dd1b8c6e2ba28c..ce2343fc546e31 100644 --- a/blocks/test/fixtures/core-latestposts.parsed.json +++ b/blocks/test/fixtures/core-latestposts.parsed.json @@ -2,7 +2,8 @@ { "blockName": "core/latestposts", "attrs": { - "poststoshow": 5 + "poststoshow": 5, + "displayPostDate": false }, "rawContent": "" }, diff --git a/blocks/test/fixtures/core-pullquote.parsed.json b/blocks/test/fixtures/core-pullquote.parsed.json index 6261a0064c05ea..92a37a6ba57a70 100644 --- a/blocks/test/fixtures/core-pullquote.parsed.json +++ b/blocks/test/fixtures/core-pullquote.parsed.json @@ -2,7 +2,7 @@ { "blockName": "core/pullquote", "attrs": null, - "rawContent": "\n
\n

Testing pullquote block...

...with a caption
\n
\n" + "rawContent": "\n
\n

Testing pullquote block...

...with a caption
\n
\n" }, { "attrs": {}, From ef05ef18356f4fb7d90a96b9a675deb1bb435268 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Thu, 29 Jun 2017 18:17:09 +0200 Subject: [PATCH 16/18] Modify the PHP parser (this should fail the build) --- lib/parser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/parser.php b/lib/parser.php index b135f0870852e2..ac559d45d19a95 100644 --- a/lib/parser.php +++ b/lib/parser.php @@ -1,4 +1,5 @@ Date: Thu, 29 Jun 2017 18:47:57 +0200 Subject: [PATCH 17/18] Revert "Modify the PHP parser (this should fail the build)" This reverts commit 57389f49de1bce57bf97012df4d207bf2fffdea3. --- lib/parser.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/parser.php b/lib/parser.php index ac559d45d19a95..b135f0870852e2 100644 --- a/lib/parser.php +++ b/lib/parser.php @@ -1,5 +1,4 @@ Date: Thu, 29 Jun 2017 19:07:36 +0200 Subject: [PATCH 18/18] Use PHP PEG parser in plugin --- gutenberg.php | 1 + lib/blocks.php | 58 ++++++++++++++------------------------------------ 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/gutenberg.php b/gutenberg.php index 97ec81112800d0..711608f206c2b8 100644 --- a/gutenberg.php +++ b/gutenberg.php @@ -19,6 +19,7 @@ require_once dirname( __FILE__ ) . '/lib/blocks.php'; require_once dirname( __FILE__ ) . '/lib/client-assets.php'; require_once dirname( __FILE__ ) . '/lib/i18n.php'; + require_once dirname( __FILE__ ) . '/lib/parser.php'; require_once dirname( __FILE__ ) . '/lib/register.php'; // Register server-side code for individual blocks. diff --git a/lib/blocks.php b/lib/blocks.php index 7aba03c076fcad..4e3b7c0c7a2e82 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -81,50 +81,24 @@ function unregister_block_type( $name ) { function do_blocks( $content ) { global $wp_registered_blocks; - // Extract the blocks from the post content. - $matcher = '#' . join( '', array( - '(?P).)*?)', - '\s*/?-->\n?)', - '(?:', - '(?P.*?)', - '(?P\n?)', - ')?', - ) ) . '#s'; - preg_match_all( $matcher, $content, $matches, PREG_OFFSET_CAPTURE ); - - $new_content = $content; - $offset_differential = 0; - foreach ( $matches[0] as $index => $block_match ) { - $block_name = $matches['block_name'][ $index ][0]; - - $output = ''; - if ( isset( $wp_registered_blocks[ $block_name ] ) ) { - $block_attributes_string = trim( $matches['attributes'][ $index ][0] ); - $block_attributes = json_decode( $block_attributes_string, true ); - if ( ! is_array( $block_attributes ) ) { - $block_attributes = array(); - } - - // Call the block's render function to generate the dynamic output. - $output = call_user_func( $wp_registered_blocks[ $block_name ]['render'], $block_attributes ); - } elseif ( isset( $matches['content'][ $index ][0] ) ) { - $output = $matches['content'][ $index ][0]; + $parser = new Gutenberg_PEG_Parser; + $blocks = $parser->parse( $content ); + + $content_after_blocks = ''; + + foreach ( $blocks as $block ) { + $block_name = isset( $block['blockName'] ) ? $block['blockName'] : null; + $attributes = is_array( $block['attrs'] ) ? $block['attrs'] : array(); + if ( $block_name && isset( $wp_registered_blocks[ $block_name ] ) ) { + $content_after_blocks .= call_user_func( + $wp_registered_blocks[ $block_name ]['render'], + $attributes + ); + } else { + $content_after_blocks .= $block['rawContent']; } - - // Replace the matched block with the static or dynamic output. - $new_content = substr_replace( - $new_content, - $output, - $block_match[1] - $offset_differential, - strlen( $block_match[0] ) - ); - - // Update offset for the next replacement. - $offset_differential += strlen( $block_match[0] ) - strlen( $output ); } - return $new_content; + return $content_after_blocks; } add_filter( 'the_content', 'do_blocks', 9 ); // BEFORE do_shortcode() and wpautop().