WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 2 months ago

#43686 new defect (bug)

Shortcodes containing asterisks may create invalid regex breaking the editor

Reported by: Vusys Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.9.4
Component: Shortcodes Keywords: has-patch dev-feedback 2nd-opinion
Focuses: administration Cc:

Description

Despite not being a reserved character an asterisk in a shortcode followed by another character will generate invalid regex which breaks the editor.

This code reproduces the issue:

<?php
foreach (['*one', '*two'] as $bullet) {
    add_shortcode('*' . $bullet, function () use ($bullet) {
        return 'oh no ' . $bullet;
    });
}

Attached is a gif showing the editor tabs not working correctly along with the error in the console in Chrome 65 and Firefox 59.

I know it's unconventional to use an asterisk in a shortcode, but my use case is porting a legacy CMS that used BBCode into WordPress while trying to keep as much of the original formatting possible for old content.

This is an issue with all plugins deactivated (except for the above code) using the Twenty Seventeen theme.

Attachments (3)

2018-04-03 22.30.25.gif (725.5 KB) - added by Vusys 3 months ago.
demo
Screen Shot 2018-04-03 at 22.33.15.png (102.4 KB) - added by Vusys 3 months ago.
console errors
43686.diff (709 bytes) - added by soulseekah 3 months ago.
wordCount patch

Download all attachments as: .zip

Change History (6)

@Vusys
3 months ago

demo

@Vusys
3 months ago

console errors

@soulseekah
3 months ago

wordCount patch

#1 @soulseekah
3 months ago

  • Keywords has-patch dev-feedback 2nd-opinion added

Welcome to Trac! :)

The Word count functionality breaks if there's a registered shortcode with asterisk character in it.

Shortcode:

add_shortcode( '*stars*', function() {
	return '★★★';
} );

Note, that the * symbol is specifically allowed in shortcode names as per the tests:

Tests_Shortcode::test_registration_good with data set #2 ('unreserved!#$%()*+,-.;?@^_{|}~chars', true)

The error?

word-count.js?ver=5.0-alpha-42125-src:64 Uncaught SyntaxError: Invalid regular expression: /\[\/?(?:wp_caption|caption|gallery|playlist|audio|video|embed|*stars*)[^\]]*?\]/: Nothing to repeat
    at new RegExp (<anonymous>)
    at new WordCounter (word-count.js?ver=5.0-alpha-42125-src:64)
    at post.js?ver=5.0-alpha-42125-src:1270

this.settings.shortcodesRegExp·=·new·RegExp(·'\\[\\/?(?:'·+·shortcodes.join(·'|'·)·+·')[^\\]]*?\\]',·'g'·);, where shortcodes is a JSON object:

/* <![CDATA[ */
var wordCountL10n = {"type":"words","shortcodes":["wp_caption","caption","gallery","playlist","audio","video","embed","*stars*"]};
/* ]]> */

43686.diff patches the regular expression escaping all special characters as needed.

I suspect that other issues may arise, since there may be other regular expressions in there. Your QTags error is probably related, but it would be crucial to see your QTags addition code to see what you're doing, otherwise no other errors are occurring on a clean WordPress install.

Last edited 3 months ago by soulseekah (previous) (diff)

#2 @Vusys
3 months ago

I'm not adding any Quicktags. It looks like that error only occurs when creating a fresh post (post-new.php), but not when editing an existing post (post.php).

#3 @SergeyBiryukov
2 months ago

  • Milestone changed from Awaiting Review to 5.0
Note: See TracTickets for help on using tickets.