I have a custom button added to TinyMCE visual editor. This button wraps any highlighted words in <em>
tags with class tle
. So, if you highlight term Pulp Fiction
and click the button, you will get <em class="tle">Pulp Fiction</em>
.
The JS code:
(function() {
tinymce.PluginManager.add('custom_em', function( editor, url ) {
editor.on('init', function(e) {
this.formatter.register('reftitle', {
inline : 'em',
classes : 'tle'
});
});
editor.addButton('custom_em', {
text: 'Title (italic)',
icon: false,
onclick : function() {
var contents = editor.selection.getContent(),
tags = jQuery(contents).find('em.tle').andSelf();
if (tags.length) {
editor.formatter.remove('reftitle');
} else {
editor.formatter.apply('reftitle', {value: contents});
}
}
});
});
})(jQuery);
It works great, but it doesn't format any terms that contain any special characters. It only works with characters A-Z and 0-9.
For example, X-Men: Days of Future Past
will not work because it contains :
. In console, it gives error: Syntax error, unrecognized expression
.
Why is it not working properly? Guessing it's an issue with my onclick function. Hoping there is a jQuery or TinyMCE guru here.