@wordpress/block-serialization-default-parser 4.16.1-next.957ca95e4c.0 → 4.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 4.17.0 (2022-09-13)
6
+
7
+ ### New Feature
8
+
9
+ - Include TypeScript type declarations ([#43722](https://github.com/WordPress/gutenberg/pull/43722)).
10
+
5
11
  ## 4.16.0 (2022-08-24)
6
12
 
7
13
  ## 4.15.0 (2022-08-10)
package/README.md CHANGED
@@ -108,7 +108,7 @@ _Parameters_
108
108
 
109
109
  _Returns_
110
110
 
111
- - `Array`: A block-based representation of the input HTML.
111
+ - `ParsedBlock[]`: A block-based representation of the input HTML.
112
112
 
113
113
  <!-- END TOKEN(Autogenerated API docs) -->
114
114
 
package/build/index.js CHANGED
@@ -4,10 +4,56 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.parse = void 0;
7
+
8
+ /**
9
+ * @type {string}
10
+ */
7
11
  let document;
12
+ /**
13
+ * @type {number}
14
+ */
15
+
8
16
  let offset;
17
+ /**
18
+ * @type {ParsedBlock[]}
19
+ */
20
+
9
21
  let output;
22
+ /**
23
+ * @type {ParsedFrame[]}
24
+ */
25
+
10
26
  let stack;
27
+ /**
28
+ * @typedef {Object|null} Attributes
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} ParsedBlock
33
+ * @property {string|null} blockName Block name.
34
+ * @property {Attributes} attrs Block attributes.
35
+ * @property {ParsedBlock[]} innerBlocks Inner blocks.
36
+ * @property {string} innerHTML Inner HTML.
37
+ * @property {Array<string|null>} innerContent Inner content.
38
+ */
39
+
40
+ /**
41
+ * @typedef {Object} ParsedFrame
42
+ * @property {ParsedBlock} block Block.
43
+ * @property {number} tokenStart Token start.
44
+ * @property {number} tokenLength Token length.
45
+ * @property {number} prevOffset Previous offset.
46
+ * @property {number|null} leadingHtmlStart Leading HTML start.
47
+ */
48
+
49
+ /**
50
+ * @typedef {'void-block'|'block-opener'|'block-closer'} TokenType
51
+ */
52
+
53
+ /**
54
+ * @typedef {[TokenType, string, Attributes, number, number]} Token
55
+ */
56
+
11
57
  /**
12
58
  * Matches block comment delimiters
13
59
  *
@@ -51,6 +97,16 @@ let stack;
51
97
  */
52
98
 
53
99
  const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
100
+ /**
101
+ * Constructs a block object.
102
+ *
103
+ * @param {string|null} blockName
104
+ * @param {Attributes} attrs
105
+ * @param {ParsedBlock[]} innerBlocks
106
+ * @param {string} innerHTML
107
+ * @param {string[]} innerContent
108
+ * @return {ParsedBlock} The block object.
109
+ */
54
110
 
55
111
  function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
56
112
  return {
@@ -61,10 +117,28 @@ function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
61
117
  innerContent
62
118
  };
63
119
  }
120
+ /**
121
+ * Constructs a freeform block object.
122
+ *
123
+ * @param {string} innerHTML
124
+ * @return {ParsedBlock} The freeform block object.
125
+ */
126
+
64
127
 
65
128
  function Freeform(innerHTML) {
66
129
  return Block(null, {}, [], innerHTML, [innerHTML]);
67
130
  }
131
+ /**
132
+ * Constructs a frame object.
133
+ *
134
+ * @param {ParsedBlock} block
135
+ * @param {number} tokenStart
136
+ * @param {number} tokenLength
137
+ * @param {number} prevOffset
138
+ * @param {number|null} leadingHtmlStart
139
+ * @return {ParsedFrame} The frame object.
140
+ */
141
+
68
142
 
69
143
  function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
70
144
  return {
@@ -150,7 +224,7 @@ function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
150
224
  * }
151
225
  * ];
