2015-06-19 06:35:26 -04:00
|
|
|
( function() {
|
|
|
|
function WordCounter( settings ) {
|
2015-07-16 17:09:26 -04:00
|
|
|
var key,
|
|
|
|
shortcodes;
|
2015-06-19 06:35:26 -04:00
|
|
|
|
|
|
|
if ( settings ) {
|
|
|
|
for ( key in settings ) {
|
|
|
|
if ( settings.hasOwnProperty( key ) ) {
|
|
|
|
this.settings[ key ] = settings[ key ];
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2015-06-19 06:35:26 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-16 17:09:26 -04:00
|
|
|
|
|
|
|
shortcodes = this.settings.l10n.shortcodes;
|
|
|
|
|
|
|
|
if ( shortcodes && shortcodes.length ) {
|
2015-07-21 11:24:24 -04:00
|
|
|
this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
|
2015-07-16 17:09:26 -04:00
|
|
|
}
|
2015-06-19 06:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WordCounter.prototype.settings = {
|
|
|
|
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
|
2015-07-21 11:24:24 -04:00
|
|
|
HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
|
2015-06-19 06:35:26 -04:00
|
|
|
spaceRegExp: / | /gi,
|
2015-07-21 11:24:24 -04:00
|
|
|
HTMLEntityRegExp: /&\S+?;/g,
|
|
|
|
connectorRegExp: /--|\u2014/g,
|
2015-07-16 05:45:26 -04:00
|
|
|
removeRegExp: new RegExp( [
|
|
|
|
'[',
|
|
|
|
// Basic Latin (extract)
|
|
|
|
'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
|
|
|
|
// Latin-1 Supplement (extract)
|
|
|
|
'\u0080-\u00BF\u00D7\u00F7',
|
|
|
|
// General Punctuation
|
|
|
|
// Superscripts and Subscripts
|
|
|
|
// Currency Symbols
|
|
|
|
// Combining Diacritical Marks for Symbols
|
|
|
|
// Letterlike Symbols
|
|
|
|
// Number Forms
|
|
|
|
// Arrows
|
|
|
|
// Mathematical Operators
|
|
|
|
// Miscellaneous Technical
|
|
|
|
// Control Pictures
|
|
|
|
// Optical Character Recognition
|
|
|
|
// Enclosed Alphanumerics
|
|
|
|
// Box Drawing
|
|
|
|
// Block Elements
|
|
|
|
// Geometric Shapes
|
|
|
|
// Miscellaneous Symbols
|
|
|
|
// Dingbats
|
|
|
|
// Miscellaneous Mathematical Symbols-A
|
|
|
|
// Supplemental Arrows-A
|
|
|
|
// Braille Patterns
|
|
|
|
// Supplemental Arrows-B
|
|
|
|
// Miscellaneous Mathematical Symbols-B
|
|
|
|
// Supplemental Mathematical Operators
|
|
|
|
// Miscellaneous Symbols and Arrows
|
|
|
|
'\u2000-\u2BFF',
|
|
|
|
// Supplemental Punctuation
|
|
|
|
'\u2E00-\u2E7F',
|
|
|
|
']'
|
|
|
|
].join( '' ), 'g' ),
|
2015-07-18 07:42:25 -04:00
|
|
|
astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
|
2015-06-19 06:35:26 -04:00
|
|
|
wordsRegExp: /\S\s+/g,
|
2015-07-27 07:19:26 -04:00
|
|
|
characters_excluding_spacesRegExp: /\S/g,
|
|
|
|
characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
|
2015-06-19 06:35:26 -04:00
|
|
|
l10n: window.wordCountL10n || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
WordCounter.prototype.count = function( text, type ) {
|
|
|
|
var count = 0;
|
|
|
|
|
2015-07-21 11:24:24 -04:00
|
|
|
type = type || this.settings.l10n.type;
|
|
|
|
|
2015-07-27 07:19:26 -04:00
|
|
|
if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
|
2015-07-21 11:24:24 -04:00
|
|
|
type = 'words';
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-06-19 06:35:26 -04:00
|
|
|
if ( text ) {
|
2015-07-15 19:48:24 -04:00
|
|
|
text = text + '\n';
|
2015-06-19 06:35:26 -04:00
|
|
|
|
2015-07-15 19:48:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLRegExp, '\n' );
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLcommentRegExp, '' );
|
2015-07-16 17:09:26 -04:00
|
|
|
|
|
|
|
if ( this.settings.shortcodesRegExp ) {
|
|
|
|
text = text.replace( this.settings.shortcodesRegExp, '\n' );
|
|
|
|
}
|
|
|
|
|
2015-06-19 06:35:26 -04:00
|
|
|
text = text.replace( this.settings.spaceRegExp, ' ' );
|
2015-07-16 05:45:26 -04:00
|
|
|
|
|
|
|
if ( type === 'words' ) {
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLEntityRegExp, '' );
|
2015-07-16 05:45:26 -04:00
|
|
|
text = text.replace( this.settings.connectorRegExp, ' ' );
|
|
|
|
text = text.replace( this.settings.removeRegExp, '' );
|
2015-07-18 07:42:25 -04:00
|
|
|
} else {
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
|
2015-07-18 07:42:25 -04:00
|
|
|
text = text.replace( this.settings.astralRegExp, 'a' );
|
2015-07-16 05:45:26 -04:00
|
|
|
}
|
2015-06-19 06:35:26 -04:00
|
|
|
|
|
|
|
text = text.match( this.settings[ type + 'RegExp' ] );
|
|
|
|
|
|
|
|
if ( text ) {
|
|
|
|
count = text.length;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2015-06-19 06:35:26 -04:00
|
|
|
|
|
|
|
return count;
|
2013-11-15 01:52:09 -05:00
|
|
|
};
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-06-19 06:35:26 -04:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.utils = window.wp.utils || {};
|
|
|
|
window.wp.utils.WordCounter = WordCounter;
|
|
|
|
} )();
|