[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> word-count.js (source)

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


Generated: Sat Jan 18 01:00:03 2020 Cross-referenced by PHPXref 0.7.1