dsh-design-studio 1.3.0 → 1.4.1
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/README.en.md +1 -0
- package/README.md +1 -0
- package/lib/bundle.js +13 -8
- package/lib/canvthumb.js +2 -2
- package/lib/client.js +109 -34
- package/lib/comp-io.js +248 -0
- package/lib/components.js +716 -0
- package/lib/core.js +31 -8
- package/lib/draft-page.js +2 -1
- package/lib/gallery/routes.js +15 -1
- package/lib/handoff.js +297 -62
- package/lib/history.js +6 -1
- package/lib/htmlpack.js +110 -4
- package/lib/index.js +164 -38
- package/lib/picker.js +23 -5
- package/lib/serve.js +64 -11
- package/lib/tunnel.js +65 -30
- package/package.json +1 -1
- package/skills/ui-design/SKILL.md +15 -1
- package/skills/ui-design/references/components.md +123 -0
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
import { tokenize, decodeEntities, lineAt } from './gallery/html.js'
|
|
2
|
+
import { cssTokens } from './gallery/css.js'
|
|
3
|
+
import { mockExpr } from './mock.js'
|
|
4
|
+
|
|
5
|
+
export const COMP_DIR = 'components'
|
|
6
|
+
export const COMP_NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/
|
|
7
|
+
export const LIMITS = Object.freeze({ depth: 8, file: 256 * 1024, out: 4 * 1024 * 1024, instances: 5000 })
|
|
8
|
+
|
|
9
|
+
const X = mockExpr()
|
|
10
|
+
const VOID = new Set('area base br col embed hr img input link meta param source track wbr'.split(' '))
|
|
11
|
+
const PASS = new Set(('id hidden role tabindex title data-back data-href data-to data-mock-show data-mock-class ' +
|
|
12
|
+
'data-mock-set data-mock-attr data-mock-reset class style').split(' '))
|
|
13
|
+
const NO_PASS = new Set(['data-mock-text', 'data-mock-each', 'data-mock-model'])
|
|
14
|
+
const PARAM = /^[\p{L}_$][\p{L}\p{N}_$-]*$/u
|
|
15
|
+
const fileOf = name => `${COMP_DIR}/${name}.html`
|
|
16
|
+
const attr = (t, name) => t.tag.attrs.find(a => a.name === name)
|
|
17
|
+
function escaped(s, attribute) {
|
|
18
|
+
const out = new Output()
|
|
19
|
+
for (let i = 0; i < s.length; i += 8192) {
|
|
20
|
+
const chunk = s.slice(i, i + 8192).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>')
|
|
21
|
+
out.write(attribute ? chunk.replaceAll('"', '"') : chunk)
|
|
22
|
+
}
|
|
23
|
+
return out.string()
|
|
24
|
+
}
|
|
25
|
+
const escapeText = s => escaped(s, false)
|
|
26
|
+
const escapeAttr = s => escaped(s, true)
|
|
27
|
+
|
|
28
|
+
export function isComponentRel(rel) {
|
|
29
|
+
if (typeof rel !== 'string') return false
|
|
30
|
+
const parts = rel.replaceAll('\\', '/').split('/')
|
|
31
|
+
return parts.length === 2 && parts[0] === COMP_DIR && parts[1].endsWith('.html') && COMP_NAME.test(parts[1].slice(0, -5))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function pageTokens(html) {
|
|
35
|
+
if (!/<c-/i.test(html)) return null
|
|
36
|
+
const tokens = tokenize(html)
|
|
37
|
+
let used = false
|
|
38
|
+
for (const t of tokens) {
|
|
39
|
+
if (t.type === 'tag' && t.tag.name.startsWith('c-')) used = true
|
|
40
|
+
if (t.raw === 'script' && /\bcustomElements\s*\.\s*define\s*\(\s*['"]c-/i.test(t.text)) return null
|
|
41
|
+
}
|
|
42
|
+
return used ? tokens : null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function usesComponents(html) {
|
|
46
|
+
return pageTokens(html) !== null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const fixes = {
|
|
50
|
+
name: '组件名用小写字母开头,只用小写字母、数字和分隔用的短横线。',
|
|
51
|
+
syntax: '检查标签、参数和 mock 表达式的写法。',
|
|
52
|
+
missing: '在设计根目录的 components 文件夹补上同名 HTML 文件。',
|
|
53
|
+
required: '在调用标签上传入这个参数,或在组件里给它写默认值。',
|
|
54
|
+
default: '同一参数只保留一个统一的默认值。',
|
|
55
|
+
alias: '改组件内部列表的别名或下标名,避开传入路径的首段。',
|
|
56
|
+
pass: '把这个绑定写到组件内部对应的普通元素上。',
|
|
57
|
+
state: '用 data-status 或 data-variant 描述组件状态。',
|
|
58
|
+
script: '删除组件里的脚本、iframe 或事件属性,用 data-mock-* 写交互。',
|
|
59
|
+
whole: '只保留组件的根元素和可选的样式块。',
|
|
60
|
+
root: '把组件内容包在一个根元素里,把样式块放在根元素旁边。',
|
|
61
|
+
slot: '组件内只留一个不带 name 属性的 slot。',
|
|
62
|
+
conflict: '删除调用标签或组件根元素上的重复属性。',
|
|
63
|
+
'style-param': '用 class、data-status 或 data-variant 切换样式。',
|
|
64
|
+
forward: '动态参数单独写成 {{参数}},把前后文字移到接收它的组件里。',
|
|
65
|
+
cycle: '去掉这条循环引用,把共用内容拆成独立组件。',
|
|
66
|
+
depth: '减少嵌套层数,每条调用链最多八层。',
|
|
67
|
+
big: '拆小组件或页面,减少重复的内容和组件实例。',
|
|
68
|
+
'mock-path': '核对路径的首段、列表别名或 mock.json 的顶层字段。',
|
|
69
|
+
'unknown-attr': '核对参数名,或在组件中声明这个参数。',
|
|
70
|
+
'css-scope': '让每个选择器都以组件根元素的某个 class 开头。',
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class Problem extends Error {
|
|
74
|
+
constructor(code, message, doc, pos) {
|
|
75
|
+
super(message)
|
|
76
|
+
this.code = code
|
|
77
|
+
this.doc = doc
|
|
78
|
+
this.pos = pos
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function fail(code, message, doc, pos) { throw new Problem(code, message, doc, pos) }
|
|
82
|
+
function diag(problem, page, doc, pos) {
|
|
83
|
+
const at = problem.doc || doc
|
|
84
|
+
return { code: problem.code, message: problem.message, fix: fixes[problem.code], page,
|
|
85
|
+
file: at.file, line: lineAt(at.src, problem.pos ?? pos) }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function pathBinding(src, doc, pos) {
|
|
89
|
+
try {
|
|
90
|
+
const ast = X.parse(src)
|
|
91
|
+
if (X.lex(src).some(t => t.k === 'op' && ['(', ')'].includes(t.v))) throw new Error()
|
|
92
|
+
if (ast.t !== 'path' || ast.segs.some(s => s.t !== 'lit' ||
|
|
93
|
+
!['number', 'string'].includes(typeof s.v) || X.BAD.has(String(s.v)))) throw new Error()
|
|
94
|
+
return { path: src.trim(), root: ast.segs[0].v }
|
|
95
|
+
} catch { fail('syntax', `「${src}」要写成数据路径,例如 user.stage 或 list[0].x`, doc, pos) }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Each 的别名只在直接子 template 的内容里可见。先给 token 配对,之后可跳过整个实例或假分支。
|
|
99
|
+
function indexDoc(src, file, tokens = tokenize(src)) {
|
|
100
|
+
const doc = { src, file, tokens }
|
|
101
|
+
const open = [], last = new Map()
|
|
102
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
103
|
+
const t = tokens[i]
|
|
104
|
+
t.index = i
|
|
105
|
+
const parent = open.at(-1)
|
|
106
|
+
t.parent = parent
|
|
107
|
+
t.scope = parent?.childScope
|
|
108
|
+
if (t.type === 'tag') {
|
|
109
|
+
t.after = i + 1
|
|
110
|
+
t.close = i + 1
|
|
111
|
+
t.childScope = t.scope
|
|
112
|
+
const name = t.tag.name
|
|
113
|
+
if (name === 'template' && parent?.each) t.childScope = { ...parent.each, outer: t.scope }
|
|
114
|
+
const each = attr(t, 'data-mock-each')
|
|
115
|
+
if (each) {
|
|
116
|
+
try { t.each = X.parseEach(each.value) } catch { /* 组件里的坏绑定在分析定义时报错;页面交给已有体检 */ }
|
|
117
|
+
}
|
|
118
|
+
if (name.startsWith('c-')) t.component = src.slice(t.start + 3, t.start + 1 + name.length)
|
|
119
|
+
if (!t.tag.selfClosing && !VOID.has(name)) {
|
|
120
|
+
t.previous = last.get(name)
|
|
121
|
+
last.set(name, open.length)
|
|
122
|
+
t.after = t.close = tokens.length
|
|
123
|
+
open.push(t)
|
|
124
|
+
}
|
|
125
|
+
} else if (t.type === 'endtag' && last.has(t.tag.name)) {
|
|
126
|
+
const depth = last.get(t.tag.name)
|
|
127
|
+
while (open.length > depth) {
|
|
128
|
+
const start = open.pop()
|
|
129
|
+
const matched = start.tag.name === t.tag.name
|
|
130
|
+
start.close = i
|
|
131
|
+
start.after = matched ? i + 1 : i
|
|
132
|
+
start.matched = matched
|
|
133
|
+
if (matched) t.openTag = start
|
|
134
|
+
if (start.previous === undefined) last.delete(start.tag.name)
|
|
135
|
+
else last.set(start.tag.name, start.previous)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return doc
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function inScope(scope, name) {
|
|
143
|
+
for (; scope; scope = scope.outer) if (scope.item === name || scope.index === name) return true
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function declare(def, name, fallback, required, pos) {
|
|
148
|
+
const key = name.toLowerCase()
|
|
149
|
+
if (!PARAM.test(name)) fail('syntax', `参数名「${name}」写法不对`, def, pos)
|
|
150
|
+
let p = def.params.get(key)
|
|
151
|
+
if (!p) { p = { name: key, required: false, def: null }; def.params.set(key, p) }
|
|
152
|
+
if (fallback !== null) {
|
|
153
|
+
if (p.def !== null && p.def !== fallback) fail('default', `参数 ${name} 写了不同的默认值`, def, pos)
|
|
154
|
+
p.def = fallback
|
|
155
|
+
}
|
|
156
|
+
p.required = p.def === null && (required || p.required)
|
|
157
|
+
return key
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 字面片段保留原文,参数值只在调用点解码一次,写入时再按文字 / 属性转义。
|
|
161
|
+
function pieces(raw, def, pos, inAttr) {
|
|
162
|
+
if (!raw.includes('{{')) return null
|
|
163
|
+
const out = []
|
|
164
|
+
let from = 0
|
|
165
|
+
for (let i = raw.indexOf('{{'); i >= 0; i = raw.indexOf('{{', from)) {
|
|
166
|
+
if (from < i) out.push(raw.slice(from, i))
|
|
167
|
+
const end = raw.indexOf('}}', i + 2)
|
|
168
|
+
if (end < 0) fail('syntax', '参数少了结束的 }}', def, pos + i)
|
|
169
|
+
const spec = raw.slice(i + 2, end)
|
|
170
|
+
const bar = spec.indexOf('|')
|
|
171
|
+
const name = (bar < 0 ? spec : spec.slice(0, bar)).trim()
|
|
172
|
+
const fallback = bar < 0 ? null : decodeEntities(spec.slice(bar + 1).trim(), inAttr).text
|
|
173
|
+
out.push({ key: declare(def, name, fallback, true, pos + i) })
|
|
174
|
+
from = end + 2
|
|
175
|
+
}
|
|
176
|
+
if (from < raw.length) out.push(raw.slice(from))
|
|
177
|
+
return out
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function collectPaths(ast, paths) {
|
|
181
|
+
if (!ast) return
|
|
182
|
+
if (ast.t === 'path') {
|
|
183
|
+
let path = String(ast.segs[0].v)
|
|
184
|
+
for (const seg of ast.segs.slice(1)) {
|
|
185
|
+
if (seg.t !== 'lit') { collectPaths(seg, paths); break }
|
|
186
|
+
path += typeof seg.v === 'number' ? `[${seg.v}]` : '.' + seg.v
|
|
187
|
+
}
|
|
188
|
+
paths.add(path)
|
|
189
|
+
} else for (const key of ['a', 'b', 'c']) collectPaths(ast[key], paths)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function inspectMock(a, doc, paths) {
|
|
193
|
+
try {
|
|
194
|
+
switch (a.name) {
|
|
195
|
+
case 'data-mock-class':
|
|
196
|
+
case 'data-mock-attr':
|
|
197
|
+
for (const pair of X.parsePairs(a.value)) {
|
|
198
|
+
if (a.name === 'data-mock-attr' && /^on/i.test(pair.name)) fail('script', `不能绑定事件属性 ${pair.name}`, doc, a.start)
|
|
199
|
+
if (a.name === 'data-mock-attr' && /^style$/i.test(pair.name)) fail('style-param', '不能用 data-mock-attr 绑定 style', doc, a.start)
|
|
200
|
+
if (paths) collectPaths(pair.e, paths)
|
|
201
|
+
}
|
|
202
|
+
break
|
|
203
|
+
case 'data-mock-each': collectPaths(X.parseEach(a.value).list, paths); break
|
|
204
|
+
case 'data-mock-set': for (const s of X.parseStmts(a.value)) collectPaths(s.rhs, paths); break
|
|
205
|
+
case 'data-mock':
|
|
206
|
+
case 'data-mock-text':
|
|
207
|
+
case 'data-mock-model':
|
|
208
|
+
case 'data-mock-show': collectPaths(X.parse(a.value), paths); break
|
|
209
|
+
}
|
|
210
|
+
} catch (e) {
|
|
211
|
+
if (e instanceof Problem) throw e
|
|
212
|
+
fail('syntax', `${a.name}="${a.value}":${e.message}`, doc, a.start)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 用 CSS 分词区分分组规则、选择器列表、属性选择器和声明中的字符串。
|
|
217
|
+
function inspectCss(def, raw, offset) {
|
|
218
|
+
const classes = new Set((attr(def.root, 'class')?.value || '').split(/\s+/))
|
|
219
|
+
const ts = cssTokens(raw).filter(t => !['ws', 'comment'].includes(t.t))
|
|
220
|
+
const groups = [true]
|
|
221
|
+
let start = 0, parens = 0
|
|
222
|
+
for (let i = 0; i < ts.length; i++) {
|
|
223
|
+
const t = ts[i]
|
|
224
|
+
if (t.t === '[' && ts[i+2]?.v === '=' && ts[i+4]?.t === ']') {
|
|
225
|
+
const key = ts[i+1].v.toLowerCase()
|
|
226
|
+
if (key === 'data-status' || key === 'data-variant') def.values[key].add(ts[i+3].v)
|
|
227
|
+
}
|
|
228
|
+
if (['func', '(', '['].includes(t.t)) parens++
|
|
229
|
+
if ([')', ']'].includes(t.t)) parens--
|
|
230
|
+
if (parens) continue
|
|
231
|
+
if (t.t === '{') {
|
|
232
|
+
const first = ts[start]
|
|
233
|
+
const group = first?.t === 'at' && /^(media|supports|layer|container|scope|starting-style)$/i.test(first.v)
|
|
234
|
+
if (groups.at(-1) && first?.t !== 'at') {
|
|
235
|
+
let depth = 0, selector = start
|
|
236
|
+
for (let j = start; j <= i; j++) {
|
|
237
|
+
const k = ts[j].t
|
|
238
|
+
if ((j === i || k === ',') && !depth) {
|
|
239
|
+
if (ts[selector]?.v !== '.' || !classes.has(ts[selector+1]?.v)) {
|
|
240
|
+
def.warnings.push(new Problem('css-scope', `选择器「${raw.slice(ts[selector].s, ts[j].s).trim()}」没有从根元素的 class 开始`, def, offset + ts[selector].s))
|
|
241
|
+
}
|
|
242
|
+
selector = j + 1
|
|
243
|
+
}
|
|
244
|
+
if (['func', '(', '['].includes(k)) depth++
|
|
245
|
+
if ([')', ']'].includes(k)) depth--
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
groups.push(group)
|
|
249
|
+
start = i + 1
|
|
250
|
+
} else if (t.t === '}' || t.t === ';') {
|
|
251
|
+
if (t.t === '}') groups.pop()
|
|
252
|
+
start = i + 1
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function readDef(read, name) {
|
|
258
|
+
const file = fileOf(name)
|
|
259
|
+
const input = read(file)
|
|
260
|
+
if (input == null) fail('missing', `找不到 ${file}`)
|
|
261
|
+
if (Buffer.byteLength(input) > LIMITS.file) fail('big', `${file} 超过 256 KB`, { file, src: '' }, 0)
|
|
262
|
+
const def = indexDoc(String(input), file)
|
|
263
|
+
Object.assign(def, { name, params: new Map(), warnings: [], styles: [], slot: false,
|
|
264
|
+
values: { 'data-status': new Set(), 'data-variant': new Set() }, paths: new Set() })
|
|
265
|
+
const whole = def.tokens.find(t => t.type === 'tag' && ['html', 'head', 'body'].includes(t.tag.name))
|
|
266
|
+
if (whole) fail('whole', `组件不能包含 <${whole.tag.name}>`, def, whole.start)
|
|
267
|
+
for (const t of def.tokens) {
|
|
268
|
+
if (t.type === 'text') {
|
|
269
|
+
if (t.raw === 'style') {
|
|
270
|
+
if (t.text.includes('{{')) fail('style-param', 'style 里不能使用组件参数', def, t.start)
|
|
271
|
+
def.styles.push(t)
|
|
272
|
+
} else if (!t.parent && t.text.trim()) fail('root', '根元素外还有文字', def, t.start)
|
|
273
|
+
else if (!t.raw || ['title', 'textarea'].includes(t.raw)) {
|
|
274
|
+
t.parts = pieces(t.text, def, t.start, false)
|
|
275
|
+
if (t.raw && t.parts) t.parent.rawParts = t.parts
|
|
276
|
+
}
|
|
277
|
+
continue
|
|
278
|
+
}
|
|
279
|
+
if (t.type !== 'tag') {
|
|
280
|
+
if (t.type === 'endtag' && !t.openTag) fail('syntax', `多了结束标签 </${t.tag.name}>`, def, t.start)
|
|
281
|
+
if (!t.parent && !['comment', 'endtag'].includes(t.type)) fail('root', '根元素外只能写空白、注释和 style', def, t.start)
|
|
282
|
+
continue
|
|
283
|
+
}
|
|
284
|
+
const { name: tag } = t.tag
|
|
285
|
+
if (tag === 'script' || tag === 'iframe') fail('script', `组件不能包含 <${tag}>`, def, t.start)
|
|
286
|
+
if (tag === 'style' && t.parent) fail('root', 'style 要放在根元素旁边', def, t.start)
|
|
287
|
+
if (tag === 'slot') {
|
|
288
|
+
if (def.slot || attr(t, 'name')) fail('slot', '只支持一个不具名的 slot', def, t.start)
|
|
289
|
+
def.slot = true
|
|
290
|
+
}
|
|
291
|
+
if (!t.parent && tag !== 'style') {
|
|
292
|
+
if (def.root || tag === 'slot') fail('root', '组件需要一个可以标记的根元素', def, t.start)
|
|
293
|
+
def.root = t
|
|
294
|
+
}
|
|
295
|
+
if (t.tag.eof || (!t.matched && !t.tag.selfClosing && !VOID.has(tag) && (t.component !== undefined || !t.parent))) {
|
|
296
|
+
fail('syntax', `<${tag}> 标签没有闭合`, def, t.start)
|
|
297
|
+
}
|
|
298
|
+
if (t.component !== undefined && !COMP_NAME.test(t.component)) fail('name', `组件名「${t.component}」不合法`, def, t.start)
|
|
299
|
+
for (const a of t.tag.attrs) {
|
|
300
|
+
if (a.dup) fail('syntax', `属性 ${a.name} 重复了`, def, a.start)
|
|
301
|
+
if (a.name === 'data-state') fail('state', '组件不能写 data-state', def, a.start)
|
|
302
|
+
if (/^on/.test(a.name)) fail('script', `组件不能写事件属性 ${a.name}`, def, a.start)
|
|
303
|
+
if (a.name === 'style' && a.raw.includes('{{')) fail('style-param', 'style 属性里不能使用组件参数', def, a.start)
|
|
304
|
+
if (t.component !== undefined && NO_PASS.has(a.name)) fail('pass', `组件实例不能写 ${a.name}`, def, a.start)
|
|
305
|
+
if (a.name === 'data-if') {
|
|
306
|
+
const test = a.value.trim()
|
|
307
|
+
t.negated = test.startsWith('!')
|
|
308
|
+
t.condition = declare(def, t.negated ? test.slice(1) : test, null, false, a.start)
|
|
309
|
+
continue
|
|
310
|
+
}
|
|
311
|
+
a.parts = pieces(a.raw, def, a.vStart, true)
|
|
312
|
+
if (a.name.startsWith(':')) {
|
|
313
|
+
if (t.component === undefined || a.parts || !PARAM.test(a.name.slice(1))) fail('syntax', `${a.name} 只用于给组件传入路径`, def, a.start)
|
|
314
|
+
a.binding = pathBinding(a.value, def, a.start)
|
|
315
|
+
def.paths.add(a.binding.path)
|
|
316
|
+
} else if (a.name.startsWith('data-mock') && !a.parts) inspectMock(a, def, def.paths)
|
|
317
|
+
if (def.values[a.name] instanceof Set && !a.parts && a.value) def.values[a.name].add(a.value)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (!def.root) fail('root', '组件缺少根元素', def, 0)
|
|
321
|
+
for (const t of def.styles) inspectCss(def, t.text, t.start)
|
|
322
|
+
for (const t of def.tokens) {
|
|
323
|
+
if (t.type !== 'tag') continue
|
|
324
|
+
for (const a of t.tag.attrs) {
|
|
325
|
+
if (def.values[a.name] instanceof Set && a.parts?.length === 1) {
|
|
326
|
+
const fallback = def.params.get(a.parts[0].key)?.def
|
|
327
|
+
if (fallback) def.values[a.name].add(fallback)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return def
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export function catalog(read, names) {
|
|
335
|
+
const items = new Map()
|
|
336
|
+
for (const input of names) {
|
|
337
|
+
const name = String(input).replace(/^components\//, '').replace(/\.html$/, '')
|
|
338
|
+
if (items.has(name)) continue
|
|
339
|
+
const item = { name, file: fileOf(name), params: [], slot: false, statuses: [], variants: [], mockPaths: [] }
|
|
340
|
+
try {
|
|
341
|
+
if (!COMP_NAME.test(name)) fail('name', `组件名「${name}」不合法`)
|
|
342
|
+
const def = readDef(read, name)
|
|
343
|
+
Object.assign(item, { params: [...def.params.values()], slot: def.slot, statuses: [...def.values['data-status']].sort(),
|
|
344
|
+
variants: [...def.values['data-variant']].sort(), mockPaths: [...def.paths].sort() })
|
|
345
|
+
} catch (e) { if (!(e instanceof Problem)) throw e; item.error = e.message }
|
|
346
|
+
items.set(name, item)
|
|
347
|
+
}
|
|
348
|
+
return [...items.values()].sort((a, b) => a.name.localeCompare(b.name, 'en'))
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function catalogLine(items) {
|
|
352
|
+
if (!items.length) return ''
|
|
353
|
+
return '组件:' + items.map(item => {
|
|
354
|
+
if (item.error) return `${item.name}(有错,见体检)`
|
|
355
|
+
const args = item.params.map(p => p.name + (p.required ? '' : p.def ? '=' + p.def : '?'))
|
|
356
|
+
return `${item.name}(${args.join(', ')})`
|
|
357
|
+
}).join(' · ')
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 拼接大字符串前在片段级计数。实例出错时可以回滚到它的起点,保留同屏其他实例。
|
|
361
|
+
class Output {
|
|
362
|
+
parts = []
|
|
363
|
+
bytes = 0
|
|
364
|
+
limit = LIMITS.out
|
|
365
|
+
write(s) {
|
|
366
|
+
if (!s) return
|
|
367
|
+
const bytes = Buffer.byteLength(s)
|
|
368
|
+
if (this.bytes + bytes > this.limit) fail('big', '展开后的页面超过 4 MB')
|
|
369
|
+
this.parts.push(s)
|
|
370
|
+
this.bytes += bytes
|
|
371
|
+
}
|
|
372
|
+
mark() { return { length: this.parts.length, bytes: this.bytes } }
|
|
373
|
+
rollback(mark) { this.parts.length = mark.length; this.bytes = mark.bytes }
|
|
374
|
+
string() { return this.parts.join('') }
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// mock 的引号内可以有分号;把单引号独立写成 "'",其分句器也能正确识别边界。
|
|
378
|
+
function quote(s) {
|
|
379
|
+
const out = new Output()
|
|
380
|
+
const mixed = s.includes("'")
|
|
381
|
+
if (mixed) out.write('(')
|
|
382
|
+
out.write("'")
|
|
383
|
+
for (let i = 0; i < s.length; i += 8192) {
|
|
384
|
+
const literal = s.slice(i, i + 8192).replaceAll('\\', '\\\\').replaceAll('\n', '\\n')
|
|
385
|
+
out.write(literal.replaceAll("'", `' + "'" + '`))
|
|
386
|
+
}
|
|
387
|
+
out.write("'")
|
|
388
|
+
if (mixed) out.write(')')
|
|
389
|
+
return out.string()
|
|
390
|
+
}
|
|
391
|
+
function expression(b) {
|
|
392
|
+
return b.def == null ? b.path : `(${b.path} == null ? ${quote(b.def)} : ${b.path})`
|
|
393
|
+
}
|
|
394
|
+
function bound(ctx, key, token) {
|
|
395
|
+
const b = ctx.bindings.get(key)
|
|
396
|
+
if (b.path && inScope(token.scope, b.root)) {
|
|
397
|
+
fail('alias', `参数 ${key} 的路径 ${b.path} 被内部列表别名 ${b.root} 遮住了`, ctx.doc, token.start)
|
|
398
|
+
}
|
|
399
|
+
return b
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function attributeValue(a, ctx, t, forwarding = false, inAttr = true) {
|
|
403
|
+
if (a.binding) return a.binding
|
|
404
|
+
if (!a.parts) return { value: a.value, bare: a.end === a.start + a.name.length }
|
|
405
|
+
if (a.parts.length === 1 && a.parts[0].key) return bound(ctx, a.parts[0].key, t)
|
|
406
|
+
const values = a.parts.map(p => typeof p === 'string' ? { value: decodeEntities(p, inAttr).text } : bound(ctx, p.key, t))
|
|
407
|
+
const dynamic = values.some(v => v.path)
|
|
408
|
+
if (dynamic && forwarding) fail('forward', `转交给 ${a.name} 的动态参数混入了其他文字`, ctx.doc, a.start)
|
|
409
|
+
const out = new Output()
|
|
410
|
+
if (dynamic && values[0].path) out.write("'' + ")
|
|
411
|
+
for (const [i, v] of values.entries()) {
|
|
412
|
+
if (dynamic && i) out.write(' + ')
|
|
413
|
+
out.write(dynamic ? (v.path ? expression(v) : quote(v.value)) : v.value)
|
|
414
|
+
}
|
|
415
|
+
return dynamic ? { expr: out.string() } : { value: out.string() }
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function joined(a, b, sep) {
|
|
419
|
+
const out = new Output()
|
|
420
|
+
out.write(a)
|
|
421
|
+
if (a && b) out.write(sep)
|
|
422
|
+
out.write(b)
|
|
423
|
+
return out.string()
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function descriptor(a, doc, binding) {
|
|
427
|
+
return { name: a.name, spelling: doc.src.slice(a.start, a.start + a.name.length),
|
|
428
|
+
raw: !a.parts && !a.name.startsWith(':') ? doc.src.slice(a.start, a.end) : null, ...binding }
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function showCondition(t, ctx) {
|
|
432
|
+
if (!t.condition) return true
|
|
433
|
+
const b = bound(ctx, t.condition, t)
|
|
434
|
+
if (b.path) return t.negated ? `!(${expression(b)})` : expression(b)
|
|
435
|
+
return Boolean(b.bare || b.value) !== t.negated
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function renderedAttrs(t, ctx, passes, show) {
|
|
439
|
+
const attrs = new Map(), dynamic = new Map()
|
|
440
|
+
for (const a of t.tag.attrs) {
|
|
441
|
+
if (a.name === 'data-if') continue
|
|
442
|
+
const v = attributeValue(a, ctx, t)
|
|
443
|
+
attrs.set(a.name, descriptor(a, ctx.doc, v))
|
|
444
|
+
if (a.parts && a.name === 'data-mock-attr' && !v.path && !v.expr) inspectMock({ ...a, value: v.value }, ctx.doc, null)
|
|
445
|
+
if (v.expr || v.path) {
|
|
446
|
+
if (a.name.includes(':') || /^(style|srcdoc|http-equiv|data-mock.*)$/.test(a.name)) fail('syntax', `mock 不能动态设置 ${a.name}`, ctx.doc, a.start)
|
|
447
|
+
dynamic.set(a.name, v.expr || expression(v))
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
for (const p of passes) {
|
|
451
|
+
const old = attrs.get(p.name)
|
|
452
|
+
if (!old) {
|
|
453
|
+
attrs.set(p.name, p)
|
|
454
|
+
if (p.path) dynamic.set(p.name, expression(p))
|
|
455
|
+
continue
|
|
456
|
+
}
|
|
457
|
+
if (p.name !== 'class' && p.name !== 'style') {
|
|
458
|
+
fail('conflict', `调用标签和组件根元素都写了 ${p.name}`, p.doc, p.pos)
|
|
459
|
+
}
|
|
460
|
+
const sep = p.name === 'class' ? ' ' : ';'
|
|
461
|
+
if (dynamic.has(p.name) || p.path) {
|
|
462
|
+
const first = dynamic.get(p.name) || quote(old.value)
|
|
463
|
+
const second = p.path ? expression(p) : quote(p.value)
|
|
464
|
+
dynamic.set(p.name, joined(first, second, ' + ' + quote(sep) + ' + '))
|
|
465
|
+
} else attrs.set(p.name, { value: joined(old.value, p.value, sep) })
|
|
466
|
+
}
|
|
467
|
+
if (typeof show === 'string') {
|
|
468
|
+
const old = attrs.get('data-mock-show')
|
|
469
|
+
attrs.set('data-mock-show', { value: old ? `(${old.value}) && (${show})` : show })
|
|
470
|
+
}
|
|
471
|
+
if (dynamic.size) {
|
|
472
|
+
const out = new Output()
|
|
473
|
+
out.write(attrs.get('data-mock-attr')?.value || '')
|
|
474
|
+
for (const [name, expr] of dynamic) {
|
|
475
|
+
attrs.delete(name)
|
|
476
|
+
if (out.bytes) out.write('; ')
|
|
477
|
+
out.write(name + ': ')
|
|
478
|
+
out.write(expr)
|
|
479
|
+
}
|
|
480
|
+
attrs.set('data-mock-attr', { value: out.string() })
|
|
481
|
+
}
|
|
482
|
+
return attrs
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function writeTag(out, t, attrs, doc) {
|
|
486
|
+
out.write(doc.src.slice(t.start, t.start + 1 + t.tag.name.length))
|
|
487
|
+
for (const [name, a] of attrs) {
|
|
488
|
+
out.write(' ')
|
|
489
|
+
if (a.raw != null) out.write(a.raw)
|
|
490
|
+
else {
|
|
491
|
+
out.write(a.spelling || name)
|
|
492
|
+
if (!a.bare) {
|
|
493
|
+
out.write('="')
|
|
494
|
+
out.write(escapeAttr(a.value))
|
|
495
|
+
out.write('"')
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
out.write(t.tag.selfClosing ? '/>' : '>')
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// 放入收件方的列表模板前,只检查投影内容实际读取的自由变量。它自己的列表别名仍就近生效。
|
|
503
|
+
function checkSlotScope(html, slot, def) {
|
|
504
|
+
if (!html.includes('data-mock')) return
|
|
505
|
+
const projected = indexDoc(html, def.file)
|
|
506
|
+
for (const t of projected.tokens) {
|
|
507
|
+
if (t.type !== 'tag') continue
|
|
508
|
+
for (const a of t.tag.attrs) {
|
|
509
|
+
if (!a.name.startsWith('data-mock')) continue
|
|
510
|
+
const paths = new Set()
|
|
511
|
+
try {
|
|
512
|
+
inspectMock(a, projected, paths)
|
|
513
|
+
if (a.name === 'data-mock-set') for (const stmt of X.parseStmts(a.value)) collectPaths(stmt.lhs, paths)
|
|
514
|
+
} catch (e) {
|
|
515
|
+
if (!(e instanceof Problem)) throw e
|
|
516
|
+
continue
|
|
517
|
+
}
|
|
518
|
+
for (const path of paths) {
|
|
519
|
+
const root = path.split(/[.\[]/, 1)[0]
|
|
520
|
+
if (!inScope(t.scope, root) && inScope(slot.scope, root)) {
|
|
521
|
+
fail('alias', `插槽内容使用的 ${root} 被组件内部列表别名遮住了`, def, slot.start)
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function placeholder(name, e) {
|
|
529
|
+
return `<div data-dsh-comp-error="${e.code}" style="background:#fff1f2;color:#881337;border:2px solid #be123c;padding:8px">组件 ${escapeText(name.slice(0, 80))}:${escapeText(e.message.slice(0, 250))}</div>`
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export function expandComponents(html, { read, page = '', mock = null, placeholders = true }) {
|
|
533
|
+
const result = { html, used: [], deps: [], errors: [], warnings: [] }
|
|
534
|
+
const tokens = pageTokens(html)
|
|
535
|
+
if (!tokens) return result
|
|
536
|
+
const doc = indexDoc(html, page, tokens)
|
|
537
|
+
const used = new Set(), defs = new Map(), styles = new Map(), seenWarnings = new Set()
|
|
538
|
+
const output = new Output(), styleOrder = []
|
|
539
|
+
let instances = 0, styleBytes = 0, insertAt = 0, stopped = false
|
|
540
|
+
const head = tokens.find(t => t.type === 'tag' && t.tag.name === 'head' && (!t.parent || t.parent.tag.name === 'html'))
|
|
541
|
+
const firstStyle = head && tokens.findIndex(t => t.type === 'tag' && t.tag.name === 'style' && t.parent === head)
|
|
542
|
+
// 没写 <head>:放到 <html> 或 doctype 后面(放最前面会落在 doctype 前,整页进怪异模式)
|
|
543
|
+
const htmlOpen = head ? -1 : tokens.findIndex(t => t.type === 'tag' && t.tag.name === 'html')
|
|
544
|
+
const doctype = head ? -1 : tokens.findIndex(t => t.type === 'doctype')
|
|
545
|
+
const insertToken = head ? (firstStyle >= 0 ? firstStyle : head.close) : htmlOpen >= 0 ? htmlOpen + 1 : doctype >= 0 ? doctype + 1 : 0
|
|
546
|
+
|
|
547
|
+
function warn(code, message, doc, pos) {
|
|
548
|
+
const key = doc.file + '\0' + pos + '\0' + code + '\0' + message
|
|
549
|
+
if (seenWarnings.has(key)) return
|
|
550
|
+
seenWarnings.add(key)
|
|
551
|
+
result.warnings.push(diag(new Problem(code, message, doc, pos), page))
|
|
552
|
+
}
|
|
553
|
+
function definition(name) {
|
|
554
|
+
if (!defs.has(name)) {
|
|
555
|
+
try { defs.set(name, readDef(read, name)) }
|
|
556
|
+
catch (e) { if (!(e instanceof Problem)) throw e; defs.set(name, e) }
|
|
557
|
+
}
|
|
558
|
+
const d = defs.get(name)
|
|
559
|
+
if (d instanceof Problem) throw d
|
|
560
|
+
for (const w of d.warnings) warn(w.code, w.message, d, w.pos)
|
|
561
|
+
return d
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function instanceArgs(t, ctx, def, inherited, condition) {
|
|
565
|
+
const bindings = new Map(), passes = [], caller = ctx.doc
|
|
566
|
+
for (const a of t.tag.attrs) {
|
|
567
|
+
if (a.name === 'data-if' && ctx.bindings) continue
|
|
568
|
+
const dynamic = a.name.startsWith(':'), key = dynamic ? a.name.slice(1) : a.name
|
|
569
|
+
if (key === 'style' && a.raw.includes('{{')) fail('style-param', 'style 属性里不能使用组件参数', caller, a.start)
|
|
570
|
+
if (a.dup || bindings.has(key)) fail('syntax', `参数或属性 ${key} 重复了`, caller, a.start)
|
|
571
|
+
if (key === 'data-state') fail('state', '组件实例不能写 data-state', caller, a.start)
|
|
572
|
+
if (NO_PASS.has(key)) fail('pass', `组件实例不能写 ${key}`, caller, a.start)
|
|
573
|
+
if (/^on/.test(key)) fail('script', `组件实例不能写事件属性 ${key}`, caller, a.start)
|
|
574
|
+
const v = dynamic ? (a.binding || pathBinding(a.value, caller, a.start)) : attributeValue(a, ctx, t, true)
|
|
575
|
+
if (dynamic && !PARAM.test(key)) fail('syntax', `参数名「${key}」写法不对`, caller, a.start)
|
|
576
|
+
if (dynamic && !inScope(t.scope, v.root) && !ctx.outerScopes.some(s => inScope(s, v.root)) &&
|
|
577
|
+
!Object.hasOwn(mock || {}, v.root)) {
|
|
578
|
+
warn('mock-path', `路径 ${v.path} 的首段不在当前列表或初始 mock 数据里`, caller, a.start)
|
|
579
|
+
}
|
|
580
|
+
if (def.params.has(key)) bindings.set(key, v)
|
|
581
|
+
else if (!dynamic && (PASS.has(key) || key.startsWith('aria-'))) {
|
|
582
|
+
let pass = v
|
|
583
|
+
if (v.path && key === 'data-mock-show') pass = { value: expression(v) }
|
|
584
|
+
else if (v.path && key.startsWith('data-mock-')) fail('syntax', `${key} 需要写具体的 mock 绑定`, caller, a.start)
|
|
585
|
+
if (key === 'data-mock-attr') inspectMock({ ...a, value: pass.value }, caller, null)
|
|
586
|
+
passes.push({ ...descriptor(a, caller, pass), doc: caller, pos: a.start })
|
|
587
|
+
} else warn('unknown-attr', `组件 ${def.name} 没有参数 ${key}`, caller, a.start)
|
|
588
|
+
}
|
|
589
|
+
for (const p of def.params.values()) {
|
|
590
|
+
const v = bindings.get(p.name)
|
|
591
|
+
if (!v && p.required) fail('required', `组件 ${def.name} 缺少参数 ${p.name}`, caller, t.start)
|
|
592
|
+
if (!v) bindings.set(p.name, { value: p.def ?? '' })
|
|
593
|
+
else if (v.path && p.def !== null && v.def == null) bindings.set(p.name, { ...v, def: p.def })
|
|
594
|
+
}
|
|
595
|
+
if (typeof condition === 'string') {
|
|
596
|
+
const prior = passes.find(p => p.name === 'data-mock-show')
|
|
597
|
+
if (prior) { prior.raw = null; prior.value = `(${prior.value}) && (${condition})` }
|
|
598
|
+
else passes.push({ name: 'data-mock-show', value: condition, doc: caller, pos: t.start })
|
|
599
|
+
}
|
|
600
|
+
return { bindings, passes: passes.concat(inherited) }
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function instance(out, t, ctx, root, show) {
|
|
604
|
+
const name = t.component, mark = out.mark(), styleMark = styleOrder.length
|
|
605
|
+
try {
|
|
606
|
+
if (!COMP_NAME.test(name)) fail('name', `组件名「${name}」不合法`, ctx.doc, t.start)
|
|
607
|
+
used.add(name)
|
|
608
|
+
if (++instances > LIMITS.instances) fail('big', '单页组件实例超过 5000 个')
|
|
609
|
+
if (!t.tag.selfClosing && !t.matched) fail('syntax', `<c-${name}> 缺少结束标签`)
|
|
610
|
+
if (ctx.chain.includes(name)) fail('cycle', `组件循环引用:${[...ctx.chain, name].join(' → ')}`)
|
|
611
|
+
if (ctx.depth >= LIMITS.depth) fail('depth', '组件嵌套超过八层')
|
|
612
|
+
const def = definition(name)
|
|
613
|
+
const args = instanceArgs(t, ctx, def, root ? ctx.passes : [], show)
|
|
614
|
+
const inside = { ...args, doc: def, marker: root ? ctx.marker : name, slot: null, depth: ctx.depth + 1,
|
|
615
|
+
chain: [...ctx.chain, name], outerScopes: t.scope ? [...ctx.outerScopes, t.scope] : ctx.outerScopes }
|
|
616
|
+
if (showCondition(def.root, inside) === false) return
|
|
617
|
+
if (def.slot && !t.tag.selfClosing && ctx.doc.src.slice(t.end, ctx.doc.tokens[t.close].start).trim()) {
|
|
618
|
+
const content = new Output()
|
|
619
|
+
render(content, { ...ctx, depth: ctx.depth + 1 }, t.index + 1, t.close)
|
|
620
|
+
inside.slot = content.string()
|
|
621
|
+
}
|
|
622
|
+
if (def.styles.length && !styles.has(name)) {
|
|
623
|
+
const block = new Output()
|
|
624
|
+
block.write(`<style data-dsh-comp-css="${name}">`)
|
|
625
|
+
for (const css of def.styles) { block.write(css.text); block.write('\n') }
|
|
626
|
+
block.write('</style>')
|
|
627
|
+
if (styleBytes + block.bytes + output.bytes > LIMITS.out) fail('big', '展开后的页面超过 4 MB')
|
|
628
|
+
styles.set(name, block.string())
|
|
629
|
+
styleOrder.push(name)
|
|
630
|
+
styleBytes += block.bytes
|
|
631
|
+
output.limit = LIMITS.out - styleBytes
|
|
632
|
+
}
|
|
633
|
+
render(out, inside, def.root.index, def.root.after)
|
|
634
|
+
} catch (e) {
|
|
635
|
+
if (!(e instanceof Problem)) throw e
|
|
636
|
+
out.rollback(mark)
|
|
637
|
+
while (styleOrder.length > styleMark) {
|
|
638
|
+
const key = styleOrder.pop()
|
|
639
|
+
styleBytes -= Buffer.byteLength(styles.get(key))
|
|
640
|
+
styles.delete(key)
|
|
641
|
+
}
|
|
642
|
+
output.limit = LIMITS.out - styleBytes
|
|
643
|
+
if (e.code === 'big' && stopped) throw e
|
|
644
|
+
if (e.code === 'big') {
|
|
645
|
+
if (!e.doc) { e.doc = ctx.doc; e.pos = t.start }
|
|
646
|
+
stopped = true
|
|
647
|
+
}
|
|
648
|
+
result.errors.push(diag(e, page, ctx.doc, t.start))
|
|
649
|
+
if (placeholders) out.write(placeholder(name, e))
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function render(out, ctx, from, end) {
|
|
654
|
+
const { doc: d } = ctx
|
|
655
|
+
for (let i = from; i < end; i++) {
|
|
656
|
+
const t = d.tokens[i]
|
|
657
|
+
if (d === doc && i === insertToken) insertAt = out.parts.length
|
|
658
|
+
if (t.type === 'tag') {
|
|
659
|
+
const root = t === d.root, show = showCondition(t, ctx)
|
|
660
|
+
if (show === false) { i = t.after - 1; continue }
|
|
661
|
+
if (t.component !== undefined) {
|
|
662
|
+
if (!stopped) instance(out, t, ctx, root, show)
|
|
663
|
+
if (t.matched || t.tag.selfClosing) i = t.after - 1
|
|
664
|
+
continue
|
|
665
|
+
}
|
|
666
|
+
if (ctx.bindings && t.tag.name === 'slot') {
|
|
667
|
+
if (ctx.slot !== null) {
|
|
668
|
+
if (t.scope) checkSlotScope(ctx.slot, t, d)
|
|
669
|
+
out.write(ctx.slot)
|
|
670
|
+
i = t.after - 1
|
|
671
|
+
}
|
|
672
|
+
continue
|
|
673
|
+
}
|
|
674
|
+
if (ctx.bindings && (root || t.condition || t.rawParts || t.tag.attrs.some(a => a.parts))) {
|
|
675
|
+
const attrs = renderedAttrs(t, ctx, root ? ctx.passes : [], show)
|
|
676
|
+
if (t.rawParts) {
|
|
677
|
+
const raw = attributeValue({ parts: t.rawParts }, ctx, t, false, false)
|
|
678
|
+
if (raw.path || raw.expr) {
|
|
679
|
+
if (attrs.has('data-mock-text')) fail('conflict', '原始文本参数和 data-mock-text 同时设置了内容', d, t.start)
|
|
680
|
+
attrs.set('data-mock-text', { value: raw.expr || expression(raw) })
|
|
681
|
+
i++ // title、textarea 按纯文字解析,绑定到宿主,不能在里面插 span。
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
if (root) attrs.set('data-dsh-c', { value: ctx.marker })
|
|
685
|
+
writeTag(out, t, attrs, d)
|
|
686
|
+
continue
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
if (ctx.bindings && t.type === 'endtag' && t.tag.name === 'slot') continue
|
|
690
|
+
if (ctx.bindings && t.parts) {
|
|
691
|
+
for (const p of t.parts) {
|
|
692
|
+
if (typeof p === 'string') out.write(p)
|
|
693
|
+
else {
|
|
694
|
+
const b = bound(ctx, p.key, t)
|
|
695
|
+
out.write(b.path ? `<span data-mock-text="${escapeAttr(expression(b))}"></span>` : escapeText(b.value))
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
} else out.write(d.src.slice(t.start, t.end))
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
try {
|
|
703
|
+
render(output, { doc, chain: [], depth: 0, outerScopes: [] }, 0, tokens.length)
|
|
704
|
+
if (output.bytes + styleBytes > LIMITS.out) fail('big', '展开后的页面超过 4 MB')
|
|
705
|
+
if (insertToken === tokens.length) insertAt = output.parts.length
|
|
706
|
+
output.parts.splice(insertAt, 0, ...styles.values())
|
|
707
|
+
result.html = output.string()
|
|
708
|
+
} catch (e) {
|
|
709
|
+
if (!(e instanceof Problem) || e.code !== 'big') throw e
|
|
710
|
+
if (!stopped) result.errors.push(diag(e, page, doc, 0))
|
|
711
|
+
result.html = placeholders ? placeholder('页面', e) : ''
|
|
712
|
+
}
|
|
713
|
+
result.used = [...used].sort()
|
|
714
|
+
result.deps = result.used.map(fileOf).sort()
|
|
715
|
+
return result
|
|
716
|
+
}
|