Skip to content
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

Fix converting caption shortcode with link #12315

Merged
merged 3 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const settings = {
caption: {
shortcode: ( attributes, { shortcode } ) => {
const { content } = shortcode;
return content.replace( /\s*<img[^>]*>\s/, '' );
return content.replace( /\s*(?:<a[^>]*>\s*)?<img[^>]*>(?:\s*<\/a>)?\s*/, '' );
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
},
},
href: {
Expand Down
14 changes: 13 additions & 1 deletion test/integration/__snapshots__/blocks-raw-handling.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Blocks raw handling rawHandler should convert HTML post to blocks with minimal content changes 1`] = `
exports[`rawHandler should convert HTML post to blocks with minimal content changes 1`] = `
"<!-- wp:heading -->
<h2>Howdy</h2>
<!-- /wp:heading -->
Expand Down Expand Up @@ -54,3 +54,15 @@ exports[`Blocks raw handling rawHandler should convert HTML post to blocks with
<blockquote><h1>Heading</h1><p>Text.</p></blockquote>
<!-- /wp:html -->"
`;

exports[`rawHandler should convert a caption shortcode 1`] = `
"<!-- wp:image {\\"id\\":122,\\"align\\":\\"none\\",\\"className\\":\\"size-medium wp-image-122\\"} -->
<figure class=\\"wp-block-image alignnone size-medium wp-image-122\\"><img src=\\"image.png\\" alt=\\"\\" class=\\"wp-image-122\\"/><figcaption>test</figcaption></figure>
<!-- /wp:image -->"
`;

exports[`rawHandler should convert a caption shortcode with link 1`] = `
"<!-- wp:image {\\"id\\":754,\\"align\\":\\"none\\"} -->
<figure class=\\"wp-block-image alignnone\\"><a href=\\"http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg\\"><img src=\\"http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg?w=604\\" alt=\\"Bell on Wharf\\" class=\\"wp-image-754\\"/></a><figcaption>Bell on wharf in San Francisco</figcaption></figure>
<!-- /wp:image -->"
`;
20 changes: 15 additions & 5 deletions test/integration/blocks-raw-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,21 @@ describe( 'Blocks raw handling', () => {
} );
} );
} );
} );

describe( 'rawHandler', () => {
it( 'should convert HTML post to blocks with minimal content changes', () => {
const HTML = readFile( path.join( __dirname, 'fixtures/wordpress-convert.html' ) );
expect( serialize( rawHandler( { HTML } ) ) ).toMatchSnapshot();
} );
describe( 'rawHandler', () => {
it( 'should convert HTML post to blocks with minimal content changes', () => {
const HTML = readFile( path.join( __dirname, 'fixtures/wordpress-convert.html' ) );
expect( serialize( rawHandler( { HTML } ) ) ).toMatchSnapshot();
} );

it( 'should convert a caption shortcode', () => {
const HTML = readFile( path.join( __dirname, 'fixtures/shortcode-caption.html' ) );
expect( serialize( rawHandler( { HTML } ) ) ).toMatchSnapshot();
} );

it( 'should convert a caption shortcode with link', () => {
const HTML = readFile( path.join( __dirname, 'fixtures/shortcode-caption-with-link.html' ) );
expect( serialize( rawHandler( { HTML } ) ) ).toMatchSnapshot();
} );
} );
3 changes: 0 additions & 3 deletions test/integration/fixtures/caption-shortcode-out.html

This file was deleted.

1 change: 1 addition & 0 deletions test/integration/fixtures/shortcode-caption-with-link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[caption id="attachment_754" align="alignnone" width="604"]<a href="http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg"><img class="wp-image-754 size-large" src="http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg?w=604" alt="Bell on Wharf" width="604" height="453" /></a> Bell on wharf in San Francisco[/caption]