PluginProbe
Gutenberg / 23.1.1
Gutenberg v23.1.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-serialization-default-parser / index.js

index.js in Gutenberg 23.1.1, at build/scripts/block-serialization-default-parser/index.js

230 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2 var wp;
3 (wp ||= {}).blockSerializationDefaultParser = (() => {
4 var __defProp = Object.defineProperty;
5 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6 var __getOwnPropNames = Object.getOwnPropertyNames;
7 var __hasOwnProp = Object.prototype.hasOwnProperty;
8 var __export = (target, all) => {
9 for (var name in all)
10 __defProp(target, name, { get: all[name], enumerable: true });
11 };
12 var __copyProps = (to, from, except, desc) => {
13 if (from && typeof from === "object" || typeof from === "function") {
14 for (let key of __getOwnPropNames(from))
15 if (!__hasOwnProp.call(to, key) && key !== except)
16 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17 }
18 return to;
19 };
20 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
22 // packages/block-serialization-default-parser/build-module/index.mjs
23 var index_exports = {};
24 __export(index_exports, {
25 parse: () => parse
26 });
27 var document;
28 var offset;
29 var output;
30 var stack;
31 var tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;
32 function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) {
33 return {
34 blockName,
35 attrs,
36 innerBlocks,
37 innerHTML,
38 innerContent
39 };
40 }
41 function Freeform(innerHTML) {
42 return Block(null, {}, [], innerHTML, [innerHTML]);
43 }
44 function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) {
45 return {
46 block,
47 tokenStart,
48 tokenLength,
49 prevOffset: prevOffset || tokenStart + tokenLength,
50 leadingHtmlStart
51 };
52 }
53 var parse = (doc) => {
54 document = doc;
55 offset = 0;
56 output = [];
57 stack = [];
58 tokenizer.lastIndex = 0;
59 do {
60 } while (proceed());
61 return output;
62 };
63 function proceed() {
64 const stackDepth = stack.length;
65 const next = nextToken();
66 const [tokenType, blockName, attrs, startOffset, tokenLength] = next;
67 const leadingHtmlStart = startOffset > offset ? offset : null;
68 switch (tokenType) {
69 case "no-more-tokens":
70 if (0 === stackDepth) {
71 addFreeform();
72 return false;
73 }
74 if (1 === stackDepth) {
75 addBlockFromStack();
76 return false;
77 }
78 while (0 < stack.length) {
79 addBlockFromStack();
80 }
81 return false;
82 case "void-block":
83 if (0 === stackDepth) {
84 if (null !== leadingHtmlStart) {
85 output.push(
86 Freeform(
87 document.substr(
88 leadingHtmlStart,
89 startOffset - leadingHtmlStart
90 )
91 )
92 );
93 }
94 output.push(Block(blockName, attrs, [], "", []));
95 offset = startOffset + tokenLength;
96 return true;
97 }
98 addInnerBlock(
99 Block(blockName, attrs, [], "", []),
100 startOffset,
101 tokenLength
102 );
103 offset = startOffset + tokenLength;
104 return true;
105 case "block-opener":
106 stack.push(
107 Frame(
108 Block(blockName, attrs, [], "", []),
109 startOffset,
110 tokenLength,
111 startOffset + tokenLength,
112 leadingHtmlStart
113 )
114 );
115 offset = startOffset + tokenLength;
116 return true;
117 case "block-closer":
118 if (0 === stackDepth) {
119 addFreeform();
120 return false;
121 }
122 if (1 === stackDepth) {
123 addBlockFromStack(startOffset);
124 offset = startOffset + tokenLength;
125 return true;
126 }
127 const stackTop = stack.pop();
128 const html = document.substr(
129 stackTop.prevOffset,
130 startOffset - stackTop.prevOffset
131 );
132 stackTop.block.innerHTML += html;
133 stackTop.block.innerContent.push(html);
134 stackTop.prevOffset = startOffset + tokenLength;
135 addInnerBlock(
136 stackTop.block,
137 stackTop.tokenStart,
138 stackTop.tokenLength,
139 startOffset + tokenLength
140 );
141 offset = startOffset + tokenLength;
142 return true;
143 default:
144 addFreeform();
145 return false;
146 }
147 }
148 function parseJSON(input) {
149 try {
150 return JSON.parse(input);
151 } catch {
152 return null;
153 }
154 }
155 function nextToken() {
156 const matches = tokenizer.exec(document);
157 if (null === matches) {
158 return ["no-more-tokens", "", null, 0, 0];
159 }
160 const startedAt = matches.index;
161 const [
162 match,
163 closerMatch,
164 namespaceMatch,
165 nameMatch,
166 attrsMatch,
167 ,
168 voidMatch
169 ] = matches;
170 const length = match.length;
171 const isCloser = !!closerMatch;
172 const isVoid = !!voidMatch;
173 const namespace = namespaceMatch || "core/";
174 const name = namespace + nameMatch;
175 const hasAttrs = !!attrsMatch;
176 const attrs = hasAttrs ? parseJSON(attrsMatch) : {};
177 if (isCloser && (isVoid || hasAttrs)) {
178 }
179 if (isVoid) {
180 return ["void-block", name, attrs, startedAt, length];
181 }
182 if (isCloser) {
183 return ["block-closer", name, null, startedAt, length];
184 }
185 return ["block-opener", name, attrs, startedAt, length];
186 }
187 function addFreeform(rawLength) {
188 const length = rawLength ? rawLength : document.length - offset;
189 if (0 === length) {
190 return;
191 }
192 output.push(Freeform(document.substr(offset, length)));
193 }
194 function addInnerBlock(block, tokenStart, tokenLength, lastOffset) {
195 const parent = stack[stack.length - 1];
196 parent.block.innerBlocks.push(block);
197 const html = document.substr(
198 parent.prevOffset,
199 tokenStart - parent.prevOffset
200 );
201 if (html) {
202 parent.block.innerHTML += html;
203 parent.block.innerContent.push(html);
204 }
205 parent.block.innerContent.push(null);
206 parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength;
207 }
208 function addBlockFromStack(endOffset) {
209 const { block, leadingHtmlStart, prevOffset, tokenStart } = stack.pop();
210 const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset);
211 if (html) {
212 block.innerHTML += html;
213 block.innerContent.push(html);
214 }
215 if (null !== leadingHtmlStart) {
216 output.push(
217 Freeform(
218 document.substr(
219 leadingHtmlStart,
220 tokenStart - leadingHtmlStart
221 )
222 )
223 );
224 }
225 output.push(block);
226 }
227 return __toCommonJS(index_exports);
228 })();
229 //# sourceMappingURL=index.js.map
230