shortcode.js 18 KB

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