Nav apraksta

block-serialization-default-parser.js 15KB

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