ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

wordcount.js (17740B)


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