PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 12.1.0, at build/shortcode/index.js

600 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) {
292 let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
293 const re = regexp(tag);
294 re.lastIndex = index;
295 const match = re.exec(text);
296
297 if (!match) {
298 return;
299 } // If we matched an escaped shortcode, try again.
300
301
302 if ('[' === match[1] && ']' === match[7]) {
303 return next(tag, text, re.lastIndex);
304 }
305
306 const result = {
307 index: match.index,
308 content: match[0],
309 shortcode: fromMatch(match)
310 }; // If we matched a leading `[`, strip it from the match and increment the
311 // index accordingly.
312
313 if (match[1]) {
314 result.content = result.content.slice(1);
315 result.index++;
316 } // If we matched a trailing `]`, strip it from the match.
317
318
319 if (match[7]) {
320 result.content = result.content.slice(0, -1);
321 }
322
323 return result;
324 }
325 /**
326 * Replace matching shortcodes in a block of text.
327 *
328 * @param {string} tag Shortcode tag.
329 * @param {string} text Text to search.
330 * @param {Function} callback Function to process the match and return
331 * replacement string.
332 *
333 * @return {string} Text with shortcodes replaced.
334 */
335
336 function replace(tag, text, callback) {
337 return text.replace(regexp(tag), function (match, left, $3, attrs, slash, content, closing, right) {
338 // If both extra brackets exist, the shortcode has been properly
339 // escaped.
340 if (left === '[' && right === ']') {
341 return match;
342 } // Create the match object and pass it through the callback.
343
344
345 const result = callback(fromMatch(arguments)); // Make sure to return any of the extra brackets if they weren't used to
346 // escape the shortcode.
347
348 return result || result === '' ? left + result + right : match;
349 });
350 }
351 /**
352 * Generate a string from shortcode parameters.
353 *
354 * Creates a shortcode instance and returns a string.
355 *
356 * Accepts the same `options` as the `shortcode()` constructor, containing a
357 * `tag` string, a string or object of `attrs`, a boolean indicating whether to
358 * format the shortcode using a `single` tag, and a `content` string.
359 *
360 * @param {Object} options
361 *
362 * @return {string} String representation of the shortcode.
363 */
364
365 function string(options) {
366 return new shortcode(options).string();
367 }
368 /**
369 * Generate a RegExp to identify a shortcode.
370 *
371 * The base regex is functionally equivalent to the one found in
372 * `get_shortcode_regex()` in `wp-includes/shortcodes.php`.
373 *
374 * Capture groups:
375 *
376 * 1. An extra `[` to allow for escaping shortcodes with double `[[]]`
377 * 2. The shortcode name
378 * 3. The shortcode argument list
379 * 4. The self closing `/`
380 * 5. The content of a shortcode when it wraps some content.
381 * 6. The closing tag.
382 * 7. An extra `]` to allow for escaping shortcodes with double `[[]]`
383 *
384 * @param {string} tag Shortcode tag.
385 *
386 * @return {RegExp} Shortcode RegExp.
387 */
388
389 function regexp(tag) {
390 return new RegExp('\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g');
391 }
392 /**
393 * Parse shortcode attributes.
394 *
395 * Shortcodes accept many types of attributes. These can chiefly be divided into
396 * named and numeric attributes:
397 *
398 * Named attributes are assigned on a key/value basis, while numeric attributes
399 * are treated as an array.
400 *
401 * Named attributes can be formatted as either `name="value"`, `name='value'`,
402 * or `name=value`. Numeric attributes can be formatted as `"value"` or just
403 * `value`.
404 *
405 * @param {string} text Serialised shortcode attributes.
406 *
407 * @return {WPShortcodeAttrs} Parsed shortcode attributes.
408 */
409
410 const attrs = memize_default()(text => {
411 const named = {};
412 const numeric = []; // This regular expression is reused from `shortcode_parse_atts()` in
413 // `wp-includes/shortcodes.php`.
414 //
415 // Capture groups:
416 //
417 // 1. An attribute name, that corresponds to...
418 // 2. a value in double quotes.
419 // 3. An attribute name, that corresponds to...
420 // 4. a value in single quotes.
421 // 5. An attribute name, that corresponds to...
422 // 6. an unquoted value.
423 // 7. A numeric attribute in double quotes.
424 // 8. A numeric attribute in single quotes.
425 // 9. An unquoted numeric attribute.
426
427 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.
428
429 text = text.replace(/[\u00a0\u200b]/g, ' ');
430 let match; // Match and normalize attributes.
431
432 while (match = pattern.exec(text)) {
433 if (match[1]) {
434 named[match[1].toLowerCase()] = match[2];
435 } else if (match[3]) {
436 named[match[3].toLowerCase()] = match[4];
437 } else if (match[5]) {
438 named[match[5].toLowerCase()] = match[6];
439 } else if (match[7]) {
440 numeric.push(match[7]);
441 } else if (match[8]) {
442 numeric.push(match[8]);
443 } else if (match[9]) {
444 numeric.push(match[9]);
445 }
446 }
447
448 return {
449 named,
450 numeric
451 };
452 });
453 /**
454 * Generate a Shortcode Object from a RegExp match.
455 *
456 * Accepts a `match` object from calling `regexp.exec()` on a `RegExp` generated
457 * by `regexp()`. `match` can also be set to the `arguments` from a callback
458 * passed to `regexp.replace()`.
459 *
460 * @param {Array} match Match array.
461 *
462 * @return {WPShortcode} Shortcode instance.
463 */
464
465 function fromMatch(match) {
466 let type;
467
468 if (match[4]) {
469 type = 'self-closing';
470 } else if (match[6]) {
471 type = 'closed';
472 } else {
473 type = 'single';
474 }
475
476 return new shortcode({
477 tag: match[2],
478 attrs: match[3],
479 type,
480 content: match[5]
481 });
482 }
483 /**
484 * Creates a shortcode instance.
485 *
486 * To access a raw representation of a shortcode, pass an `options` object,
487 * containing a `tag` string, a string or object of `attrs`, a string indicating
488 * the `type` of the shortcode ('single', 'self-closing', or 'closed'), and a
489 * `content` string.
490 *
491 * @param {Object} options Options as described.
492 *
493 * @return {WPShortcode} Shortcode instance.
494 */
495
496 const shortcode = (0,external_lodash_namespaceObject.extend)(function (options) {
497 (0,external_lodash_namespaceObject.extend)(this, (0,external_lodash_namespaceObject.pick)(options || {}, 'tag', 'attrs', 'type', 'content'));
498 const attributes = this.attrs; // Ensure we have a correctly formatted `attrs` object.
499
500 this.attrs = {
501 named: {},
502 numeric: []
503 };
504
505 if (!attributes) {
506 return;
507 } // Parse a string of attributes.
508
509
510 if ((0,external_lodash_namespaceObject.isString)(attributes)) {
511 this.attrs = attrs(attributes); // Identify a correctly formatted `attrs` object.
512 } else if ((0,external_lodash_namespaceObject.isEqual)(Object.keys(attributes), ['named', 'numeric'])) {
513 this.attrs = attributes; // Handle a flat object of attributes.
514 } else {
515 (0,external_lodash_namespaceObject.forEach)(attributes, (value, key) => {
516 this.set(key, value);
517 });
518 }
519 }, {
520 next,
521 replace,
522 string,
523 regexp,
524 attrs,
525 fromMatch
526 });
527 (0,external_lodash_namespaceObject.extend)(shortcode.prototype, {
528 /**
529 * Get a shortcode attribute.
530 *
531 * Automatically detects whether `attr` is named or numeric and routes it
532 * accordingly.
533 *
534 * @param {(number|string)} attr Attribute key.
535 *
536 * @return {string} Attribute value.
537 */
538 get(attr) {
539 return this.attrs[(0,external_lodash_namespaceObject.isNumber)(attr) ? 'numeric' : 'named'][attr];
540 },
541
542 /**
543 * Set a shortcode attribute.
544 *
545 * Automatically detects whether `attr` is named or numeric and routes it
546 * accordingly.
547 *
548 * @param {(number|string)} attr Attribute key.
549 * @param {string} value Attribute value.
550 *
551 * @return {WPShortcode} Shortcode instance.
552 */
553 set(attr, value) {
554 this.attrs[(0,external_lodash_namespaceObject.isNumber)(attr) ? 'numeric' : 'named'][attr] = value;
555 return this;
556 },
557
558 /**
559 * Transform the shortcode into a string.
560 *
561 * @return {string} String representation of the shortcode.
562 */
563 string() {
564 let text = '[' + this.tag;
565 (0,external_lodash_namespaceObject.forEach)(this.attrs.numeric, value => {
566 if (/\s/.test(value)) {
567 text += ' "' + value + '"';
568 } else {
569 text += ' ' + value;
570 }
571 });
572 (0,external_lodash_namespaceObject.forEach)(this.attrs.named, (value, name) => {
573 text += ' ' + name + '="' + value + '"';
574 }); // If the tag is marked as `single` or `self-closing`, close the tag and
575 // ignore any additional content.
576
577 if ('single' === this.type) {
578 return text + ']';
579 } else if ('self-closing' === this.type) {
580 return text + ' /]';
581 } // Complete the opening tag.
582
583
584 text += ']';
585
586 if (this.content) {
587 text += this.content;
588 } // Add the closing tag.
589
590
591 return text + '[/' + this.tag + ']';
592 }
593
594 });
595 /* harmony default export */ var build_module = (shortcode);
596 //# sourceMappingURL=index.js.map
597 }();
598 (window.wp = window.wp || {}).shortcode = __webpack_exports__.default;
599 /******/ })()
600 ;