wp/wp-includes/js/dist/shortcode.js
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    80 /******/ 	// __webpack_public_path__
    80 /******/ 	// __webpack_public_path__
    81 /******/ 	__webpack_require__.p = "";
    81 /******/ 	__webpack_require__.p = "";
    82 /******/
    82 /******/
    83 /******/
    83 /******/
    84 /******/ 	// Load entry module and return exports
    84 /******/ 	// Load entry module and return exports
    85 /******/ 	return __webpack_require__(__webpack_require__.s = 431);
    85 /******/ 	return __webpack_require__(__webpack_require__.s = "/2FX");
    86 /******/ })
    86 /******/ })
    87 /************************************************************************/
    87 /************************************************************************/
    88 /******/ ({
    88 /******/ ({
    89 
    89 
    90 /***/ 2:
    90 /***/ "/2FX":
    91 /***/ (function(module, exports) {
       
    92 
       
    93 (function() { module.exports = this["lodash"]; }());
       
    94 
       
    95 /***/ }),
       
    96 
       
    97 /***/ 431:
       
    98 /***/ (function(module, __webpack_exports__, __webpack_require__) {
    91 /***/ (function(module, __webpack_exports__, __webpack_require__) {
    99 
    92 
   100 "use strict";
    93 "use strict";
   101 __webpack_require__.r(__webpack_exports__);
    94 __webpack_require__.r(__webpack_exports__);
   102 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "next", function() { return next; });
    95 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "next", function() { return next; });
   103 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "replace", function() { return replace; });
    96 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "replace", function() { return replace; });
   104 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "string", function() { return string; });
    97 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "string", function() { return string; });
   105 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "regexp", function() { return regexp; });
    98 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "regexp", function() { return regexp; });
   106 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "attrs", function() { return attrs; });
    99 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "attrs", function() { return attrs; });
   107 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "fromMatch", function() { return fromMatch; });
   100 /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "fromMatch", function() { return fromMatch; });
   108 /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
   101 /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("YLtl");
   109 /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_0__);
   102 /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_0__);
   110 /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(60);
   103 /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("4eJC");
   111 /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(memize__WEBPACK_IMPORTED_MODULE_1__);
   104 /* harmony import */ var memize__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(memize__WEBPACK_IMPORTED_MODULE_1__);
   112 /**
   105 /**
   113  * External dependencies
   106  * External dependencies
   114  */
   107  */
   115 
   108 
   151  * @param {number} index Index to start search from.
   144  * @param {number} index Index to start search from.
   152  *
   145  *
   153  * @return {?WPShortcodeMatch} Matched information.
   146  * @return {?WPShortcodeMatch} Matched information.
   154  */
   147  */
   155 
   148 
   156 function next(tag, text) {
   149 function next(tag, text, index = 0) {
   157   var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
   150   const re = regexp(tag);
   158   var re = regexp(tag);
       
   159   re.lastIndex = index;
   151   re.lastIndex = index;
   160   var match = re.exec(text);
   152   const match = re.exec(text);
   161 
   153 
   162   if (!match) {
   154   if (!match) {
   163     return;
   155     return;
   164   } // If we matched an escaped shortcode, try again.
   156   } // If we matched an escaped shortcode, try again.
   165 
   157 
   166 
   158 
   167   if ('[' === match[1] && ']' === match[7]) {
   159   if ('[' === match[1] && ']' === match[7]) {
   168     return next(tag, text, re.lastIndex);
   160     return next(tag, text, re.lastIndex);
   169   }
   161   }
   170 
   162 
   171   var result = {
   163   const result = {
   172     index: match.index,
   164     index: match.index,
   173     content: match[0],
   165     content: match[0],
   174     shortcode: fromMatch(match)
   166     shortcode: fromMatch(match)
   175   }; // If we matched a leading `[`, strip it from the match and increment the
   167   }; // If we matched a leading `[`, strip it from the match and increment the
   176   // index accordingly.
   168   // index accordingly.
   205     if (left === '[' && right === ']') {
   197     if (left === '[' && right === ']') {
   206       return match;
   198       return match;
   207     } // Create the match object and pass it through the callback.
   199     } // Create the match object and pass it through the callback.
   208 
   200 
   209 
   201 
   210     var result = callback(fromMatch(arguments)); // Make sure to return any of the extra brackets if they weren't used to
   202     const result = callback(fromMatch(arguments)); // Make sure to return any of the extra brackets if they weren't used to
   211     // escape the shortcode.
   203     // escape the shortcode.
   212 
   204 
   213     return result ? left + result + right : match;
   205     return result || result === '' ? left + result + right : match;
   214   });
   206   });
   215 }
   207 }
   216 /**
   208 /**
   217  * Generate a string from shortcode parameters.
   209  * Generate a string from shortcode parameters.
   218  *
   210  *
   270  * @param {string} text Serialised shortcode attributes.
   262  * @param {string} text Serialised shortcode attributes.
   271  *
   263  *
   272  * @return {WPShortcodeAttrs} Parsed shortcode attributes.
   264  * @return {WPShortcodeAttrs} Parsed shortcode attributes.
   273  */
   265  */
   274 
   266 
   275 var attrs = memize__WEBPACK_IMPORTED_MODULE_1___default()(function (text) {
   267 const attrs = memize__WEBPACK_IMPORTED_MODULE_1___default()(text => {
   276   var named = {};
   268   const named = {};
   277   var numeric = []; // This regular expression is reused from `shortcode_parse_atts()` in
   269   const numeric = []; // This regular expression is reused from `shortcode_parse_atts()` in
   278   // `wp-includes/shortcodes.php`.
   270   // `wp-includes/shortcodes.php`.
   279   //
   271   //
   280   // Capture groups:
   272   // Capture groups:
   281   //
   273   //
   282   // 1. An attribute name, that corresponds to...
   274   // 1. An attribute name, that corresponds to...
   287   // 6. an unquoted value.
   279   // 6. an unquoted value.
   288   // 7. A numeric attribute in double quotes.
   280   // 7. A numeric attribute in double quotes.
   289   // 8. A numeric attribute in single quotes.
   281   // 8. A numeric attribute in single quotes.
   290   // 9. An unquoted numeric attribute.
   282   // 9. An unquoted numeric attribute.
   291 
   283 
   292   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.
   284   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.
   293 
   285 
   294   text = text.replace(/[\u00a0\u200b]/g, ' ');
   286   text = text.replace(/[\u00a0\u200b]/g, ' ');
   295   var match; // Match and normalize attributes.
   287   let match; // Match and normalize attributes.
   296 
   288 
   297   while (match = pattern.exec(text)) {
   289   while (match = pattern.exec(text)) {
   298     if (match[1]) {
   290     if (match[1]) {
   299       named[match[1].toLowerCase()] = match[2];
   291       named[match[1].toLowerCase()] = match[2];
   300     } else if (match[3]) {
   292     } else if (match[3]) {
   309       numeric.push(match[9]);
   301       numeric.push(match[9]);
   310     }
   302     }
   311   }
   303   }
   312 
   304 
   313   return {
   305   return {
   314     named: named,
   306     named,
   315     numeric: numeric
   307     numeric
   316   };
   308   };
   317 });
   309 });
   318 /**
   310 /**
   319  * Generate a Shortcode Object from a RegExp match.
   311  * Generate a Shortcode Object from a RegExp match.
   320  *
   312  *
   326  *
   318  *
   327  * @return {WPShortcode} Shortcode instance.
   319  * @return {WPShortcode} Shortcode instance.
   328  */
   320  */
   329 
   321 
   330 function fromMatch(match) {
   322 function fromMatch(match) {
   331   var type;
   323   let type;
   332 
   324 
   333   if (match[4]) {
   325   if (match[4]) {
   334     type = 'self-closing';
   326     type = 'self-closing';
   335   } else if (match[6]) {
   327   } else if (match[6]) {
   336     type = 'closed';
   328     type = 'closed';
   339   }
   331   }
   340 
   332 
   341   return new shortcode({
   333   return new shortcode({
   342     tag: match[2],
   334     tag: match[2],
   343     attrs: match[3],
   335     attrs: match[3],
   344     type: type,
   336     type,
   345     content: match[5]
   337     content: match[5]
   346   });
   338   });
   347 }
   339 }
   348 /**
   340 /**
   349  * Creates a shortcode instance.
   341  * Creates a shortcode instance.
   356  * @param {Object} options Options as described.
   348  * @param {Object} options Options as described.
   357  *
   349  *
   358  * @return {WPShortcode} Shortcode instance.
   350  * @return {WPShortcode} Shortcode instance.
   359  */
   351  */
   360 
   352 
   361 var shortcode = Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(function (options) {
   353 const shortcode = Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(function (options) {
   362   var _this = this;
       
   363 
       
   364   Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(this, Object(lodash__WEBPACK_IMPORTED_MODULE_0__["pick"])(options || {}, 'tag', 'attrs', 'type', 'content'));
   354   Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(this, Object(lodash__WEBPACK_IMPORTED_MODULE_0__["pick"])(options || {}, 'tag', 'attrs', 'type', 'content'));
   365   var attributes = this.attrs; // Ensure we have a correctly formatted `attrs` object.
   355   const attributes = this.attrs; // Ensure we have a correctly formatted `attrs` object.
   366 
   356 
   367   this.attrs = {
   357   this.attrs = {
   368     named: {},
   358     named: {},
   369     numeric: []
   359     numeric: []
   370   };
   360   };
   377   if (Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isString"])(attributes)) {
   367   if (Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isString"])(attributes)) {
   378     this.attrs = attrs(attributes); // Identify a correctly formatted `attrs` object.
   368     this.attrs = attrs(attributes); // Identify a correctly formatted `attrs` object.
   379   } else if (Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isEqual"])(Object.keys(attributes), ['named', 'numeric'])) {
   369   } else if (Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isEqual"])(Object.keys(attributes), ['named', 'numeric'])) {
   380     this.attrs = attributes; // Handle a flat object of attributes.
   370     this.attrs = attributes; // Handle a flat object of attributes.
   381   } else {
   371   } else {
   382     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(attributes, function (value, key) {
   372     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(attributes, (value, key) => {
   383       _this.set(key, value);
   373       this.set(key, value);
   384     });
   374     });
   385   }
   375   }
   386 }, {
   376 }, {
   387   next: next,
   377   next,
   388   replace: replace,
   378   replace,
   389   string: string,
   379   string,
   390   regexp: regexp,
   380   regexp,
   391   attrs: attrs,
   381   attrs,
   392   fromMatch: fromMatch
   382   fromMatch
   393 });
   383 });
   394 Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(shortcode.prototype, {
   384 Object(lodash__WEBPACK_IMPORTED_MODULE_0__["extend"])(shortcode.prototype, {
   395   /**
   385   /**
   396    * Get a shortcode attribute.
   386    * Get a shortcode attribute.
   397    *
   387    *
   400    *
   390    *
   401    * @param {(number|string)} attr Attribute key.
   391    * @param {(number|string)} attr Attribute key.
   402    *
   392    *
   403    * @return {string} Attribute value.
   393    * @return {string} Attribute value.
   404    */
   394    */
   405   get: function get(attr) {
   395   get(attr) {
   406     return this.attrs[Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isNumber"])(attr) ? 'numeric' : 'named'][attr];
   396     return this.attrs[Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isNumber"])(attr) ? 'numeric' : 'named'][attr];
   407   },
   397   },
   408 
   398 
   409   /**
   399   /**
   410    * Set a shortcode attribute.
   400    * Set a shortcode attribute.
   415    * @param {(number|string)} attr  Attribute key.
   405    * @param {(number|string)} attr  Attribute key.
   416    * @param {string}          value Attribute value.
   406    * @param {string}          value Attribute value.
   417    *
   407    *
   418    * @return {WPShortcode} Shortcode instance.
   408    * @return {WPShortcode} Shortcode instance.
   419    */
   409    */
   420   set: function set(attr, value) {
   410   set(attr, value) {
   421     this.attrs[Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isNumber"])(attr) ? 'numeric' : 'named'][attr] = value;
   411     this.attrs[Object(lodash__WEBPACK_IMPORTED_MODULE_0__["isNumber"])(attr) ? 'numeric' : 'named'][attr] = value;
   422     return this;
   412     return this;
   423   },
   413   },
   424 
   414 
   425   /**
   415   /**
   426    * Transform the shortcode into a string.
   416    * Transform the shortcode into a string.
   427    *
   417    *
   428    * @return {string} String representation of the shortcode.
   418    * @return {string} String representation of the shortcode.
   429    */
   419    */
   430   string: function string() {
   420   string() {
   431     var text = '[' + this.tag;
   421     let text = '[' + this.tag;
   432     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(this.attrs.numeric, function (value) {
   422     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(this.attrs.numeric, value => {
   433       if (/\s/.test(value)) {
   423       if (/\s/.test(value)) {
   434         text += ' "' + value + '"';
   424         text += ' "' + value + '"';
   435       } else {
   425       } else {
   436         text += ' ' + value;
   426         text += ' ' + value;
   437       }
   427       }
   438     });
   428     });
   439     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(this.attrs.named, function (value, name) {
   429     Object(lodash__WEBPACK_IMPORTED_MODULE_0__["forEach"])(this.attrs.named, (value, name) => {
   440       text += ' ' + name + '="' + value + '"';
   430       text += ' ' + name + '="' + value + '"';
   441     }); // If the tag is marked as `single` or `self-closing`, close the tag and
   431     }); // If the tag is marked as `single` or `self-closing`, close the tag and
   442     // ignore any additional content.
   432     // ignore any additional content.
   443 
   433 
   444     if ('single' === this.type) {
   434     if ('single' === this.type) {
   455     } // Add the closing tag.
   445     } // Add the closing tag.
   456 
   446 
   457 
   447 
   458     return text + '[/' + this.tag + ']';
   448     return text + '[/' + this.tag + ']';
   459   }
   449   }
       
   450 
   460 });
   451 });
   461 /* harmony default export */ __webpack_exports__["default"] = (shortcode);
   452 /* harmony default export */ __webpack_exports__["default"] = (shortcode);
   462 
   453 
   463 
   454 
   464 /***/ }),
   455 /***/ }),
   465 
   456 
   466 /***/ 60:
   457 /***/ "4eJC":
   467 /***/ (function(module, exports, __webpack_require__) {
   458 /***/ (function(module, exports, __webpack_require__) {
   468 
   459 
   469 /**
   460 /**
   470  * Memize options object.
   461  * Memize options object.
   471  *
   462  *
   627 }
   618 }
   628 
   619 
   629 module.exports = memize;
   620 module.exports = memize;
   630 
   621 
   631 
   622 
       
   623 /***/ }),
       
   624 
       
   625 /***/ "YLtl":
       
   626 /***/ (function(module, exports) {
       
   627 
       
   628 (function() { module.exports = window["lodash"]; }());
       
   629 
   632 /***/ })
   630 /***/ })
   633 
   631 
   634 /******/ })["default"];
   632 /******/ })["default"];