[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   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  /************************************************************************/
  25  var __webpack_exports__ = {};
  26  
  27  // EXPORTS
  28  __webpack_require__.d(__webpack_exports__, {
  29    "default": () => (/* binding */ index_default)
  30  });
  31  
  32  // UNUSED EXPORTS: attrs, fromMatch, next, regexp, replace, string
  33  
  34  ;// ./node_modules/memize/dist/index.js
  35  /**
  36   * Memize options object.
  37   *
  38   * @typedef MemizeOptions
  39   *
  40   * @property {number} [maxSize] Maximum size of the cache.
  41   */
  42  
  43  /**
  44   * Internal cache entry.
  45   *
  46   * @typedef MemizeCacheNode
  47   *
  48   * @property {?MemizeCacheNode|undefined} [prev] Previous node.
  49   * @property {?MemizeCacheNode|undefined} [next] Next node.
  50   * @property {Array<*>}                   args   Function arguments for cache
  51   *                                               entry.
  52   * @property {*}                          val    Function result.
  53   */
  54  
  55  /**
  56   * Properties of the enhanced function for controlling cache.
  57   *
  58   * @typedef MemizeMemoizedFunction
  59   *
  60   * @property {()=>void} clear Clear the cache.
  61   */
  62  
  63  /**
  64   * Accepts a function to be memoized, and returns a new memoized function, with
  65   * optional options.
  66   *
  67   * @template {(...args: any[]) => any} F
  68   *
  69   * @param {F}             fn        Function to memoize.
  70   * @param {MemizeOptions} [options] Options object.
  71   *
  72   * @return {((...args: Parameters<F>) => ReturnType<F>) & MemizeMemoizedFunction} Memoized function.
  73   */
  74  function memize(fn, options) {
  75      var size = 0;
  76  
  77      /** @type {?MemizeCacheNode|undefined} */
  78      var head;
  79  
  80      /** @type {?MemizeCacheNode|undefined} */
  81      var tail;
  82  
  83      options = options || {};
  84  
  85  	function memoized(/* ...args */) {
  86          var node = head,
  87              len = arguments.length,
  88              args,
  89              i;
  90  
  91          searchCache: while (node) {
  92              // Perform a shallow equality test to confirm that whether the node
  93              // under test is a candidate for the arguments passed. Two arrays
  94              // are shallowly equal if their length matches and each entry is
  95              // strictly equal between the two sets. Avoid abstracting to a
  96              // function which could incur an arguments leaking deoptimization.
  97  
  98              // Check whether node arguments match arguments length
  99              if (node.args.length !== arguments.length) {
 100                  node = node.next;
 101                  continue;
 102              }
 103  
 104              // Check whether node arguments match arguments values
 105              for (i = 0; i < len; i++) {
 106                  if (node.args[i] !== arguments[i]) {
 107                      node = node.next;
 108                      continue searchCache;
 109                  }
 110              }
 111  
 112              // At this point we can assume we've found a match
 113  
 114              // Surface matched node to head if not already
 115              if (node !== head) {
 116                  // As tail, shift to previous. Must only shift if not also
 117                  // head, since if both head and tail, there is no previous.
 118                  if (node === tail) {
 119                      tail = node.prev;
 120                  }
 121  
 122                  // Adjust siblings to point to each other. If node was tail,
 123                  // this also handles new tail's empty `next` assignment.
 124                  /** @type {MemizeCacheNode} */ (node.prev).next = node.next;
 125                  if (node.next) {
 126                      node.next.prev = node.prev;
 127                  }
 128  
 129                  node.next = head;
 130                  node.prev = null;
 131                  /** @type {MemizeCacheNode} */ (head).prev = node;
 132                  head = node;
 133              }
 134  
 135              // Return immediately
 136              return node.val;
 137          }
 138  
 139          // No cached value found. Continue to insertion phase:
 140  
 141          // Create a copy of arguments (avoid leaking deoptimization)
 142          args = new Array(len);
 143          for (i = 0; i < len; i++) {
 144              args[i] = arguments[i];
 145          }
 146  
 147          node = {
 148              args: args,
 149  
 150              // Generate the result from original function
 151              val: fn.apply(null, args),
 152          };
 153  
 154          // Don't need to check whether node is already head, since it would
 155          // have been returned above already if it was
 156  
 157          // Shift existing head down list
 158          if (head) {
 159              head.prev = node;
 160              node.next = head;
 161          } else {
 162              // If no head, follows that there's no tail (at initial or reset)
 163              tail = node;
 164          }
 165  
 166          // Trim tail if we're reached max size and are pending cache insertion
 167          if (size === /** @type {MemizeOptions} */ (options).maxSize) {
 168              tail = /** @type {MemizeCacheNode} */ (tail).prev;
 169              /** @type {MemizeCacheNode} */ (tail).next = null;
 170          } else {
 171              size++;
 172          }
 173  
 174          head = node;
 175  
 176          return node.val;
 177      }
 178  
 179      memoized.clear = function () {
 180          head = null;
 181          tail = null;
 182          size = 0;
 183      };
 184  
 185      // Ignore reason: There's not a clear solution to create an intersection of
 186      // the function with additional properties, where the goal is to retain the
 187      // function signature of the incoming argument and add control properties
 188      // on the return value.
 189  
 190      // @ts-ignore
 191      return memoized;
 192  }
 193  
 194  
 195  
 196  ;// ./node_modules/@wordpress/shortcode/build-module/index.js
 197  
 198  
 199  function next(tag, text, index = 0) {
 200    const re = regexp(tag);
 201    re.lastIndex = index;
 202    const match = re.exec(text);
 203    if (!match) {
 204      return;
 205    }
 206    if ("[" === match[1] && "]" === match[7]) {
 207      return next(tag, text, re.lastIndex);
 208    }
 209    const result = {
 210      index: match.index,
 211      content: match[0],
 212      shortcode: fromMatch(match)
 213    };
 214    if (match[1]) {
 215      result.content = result.content.slice(1);
 216      result.index++;
 217    }
 218    if (match[7]) {
 219      result.content = result.content.slice(0, -1);
 220    }
 221    return result;
 222  }
 223  function replace(tag, text, callback) {
 224    return text.replace(
 225      regexp(tag),
 226      function(match, left, $3, attrs2, slash, content, closing, right) {
 227        if (left === "[" && right === "]") {
 228          return match;
 229        }
 230        const result = callback(fromMatch(arguments));
 231        return result || result === "" ? left + result + right : match;
 232      }
 233    );
 234  }
 235  function string(options) {
 236    return new shortcode(options).string();
 237  }
 238  function regexp(tag) {
 239    return new RegExp(
 240      "\\[(\\[?)(" + tag + ")(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)",
 241      "g"
 242    );
 243  }
 244  const attrs = memize((text) => {
 245    const named = {};
 246    const numeric = [];
 247    const pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|'([^']*)'(?:\s|$)|(\S+)(?:\s|$)/g;
 248    text = text.replace(/[\u00a0\u200b]/g, " ");
 249    let match;
 250    while (match = pattern.exec(text)) {
 251      if (match[1]) {
 252        named[match[1].toLowerCase()] = match[2];
 253      } else if (match[3]) {
 254        named[match[3].toLowerCase()] = match[4];
 255      } else if (match[5]) {
 256        named[match[5].toLowerCase()] = match[6];
 257      } else if (match[7]) {
 258        numeric.push(match[7]);
 259      } else if (match[8]) {
 260        numeric.push(match[8]);
 261      } else if (match[9]) {
 262        numeric.push(match[9]);
 263      }
 264    }
 265    return { named, numeric };
 266  });
 267  function fromMatch(match) {
 268    let type;
 269    if (match[4]) {
 270      type = "self-closing";
 271    } else if (match[6]) {
 272      type = "closed";
 273    } else {
 274      type = "single";
 275    }
 276    return new shortcode({
 277      tag: match[2],
 278      attrs: match[3],
 279      type,
 280      content: match[5]
 281    });
 282  }
 283  const shortcode = Object.assign(
 284    function(options) {
 285      const { tag, attrs: attributes, type, content } = options || {};
 286      Object.assign(this, { tag, type, content });
 287      this.attrs = {
 288        named: {},
 289        numeric: []
 290      };
 291      if (!attributes) {
 292        return;
 293      }
 294      const attributeTypes = ["named", "numeric"];
 295      if (typeof attributes === "string") {
 296        this.attrs = attrs(attributes);
 297      } else if (attributes.length === attributeTypes.length && attributeTypes.every((t, key) => t === attributes[key])) {
 298        this.attrs = attributes;
 299      } else {
 300        Object.entries(attributes).forEach(([key, value]) => {
 301          this.set(key, value);
 302        });
 303      }
 304    },
 305    {
 306      next,
 307      replace,
 308      string,
 309      regexp,
 310      attrs,
 311      fromMatch
 312    }
 313  );
 314  Object.assign(shortcode.prototype, {
 315    /**
 316     * Get a shortcode attribute.
 317     *
 318     * Automatically detects whether `attr` is named or numeric and routes it
 319     * accordingly.
 320     *
 321     * @param {(number|string)} attr Attribute key.
 322     *
 323     * @return {string} Attribute value.
 324     */
 325    get(attr) {
 326      return this.attrs[typeof attr === "number" ? "numeric" : "named"][attr];
 327    },
 328    /**
 329     * Set a shortcode attribute.
 330     *
 331     * Automatically detects whether `attr` is named or numeric and routes it
 332     * accordingly.
 333     *
 334     * @param {(number|string)} attr  Attribute key.
 335     * @param {string}          value Attribute value.
 336     *
 337     * @return {InstanceType< import('./types').shortcode >} Shortcode instance.
 338     */
 339    set(attr, value) {
 340      this.attrs[typeof attr === "number" ? "numeric" : "named"][attr] = value;
 341      return this;
 342    },
 343    /**
 344     * Transform the shortcode into a string.
 345     *
 346     * @return {string} String representation of the shortcode.
 347     */
 348    string() {
 349      let text = "[" + this.tag;
 350      this.attrs.numeric.forEach((value) => {
 351        if (/\s/.test(value)) {
 352          text += ' "' + value + '"';
 353        } else {
 354          text += " " + value;
 355        }
 356      });
 357      Object.entries(this.attrs.named).forEach(([name, value]) => {
 358        text += " " + name + '="' + value + '"';
 359      });
 360      if ("single" === this.type) {
 361        return text + "]";
 362      } else if ("self-closing" === this.type) {
 363        return text + " /]";
 364      }
 365      text += "]";
 366      if (this.content) {
 367        text += this.content;
 368      }
 369      return text + "[/" + this.tag + "]";
 370    }
 371  });
 372  var index_default = shortcode;
 373  
 374  
 375  (window.wp = window.wp || {}).shortcode = __webpack_exports__["default"];
 376  /******/ })()
 377  ;


Generated : Thu Oct 30 08:20:06 2025 Cross-referenced by PHPXref