PluginProbe
Gutenberg / 11.6.0
Gutenberg v11.6.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 7.4.0 All 402 releases
gutenberg / build / shortcode / index.js

index.js in Gutenberg 11.6.0, at build/shortcode/index.js

599 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /******/ (function() { // webpackBootstrap
2 /******/ var __webpack_modules__ = ({
3
4 /***/ 9588:
5 /***/ (function(module) {
6
7 /**
8 * Memize options object.
9 *
10 * @typedef MemizeOptions
11 *
12 * @property {number} [maxSize] Maximum size of the cache.
13 */
14
15 /**
16 * Internal cache entry.
17 *
18 * @typedef MemizeCacheNode
19 *
20 * @property {?MemizeCacheNode|undefined} [prev] Previous node.
21 * @property {?MemizeCacheNode|undefined} [next] Next node.
22 * @property {Array<*>} args Function arguments for cache
23 * entry.
24 * @property {*} val Function result.
25 */
26
27 /**
28 * Properties of the enhanced function for controlling cache.
29 *
30 * @typedef MemizeMemoizedFunction
31 *
32 * @property {()=>void} clear Clear the cache.
33 */
34
35 /**
36 * Accepts a function to be memoized, and returns a new memoized function, with
37 * optional options.
38 *
39 * @template {Function} F
40 *
41 * @param {F} fn Function to memoize.
42 * @param {MemizeOptions} [options] Options object.
43 *
44 * @return {F & MemizeMemoizedFunction} Memoized function.
45 */
46 function memize( fn, options ) {
47 var size = 0;
48
49 /** @type {?MemizeCacheNode|undefined} */
50 var head;
51
52 /** @type {?MemizeCacheNode|undefined} */
53 var tail;
54
55 options = options || {};
56
57 function memoized( /* ...args */ ) {
58 var node = head,
59 len = arguments.length,
60 args, i;
61
62 searchCache: while ( node ) {
63 // Perform a shallow equality test to confirm that whether the node
64 // under test is a candidate for the arguments passed. Two arrays
65 // are shallowly equal if their length matches and each entry is
66 // strictly equal between the two sets. Avoid abstracting to a
67 // function which could incur an arguments leaking deoptimization.
68
69 // Check whether node arguments match arguments length
70 if ( node.args.length !== arguments.length ) {
71 node = node.next;
72 continue;
73 }
74
75 // Check whether node arguments match arguments values
76 for ( i = 0; i < len; i++ ) {
77 if ( node.args[ i ] !== arguments[ i ] ) {
78 node = node.next;
79 continue searchCache;
80 }
81 }
82
83 // At this point we can assume we've found a match
84
85 // Surface matched node to head if not already
86 if ( node !== head ) {
87 // As tail, shift to previous. Must only shift if not also
88 // head, since if both head and tail, there is no previous.
89 if ( node === tail ) {
90 tail = node.prev;
91 }
92
93 // Adjust siblings to point to each other. If node was tail,
94 // this also handles new tail's empty `next` assignment.
95 /** @type {MemizeCacheNode} */ ( node.prev ).next = node.next;
96 if ( node.next ) {
97 node.next.prev = node.prev;
98 }
99
100 node.next = head;
101 node.prev = null;
102 /** @type {MemizeCacheNode} */ ( head ).prev = node;
103 head = node;
104 }
105
106 // Return immediately
107 return node.val;
108 }
109
110 // No cached value found. Continue to insertion phase:
111
112 // Create a copy of arguments (avoid leaking deoptimization)
113 args = new Array( len );
114 for ( i = 0; i < len; i++ ) {
115 args[ i ] = arguments[ i ];
116 }
117
118 node = {
119 args: args,
120
121 // Generate the result from original function
122 val: fn.apply( null, args ),
123 };
124
125 // Don't need to check whether node is already head, since it would
126 // have been returned above already if it was
127
128 // Shift existing head down list
129 if ( head ) {
130 head.prev = node;
131 node.next = head;
132 } else {
133 // If no head, follows that there's no tail (at initial or reset)
134 tail = node;
135 }
136
137 // Trim tail if we're reached max size and are pending cache insertion
138 if ( size === /** @type {MemizeOptions} */ ( options ).maxSize ) {
139 tail = /** @type {MemizeCacheNode} */ ( tail ).prev;
140 /** @type {MemizeCacheNode} */ ( tail ).next = null;
141 } else {
142 size++;
143 }
144
145 head = node;
146
147 return node.val;
148 }
149
150 memoized.clear = function() {
151 head = null;
152 tail = null;
153 size = 0;
154 };
155
156 if ( false ) {}
157
158 // Ignore reason: There's not a clear solution to create an intersection of
159 // the function with additional properties, where the goal is to retain the
160 // function signature of the incoming argument and add control properties
161 // on the return value.
162
163 // @ts-ignore
164 return memoized;
165 }
166
167 module.exports = memize;
168
169
170 /***/ })
171
172 /******/ });
173 /************************************************************************/
174 /******/ // The module cache
175 /******/ var __webpack_module_cache__ = {};
176 /******/
177 /******/ // The require function
178 /******/ function __webpack_require__(moduleId) {
179 /******/ // Check if module is in cache
180 /******/ var cachedModule = __webpack_module_cache__[moduleId];
181 /******/ if (cachedModule !== undefined) {
182 /******/ return cachedModule.exports;
183 /******/ }
184 /******/ // Create a new module (and put it into the cache)
185 /******/ var module = __webpack_module_cache__[moduleId] = {
186 /******/ // no module.id needed
187 /******/ // no module.loaded needed
188 /******/ exports: {}
189 /******/ };
190 /******/
191 /******/ // Execute the module function
192 /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
193 /******/
194 /******/ // Return the exports of the module
195 /******/ return module.exports;
196 /******/ }
197 /******/
198 /************************************************************************/
199 /******/ /* webpack/runtime/compat get default export */
200 /******/ !function() {
201 /******/ // getDefaultExport function for compatibility with non-harmony modules
202 /******/ __webpack_require__.n = function(module) {
203 /******/ var getter = module && module.__esModule ?
204 /******/ function() { return module['default']; } :
205 /******/ function() { return module; };
206 /******/ __webpack_require__.d(getter, { a: getter });
207 /******/ return getter;
208 /******/ };
209 /******/ }();
210 /******/
211 /******/ /* webpack/runtime/define property getters */
212 /******/ !function() {
213 /******/ // define getter functions for harmony exports
214 /******/ __webpack_require__.d = function(exports, definition) {
215 /******/ for(var key in definition) {
216 /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
217 /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
218 /******/ }
219 /******/ }
220 /******/ };
221 /******/ }();
222 /******/
223 /******/ /* webpack/runtime/hasOwnProperty shorthand */
224 /******/ !function() {
225 /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
226 /******/ }();
227 /******/
228 /************************************************************************/
229 var __webpack_exports__ = {};
230 // This entry need to be wrapped in an IIFE because it need to be in strict mode.
231 !function() {
232 "use strict";
233
234 // EXPORTS
235 __webpack_require__.d(__webpack_exports__, {
236 "default": function() { return /* binding */ build_module; }
237 });
238
239 // UNUSED EXPORTS: attrs, fromMatch, next, regexp, replace, string
240
241 ;// CONCATENATED MODULE: external "lodash"
242 var external_lodash_namespaceObject = window["lodash"];
243 // EXTERNAL MODULE: ./node_modules/memize/index.js
244 var memize = __webpack_require__(9588);
245 var memize_default = /*#__PURE__*/__webpack_require__.n(memize);
246 ;// CONCATENATED MODULE: ./packages/shortcode/build-module/index.js
247 /**
248 * External dependencies
249 */
250
251
252 /**
253 * Shortcode attributes object.
254 *
255 * @typedef {Object} WPShortcodeAttrs
256 *
257 * @property {Object} named Object with named attributes.
258 * @property {Array} numeric Array with numeric attributes.
259 */
260
261 /**
262 * Shortcode object.
263 *
264 * @typedef {Object} WPShortcode
265 *
266 * @property {string} tag Shortcode tag.
267 * @property {WPShortcodeAttrs} attrs Shortcode attributes.
268 * @property {string} content Shortcode content.
269 * @property {string} type Shortcode type: `self-closing`,
270 * `closed`, or `single`.
271 */
272
273 /**
274 * @typedef {Object} WPShortcodeMatch
275 *
276 * @property {number} index Index the shortcode is found at.
277 * @property {string} content Matched content.
278 * @property {WPShortcode} shortcode Shortcode instance of the match.
279 */
280
281 /**
282 * Find the next matching shortcode.
283 *
284 * @param {string} tag Shortcode tag.
285 * @param {string} text Text to search.
286 * @param {number} index Index to start search from.
287 *
288 * @return {?WPShortcodeMatch} Matched information.
289 */
290
291 function next(tag, text, index = 0) {
292 const re = regexp(tag);
293 re.lastIndex = index;
294 const match = re.exec(text);
295
296 if (!match) {
297 return;
298 } // If we matched an escaped shortcode, try again.
299
300
301 if ('[' === match[1] && ']' === match[7]) {
302 return next(tag, text, re.lastIndex);
303 }
304
305 const result = {
306 index: match.index,
307 content: match[0],
308 shortcode: fromMatch(match)
309 }; // If we matched a leading `[`, strip it from the match and increment the
310 // index accordingly.
311
312 if (match[1]) {
313 result.content = result.content.slice(1);
314 result.index++;
315 } // If we matched a trailing `]`, strip it from the match.
316
317
318 if (match[7]) {
319 result.content = result.content.slice(0, -1);
320 }
321
322 return result;
323 }
324 /**
325 * Replace matching shortcodes in a block of text.
326 *
327 * @param {string} tag Shortcode tag.
328 * @param {string} text Text to search.
329 * @param {Function} callback Function to process the match and return
330 * replacement string.
331 *
332 * @return {string} Text with shortcodes replaced.
333 */
334
335 function replace(tag, text, callback) {
336 return text.replace(regexp(tag), function (match, left, $3, attrs, slash, content, closing, right) {
337 // If both extra brackets exist, the shortcode has been properly
338 // escaped.
339 if (left === '[' && right === ']') {
340 return match;
341 } // Create the match object and pass it through the callback.
342
343
344 const result = callback(fromMatch(arguments)); // Make sure to return any of the extra brackets if they weren't used to
345 // escape the shortcode.
346
347 return result || result === '' ? left + result + right : match;
348 });
349 }
350 /**
351 * Generate a string from shortcode parameters.
352 *
353 * Creates a shortcode instance and returns a string.
354 *
355 * Accepts the same `options` as the `shortcode()` constructor, containing a
356 * `tag` string, a string or object of `attrs`, a boolean indicating whether to
357 * format the shortcode using a `single` tag, and a `content` string.
358 *
359 * @param {Object} options
360 *
361 * @return {string} String representation of the shortcode.
362 */
363
364 function string(options) {
365 return new shortcode(options).string();
366 }
367 /**
368 * Generate a RegExp to identify a shortcode.
369 *
370 * The base regex is functionally equivalent to the one found in
371 * `get_shortcode_regex()` in `wp-includes/shortcodes.php`.
372 *
373 * Capture groups:
374 *
375 * 1. An extra `[` to allow for escaping shortcodes with double `[[]]`
376 * 2. The shortcode name
377 * 3. The shortcode argument list
378 * 4. The self closing `/`
379 * 5. The content of a shortcode when it wraps some content.
380 * 6. The closing tag.
381 * 7. An extra `]` to allow for escaping shortcodes with double `[[]]`
382 *
383 * @param {string} tag Shortcode tag.
384 *
385 * @return {RegExp} Shortcode RegExp.
386 */
387
388 function regexp(tag) {
389 return new RegExp('\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g');
390 }
391 /**
392 * Parse shortcode attributes.
393 *
394 * Shortcodes accept many types of attributes. These can chiefly be divided into
395 * named and numeric attributes:
396 *
397 * Named attributes are assigned on a key/value basis, while numeric attributes
398 * are treated as an array.
399 *
400 * Named attributes can be formatted as either `name="value"`, `name='value'`,
401 * or `name=value`. Numeric attributes can be formatted as `"value"` or just
402 * `value`.
403 *
404 * @param {string} text Serialised shortcode attributes.
405 *
406 * @return {WPShortcodeAttrs} Parsed shortcode attributes.
407 */
408
409 const attrs = memize_default()(text => {
410 const named = {};
411 const numeric = []; // This regular expression is reused from `shortcode_parse_atts()` in
412 // `wp-includes/shortcodes.php`.
413 //
414 // Capture groups:
415 //
416 // 1. An attribute name, that corresponds to...
417 // 2. a value in double quotes.
418 // 3. An attribute name, that corresponds to...
419 // 4. a value in single quotes.
420 // 5. An attribute name, that corresponds to...
421 // 6. an unquoted value.
422 // 7. A numeric attribute in double quotes.
423 // 8. A numeric attribute in single quotes.
424 // 9. An unquoted numeric attribute.
425
426 const pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|'([^']*)'(?:\s|$)|(\S+)(?:\s|$)/g; // Map zero-width spaces to actual spaces.
427
428 text = text.replace(/[\u00a0\u200b]/g, ' ');
429 let match; // Match and normalize attributes.
430
431 while (match = pattern.exec(text)) {
432 if (match[1]) {
433 named[match[1].toLowerCase()] = match[2];
434 } else if (match[3]) {
435 named[match[3].toLowerCase()] = match[4];
436 } else if (match[5]) {
437 named[match[5].toLowerCase()] = match[6];
438 } else if (match[7]) {
439 numeric.push(match[7]);
440 } else if (match[8]) {
441 numeric.push(match[8]);
442 } else if (match[9]) {
443 numeric.push(match[9]);
444 }
445 }
446
447 return {
448 named,
449 numeric
450 };
451 });
452 /**
453 * Generate a Shortcode Object from a RegExp match.
454 *
455 * Accepts a `match` object from calling `regexp.exec()` on a `RegExp` generated
456 * by `regexp()`. `match` can also be set to the `arguments` from a callback
457 * passed to `regexp.replace()`.
458 *
459 * @param {Array} match Match array.
460 *
461 * @return {WPShortcode} Shortcode instance.
462 */
463
464 function fromMatch(match) {
465 let type;
466
467 if (match[4]) {
468 type = 'self-closing';
469 } else if (match[6]) {
470 type = 'closed';
471 } else {
472 type = 'single';
473 }
474
475 return new shortcode({
476 tag: match[2],
477 attrs: match[3],
478 type,
479 content: match[5]
480 });
481 }
482 /**
483 * Creates a shortcode instance.
484 *
485 * To access a raw representation of a shortcode, pass an `options` object,
486 * containing a `tag` string, a string or object of `attrs`, a string indicating
487 * the `type` of the shortcode ('single', 'self-closing', or 'closed'), and a
488 * `content` string.
489 *
490 * @param {Object} options Options as described.
491 *
492 * @return {WPShortcode} Shortcode instance.
493 */
494
495 const shortcode = (0,external_lodash_namespaceObject.extend)(function (options) {
496 (0,external_lodash_namespaceObject.extend)(this, (0,external_lodash_namespaceObject.pick)(options || {}, 'tag', 'attrs', 'type', 'content'));
497 const attributes = this.attrs; // Ensure we have a correctly formatted `attrs` object.
498
499 this.attrs = {
500 named: {},
501 numeric: []
502 };
503
504 if (!attributes) {
505 return;
506 } // Parse a string of attributes.
507
508
509 if ((0,external_lodash_namespaceObject.isString)(attributes)) {
510 this.attrs = attrs(attributes); // Identify a correctly formatted `attrs` object.
511 } else if ((0,external_lodash_namespaceObject.isEqual)(Object.keys(attributes), ['named', 'numeric'])) {
512 this.attrs = attributes; // Handle a flat object of attributes.
513 } else {
514 (0,external_lodash_namespaceObject.forEach)(attributes, (value, key) => {
515 this.set(key, value);
516 });
517 }
518 }, {
519 next,
520 replace,
521 string,
522 regexp,
523 attrs,
524 fromMatch
525 });
526 (0,external_lodash_namespaceObject.extend)(shortcode.prototype, {
527 /**
528 * Get a shortcode attribute.
529 *
530 * Automatically detects whether `attr` is named or numeric and routes it
531 * accordingly.
532 *
533 * @param {(number|string)} attr Attribute key.
534 *
535 * @return {string} Attribute value.
536 */
537 get(attr) {
538 return this.attrs[(0,external_lodash_namespaceObject.isNumber)(attr) ? 'numeric' : 'named'][attr];
539 },
540
541 /**
542 * Set a shortcode attribute.
543 *
544 * Automatically detects whether `attr` is named or numeric and routes it
545 * accordingly.
546 *
547 * @param {(number|string)} attr Attribute key.
548 * @param {string} value Attribute value.
549 *
550 * @return {WPShortcode} Shortcode instance.
551 */
552 set(attr, value) {
553 this.attrs[(0,external_lodash_namespaceObject.isNumber)(attr) ? 'numeric' : 'named'][attr] = value;
554 return this;
555 },
556
557 /**
558 * Transform the shortcode into a string.
559 *
560 * @return {string} String representation of the shortcode.
561 */
562 string() {
563 let text = '[' + this.tag;
564 (0,external_lodash_namespaceObject.forEach)(this.attrs.numeric, value => {
565 if (/\s/.test(value)) {
566 text += ' "' + value + '"';
567 } else {
568 text += ' ' + value;
569 }
570 });
571 (0,external_lodash_namespaceObject.forEach)(this.attrs.named, (value, name) => {
572 text += ' ' + name + '="' + value + '"';
573 }); // If the tag is marked as `single` or `self-closing`, close the tag and
574 // ignore any additional content.
575
576 if ('single' === this.type) {
577 return text + ']';
578 } else if ('self-closing' === this.type) {
579 return text + ' /]';
580 } // Complete the opening tag.
581
582
583 text += ']';
584
585 if (this.content) {
586 text += this.content;
587 } // Add the closing tag.
588
589
590 return text + '[/' + this.tag + ']';
591 }
592
593 });
594 /* harmony default export */ var build_module = (shortcode);
595 //# sourceMappingURL=index.js.map
596 }();
597 (window.wp = window.wp || {}).shortcode = __webpack_exports__.default;
598 /******/ })()
599 ;