[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/dist/ -> wordcount.js (source)

   1  /******/ (function() { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/define property getters */
   8  /******/     !function() {
   9  /******/         // define getter functions for harmony exports
  10  /******/         __webpack_require__.d = function(exports, definition) {
  11  /******/             for(var key in definition) {
  12  /******/                 if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13  /******/                     Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14  /******/                 }
  15  /******/             }
  16  /******/         };
  17  /******/     }();
  18  /******/     
  19  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  20  /******/     !function() {
  21  /******/         __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22  /******/     }();
  23  /******/     
  24  /******/     /* webpack/runtime/make namespace object */
  25  /******/     !function() {
  26  /******/         // define __esModule on exports
  27  /******/         __webpack_require__.r = function(exports) {
  28  /******/             if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29  /******/                 Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30  /******/             }
  31  /******/             Object.defineProperty(exports, '__esModule', { value: true });
  32  /******/         };
  33  /******/     }();
  34  /******/     
  35  /************************************************************************/
  36  var __webpack_exports__ = {};
  37  // ESM COMPAT FLAG
  38  __webpack_require__.r(__webpack_exports__);
  39  
  40  // EXPORTS
  41  __webpack_require__.d(__webpack_exports__, {
  42    "count": function() { return /* binding */ count; }
  43  });
  44  
  45  ;// CONCATENATED MODULE: external "lodash"
  46  var external_lodash_namespaceObject = window["lodash"];
  47  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
  48  /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
  49  
  50  /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
  51  
  52  /**
  53   * @typedef WPWordCountSettingsFields
  54   * @property {RegExp}              HTMLRegExp                        Regular expression that matches HTML tags
  55   * @property {RegExp}              HTMLcommentRegExp                 Regular expression that matches HTML comments
  56   * @property {RegExp}              spaceRegExp                       Regular expression that matches spaces in HTML
  57   * @property {RegExp}              HTMLEntityRegExp                  Regular expression that matches HTML entities
  58   * @property {RegExp}              connectorRegExp                   Regular expression that matches word connectors, like em-dash
  59   * @property {RegExp}              removeRegExp                      Regular expression that matches various characters to be removed when counting
  60   * @property {RegExp}              astralRegExp                      Regular expression that matches astral UTF-16 code points
  61   * @property {RegExp}              wordsRegExp                       Regular expression that matches words
  62   * @property {RegExp}              characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
  63   * @property {RegExp}              characters_including_spacesRegExp Regular expression that matches characters including spaces
  64   * @property {RegExp}              shortcodesRegExp                  Regular expression that matches WordPress shortcodes
  65   * @property {string[]}            shortcodes                        List of all shortcodes
  66   * @property {WPWordCountStrategy} type                              Describes what and how are we counting
  67   * @property {WPWordCountL10n}     l10n                              Object with human translations
  68   */
  69  
  70  /**
  71   * Lower-level settings for word counting that can be overridden.
  72   *
  73   * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
  74   */
  75  // Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
  76  
  77  /* eslint-disable jsdoc/valid-types */
  78  
  79  /**
  80   * Word counting settings that include non-optional values we set if missing
  81   *
  82   * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
  83   */
  84  
  85  /* eslint-enable jsdoc/valid-types */
  86  const defaultSettings = {
  87    HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  88    HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  89    spaceRegExp: /&nbsp;|&#160;/gi,
  90    HTMLEntityRegExp: /&\S+?;/g,
  91    // \u2014 = em-dash.
  92    connectorRegExp: /--|\u2014/g,
  93    // Characters to be removed from input text.
  94    removeRegExp: new RegExp(['[', // Basic Latin (extract)
  95    '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E', // Latin-1 Supplement (extract)
  96    '\u0080-\u00BF\u00D7\u00F7',
  97    /*
  98     * The following range consists of:
  99     * General Punctuation
 100     * Superscripts and Subscripts
 101     * Currency Symbols
 102     * Combining Diacritical Marks for Symbols
 103     * Letterlike Symbols
 104     * Number Forms
 105     * Arrows
 106     * Mathematical Operators
 107     * Miscellaneous Technical
 108     * Control Pictures
 109     * Optical Character Recognition
 110     * Enclosed Alphanumerics
 111     * Box Drawing
 112     * Block Elements
 113     * Geometric Shapes
 114     * Miscellaneous Symbols
 115     * Dingbats
 116     * Miscellaneous Mathematical Symbols-A
 117     * Supplemental Arrows-A
 118     * Braille Patterns
 119     * Supplemental Arrows-B
 120     * Miscellaneous Mathematical Symbols-B
 121     * Supplemental Mathematical Operators
 122     * Miscellaneous Symbols and Arrows
 123     */
 124    '\u2000-\u2BFF', // Supplemental Punctuation.
 125    '\u2E00-\u2E7F', ']'].join(''), 'g'),
 126    // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
 127    astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
 128    wordsRegExp: /\S\s+/g,
 129    characters_excluding_spacesRegExp: /\S/g,
 130  
 131    /*
 132     * Match anything that is not a formatting character, excluding:
 133     * \f = form feed
 134     * \n = new line
 135     * \r = carriage return
 136     * \t = tab
 137     * \v = vertical tab
 138     * \u00AD = soft hyphen
 139     * \u2028 = line separator
 140     * \u2029 = paragraph separator
 141     */
 142    characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
 143    l10n: {
 144      type: 'words'
 145    }
 146  };
 147  
 148  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
 149  /**
 150   * Replaces items matched in the regex with new line
 151   *
 152   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 153   * @param {string}                                text     The string being counted.
 154   *
 155   * @return {string} The manipulated text.
 156   */
 157  function stripTags(settings, text) {
 158    return text.replace(settings.HTMLRegExp, '\n');
 159  }
 160  
 161  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
 162  /**
 163   * Replaces items matched in the regex with character.
 164   *
 165   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 166   * @param {string}                                text     The string being counted.
 167   *
 168   * @return {string} The manipulated text.
 169   */
 170  function transposeAstralsToCountableChar(settings, text) {
 171    return text.replace(settings.astralRegExp, 'a');
 172  }
 173  
 174  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
 175  /**
 176   * Removes items matched in the regex.
 177   *
 178   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 179   * @param {string}                                text     The string being counted.
 180   *
 181   * @return {string} The manipulated text.
 182   */
 183  function stripHTMLEntities(settings, text) {
 184    return text.replace(settings.HTMLEntityRegExp, '');
 185  }
 186  
 187  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
 188  /**
 189   * Replaces items matched in the regex with spaces.
 190   *
 191   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 192   * @param {string}                                text     The string being counted.
 193   *
 194   * @return {string} The manipulated text.
 195   */
 196  function stripConnectors(settings, text) {
 197    return text.replace(settings.connectorRegExp, ' ');
 198  }
 199  
 200  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
 201  /**
 202   * Removes items matched in the regex.
 203   *
 204   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 205   * @param {string}                                text     The string being counted.
 206   *
 207   * @return {string} The manipulated text.
 208   */
 209  function stripRemovables(settings, text) {
 210    return text.replace(settings.removeRegExp, '');
 211  }
 212  
 213  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
 214  /**
 215   * Removes items matched in the regex.
 216   *
 217   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 218   * @param {string}                                text     The string being counted.
 219   *
 220   * @return {string} The manipulated text.
 221   */
 222  function stripHTMLComments(settings, text) {
 223    return text.replace(settings.HTMLcommentRegExp, '');
 224  }
 225  
 226  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
 227  /**
 228   * Replaces items matched in the regex with a new line.
 229   *
 230   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 231   * @param {string}                                text     The string being counted.
 232   *
 233   * @return {string} The manipulated text.
 234   */
 235  function stripShortcodes(settings, text) {
 236    if (settings.shortcodesRegExp) {
 237      return text.replace(settings.shortcodesRegExp, '\n');
 238    }
 239  
 240    return text;
 241  }
 242  
 243  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
 244  /**
 245   * Replaces items matched in the regex with spaces.
 246   *
 247   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 248   * @param {string}                                text     The string being counted.
 249   *
 250   * @return {string} The manipulated text.
 251   */
 252  function stripSpaces(settings, text) {
 253    return text.replace(settings.spaceRegExp, ' ');
 254  }
 255  
 256  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
 257  /**
 258   * Replaces items matched in the regex with a single character.
 259   *
 260   * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
 261   * @param {string}                                text     The string being counted.
 262   *
 263   * @return {string} The manipulated text.
 264   */
 265  function transposeHTMLEntitiesToCountableChars(settings, text) {
 266    return text.replace(settings.HTMLEntityRegExp, 'a');
 267  }
 268  
 269  ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
 270  /**
 271   * External dependencies
 272   */
 273  
 274  /**
 275   * Internal dependencies
 276   */
 277  
 278  
 279  
 280  
 281  
 282  
 283  
 284  
 285  
 286  
 287  
 288  /**
 289   * @typedef {import('./defaultSettings').WPWordCountDefaultSettings}  WPWordCountSettings
 290   * @typedef {import('./defaultSettings').WPWordCountUserSettings}     WPWordCountUserSettings
 291   */
 292  
 293  /**
 294   * Possible ways of counting.
 295   *
 296   * @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
 297   */
 298  
 299  /**
 300   * Private function to manage the settings.
 301   *
 302   * @param {WPWordCountStrategy}     type         The type of count to be done.
 303   * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
 304   *
 305   * @return {WPWordCountSettings} The combined settings object to be used.
 306   */
 307  
 308  function loadSettings(type, userSettings) {
 309    var _settings$l10n$shortc, _settings$l10n;
 310  
 311    const settings = (0,external_lodash_namespaceObject.extend)({}, defaultSettings, userSettings);
 312    settings.shortcodes = (_settings$l10n$shortc = (_settings$l10n = settings.l10n) === null || _settings$l10n === void 0 ? void 0 : _settings$l10n.shortcodes) !== null && _settings$l10n$shortc !== void 0 ? _settings$l10n$shortc : [];
 313  
 314    if (settings.shortcodes && settings.shortcodes.length) {
 315      settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
 316    }
 317  
 318    settings.type = type;
 319  
 320    if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
 321      settings.type = 'words';
 322    }
 323  
 324    return settings;
 325  }
 326  /**
 327   * Count the words in text
 328   *
 329   * @param {string}              text     The text being processed
 330   * @param {RegExp}              regex    The regular expression pattern being matched
 331   * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
 332   *
 333   * @return {number} Count of words.
 334   */
 335  
 336  
 337  function countWords(text, regex, settings) {
 338    var _text$match$length, _text$match;
 339  
 340    text = (0,external_lodash_namespaceObject.flow)(stripTags.bind(null, settings), stripHTMLComments.bind(null, settings), stripShortcodes.bind(null, settings), stripSpaces.bind(null, settings), stripHTMLEntities.bind(null, settings), stripConnectors.bind(null, settings), stripRemovables.bind(null, settings))(text);
 341    text = text + '\n';
 342    return (_text$match$length = (_text$match = text.match(regex)) === null || _text$match === void 0 ? void 0 : _text$match.length) !== null && _text$match$length !== void 0 ? _text$match$length : 0;
 343  }
 344  /**
 345   * Count the characters in text
 346   *
 347   * @param {string}              text     The text being processed
 348   * @param {RegExp}              regex    The regular expression pattern being matched
 349   * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
 350   *
 351   * @return {number} Count of characters.
 352   */
 353  
 354  
 355  function countCharacters(text, regex, settings) {
 356    var _text$match$length2, _text$match2;
 357  
 358    text = (0,external_lodash_namespaceObject.flow)(stripTags.bind(null, settings), stripHTMLComments.bind(null, settings), stripShortcodes.bind(null, settings), transposeAstralsToCountableChar.bind(null, settings), stripSpaces.bind(null, settings), transposeHTMLEntitiesToCountableChars.bind(null, settings))(text);
 359    text = text + '\n';
 360    return (_text$match$length2 = (_text$match2 = text.match(regex)) === null || _text$match2 === void 0 ? void 0 : _text$match2.length) !== null && _text$match$length2 !== void 0 ? _text$match$length2 : 0;
 361  }
 362  /**
 363   * Count some words.
 364   *
 365   * @param {string}                  text         The text being processed
 366   * @param {WPWordCountStrategy}     type         The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
 367   * @param {WPWordCountUserSettings} userSettings Custom settings object.
 368   *
 369   * @example
 370   * ```js
 371   * import { count } from '@wordpress/wordcount';
 372   * const numberOfWords = count( 'Words to count', 'words', {} )
 373   * ```
 374   *
 375   * @return {number} The word or character count.
 376   */
 377  
 378  
 379  function count(text, type, userSettings) {
 380    const settings = loadSettings(type, userSettings);
 381    let matchRegExp;
 382  
 383    switch (settings.type) {
 384      case 'words':
 385        matchRegExp = settings.wordsRegExp;
 386        return countWords(text, matchRegExp, settings);
 387  
 388      case 'characters_including_spaces':
 389        matchRegExp = settings.characters_including_spacesRegExp;
 390        return countCharacters(text, matchRegExp, settings);
 391  
 392      case 'characters_excluding_spaces':
 393        matchRegExp = settings.characters_excluding_spacesRegExp;
 394        return countCharacters(text, matchRegExp, settings);
 395  
 396      default:
 397        return 0;
 398    }
 399  }
 400  
 401  (window.wp = window.wp || {}).wordcount = __webpack_exports__;
 402  /******/ })()
 403  ;


Generated: Thu Apr 25 01:00:03 2024 Cross-referenced by PHPXref 0.7.1