wp/wp-includes/js/dist/wordcount.js
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     1 /******/ (function() { // webpackBootstrap
     1 /******/ (() => { // webpackBootstrap
     2 /******/ 	"use strict";
     2 /******/ 	"use strict";
     3 /******/ 	// The require scope
     3 /******/ 	// The require scope
     4 /******/ 	var __webpack_require__ = {};
     4 /******/ 	var __webpack_require__ = {};
     5 /******/ 	
     5 /******/ 	
     6 /************************************************************************/
     6 /************************************************************************/
     7 /******/ 	/* webpack/runtime/define property getters */
     7 /******/ 	/* webpack/runtime/define property getters */
     8 /******/ 	!function() {
     8 /******/ 	(() => {
     9 /******/ 		// define getter functions for harmony exports
     9 /******/ 		// define getter functions for harmony exports
    10 /******/ 		__webpack_require__.d = function(exports, definition) {
    10 /******/ 		__webpack_require__.d = (exports, definition) => {
    11 /******/ 			for(var key in definition) {
    11 /******/ 			for(var key in definition) {
    12 /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
    12 /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
    13 /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
    13 /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
    14 /******/ 				}
    14 /******/ 				}
    15 /******/ 			}
    15 /******/ 			}
    16 /******/ 		};
    16 /******/ 		};
    17 /******/ 	}();
    17 /******/ 	})();
    18 /******/ 	
    18 /******/ 	
    19 /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
    19 /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
    20 /******/ 	!function() {
    20 /******/ 	(() => {
    21 /******/ 		__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
    21 /******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
    22 /******/ 	}();
    22 /******/ 	})();
    23 /******/ 	
    23 /******/ 	
    24 /******/ 	/* webpack/runtime/make namespace object */
    24 /******/ 	/* webpack/runtime/make namespace object */
    25 /******/ 	!function() {
    25 /******/ 	(() => {
    26 /******/ 		// define __esModule on exports
    26 /******/ 		// define __esModule on exports
    27 /******/ 		__webpack_require__.r = function(exports) {
    27 /******/ 		__webpack_require__.r = (exports) => {
    28 /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
    28 /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
    29 /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
    29 /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
    30 /******/ 			}
    30 /******/ 			}
    31 /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
    31 /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
    32 /******/ 		};
    32 /******/ 		};
    33 /******/ 	}();
    33 /******/ 	})();
    34 /******/ 	
    34 /******/ 	
    35 /************************************************************************/
    35 /************************************************************************/
    36 var __webpack_exports__ = {};
    36 var __webpack_exports__ = {};
    37 // ESM COMPAT FLAG
    37 // ESM COMPAT FLAG
    38 __webpack_require__.r(__webpack_exports__);
    38 __webpack_require__.r(__webpack_exports__);
    39 
    39 
    40 // EXPORTS
    40 // EXPORTS
    41 __webpack_require__.d(__webpack_exports__, {
    41 __webpack_require__.d(__webpack_exports__, {
    42   "count": function() { return /* binding */ count; }
    42   count: () => (/* binding */ count)
    43 });
    43 });
    44 
    44 
    45 ;// CONCATENATED MODULE: external "lodash"
       
    46 var external_lodash_namespaceObject = window["lodash"];
       
    47 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
    45 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
    48 /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
    46 /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
    49 
    47 
    50 /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
    48 /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
    51 
    49 
    70 /**
    68 /**
    71  * Lower-level settings for word counting that can be overridden.
    69  * Lower-level settings for word counting that can be overridden.
    72  *
    70  *
    73  * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
    71  * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
    74  */
    72  */
       
    73 
    75 // Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
    74 // 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 */
    75 /* eslint-disable jsdoc/valid-types */
    78 
       
    79 /**
    76 /**
    80  * Word counting settings that include non-optional values we set if missing
    77  * Word counting settings that include non-optional values we set if missing
    81  *
    78  *
    82  * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
    79  * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
    83  */
    80  */
    84 
       
    85 /* eslint-enable jsdoc/valid-types */
    81 /* eslint-enable jsdoc/valid-types */
       
    82 
    86 const defaultSettings = {
    83 const defaultSettings = {
    87   HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
    84   HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
    88   HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
    85   HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
    89   spaceRegExp: /&nbsp;|&#160;/gi,
    86   spaceRegExp: /&nbsp;|&#160;/gi,
    90   HTMLEntityRegExp: /&\S+?;/g,
    87   HTMLEntityRegExp: /&\S+?;/g,
    91   // \u2014 = em-dash.
    88   // \u2014 = em-dash.
    92   connectorRegExp: /--|\u2014/g,
    89   connectorRegExp: /--|\u2014/g,
    93   // Characters to be removed from input text.
    90   // Characters to be removed from input text.
    94   removeRegExp: new RegExp(['[', // Basic Latin (extract)
    91   removeRegExp: new RegExp(['[',
    95   '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E', // Latin-1 Supplement (extract)
    92   // Basic Latin (extract)
       
    93   '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E',
       
    94   // Latin-1 Supplement (extract)
    96   '\u0080-\u00BF\u00D7\u00F7',
    95   '\u0080-\u00BF\u00D7\u00F7',
    97   /*
    96   /*
    98    * The following range consists of:
    97    * The following range consists of:
    99    * General Punctuation
    98    * General Punctuation
   100    * Superscripts and Subscripts
    99    * Superscripts and Subscripts
   119    * Supplemental Arrows-B
   118    * Supplemental Arrows-B
   120    * Miscellaneous Mathematical Symbols-B
   119    * Miscellaneous Mathematical Symbols-B
   121    * Supplemental Mathematical Operators
   120    * Supplemental Mathematical Operators
   122    * Miscellaneous Symbols and Arrows
   121    * Miscellaneous Symbols and Arrows
   123    */
   122    */
   124   '\u2000-\u2BFF', // Supplemental Punctuation.
   123   '\u2000-\u2BFF',
       
   124   // Supplemental Punctuation.
   125   '\u2E00-\u2E7F', ']'].join(''), 'g'),
   125   '\u2E00-\u2E7F', ']'].join(''), 'g'),
   126   // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
   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,
   127   astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
   128   wordsRegExp: /\S\s+/g,
   128   wordsRegExp: /\S\s+/g,
   129   characters_excluding_spacesRegExp: /\S/g,
   129   characters_excluding_spacesRegExp: /\S/g,
   130 
       
   131   /*
   130   /*
   132    * Match anything that is not a formatting character, excluding:
   131    * Match anything that is not a formatting character, excluding:
   133    * \f = form feed
   132    * \f = form feed
   134    * \n = new line
   133    * \n = new line
   135    * \r = carriage return
   134    * \r = carriage return
   234  */
   233  */
   235 function stripShortcodes(settings, text) {
   234 function stripShortcodes(settings, text) {
   236   if (settings.shortcodesRegExp) {
   235   if (settings.shortcodesRegExp) {
   237     return text.replace(settings.shortcodesRegExp, '\n');
   236     return text.replace(settings.shortcodesRegExp, '\n');
   238   }
   237   }
   239 
       
   240   return text;
   238   return text;
   241 }
   239 }
   242 
   240 
   243 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
   241 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
   244 /**
   242 /**
   266   return text.replace(settings.HTMLEntityRegExp, 'a');
   264   return text.replace(settings.HTMLEntityRegExp, 'a');
   267 }
   265 }
   268 
   266 
   269 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
   267 ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
   270 /**
   268 /**
   271  * External dependencies
       
   272  */
       
   273 
       
   274 /**
       
   275  * Internal dependencies
   269  * Internal dependencies
   276  */
   270  */
   277 
   271 
   278 
   272 
   279 
   273 
   302  * @param {WPWordCountStrategy}     type         The type of count to be done.
   296  * @param {WPWordCountStrategy}     type         The type of count to be done.
   303  * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
   297  * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
   304  *
   298  *
   305  * @return {WPWordCountSettings} The combined settings object to be used.
   299  * @return {WPWordCountSettings} The combined settings object to be used.
   306  */
   300  */
   307 
       
   308 function loadSettings(type, userSettings) {
   301 function loadSettings(type, userSettings) {
   309   var _settings$l10n$shortc, _settings$l10n;
   302   var _settings$l10n$shortc;
   310 
   303   const settings = Object.assign({}, defaultSettings, userSettings);
   311   const settings = (0,external_lodash_namespaceObject.extend)({}, defaultSettings, userSettings);
   304   settings.shortcodes = (_settings$l10n$shortc = settings.l10n?.shortcodes) !== null && _settings$l10n$shortc !== void 0 ? _settings$l10n$shortc : [];
   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) {
   305   if (settings.shortcodes && settings.shortcodes.length) {
   315     settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
   306     settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
   316   }
   307   }
   317 
       
   318   settings.type = type;
   308   settings.type = type;
   319 
       
   320   if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
   309   if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
   321     settings.type = 'words';
   310     settings.type = 'words';
   322   }
   311   }
   323 
       
   324   return settings;
   312   return settings;
   325 }
   313 }
       
   314 
   326 /**
   315 /**
   327  * Count the words in text
   316  * Count the words in text
   328  *
   317  *
   329  * @param {string}              text     The text being processed
   318  * @param {string}              text     The text being processed
   330  * @param {RegExp}              regex    The regular expression pattern being matched
   319  * @param {RegExp}              regex    The regular expression pattern being matched
   331  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   320  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   332  *
   321  *
   333  * @return {number} Count of words.
   322  * @return {number} Count of words.
   334  */
   323  */
   335 
       
   336 
       
   337 function countWords(text, regex, settings) {
   324 function countWords(text, regex, settings) {
   338   var _text$match$length, _text$match;
   325   var _text$match$length;
   339 
   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);
   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';
   327   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;
   328   return (_text$match$length = text.match(regex)?.length) !== null && _text$match$length !== void 0 ? _text$match$length : 0;
   343 }
   329 }
       
   330 
   344 /**
   331 /**
   345  * Count the characters in text
   332  * Count the characters in text
   346  *
   333  *
   347  * @param {string}              text     The text being processed
   334  * @param {string}              text     The text being processed
   348  * @param {RegExp}              regex    The regular expression pattern being matched
   335  * @param {RegExp}              regex    The regular expression pattern being matched
   349  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   336  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   350  *
   337  *
   351  * @return {number} Count of characters.
   338  * @return {number} Count of characters.
   352  */
   339  */
   353 
       
   354 
       
   355 function countCharacters(text, regex, settings) {
   340 function countCharacters(text, regex, settings) {
   356   var _text$match$length2, _text$match2;
   341   var _text$match$length2;
   357 
   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);
   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';
   343   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;
   344   return (_text$match$length2 = text.match(regex)?.length) !== null && _text$match$length2 !== void 0 ? _text$match$length2 : 0;
   361 }
   345 }
       
   346 
   362 /**
   347 /**
   363  * Count some words.
   348  * Count some words.
   364  *
   349  *
   365  * @param {string}                  text         The text being processed
   350  * @param {string}                  text         The text being processed
   366  * @param {WPWordCountStrategy}     type         The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
   351  * @param {WPWordCountStrategy}     type         The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
   372  * const numberOfWords = count( 'Words to count', 'words', {} )
   357  * const numberOfWords = count( 'Words to count', 'words', {} )
   373  * ```
   358  * ```
   374  *
   359  *
   375  * @return {number} The word or character count.
   360  * @return {number} The word or character count.
   376  */
   361  */
   377 
       
   378 
       
   379 function count(text, type, userSettings) {
   362 function count(text, type, userSettings) {
   380   const settings = loadSettings(type, userSettings);
   363   const settings = loadSettings(type, userSettings);
   381   let matchRegExp;
   364   let matchRegExp;
   382 
       
   383   switch (settings.type) {
   365   switch (settings.type) {
   384     case 'words':
   366     case 'words':
   385       matchRegExp = settings.wordsRegExp;
   367       matchRegExp = settings.wordsRegExp;
   386       return countWords(text, matchRegExp, settings);
   368       return countWords(text, matchRegExp, settings);
   387 
       
   388     case 'characters_including_spaces':
   369     case 'characters_including_spaces':
   389       matchRegExp = settings.characters_including_spacesRegExp;
   370       matchRegExp = settings.characters_including_spacesRegExp;
   390       return countCharacters(text, matchRegExp, settings);
   371       return countCharacters(text, matchRegExp, settings);
   391 
       
   392     case 'characters_excluding_spaces':
   372     case 'characters_excluding_spaces':
   393       matchRegExp = settings.characters_excluding_spacesRegExp;
   373       matchRegExp = settings.characters_excluding_spacesRegExp;
   394       return countCharacters(text, matchRegExp, settings);
   374       return countCharacters(text, matchRegExp, settings);
   395 
       
   396     default:
   375     default:
   397       return 0;
   376       return 0;
   398   }
   377   }
   399 }
   378 }
   400 
   379