2016-08-26 05:25:27 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Word or character counting functionality. Count words or characters in a
|
|
|
|
* provided text string.
|
2016-08-26 05:25:27 -04:00
|
|
|
*
|
|
|
|
* @namespace wp.utils
|
2020-01-28 19:45:18 -05:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2018-06-27 22:30:15 -04:00
|
|
|
* @output wp-admin/js/word-count.js
|
2016-08-26 05:25:27 -04:00
|
|
|
*/
|
|
|
|
|
2015-06-19 06:35:26 -04:00
|
|
|
( function() {
|
2016-08-26 05:25:27 -04:00
|
|
|
/**
|
|
|
|
* Word counting utility
|
|
|
|
*
|
|
|
|
* @namespace wp.utils.wordcounter
|
|
|
|
* @memberof wp.utils
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @param {Object} settings Optional. Key-value object containing overrides for
|
|
|
|
* settings.
|
|
|
|
* @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements.
|
|
|
|
* @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments.
|
|
|
|
* @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space
|
|
|
|
* characters.
|
|
|
|
* @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities.
|
|
|
|
* @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that
|
|
|
|
* split words.
|
|
|
|
* @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted
|
|
|
|
* characters to reduce false-positives.
|
|
|
|
* @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted
|
|
|
|
* characters when searching for non-words.
|
|
|
|
* @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces.
|
|
|
|
* @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which
|
|
|
|
* are non-spaces.
|
|
|
|
* @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters
|
|
|
|
* including spaces.
|
|
|
|
* @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes.
|
|
|
|
* @param {Object} settings.l10n Optional. Localization object containing specific
|
|
|
|
* configuration for the current localization.
|
|
|
|
* @param {String} settings.l10n.type Optional. Method of finding words to count.
|
|
|
|
* @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed
|
|
|
|
* from the text.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-08-26 05:25:27 -04:00
|
|
|
*/
|
2015-06-19 06:35:26 -04:00
|
|
|
function WordCounter( settings ) {
|
2015-07-16 17:09:26 -04:00
|
|
|
var key,
|
|
|
|
shortcodes;
|
2015-06-19 06:35:26 -04:00
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Apply provided settings to object settings.
|
2015-06-19 06:35:26 -04:00
|
|
|
if ( settings ) {
|
|
|
|
for ( key in settings ) {
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Only apply valid settings.
|
2015-06-19 06:35:26 -04:00
|
|
|
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;
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// If there are any localization shortcodes, add this as type in the settings.
|
2015-07-16 17:09:26 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Default settings.
|
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,
|
2016-08-26 05:25:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// \u2014 = em-dash.
|
2015-07-21 11:24:24 -04:00
|
|
|
connectorRegExp: /--|\u2014/g,
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Characters to be removed from input text.
|
2015-07-16 05:45:26 -04:00
|
|
|
removeRegExp: new RegExp( [
|
|
|
|
'[',
|
2016-08-26 05:25:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Basic Latin (extract).
|
2015-07-16 05:45:26 -04:00
|
|
|
'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
|
2016-08-26 05:25:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Latin-1 Supplement (extract).
|
2015-07-16 05:45:26 -04:00
|
|
|
'\u0080-\u00BF\u00D7\u00F7',
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The following range consists of:
|
|
|
|
* 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
|
|
|
|
*/
|
2015-07-16 05:45:26 -04:00
|
|
|
'\u2000-\u2BFF',
|
2016-08-26 05:25:27 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Supplemental Punctuation.
|
2015-07-16 05:45:26 -04:00
|
|
|
'\u2E00-\u2E7F',
|
|
|
|
']'
|
|
|
|
].join( '' ), 'g' ),
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
|
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,
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Match anything that is not a formatting character, excluding:
|
|
|
|
* \f = form feed
|
|
|
|
* \n = new line
|
|
|
|
* \r = carriage return
|
|
|
|
* \t = tab
|
|
|
|
* \v = vertical tab
|
|
|
|
* \u00AD = soft hyphen
|
|
|
|
* \u2028 = line separator
|
|
|
|
* \u2029 = paragraph separator
|
|
|
|
*/
|
2015-07-27 07:19:26 -04:00
|
|
|
characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
|
2015-06-19 06:35:26 -04:00
|
|
|
l10n: window.wordCountL10n || {}
|
|
|
|
};
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
/**
|
|
|
|
* Counts the number of words (or other specified type) in the specified text.
|
|
|
|
*
|
2020-01-28 19:45:18 -05:00
|
|
|
* @since 2.6.0
|
|
|
|
*
|
2016-08-26 05:25:27 -04:00
|
|
|
* @memberof wp.utils.wordcounter
|
|
|
|
*
|
|
|
|
* @param {String} text Text to count elements in.
|
|
|
|
* @param {String} type Optional. Specify type to use.
|
|
|
|
*
|
|
|
|
* @return {Number} The number of items counted.
|
|
|
|
*/
|
2015-06-19 06:35:26 -04:00
|
|
|
WordCounter.prototype.count = function( text, type ) {
|
|
|
|
var count = 0;
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Use default type if none was provided.
|
2015-07-21 11:24:24 -04:00
|
|
|
type = type || this.settings.l10n.type;
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'.
|
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
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// If we have any text at all.
|
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
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Replace all HTML with a new-line.
|
2015-07-15 19:48:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLRegExp, '\n' );
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Remove all HTML comments.
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLcommentRegExp, '' );
|
2015-07-16 17:09:26 -04:00
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// If a shortcode regular expression has been provided use it to remove shortcodes.
|
2015-07-16 17:09:26 -04:00
|
|
|
if ( this.settings.shortcodesRegExp ) {
|
|
|
|
text = text.replace( this.settings.shortcodesRegExp, '\n' );
|
|
|
|
}
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Normalize non-breaking space to a normal space.
|
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' ) {
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Remove HTML Entities.
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLEntityRegExp, '' );
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Convert connectors to spaces to count attached text as words.
|
2015-07-16 05:45:26 -04:00
|
|
|
text = text.replace( this.settings.connectorRegExp, ' ' );
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Remove unwanted characters.
|
2015-07-16 05:45:26 -04:00
|
|
|
text = text.replace( this.settings.removeRegExp, '' );
|
2015-07-18 07:42:25 -04:00
|
|
|
} else {
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Convert HTML Entities to "a".
|
2015-07-21 11:24:24 -04:00
|
|
|
text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
|
2016-08-26 05:25:27 -04:00
|
|
|
|
|
|
|
// Remove surrogate points.
|
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
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Match with the selected type regular expression to count the items.
|
2015-06-19 06:35:26 -04:00
|
|
|
text = text.match( this.settings[ type + 'RegExp' ] );
|
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// If we have any matches, set the count to the number of items found.
|
2015-06-19 06:35:26 -04:00
|
|
|
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
|
|
|
|
2016-08-26 05:25:27 -04:00
|
|
|
// Add the WordCounter to the WP Utils.
|
2015-06-19 06:35:26 -04:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.utils = window.wp.utils || {};
|
|
|
|
window.wp.utils.WordCounter = WordCounter;
|
|
|
|
} )();
|