mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-16 15:46:04 +01:00
fd95002b2a
* The WordCounter should only do one thing: count words. This makes it also easier to test. * Add some really basic unit tests. * Instead of only refreshing the count on enter and delete, refresh the count when the user stops typing. Also look at paste and content changes in TinyMCE. * Use `match` instead of `replace` when it is appropriate. * More readable code. See #30966. Fixes #26620. Built from https://develop.svn.wordpress.org/trunk@32856 git-svn-id: http://core.svn.wordpress.org/trunk@32827 1a063a9b-81f0-0310-95a4-ce76da25c4cd
49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
( function() {
|
|
function WordCounter( settings ) {
|
|
var key;
|
|
|
|
if ( settings ) {
|
|
for ( key in settings ) {
|
|
if ( settings.hasOwnProperty( key ) ) {
|
|
this.settings[ key ] = settings[ key ];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
WordCounter.prototype.settings = {
|
|
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
|
|
spaceRegExp: / | /gi,
|
|
removeRegExp: /[0-9.(),;:!?%#$¿'"_+=\\\/-]+/g,
|
|
wordsRegExp: /\S\s+/g,
|
|
charactersRegExp: /\S/g,
|
|
l10n: window.wordCountL10n || {}
|
|
};
|
|
|
|
WordCounter.prototype.count = function( text, type ) {
|
|
var count = 0;
|
|
|
|
type = type || this.settings.l10n.type || 'words';
|
|
|
|
if ( text ) {
|
|
text = ' ' + text + ' ';
|
|
|
|
text = text.replace( this.settings.HTMLRegExp, ' ' );
|
|
text = text.replace( this.settings.spaceRegExp, ' ' );
|
|
text = text.replace( this.settings.removeRegExp, '' );
|
|
|
|
text = text.match( this.settings[ type + 'RegExp' ] );
|
|
|
|
if ( text ) {
|
|
count = text.length;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
};
|
|
|
|
window.wp = window.wp || {};
|
|
window.wp.utils = window.wp.utils || {};
|
|
window.wp.utils.WordCounter = WordCounter;
|
|
} )();
|