Skip to content

Commit

Permalink
Fix: Quote to heading transform (#14348)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored and youknowriad committed Mar 20, 2019
1 parent cb5cc58 commit f168939
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 51 deletions.
30 changes: 18 additions & 12 deletions packages/block-library/src/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
},
},

Expand Down
108 changes: 70 additions & 38 deletions packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@ exports[`Quote can be converted to a pullquote 1`] = `
<!-- /wp:pullquote -->"
`;
exports[`Quote can be converted to headings 1`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>two</p><cite>cite</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote can be converted to headings 2`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>two</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p><cite>cite</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote can be converted to headings 3`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>two</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>cite</h2>
<!-- /wp:heading -->"
`;
exports[`Quote can be converted to paragraphs and renders a paragraph for the cite, if it exists 1`] = `
"<!-- wp:paragraph -->
<p>one</p>
Expand Down Expand Up @@ -117,3 +79,73 @@ exports[`Quote can be merged into from a paragraph 1`] = `
<blockquote class=\\"wp-block-quote\\"><p>test</p></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote is transformed to a heading and a quote if the quote contains a citation 1`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p><cite>cite</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote is transformed to a heading and a quote if the quote contains multiple paragraphs 1`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>two</p><p>three</p></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote is transformed to a heading if the quote just contains one paragraph 1`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->"
`;
exports[`Quote is transformed to an empty heading if the quote is empty 1`] = `
"<!-- wp:heading -->
<h2></h2>
<!-- /wp:heading -->"
`;
exports[`Quote the resuling quote after transforming to a heading can be transformed again 1`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p>two</p><cite>cite</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote the resuling quote after transforming to a heading can be transformed again 2`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>two</h2>
<!-- /wp:heading -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p><cite>cite</cite></blockquote>
<!-- /wp:quote -->"
`;
exports[`Quote the resuling quote after transforming to a heading can be transformed again 3`] = `
"<!-- wp:heading -->
<h2>one</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>two</h2>
<!-- /wp:heading -->
<!-- wp:heading -->
<h2>cite</h2>
<!-- /wp:heading -->"
`;
35 changes: 34 additions & 1 deletion packages/e2e-tests/specs/blocks/quote.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit f168939

Please sign in to comment.