block-serialization-default-parser.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. this["wp"] = this["wp"] || {}; this["wp"]["blockSerializationDefaultParser"] =
  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 = 235);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ 23:
  90. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  91. "use strict";
  92. // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/arrayWithHoles.js
  93. var arrayWithHoles = __webpack_require__(38);
  94. // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/iterableToArrayLimit.js
  95. function _iterableToArrayLimit(arr, i) {
  96. var _arr = [];
  97. var _n = true;
  98. var _d = false;
  99. var _e = undefined;
  100. try {
  101. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  102. _arr.push(_s.value);
  103. if (i && _arr.length === i) break;
  104. }
  105. } catch (err) {
  106. _d = true;
  107. _e = err;
  108. } finally {
  109. try {
  110. if (!_n && _i["return"] != null) _i["return"]();
  111. } finally {
  112. if (_d) throw _e;
  113. }
  114. }
  115. return _arr;
  116. }
  117. // EXTERNAL MODULE: ./node_modules/@babel/runtime/helpers/esm/nonIterableRest.js
  118. var nonIterableRest = __webpack_require__(39);
  119. // CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/slicedToArray.js
  120. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _slicedToArray; });
  121. function _slicedToArray(arr, i) {
  122. return Object(arrayWithHoles["a" /* default */])(arr) || _iterableToArrayLimit(arr, i) || Object(nonIterableRest["a" /* default */])();
  123. }
  124. /***/ }),
  125. /***/ 235:
  126. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  127. "use strict";
  128. __webpack_require__.r(__webpack_exports__);
  129. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "parse", function() { return parse; });
  130. /* harmony import */ var _babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(23);
  131. var document;
  132. var offset;
  133. var output;
  134. var stack;
  135. /**
  136. * Matches block comment delimiters
  137. *
  138. * While most of this pattern is straightforward the attribute parsing
  139. * incorporates a tricks to make sure we don't choke on specific input
  140. *
  141. * - since JavaScript has no possessive quantifier or atomic grouping
  142. * we are emulating it with a trick
  143. *
  144. * we want a possessive quantifier or atomic group to prevent backtracking
  145. * on the `}`s should we fail to match the remainder of the pattern
  146. *
  147. * we can emulate this with a positive lookahead and back reference
  148. * (a++)*c === ((?=(a+))\1)*c
  149. *
  150. * let's examine an example:
  151. * - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps
  152. * - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps
  153. * - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps
  154. *
  155. * this is because the possessive `++` and the atomic group `(?>)`
  156. * tell the engine that all those `a`s belong together as a single group
  157. * and so it won't split it up when stepping backwards to try and match
  158. *
  159. * if we use /((?=(a+))\1)*c/ then we get the same behavior as the atomic group
  160. * or possessive and prevent the backtracking because the `a+` is matched but
  161. * not captured. thus, we find the long string of `a`s and remember it, then
  162. * reference it as a whole unit inside our pattern
  163. *
  164. * @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead
  165. * @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups
  166. * @see https://javascript.info/regexp-infinite-backtracking-problem
  167. *
  168. * once browsers reliably support atomic grouping or possessive
  169. * quantifiers natively we should remove this trick and simplify
  170. *
  171. * @type RegExp
  172. *
  173. * @since 3.8.0
  174. * @since 4.6.1 added optimization to prevent backtracking on attribute parsing
  175. */
  176. var tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
  177. function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
  178. return {
  179. blockName: blockName,
  180. attrs: attrs,
  181. innerBlocks: innerBlocks,
  182. innerHTML: innerHTML,
  183. innerContent: innerContent
  184. };
  185. }
  186. function Freeform(innerHTML) {
  187. return Block(null, {}, [], innerHTML, [innerHTML]);
  188. }
  189. function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
  190. return {
  191. block: block,
  192. tokenStart: tokenStart,
  193. tokenLength: tokenLength,
  194. prevOffset: prevOffset || tokenStart + tokenLength,
  195. leadingHtmlStart: leadingHtmlStart
  196. };
  197. }
  198. /**
  199. * Parser function, that converts input HTML into a block based structure.
  200. *
  201. * @param {string} doc The HTML document to parse.
  202. *
  203. * @example
  204. * Input post:
  205. * ```html
  206. * <!-- wp:columns {"columns":3} -->
  207. * <div class="wp-block-columns has-3-columns"><!-- wp:column -->
  208. * <div class="wp-block-column"><!-- wp:paragraph -->
  209. * <p>Left</p>
  210. * <!-- /wp:paragraph --></div>
  211. * <!-- /wp:column -->
  212. *
  213. * <!-- wp:column -->
  214. * <div class="wp-block-column"><!-- wp:paragraph -->
  215. * <p><strong>Middle</strong></p>
  216. * <!-- /wp:paragraph --></div>
  217. * <!-- /wp:column -->
  218. *
  219. * <!-- wp:column -->
  220. * <div class="wp-block-column"></div>
  221. * <!-- /wp:column --></div>
  222. * <!-- /wp:columns -->
  223. * ```
  224. *
  225. * Parsing code:
  226. * ```js
  227. * import { parse } from '@wordpress/block-serialization-default-parser';
  228. *
  229. * parse( post ) === [
  230. * {
  231. * blockName: "core/columns",
  232. * attrs: {
  233. * columns: 3
  234. * },
  235. * innerBlocks: [
  236. * {
  237. * blockName: "core/column",
  238. * attrs: null,
  239. * innerBlocks: [
  240. * {
  241. * blockName: "core/paragraph",
  242. * attrs: null,
  243. * innerBlocks: [],
  244. * innerHTML: "\n<p>Left</p>\n"
  245. * }
  246. * ],
  247. * innerHTML: '\n<div class="wp-block-column"></div>\n'
  248. * },
  249. * {
  250. * blockName: "core/column",
  251. * attrs: null,
  252. * innerBlocks: [
  253. * {
  254. * blockName: "core/paragraph",
  255. * attrs: null,
  256. * innerBlocks: [],
  257. * innerHTML: "\n<p><strong>Middle</strong></p>\n"
  258. * }
  259. * ],
  260. * innerHTML: '\n<div class="wp-block-column"></div>\n'
  261. * },
  262. * {
  263. * blockName: "core/column",
  264. * attrs: null,
  265. * innerBlocks: [],
  266. * innerHTML: '\n<div class="wp-block-column"></div>\n'
  267. * }
  268. * ],
  269. * innerHTML: '\n<div class="wp-block-columns has-3-columns">\n\n\n\n</div>\n'
  270. * }
  271. * ];
  272. * ```
  273. * @return {Array} A block-based representation of the input HTML.
  274. */
  275. var parse = function parse(doc) {
  276. document = doc;
  277. offset = 0;
  278. output = [];
  279. stack = [];
  280. tokenizer.lastIndex = 0;
  281. do {// twiddle our thumbs
  282. } while (proceed());
  283. return output;
  284. };
  285. function proceed() {
  286. var next = nextToken();
  287. var _next = Object(_babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(next, 5),
  288. tokenType = _next[0],
  289. blockName = _next[1],
  290. attrs = _next[2],
  291. startOffset = _next[3],
  292. tokenLength = _next[4];
  293. var stackDepth = stack.length; // we may have some HTML soup before the next block
  294. var leadingHtmlStart = startOffset > offset ? offset : null;
  295. switch (tokenType) {
  296. case 'no-more-tokens':
  297. // if not in a block then flush output
  298. if (0 === stackDepth) {
  299. addFreeform();
  300. return false;
  301. } // Otherwise we have a problem
  302. // This is an error
  303. // we have options
  304. // - treat it all as freeform text
  305. // - assume an implicit closer (easiest when not nesting)
  306. // for the easy case we'll assume an implicit closer
  307. if (1 === stackDepth) {
  308. addBlockFromStack();
  309. return false;
  310. } // for the nested case where it's more difficult we'll
  311. // have to assume that multiple closers are missing
  312. // and so we'll collapse the whole stack piecewise
  313. while (0 < stack.length) {
  314. addBlockFromStack();
  315. }
  316. return false;
  317. case 'void-block':
  318. // easy case is if we stumbled upon a void block
  319. // in the top-level of the document
  320. if (0 === stackDepth) {
  321. if (null !== leadingHtmlStart) {
  322. output.push(Freeform(document.substr(leadingHtmlStart, startOffset - leadingHtmlStart)));
  323. }
  324. output.push(Block(blockName, attrs, [], '', []));
  325. offset = startOffset + tokenLength;
  326. return true;
  327. } // otherwise we found an inner block
  328. addInnerBlock(Block(blockName, attrs, [], '', []), startOffset, tokenLength);
  329. offset = startOffset + tokenLength;
  330. return true;
  331. case 'block-opener':
  332. // track all newly-opened blocks on the stack
  333. stack.push(Frame(Block(blockName, attrs, [], '', []), startOffset, tokenLength, startOffset + tokenLength, leadingHtmlStart));
  334. offset = startOffset + tokenLength;
  335. return true;
  336. case 'block-closer':
  337. // if we're missing an opener we're in trouble
  338. // This is an error
  339. if (0 === stackDepth) {
  340. // we have options
  341. // - assume an implicit opener
  342. // - assume _this_ is the opener
  343. // - give up and close out the document
  344. addFreeform();
  345. return false;
  346. } // if we're not nesting then this is easy - close the block
  347. if (1 === stackDepth) {
  348. addBlockFromStack(startOffset);
  349. offset = startOffset + tokenLength;
  350. return true;
  351. } // otherwise we're nested and we have to close out the current
  352. // block and add it as a innerBlock to the parent
  353. var stackTop = stack.pop();
  354. var html = document.substr(stackTop.prevOffset, startOffset - stackTop.prevOffset);
  355. stackTop.block.innerHTML += html;
  356. stackTop.block.innerContent.push(html);
  357. stackTop.prevOffset = startOffset + tokenLength;
  358. addInnerBlock(stackTop.block, stackTop.tokenStart, stackTop.tokenLength, startOffset + tokenLength);
  359. offset = startOffset + tokenLength;
  360. return true;
  361. default:
  362. // This is an error
  363. addFreeform();
  364. return false;
  365. }
  366. }
  367. /**
  368. * Parse JSON if valid, otherwise return null
  369. *
  370. * Note that JSON coming from the block comment
  371. * delimiters is constrained to be an object
  372. * and cannot be things like `true` or `null`
  373. *
  374. * @param {string} input JSON input string to parse
  375. * @return {Object|null} parsed JSON if valid
  376. */
  377. function parseJSON(input) {
  378. try {
  379. return JSON.parse(input);
  380. } catch (e) {
  381. return null;
  382. }
  383. }
  384. function nextToken() {
  385. // aye the magic
  386. // we're using a single RegExp to tokenize the block comment delimiters
  387. // we're also using a trick here because the only difference between a
  388. // block opener and a block closer is the leading `/` before `wp:` (and
  389. // a closer has no attributes). we can trap them both and process the
  390. // match back in Javascript to see which one it was.
  391. var matches = tokenizer.exec(document); // we have no more tokens
  392. if (null === matches) {
  393. return ['no-more-tokens'];
  394. }
  395. var startedAt = matches.index;
  396. var _matches = Object(_babel_runtime_helpers_esm_slicedToArray__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(matches, 7),
  397. match = _matches[0],
  398. closerMatch = _matches[1],
  399. namespaceMatch = _matches[2],
  400. nameMatch = _matches[3],
  401. attrsMatch = _matches[4],
  402. /* internal/unused */
  403. voidMatch = _matches[6];
  404. var length = match.length;
  405. var isCloser = !!closerMatch;
  406. var isVoid = !!voidMatch;
  407. var namespace = namespaceMatch || 'core/';
  408. var name = namespace + nameMatch;
  409. var hasAttrs = !!attrsMatch;
  410. var attrs = hasAttrs ? parseJSON(attrsMatch) : {}; // This state isn't allowed
  411. // This is an error
  412. if (isCloser && (isVoid || hasAttrs)) {// we can ignore them since they don't hurt anything
  413. // we may warn against this at some point or reject it
  414. }
  415. if (isVoid) {
  416. return ['void-block', name, attrs, startedAt, length];
  417. }
  418. if (isCloser) {
  419. return ['block-closer', name, null, startedAt, length];
  420. }
  421. return ['block-opener', name, attrs, startedAt, length];
  422. }
  423. function addFreeform(rawLength) {
  424. var length = rawLength ? rawLength : document.length - offset;
  425. if (0 === length) {
  426. return;
  427. }
  428. output.push(Freeform(document.substr(offset, length)));
  429. }
  430. function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
  431. var parent = stack[stack.length - 1];
  432. parent.block.innerBlocks.push(block);
  433. var html = document.substr(parent.prevOffset, tokenStart - parent.prevOffset);
  434. if (html) {
  435. parent.block.innerHTML += html;
  436. parent.block.innerContent.push(html);
  437. }
  438. parent.block.innerContent.push(null);
  439. parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
  440. }
  441. function addBlockFromStack(endOffset) {
  442. var _stack$pop = stack.pop(),
  443. block = _stack$pop.block,
  444. leadingHtmlStart = _stack$pop.leadingHtmlStart,
  445. prevOffset = _stack$pop.prevOffset,
  446. tokenStart = _stack$pop.tokenStart;
  447. var html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
  448. if (html) {
  449. block.innerHTML += html;
  450. block.innerContent.push(html);
  451. }
  452. if (null !== leadingHtmlStart) {
  453. output.push(Freeform(document.substr(leadingHtmlStart, tokenStart - leadingHtmlStart)));
  454. }
  455. output.push(block);
  456. }
  457. /***/ }),
  458. /***/ 38:
  459. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  460. "use strict";
  461. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _arrayWithHoles; });
  462. function _arrayWithHoles(arr) {
  463. if (Array.isArray(arr)) return arr;
  464. }
  465. /***/ }),
  466. /***/ 39:
  467. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  468. "use strict";
  469. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return _nonIterableRest; });
  470. function _nonIterableRest() {
  471. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  472. }
  473. /***/ })
  474. /******/ });