word-count.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * Word or character counting functionality. Count words or characters in a
  3. * provided text string.
  4. *
  5. * @namespace wp.utils
  6. * @since 2.6.0
  7. * @output wp-admin/js/word-count.js
  8. */
  9. ( function() {
  10. /**
  11. * Word counting utility
  12. *
  13. * @namespace wp.utils.wordcounter
  14. * @memberof wp.utils
  15. *
  16. * @class
  17. *
  18. * @param {Object} settings Optional. Key-value object containing overrides for
  19. * settings.
  20. * @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements.
  21. * @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments.
  22. * @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space
  23. * characters.
  24. * @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities.
  25. * @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that
  26. * split words.
  27. * @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted
  28. * characters to reduce false-positives.
  29. * @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted
  30. * characters when searching for non-words.
  31. * @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces.
  32. * @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which
  33. * are non-spaces.
  34. * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters
  35. * including spaces.
  36. * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes.
  37. * @param {Object} settings.l10n Optional. Localization object containing specific
  38. * configuration for the current localization.
  39. * @param {String} settings.l10n.type Optional. Method of finding words to count.
  40. * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed
  41. * from the text.
  42. *
  43. * @return void
  44. */
  45. function WordCounter( settings ) {
  46. var key,
  47. shortcodes;
  48. // Apply provided settings to object settings.
  49. if ( settings ) {
  50. for ( key in settings ) {
  51. // Only apply valid settings.
  52. if ( settings.hasOwnProperty( key ) ) {
  53. this.settings[ key ] = settings[ key ];
  54. }
  55. }
  56. }
  57. shortcodes = this.settings.l10n.shortcodes;
  58. // If there are any localization shortcodes, add this as type in the settings.
  59. if ( shortcodes && shortcodes.length ) {
  60. this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
  61. }
  62. }
  63. // Default settings.
  64. WordCounter.prototype.settings = {
  65. HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  66. HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  67. spaceRegExp: /&nbsp;|&#160;/gi,
  68. HTMLEntityRegExp: /&\S+?;/g,
  69. // \u2014 = em-dash
  70. connectorRegExp: /--|\u2014/g,
  71. // Characters to be removed from input text.
  72. removeRegExp: new RegExp( [
  73. '[',
  74. // Basic Latin (extract)
  75. '\u0021-\u0040\u005B-\u0060\u007B-\u007E',
  76. // Latin-1 Supplement (extract)
  77. '\u0080-\u00BF\u00D7\u00F7',
  78. /*
  79. * The following range consists of:
  80. * General Punctuation
  81. * Superscripts and Subscripts
  82. * Currency Symbols
  83. * Combining Diacritical Marks for Symbols
  84. * Letterlike Symbols
  85. * Number Forms
  86. * Arrows
  87. * Mathematical Operators
  88. * Miscellaneous Technical
  89. * Control Pictures
  90. * Optical Character Recognition
  91. * Enclosed Alphanumerics
  92. * Box Drawing
  93. * Block Elements
  94. * Geometric Shapes
  95. * Miscellaneous Symbols
  96. * Dingbats
  97. * Miscellaneous Mathematical Symbols-A
  98. * Supplemental Arrows-A
  99. * Braille Patterns
  100. * Supplemental Arrows-B
  101. * Miscellaneous Mathematical Symbols-B
  102. * Supplemental Mathematical Operators
  103. * Miscellaneous Symbols and Arrows
  104. */
  105. '\u2000-\u2BFF',
  106. // Supplemental Punctuation
  107. '\u2E00-\u2E7F',
  108. ']'
  109. ].join( '' ), 'g' ),
  110. // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
  111. astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  112. wordsRegExp: /\S\s+/g,
  113. characters_excluding_spacesRegExp: /\S/g,
  114. /*
  115. * Match anything that is not a formatting character, excluding:
  116. * \f = form feed
  117. * \n = new line
  118. * \r = carriage return
  119. * \t = tab
  120. * \v = vertical tab
  121. * \u00AD = soft hyphen
  122. * \u2028 = line separator
  123. * \u2029 = paragraph separator
  124. */
  125. characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
  126. l10n: window.wordCountL10n || {}
  127. };
  128. /**
  129. * Counts the number of words (or other specified type) in the specified text.
  130. *
  131. * @since 2.6.0
  132. * @memberof wp.utils.wordcounter
  133. *
  134. * @param {String} text Text to count elements in.
  135. * @param {String} type Optional. Specify type to use.
  136. *
  137. * @return {Number} The number of items counted.
  138. */
  139. WordCounter.prototype.count = function( text, type ) {
  140. var count = 0;
  141. // Use default type if none was provided.
  142. type = type || this.settings.l10n.type;
  143. // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'.
  144. if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
  145. type = 'words';
  146. }
  147. // If we have any text at all.
  148. if ( text ) {
  149. text = text + '\n';
  150. // Replace all HTML with a new-line.
  151. text = text.replace( this.settings.HTMLRegExp, '\n' );
  152. // Remove all HTML comments.
  153. text = text.replace( this.settings.HTMLcommentRegExp, '' );
  154. // If a shortcode regular expression has been provided use it to remove shortcodes.
  155. if ( this.settings.shortcodesRegExp ) {
  156. text = text.replace( this.settings.shortcodesRegExp, '\n' );
  157. }
  158. // Normalize non-breaking space to a normal space.
  159. text = text.replace( this.settings.spaceRegExp, ' ' );
  160. if ( type === 'words' ) {
  161. // Remove HTML Entities.
  162. text = text.replace( this.settings.HTMLEntityRegExp, '' );
  163. // Convert connectors to spaces to count attached text as words.
  164. text = text.replace( this.settings.connectorRegExp, ' ' );
  165. // Remove unwanted characters.
  166. text = text.replace( this.settings.removeRegExp, '' );
  167. } else {
  168. // Convert HTML Entities to "a".
  169. text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
  170. // Remove surrogate points.
  171. text = text.replace( this.settings.astralRegExp, 'a' );
  172. }
  173. // Match with the selected type regular expression to count the items.
  174. text = text.match( this.settings[ type + 'RegExp' ] );
  175. // If we have any matches, set the count to the number of items found.
  176. if ( text ) {
  177. count = text.length;
  178. }
  179. }
  180. return count;
  181. };
  182. // Add the WordCounter to the WP Utils.
  183. window.wp = window.wp || {};
  184. window.wp.utils = window.wp.utils || {};
  185. window.wp.utils.WordCounter = WordCounter;
  186. } )();