author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1 |
/******/ (() => { // webpackBootstrap |
19 | 2 |
/******/ "use strict"; |
3 |
/******/ // The require scope |
|
4 |
/******/ var __webpack_require__ = {}; |
|
5 |
/******/ |
|
6 |
/************************************************************************/ |
|
7 |
/******/ /* webpack/runtime/define property getters */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
8 |
/******/ (() => { |
19 | 9 |
/******/ // define getter functions for harmony exports |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
10 |
/******/ __webpack_require__.d = (exports, definition) => { |
19 | 11 |
/******/ for(var key in definition) { |
12 |
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { |
|
13 |
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); |
|
14 |
/******/ } |
|
15 |
/******/ } |
|
9 | 16 |
/******/ }; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
/******/ })(); |
19 | 18 |
/******/ |
19 |
/******/ /* webpack/runtime/hasOwnProperty shorthand */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
/******/ (() => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
/******/ })(); |
19 | 23 |
/******/ |
24 |
/******/ /* webpack/runtime/make namespace object */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
/******/ (() => { |
19 | 26 |
/******/ // define __esModule on exports |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
/******/ __webpack_require__.r = (exports) => { |
19 | 28 |
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { |
29 |
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); |
|
30 |
/******/ } |
|
31 |
/******/ Object.defineProperty(exports, '__esModule', { value: true }); |
|
32 |
/******/ }; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
/******/ })(); |
19 | 34 |
/******/ |
9 | 35 |
/************************************************************************/ |
19 | 36 |
var __webpack_exports__ = {}; |
9 | 37 |
__webpack_require__.r(__webpack_exports__); |
19 | 38 |
/* harmony export */ __webpack_require__.d(__webpack_exports__, { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
/* harmony export */ parse: () => (/* binding */ parse) |
19 | 40 |
/* harmony export */ }); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
41 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
* @type {string} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
*/ |
18 | 44 |
let document; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
46 |
* @type {number} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
*/ |
18 | 48 |
let offset; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
* @type {ParsedBlock[]} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
*/ |
18 | 52 |
let output; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
* @type {ParsedFrame[]} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
*/ |
18 | 56 |
let stack; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
* @typedef {Object|null} Attributes |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
60 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
* @typedef {Object} ParsedBlock |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
* @property {string|null} blockName Block name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
65 |
* @property {Attributes} attrs Block attributes. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
* @property {ParsedBlock[]} innerBlocks Inner blocks. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
* @property {string} innerHTML Inner HTML. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
* @property {Array<string|null>} innerContent Inner content. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
* @typedef {Object} ParsedFrame |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
* @property {ParsedBlock} block Block. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
* @property {number} tokenStart Token start. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
* @property {number} tokenLength Token length. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
* @property {number} prevOffset Previous offset. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
* @property {number|null} leadingHtmlStart Leading HTML start. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
* @typedef {'no-more-tokens'|'void-block'|'block-opener'|'block-closer'} TokenType |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
* @typedef {[TokenType, string, Attributes, number, number]} Token |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
|
9 | 88 |
/** |
89 |
* Matches block comment delimiters |
|
90 |
* |
|
91 |
* While most of this pattern is straightforward the attribute parsing |
|
92 |
* incorporates a tricks to make sure we don't choke on specific input |
|
93 |
* |
|
94 |
* - since JavaScript has no possessive quantifier or atomic grouping |
|
95 |
* we are emulating it with a trick |
|
96 |
* |
|
97 |
* we want a possessive quantifier or atomic group to prevent backtracking |
|
98 |
* on the `}`s should we fail to match the remainder of the pattern |
|
99 |
* |
|
100 |
* we can emulate this with a positive lookahead and back reference |
|
101 |
* (a++)*c === ((?=(a+))\1)*c |
|
102 |
* |
|
103 |
* let's examine an example: |
|
104 |
* - /(a+)*c/.test('aaaaaaaaaaaaad') fails after over 49,000 steps |
|
105 |
* - /(a++)*c/.test('aaaaaaaaaaaaad') fails after 85 steps |
|
106 |
* - /(?>a+)*c/.test('aaaaaaaaaaaaad') fails after 126 steps |
|
107 |
* |
|
108 |
* this is because the possessive `++` and the atomic group `(?>)` |
|
109 |
* tell the engine that all those `a`s belong together as a single group |
|
110 |
* and so it won't split it up when stepping backwards to try and match |
|
111 |
* |
|
112 |
* if we use /((?=(a+))\1)*c/ then we get the same behavior as the atomic group |
|
113 |
* or possessive and prevent the backtracking because the `a+` is matched but |
|
114 |
* not captured. thus, we find the long string of `a`s and remember it, then |
|
115 |
* reference it as a whole unit inside our pattern |
|
116 |
* |
|
16 | 117 |
* @see http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead |
118 |
* @see http://blog.stevenlevithan.com/archives/mimic-atomic-groups |
|
119 |
* @see https://javascript.info/regexp-infinite-backtracking-problem |
|
9 | 120 |
* |
121 |
* once browsers reliably support atomic grouping or possessive |
|
122 |
* quantifiers natively we should remove this trick and simplify |
|
123 |
* |
|
16 | 124 |
* @type {RegExp} |
9 | 125 |
* |
126 |
* @since 3.8.0 |
|
127 |
* @since 4.6.1 added optimization to prevent backtracking on attribute parsing |
|
128 |
*/ |
|
18 | 129 |
const tokenizer = /<!--\s+(\/)?wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g; |
9 | 130 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
* Constructs a block object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
* @param {string|null} blockName |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
* @param {Attributes} attrs |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
* @param {ParsedBlock[]} innerBlocks |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
* @param {string} innerHTML |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
* @param {string[]} innerContent |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
* @return {ParsedBlock} The block object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
*/ |
9 | 141 |
function Block(blockName, attrs, innerBlocks, innerHTML, innerContent) { |
142 |
return { |
|
18 | 143 |
blockName, |
144 |
attrs, |
|
145 |
innerBlocks, |
|
146 |
innerHTML, |
|
147 |
innerContent |
|
9 | 148 |
}; |
149 |
} |
|
150 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
* Constructs a freeform block object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
* @param {string} innerHTML |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
* @return {ParsedBlock} The freeform block object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
*/ |
9 | 157 |
function Freeform(innerHTML) { |
158 |
return Block(null, {}, [], innerHTML, [innerHTML]); |
|
159 |
} |
|
160 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
* Constructs a frame object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
* @param {ParsedBlock} block |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
* @param {number} tokenStart |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
* @param {number} tokenLength |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
* @param {number} prevOffset |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
* @param {number|null} leadingHtmlStart |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
* @return {ParsedFrame} The frame object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
*/ |
9 | 171 |
function Frame(block, tokenStart, tokenLength, prevOffset, leadingHtmlStart) { |
172 |
return { |
|
18 | 173 |
block, |
174 |
tokenStart, |
|
175 |
tokenLength, |
|
9 | 176 |
prevOffset: prevOffset || tokenStart + tokenLength, |
18 | 177 |
leadingHtmlStart |
9 | 178 |
}; |
179 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
180 |
|
9 | 181 |
/** |
182 |
* Parser function, that converts input HTML into a block based structure. |
|
183 |
* |
|
184 |
* @param {string} doc The HTML document to parse. |
|
185 |
* |
|
186 |
* @example |
|
187 |
* Input post: |
|
188 |
* ```html |
|
189 |
* <!-- wp:columns {"columns":3} --> |
|
190 |
* <div class="wp-block-columns has-3-columns"><!-- wp:column --> |
|
191 |
* <div class="wp-block-column"><!-- wp:paragraph --> |
|
192 |
* <p>Left</p> |
|
193 |
* <!-- /wp:paragraph --></div> |
|
194 |
* <!-- /wp:column --> |
|
195 |
* |
|
196 |
* <!-- wp:column --> |
|
197 |
* <div class="wp-block-column"><!-- wp:paragraph --> |
|
198 |
* <p><strong>Middle</strong></p> |
|
199 |
* <!-- /wp:paragraph --></div> |
|
200 |
* <!-- /wp:column --> |
|
201 |
* |
|
202 |
* <!-- wp:column --> |
|
203 |
* <div class="wp-block-column"></div> |
|
204 |
* <!-- /wp:column --></div> |
|
205 |
* <!-- /wp:columns --> |
|
206 |
* ``` |
|
207 |
* |
|
208 |
* Parsing code: |
|
209 |
* ```js |
|
210 |
* import { parse } from '@wordpress/block-serialization-default-parser'; |
|
211 |
* |
|
212 |
* parse( post ) === [ |
|
213 |
* { |
|
214 |
* blockName: "core/columns", |
|
215 |
* attrs: { |
|
216 |
* columns: 3 |
|
217 |
* }, |
|
218 |
* innerBlocks: [ |
|
219 |
* { |
|
220 |
* blockName: "core/column", |
|
221 |
* attrs: null, |
|
222 |
* innerBlocks: [ |
|
223 |
* { |
|
224 |
* blockName: "core/paragraph", |
|
225 |
* attrs: null, |
|
226 |
* innerBlocks: [], |
|
227 |
* innerHTML: "\n<p>Left</p>\n" |
|
228 |
* } |
|
229 |
* ], |
|
230 |
* innerHTML: '\n<div class="wp-block-column"></div>\n' |
|
231 |
* }, |
|
232 |
* { |
|
233 |
* blockName: "core/column", |
|
234 |
* attrs: null, |
|
235 |
* innerBlocks: [ |
|
236 |
* { |
|
237 |
* blockName: "core/paragraph", |
|
238 |
* attrs: null, |
|
239 |
* innerBlocks: [], |
|
240 |
* innerHTML: "\n<p><strong>Middle</strong></p>\n" |
|
241 |
* } |
|
242 |
* ], |
|
243 |
* innerHTML: '\n<div class="wp-block-column"></div>\n' |
|
244 |
* }, |
|
245 |
* { |
|
246 |
* blockName: "core/column", |
|
247 |
* attrs: null, |
|
248 |
* innerBlocks: [], |
|
249 |
* innerHTML: '\n<div class="wp-block-column"></div>\n' |
|
250 |
* } |
|
251 |
* ], |
|
252 |
* innerHTML: '\n<div class="wp-block-columns has-3-columns">\n\n\n\n</div>\n' |
|
253 |
* } |
|
254 |
* ]; |
|
255 |
* ``` |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
256 |
* @return {ParsedBlock[]} A block-based representation of the input HTML. |
9 | 257 |
*/ |
18 | 258 |
const parse = doc => { |
9 | 259 |
document = doc; |
260 |
offset = 0; |
|
261 |
output = []; |
|
262 |
stack = []; |
|
263 |
tokenizer.lastIndex = 0; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
264 |
do { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
265 |
// twiddle our thumbs |
9 | 266 |
} while (proceed()); |
267 |
return output; |
|
268 |
}; |
|
269 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
270 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
271 |
* Parses the next token in the input document. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
272 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
273 |
* @return {boolean} Returns true when there is more tokens to parse. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
274 |
*/ |
9 | 275 |
function proceed() { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
276 |
const stackDepth = stack.length; |
18 | 277 |
const next = nextToken(); |
278 |
const [tokenType, blockName, attrs, startOffset, tokenLength] = next; |
|
9 | 279 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
280 |
// We may have some HTML soup before the next block. |
18 | 281 |
const leadingHtmlStart = startOffset > offset ? offset : null; |
9 | 282 |
switch (tokenType) { |
283 |
case 'no-more-tokens': |
|
19 | 284 |
// If not in a block then flush output. |
9 | 285 |
if (0 === stackDepth) { |
286 |
addFreeform(); |
|
287 |
return false; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
290 |
// Otherwise we have a problem |
9 | 291 |
// This is an error |
292 |
// we have options |
|
293 |
// - treat it all as freeform text |
|
294 |
// - assume an implicit closer (easiest when not nesting) |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
295 |
|
19 | 296 |
// For the easy case we'll assume an implicit closer. |
9 | 297 |
if (1 === stackDepth) { |
298 |
addBlockFromStack(); |
|
299 |
return false; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
300 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
301 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
302 |
// For the nested case where it's more difficult we'll |
9 | 303 |
// have to assume that multiple closers are missing |
19 | 304 |
// and so we'll collapse the whole stack piecewise. |
9 | 305 |
while (0 < stack.length) { |
306 |
addBlockFromStack(); |
|
307 |
} |
|
308 |
return false; |
|
309 |
case 'void-block': |
|
310 |
// easy case is if we stumbled upon a void block |
|
19 | 311 |
// in the top-level of the document. |
9 | 312 |
if (0 === stackDepth) { |
313 |
if (null !== leadingHtmlStart) { |
|
314 |
output.push(Freeform(document.substr(leadingHtmlStart, startOffset - leadingHtmlStart))); |
|
315 |
} |
|
316 |
output.push(Block(blockName, attrs, [], '', [])); |
|
317 |
offset = startOffset + tokenLength; |
|
318 |
return true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
319 |
} |
9 | 320 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
321 |
// Otherwise we found an inner block. |
9 | 322 |
addInnerBlock(Block(blockName, attrs, [], '', []), startOffset, tokenLength); |
323 |
offset = startOffset + tokenLength; |
|
324 |
return true; |
|
325 |
case 'block-opener': |
|
19 | 326 |
// Track all newly-opened blocks on the stack. |
9 | 327 |
stack.push(Frame(Block(blockName, attrs, [], '', []), startOffset, tokenLength, startOffset + tokenLength, leadingHtmlStart)); |
328 |
offset = startOffset + tokenLength; |
|
329 |
return true; |
|
330 |
case 'block-closer': |
|
19 | 331 |
// If we're missing an opener we're in trouble |
332 |
// This is an error. |
|
9 | 333 |
if (0 === stackDepth) { |
19 | 334 |
// We have options |
9 | 335 |
// - assume an implicit opener |
336 |
// - assume _this_ is the opener |
|
19 | 337 |
// - give up and close out the document. |
9 | 338 |
addFreeform(); |
339 |
return false; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
340 |
} |
9 | 341 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
342 |
// If we're not nesting then this is easy - close the block. |
9 | 343 |
if (1 === stackDepth) { |
344 |
addBlockFromStack(startOffset); |
|
345 |
offset = startOffset + tokenLength; |
|
346 |
return true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
347 |
} |
9 | 348 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
// Otherwise we're nested and we have to close out the current |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
// block and add it as a innerBlock to the parent. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
351 |
const stackTop = /** @type {ParsedFrame} */stack.pop(); |
18 | 352 |
const html = document.substr(stackTop.prevOffset, startOffset - stackTop.prevOffset); |
9 | 353 |
stackTop.block.innerHTML += html; |
354 |
stackTop.block.innerContent.push(html); |
|
355 |
stackTop.prevOffset = startOffset + tokenLength; |
|
356 |
addInnerBlock(stackTop.block, stackTop.tokenStart, stackTop.tokenLength, startOffset + tokenLength); |
|
357 |
offset = startOffset + tokenLength; |
|
358 |
return true; |
|
359 |
default: |
|
19 | 360 |
// This is an error. |
9 | 361 |
addFreeform(); |
362 |
return false; |
|
363 |
} |
|
364 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
365 |
|
9 | 366 |
/** |
367 |
* Parse JSON if valid, otherwise return null |
|
368 |
* |
|
369 |
* Note that JSON coming from the block comment |
|
370 |
* delimiters is constrained to be an object |
|
371 |
* and cannot be things like `true` or `null` |
|
372 |
* |
|
373 |
* @param {string} input JSON input string to parse |
|
374 |
* @return {Object|null} parsed JSON if valid |
|
375 |
*/ |
|
376 |
function parseJSON(input) { |
|
377 |
try { |
|
378 |
return JSON.parse(input); |
|
379 |
} catch (e) { |
|
380 |
return null; |
|
381 |
} |
|
382 |
} |
|
383 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
384 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
385 |
* Finds the next token in the document. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
386 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
* @return {Token} The next matched token. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
388 |
*/ |
9 | 389 |
function nextToken() { |
19 | 390 |
// Aye the magic |
9 | 391 |
// we're using a single RegExp to tokenize the block comment delimiters |
392 |
// we're also using a trick here because the only difference between a |
|
393 |
// block opener and a block closer is the leading `/` before `wp:` (and |
|
394 |
// a closer has no attributes). we can trap them both and process the |
|
18 | 395 |
// match back in JavaScript to see which one it was. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
396 |
const matches = tokenizer.exec(document); |
9 | 397 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
398 |
// We have no more tokens. |
9 | 399 |
if (null === matches) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
400 |
return ['no-more-tokens', '', null, 0, 0]; |
9 | 401 |
} |
18 | 402 |
const startedAt = matches.index; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
403 |
const [match, closerMatch, namespaceMatch, nameMatch, attrsMatch /* Internal/unused. */,, voidMatch] = matches; |
18 | 404 |
const length = match.length; |
405 |
const isCloser = !!closerMatch; |
|
406 |
const isVoid = !!voidMatch; |
|
407 |
const namespace = namespaceMatch || 'core/'; |
|
408 |
const name = namespace + nameMatch; |
|
409 |
const hasAttrs = !!attrsMatch; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
410 |
const attrs = hasAttrs ? parseJSON(attrsMatch) : {}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
411 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
412 |
// This state isn't allowed |
19 | 413 |
// This is an error. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
414 |
if (isCloser && (isVoid || hasAttrs)) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
415 |
// We can ignore them since they don't hurt anything |
19 | 416 |
// we may warn against this at some point or reject it. |
9 | 417 |
} |
418 |
if (isVoid) { |
|
419 |
return ['void-block', name, attrs, startedAt, length]; |
|
420 |
} |
|
421 |
if (isCloser) { |
|
422 |
return ['block-closer', name, null, startedAt, length]; |
|
423 |
} |
|
424 |
return ['block-opener', name, attrs, startedAt, length]; |
|
425 |
} |
|
426 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
427 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
428 |
* Adds a freeform block to the output. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
429 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
430 |
* @param {number} [rawLength] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
431 |
*/ |
9 | 432 |
function addFreeform(rawLength) { |
18 | 433 |
const length = rawLength ? rawLength : document.length - offset; |
9 | 434 |
if (0 === length) { |
435 |
return; |
|
436 |
} |
|
437 |
output.push(Freeform(document.substr(offset, length))); |
|
438 |
} |
|
439 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
440 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
441 |
* Adds inner block to the parent block. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
442 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
443 |
* @param {ParsedBlock} block |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
444 |
* @param {number} tokenStart |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
445 |
* @param {number} tokenLength |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
446 |
* @param {number} [lastOffset] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
447 |
*/ |
9 | 448 |
function addInnerBlock(block, tokenStart, tokenLength, lastOffset) { |
18 | 449 |
const parent = stack[stack.length - 1]; |
9 | 450 |
parent.block.innerBlocks.push(block); |
18 | 451 |
const html = document.substr(parent.prevOffset, tokenStart - parent.prevOffset); |
9 | 452 |
if (html) { |
453 |
parent.block.innerHTML += html; |
|
454 |
parent.block.innerContent.push(html); |
|
455 |
} |
|
456 |
parent.block.innerContent.push(null); |
|
457 |
parent.prevOffset = lastOffset ? lastOffset : tokenStart + tokenLength; |
|
458 |
} |
|
459 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
460 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
461 |
* Adds block from the stack to the output. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
463 |
* @param {number} [endOffset] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
464 |
*/ |
9 | 465 |
function addBlockFromStack(endOffset) { |
18 | 466 |
const { |
467 |
block, |
|
468 |
leadingHtmlStart, |
|
469 |
prevOffset, |
|
470 |
tokenStart |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
471 |
} = /** @type {ParsedFrame} */stack.pop(); |
18 | 472 |
const html = endOffset ? document.substr(prevOffset, endOffset - prevOffset) : document.substr(prevOffset); |
9 | 473 |
if (html) { |
474 |
block.innerHTML += html; |
|
475 |
block.innerContent.push(html); |
|
476 |
} |
|
477 |
if (null !== leadingHtmlStart) { |
|
478 |
output.push(Freeform(document.substr(leadingHtmlStart, tokenStart - leadingHtmlStart))); |
|
479 |
} |
|
480 |
output.push(block); |
|
481 |
} |
|
482 |
||
19 | 483 |
(window.wp = window.wp || {}).blockSerializationDefaultParser = __webpack_exports__; |
484 |
/******/ })() |
|
485 |
; |