[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref