wordcount.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. // EXPORTS
  40. __webpack_require__.d(__webpack_exports__, {
  41. "count": function() { return /* binding */ count; }
  42. });
  43. ;// CONCATENATED MODULE: external "lodash"
  44. var external_lodash_namespaceObject = window["lodash"];
  45. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
  46. /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
  47. /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
  48. /**
  49. * @typedef WPWordCountSettingsFields
  50. * @property {RegExp} HTMLRegExp Regular expression that matches HTML tags
  51. * @property {RegExp} HTMLcommentRegExp Regular expression that matches HTML comments
  52. * @property {RegExp} spaceRegExp Regular expression that matches spaces in HTML
  53. * @property {RegExp} HTMLEntityRegExp Regular expression that matches HTML entities
  54. * @property {RegExp} connectorRegExp Regular expression that matches word connectors, like em-dash
  55. * @property {RegExp} removeRegExp Regular expression that matches various characters to be removed when counting
  56. * @property {RegExp} astralRegExp Regular expression that matches astral UTF-16 code points
  57. * @property {RegExp} wordsRegExp Regular expression that matches words
  58. * @property {RegExp} characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
  59. * @property {RegExp} characters_including_spacesRegExp Regular expression that matches characters including spaces
  60. * @property {RegExp} shortcodesRegExp Regular expression that matches WordPress shortcodes
  61. * @property {string[]} shortcodes List of all shortcodes
  62. * @property {WPWordCountStrategy} type Describes what and how are we counting
  63. * @property {WPWordCountL10n} l10n Object with human translations
  64. */
  65. /**
  66. * Lower-level settings for word counting that can be overridden.
  67. *
  68. * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
  69. */
  70. // Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
  71. /* eslint-disable jsdoc/valid-types */
  72. /**
  73. * Word counting settings that include non-optional values we set if missing
  74. *
  75. * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
  76. */
  77. /* eslint-enable jsdoc/valid-types */
  78. const defaultSettings = {
  79. HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  80. HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  81. spaceRegExp: /&nbsp;|&#160;/gi,
  82. HTMLEntityRegExp: /&\S+?;/g,
  83. // \u2014 = em-dash.
  84. connectorRegExp: /--|\u2014/g,
  85. // Characters to be removed from input text.
  86. removeRegExp: new RegExp(['[', // Basic Latin (extract)
  87. '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E', // Latin-1 Supplement (extract)
  88. '\u0080-\u00BF\u00D7\u00F7',
  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', // Supplemental Punctuation.
  117. '\u2E00-\u2E7F', ']'].join(''), 'g'),
  118. // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
  119. astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  120. wordsRegExp: /\S\s+/g,
  121. characters_excluding_spacesRegExp: /\S/g,
  122. /*
  123. * Match anything that is not a formatting character, excluding:
  124. * \f = form feed
  125. * \n = new line
  126. * \r = carriage return
  127. * \t = tab
  128. * \v = vertical tab
  129. * \u00AD = soft hyphen
  130. * \u2028 = line separator
  131. * \u2029 = paragraph separator
  132. */
  133. characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
  134. l10n: {
  135. type: 'words'
  136. }
  137. };
  138. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
  139. /**
  140. * Replaces items matched in the regex with new line
  141. *
  142. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  143. * @param {string} text The string being counted.
  144. *
  145. * @return {string} The manipulated text.
  146. */
  147. function stripTags(settings, text) {
  148. return text.replace(settings.HTMLRegExp, '\n');
  149. }
  150. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
  151. /**
  152. * Replaces items matched in the regex with character.
  153. *
  154. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  155. * @param {string} text The string being counted.
  156. *
  157. * @return {string} The manipulated text.
  158. */
  159. function transposeAstralsToCountableChar(settings, text) {
  160. return text.replace(settings.astralRegExp, 'a');
  161. }
  162. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
  163. /**
  164. * Removes items matched in the regex.
  165. *
  166. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  167. * @param {string} text The string being counted.
  168. *
  169. * @return {string} The manipulated text.
  170. */
  171. function stripHTMLEntities(settings, text) {
  172. return text.replace(settings.HTMLEntityRegExp, '');
  173. }
  174. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
  175. /**
  176. * Replaces items matched in the regex with spaces.
  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 stripConnectors(settings, text) {
  184. return text.replace(settings.connectorRegExp, ' ');
  185. }
  186. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
  187. /**
  188. * Removes items matched in the regex.
  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 stripRemovables(settings, text) {
  196. return text.replace(settings.removeRegExp, '');
  197. }
  198. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
  199. /**
  200. * Removes items matched in the regex.
  201. *
  202. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  203. * @param {string} text The string being counted.
  204. *
  205. * @return {string} The manipulated text.
  206. */
  207. function stripHTMLComments(settings, text) {
  208. return text.replace(settings.HTMLcommentRegExp, '');
  209. }
  210. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
  211. /**
  212. * Replaces items matched in the regex with a new line.
  213. *
  214. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  215. * @param {string} text The string being counted.
  216. *
  217. * @return {string} The manipulated text.
  218. */
  219. function stripShortcodes(settings, text) {
  220. if (settings.shortcodesRegExp) {
  221. return text.replace(settings.shortcodesRegExp, '\n');
  222. }
  223. return text;
  224. }
  225. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
  226. /**
  227. * Replaces items matched in the regex with spaces.
  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 stripSpaces(settings, text) {
  235. return text.replace(settings.spaceRegExp, ' ');
  236. }
  237. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
  238. /**
  239. * Replaces items matched in the regex with a single character.
  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 transposeHTMLEntitiesToCountableChars(settings, text) {
  247. return text.replace(settings.HTMLEntityRegExp, 'a');
  248. }
  249. ;// CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
  250. /**
  251. * External dependencies
  252. */
  253. /**
  254. * Internal dependencies
  255. */
  256. /**
  257. * @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings
  258. * @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings
  259. */
  260. /**
  261. * Possible ways of counting.
  262. *
  263. * @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
  264. */
  265. /**
  266. * Private function to manage the settings.
  267. *
  268. * @param {WPWordCountStrategy} type The type of count to be done.
  269. * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
  270. *
  271. * @return {WPWordCountSettings} The combined settings object to be used.
  272. */
  273. function loadSettings(type, userSettings) {
  274. var _settings$l10n$shortc, _settings$l10n;
  275. const settings = (0,external_lodash_namespaceObject.extend)({}, defaultSettings, userSettings);
  276. 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 : [];
  277. if (settings.shortcodes && settings.shortcodes.length) {
  278. settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
  279. }
  280. settings.type = type;
  281. if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
  282. settings.type = 'words';
  283. }
  284. return settings;
  285. }
  286. /**
  287. * Count the words in text
  288. *
  289. * @param {string} text The text being processed
  290. * @param {RegExp} regex The regular expression pattern being matched
  291. * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
  292. *
  293. * @return {number} Count of words.
  294. */
  295. function countWords(text, regex, settings) {
  296. var _text$match$length, _text$match;
  297. 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);
  298. text = text + '\n';
  299. 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;
  300. }
  301. /**
  302. * Count the characters in text
  303. *
  304. * @param {string} text The text being processed
  305. * @param {RegExp} regex The regular expression pattern being matched
  306. * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
  307. *
  308. * @return {number} Count of characters.
  309. */
  310. function countCharacters(text, regex, settings) {
  311. var _text$match$length2, _text$match2;
  312. 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);
  313. text = text + '\n';
  314. 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;
  315. }
  316. /**
  317. * Count some words.
  318. *
  319. * @param {string} text The text being processed
  320. * @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
  321. * @param {WPWordCountUserSettings} userSettings Custom settings object.
  322. *
  323. * @example
  324. * ```js
  325. * import { count } from '@wordpress/wordcount';
  326. * const numberOfWords = count( 'Words to count', 'words', {} )
  327. * ```
  328. *
  329. * @return {number} The word or character count.
  330. */
  331. function count(text, type, userSettings) {
  332. const settings = loadSettings(type, userSettings);
  333. let matchRegExp;
  334. switch (settings.type) {
  335. case 'words':
  336. matchRegExp = settings.wordsRegExp;
  337. return countWords(text, matchRegExp, settings);
  338. case 'characters_including_spaces':
  339. matchRegExp = settings.characters_including_spacesRegExp;
  340. return countCharacters(text, matchRegExp, settings);
  341. case 'characters_excluding_spaces':
  342. matchRegExp = settings.characters_excluding_spacesRegExp;
  343. return countCharacters(text, matchRegExp, settings);
  344. default:
  345. return 0;
  346. }
  347. }
  348. (window.wp = window.wp || {}).wordcount = __webpack_exports__;
  349. /******/ })()
  350. ;