wordcount.js 15 KB

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