Opened 6 years ago
Last modified 6 years ago
#43686 new defect (bug)
Shortcodes containing asterisks may create invalid regex breaking the editor
Reported by: | Vusys | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
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)
Change History (8)
#1
@
6 years 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.
demo