[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  "use strict";
   2  var wp;
   3  (wp ||= {}).shortcode = (() => {
   4    var __defProp = Object.defineProperty;
   5    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   6    var __getOwnPropNames = Object.getOwnPropertyNames;
   7    var __hasOwnProp = Object.prototype.hasOwnProperty;
   8    var __export = (target, all) => {
   9      for (var name in all)
  10        __defProp(target, name, { get: all[name], enumerable: true });
  11    };
  12    var __copyProps = (to, from, except, desc) => {
  13      if (from && typeof from === "object" || typeof from === "function") {
  14        for (let key of __getOwnPropNames(from))
  15          if (!__hasOwnProp.call(to, key) && key !== except)
  16            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17      }
  18      return to;
  19    };
  20    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  21  
  22    // packages/shortcode/build-module/index.js
  23    var index_exports = {};
  24    __export(index_exports, {
  25      attrs: () => attrs,
  26      default: () => index_default,
  27      fromMatch: () => fromMatch,
  28      next: () => next,
  29      regexp: () => regexp,
  30      replace: () => replace,
  31      string: () => string
  32    });
  33  
  34    // node_modules/memize/dist/index.js
  35    function memize(fn, options) {
  36      var size = 0;
  37      var head;
  38      var tail;
  39      options = options || {};
  40      function memoized() {
  41        var node = head, len = arguments.length, args, i;
  42        searchCache: while (node) {
  43          if (node.args.length !== arguments.length) {
  44            node = node.next;
  45            continue;
  46          }
  47          for (i = 0; i < len; i++) {
  48            if (node.args[i] !== arguments[i]) {
  49              node = node.next;
  50              continue searchCache;
  51            }
  52          }
  53          if (node !== head) {
  54            if (node === tail) {
  55              tail = node.prev;
  56            }
  57            node.prev.next = node.next;
  58            if (node.next) {
  59              node.next.prev = node.prev;
  60            }
  61            node.next = head;
  62            node.prev = null;
  63            head.prev = node;
  64            head = node;
  65          }
  66          return node.val;
  67        }
  68        args = new Array(len);
  69        for (i = 0; i < len; i++) {
  70          args[i] = arguments[i];
  71        }
  72        node = {
  73          args,
  74          // Generate the result from original function
  75          val: fn.apply(null, args)
  76        };
  77        if (head) {
  78          head.prev = node;
  79          node.next = head;
  80        } else {
  81          tail = node;
  82        }
  83        if (size === /** @type {MemizeOptions} */
  84        options.maxSize) {
  85          tail = /** @type {MemizeCacheNode} */
  86          tail.prev;
  87          tail.next = null;
  88        } else {
  89          size++;
  90        }
  91        head = node;
  92        return node.val;
  93      }
  94      memoized.clear = function() {
  95        head = null;
  96        tail = null;
  97        size = 0;
  98      };
  99      return memoized;
 100    }
 101  
 102    // packages/shortcode/build-module/index.js
 103    function next(tag, text, index = 0) {
 104      const re = regexp(tag);
 105      re.lastIndex = index;
 106      const match = re.exec(text);
 107      if (!match) {
 108        return;
 109      }
 110      if ("[" === match[1] && "]" === match[7]) {
 111        return next(tag, text, re.lastIndex);
 112      }
 113      const result = {
 114        index: match.index,
 115        content: match[0],
 116        shortcode: fromMatch(match)
 117      };
 118      if (match[1]) {
 119        result.content = result.content.slice(1);
 120        result.index++;
 121      }
 122      if (match[7]) {
 123        result.content = result.content.slice(0, -1);
 124      }
 125      return result;
 126    }
 127    function replace(tag, text, callback) {
 128      return text.replace(
 129        regexp(tag),
 130        function(match, left, $3, attrs2, slash, content, closing, right) {
 131          if (left === "[" && right === "]") {
 132            return match;
 133          }
 134          const result = callback(fromMatch(arguments));
 135          return result || result === "" ? left + result + right : match;
 136        }
 137      );
 138    }
 139    function string(options) {
 140      return new shortcode(options).string();
 141    }
 142    function regexp(tag) {
 143      return new RegExp(
 144        "\\[(\\[?)(" + tag + ")(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)",
 145        "g"
 146      );
 147    }
 148    var attrs = memize((text) => {
 149      const named = {};
 150      const numeric = [];
 151      const pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|'([^']*)'(?:\s|$)|(\S+)(?:\s|$)/g;
 152      text = text.replace(/[\u00a0\u200b]/g, " ");
 153      let match;
 154      while (match = pattern.exec(text)) {
 155        if (match[1]) {
 156          named[match[1].toLowerCase()] = match[2];
 157        } else if (match[3]) {
 158          named[match[3].toLowerCase()] = match[4];
 159        } else if (match[5]) {
 160          named[match[5].toLowerCase()] = match[6];
 161        } else if (match[7]) {
 162          numeric.push(match[7]);
 163        } else if (match[8]) {
 164          numeric.push(match[8]);
 165        } else if (match[9]) {
 166          numeric.push(match[9]);
 167        }
 168      }
 169      return { named, numeric };
 170    });
 171    function fromMatch(match) {
 172      let type;
 173      if (match[4]) {
 174        type = "self-closing";
 175      } else if (match[6]) {
 176        type = "closed";
 177      } else {
 178        type = "single";
 179      }
 180      return new shortcode({
 181        tag: match[2],
 182        attrs: match[3],
 183        type,
 184        content: match[5]
 185      });
 186    }
 187    var shortcode = Object.assign(
 188      function(options) {
 189        const { tag, attrs: attributes, type, content } = options || {};
 190        Object.assign(this, { tag, type, content });
 191        this.attrs = {
 192          named: {},
 193          numeric: []
 194        };
 195        if (!attributes) {
 196          return;
 197        }
 198        const attributeTypes = ["named", "numeric"];
 199        if (typeof attributes === "string") {
 200          this.attrs = attrs(attributes);
 201        } else if (attributes.length === attributeTypes.length && attributeTypes.every((t, key) => t === attributes[key])) {
 202          this.attrs = attributes;
 203        } else {
 204          Object.entries(attributes).forEach(([key, value]) => {
 205            this.set(key, value);
 206          });
 207        }
 208      },
 209      {
 210        next,
 211        replace,
 212        string,
 213        regexp,
 214        attrs,
 215        fromMatch
 216      }
 217    );
 218    Object.assign(shortcode.prototype, {
 219      /**
 220       * Get a shortcode attribute.
 221       *
 222       * Automatically detects whether `attr` is named or numeric and routes it
 223       * accordingly.
 224       *
 225       * @param {(number|string)} attr Attribute key.
 226       *
 227       * @return {string} Attribute value.
 228       */
 229      get(attr) {
 230        return this.attrs[typeof attr === "number" ? "numeric" : "named"][attr];
 231      },
 232      /**
 233       * Set a shortcode attribute.
 234       *
 235       * Automatically detects whether `attr` is named or numeric and routes it
 236       * accordingly.
 237       *
 238       * @param {(number|string)} attr  Attribute key.
 239       * @param {string}          value Attribute value.
 240       *
 241       * @return {InstanceType< import('./types').shortcode >} Shortcode instance.
 242       */
 243      set(attr, value) {
 244        this.attrs[typeof attr === "number" ? "numeric" : "named"][attr] = value;
 245        return this;
 246      },
 247      /**
 248       * Transform the shortcode into a string.
 249       *
 250       * @return {string} String representation of the shortcode.
 251       */
 252      string() {
 253        let text = "[" + this.tag;
 254        this.attrs.numeric.forEach((value) => {
 255          if (/\s/.test(value)) {
 256            text += ' "' + value + '"';
 257          } else {
 258            text += " " + value;
 259          }
 260        });
 261        Object.entries(this.attrs.named).forEach(([name, value]) => {
 262          text += " " + name + '="' + value + '"';
 263        });
 264        if ("single" === this.type) {
 265          return text + "]";
 266        } else if ("self-closing" === this.type) {
 267          return text + " /]";
 268        }
 269        text += "]";
 270        if (this.content) {
 271          text += this.content;
 272        }
 273        return text + "[/" + this.tag + "]";
 274      }
 275    });
 276    var index_default = shortcode;
 277    return __toCommonJS(index_exports);
 278  })();
 279  if (typeof wp.shortcode === 'object' && wp.shortcode.default) { wp.shortcode = wp.shortcode.default; }


Generated : Sun Apr 26 08:20:11 2026 Cross-referenced by PHPXref