PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / wordcount / index.js

index.js in Gutenberg 20.0.1, at build/wordcount/index.js

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