From 044bc221834ab23a88a26854645bb1b07efa565e Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 8 Mar 2019 19:24:08 +0000 Subject: [PATCH] Fix: Quote to heading transform --- packages/block-library/src/quote/index.js | 30 +++-- .../blocks/__snapshots__/quote.test.js.snap | 108 ++++++++++++------ packages/e2e-tests/specs/blocks/quote.test.js | 35 +++++- 3 files changed, 122 insertions(+), 51 deletions(-) diff --git a/packages/block-library/src/quote/index.js b/packages/block-library/src/quote/index.js index 535dd29e4d8b5..20458db83572e 100644 --- a/packages/block-library/src/quote/index.js +++ b/packages/block-library/src/quote/index.js @@ -186,21 +186,27 @@ export const settings = { } const pieces = split( create( { html: value, multilineTag: 'p' } ), '\u2028' ); + + const headingBlock = createBlock( 'core/heading', { + content: toHTMLString( { value: pieces[ 0 ] } ), + } ); + + if ( ! citation && pieces.length === 1 ) { + return headingBlock; + } + const quotePieces = pieces.slice( 1 ); - return [ - createBlock( 'core/heading', { - content: toHTMLString( { value: pieces[ 0 ] } ), - } ), - createBlock( 'core/quote', { - ...attrs, - citation, - value: toHTMLString( { - value: quotePieces.length ? join( pieces.slice( 1 ), '\u2028' ) : create(), - multilineTag: 'p', - } ), + const quoteBlock = createBlock( 'core/quote', { + ...attrs, + citation, + value: toHTMLString( { + value: quotePieces.length ? join( pieces.slice( 1 ), '\u2028' ) : create(), + multilineTag: 'p', } ), - ]; + } ); + + return [ headingBlock, quoteBlock ]; }, }, diff --git a/packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap b/packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap index 6a46b767390ef..57d1d187978f4 100644 --- a/packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap +++ b/packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap @@ -6,44 +6,6 @@ exports[`Quote can be converted to a pullquote 1`] = ` " `; -exports[`Quote can be converted to headings 1`] = ` -" -

one

- - - -

two

cite
-" -`; - -exports[`Quote can be converted to headings 2`] = ` -" -

one

- - - -

two

- - - -

cite
-" -`; - -exports[`Quote can be converted to headings 3`] = ` -" -

one

- - - -

two

- - - -

cite

-" -`; - exports[`Quote can be converted to paragraphs and renders a paragraph for the cite, if it exists 1`] = ` "

one

@@ -117,3 +79,73 @@ exports[`Quote can be merged into from a paragraph 1`] = `

test

" `; + +exports[`Quote is transformed to a heading and a quote if the quote contains a citation 1`] = ` +" +

one

+ + + +

cite
+" +`; + +exports[`Quote is transformed to a heading and a quote if the quote contains multiple paragraphs 1`] = ` +" +

one

+ + + +

two

three

+" +`; + +exports[`Quote is transformed to a heading if the quote just contains one paragraph 1`] = ` +" +

one

+" +`; + +exports[`Quote is transformed to an empty heading if the quote is empty 1`] = ` +" +

+" +`; + +exports[`Quote the resuling quote after transforming to a heading can be transformed again 1`] = ` +" +

one

+ + + +

two

cite
+" +`; + +exports[`Quote the resuling quote after transforming to a heading can be transformed again 2`] = ` +" +

one

+ + + +

two

+ + + +

cite
+" +`; + +exports[`Quote the resuling quote after transforming to a heading can be transformed again 3`] = ` +" +

one

+ + + +

two

+ + + +

cite

+" +`; diff --git a/packages/e2e-tests/specs/blocks/quote.test.js b/packages/e2e-tests/specs/blocks/quote.test.js index 11f1fd5e08638..67e279ea19038 100644 --- a/packages/e2e-tests/specs/blocks/quote.test.js +++ b/packages/e2e-tests/specs/blocks/quote.test.js @@ -116,7 +116,40 @@ describe( 'Quote', () => { expect( await getEditedPostContent() ).toMatchSnapshot(); } ); - it( 'can be converted to headings', async () => { + it( 'is transformed to an empty heading if the quote is empty', async () => { + await insertBlock( 'Quote' ); + await transformBlockTo( 'Heading' ); + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); + + it( 'is transformed to a heading if the quote just contains one paragraph', async () => { + await insertBlock( 'Quote' ); + await page.keyboard.type( 'one' ); + await transformBlockTo( 'Heading' ); + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); + + it( 'is transformed to a heading and a quote if the quote contains multiple paragraphs', async () => { + await insertBlock( 'Quote' ); + await page.keyboard.type( 'one' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( 'two' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( 'three' ); + await transformBlockTo( 'Heading' ); + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); + + it( 'is transformed to a heading and a quote if the quote contains a citation', async () => { + await insertBlock( 'Quote' ); + await page.keyboard.type( 'one' ); + await page.keyboard.press( 'Tab' ); + await page.keyboard.type( 'cite' ); + await transformBlockTo( 'Heading' ); + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); + + it( 'the resuling quote after transforming to a heading can be transformed again', async () => { await insertBlock( 'Quote' ); await page.keyboard.type( 'one' ); await page.keyboard.press( 'Enter' );