Editor: word count: exclude shortcodes

Props desaiuditd, adamsilverstein, azaozz and iseulde.
Fixes #27386. See #30966.


git-svn-id: https://develop.svn.wordpress.org/trunk@33299 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ella Iseulde Van Dorpe 2015-07-16 21:08:05 +00:00
parent 03653fa600
commit c2c5c5ac27
3 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,7 @@
( function() {
function WordCounter( settings ) {
var key;
var key,
shortcodes;
if ( settings ) {
for ( key in settings ) {
@ -9,6 +10,12 @@
}
}
}
shortcodes = this.settings.l10n.shortcodes;
if ( shortcodes && shortcodes.length ) {
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' );
}
}
WordCounter.prototype.settings = {
@ -65,6 +72,11 @@
text = text + '\n';
text = text.replace( this.settings.HTMLRegExp, '\n' );
if ( this.settings.shortcodesRegExp ) {
text = text.replace( this.settings.shortcodesRegExp, '\n' );
}
text = text.replace( this.settings.spaceRegExp, ' ' );
if ( type === 'words' ) {

View File

@ -400,7 +400,8 @@ function wp_default_scripts( &$scripts ) {
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters', or 'all' to include spaces. Otherwise, enter 'words'.
Do not translate into your own language. */
'type' => _x( 'words', 'word count: words, characters or all?' )
'type' => _x( 'words', 'word count: words, characters or all?' ),
'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array()
) );
$scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 );

View File

@ -44,6 +44,13 @@
words: 3,
characters: 14,
all: 14
},
{
message: 'Shortcodes.',
string: 'one [shortcode attribute="value"]two[/shortcode]three',
words: 3,
characters: 11,
all: 12
}
], function( test ) {
_.each( [ 'words', 'characters', 'all' ], function( type ) {
@ -51,4 +58,8 @@
} );
} );
} );
} )( window.QUnit, new window.wp.utils.WordCounter() );
} )( window.QUnit, new window.wp.utils.WordCounter( {
l10n: {
shortcodes: [ 'shortcode' ]
}
} ) );