From da6d2c2bb7d54acd65bd6f18f7569f8d2d38fc75 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Thu, 12 Dec 2019 11:51:52 +0000 Subject: [PATCH] Raw handling: Skip shortcode if not on its own line (#19059) 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. --- .../blocks/src/api/raw-handling/shortcode-converter.js | 9 ++++++--- test/integration/shortcode-converter.test.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/blocks/src/api/raw-handling/shortcode-converter.js b/packages/blocks/src/api/raw-handling/shortcode-converter.js index 807b90e279f79..c15b4cce3f5ad 100644 --- a/packages/blocks/src/api/raw-handling/shortcode-converter.js +++ b/packages/blocks/src/api/raw-handling/shortcode-converter.js @@ -36,9 +36,9 @@ 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), @@ -46,7 +46,10 @@ function segmentHTMLToShortcodeBlock( HTML, lastIndex = 0, excludedBlockNames = // this segment. if ( ! includes( match.shortcode.content || '', '<' ) && - ! /(\n|

)\s*$/.test( beforeHTML ) + ! ( + /(\n|

)\s*$/.test( beforeHTML ) && + /^\s*(\n|<\/p>)/.test( afterHTML ) + ) ) { return segmentHTMLToShortcodeBlock( HTML, lastIndex ); } diff --git a/test/integration/shortcode-converter.test.js b/test/integration/shortcode-converter.test.js index dc59ee49d2fb5..305301e0237bb 100644 --- a/test/integration/shortcode-converter.test.js +++ b/test/integration/shortcode-converter.test.js @@ -219,6 +219,16 @@ describe( 'segmentHTMLToShortcodeBlock', () => { expect( transformed ).toHaveLength( 9 ); } ); + it( 'should not convert inline shortcodes', () => { + const originalInASentence = `

Here is a nice [foo shortcode].

`; + expect( segmentHTMLToShortcodeBlock( originalInASentence, 0 ) ) + .toEqual( [ originalInASentence ] ); + + const originalMultipleShortcodes = `

[foo bar] [baz quux]

`; + expect( segmentHTMLToShortcodeBlock( originalMultipleShortcodes, 0 ) ) + .toEqual( [ originalMultipleShortcodes ] ); + } ); + it( 'should convert regardless of shortcode alias', () => { const original = `

[my-gallery ids="1,2,3"]

[my-bunch-of-images ids="4,5,6"]

`;