2015-06-19 12:35:26 +02:00
|
|
|
( function() {
|
|
|
|
function WordCounter( settings ) {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
if ( settings ) {
|
|
|
|
for ( key in settings ) {
|
|
|
|
if ( settings.hasOwnProperty( key ) ) {
|
|
|
|
this.settings[ key ] = settings[ key ];
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2015-06-19 12:35:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2015-06-19 12:35:26 +02:00
|
|
|
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;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2015-06-19 12:35:26 +02:00
|
|
|
|
|
|
|
return count;
|
2013-11-15 07:52:09 +01:00
|
|
|
};
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2015-06-19 12:35:26 +02:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.utils = window.wp.utils || {};
|
|
|
|
window.wp.utils.WordCounter = WordCounter;
|
|
|
|
} )();
|