Repository navigation
Fix and regenerate invalid block fixtures #2056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c8f55a7
80ca6d0
eccc524
69eabb5
fefcd7f
2f5a485
269cac5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,3 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { noop } from 'lodash'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
|
|
@@ -26,6 +21,12 @@ describe( 'block parser', () => { | |
| const defaultBlockSettings = { | ||
| save: ( { attributes } ) => attributes.fruit, | ||
| }; | ||
| const identityBlock = { | ||
| attributes( rawContent ) { | ||
| return { content: rawContent }; | ||
| }, | ||
| save: ( { attributes } ) => attributes.content, | ||
| }; | ||
|
|
||
| afterEach( () => { | ||
| setUnknownTypeHandler( undefined ); | ||
|
|
@@ -97,11 +98,11 @@ describe( 'block parser', () => { | |
| it( 'returns false is block is not valid', () => { | ||
| registerBlockType( 'core/test-block', defaultBlockSettings ); | ||
|
|
||
| expect( isValidBlock( | ||
| expect( () => isValidBlock( | ||
| 'Apples', | ||
| getBlockType( 'core/test-block' ), | ||
| { fruit: 'Bananas' } | ||
| ) ).toBe( false ); | ||
| ) ).toThrow(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we test a specific error message here, or save this for later when we're ready to expose this info to the UI?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a pain to test with the ANSI escapes, unless we were to disable that for testing, or just test the pattern.
Which I'm starting to wonder, do we want the error message exposed in the UI anywhere? Currently we're only generating and throwing the error in development modes, which means it's not something generally accessible in the UI (in distributed plugin anyways).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify, I think what should be shown in the UI is more like a text-mode diff and a prompt to confirm that the changes are acceptable. |
||
| } ); | ||
|
|
||
| it( 'returns true is block is valid', () => { | ||
|
|
@@ -117,27 +118,30 @@ describe( 'block parser', () => { | |
|
|
||
| describe( 'createBlockWithFallback', () => { | ||
| it( 'should create the requested block if it exists', () => { | ||
| registerBlockType( 'core/test-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/test-block', identityBlock ); | ||
|
|
||
| const block = createBlockWithFallback( | ||
| 'core/test-block', | ||
| 'content', | ||
| { attr: 'value' } | ||
| ); | ||
| expect( block.name ).toEqual( 'core/test-block' ); | ||
| expect( block.attributes ).toEqual( { attr: 'value' } ); | ||
| expect( block.attributes ).toEqual( { | ||
| attr: 'value', | ||
| content: 'content', | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should create the requested block with no attributes if it exists', () => { | ||
| registerBlockType( 'core/test-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/test-block', identityBlock ); | ||
|
|
||
| const block = createBlockWithFallback( 'core/test-block', 'content' ); | ||
| expect( block.name ).toEqual( 'core/test-block' ); | ||
| expect( block.attributes ).toEqual( {} ); | ||
| expect( block.attributes ).toEqual( { content: 'content' } ); | ||
| } ); | ||
|
|
||
| it( 'should fall back to the unknown type handler for unknown blocks if present', () => { | ||
| registerBlockType( 'core/unknown-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/unknown-block', identityBlock ); | ||
| setUnknownTypeHandler( 'core/unknown-block' ); | ||
|
|
||
| const block = createBlockWithFallback( | ||
|
|
@@ -146,16 +150,19 @@ describe( 'block parser', () => { | |
| { attr: 'value' } | ||
| ); | ||
| expect( block.name ).toEqual( 'core/unknown-block' ); | ||
| expect( block.attributes ).toEqual( { attr: 'value' } ); | ||
| expect( block.attributes ).toEqual( { | ||
| attr: 'value', | ||
| content: 'content', | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should fall back to the unknown type handler if block type not specified', () => { | ||
| registerBlockType( 'core/unknown-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/unknown-block', identityBlock ); | ||
| setUnknownTypeHandler( 'core/unknown-block' ); | ||
|
|
||
| const block = createBlockWithFallback( null, 'content' ); | ||
| expect( block.name ).toEqual( 'core/unknown-block' ); | ||
| expect( block.attributes ).toEqual( {} ); | ||
| expect( block.attributes ).toEqual( { content: 'content' } ); | ||
| } ); | ||
|
|
||
| it( 'should not create a block if no unknown type handler', () => { | ||
|
|
@@ -166,15 +173,7 @@ describe( 'block parser', () => { | |
|
|
||
| describe( 'parse()', () => { | ||
| it( 'should parse the post content, including block attributes', () => { | ||
| registerBlockType( 'core/test-block', { | ||
| // Currently this is the only way to test block content parsing? | ||
| attributes: function( rawContent ) { | ||
| return { | ||
| content: rawContent, | ||
| }; | ||
| }, | ||
| save: noop, | ||
| } ); | ||
| registerBlockType( 'core/test-block', identityBlock ); | ||
|
|
||
| const parsed = parse( | ||
| '<!-- wp:core/test-block {"smoked":"yes","url":"http://google.com","chicken":"ribs & \'wings\'"} -->' + | ||
|
|
@@ -200,17 +199,10 @@ describe( 'block parser', () => { | |
| } ); | ||
|
|
||
| it( 'should parse the post content, ignoring unknown blocks', () => { | ||
| registerBlockType( 'core/test-block', { | ||
| attributes: function( rawContent ) { | ||
| return { | ||
| content: rawContent + ' & Chicken', | ||
| }; | ||
| }, | ||
| save: noop, | ||
| } ); | ||
| registerBlockType( 'core/test-block', identityBlock ); | ||
|
|
||
| const parsed = parse( | ||
| '<!-- wp:core/test-block -->\nRibs\n<!-- /wp:core/test-block -->' + | ||
| '<!-- wp:core/test-block -->Ribs & Chicken<!-- /wp:core/test-block -->' + | ||
| '<p>Broccoli</p>' + | ||
| '<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->' | ||
| ); | ||
|
|
@@ -225,12 +217,12 @@ describe( 'block parser', () => { | |
|
|
||
| it( 'should parse the post content, using unknown block handler', () => { | ||
| registerBlockType( 'core/test-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/unknown-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/unknown-block', identityBlock ); | ||
|
|
||
| setUnknownTypeHandler( 'core/unknown-block' ); | ||
|
|
||
| const parsed = parse( | ||
| '<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' + | ||
| '<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' + | ||
| '<p>Broccoli</p>' + | ||
| '<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->' | ||
| ); | ||
|
|
@@ -245,23 +237,15 @@ describe( 'block parser', () => { | |
|
|
||
| it( 'should parse the post content, including raw HTML at each end', () => { | ||
| registerBlockType( 'core/test-block', defaultBlockSettings ); | ||
| registerBlockType( 'core/unknown-block', { | ||
| // Currently this is the only way to test block content parsing? | ||
| attributes: function( rawContent ) { | ||
| return { | ||
| content: rawContent, | ||
| }; | ||
| }, | ||
| save: noop, | ||
| } ); | ||
| registerBlockType( 'core/unknown-block', identityBlock ); | ||
|
|
||
| setUnknownTypeHandler( 'core/unknown-block' ); | ||
|
|
||
| const parsed = parse( | ||
| '<p>Cauliflower</p>' + | ||
| '<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' + | ||
| '<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' + | ||
| '\n<p>Broccoli</p>\n' + | ||
| '<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' + | ||
| '<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' + | ||
| '<p>Romanesco</p>' | ||
| ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:core/cover-text {"backgroundColor":"#fcb900"} --> | ||
| <div style="background-color:#fcb900"><p>Text with color.</p></div> | ||
| <div style="background-color:#fcb900;" class="wp-block-cover-text"><p>Text with color.</p></div> | ||
| <!-- /wp:core/cover-text --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <!-- wp:core/cover-text {"backgroundColor":"#fcb900"} --> | ||
| <div style="background-color:#fcb900"> | ||
| <div style="background-color:#fcb900;" class="wp-block-cover-text"> | ||
| <p>Text with color.</p> | ||
| </div> | ||
| <!-- /wp:core/cover-text --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <!-- wp:core/cover-text {"backgroundColor":"#fcb900"} --> | ||
| <div style="background-color:#fcb900"><p>Text with color.</p></div> | ||
| <!-- /wp:core/cover-text --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [ | ||
| { | ||
| "uid": "_uid_0", | ||
| "name": "core/cover-text", | ||
| "isValid": false, | ||
| "attributes": { | ||
| "backgroundColor": "#fcb900", | ||
| "content": [ | ||
| [ | ||
| "Text with color." | ||
| ] | ||
| ] | ||
| }, | ||
| "error": {}, | ||
| "originalContent": "<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [ | ||
| { | ||
| "blockName": "core/cover-text", | ||
| "attrs": { | ||
| "backgroundColor": "#fcb900" | ||
| }, | ||
| "rawContent": "\n<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>\n" | ||
| }, | ||
| { | ||
| "attrs": {}, | ||
| "rawContent": "\n" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,6 @@ | ||
| <!-- wp:core/gallery {"images":[{"sizes":{"full":{"url":"https://cldup.com/uuUqE_dXzy.jpg","height":1080,"width":810,"orientation":"portrait"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":1,"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title"},{"sizes":{"full":{"url":"http://google.com/hi.png","height":1080,"width":1440,"orientation":"landscape"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":2,"url":"http://google.com/hi.png","alt":"title"}]} --> | ||
| <div class="wp-block-gallery"> | ||
| <figure class="blocks-gallery-image"> | ||
| <img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" /> | ||
| </figure> | ||
| <figure class="blocks-gallery-image"> | ||
| <img src="http://google.com/hi.png" alt="title" /> | ||
| </figure> | ||
| <div class="wp-block-gallery alignnone columns-2 is-cropped"> | ||
| <figure class="blocks-gallery-image"><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" /></figure> | ||
| <figure class="blocks-gallery-image"><img src="http://google.com/hi.png" alt="title" /></figure> | ||
| </div> | ||
| <!-- /wp:core/gallery --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or teach
chalkto output HTML. ANSI escape sequences are quite convoluted, and it seems like what we really want is an intermediate representation that can be outputted in either form.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did run across this: https://www.npmjs.com/package/ansi_up
I'd also toyed with ideas for converting ANSI automatically to CSS-styled
console.logdepending on the environment.