152
226
  * ```
153
- * @return {Array} A block-based representation of the input HTML.
227
+ * @return {ParsedBlock[]} A block-based representation of the input HTML.
154
228
  */
155
229
 
156
230
 
@@ -166,44 +240,52 @@ const parse = doc => {
166
240
 
167
241
  return output;
168
242
  };
243
+ /**
244
+ * Parses the next token in the input document.
245
+ *
246
+ * @return {boolean} Returns true when there is more tokens to parse.
247
+ */
248
+
169
249
 
170
250
  exports.parse = parse;
171
251
 
172
252
  function proceed() {
253
+ const stackDepth = stack.length;
173
254
  const next = nextToken();
174
- const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
175
- const stackDepth = stack.length; // We may have some HTML soup before the next block.
176
255
 
177
- const leadingHtmlStart = startOffset > offset ? offset : null;
256
+ if (next === null) {
257
+ // If not in a block then flush output.
258
+ if (0 === stackDepth) {
259
+ addFreeform();
260
+ return false;
261
+ } // Otherwise we have a problem
262
+ // This is an error
263
+ // we have options
264
+ // - treat it all as freeform text
265
+ // - assume an implicit closer (easiest when not nesting)
266
+ // For the easy case we'll assume an implicit closer.
178
267
 
179
- switch (tokenType) {
180
- case 'no-more-tokens':
181
- // If not in a block then flush output.
182
- if (0 === stackDepth) {
183
- addFreeform();
184
- return false;
185
- } // Otherwise we have a problem
186
- // This is an error
187
- // we have options
188
- // - treat it all as freeform text
189
- // - assume an implicit closer (easiest when not nesting)
190
- // For the easy case we'll assume an implicit closer.
191
268
 
269
+ if (1 === stackDepth) {
270
+ addBlockFromStack();
271
+ return false;
272
+ } // For the nested case where it's more difficult we'll
273
+ // have to assume that multiple closers are missing
274
+ // and so we'll collapse the whole stack piecewise.
192
275
 
193
- if (1 === stackDepth) {
194
- addBlockFromStack();
195
- return false;
196
- } // For the nested case where it's more difficult we'll
197
- // have to assume that multiple closers are missing
198
- // and so we'll collapse the whole stack piecewise.
199
276
 
277
+ while (0 < stack.length) {
278
+ addBlockFromStack();
279
+ }
200
280
 
201
- while (0 < stack.length) {
202
- addBlockFromStack();
203
- }
281
+ return false;
282
+ }
204
283
 
205
- return false;
284
+ const [tokenType, blockName, attrs, startOffset, tokenLength] = next; // We may have some HTML soup before the next block.
285
+
286
+ const leadingHtmlStart = startOffset > offset ? offset : null;
206
287
 
288
+ switch (tokenType) {
207
289
  case 'void-block':
208
290
  // easy case is if we stumbled upon a void block
209
291
  // in the top-level of the document.
@@ -249,7 +331,9 @@ function proceed() {
249
331
  // block and add it as a innerBlock to the parent.
250
332
 
251
333
 
252
- const stackTop = stack.pop();
334
+ const stackTop =
335
+ /** @type {ParsedFrame} */
336
+ stack.pop();
253
337
  const html = document.substr(stackTop.prevOffset, startOffset - stackTop.prevOffset);
254
338
  stackTop.block.innerHTML += html;
255
339
  stackTop.block.innerContent.push(html);
@@ -283,6 +367,12 @@ function parseJSON(input) {
283
367
  return null;
284
368
  }
285
369
  }
370
+ /**
371
+ * Finds the next token in the document.
372
+ *
373
+ * @return {Token|null} The next matched token.
374
+ */
375
+
286
376
 
287
377
  function nextToken() {
288
378
  // Aye the magic
@@ -294,7 +384,7 @@ function nextToken() {
294
384
  const matches = tokenizer.exec(document); // We have no more tokens.
295
385
 
296
386
  if (null === matches) {
297
- return ['no-more-tokens'];
387
+ return null;
298
388
  }
299
389
 
300
390
  const startedAt = matches.index;
@@ -324,6 +414,12 @@ function nextToken() {
324
414
 
325
415
  return ['block-opener', name, attrs, startedAt, length];
326
416
  }
417
+ /**
418
+ * Adds a freeform block to the output.
419
+ *
420
+ * @param {number} [rawLength]
421
+ */
422
+
327
423
 
328
424
  function addFreeform(rawLength) {
329
425
  const length = rawLength ? rawLength : document.length - offset;
@@ -334,6 +430,15 @@ function addFreeform(rawLength) {
334
430
 
335
431
  output.push(Freeform(document.substr(offset, length)));
336
432
  }
433
+ /**
434
+ * Adds inner block to the parent block.
435
+ *
436
+ * @param {ParsedBlock} block
437
+ * @param {number} tokenStart
438
+ * @param {number} tokenLength
439
+ * @param {number} [lastOffset]
440
+ */
441
+
337
442
 
338
443
  function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
339
444
  const parent = stack[stack.length - 1];
@@ -348,6 +453,12 @@ function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
348
453
  parent.block.innerContent.push(null);
349
454
  parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
350
455
  }
456
+ /**
457
+ * Adds block from the stack to the output.
458
+ *
459
+ * @param {number} [endOffset]
460
+ */
461
+
351
462
 
352
463
  function addBlockFromStack(endOffset) {
353
464
  const {
@@ -355,7 +466,9 @@ function addBlockFromStack(endOffset) {
355
466
  leadingHtmlStart,
356
467
  prevOffset,
357
468
  tokenStart
358
- } = stack.pop();
469
+ } =
470
+ /** @type {ParsedFrame} */
471
+ stack.pop();
359
472
  const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
360
473
 
361
474
  if (html) {
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/block-serialization-default-parser/src/index.js"],"names":["document","offset","output","stack","tokenizer","Block","blockName","attrs","innerBlocks","innerHTML","innerContent","Freeform","Frame","block","tokenStart","tokenLength","prevOffset","leadingHtmlStart","parse","doc","lastIndex","proceed","next","nextToken","tokenType","startOffset","stackDepth","length","addFreeform","addBlockFromStack","push","substr","addInnerBlock","stackTop","pop","html","parseJSON","input","JSON","e","matches","exec","startedAt","index","match","closerMatch","namespaceMatch","nameMatch","attrsMatch","voidMatch","isCloser","isVoid","namespace","name","hasAttrs","rawLength","lastOffset","parent","endOffset"],"mappings":";;;;;;AAAA,IAAIA,QAAJ;AACA,IAAIC,MAAJ;AACA,IAAIC,MAAJ;AACA,IAAIC,KAAJ;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,SAAS,GACd,8HADD;;AAGA,SAASC,KAAT,CAAgBC,SAAh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CAAgC,IAAhC;AACA2B,EAAAA,MAAM,CAACzC,UAAP,GAAoBwC,UAAU,GAAGA,UAAH,GAAgB1C,UAAU,GAAGC,WAA3D;AACA;;AAED,SAASc,iBAAT,CAA4B6B,SAA5B,EAAwC;AACvC,QAAM;AAAE7C,IAAAA,KAAF;AAASI,IAAAA,gBAAT;AAA2BD,IAAAA,UAA3B;AAAuCF,IAAAA;AAAvC,MAAsDX,KAAK,CAAC+B,GAAN,EAA5D;AAEA,QAAMC,IAAI,GAAGuB,SAAS,GACnB1D,QAAQ,CAAC+B,MAAT,CAAiBf,UAAjB,EAA6B0C,SAAS,GAAG1C,UAAzC,CADmB,GAEnBhB,QAAQ,CAAC+B,MAAT,CAAiBf,UAAjB,CAFH;;AAIA,MAAKmB,IAAL,EAAY;AACXtB,IAAAA,KAAK,CAACJ,SAAN,IAAmB0B,IAAnB;AACAtB,IAAAA,KAAK,CAACH,YAAN,CAAmBoB,IAAnB,CAAyBK,IAAzB;AACA;;AAED,MAAK,SAASlB,gBAAd,EAAiC;AAChCf,IAAAA,MAAM,CAAC4B,IAAP,CACCnB,QAAQ,CACPX,QAAQ,CAAC+B,MAAT,CACCd,gBADD,EAECH,UAAU,GAAGG,gBAFd,CADO,CADT;AAQA;;AAEDf,EAAAA,MAAM,CAAC4B,IAAP,CAAajB,KAAb;AACA","sourcesContent":["let document;\nlet offset;\nlet output;\nlet stack;\n\n/**\n * Matches block comment delimiters\n *\n * While most of this pattern is straightforward the attribute parsing\n * incorporates a tricks to make sure we don't choke on specific input\n *\n * - since JavaScript has no possessive quantifier or atomic grouping\n * we are emulating it with a trick\n *\n * we want a possessive quantifier or atomic group to prevent backtracking\n * on the `}`s should we fail to match the remainder of the pattern\n *\n * we can emulate this with a positive lookahead and back reference\n * (a++)*c === ((?=(a+))\\1)*c\n *\n * let's examine an example:\n * - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps\n * - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps\n * - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps\n *\n * this is because the possessive `++` and the atomic group `(?>)`\n * tell the engine that all those `a`s belong together as a single group\n * and so it won't split it up when stepping backwards to try and match\n *\n * if we use /((?=(a+))\\1)*c/ then we get the same behavior as the atomic group\n * or possessive and prevent the backtracking because the `a+` is matched but\n * not captured. thus, we find the long string of `a`s and remember it, then\n * reference it as a whole unit inside our pattern\n *\n * @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead\n * @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups\n * @see https://javascript.info/regexp-infinite-backtracking-problem\n *\n * once browsers reliably support atomic grouping or possessive\n * quantifiers natively we should remove this trick and simplify\n *\n * @type {RegExp}\n *\n * @since 3.8.0\n * @since 4.6.1 added optimization to prevent backtracking on attribute parsing\n */\nconst tokenizer =\n\t/<!--\\s+(\\/)?wp:([a-z][a-z0-9_-]*\\/)?([a-z][a-z0-9_-]*)\\s+({(?:(?=([^}]+|}+(?=})|(?!}\\s+\\/?-->)[^])*)\\5|[^]*?)}\\s+)?(\\/)?-->/g;\n\nfunction Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {\n\treturn {\n\t\tblockName,\n\t\tattrs,\n\t\tinnerBlocks,\n\t\tinnerHTML,\n\t\tinnerContent,\n\t};\n}\n\nfunction Freeform( innerHTML ) {\n\treturn Block( null, {}, [], innerHTML, [ innerHTML ] );\n}\n\nfunction Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {\n\treturn {\n\t\tblock,\n\t\ttokenStart,\n\t\ttokenLength,\n\t\tprevOffset: prevOffset || tokenStart + tokenLength,\n\t\tleadingHtmlStart,\n\t};\n}\n\n/**\n * Parser function, that converts input HTML into a block based structure.\n *\n * @param {string} doc The HTML document to parse.\n *\n * @example\n * Input post:\n * ```html\n * <!-- wp:columns {\"columns\":3} -->\n * <div class=\"wp-block-columns has-3-columns\"><!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p>Left</p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p><strong>Middle</strong></p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"></div>\n * <!-- /wp:column --></div>\n * <!-- /wp:columns -->\n * ```\n *\n * Parsing code:\n * ```js\n * import { parse } from '@wordpress/block-serialization-default-parser';\n *\n * parse( post ) === [\n * {\n * blockName: \"core/columns\",\n * attrs: {\n * columns: 3\n * },\n * innerBlocks: [\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p>Left</p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p><strong>Middle</strong></p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-columns has-3-columns\">\\n\\n\\n\\n</div>\\n'\n * }\n * ];\n * ```\n * @return {Array} A block-based representation of the input HTML.\n */\nexport const parse = ( doc ) => {\n\tdocument = doc;\n\toffset = 0;\n\toutput = [];\n\tstack = [];\n\ttokenizer.lastIndex = 0;\n\n\tdo {\n\t\t// twiddle our thumbs\n\t} while ( proceed() );\n\n\treturn output;\n};\n\nfunction proceed() {\n\tconst next = nextToken();\n\tconst [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;\n\tconst stackDepth = stack.length;\n\n\t// We may have some HTML soup before the next block.\n\tconst leadingHtmlStart = startOffset > offset ? offset : null;\n\n\tswitch ( tokenType ) {\n\t\tcase 'no-more-tokens':\n\t\t\t// If not in a block then flush output.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// Otherwise we have a problem\n\t\t\t// This is an error\n\t\t\t// we have options\n\t\t\t// - treat it all as freeform text\n\t\t\t// - assume an implicit closer (easiest when not nesting)\n\n\t\t\t// For the easy case we'll assume an implicit closer.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// For the nested case where it's more difficult we'll\n\t\t\t// have to assume that multiple closers are missing\n\t\t\t// and so we'll collapse the whole stack piecewise.\n\t\t\twhile ( 0 < stack.length ) {\n\t\t\t\taddBlockFromStack();\n\t\t\t}\n\t\t\treturn false;\n\n\t\tcase 'void-block':\n\t\t\t// easy case is if we stumbled upon a void block\n\t\t\t// in the top-level of the document.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\tif ( null !== leadingHtmlStart ) {\n\t\t\t\t\toutput.push(\n\t\t\t\t\t\tFreeform(\n\t\t\t\t\t\t\tdocument.substr(\n\t\t\t\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\t\t\t\tstartOffset - leadingHtmlStart\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\toutput.push( Block( blockName, attrs, [], '', [] ) );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we found an inner block.\n\t\t\taddInnerBlock(\n\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\tstartOffset,\n\t\t\t\ttokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-opener':\n\t\t\t// Track all newly-opened blocks on the stack.\n\t\t\tstack.push(\n\t\t\t\tFrame(\n\t\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\t\tstartOffset,\n\t\t\t\t\ttokenLength,\n\t\t\t\t\tstartOffset + tokenLength,\n\t\t\t\t\tleadingHtmlStart\n\t\t\t\t)\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-closer':\n\t\t\t// If we're missing an opener we're in trouble\n\t\t\t// This is an error.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\t// We have options\n\t\t\t\t// - assume an implicit opener\n\t\t\t\t// - assume _this_ is the opener\n\t\t\t\t// - give up and close out the document.\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// If we're not nesting then this is easy - close the block.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack( startOffset );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we're nested and we have to close out the current\n\t\t\t// block and add it as a innerBlock to the parent.\n\t\t\tconst stackTop = stack.pop();\n\t\t\tconst html = document.substr(\n\t\t\t\tstackTop.prevOffset,\n\t\t\t\tstartOffset - stackTop.prevOffset\n\t\t\t);\n\t\t\tstackTop.block.innerHTML += html;\n\t\t\tstackTop.block.innerContent.push( html );\n\t\t\tstackTop.prevOffset = startOffset + tokenLength;\n\n\t\t\taddInnerBlock(\n\t\t\t\tstackTop.block,\n\t\t\t\tstackTop.tokenStart,\n\t\t\t\tstackTop.tokenLength,\n\t\t\t\tstartOffset + tokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tdefault:\n\t\t\t// This is an error.\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Parse JSON if valid, otherwise return null\n *\n * Note that JSON coming from the block comment\n * delimiters is constrained to be an object\n * and cannot be things like `true` or `null`\n *\n * @param {string} input JSON input string to parse\n * @return {Object|null} parsed JSON if valid\n */\nfunction parseJSON( input ) {\n\ttry {\n\t\treturn JSON.parse( input );\n\t} catch ( e ) {\n\t\treturn null;\n\t}\n}\n\nfunction nextToken() {\n\t// Aye the magic\n\t// we're using a single RegExp to tokenize the block comment delimiters\n\t// we're also using a trick here because the only difference between a\n\t// block opener and a block closer is the leading `/` before `wp:` (and\n\t// a closer has no attributes). we can trap them both and process the\n\t// match back in JavaScript to see which one it was.\n\tconst matches = tokenizer.exec( document );\n\n\t// We have no more tokens.\n\tif ( null === matches ) {\n\t\treturn [ 'no-more-tokens' ];\n\t}\n\n\tconst startedAt = matches.index;\n\tconst [\n\t\tmatch,\n\t\tcloserMatch,\n\t\tnamespaceMatch,\n\t\tnameMatch,\n\t\tattrsMatch /* Internal/unused. */,\n\t\t,\n\t\tvoidMatch,\n\t] = matches;\n\n\tconst length = match.length;\n\tconst isCloser = !! closerMatch;\n\tconst isVoid = !! voidMatch;\n\tconst namespace = namespaceMatch || 'core/';\n\tconst name = namespace + nameMatch;\n\tconst hasAttrs = !! attrsMatch;\n\tconst attrs = hasAttrs ? parseJSON( attrsMatch ) : {};\n\n\t// This state isn't allowed\n\t// This is an error.\n\tif ( isCloser && ( isVoid || hasAttrs ) ) {\n\t\t// We can ignore them since they don't hurt anything\n\t\t// we may warn against this at some point or reject it.\n\t}\n\n\tif ( isVoid ) {\n\t\treturn [ 'void-block', name, attrs, startedAt, length ];\n\t}\n\n\tif ( isCloser ) {\n\t\treturn [ 'block-closer', name, null, startedAt, length ];\n\t}\n\n\treturn [ 'block-opener', name, attrs, startedAt, length ];\n}\n\nfunction addFreeform( rawLength ) {\n\tconst length = rawLength ? rawLength : document.length - offset;\n\n\tif ( 0 === length ) {\n\t\treturn;\n\t}\n\n\toutput.push( Freeform( document.substr( offset, length ) ) );\n}\n\nfunction addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {\n\tconst parent = stack[ stack.length - 1 ];\n\tparent.block.innerBlocks.push( block );\n\tconst html = document.substr(\n\t\tparent.prevOffset,\n\t\ttokenStart - parent.prevOffset\n\t);\n\n\tif ( html ) {\n\t\tparent.block.innerHTML += html;\n\t\tparent.block.innerContent.push( html );\n\t}\n\n\tparent.block.innerContent.push( null );\n\tparent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;\n}\n\nfunction addBlockFromStack( endOffset ) {\n\tconst { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();\n\n\tconst html = endOffset\n\t\t? document.substr( prevOffset, endOffset - prevOffset )\n\t\t: document.substr( prevOffset );\n\n\tif ( html ) {\n\t\tblock.innerHTML += html;\n\t\tblock.innerContent.push( html );\n\t}\n\n\tif ( null !== leadingHtmlStart ) {\n\t\toutput.push(\n\t\t\tFreeform(\n\t\t\t\tdocument.substr(\n\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\ttokenStart - leadingHtmlStart\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t}\n\n\toutput.push( block );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/block-serialization-default-parser/src/index.js"],"names":["document","offset","output","stack","tokenizer","Block","blockName","attrs","innerBlocks","innerHTML","innerContent","Freeform","Frame","block","tokenStart","tokenLength","prevOffset","leadingHtmlStart","parse","doc","lastIndex","proceed","stackDepth","length","next","nextToken","addFreeform","addBlockFromStack","tokenType","startOffset","push","substr","addInnerBlock","stackTop","pop","html","parseJSON","input","JSON","e","matches","exec","startedAt","index","match","closerMatch","namespaceMatch","nameMatch","attrsMatch","voidMatch","isCloser","isVoid","namespace","name","hasAttrs","rawLength","lastOffset","parent","endOffset"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA,IAAIA,QAAJ;AACA;AACA;AACA;;AACA,IAAIC,MAAJ;AACA;AACA;AACA;;AACA,IAAIC,MAAJ;AACA;AACA;AACA;;AACA,IAAIC,KAAJ;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,SAAS,GACd,8HADD;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,KAAT,CAAgBC,SAAhB,EAA2BC,KAA3B,EAAkCC,WAAlC,EAA+CC,SAA/C,EAA0DC,YAA1D,EAAyE;AACxE,SAAO;AACNJ,IAAAA,SADM;AAENC,IAAAA,KAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,SAJM;AAKNC,IAAAA;AALM,GAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASC,QAAT,CAAmBF,SAAnB,EAA+B;AAC9B,SAAOJ,KAAK,CAAE,IAAF,EAAQ,EAAR,EAAY,EAAZ,EAAgBI,SAAhB,EAA2B,CAAEA,SAAF,CAA3B,CAAZ;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASG,KAAT,CAAgBC,KAAhB,EAAuBC,UAAvB,EAAmCC,WAAnC,EAAgDC,UAAhD,EAA4DC,gBAA5D,EAA+E;AAC9E,SAAO;AACNJ,IAAAA,KADM;AAENC,IAAAA,UAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,UAAU,EAAEA,UAAU,IAAIF,UAAU,GAAGC,WAJjC;AAKNE,IAAAA;AALM,GAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA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K,IAAzB;AACA;;AAED,MAAK,SAASlB,gBAAd,EAAiC;AAChCf,IAAAA,MAAM,CAAC4B,IAAP,CACCnB,QAAQ,CACPX,QAAQ,CAAC+B,MAAT,CACCd,gBADD,EAECH,UAAU,GAAGG,gBAFd,CADO,CADT;AAQA;;AAEDf,EAAAA,MAAM,CAAC4B,IAAP,CAAajB,KAAb;AACA","sourcesContent":["/**\n * @type {string}\n */\nlet document;\n/**\n * @type {number}\n */\nlet offset;\n/**\n * @type {ParsedBlock[]}\n */\nlet output;\n/**\n * @type {ParsedFrame[]}\n */\nlet stack;\n\n/**\n * @typedef {Object|null} Attributes\n */\n\n/**\n * @typedef {Object} ParsedBlock\n * @property {string|null} blockName Block name.\n * @property {Attributes} attrs Block attributes.\n * @property {ParsedBlock[]} innerBlocks Inner blocks.\n * @property {string} innerHTML Inner HTML.\n * @property {Array<string|null>} innerContent Inner content.\n */\n\n/**\n * @typedef {Object} ParsedFrame\n * @property {ParsedBlock} block Block.\n * @property {number} tokenStart Token start.\n * @property {number} tokenLength Token length.\n * @property {number} prevOffset Previous offset.\n * @property {number|null} leadingHtmlStart Leading HTML start.\n */\n\n/**\n * @typedef {'void-block'|'block-opener'|'block-closer'} TokenType\n */\n\n/**\n * @typedef {[TokenType, string, Attributes, number, number]} Token\n */\n\n/**\n * Matches block comment delimiters\n *\n * While most of this pattern is straightforward the attribute parsing\n * incorporates a tricks to make sure we don't choke on specific input\n *\n * - since JavaScript has no possessive quantifier or atomic grouping\n * we are emulating it with a trick\n *\n * we want a possessive quantifier or atomic group to prevent backtracking\n * on the `}`s should we fail to match the remainder of the pattern\n *\n * we can emulate this with a positive lookahead and back reference\n * (a++)*c === ((?=(a+))\\1)*c\n *\n * let's examine an example:\n * - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps\n * - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps\n * - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps\n *\n * this is because the possessive `++` and the atomic group `(?>)`\n * tell the engine that all those `a`s belong together as a single group\n * and so it won't split it up when stepping backwards to try and match\n *\n * if we use /((?=(a+))\\1)*c/ then we get the same behavior as the atomic group\n * or possessive and prevent the backtracking because the `a+` is matched but\n * not captured. thus, we find the long string of `a`s and remember it, then\n * reference it as a whole unit inside our pattern\n *\n * @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead\n * @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups\n * @see https://javascript.info/regexp-infinite-backtracking-problem\n *\n * once browsers reliably support atomic grouping or possessive\n * quantifiers natively we should remove this trick and simplify\n *\n * @type {RegExp}\n *\n * @since 3.8.0\n * @since 4.6.1 added optimization to prevent backtracking on attribute parsing\n */\nconst tokenizer =\n\t/<!--\\s+(\\/)?wp:([a-z][a-z0-9_-]*\\/)?([a-z][a-z0-9_-]*)\\s+({(?:(?=([^}]+|}+(?=})|(?!}\\s+\\/?-->)[^])*)\\5|[^]*?)}\\s+)?(\\/)?-->/g;\n\n/**\n * Constructs a block object.\n *\n * @param {string|null} blockName\n * @param {Attributes} attrs\n * @param {ParsedBlock[]} innerBlocks\n * @param {string} innerHTML\n * @param {string[]} innerContent\n * @return {ParsedBlock} The block object.\n */\nfunction Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {\n\treturn {\n\t\tblockName,\n\t\tattrs,\n\t\tinnerBlocks,\n\t\tinnerHTML,\n\t\tinnerContent,\n\t};\n}\n\n/**\n * Constructs a freeform block object.\n *\n * @param {string} innerHTML\n * @return {ParsedBlock} The freeform block object.\n */\nfunction Freeform( innerHTML ) {\n\treturn Block( null, {}, [], innerHTML, [ innerHTML ] );\n}\n\n/**\n * Constructs a frame object.\n *\n * @param {ParsedBlock} block\n * @param {number} tokenStart\n * @param {number} tokenLength\n * @param {number} prevOffset\n * @param {number|null} leadingHtmlStart\n * @return {ParsedFrame} The frame object.\n */\nfunction Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {\n\treturn {\n\t\tblock,\n\t\ttokenStart,\n\t\ttokenLength,\n\t\tprevOffset: prevOffset || tokenStart + tokenLength,\n\t\tleadingHtmlStart,\n\t};\n}\n\n/**\n * Parser function, that converts input HTML into a block based structure.\n *\n * @param {string} doc The HTML document to parse.\n *\n * @example\n * Input post:\n * ```html\n * <!-- wp:columns {\"columns\":3} -->\n * <div class=\"wp-block-columns has-3-columns\"><!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p>Left</p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p><strong>Middle</strong></p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"></div>\n * <!-- /wp:column --></div>\n * <!-- /wp:columns -->\n * ```\n *\n * Parsing code:\n * ```js\n * import { parse } from '@wordpress/block-serialization-default-parser';\n *\n * parse( post ) === [\n * {\n * blockName: \"core/columns\",\n * attrs: {\n * columns: 3\n * },\n * innerBlocks: [\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p>Left</p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p><strong>Middle</strong></p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-columns has-3-columns\">\\n\\n\\n\\n</div>\\n'\n * }\n * ];\n * ```\n * @return {ParsedBlock[]} A block-based representation of the input HTML.\n */\nexport const parse = ( doc ) => {\n\tdocument = doc;\n\toffset = 0;\n\toutput = [];\n\tstack = [];\n\ttokenizer.lastIndex = 0;\n\n\tdo {\n\t\t// twiddle our thumbs\n\t} while ( proceed() );\n\n\treturn output;\n};\n\n/**\n * Parses the next token in the input document.\n *\n * @return {boolean} Returns true when there is more tokens to parse.\n */\nfunction proceed() {\n\tconst stackDepth = stack.length;\n\tconst next = nextToken();\n\tif ( next === null ) {\n\t\t// If not in a block then flush output.\n\t\tif ( 0 === stackDepth ) {\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Otherwise we have a problem\n\t\t// This is an error\n\t\t// we have options\n\t\t// - treat it all as freeform text\n\t\t// - assume an implicit closer (easiest when not nesting)\n\n\t\t// For the easy case we'll assume an implicit closer.\n\t\tif ( 1 === stackDepth ) {\n\t\t\taddBlockFromStack();\n\t\t\treturn false;\n\t\t}\n\n\t\t// For the nested case where it's more difficult we'll\n\t\t// have to assume that multiple closers are missing\n\t\t// and so we'll collapse the whole stack piecewise.\n\t\twhile ( 0 < stack.length ) {\n\t\t\taddBlockFromStack();\n\t\t}\n\t\treturn false;\n\t}\n\tconst [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;\n\n\t// We may have some HTML soup before the next block.\n\tconst leadingHtmlStart = startOffset > offset ? offset : null;\n\n\tswitch ( tokenType ) {\n\t\tcase 'void-block':\n\t\t\t// easy case is if we stumbled upon a void block\n\t\t\t// in the top-level of the document.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\tif ( null !== leadingHtmlStart ) {\n\t\t\t\t\toutput.push(\n\t\t\t\t\t\tFreeform(\n\t\t\t\t\t\t\tdocument.substr(\n\t\t\t\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\t\t\t\tstartOffset - leadingHtmlStart\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\toutput.push( Block( blockName, attrs, [], '', [] ) );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we found an inner block.\n\t\t\taddInnerBlock(\n\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\tstartOffset,\n\t\t\t\ttokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-opener':\n\t\t\t// Track all newly-opened blocks on the stack.\n\t\t\tstack.push(\n\t\t\t\tFrame(\n\t\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\t\tstartOffset,\n\t\t\t\t\ttokenLength,\n\t\t\t\t\tstartOffset + tokenLength,\n\t\t\t\t\tleadingHtmlStart\n\t\t\t\t)\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-closer':\n\t\t\t// If we're missing an opener we're in trouble\n\t\t\t// This is an error.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\t// We have options\n\t\t\t\t// - assume an implicit opener\n\t\t\t\t// - assume _this_ is the opener\n\t\t\t\t// - give up and close out the document.\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// If we're not nesting then this is easy - close the block.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack( startOffset );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we're nested and we have to close out the current\n\t\t\t// block and add it as a innerBlock to the parent.\n\t\t\tconst stackTop = /** @type {ParsedFrame} */ ( stack.pop() );\n\t\t\tconst html = document.substr(\n\t\t\t\tstackTop.prevOffset,\n\t\t\t\tstartOffset - stackTop.prevOffset\n\t\t\t);\n\t\t\tstackTop.block.innerHTML += html;\n\t\t\tstackTop.block.innerContent.push( html );\n\t\t\tstackTop.prevOffset = startOffset + tokenLength;\n\n\t\t\taddInnerBlock(\n\t\t\t\tstackTop.block,\n\t\t\t\tstackTop.tokenStart,\n\t\t\t\tstackTop.tokenLength,\n\t\t\t\tstartOffset + tokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tdefault:\n\t\t\t// This is an error.\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Parse JSON if valid, otherwise return null\n *\n * Note that JSON coming from the block comment\n * delimiters is constrained to be an object\n * and cannot be things like `true` or `null`\n *\n * @param {string} input JSON input string to parse\n * @return {Object|null} parsed JSON if valid\n */\nfunction parseJSON( input ) {\n\ttry {\n\t\treturn JSON.parse( input );\n\t} catch ( e ) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Finds the next token in the document.\n *\n * @return {Token|null} The next matched token.\n */\nfunction nextToken() {\n\t// Aye the magic\n\t// we're using a single RegExp to tokenize the block comment delimiters\n\t// we're also using a trick here because the only difference between a\n\t// block opener and a block closer is the leading `/` before `wp:` (and\n\t// a closer has no attributes). we can trap them both and process the\n\t// match back in JavaScript to see which one it was.\n\tconst matches = tokenizer.exec( document );\n\n\t// We have no more tokens.\n\tif ( null === matches ) {\n\t\treturn null;\n\t}\n\n\tconst startedAt = matches.index;\n\tconst [\n\t\tmatch,\n\t\tcloserMatch,\n\t\tnamespaceMatch,\n\t\tnameMatch,\n\t\tattrsMatch /* Internal/unused. */,\n\t\t,\n\t\tvoidMatch,\n\t] = matches;\n\n\tconst length = match.length;\n\tconst isCloser = !! closerMatch;\n\tconst isVoid = !! voidMatch;\n\tconst namespace = namespaceMatch || 'core/';\n\tconst name = namespace + nameMatch;\n\tconst hasAttrs = !! attrsMatch;\n\tconst attrs = hasAttrs ? parseJSON( attrsMatch ) : {};\n\n\t// This state isn't allowed\n\t// This is an error.\n\tif ( isCloser && ( isVoid || hasAttrs ) ) {\n\t\t// We can ignore them since they don't hurt anything\n\t\t// we may warn against this at some point or reject it.\n\t}\n\n\tif ( isVoid ) {\n\t\treturn [ 'void-block', name, attrs, startedAt, length ];\n\t}\n\n\tif ( isCloser ) {\n\t\treturn [ 'block-closer', name, null, startedAt, length ];\n\t}\n\n\treturn [ 'block-opener', name, attrs, startedAt, length ];\n}\n\n/**\n * Adds a freeform block to the output.\n *\n * @param {number} [rawLength]\n */\nfunction addFreeform( rawLength ) {\n\tconst length = rawLength ? rawLength : document.length - offset;\n\n\tif ( 0 === length ) {\n\t\treturn;\n\t}\n\n\toutput.push( Freeform( document.substr( offset, length ) ) );\n}\n\n/**\n * Adds inner block to the parent block.\n *\n * @param {ParsedBlock} block\n * @param {number} tokenStart\n * @param {number} tokenLength\n * @param {number} [lastOffset]\n */\nfunction addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {\n\tconst parent = stack[ stack.length - 1 ];\n\tparent.block.innerBlocks.push( block );\n\tconst html = document.substr(\n\t\tparent.prevOffset,\n\t\ttokenStart - parent.prevOffset\n\t);\n\n\tif ( html ) {\n\t\tparent.block.innerHTML += html;\n\t\tparent.block.innerContent.push( html );\n\t}\n\n\tparent.block.innerContent.push( null );\n\tparent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;\n}\n\n/**\n * Adds block from the stack to the output.\n *\n * @param {number} [endOffset]\n */\nfunction addBlockFromStack( endOffset ) {\n\tconst { block, leadingHtmlStart, prevOffset, tokenStart } =\n\t\t/** @type {ParsedFrame} */ ( stack.pop() );\n\n\tconst html = endOffset\n\t\t? document.substr( prevOffset, endOffset - prevOffset )\n\t\t: document.substr( prevOffset );\n\n\tif ( html ) {\n\t\tblock.innerHTML += html;\n\t\tblock.innerContent.push( html );\n\t}\n\n\tif ( null !== leadingHtmlStart ) {\n\t\toutput.push(\n\t\t\tFreeform(\n\t\t\t\tdocument.substr(\n\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\ttokenStart - leadingHtmlStart\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t}\n\n\toutput.push( block );\n}\n"]}
@@ -1,7 +1,52 @@
1
+ /**
2
+ * @type {string}
3
+ */
1
4
  let document;
5
+ /**
6
+ * @type {number}
7
+ */
8
+
2
9
  let offset;
10
+ /**
11
+ * @type {ParsedBlock[]}
12
+ */
13
+
3
14
  let output;
15
+ /**
16
+ * @type {ParsedFrame[]}
17
+ */
18
+
4
19
  let stack;
20
+ /**
21
+ * @typedef {Object|null} Attributes
22
+ */
23
+
24
+ /**
25
+ * @typedef {Object} ParsedBlock
26
+ * @property {string|null} blockName Block name.
27
+ * @property {Attributes} attrs Block attributes.
28
+ * @property {ParsedBlock[]} innerBlocks Inner blocks.
29
+ * @property {string} innerHTML Inner HTML.
30
+ * @property {Array<string|null>} innerContent Inner content.
31
+ */
32
+
33
+ /**
34
+ * @typedef {Object} ParsedFrame
35
+ * @property {ParsedBlock} block Block.
36
+ * @property {number} tokenStart Token start.
37
+ * @property {number} tokenLength Token length.
38
+ * @property {number} prevOffset Previous offset.
39
+ * @property {number|null} leadingHtmlStart Leading HTML start.
40
+ */
41
+
42
+ /**
43
+ * @typedef {'void-block'|'block-opener'|'block-closer'} TokenType
44
+ */
45
+
46
+ /**
47
+ * @typedef {[TokenType, string, Attributes, number, number]} Token
48
+ */
49
+
5
50
  /**
6
51
  * Matches block comment delimiters
7
52
  *
@@ -45,6 +90,16 @@ let stack;
45
90
  */
46
91
 
47
92
  const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
93
+ /**
94
+ * Constructs a block object.
95
+ *
96
+ * @param {string|null} blockName
97
+ * @param {Attributes} attrs
98
+ * @param {ParsedBlock[]} innerBlocks
99
+ * @param {string} innerHTML
100
+ * @param {string[]} innerContent
101
+ * @return {ParsedBlock} The block object.
102
+ */
48
103
 
49
104
  function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
50
105
  return {
@@ -55,10 +110,28 @@ function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
55
110
  innerContent
56
111
  };
57
112
  }
113
+ /**
114
+ * Constructs a freeform block object.
115
+ *
116
+ * @param {string} innerHTML
117
+ * @return {ParsedBlock} The freeform block object.
118
+ */
119
+
58
120
 
59
121
  function Freeform(innerHTML) {
60
122
  return Block(null, {}, [], innerHTML, [innerHTML]);
61
123
  }
124
+ /**
125
+ * Constructs a frame object.
126
+ *
127
+ * @param {ParsedBlock} block
128
+ * @param {number} tokenStart
129
+ * @param {number} tokenLength
130
+ * @param {number} prevOffset
131
+ * @param {number|null} leadingHtmlStart
132
+ * @return {ParsedFrame} The frame object.
133
+ */
134
+
62
135
 
63
136
  function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
64
137
  return {
@@ -144,7 +217,7 @@ function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
144
217
  * }
145
218
  * ];
