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

Conversation

danielbachhuber
Copy link
Member

@danielbachhuber danielbachhuber commented Jul 9, 2018

Description

Removes memize() call around wp.shortcode.regexp() to avoid buggy behavior when converting successive shortcodes.

Fixes #7030

How has this been tested?

  1. Add the following code to a Classic Editor post:
[foo one]

[foo two]

[foo three]
  1. Open the post in Gutenberg.
  2. Hit 'Convert to Blocks' and see that all shortcode instances have been converted to blocks.

multipleshortcodes

Types of changes

Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@danielbachhuber
Copy link
Member Author

To debug the test case, add a console.log() to view beforeHTML:

image

When you do so, you'll notice that console.log() is only run three times, not four:

image

But why is segmentHTMLToShortcodeBlock() only being called three times, when it should be called four?

Adding another console.log() as the HTML is processed:

image

Take a look at the last string instance output:

image

The last instance is different than its peers in that: if you remove the shortcode, then the remaining HTML is simply a </p> tag (i.e. unbalanced HTML).

I don't have any good ideas on how to unwind this issue though.

@azaozz @iseulde @aduth Pointers?

@danielbachhuber danielbachhuber added [Status] Blocked Used to indicate that a current effort isn't able to move forward [Feature] Paste labels Jul 10, 2018
@aduth
Copy link
Member

aduth commented Jul 11, 2018

Chatted about this with @danielbachhuber , seems to stem from this related Core issue:

https://core.trac.wordpress.org/ticket/35545

Which is that we're using shortcode regular expressions as stateful (RegExp#exec) while also memoizing their creation, thus causing unpredictable results like the one surfaced here.

There's not much to go by so far as the context for memoizing in the initial introduction of the shortcodes utility. Typically it'd be for performance, though in this case we're simply concatenating a string into a new regular expression, so doubtful we're really saving much (the overhead of memoization is likely more costly). Given that we are using the regular expressions as stateful, it may have been intentional to return a cached copy, but again the context is unclear.

My intuition is: Remove memize wrapper from wp.shortcode.regexp. This fixes #7030.

cc @iseulde @azaozz @koop

@danielbachhuber danielbachhuber added the [Type] Bug An existing feature does not function as intended label Jul 11, 2018
@danielbachhuber danielbachhuber changed the title [WIP] Properly convert successive shortcodes Properly convert successive shortcodes Jul 11, 2018
@danielbachhuber danielbachhuber removed the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Jul 11, 2018
@danielbachhuber danielbachhuber added this to the 3.3 milestone Jul 11, 2018
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@@ -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

@danielbachhuber
Copy link
Member Author

Merging to incorporate into #7892. The failure is a codecov false positive.

@danielbachhuber danielbachhuber merged commit 47bc275 into master Jul 11, 2018
@danielbachhuber danielbachhuber deleted the 7030-successive-shortcodes branch July 11, 2018 17:16
@ktmn
Copy link

ktmn commented Jul 15, 2018

Does it also handle nesting?

[foo]
  [bar]
    [asd /]
  [/bar]
[/foo]
[foo]
  [bar][/bar]
[/foo]

Would it create 2 blocks?

@wpexplorer
Copy link

There seems to be issues still with converting blocks that are next to each other. I recorded a quick video to show you: https://a.cl.ly/jkuOjvOQ

There are many page builders out there that are shortcode based and basically everything added to the page are shortcodes and there isn't any space between them. So there is no way currently to convert these page builders to Blocks without having to first manually space out the shortcodes.

Thanks for all the great works you guys do over there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Paste [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[2.9.2] Convert to Blocks not properly converting successive Shortcodes
4 participants