[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/dist/ -> shortcode.js (source)

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


Generated: Wed Dec 1 01:00:03 2021 Cross-referenced by PHPXref 0.7.1