146
219
  * ```
147
- * @return {Array} A block-based representation of the input HTML.
220
+ * @return {ParsedBlock[]} A block-based representation of the input HTML.
148
221
  */
149
222
 
150
223
 
@@ -160,42 +233,49 @@ export const parse = doc => {
160
233
 
161
234
  return output;
162
235
  };
236
+ /**
237
+ * Parses the next token in the input document.
238
+ *
239
+ * @return {boolean} Returns true when there is more tokens to parse.
240
+ */
163
241
 
164
242
  function proceed() {
243
+ const stackDepth = stack.length;
165
244
  const next = nextToken();
166
- const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
167
- const stackDepth = stack.length; // We may have some HTML soup before the next block.
168
245
 
169
- const leadingHtmlStart = startOffset > offset ? offset : null;
246
+ if (next === null) {
247
+ // If not in a block then flush output.
248
+ if (0 === stackDepth) {
249
+ addFreeform();
250
+ return false;
251
+ } // Otherwise we have a problem
252
+ // This is an error
253
+ // we have options
254
+ // - treat it all as freeform text
255
+ // - assume an implicit closer (easiest when not nesting)
256
+ // For the easy case we'll assume an implicit closer.
170
257
 
171
- switch (tokenType) {
172
- case 'no-more-tokens':
173
- // If not in a block then flush output.
174
- if (0 === stackDepth) {
175
- addFreeform();
176
- return false;
177
- } // Otherwise we have a problem
178
- // This is an error
179
- // we have options
180
- // - treat it all as freeform text
181
- // - assume an implicit closer (easiest when not nesting)
182
- // For the easy case we'll assume an implicit closer.
183
258
 
259
+ if (1 === stackDepth) {
260
+ addBlockFromStack();
261
+ return false;
262
+ } // For the nested case where it's more difficult we'll
263
+ // have to assume that multiple closers are missing
264
+ // and so we'll collapse the whole stack piecewise.
184
265
 
185
- if (1 === stackDepth) {
186
- addBlockFromStack();
187
- return false;
188
- } // For the nested case where it's more difficult we'll
189
- // have to assume that multiple closers are missing
190
- // and so we'll collapse the whole stack piecewise.
191
266
 
267
+ while (0 < stack.length) {
268
+ addBlockFromStack();
269
+ }
192
270
 
193
- while (0 < stack.length) {
194
- addBlockFromStack();
195
- }
271
+ return false;
272
+ }
196
273
 
197
- return false;
274
+ const [tokenType, blockName, attrs, startOffset, tokenLength] = next; // We may have some HTML soup before the next block.
275
+
276
+ const leadingHtmlStart = startOffset > offset ? offset : null;
198
277
 
278
+ switch (tokenType) {
199
279
  case 'void-block':
200
280
  // easy case is if we stumbled upon a void block
201
281
  // in the top-level of the document.
@@ -241,7 +321,9 @@ function proceed() {
241
321
  // block and add it as a innerBlock to the parent.
242
322
 
243
323
 
244
- const stackTop = stack.pop();
324
+ const stackTop =
325
+ /** @type {ParsedFrame} */
326
+ stack.pop();
245
327
  const html = document.substr(stackTop.prevOffset, startOffset - stackTop.prevOffset);
246
328
  stackTop.block.innerHTML += html;
247
329
  stackTop.block.innerContent.push(html);
@@ -275,6 +357,12 @@ function parseJSON(input) {
275
357
  return null;
276
358
  }
277
359
  }
360
+ /**
361
+ * Finds the next token in the document.
362
+ *
363
+ * @return {Token|null} The next matched token.
364
+ */
365
+
278
366
 
279
367
  function nextToken() {
280
368
  // Aye the magic
@@ -286,7 +374,7 @@ function nextToken() {
286
374
  const matches = tokenizer.exec(document); // We have no more tokens.
287
375
 
288
376
  if (null === matches) {
289
- return ['no-more-tokens'];
377
+ return null;
290
378
  }
291
379
 
292
380
  const startedAt = matches.index;
@@ -316,6 +404,12 @@ function nextToken() {
316
404
 
317
405
  return ['block-opener', name, attrs, startedAt, length];
318
406
  }
407
+ /**
408
+ * Adds a freeform block to the output.
409
+ *
410
+ * @param {number} [rawLength]
411
+ */
412
+
319
413
 
320
414
  function addFreeform(rawLength) {
321
415
  const length = rawLength ? rawLength : document.length - offset;
@@ -326,6 +420,15 @@ function addFreeform(rawLength) {
326
420
 
327
421
  output.push(Freeform(document.substr(offset, length)));
328
422
  }
423
+ /**
424
+ * Adds inner block to the parent block.
425
+ *
426
+ * @param {ParsedBlock} block
427
+ * @param {number} tokenStart
428
+ * @param {number} tokenLength
429
+ * @param {number} [lastOffset]
430
+ */
431
+
329
432
 
330
433
  function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
331
434
  const parent = stack[stack.length - 1];
@@ -340,6 +443,12 @@ function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
340
443
  parent.block.innerContent.push(null);
341
444
  parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
342
445
  }
446
+ /**
447
+ * Adds block from the stack to the output.
448
+ *
449
+ * @param {number} [endOffset]
450
+ */
451
+
343
452
 
344
453
  function addBlockFromStack(endOffset) {
345
454
  const {
@@ -347,7 +456,9 @@ function addBlockFromStack(endOffset) {
347
456
  leadingHtmlStart,
348
457
  prevOffset,
349
458
  tokenStart
350
- } = stack.pop();
459
+ } =
460
+ /** @type {ParsedFrame} */
461
+ stack.pop();
351
462
  const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
352
463
 
353
464
  if (html) {
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/block-serialization-default-parser/src/index.js"],"names":["document","offset","output","stack","tokenizer","Block","blockName","attrs","innerBlocks","innerHTML","innerContent","Freeform","Frame","block","tokenStart","tokenLength","prevOffset","leadingHtmlStart","parse","doc","lastIndex","proceed","next","nextToken","tokenType","startOffset","stackDepth","length","addFreeform","addBlockFromStack","push","substr","addInnerBlock","stackTop","pop","html","parseJSON","input","JSON","e","matches","exec","startedAt","index","match","closerMatch","namespaceMatch","nameMatch","attrsMatch","voidMatch","isCloser","isVoid","namespace","name","hasAttrs","rawLength","lastOffset","parent","endOffset"],"mappings":"AAAA,IAAIA,QAAJ;AACA,IAAIC,MAAJ;AACA,IAAIC,MAAJ;AACA,IAAIC,KAAJ;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,SAAS,GACd,8HADD;;AAGA,SAASC,KAAT,CAAgBC,SAAhB,EAA2BC,KAA3B,EAAkCC,WAAlC,EAA+CC,SAA/C,EAA0DC,YAA1D,EAAyE;AACxE,SAAO;AACNJ,IAAAA,SADM;AAENC,IAAAA,KAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,SAJM;AAKNC,IAAAA;AALM,GAAP;AAOA;;AAED,SAASC,QAAT,CAAmBF,SAAnB,EAA+B;AAC9B,SAAOJ,KAAK,CAAE,IAAF,EAAQ,EAAR,EAAY,EAAZ,EAAgBI,SAAhB,EAA2B,CAAEA,SAAF,CAA3B,CAAZ;AACA;;AAED,SAASG,KAAT,CAAgBC,KAAhB,EAAuBC,UAAvB,EAAmCC,WAAnC,EAAgDC,UAAhD,EAA4DC,gBAA5D,EAA+E;AAC9E,SAAO;AACNJ,IAAAA,KADM;AAENC,IAAAA,UAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,UAAU,EAAEA,UAAU,IAAIF,UAAU,GAAGC,WAJjC;AAKNE,IAAAA;AALM,GAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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,CAAC+B,MAAT,CACCd,gBADD,EAECH,UAAU,GAAGG,gBAFd,CADO,CADT;AAQA;;AAEDf,EAAAA,MAAM,CAAC4B,IAAP,CAAajB,KAAb;AACA","sourcesContent":["let document;\nlet offset;\nlet output;\nlet stack;\n\n/**\n * Matches block comment delimiters\n *\n * While most of this pattern is straightforward the attribute parsing\n * incorporates a tricks to make sure we don't choke on specific input\n *\n * - since JavaScript has no possessive quantifier or atomic grouping\n * we are emulating it with a trick\n *\n * we want a possessive quantifier or atomic group to prevent backtracking\n * on the `}`s should we fail to match the remainder of the pattern\n *\n * we can emulate this with a positive lookahead and back reference\n * (a++)*c === ((?=(a+))\\1)*c\n *\n * let's examine an example:\n * - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps\n * - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps\n * - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps\n *\n * this is because the possessive `++` and the atomic group `(?>)`\n * tell the engine that all those `a`s belong together as a single group\n * and so it won't split it up when stepping backwards to try and match\n *\n * if we use /((?=(a+))\\1)*c/ then we get the same behavior as the atomic group\n * or possessive and prevent the backtracking because the `a+` is matched but\n * not captured. thus, we find the long string of `a`s and remember it, then\n * reference it as a whole unit inside our pattern\n *\n * @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead\n * @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups\n * @see https://javascript.info/regexp-infinite-backtracking-problem\n *\n * once browsers reliably support atomic grouping or possessive\n * quantifiers natively we should remove this trick and simplify\n *\n * @type {RegExp}\n *\n * @since 3.8.0\n * @since 4.6.1 added optimization to prevent backtracking on attribute parsing\n */\nconst tokenizer =\n\t/<!--\\s+(\\/)?wp:([a-z][a-z0-9_-]*\\/)?([a-z][a-z0-9_-]*)\\s+({(?:(?=([^}]+|}+(?=})|(?!}\\s+\\/?-->)[^])*)\\5|[^]*?)}\\s+)?(\\/)?-->/g;\n\nfunction Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {\n\treturn {\n\t\tblockName,\n\t\tattrs,\n\t\tinnerBlocks,\n\t\tinnerHTML,\n\t\tinnerContent,\n\t};\n}\n\nfunction Freeform( innerHTML ) {\n\treturn Block( null, {}, [], innerHTML, [ innerHTML ] );\n}\n\nfunction Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {\n\treturn {\n\t\tblock,\n\t\ttokenStart,\n\t\ttokenLength,\n\t\tprevOffset: prevOffset || tokenStart + tokenLength,\n\t\tleadingHtmlStart,\n\t};\n}\n\n/**\n * Parser function, that converts input HTML into a block based structure.\n *\n * @param {string} doc The HTML document to parse.\n *\n * @example\n * Input post:\n * ```html\n * <!-- wp:columns {\"columns\":3} -->\n * <div class=\"wp-block-columns has-3-columns\"><!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p>Left</p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p><strong>Middle</strong></p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"></div>\n * <!-- /wp:column --></div>\n * <!-- /wp:columns -->\n * ```\n *\n * Parsing code:\n * ```js\n * import { parse } from '@wordpress/block-serialization-default-parser';\n *\n * parse( post ) === [\n * {\n * blockName: \"core/columns\",\n * attrs: {\n * columns: 3\n * },\n * innerBlocks: [\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p>Left</p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p><strong>Middle</strong></p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-columns has-3-columns\">\\n\\n\\n\\n</div>\\n'\n * }\n * ];\n * ```\n * @return {Array} A block-based representation of the input HTML.\n */\nexport const parse = ( doc ) => {\n\tdocument = doc;\n\toffset = 0;\n\toutput = [];\n\tstack = [];\n\ttokenizer.lastIndex = 0;\n\n\tdo {\n\t\t// twiddle our thumbs\n\t} while ( proceed() );\n\n\treturn output;\n};\n\nfunction proceed() {\n\tconst next = nextToken();\n\tconst [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;\n\tconst stackDepth = stack.length;\n\n\t// We may have some HTML soup before the next block.\n\tconst leadingHtmlStart = startOffset > offset ? offset : null;\n\n\tswitch ( tokenType ) {\n\t\tcase 'no-more-tokens':\n\t\t\t// If not in a block then flush output.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// Otherwise we have a problem\n\t\t\t// This is an error\n\t\t\t// we have options\n\t\t\t// - treat it all as freeform text\n\t\t\t// - assume an implicit closer (easiest when not nesting)\n\n\t\t\t// For the easy case we'll assume an implicit closer.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// For the nested case where it's more difficult we'll\n\t\t\t// have to assume that multiple closers are missing\n\t\t\t// and so we'll collapse the whole stack piecewise.\n\t\t\twhile ( 0 < stack.length ) {\n\t\t\t\taddBlockFromStack();\n\t\t\t}\n\t\t\treturn false;\n\n\t\tcase 'void-block':\n\t\t\t// easy case is if we stumbled upon a void block\n\t\t\t// in the top-level of the document.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\tif ( null !== leadingHtmlStart ) {\n\t\t\t\t\toutput.push(\n\t\t\t\t\t\tFreeform(\n\t\t\t\t\t\t\tdocument.substr(\n\t\t\t\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\t\t\t\tstartOffset - leadingHtmlStart\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\toutput.push( Block( blockName, attrs, [], '', [] ) );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we found an inner block.\n\t\t\taddInnerBlock(\n\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\tstartOffset,\n\t\t\t\ttokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-opener':\n\t\t\t// Track all newly-opened blocks on the stack.\n\t\t\tstack.push(\n\t\t\t\tFrame(\n\t\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\t\tstartOffset,\n\t\t\t\t\ttokenLength,\n\t\t\t\t\tstartOffset + tokenLength,\n\t\t\t\t\tleadingHtmlStart\n\t\t\t\t)\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-closer':\n\t\t\t// If we're missing an opener we're in trouble\n\t\t\t// This is an error.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\t// We have options\n\t\t\t\t// - assume an implicit opener\n\t\t\t\t// - assume _this_ is the opener\n\t\t\t\t// - give up and close out the document.\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// If we're not nesting then this is easy - close the block.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack( startOffset );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we're nested and we have to close out the current\n\t\t\t// block and add it as a innerBlock to the parent.\n\t\t\tconst stackTop = stack.pop();\n\t\t\tconst html = document.substr(\n\t\t\t\tstackTop.prevOffset,\n\t\t\t\tstartOffset - stackTop.prevOffset\n\t\t\t);\n\t\t\tstackTop.block.innerHTML += html;\n\t\t\tstackTop.block.innerContent.push( html );\n\t\t\tstackTop.prevOffset = startOffset + tokenLength;\n\n\t\t\taddInnerBlock(\n\t\t\t\tstackTop.block,\n\t\t\t\tstackTop.tokenStart,\n\t\t\t\tstackTop.tokenLength,\n\t\t\t\tstartOffset + tokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tdefault:\n\t\t\t// This is an error.\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Parse JSON if valid, otherwise return null\n *\n * Note that JSON coming from the block comment\n * delimiters is constrained to be an object\n * and cannot be things like `true` or `null`\n *\n * @param {string} input JSON input string to parse\n * @return {Object|null} parsed JSON if valid\n */\nfunction parseJSON( input ) {\n\ttry {\n\t\treturn JSON.parse( input );\n\t} catch ( e ) {\n\t\treturn null;\n\t}\n}\n\nfunction nextToken() {\n\t// Aye the magic\n\t// we're using a single RegExp to tokenize the block comment delimiters\n\t// we're also using a trick here because the only difference between a\n\t// block opener and a block closer is the leading `/` before `wp:` (and\n\t// a closer has no attributes). we can trap them both and process the\n\t// match back in JavaScript to see which one it was.\n\tconst matches = tokenizer.exec( document );\n\n\t// We have no more tokens.\n\tif ( null === matches ) {\n\t\treturn [ 'no-more-tokens' ];\n\t}\n\n\tconst startedAt = matches.index;\n\tconst [\n\t\tmatch,\n\t\tcloserMatch,\n\t\tnamespaceMatch,\n\t\tnameMatch,\n\t\tattrsMatch /* Internal/unused. */,\n\t\t,\n\t\tvoidMatch,\n\t] = matches;\n\n\tconst length = match.length;\n\tconst isCloser = !! closerMatch;\n\tconst isVoid = !! voidMatch;\n\tconst namespace = namespaceMatch || 'core/';\n\tconst name = namespace + nameMatch;\n\tconst hasAttrs = !! attrsMatch;\n\tconst attrs = hasAttrs ? parseJSON( attrsMatch ) : {};\n\n\t// This state isn't allowed\n\t// This is an error.\n\tif ( isCloser && ( isVoid || hasAttrs ) ) {\n\t\t// We can ignore them since they don't hurt anything\n\t\t// we may warn against this at some point or reject it.\n\t}\n\n\tif ( isVoid ) {\n\t\treturn [ 'void-block', name, attrs, startedAt, length ];\n\t}\n\n\tif ( isCloser ) {\n\t\treturn [ 'block-closer', name, null, startedAt, length ];\n\t}\n\n\treturn [ 'block-opener', name, attrs, startedAt, length ];\n}\n\nfunction addFreeform( rawLength ) {\n\tconst length = rawLength ? rawLength : document.length - offset;\n\n\tif ( 0 === length ) {\n\t\treturn;\n\t}\n\n\toutput.push( Freeform( document.substr( offset, length ) ) );\n}\n\nfunction addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {\n\tconst parent = stack[ stack.length - 1 ];\n\tparent.block.innerBlocks.push( block );\n\tconst html = document.substr(\n\t\tparent.prevOffset,\n\t\ttokenStart - parent.prevOffset\n\t);\n\n\tif ( html ) {\n\t\tparent.block.innerHTML += html;\n\t\tparent.block.innerContent.push( html );\n\t}\n\n\tparent.block.innerContent.push( null );\n\tparent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;\n}\n\nfunction addBlockFromStack( endOffset ) {\n\tconst { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();\n\n\tconst html = endOffset\n\t\t? document.substr( prevOffset, endOffset - prevOffset )\n\t\t: document.substr( prevOffset );\n\n\tif ( html ) {\n\t\tblock.innerHTML += html;\n\t\tblock.innerContent.push( html );\n\t}\n\n\tif ( null !== leadingHtmlStart ) {\n\t\toutput.push(\n\t\t\tFreeform(\n\t\t\t\tdocument.substr(\n\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\ttokenStart - leadingHtmlStart\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t}\n\n\toutput.push( block );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/block-serialization-default-parser/src/index.js"],"names":["document","offset","output","stack","tokenizer","Block","blockName","attrs","innerBlocks","innerHTML","innerContent","Freeform","Frame","block","tokenStart","tokenLength","prevOffset","leadingHtmlStart","parse","doc","lastIndex","proceed","stackDepth","length","next","nextToken","addFreeform","addBlockFromStack","tokenType","startOffset","push","substr","addInnerBlock","stackTop","pop","html","parseJSON","input","JSON","e","matches","exec","startedAt","index","match","closerMatch","namespaceMatch","nameMatch","attrsMatch","voidMatch","isCloser","isVoid","namespace","name","hasAttrs","rawLength","lastOffset","parent","endOffset"],"mappings":"AAAA;AACA;AACA;AACA,IAAIA,QAAJ;AACA;AACA;AACA;;AACA,IAAIC,MAAJ;AACA;AACA;AACA;;AACA,IAAIC,MAAJ;AACA;AACA;AACA;;AACA,IAAIC,KAAJ;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,SAAS,GACd,8HADD;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,KAAT,CAAgBC,SAAhB,EAA2BC,KAA3B,EAAkCC,WAAlC,EAA+CC,SAA/C,EAA0DC,YAA1D,EAAyE;AACxE,SAAO;AACNJ,IAAAA,SADM;AAENC,IAAAA,KAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,SAJM;AAKNC,IAAAA;AALM,GAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASC,QAAT,CAAmBF,SAAnB,EAA+B;AAC9B,SAAOJ,KAAK,CAAE,IAAF,EAAQ,EAAR,EAAY,EAAZ,EAAgBI,SAAhB,EAA2B,CAAEA,SAAF,CAA3B,CAAZ;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASG,KAAT,CAAgBC,KAAhB,EAAuBC,UAAvB,EAAmCC,WAAnC,EAAgDC,UAAhD,EAA4DC,gBAA5D,EAA+E;AAC9E,SAAO;AACNJ,IAAAA,KADM;AAENC,IAAAA,UAFM;AAGNC,IAAAA,WAHM;AAINC,IAAAA,UAAU,EAAEA,UAAU,IAAIF,UAAU,GAAGC,WAJjC;AAKNE,IAAAA;AALM,GAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,MAAMC,KAAK,GAAKC,GAAF,IAAW;AAC/BnB,EAAAA,QAAQ,GAAGmB,GAAX;AACAlB,EAAAA,MAAM,GAAG,CAAT;AACAC,EAAAA,MAAM,GAAG,EAAT;AACAC,EAAAA,KAAK,GAAG,EAAR;AACAC,EAAAA,SAAS,CAACgB,SAAV,GAAsB,CAAtB;;AAEA,KAAG,CACF;AACA,GAFD,QAEUC,OAAO,EAFjB;;AAIA,SAAOnB,MAAP;AACA,CAZM;AAcP;AACA;AACA;AACA;AACA;;AACA,SAASmB,OAAT,GAAmB;AAClB,QAAMC,UAAU,GAAGnB,KAAK,CAACoB,MAAzB;AACA,QAAMC,IAAI,GAAGC,SAAS,EAAtB;;AACA,MAAKD,IAAI,KAAK,IAAd,EAAqB;AACpB;AACA,QAAK,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AACA;AAED;AACA;AACA;AACA;AACA;;;AACA,SAASY,iBAAT,CAA4B+B,SAA5B,EAAwC;AACvC,QAAM;AAAE7C,IAAAA,KAAF;AAASI,IAAAA,gBAAT;AAA2BD,IAAAA,UAA3B;AAAuCF,IAAAA;AAAvC;AACL;AAA6BX,EAAAA,KAAK,CAAC+B,GAAN,EAD9B;AAGA,QAAMC,IAAI,GAAGuB,SAAS,GACnB1D,QAAQ,CAAC+B,MAAT,CAAiBf,UAAjB,EAA6B0C,SAAS,GAAG1C,UAAzC,CADmB,GAEnBhB,QAAQ,CAAC+B,MAAT,CAAiBf,UAAjB,CAFH;;AAIA,MAAKmB,IAAL,EAAY;AACXtB,IAAAA,KAAK,CAACJ,SAAN,IAAmB0B,IAAnB;AACAtB,IAAAA,KAAK,CAACH,YAAN,CAAmBoB,IAAnB,CAAyBK,IAAzB;AACA;;AAED,MAAK,SAASlB,gBAAd,EAAiC;AAChCf,IAAAA,MAAM,CAAC4B,IAAP,CACCnB,QAAQ,CACPX,QAAQ,CAAC+B,MAAT,CACCd,gBADD,EAECH,UAAU,GAAGG,gBAFd,CADO,CADT;AAQA;;AAEDf,EAAAA,MAAM,CAAC4B,IAAP,CAAajB,KAAb;AACA","sourcesContent":["/**\n * @type {string}\n */\nlet document;\n/**\n * @type {number}\n */\nlet offset;\n/**\n * @type {ParsedBlock[]}\n */\nlet output;\n/**\n * @type {ParsedFrame[]}\n */\nlet stack;\n\n/**\n * @typedef {Object|null} Attributes\n */\n\n/**\n * @typedef {Object} ParsedBlock\n * @property {string|null} blockName Block name.\n * @property {Attributes} attrs Block attributes.\n * @property {ParsedBlock[]} innerBlocks Inner blocks.\n * @property {string} innerHTML Inner HTML.\n * @property {Array<string|null>} innerContent Inner content.\n */\n\n/**\n * @typedef {Object} ParsedFrame\n * @property {ParsedBlock} block Block.\n * @property {number} tokenStart Token start.\n * @property {number} tokenLength Token length.\n * @property {number} prevOffset Previous offset.\n * @property {number|null} leadingHtmlStart Leading HTML start.\n */\n\n/**\n * @typedef {'void-block'|'block-opener'|'block-closer'} TokenType\n */\n\n/**\n * @typedef {[TokenType, string, Attributes, number, number]} Token\n */\n\n/**\n * Matches block comment delimiters\n *\n * While most of this pattern is straightforward the attribute parsing\n * incorporates a tricks to make sure we don't choke on specific input\n *\n * - since JavaScript has no possessive quantifier or atomic grouping\n * we are emulating it with a trick\n *\n * we want a possessive quantifier or atomic group to prevent backtracking\n * on the `}`s should we fail to match the remainder of the pattern\n *\n * we can emulate this with a positive lookahead and back reference\n * (a++)*c === ((?=(a+))\\1)*c\n *\n * let's examine an example:\n * - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps\n * - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps\n * - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps\n *\n * this is because the possessive `++` and the atomic group `(?>)`\n * tell the engine that all those `a`s belong together as a single group\n * and so it won't split it up when stepping backwards to try and match\n *\n * if we use /((?=(a+))\\1)*c/ then we get the same behavior as the atomic group\n * or possessive and prevent the backtracking because the `a+` is matched but\n * not captured. thus, we find the long string of `a`s and remember it, then\n * reference it as a whole unit inside our pattern\n *\n * @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead\n * @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups\n * @see https://javascript.info/regexp-infinite-backtracking-problem\n *\n * once browsers reliably support atomic grouping or possessive\n * quantifiers natively we should remove this trick and simplify\n *\n * @type {RegExp}\n *\n * @since 3.8.0\n * @since 4.6.1 added optimization to prevent backtracking on attribute parsing\n */\nconst tokenizer =\n\t/<!--\\s+(\\/)?wp:([a-z][a-z0-9_-]*\\/)?([a-z][a-z0-9_-]*)\\s+({(?:(?=([^}]+|}+(?=})|(?!}\\s+\\/?-->)[^])*)\\5|[^]*?)}\\s+)?(\\/)?-->/g;\n\n/**\n * Constructs a block object.\n *\n * @param {string|null} blockName\n * @param {Attributes} attrs\n * @param {ParsedBlock[]} innerBlocks\n * @param {string} innerHTML\n * @param {string[]} innerContent\n * @return {ParsedBlock} The block object.\n */\nfunction Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {\n\treturn {\n\t\tblockName,\n\t\tattrs,\n\t\tinnerBlocks,\n\t\tinnerHTML,\n\t\tinnerContent,\n\t};\n}\n\n/**\n * Constructs a freeform block object.\n *\n * @param {string} innerHTML\n * @return {ParsedBlock} The freeform block object.\n */\nfunction Freeform( innerHTML ) {\n\treturn Block( null, {}, [], innerHTML, [ innerHTML ] );\n}\n\n/**\n * Constructs a frame object.\n *\n * @param {ParsedBlock} block\n * @param {number} tokenStart\n * @param {number} tokenLength\n * @param {number} prevOffset\n * @param {number|null} leadingHtmlStart\n * @return {ParsedFrame} The frame object.\n */\nfunction Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {\n\treturn {\n\t\tblock,\n\t\ttokenStart,\n\t\ttokenLength,\n\t\tprevOffset: prevOffset || tokenStart + tokenLength,\n\t\tleadingHtmlStart,\n\t};\n}\n\n/**\n * Parser function, that converts input HTML into a block based structure.\n *\n * @param {string} doc The HTML document to parse.\n *\n * @example\n * Input post:\n * ```html\n * <!-- wp:columns {\"columns\":3} -->\n * <div class=\"wp-block-columns has-3-columns\"><!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p>Left</p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"><!-- wp:paragraph -->\n * <p><strong>Middle</strong></p>\n * <!-- /wp:paragraph --></div>\n * <!-- /wp:column -->\n *\n * <!-- wp:column -->\n * <div class=\"wp-block-column\"></div>\n * <!-- /wp:column --></div>\n * <!-- /wp:columns -->\n * ```\n *\n * Parsing code:\n * ```js\n * import { parse } from '@wordpress/block-serialization-default-parser';\n *\n * parse( post ) === [\n * {\n * blockName: \"core/columns\",\n * attrs: {\n * columns: 3\n * },\n * innerBlocks: [\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p>Left</p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [\n * {\n * blockName: \"core/paragraph\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: \"\\n<p><strong>Middle</strong></p>\\n\"\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * },\n * {\n * blockName: \"core/column\",\n * attrs: null,\n * innerBlocks: [],\n * innerHTML: '\\n<div class=\"wp-block-column\"></div>\\n'\n * }\n * ],\n * innerHTML: '\\n<div class=\"wp-block-columns has-3-columns\">\\n\\n\\n\\n</div>\\n'\n * }\n * ];\n * ```\n * @return {ParsedBlock[]} A block-based representation of the input HTML.\n */\nexport const parse = ( doc ) => {\n\tdocument = doc;\n\toffset = 0;\n\toutput = [];\n\tstack = [];\n\ttokenizer.lastIndex = 0;\n\n\tdo {\n\t\t// twiddle our thumbs\n\t} while ( proceed() );\n\n\treturn output;\n};\n\n/**\n * Parses the next token in the input document.\n *\n * @return {boolean} Returns true when there is more tokens to parse.\n */\nfunction proceed() {\n\tconst stackDepth = stack.length;\n\tconst next = nextToken();\n\tif ( next === null ) {\n\t\t// If not in a block then flush output.\n\t\tif ( 0 === stackDepth ) {\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Otherwise we have a problem\n\t\t// This is an error\n\t\t// we have options\n\t\t// - treat it all as freeform text\n\t\t// - assume an implicit closer (easiest when not nesting)\n\n\t\t// For the easy case we'll assume an implicit closer.\n\t\tif ( 1 === stackDepth ) {\n\t\t\taddBlockFromStack();\n\t\t\treturn false;\n\t\t}\n\n\t\t// For the nested case where it's more difficult we'll\n\t\t// have to assume that multiple closers are missing\n\t\t// and so we'll collapse the whole stack piecewise.\n\t\twhile ( 0 < stack.length ) {\n\t\t\taddBlockFromStack();\n\t\t}\n\t\treturn false;\n\t}\n\tconst [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;\n\n\t// We may have some HTML soup before the next block.\n\tconst leadingHtmlStart = startOffset > offset ? offset : null;\n\n\tswitch ( tokenType ) {\n\t\tcase 'void-block':\n\t\t\t// easy case is if we stumbled upon a void block\n\t\t\t// in the top-level of the document.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\tif ( null !== leadingHtmlStart ) {\n\t\t\t\t\toutput.push(\n\t\t\t\t\t\tFreeform(\n\t\t\t\t\t\t\tdocument.substr(\n\t\t\t\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\t\t\t\tstartOffset - leadingHtmlStart\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\toutput.push( Block( blockName, attrs, [], '', [] ) );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we found an inner block.\n\t\t\taddInnerBlock(\n\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\tstartOffset,\n\t\t\t\ttokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-opener':\n\t\t\t// Track all newly-opened blocks on the stack.\n\t\t\tstack.push(\n\t\t\t\tFrame(\n\t\t\t\t\tBlock( blockName, attrs, [], '', [] ),\n\t\t\t\t\tstartOffset,\n\t\t\t\t\ttokenLength,\n\t\t\t\t\tstartOffset + tokenLength,\n\t\t\t\t\tleadingHtmlStart\n\t\t\t\t)\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tcase 'block-closer':\n\t\t\t// If we're missing an opener we're in trouble\n\t\t\t// This is an error.\n\t\t\tif ( 0 === stackDepth ) {\n\t\t\t\t// We have options\n\t\t\t\t// - assume an implicit opener\n\t\t\t\t// - assume _this_ is the opener\n\t\t\t\t// - give up and close out the document.\n\t\t\t\taddFreeform();\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// If we're not nesting then this is easy - close the block.\n\t\t\tif ( 1 === stackDepth ) {\n\t\t\t\taddBlockFromStack( startOffset );\n\t\t\t\toffset = startOffset + tokenLength;\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t// Otherwise we're nested and we have to close out the current\n\t\t\t// block and add it as a innerBlock to the parent.\n\t\t\tconst stackTop = /** @type {ParsedFrame} */ ( stack.pop() );\n\t\t\tconst html = document.substr(\n\t\t\t\tstackTop.prevOffset,\n\t\t\t\tstartOffset - stackTop.prevOffset\n\t\t\t);\n\t\t\tstackTop.block.innerHTML += html;\n\t\t\tstackTop.block.innerContent.push( html );\n\t\t\tstackTop.prevOffset = startOffset + tokenLength;\n\n\t\t\taddInnerBlock(\n\t\t\t\tstackTop.block,\n\t\t\t\tstackTop.tokenStart,\n\t\t\t\tstackTop.tokenLength,\n\t\t\t\tstartOffset + tokenLength\n\t\t\t);\n\t\t\toffset = startOffset + tokenLength;\n\t\t\treturn true;\n\n\t\tdefault:\n\t\t\t// This is an error.\n\t\t\taddFreeform();\n\t\t\treturn false;\n\t}\n}\n\n/**\n * Parse JSON if valid, otherwise return null\n *\n * Note that JSON coming from the block comment\n * delimiters is constrained to be an object\n * and cannot be things like `true` or `null`\n *\n * @param {string} input JSON input string to parse\n * @return {Object|null} parsed JSON if valid\n */\nfunction parseJSON( input ) {\n\ttry {\n\t\treturn JSON.parse( input );\n\t} catch ( e ) {\n\t\treturn null;\n\t}\n}\n\n/**\n * Finds the next token in the document.\n *\n * @return {Token|null} The next matched token.\n */\nfunction nextToken() {\n\t// Aye the magic\n\t// we're using a single RegExp to tokenize the block comment delimiters\n\t// we're also using a trick here because the only difference between a\n\t// block opener and a block closer is the leading `/` before `wp:` (and\n\t// a closer has no attributes). we can trap them both and process the\n\t// match back in JavaScript to see which one it was.\n\tconst matches = tokenizer.exec( document );\n\n\t// We have no more tokens.\n\tif ( null === matches ) {\n\t\treturn null;\n\t}\n\n\tconst startedAt = matches.index;\n\tconst [\n\t\tmatch,\n\t\tcloserMatch,\n\t\tnamespaceMatch,\n\t\tnameMatch,\n\t\tattrsMatch /* Internal/unused. */,\n\t\t,\n\t\tvoidMatch,\n\t] = matches;\n\n\tconst length = match.length;\n\tconst isCloser = !! closerMatch;\n\tconst isVoid = !! voidMatch;\n\tconst namespace = namespaceMatch || 'core/';\n\tconst name = namespace + nameMatch;\n\tconst hasAttrs = !! attrsMatch;\n\tconst attrs = hasAttrs ? parseJSON( attrsMatch ) : {};\n\n\t// This state isn't allowed\n\t// This is an error.\n\tif ( isCloser && ( isVoid || hasAttrs ) ) {\n\t\t// We can ignore them since they don't hurt anything\n\t\t// we may warn against this at some point or reject it.\n\t}\n\n\tif ( isVoid ) {\n\t\treturn [ 'void-block', name, attrs, startedAt, length ];\n\t}\n\n\tif ( isCloser ) {\n\t\treturn [ 'block-closer', name, null, startedAt, length ];\n\t}\n\n\treturn [ 'block-opener', name, attrs, startedAt, length ];\n}\n\n/**\n * Adds a freeform block to the output.\n *\n * @param {number} [rawLength]\n */\nfunction addFreeform( rawLength ) {\n\tconst length = rawLength ? rawLength : document.length - offset;\n\n\tif ( 0 === length ) {\n\t\treturn;\n\t}\n\n\toutput.push( Freeform( document.substr( offset, length ) ) );\n}\n\n/**\n * Adds inner block to the parent block.\n *\n * @param {ParsedBlock} block\n * @param {number} tokenStart\n * @param {number} tokenLength\n * @param {number} [lastOffset]\n */\nfunction addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {\n\tconst parent = stack[ stack.length - 1 ];\n\tparent.block.innerBlocks.push( block );\n\tconst html = document.substr(\n\t\tparent.prevOffset,\n\t\ttokenStart - parent.prevOffset\n\t);\n\n\tif ( html ) {\n\t\tparent.block.innerHTML += html;\n\t\tparent.block.innerContent.push( html );\n\t}\n\n\tparent.block.innerContent.push( null );\n\tparent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;\n}\n\n/**\n * Adds block from the stack to the output.\n *\n * @param {number} [endOffset]\n */\nfunction addBlockFromStack( endOffset ) {\n\tconst { block, leadingHtmlStart, prevOffset, tokenStart } =\n\t\t/** @type {ParsedFrame} */ ( stack.pop() );\n\n\tconst html = endOffset\n\t\t? document.substr( prevOffset, endOffset - prevOffset )\n\t\t: document.substr( prevOffset );\n\n\tif ( html ) {\n\t\tblock.innerHTML += html;\n\t\tblock.innerContent.push( html );\n\t}\n\n\tif ( null !== leadingHtmlStart ) {\n\t\toutput.push(\n\t\t\tFreeform(\n\t\t\t\tdocument.substr(\n\t\t\t\t\tleadingHtmlStart,\n\t\t\t\t\ttokenStart - leadingHtmlStart\n\t\t\t\t)\n\t\t\t)\n\t\t);\n\t}\n\n\toutput.push( block );\n}\n"]}
@@ -0,0 +1,49 @@
1
+ export function parse(doc: string): ParsedBlock[];
2
+ export type Attributes = Object | null;
3
+ export type ParsedBlock = {
4
+ /**
5
+ * Block name.
6
+ */
7
+ blockName: string | null;
8
+ /**
9
+ * Block attributes.
10
+ */
11
+ attrs: Attributes;
12
+ /**
13
+ * Inner blocks.
14
+ */
15
+ innerBlocks: ParsedBlock[];
16
+ /**
17
+ * Inner HTML.
18
+ */
19
+ innerHTML: string;
20
+ /**
21
+ * Inner content.
22
+ */
23
+ innerContent: Array<string | null>;
24
+ };
25
+ export type ParsedFrame = {
26
+ /**
27
+ * Block.
28
+ */
29
+ block: ParsedBlock;
30
+ /**
31
+ * Token start.
32
+ */
33
+ tokenStart: number;
34
+ /**
35
+ * Token length.
36
+ */
37
+ tokenLength: number;
38
+ /**
39
+ * Previous offset.
40
+ */
41
+ prevOffset: number;
42
+ /**
43
+ * Leading HTML start.
44
+ */
45
+ leadingHtmlStart: number | null;
46
+ };
47
+ export type TokenType = 'void-block' | 'block-opener' | 'block-closer';
48
+ export type Token = [TokenType, string, Attributes, number, number];
49
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AA0NO,2BA1EI,MAAM,GAwEL,WAAW,EAAE,CAcxB;yBApNY,MAAM,GAAC,IAAI;;;;;eAKV,MAAM,GAAC,IAAI;;;;WACX,UAAU;;;;iBACV,WAAW,EAAE;;;;eACb,MAAM;;;;kBACN,MAAM,MAAM,GAAC,IAAI,CAAC;;;;;;WAKlB,WAAW;;;;gBACX,MAAM;;;;iBACN,MAAM;;;;gBACN,MAAM;;;;sBACN,MAAM,GAAC,IAAI;;wBAIZ,YAAY,GAAC,cAAc,GAAC,cAAc;oBAI1C,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/block-serialization-default-parser",
3
- "version": "4.16.1-next.957ca95e4c.0",
3
+ "version": "4.17.0",
4
4
  "description": "Block serialization specification parser for WordPress posts.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -25,6 +25,7 @@
25
25
  "main": "build/index.js",
26
26
  "module": "build-module/index.js",
27
27
  "react-native": "src/index",
28
+ "types": "build-types",
28
29
  "sideEffects": false,
29
30
  "dependencies": {
30
31
  "@babel/runtime": "^7.16.0"
@@ -32,5 +33,5 @@
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "272a74bbbaab10ee24424eafe9578e705fbfbbb4"
36
+ "gitHead": "0d732d4b184adcb28cc83087603e81b764390d4b"
36
37
  }
package/src/index.js CHANGED
@@ -1,8 +1,50 @@
1
+ /**
2
+ * @type {string}
3
+ */
1
4
  let document;
5
+ /**
6
+ * @type {number}
7
+ */
2
8
  let offset;
9
+ /**
10
+ * @type {ParsedBlock[]}
11
+ */
3
12
  let output;
13
+ /**
14
+ * @type {ParsedFrame[]}
15
+ */
4
16
  let stack;
5
17
 
18
+ /**
19
+ * @typedef {Object|null} Attributes
20
+ */
21
+
22
+ /**
23
+ * @typedef {Object} ParsedBlock
24
+ * @property {string|null} blockName Block name.
25
+ * @property {Attributes} attrs Block attributes.
26
+ * @property {ParsedBlock[]} innerBlocks Inner blocks.
27
+ * @property {string} innerHTML Inner HTML.
28
+ * @property {Array<string|null>} innerContent Inner content.
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} ParsedFrame
33
+ * @property {ParsedBlock} block Block.
34
+ * @property {number} tokenStart Token start.
35
+ * @property {number} tokenLength Token length.
36
+ * @property {number} prevOffset Previous offset.
37
+ * @property {number|null} leadingHtmlStart Leading HTML start.
38
+ */
39
+
40
+ /**
41
+ * @typedef {'void-block'|'block-opener'|'block-closer'} TokenType
42
+ */
43
+
44
+ /**
45
+ * @typedef {[TokenType, string, Attributes, number, number]} Token
46
+ */
47
+
6
48
  /**
7
49
  * Matches block comment delimiters
8
50
  *
@@ -47,6 +89,16 @@ let stack;
47
89
  const tokenizer =
48
90
  /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
49
91
 
92
+ /**
93
+ * Constructs a block object.
94
+ *
95
+ * @param {string|null} blockName
96
+ * @param {Attributes} attrs
97
+ * @param {ParsedBlock[]} innerBlocks
98
+ * @param {string} innerHTML
99
+ * @param {string[]} innerContent
100
+ * @return {ParsedBlock} The block object.
101
+ */
50
102
  function Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {
51
103
  return {
52
104
  blockName,
@@ -57,10 +109,26 @@ function Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {
57
109
  };
58
110
  }
59
111
 
112
+ /**
113
+ * Constructs a freeform block object.
114
+ *
115
+ * @param {string} innerHTML
116
+ * @return {ParsedBlock} The freeform block object.
117
+ */
60
118
  function Freeform( innerHTML ) {
61
119
  return Block( null, {}, [], innerHTML, [ innerHTML ] );
62
120
  }
63
121
 
122
+ /**
123
+ * Constructs a frame object.
124
+ *
125
+ * @param {ParsedBlock} block
126
+ * @param {number} tokenStart
127
+ * @param {number} tokenLength
128
+ * @param {number} prevOffset
129
+ * @param {number|null} leadingHtmlStart
130
+ * @return {ParsedFrame} The frame object.
131
+ */
64
132
  function Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {
65
133
  return {
66
134
  block,
@@ -146,7 +214,7 @@ function Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {
146
214
  * }
147
215
  * ];
148
216
  * ```
149
- * @return {Array} A block-based representation of the input HTML.
217
+ * @return {ParsedBlock[]} A block-based representation of the input HTML.
150
218
  */
151
219
  export const parse = ( doc ) => {
152
220
  document = doc;
@@ -162,42 +230,47 @@ export const parse = ( doc ) => {
162
230
  return output;
163
231
  };
164
232
 
233
+ /**
234
+ * Parses the next token in the input document.
235
+ *
236
+ * @return {boolean} Returns true when there is more tokens to parse.
237
+ */
165
238
  function proceed() {
239
+ const stackDepth = stack.length;
166
240
  const next = nextToken();
241
+ if ( next === null ) {
242
+ // If not in a block then flush output.
243
+ if ( 0 === stackDepth ) {
244
+ addFreeform();
245
+ return false;
246
+ }
247
+
248
+ // Otherwise we have a problem
249
+ // This is an error
250
+ // we have options
251
+ // - treat it all as freeform text
252
+ // - assume an implicit closer (easiest when not nesting)
253
+
254
+ // For the easy case we'll assume an implicit closer.
255
+ if ( 1 === stackDepth ) {
256
+ addBlockFromStack();
257
+ return false;
258
+ }
259
+
260
+ // For the nested case where it's more difficult we'll
261
+ // have to assume that multiple closers are missing
262
+ // and so we'll collapse the whole stack piecewise.
263
+ while ( 0 < stack.length ) {
264
+ addBlockFromStack();
265
+ }
266
+ return false;
267
+ }
167
268
  const [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;
168
- const stackDepth = stack.length;
169
269
 
170
270
  // We may have some HTML soup before the next block.
171
271
  const leadingHtmlStart = startOffset > offset ? offset : null;
172
272
 
173
273
  switch ( tokenType ) {
174
- case 'no-more-tokens':
175
- // If not in a block then flush output.
176
- if ( 0 === stackDepth ) {
177
- addFreeform();
178
- return false;
179
- }
180
-
181
- // Otherwise we have a problem
182
- // This is an error
183
- // we have options
184
- // - treat it all as freeform text
185
- // - assume an implicit closer (easiest when not nesting)
186
-
187
- // For the easy case we'll assume an implicit closer.
188
- if ( 1 === stackDepth ) {
189
- addBlockFromStack();
190
- return false;
191
- }
192
-
193
- // For the nested case where it's more difficult we'll
194
- // have to assume that multiple closers are missing
195
- // and so we'll collapse the whole stack piecewise.
196
- while ( 0 < stack.length ) {
197
- addBlockFromStack();
198
- }
199
- return false;
200
-
201
274
  case 'void-block':
202
275
  // easy case is if we stumbled upon a void block
203
276
  // in the top-level of the document.
@@ -261,7 +334,7 @@ function proceed() {
261
334
 
262
335
  // Otherwise we're nested and we have to close out the current
263
336
  // block and add it as a innerBlock to the parent.
264
- const stackTop = stack.pop();
337
+ const stackTop = /** @type {ParsedFrame} */ ( stack.pop() );
265
338
  const html = document.substr(
266
339
  stackTop.prevOffset,
267
340
  startOffset - stackTop.prevOffset
@@ -304,6 +377,11 @@ function parseJSON( input ) {
304
377
  }
305
378
  }
306
379
 
380
+ /**
381
+ * Finds the next token in the document.
382
+ *
383
+ * @return {Token|null} The next matched token.
384
+ */
307
385
  function nextToken() {
308
386
  // Aye the magic
309
387
  // we're using a single RegExp to tokenize the block comment delimiters
@@ -315,7 +393,7 @@ function nextToken() {
315
393
 
316
394
  // We have no more tokens.
317
395
  if ( null === matches ) {
318
- return [ 'no-more-tokens' ];
396
+ return null;
319
397
  }
320
398
 
321
399
  const startedAt = matches.index;
@@ -355,6 +433,11 @@ function nextToken() {
355
433
  return [ 'block-opener', name, attrs, startedAt, length ];
356
434
  }
357
435
 
436
+ /**
437
+ * Adds a freeform block to the output.
438
+ *
439
+ * @param {number} [rawLength]
440
+ */
358
441
  function addFreeform( rawLength ) {
359
442
  const length = rawLength ? rawLength : document.length - offset;
360
443
 
@@ -365,6 +448,14 @@ function addFreeform( rawLength ) {
365
448
  output.push( Freeform( document.substr( offset, length ) ) );
366
449
  }
367
450
 
451
+ /**
452
+ * Adds inner block to the parent block.
453
+ *
454
+ * @param {ParsedBlock} block
455
+ * @param {number} tokenStart
456
+ * @param {number} tokenLength
457
+ * @param {number} [lastOffset]
458
+ */
368
459
  function addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {
369
460
  const parent = stack[ stack.length - 1 ];
370
461
  parent.block.innerBlocks.push( block );
@@ -382,8 +473,14 @@ function addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {
382
473
  parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
383
474
  }
384
475
 
476
+ /**
477
+ * Adds block from the stack to the output.
478
+ *
479
+ * @param {number} [endOffset]
480
+ */
385
481
  function addBlockFromStack( endOffset ) {
386
- const { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();
482
+ const { block, leadingHtmlStart, prevOffset, tokenStart } =
483
+ /** @type {ParsedFrame} */ ( stack.pop() );
387
484
 
388
485
  const html = endOffset
389
486
  ? document.substr( prevOffset, endOffset - prevOffset )
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "declarationDir": "build-types"
6
+ },
7
+ "include": [ "src/**/*" ]
8
+ }
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../../node_modules/typescript/lib/lib.es5.d.ts","../../node_modules/typescript/lib/lib.es2015.d.ts","../../node_modules/typescript/lib/lib.es2016.d.ts","../../node_modules/typescript/lib/lib.es2017.d.ts","../../node_modules/typescript/lib/lib.es2018.d.ts","../../node_modules/typescript/lib/lib.es2019.d.ts","../../node_modules/typescript/lib/lib.es2020.d.ts","../../node_modules/typescript/lib/lib.es2021.d.ts","../../node_modules/typescript/lib/lib.esnext.d.ts","../../node_modules/typescript/lib/lib.dom.d.ts","../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../node_modules/typescript/lib/lib.es2021.promise.d.ts","../../node_modules/typescript/lib/lib.es2021.string.d.ts","../../node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../node_modules/typescript/lib/lib.esnext.intl.d.ts","./src/index.js"],"fileInfos":[{"version":"aa9fb4c70f369237c2f45f9d969c9a59e0eae9a192962eb48581fe864aa609db","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940","eb75e89d63b3b72dd9ca8b0cac801cecae5be352307c004adeaa60bc9d6df51f","2cc028cd0bdb35b1b5eb723d84666a255933fffbea607f72cbd0c7c7b4bee144",{"version":"e54c8715a4954cfdc66cd69489f2b725c09ebf37492dbd91cff0a1688b1159e8","affectsGlobalScope":true},{"version":"51b8b27c21c066bf877646e320bf6a722b80d1ade65e686923cd9d4494aef1ca","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"2c8c5ee58f30e7c944e04ab1fb5506fdbb4dd507c9efa6972cf4b91cec90c503","affectsGlobalScope":true},{"version":"2bb4b3927299434052b37851a47bf5c39764f2ba88a888a107b32262e9292b7c","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"df9c8a72ca8b0ed62f5470b41208a0587f0f73f0a7db28e5a1272cf92537518e","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"93544ca2f26a48716c1b6c5091842cad63129daac422dfa4bc52460465f22bb1","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"7435b75fdf3509622e79622dbe5091cf4b09688410ee2034e4fc17d0c99d0862","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"9f1817f7c3f02f6d56e0f403b927e90bb133f371dcebc36fa7d6d208ef6899da","affectsGlobalScope":true},{"version":"cd6efb9467a8b6338ece2e2855e37765700f2cd061ca54b01b33878cf5c7677e","affectsGlobalScope":true},{"version":"fb4416144c1bf0323ccbc9afb0ab289c07312214e8820ad17d709498c865a3fe","affectsGlobalScope":true},{"version":"5b0ca94ec819d68d33da516306c15297acec88efeb0ae9e2b39f71dbd9685ef7","affectsGlobalScope":true},{"version":"4632665b87204bb1caa8b44d165bce0c50dfab177df5b561b345a567cabacf9a","affectsGlobalScope":true},"2245e80ab3cc4cb6e496399c3b08ca3d49238819aee83a87dc3e02ca6235340f"],"options":{"allowSyntheticDefaultImports":true,"composite":true,"declaration":true,"declarationDir":"./build-types","declarationMap":true,"emitDeclarationOnly":true,"esModuleInterop":false,"importsNotUsedAsValues":2,"jsx":1,"module":99,"noFallthroughCasesInSwitch":true,"noImplicitReturns":true,"noUnusedLocals":true,"noUnusedParameters":true,"rootDir":"./src","strict":true,"target":99},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[10,12,11,2,13,14,15,16,17,18,19,20,3,4,24,21,22,23,25,26,27,5,28,29,30,31,6,32,33,34,35,7,40,36,37,38,39,8,41,42,43,1,9,44,45]},"version":"4.4.2"}