Nenhuma Descrição

shortcode.js 17KB

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