Skip to content

Commit

Permalink
Raw handling: Skip shortcode if not on its own line (#19059)
Browse files Browse the repository at this point in the history
Improves the existing logic for detection of "inline shortcodes" by
ensuring that a shortcodes stands not only at the start of a
line/paragraph, but also at the end of one.
  • Loading branch information
mcsf authored Dec 12, 2019
1 parent bfc58f5 commit da6d2c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/blocks/src/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ function segmentHTMLToShortcodeBlock( HTML, lastIndex = 0, excludedBlockNames =
const previousIndex = lastIndex;

if ( ( match = next( transformTag, HTML, lastIndex ) ) ) {
const beforeHTML = HTML.substr( 0, match.index );

lastIndex = match.index + match.content.length;
const beforeHTML = HTML.substr( 0, match.index );
const afterHTML = HTML.substr( lastIndex );

// If the shortcode content does not contain HTML and the shortcode is
// not on a new line (or in paragraph from Markdown converter),
// consider the shortcode as inline text, and thus skip conversion for
// this segment.
if (
! includes( match.shortcode.content || '', '<' ) &&
! /(\n|<p>)\s*$/.test( beforeHTML )
! (
/(\n|<p>)\s*$/.test( beforeHTML ) &&
/^\s*(\n|<\/p>)/.test( afterHTML )
)
) {
return segmentHTMLToShortcodeBlock( HTML, lastIndex );
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/shortcode-converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ describe( 'segmentHTMLToShortcodeBlock', () => {
expect( transformed ).toHaveLength( 9 );
} );

it( 'should not convert inline shortcodes', () => {
const originalInASentence = `<p>Here is a nice [foo shortcode].</p>`;
expect( segmentHTMLToShortcodeBlock( originalInASentence, 0 ) )
.toEqual( [ originalInASentence ] );

const originalMultipleShortcodes = `<p>[foo bar] [baz quux]</p>`;
expect( segmentHTMLToShortcodeBlock( originalMultipleShortcodes, 0 ) )
.toEqual( [ originalMultipleShortcodes ] );
} );

it( 'should convert regardless of shortcode alias', () => {
const original = `<p>[my-gallery ids="1,2,3"]</p>
<p>[my-bunch-of-images ids="4,5,6"]</p>`;
Expand Down

0 comments on commit da6d2c2

Please sign in to comment.