wordcount.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 = 352);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ 2:
  90. /***/ (function(module, exports) {
  91. (function() { module.exports = this["lodash"]; }());
  92. /***/ }),
  93. /***/ 352:
  94. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  95. "use strict";
  96. __webpack_require__.r(__webpack_exports__);
  97. // EXTERNAL MODULE: external "lodash"
  98. var external_lodash_ = __webpack_require__(2);
  99. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
  100. var defaultSettings = {
  101. HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  102. HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  103. spaceRegExp: /&nbsp;|&#160;/gi,
  104. HTMLEntityRegExp: /&\S+?;/g,
  105. // \u2014 = em-dash
  106. connectorRegExp: /--|\u2014/g,
  107. // Characters to be removed from input text.
  108. removeRegExp: new RegExp(['[', // Basic Latin (extract)
  109. "!-@[-`{-~", // Latin-1 Supplement (extract)
  110. "\x80-\xBF\xD7\xF7",
  111. /*
  112. * The following range consists of:
  113. * General Punctuation
  114. * Superscripts and Subscripts
  115. * Currency Symbols
  116. * Combining Diacritical Marks for Symbols
  117. * Letterlike Symbols
  118. * Number Forms
  119. * Arrows
  120. * Mathematical Operators
  121. * Miscellaneous Technical
  122. * Control Pictures
  123. * Optical Character Recognition
  124. * Enclosed Alphanumerics
  125. * Box Drawing
  126. * Block Elements
  127. * Geometric Shapes
  128. * Miscellaneous Symbols
  129. * Dingbats
  130. * Miscellaneous Mathematical Symbols-A
  131. * Supplemental Arrows-A
  132. * Braille Patterns
  133. * Supplemental Arrows-B
  134. * Miscellaneous Mathematical Symbols-B
  135. * Supplemental Mathematical Operators
  136. * Miscellaneous Symbols and Arrows
  137. */
  138. "\u2000-\u2BFF", // Supplemental Punctuation
  139. "\u2E00-\u2E7F", ']'].join(''), 'g'),
  140. // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
  141. astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  142. wordsRegExp: /\S\s+/g,
  143. characters_excluding_spacesRegExp: /\S/g,
  144. /*
  145. * Match anything that is not a formatting character, excluding:
  146. * \f = form feed
  147. * \n = new line
  148. * \r = carriage return
  149. * \t = tab
  150. * \v = vertical tab
  151. * \u00AD = soft hyphen
  152. * \u2028 = line separator
  153. * \u2029 = paragraph separator
  154. */
  155. characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
  156. l10n: {
  157. type: 'words'
  158. }
  159. };
  160. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
  161. /**
  162. * Replaces items matched in the regex with new line
  163. *
  164. * @param {Object} 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. /* harmony default export */ var stripTags = (function (settings, text) {
  170. if (settings.HTMLRegExp) {
  171. return text.replace(settings.HTMLRegExp, '\n');
  172. }
  173. });
  174. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
  175. /**
  176. * Replaces items matched in the regex with character.
  177. *
  178. * @param {Object} 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. /* harmony default export */ var transposeAstralsToCountableChar = (function (settings, text) {
  184. if (settings.astralRegExp) {
  185. return text.replace(settings.astralRegExp, 'a');
  186. }
  187. return text;
  188. });
  189. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
  190. /**
  191. * Removes items matched in the regex.
  192. *
  193. * @param {Object} settings The main settings object containing regular expressions
  194. * @param {string} text The string being counted.
  195. *
  196. * @return {string} The manipulated text.
  197. */
  198. /* harmony default export */ var stripHTMLEntities = (function (settings, text) {
  199. if (settings.HTMLEntityRegExp) {
  200. return text.replace(settings.HTMLEntityRegExp, '');
  201. }
  202. return text;
  203. });
  204. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
  205. /**
  206. * Replaces items matched in the regex with spaces.
  207. *
  208. * @param {Object} settings The main settings object containing regular expressions
  209. * @param {string} text The string being counted.
  210. *
  211. * @return {string} The manipulated text.
  212. */
  213. /* harmony default export */ var stripConnectors = (function (settings, text) {
  214. if (settings.connectorRegExp) {
  215. return text.replace(settings.connectorRegExp, ' ');
  216. }
  217. return text;
  218. });
  219. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
  220. /**
  221. * Removes items matched in the regex.
  222. *
  223. * @param {Object} settings The main settings object containing regular expressions
  224. * @param {string} text The string being counted.
  225. *
  226. * @return {string} The manipulated text.
  227. */
  228. /* harmony default export */ var stripRemovables = (function (settings, text) {
  229. if (settings.removeRegExp) {
  230. return text.replace(settings.removeRegExp, '');
  231. }
  232. return text;
  233. });
  234. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
  235. /**
  236. * Removes items matched in the regex.
  237. *
  238. * @param {Object} settings The main settings object containing regular expressions
  239. * @param {string} text The string being counted.
  240. *
  241. * @return {string} The manipulated text.
  242. */
  243. /* harmony default export */ var stripHTMLComments = (function (settings, text) {
  244. if (settings.HTMLcommentRegExp) {
  245. return text.replace(settings.HTMLcommentRegExp, '');
  246. }
  247. return text;
  248. });
  249. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
  250. /**
  251. * Replaces items matched in the regex with a new line.
  252. *
  253. * @param {Object} settings The main settings object containing regular expressions
  254. * @param {string} text The string being counted.
  255. *
  256. * @return {string} The manipulated text.
  257. */
  258. /* harmony default export */ var stripShortcodes = (function (settings, text) {
  259. if (settings.shortcodesRegExp) {
  260. return text.replace(settings.shortcodesRegExp, '\n');
  261. }
  262. return text;
  263. });
  264. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
  265. /**
  266. * Replaces items matched in the regex with spaces.
  267. *
  268. * @param {Object} settings The main settings object containing regular expressions
  269. * @param {string} text The string being counted.
  270. *
  271. * @return {string} The manipulated text.
  272. */
  273. /* harmony default export */ var stripSpaces = (function (settings, text) {
  274. if (settings.spaceRegExp) {
  275. return text.replace(settings.spaceRegExp, ' ');
  276. }
  277. });
  278. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
  279. /**
  280. * Replaces items matched in the regex with a single character.
  281. *
  282. * @param {Object} settings The main settings object containing regular expressions
  283. * @param {string} text The string being counted.
  284. *
  285. * @return {string} The manipulated text.
  286. */
  287. /* harmony default export */ var transposeHTMLEntitiesToCountableChars = (function (settings, text) {
  288. if (settings.HTMLEntityRegExp) {
  289. return text.replace(settings.HTMLEntityRegExp, 'a');
  290. }
  291. return text;
  292. });
  293. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
  294. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "count", function() { return count; });
  295. /**
  296. * External dependencies
  297. */
  298. /**
  299. * Internal dependencies
  300. */
  301. /**
  302. * Private function to manage the settings.
  303. *
  304. * @param {string} type The type of count to be done.
  305. * @param {Object} userSettings Custom settings for the count.
  306. *
  307. * @return {void|Object|*} The combined settings object to be used.
  308. */
  309. function loadSettings(type, userSettings) {
  310. var settings = Object(external_lodash_["extend"])(defaultSettings, userSettings);
  311. settings.shortcodes = settings.l10n.shortcodes || {};
  312. if (settings.shortcodes && settings.shortcodes.length) {
  313. settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
  314. }
  315. settings.type = type || settings.l10n.type;
  316. if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
  317. settings.type = 'words';
  318. }
  319. return settings;
  320. }
  321. /**
  322. * Match the regex for the type 'words'
  323. *
  324. * @param {string} text The text being processed
  325. * @param {string} regex The regular expression pattern being matched
  326. * @param {Object} settings Settings object containing regular expressions for each strip function
  327. *
  328. * @return {Array|{index: number, input: string}} The matched string.
  329. */
  330. function matchWords(text, regex, settings) {
  331. text = Object(external_lodash_["flow"])(stripTags.bind(this, settings), stripHTMLComments.bind(this, settings), stripShortcodes.bind(this, settings), stripSpaces.bind(this, settings), stripHTMLEntities.bind(this, settings), stripConnectors.bind(this, settings), stripRemovables.bind(this, settings))(text);
  332. text = text + '\n';
  333. return text.match(regex);
  334. }
  335. /**
  336. * Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces'
  337. *
  338. * @param {string} text The text being processed
  339. * @param {string} regex The regular expression pattern being matched
  340. * @param {Object} settings Settings object containing regular expressions for each strip function
  341. *
  342. * @return {Array|{index: number, input: string}} The matched string.
  343. */
  344. function matchCharacters(text, regex, settings) {
  345. text = Object(external_lodash_["flow"])(stripTags.bind(this, settings), stripHTMLComments.bind(this, settings), stripShortcodes.bind(this, settings), stripSpaces.bind(this, settings), transposeAstralsToCountableChar.bind(this, settings), transposeHTMLEntitiesToCountableChars.bind(this, settings))(text);
  346. text = text + '\n';
  347. return text.match(regex);
  348. }
  349. /**
  350. * Count some words.
  351. *
  352. * @param {string} text The text being processed
  353. * @param {string} type The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
  354. * @param {Object} userSettings Custom settings object.
  355. *
  356. * @example
  357. * ```js
  358. * import { count } from '@wordpress/wordcount';
  359. * const numberOfWords = count( 'Words to count', 'words', {} )
  360. * ```
  361. *
  362. * @return {number} The word or character count.
  363. */
  364. function count(text, type, userSettings) {
  365. if ('' === text) {
  366. return 0;
  367. }
  368. if (text) {
  369. var settings = loadSettings(type, userSettings);
  370. var matchRegExp = settings[type + 'RegExp'];
  371. var results = 'words' === settings.type ? matchWords(text, matchRegExp, settings) : matchCharacters(text, matchRegExp, settings);
  372. return results ? results.length : 0;
  373. }
  374. }
  375. /***/ })
  376. /******/ });