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

Properly convert successive shortcodes #7846

Merged
merged 6 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
66 changes: 66 additions & 0 deletions blocks/api/raw-handling/test/shortcode-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,70 @@ describe( 'segmentHTMLToShortcodeBlock', () => {

<p>Bar</p>` );
} );

it( 'should convert two instances of the same shortcode', () => {
const original = `<p>[foo one]</p>
<p>[foo two]</p>`;

const transformed = segmentHTMLToShortcodeBlock( original, 0 );
expect( transformed[ 0 ] ).toEqual( '<p>' );
const firstExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo one]',
} );
// uid will always be random.
firstExpectedBlock.uid = transformed[ 1 ].uid;
expect( transformed[ 1 ] ).toEqual( firstExpectedBlock );
expect( transformed[ 2 ] ).toEqual( `</p>
<p>` );
const secondExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo two]',
} );
// uid will always be random.
secondExpectedBlock.uid = transformed[ 3 ].uid;
expect( transformed[ 3 ] ).toEqual( secondExpectedBlock );
expect( transformed[ 4 ] ).toEqual( '</p>' );
expect( transformed ).toHaveLength( 5 );
} );

it( 'should convert four instances of the same shortcode', () => {
const original = `<p>[foo one]</p>
<p>[foo two]</p>
<p>[foo three]</p>
<p>[foo four]</p>`;

const transformed = segmentHTMLToShortcodeBlock( original, 0 );
expect( transformed[ 0 ] ).toEqual( '<p>' );
const firstExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo one]',
} );
// uid will always be random.
firstExpectedBlock.uid = transformed[ 1 ].uid;
expect( transformed[ 1 ] ).toEqual( firstExpectedBlock );
expect( transformed[ 2 ] ).toEqual( `</p>
<p>` );
const secondExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo two]',
} );
// uid will always be random.
secondExpectedBlock.uid = transformed[ 3 ].uid;
expect( transformed[ 3 ] ).toEqual( secondExpectedBlock );
expect( transformed[ 4 ] ).toEqual( `</p>
<p>` );
const thirdExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo three]',
} );
// uid will always be random.
thirdExpectedBlock.uid = transformed[ 5 ].uid;
expect( transformed[ 5 ] ).toEqual( thirdExpectedBlock );
expect( transformed[ 6 ] ).toEqual( `</p>
<p>` );
const fourthExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo four]',
} );
// uid will always be random.
fourthExpectedBlock.uid = transformed[ 7 ].uid;
expect( transformed[ 7 ] ).toEqual( fourthExpectedBlock );
expect( transformed[ 8 ] ).toEqual( '</p>' );
expect( transformed ).toHaveLength( 9 );
} );
} );
4 changes: 2 additions & 2 deletions packages/shortcode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ export function string( options ) {
*
* @return {RegExp} Shortcode RegExp.
*/
export const regexp = memize( ( tag ) => {
export const regexp = ( tag ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Simply for consistency, we could format this as the other plain functions in this file now:

export function regexp( tag ) {
	 // ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply for consistency, we could format this as the other plain functions in this file now:

Good point, 1bf5ba0

return new RegExp( '\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g' );
} );
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi-colon should be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't hate you if you just removed it yourself :) a712bf7


/**
* Parse shortcode attributes.
Expand Down