Index: src/wp-admin/js/word-count.js
===================================================================
--- src/wp-admin/js/word-count.js (revision 33407)
+++ src/wp-admin/js/word-count.js (working copy)
@@ -1,4 +1,4 @@
-( function() {
+( function( window, wp, l10n ) {
function WordCounter( settings ) {
var key,
shortcodes;
@@ -13,7 +13,7 @@
shortcodes = this.settings.l10n.shortcodes;
- if ( shortcodes && shortcodes.length ) {
+ if ( shortcodes.length ) {
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
}
}
@@ -60,19 +60,14 @@
']'
].join( '' ), 'g' ),
astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
- wordsRegExp: /\S\s+/g,
- charactersRegExp: /\S/g,
- allRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
- l10n: window.wordCountL10n || {}
+ l10n: l10n || {}
};
- WordCounter.prototype.count = function( text, type ) {
+ WordCounter.prototype.count = function( text, characters ) {
var count = 0;
- type = type || this.settings.l10n.type;
-
- if ( type !== 'characters' && type !== 'all' ) {
- type = 'words';
+ if ( characters == null ) {
+ characters = this.settings.l10n.characters;
}
if ( text ) {
@@ -87,16 +82,16 @@
text = text.replace( this.settings.spaceRegExp, ' ' );
- if ( type === 'words' ) {
+ if ( characters ) {
+ text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
+ text = text.replace( this.settings.astralRegExp, 'a' );
+ } else {
text = text.replace( this.settings.HTMLEntityRegExp, '' );
text = text.replace( this.settings.connectorRegExp, ' ' );
text = text.replace( this.settings.removeRegExp, '' );
- } else {
- text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
- text = text.replace( this.settings.astralRegExp, 'a' );
}
- text = text.match( this.settings[ type + 'RegExp' ] );
+ text = text.match( characters ? /[^\f\n\r\t\v\u00AD\u2028\u2029]/g : /\S\s+/g );
if ( text ) {
count = text.length;
@@ -106,7 +101,7 @@
return count;
};
- window.wp = window.wp || {};
- window.wp.utils = window.wp.utils || {};
- window.wp.utils.WordCounter = WordCounter;
-} )();
+ wp = wp || {};
+ wp.utils = wp.utils || {};
+ wp.utils.WordCounter = WordCounter;
+} )( window, window.wp, window.wordCountL10n );
Index: src/wp-includes/script-loader.php
===================================================================
--- src/wp-includes/script-loader.php (revision 33407)
+++ src/wp-includes/script-loader.php (working copy)
@@ -402,9 +402,8 @@
$scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 );
did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array(
/* 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?' ),
+ enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
+ 'characters' => _x( 'words', 'word count: words or characters?' ) === 'characters',
'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array()
) );
Index: tests/qunit/wp-admin/js/word-count.js
===================================================================
--- tests/qunit/wp-admin/js/word-count.js (revision 33407)
+++ tests/qunit/wp-admin/js/word-count.js (working copy)
@@ -7,75 +7,65 @@
message: 'Basic test.',
string: 'one two three',
words: 3,
- characters: 11,
- all: 13
+ characters: 13
},
{
message: 'HTML tags.',
string: 'one two
three',
words: 3,
- characters: 11,
- all: 12
+ characters: 12
},
{
message: 'Line breaks.',
string: 'one\ntwo\nthree',
words: 3,
- characters: 11,
- all: 11
+ characters: 11
},
{
message: 'Encoded spaces.',
string: 'one two three',
words: 3,
- characters: 11,
- all: 13
+ characters: 13
},
{
message: 'Punctuation.',
string: 'It\'s two three \u2026 4?',
words: 3,
- characters: 15,
- all: 19
+ characters: 19
},
{
message: 'Em dash.',
string: 'one\u2014two--three',
words: 3,
- characters: 14,
- all: 14
+ characters: 14
},
{
message: 'Shortcodes.',
string: 'one [shortcode attribute="value"]two[/shortcode]three',
words: 3,
- characters: 11,
- all: 12
+ characters: 12
},
{
message: 'Astrals.',
string: '\uD83D\uDCA9',
words: 1,
- characters: 1,
- all: 1
+ characters: 1
},
{
message: 'HTML comment.',
string: 'onetwo three',
words: 2,
- characters: 11,
- all: 12
+ characters: 12
},
{
message: 'HTML entity.',
string: '> test',
words: 1,
- characters: 5,
- all: 6
+ characters: 6
}
], function( test ) {
- _.each( [ 'words', 'characters', 'all' ], function( type ) {
- assert.equal( wordCounter.count( test.string, type ), test[ type ], test.message + ' (' + type + ')' );
+ _.each( [ 'words', 'characters' ], function( type ) {
+ assert.equal( wordCounter.count( test.string, type === 'characters' ), test[ type ], test.message + ' (' + type + ')' );
} );
} );
} );