Sin descripción

shortcode.js 19KB

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