Açıklama Yok

wordcount.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /***/ "YLtl":
  90. /***/ (function(module, exports) {
  91. (function() { module.exports = window["lodash"]; }());
  92. /***/ }),
  93. /***/ "pC98":
  94. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  95. "use strict";
  96. // ESM COMPAT FLAG
  97. __webpack_require__.r(__webpack_exports__);
  98. // EXPORTS
  99. __webpack_require__.d(__webpack_exports__, "count", function() { return /* binding */ count; });
  100. // EXTERNAL MODULE: external "lodash"
  101. var external_lodash_ = __webpack_require__("YLtl");
  102. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
  103. /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
  104. /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
  105. /**
  106. * @typedef WPWordCountSettingsFields
  107. * @property {RegExp} HTMLRegExp Regular expression that matches HTML tags
  108. * @property {RegExp} HTMLcommentRegExp Regular expression that matches HTML comments
  109. * @property {RegExp} spaceRegExp Regular expression that matches spaces in HTML
  110. * @property {RegExp} HTMLEntityRegExp Regular expression that matches HTML entities
  111. * @property {RegExp} connectorRegExp Regular expression that matches word connectors, like em-dash
  112. * @property {RegExp} removeRegExp Regular expression that matches various characters to be removed when counting
  113. * @property {RegExp} astralRegExp Regular expression that matches astral UTF-16 code points
  114. * @property {RegExp} wordsRegExp Regular expression that matches words
  115. * @property {RegExp} characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
  116. * @property {RegExp} characters_including_spacesRegExp Regular expression that matches characters including spaces
  117. * @property {RegExp} shortcodesRegExp Regular expression that matches WordPress shortcodes
  118. * @property {string[]} shortcodes List of all shortcodes
  119. * @property {WPWordCountStrategy} type Describes what and how are we counting
  120. * @property {WPWordCountL10n} l10n Object with human translations
  121. */
  122. /**
  123. * Lower-level settings for word counting that can be overridden.
  124. *
  125. * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
  126. */
  127. // Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
  128. /* eslint-disable jsdoc/valid-types */
  129. /**
  130. * Word counting settings that include non-optional values we set if missing
  131. *
  132. * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
  133. */
  134. /* eslint-enable jsdoc/valid-types */
  135. const defaultSettings = {
  136. HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  137. HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  138. spaceRegExp: /&nbsp;|&#160;/gi,
  139. HTMLEntityRegExp: /&\S+?;/g,
  140. // \u2014 = em-dash
  141. connectorRegExp: /--|\u2014/g,
  142. // Characters to be removed from input text.
  143. removeRegExp: new RegExp(['[', // Basic Latin (extract)
  144. '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E', // Latin-1 Supplement (extract)
  145. '\u0080-\u00BF\u00D7\u00F7',
  146. /*
  147. * The following range consists of:
  148. * General Punctuation
  149. * Superscripts and Subscripts
  150. * Currency Symbols
  151. * Combining Diacritical Marks for Symbols
  152. * Letterlike Symbols
  153. * Number Forms
  154. * Arrows
  155. * Mathematical Operators
  156. * Miscellaneous Technical
  157. * Control Pictures
  158. * Optical Character Recognition
  159. * Enclosed Alphanumerics
  160. * Box Drawing
  161. * Block Elements
  162. * Geometric Shapes
  163. * Miscellaneous Symbols
  164. * Dingbats
  165. * Miscellaneous Mathematical Symbols-A
  166. * Supplemental Arrows-A
  167. * Braille Patterns
  168. * Supplemental Arrows-B
  169. * Miscellaneous Mathematical Symbols-B
  170. * Supplemental Mathematical Operators
  171. * Miscellaneous Symbols and Arrows
  172. */
  173. '\u2000-\u2BFF', // Supplemental Punctuation
  174. '\u2E00-\u2E7F', ']'].join(''), 'g'),
  175. // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
  176. astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  177. wordsRegExp: /\S\s+/g,
  178. characters_excluding_spacesRegExp: /\S/g,
  179. /*
  180. * Match anything that is not a formatting character, excluding:
  181. * \f = form feed
  182. * \n = new line
  183. * \r = carriage return
  184. * \t = tab
  185. * \v = vertical tab
  186. * \u00AD = soft hyphen
  187. * \u2028 = line separator
  188. * \u2029 = paragraph separator
  189. */
  190. characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
  191. l10n: {
  192. type: 'words'
  193. }
  194. };
  195. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
  196. /**
  197. * Replaces items matched in the regex with new line
  198. *
  199. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  200. * @param {string} text The string being counted.
  201. *
  202. * @return {string} The manipulated text.
  203. */
  204. function stripTags(settings, text) {
  205. return text.replace(settings.HTMLRegExp, '\n');
  206. }
  207. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
  208. /**
  209. * Replaces items matched in the regex with character.
  210. *
  211. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  212. * @param {string} text The string being counted.
  213. *
  214. * @return {string} The manipulated text.
  215. */
  216. function transposeAstralsToCountableChar(settings, text) {
  217. return text.replace(settings.astralRegExp, 'a');
  218. }
  219. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
  220. /**
  221. * Removes items matched in the regex.
  222. *
  223. * @param {import('./index').WPWordCountSettings} 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. function stripHTMLEntities(settings, text) {
  229. return text.replace(settings.HTMLEntityRegExp, '');
  230. }
  231. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
  232. /**
  233. * Replaces items matched in the regex with spaces.
  234. *
  235. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  236. * @param {string} text The string being counted.
  237. *
  238. * @return {string} The manipulated text.
  239. */
  240. function stripConnectors(settings, text) {
  241. return text.replace(settings.connectorRegExp, ' ');
  242. }
  243. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
  244. /**
  245. * Removes items matched in the regex.
  246. *
  247. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  248. * @param {string} text The string being counted.
  249. *
  250. * @return {string} The manipulated text.
  251. */
  252. function stripRemovables(settings, text) {
  253. return text.replace(settings.removeRegExp, '');
  254. }
  255. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
  256. /**
  257. * Removes items matched in the regex.
  258. *
  259. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  260. * @param {string} text The string being counted.
  261. *
  262. * @return {string} The manipulated text.
  263. */
  264. function stripHTMLComments(settings, text) {
  265. return text.replace(settings.HTMLcommentRegExp, '');
  266. }
  267. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
  268. /**
  269. * Replaces items matched in the regex with a new line.
  270. *
  271. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  272. * @param {string} text The string being counted.
  273. *
  274. * @return {string} The manipulated text.
  275. */
  276. function stripShortcodes(settings, text) {
  277. if (settings.shortcodesRegExp) {
  278. return text.replace(settings.shortcodesRegExp, '\n');
  279. }
  280. return text;
  281. }
  282. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
  283. /**
  284. * Replaces items matched in the regex with spaces.
  285. *
  286. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  287. * @param {string} text The string being counted.
  288. *
  289. * @return {string} The manipulated text.
  290. */
  291. function stripSpaces(settings, text) {
  292. return text.replace(settings.spaceRegExp, ' ');
  293. }
  294. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
  295. /**
  296. * Replaces items matched in the regex with a single character.
  297. *
  298. * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
  299. * @param {string} text The string being counted.
  300. *
  301. * @return {string} The manipulated text.
  302. */
  303. function transposeHTMLEntitiesToCountableChars(settings, text) {
  304. return text.replace(settings.HTMLEntityRegExp, 'a');
  305. }
  306. // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
  307. /**
  308. * External dependencies
  309. */
  310. /**
  311. * Internal dependencies
  312. */
  313. /**
  314. * @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings
  315. * @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings
  316. */
  317. /**
  318. * Possible ways of counting.
  319. *
  320. * @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
  321. */
  322. /**
  323. * Private function to manage the settings.
  324. *
  325. * @param {WPWordCountStrategy} type The type of count to be done.
  326. * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
  327. *
  328. * @return {WPWordCountSettings} The combined settings object to be used.
  329. */
  330. function loadSettings(type, userSettings) {
  331. var _settings$l10n$shortc, _settings$l10n;
  332. const settings = Object(external_lodash_["extend"])({}, defaultSettings, userSettings);
  333. 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 : [];
  334. if (settings.shortcodes && settings.shortcodes.length) {
  335. settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
  336. }
  337. settings.type = type;
  338. if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
  339. settings.type = 'words';
  340. }
  341. return settings;
  342. }
  343. /**
  344. * Count the words in text
  345. *
  346. * @param {string} text The text being processed
  347. * @param {RegExp} regex The regular expression pattern being matched
  348. * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
  349. *
  350. * @return {number} Count of words.
  351. */
  352. function countWords(text, regex, settings) {
  353. var _text$match$length, _text$match;
  354. 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);
  355. text = text + '\n';
  356. 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;
  357. }
  358. /**
  359. * Count the characters in text
  360. *
  361. * @param {string} text The text being processed
  362. * @param {RegExp} regex The regular expression pattern being matched
  363. * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
  364. *
  365. * @return {number} Count of characters.
  366. */
  367. function countCharacters(text, regex, settings) {
  368. var _text$match$length2, _text$match2;
  369. 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);
  370. text = text + '\n';
  371. 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;
  372. }
  373. /**
  374. * Count some words.
  375. *
  376. * @param {string} text The text being processed
  377. * @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
  378. * @param {WPWordCountUserSettings} userSettings Custom settings object.
  379. *
  380. * @example
  381. * ```js
  382. * import { count } from '@wordpress/wordcount';
  383. * const numberOfWords = count( 'Words to count', 'words', {} )
  384. * ```
  385. *
  386. * @return {number} The word or character count.
  387. */
  388. function count(text, type, userSettings) {
  389. const settings = loadSettings(type, userSettings);
  390. let matchRegExp;
  391. switch (settings.type) {
  392. case 'words':
  393. matchRegExp = settings.wordsRegExp;
  394. return countWords(text, matchRegExp, settings);
  395. case 'characters_including_spaces':
  396. matchRegExp = settings.characters_including_spacesRegExp;
  397. return countCharacters(text, matchRegExp, settings);
  398. case 'characters_excluding_spaces':
  399. matchRegExp = settings.characters_excluding_spacesRegExp;
  400. return countCharacters(text, matchRegExp, settings);
  401. default:
  402. return 0;
  403. }
  404. }
  405. /***/ })
  406. /******/ });