shortcode.js 17 KB

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. /* unused harmony exports next, replace, string, regexp, attrs, fromMatch */
  200. /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9756);
  201. /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(memize__WEBPACK_IMPORTED_MODULE_0__);
  202. /**
  203. * External dependencies
  204. */
  205. /**
  206. * Shortcode attributes object.
  207. *
  208. * @typedef {Object} WPShortcodeAttrs
  209. *
  210. * @property {Object} named Object with named attributes.
  211. * @property {Array} numeric Array with numeric attributes.
  212. */
  213. /**
  214. * Shortcode object.
  215. *
  216. * @typedef {Object} WPShortcode
  217. *
  218. * @property {string} tag Shortcode tag.
  219. * @property {WPShortcodeAttrs} attrs Shortcode attributes.
  220. * @property {string} content Shortcode content.
  221. * @property {string} type Shortcode type: `self-closing`,
  222. * `closed`, or `single`.
  223. */
  224. /**
  225. * @typedef {Object} WPShortcodeMatch
  226. *
  227. * @property {number} index Index the shortcode is found at.
  228. * @property {string} content Matched content.
  229. * @property {WPShortcode} shortcode Shortcode instance of the match.
  230. */
  231. /**
  232. * Find the next matching shortcode.
  233. *
  234. * @param {string} tag Shortcode tag.
  235. * @param {string} text Text to search.
  236. * @param {number} index Index to start search from.
  237. *
  238. * @return {?WPShortcodeMatch} Matched information.
  239. */
  240. function next(tag, text) {
  241. let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  242. const re = regexp(tag);
  243. re.lastIndex = index;
  244. const match = re.exec(text);
  245. if (!match) {
  246. return;
  247. } // If we matched an escaped shortcode, try again.
  248. if ('[' === match[1] && ']' === match[7]) {
  249. return next(tag, text, re.lastIndex);
  250. }
  251. const result = {
  252. index: match.index,
  253. content: match[0],
  254. shortcode: fromMatch(match)
  255. }; // If we matched a leading `[`, strip it from the match and increment the
  256. // index accordingly.
  257. if (match[1]) {
  258. result.content = result.content.slice(1);
  259. result.index++;
  260. } // If we matched a trailing `]`, strip it from the match.
  261. if (match[7]) {
  262. result.content = result.content.slice(0, -1);
  263. }
  264. return result;
  265. }
  266. /**
  267. * Replace matching shortcodes in a block of text.
  268. *
  269. * @param {string} tag Shortcode tag.
  270. * @param {string} text Text to search.
  271. * @param {Function} callback Function to process the match and return
  272. * replacement string.
  273. *
  274. * @return {string} Text with shortcodes replaced.
  275. */
  276. function replace(tag, text, callback) {
  277. return text.replace(regexp(tag), function (match, left, $3, attrs, slash, content, closing, right) {
  278. // If both extra brackets exist, the shortcode has been properly
  279. // escaped.
  280. if (left === '[' && right === ']') {
  281. return match;
  282. } // Create the match object and pass it through the callback.
  283. const result = callback(fromMatch(arguments)); // Make sure to return any of the extra brackets if they weren't used to
  284. // escape the shortcode.
  285. return result || result === '' ? left + result + right : match;
  286. });
  287. }
  288. /**
  289. * Generate a string from shortcode parameters.
  290. *
  291. * Creates a shortcode instance and returns a string.
  292. *
  293. * Accepts the same `options` as the `shortcode()` constructor, containing a
  294. * `tag` string, a string or object of `attrs`, a boolean indicating whether to
  295. * format the shortcode using a `single` tag, and a `content` string.
  296. *
  297. * @param {Object} options
  298. *
  299. * @return {string} String representation of the shortcode.
  300. */
  301. function string(options) {
  302. return new shortcode(options).string();
  303. }
  304. /**
  305. * Generate a RegExp to identify a shortcode.
  306. *
  307. * The base regex is functionally equivalent to the one found in
  308. * `get_shortcode_regex()` in `wp-includes/shortcodes.php`.
  309. *
  310. * Capture groups:
  311. *
  312. * 1. An extra `[` to allow for escaping shortcodes with double `[[]]`
  313. * 2. The shortcode name
  314. * 3. The shortcode argument list
  315. * 4. The self closing `/`
  316. * 5. The content of a shortcode when it wraps some content.
  317. * 6. The closing tag.
  318. * 7. An extra `]` to allow for escaping shortcodes with double `[[]]`
  319. *
  320. * @param {string} tag Shortcode tag.
  321. *
  322. * @return {RegExp} Shortcode RegExp.
  323. */
  324. function regexp(tag) {
  325. return new RegExp('\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g');
  326. }
  327. /**
  328. * Parse shortcode attributes.
  329. *
  330. * Shortcodes accept many types of attributes. These can chiefly be divided into
  331. * named and numeric attributes:
  332. *
  333. * Named attributes are assigned on a key/value basis, while numeric attributes
  334. * are treated as an array.
  335. *
  336. * Named attributes can be formatted as either `name="value"`, `name='value'`,
  337. * or `name=value`. Numeric attributes can be formatted as `"value"` or just
  338. * `value`.
  339. *
  340. * @param {string} text Serialised shortcode attributes.
  341. *
  342. * @return {WPShortcodeAttrs} Parsed shortcode attributes.
  343. */
  344. const attrs = memize__WEBPACK_IMPORTED_MODULE_0___default()(text => {
  345. const named = {};
  346. const numeric = []; // This regular expression is reused from `shortcode_parse_atts()` in
  347. // `wp-includes/shortcodes.php`.
  348. //
  349. // Capture groups:
  350. //
  351. // 1. An attribute name, that corresponds to...
  352. // 2. a value in double quotes.
  353. // 3. An attribute name, that corresponds to...
  354. // 4. a value in single quotes.
  355. // 5. An attribute name, that corresponds to...
  356. // 6. an unquoted value.
  357. // 7. A numeric attribute in double quotes.
  358. // 8. A numeric attribute in single quotes.
  359. // 9. An unquoted numeric attribute.
  360. 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.
  361. text = text.replace(/[\u00a0\u200b]/g, ' ');
  362. let match; // Match and normalize attributes.
  363. while (match = pattern.exec(text)) {
  364. if (match[1]) {
  365. named[match[1].toLowerCase()] = match[2];
  366. } else if (match[3]) {
  367. named[match[3].toLowerCase()] = match[4];
  368. } else if (match[5]) {
  369. named[match[5].toLowerCase()] = match[6];
  370. } else if (match[7]) {
  371. numeric.push(match[7]);
  372. } else if (match[8]) {
  373. numeric.push(match[8]);
  374. } else if (match[9]) {
  375. numeric.push(match[9]);
  376. }
  377. }
  378. return {
  379. named,
  380. numeric
  381. };
  382. });
  383. /**
  384. * Generate a Shortcode Object from a RegExp match.
  385. *
  386. * Accepts a `match` object from calling `regexp.exec()` on a `RegExp` generated
  387. * by `regexp()`. `match` can also be set to the `arguments` from a callback
  388. * passed to `regexp.replace()`.
  389. *
  390. * @param {Array} match Match array.
  391. *
  392. * @return {WPShortcode} Shortcode instance.
  393. */
  394. function fromMatch(match) {
  395. let type;
  396. if (match[4]) {
  397. type = 'self-closing';
  398. } else if (match[6]) {
  399. type = 'closed';
  400. } else {
  401. type = 'single';
  402. }
  403. return new shortcode({
  404. tag: match[2],
  405. attrs: match[3],
  406. type,
  407. content: match[5]
  408. });
  409. }
  410. /**
  411. * Creates a shortcode instance.
  412. *
  413. * To access a raw representation of a shortcode, pass an `options` object,
  414. * containing a `tag` string, a string or object of `attrs`, a string indicating
  415. * the `type` of the shortcode ('single', 'self-closing', or 'closed'), and a
  416. * `content` string.
  417. *
  418. * @param {Object} options Options as described.
  419. *
  420. * @return {WPShortcode} Shortcode instance.
  421. */
  422. const shortcode = Object.assign(function (options) {
  423. const {
  424. tag,
  425. attrs: attributes,
  426. type,
  427. content
  428. } = options || {};
  429. Object.assign(this, {
  430. tag,
  431. type,
  432. content
  433. }); // Ensure we have a correctly formatted `attrs` object.
  434. this.attrs = {
  435. named: {},
  436. numeric: []
  437. };
  438. if (!attributes) {
  439. return;
  440. }
  441. const attributeTypes = ['named', 'numeric']; // Parse a string of attributes.
  442. if (typeof attributes === 'string') {
  443. this.attrs = attrs(attributes); // Identify a correctly formatted `attrs` object.
  444. } else if (attributes.length === attributeTypes.length && attributeTypes.every((t, key) => t === attributes[key])) {
  445. this.attrs = attributes; // Handle a flat object of attributes.
  446. } else {
  447. Object.entries(attributes).forEach(_ref => {
  448. let [key, value] = _ref;
  449. this.set(key, value);
  450. });
  451. }
  452. }, {
  453. next,
  454. replace,
  455. string,
  456. regexp,
  457. attrs,
  458. fromMatch
  459. });
  460. Object.assign(shortcode.prototype, {
  461. /**
  462. * Get a shortcode attribute.
  463. *
  464. * Automatically detects whether `attr` is named or numeric and routes it
  465. * accordingly.
  466. *
  467. * @param {(number|string)} attr Attribute key.
  468. *
  469. * @return {string} Attribute value.
  470. */
  471. get(attr) {
  472. return this.attrs[typeof attr === 'number' ? 'numeric' : 'named'][attr];
  473. },
  474. /**
  475. * Set a shortcode attribute.
  476. *
  477. * Automatically detects whether `attr` is named or numeric and routes it
  478. * accordingly.
  479. *
  480. * @param {(number|string)} attr Attribute key.
  481. * @param {string} value Attribute value.
  482. *
  483. * @return {WPShortcode} Shortcode instance.
  484. */
  485. set(attr, value) {
  486. this.attrs[typeof attr === 'number' ? 'numeric' : 'named'][attr] = value;
  487. return this;
  488. },
  489. /**
  490. * Transform the shortcode into a string.
  491. *
  492. * @return {string} String representation of the shortcode.
  493. */
  494. string() {
  495. let text = '[' + this.tag;
  496. this.attrs.numeric.forEach(value => {
  497. if (/\s/.test(value)) {
  498. text += ' "' + value + '"';
  499. } else {
  500. text += ' ' + value;
  501. }
  502. });
  503. Object.entries(this.attrs.named).forEach(_ref2 => {
  504. let [name, value] = _ref2;
  505. text += ' ' + name + '="' + value + '"';
  506. }); // If the tag is marked as `single` or `self-closing`, close the tag and
  507. // ignore any additional content.
  508. if ('single' === this.type) {
  509. return text + ']';
  510. } else if ('self-closing' === this.type) {
  511. return text + ' /]';
  512. } // Complete the opening tag.
  513. text += ']';
  514. if (this.content) {
  515. text += this.content;
  516. } // Add the closing tag.
  517. return text + '[/' + this.tag + ']';
  518. }
  519. });
  520. /* harmony default export */ __webpack_exports__["default"] = (shortcode);
  521. }();
  522. (window.wp = window.wp || {}).shortcode = __webpack_exports__["default"];
  523. /******/ })()
  524. ;