wp/wp-admin/js/word-count.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 /**
     1 /**
     2  * Word or character counting functionality. Count words or characters in a
     2  * Word or character counting functionality. Count words or characters in a
     3  * provided text string.
     3  * provided text string.
     4  *
     4  *
     5  * @namespace wp.utils
     5  * @namespace wp.utils
     6  * @since     2.6.0
     6  *
       
     7  * @since 2.6.0
     7  * @output wp-admin/js/word-count.js
     8  * @output wp-admin/js/word-count.js
     8  */
     9  */
     9 
    10 
    10 ( function() {
    11 ( function() {
    11 	/**
    12 	/**
    35 	 * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters
    36 	 * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters
    36 	 *                                                            including spaces.
    37 	 *                                                            including spaces.
    37 	 * @param {RegExp} settings.shortcodesRegExp                  Optional. Regular expression to find shortcodes.
    38 	 * @param {RegExp} settings.shortcodesRegExp                  Optional. Regular expression to find shortcodes.
    38 	 * @param {Object} settings.l10n                              Optional. Localization object containing specific
    39 	 * @param {Object} settings.l10n                              Optional. Localization object containing specific
    39 	 *                                                            configuration for the current localization.
    40 	 *                                                            configuration for the current localization.
    40 	 * @param {String} settings.l10n.type                         Optional. Method of finding words to count.
    41 	 * @param {string} settings.l10n.type                         Optional. Method of finding words to count.
    41 	 * @param {Array}  settings.l10n.shortcodes                   Optional. Array of shortcodes that should be removed
    42 	 * @param {Array}  settings.l10n.shortcodes                   Optional. Array of shortcodes that should be removed
    42 	 *                                                            from the text.
    43 	 *                                                            from the text.
    43 	 *
    44 	 *
    44 	 * @return void
    45 	 * @return {void}
    45 	 */
    46 	 */
    46 	function WordCounter( settings ) {
    47 	function WordCounter( settings ) {
    47 		var key,
    48 		var key,
    48 			shortcodes;
    49 			shortcodes;
    49 
    50 
    71 		HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
    72 		HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
    72 		HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
    73 		HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
    73 		spaceRegExp: /&nbsp;|&#160;/gi,
    74 		spaceRegExp: /&nbsp;|&#160;/gi,
    74 		HTMLEntityRegExp: /&\S+?;/g,
    75 		HTMLEntityRegExp: /&\S+?;/g,
    75 
    76 
    76 		// \u2014 = em-dash
    77 		// \u2014 = em-dash.
    77 		connectorRegExp: /--|\u2014/g,
    78 		connectorRegExp: /--|\u2014/g,
    78 
    79 
    79 		// Characters to be removed from input text.
    80 		// Characters to be removed from input text.
    80 		removeRegExp: new RegExp( [
    81 		removeRegExp: new RegExp( [
    81 			'[',
    82 			'[',
    82 
    83 
    83 				// Basic Latin (extract)
    84 				// Basic Latin (extract).
    84 				'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
    85 				'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
    85 
    86 
    86 				// Latin-1 Supplement (extract)
    87 				// Latin-1 Supplement (extract).
    87 				'\u0080-\u00BF\u00D7\u00F7',
    88 				'\u0080-\u00BF\u00D7\u00F7',
    88 
    89 
    89 				/*
    90 				/*
    90 				 * The following range consists of:
    91 				 * The following range consists of:
    91 				 * General Punctuation
    92 				 * General Punctuation
   113 				 * Supplemental Mathematical Operators
   114 				 * Supplemental Mathematical Operators
   114 				 * Miscellaneous Symbols and Arrows
   115 				 * Miscellaneous Symbols and Arrows
   115 				 */
   116 				 */
   116 				'\u2000-\u2BFF',
   117 				'\u2000-\u2BFF',
   117 
   118 
   118 				// Supplemental Punctuation
   119 				// Supplemental Punctuation.
   119 				'\u2E00-\u2E7F',
   120 				'\u2E00-\u2E7F',
   120 			']'
   121 			']'
   121 		].join( '' ), 'g' ),
   122 		].join( '' ), 'g' ),
   122 
   123 
   123 		// Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
   124 		// Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
   141 	};
   142 	};
   142 
   143 
   143 	/**
   144 	/**
   144 	 * Counts the number of words (or other specified type) in the specified text.
   145 	 * Counts the number of words (or other specified type) in the specified text.
   145 	 *
   146 	 *
   146 	 * @since    2.6.0
   147 	 * @since 2.6.0
       
   148 	 *
   147 	 * @memberof wp.utils.wordcounter
   149 	 * @memberof wp.utils.wordcounter
   148 	 *
   150 	 *
   149 	 * @param {String}  text Text to count elements in.
   151 	 * @param {string}  text Text to count elements in.
   150 	 * @param {String}  type Optional. Specify type to use.
   152 	 * @param {string}  type Optional. Specify type to use.
   151 	 *
   153 	 *
   152 	 * @return {Number} The number of items counted.
   154 	 * @return {number} The number of items counted.
   153 	 */
   155 	 */
   154 	WordCounter.prototype.count = function( text, type ) {
   156 	WordCounter.prototype.count = function( text, type ) {
   155 		var count = 0;
   157 		var count = 0;
   156 
   158 
   157 		// Use default type if none was provided.
   159 		// Use default type if none was provided.