dsh-design-studio 0.23.0 → 0.24.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/lib/browser.js CHANGED
@@ -262,6 +262,9 @@ export function createBrowser({ bin = findBrowser(), log = () => {}, idleMs = ID
262
262
  if (nav.errorText) throw new Error(blocked.length ? `不是允许的地址,没打开:${blocked[0].slice(0, 120)}` : nav.errorText)
263
263
  await loaded
264
264
  const page = {
265
+ // 换深浅色媒体(prefers-color-scheme),不重开页面:存外框时浅色、深色都在真的媒体环境下量
266
+ media: (dk) => c('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: dk ? 'dark' : 'light' }, { name: 'prefers-reduced-motion', value: 'reduce' }] }),
267
+ mouse: (x, y) => c('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y }), // 把鼠标挪到页面上某处(测「鼠标停在外框上」用)
265
268
  eval: async (expression) => {
266
269
  const r = await c('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true })
267
270
  if (r.exceptionDetails) throw new Error((r.exceptionDetails.exception && r.exceptionDetails.exception.description) || r.exceptionDetails.text || 'eval failed')
package/lib/bundle.js CHANGED
@@ -36,6 +36,33 @@ const escScript = (s) => String(s).replace(/<\/(script)/gi, '<\\/$1').replace(/<
36
36
  /** 属性值里的实体解回来(只管引号和 &,style 里用得到的就这些) */
37
37
  const unAttr = (s) => String(s).replace(/&(quot|#0*34|#x0*22);/gi, '"').replace(/&(apos|#0*39|#x0*27);/gi, "'").replace(/&amp;/gi, '&')
38
38
 
39
+ /**
40
+ * srcset 拆成 [地址, 描述] 一项一项(照 HTML 的规矩:地址到空白为止,所以 data: 里的逗号不拆)
41
+ */
42
+ export function srcsetParts(v) {
43
+ const out = []
44
+ const s = String(v)
45
+ let i = 0
46
+ while (i < s.length) {
47
+ while (i < s.length && /[\s,]/.test(s[i])) i++
48
+ if (i >= s.length) break
49
+ let j = i
50
+ while (j < s.length && !/\s/.test(s[j])) j++
51
+ let url = s.slice(i, j)
52
+ let desc = ''
53
+ if (url.endsWith(',')) url = url.replace(/,+$/, '')
54
+ else {
55
+ let k = j
56
+ while (k < s.length && s[k] !== ',') k++
57
+ desc = s.slice(j, k).trim()
58
+ j = k
59
+ }
60
+ out.push([url, desc])
61
+ i = j
62
+ }
63
+ return out
64
+ }
65
+
39
66
  /** 模块里的 import … from '…' / import '…' / export … from '…' / import('…') */
40
67
  const IMPORT_RE = /(\bimport\s*(?:[\w$*{}\s,]+?\s*from\s*)?|\bexport\s*(?:\*(?:\s*as\s+[\w$]+)?|\{[^}]*\})\s*from\s*|\bimport\s*\(\s*)(["'])([^"'\n]+)\2/g
41
68
  const relSpec = (spec) => (/^\.{0,2}\//.test(spec) ? localRef(spec) : '')
@@ -103,17 +130,14 @@ export function bundle(html, { key, read, tail = '', base = '', lead = '' }) {
103
130
  const rel = join(from, ref)
104
131
  const buf = get(rel)
105
132
  if (!buf || !spend(buf.length)) return m
106
- const inner = css(buf.toString('utf8'), rel, depth + 1)
133
+ const inner = css(buf.toString('utf8').replace(/^\ufeff/, ''), rel, depth + 1)
107
134
  return media.trim() ? `@media ${media.trim()}{${inner}}` : inner
108
135
  })
109
- .replace(/url\(\s*(["']?)([^"')]+)\1\s*\)/gi, (m, q, url) => {
136
+ .replace(/url\(\s*(?:"([^"]*)"|'([^']*)'|([^"')\s]+))\s*\)/gi, (m, u1, u2, u3) => {
137
+ const url = u1 ?? u2 ?? u3
110
138
  const d = dataUrl(from, url)
111
139
  return d === url ? m : bare ? `url(${d})` : `url("${d}")`
112
140
  })
113
- const srcset = (v) => String(v).split(',').map((part) => {
114
- const [u, ...rest] = part.trim().split(/\s+/)
115
- return [dataUrl(key, u), ...rest].join(' ')
116
- }).join(', ')
117
141
 
118
142
  // ES 模块:入口和依赖都换成 data:,模块里相对的 import 改写成原预览地址的绝对 URL,再用 importmap 把这些地址映射到 data:
119
143
  // (data: 模块里没法按相对路径解析;行内模块靠 <base> 解析成同样的绝对地址)。页面自己有 importmap 就不动模块。
@@ -158,7 +182,7 @@ export function bundle(html, { key, read, tail = '', base = '', lead = '' }) {
158
182
  const buf = get(rel)
159
183
  if (!buf || !spend(buf.length)) return tag
160
184
  const media = /\bmedia\s*=\s*(["'])([^"']*)\1/i.exec(tag)
161
- return `<style data-href="${escAttr(href)}"${media ? ` media="${escAttr(media[2])}"` : ''}>${css(buf.toString('utf8'), rel).replace(/<\/style/gi, '<\\/style')}</style>`
185
+ return `<style data-href="${escAttr(href)}"${media ? ` media="${escAttr(media[2])}"` : ''}>${css(buf.toString('utf8').replace(/^\ufeff/, ''), rel).replace(/<\/style/gi, '<\\/style')}</style>`
162
186
  })
163
187
  // 页面里的 <style>:url() 相对这一屏
164
188
  .replace(/(<style\b[^>]*>)([\s\S]*?)(<\/style\s*>)/gi, (m, a, body, b) => (/\bdata-href=/.test(a) ? m : a + css(body, key) + b))
@@ -179,11 +203,33 @@ export function bundle(html, { key, read, tail = '', base = '', lead = '' }) {
179
203
  return d ? `<script${attrs.replace(s[0], `src="${d}"`)}>${body}</script>` : m
180
204
  })
181
205
  // 图片、视频、音频、SVG 里的 image
182
- .replace(/(<(?:img|source|video|audio|input|image|embed|track)\b[^>]*>)/gi, (tag) => tag
183
- .replace(/(\s(?:src|poster|href|xlink:href)\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => `${a}${q}${escAttr(dataUrl(key, u))}${q}`)
184
- .replace(/(\ssrcset\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => `${a}${q}${escAttr(srcset(u))}${q}`))
206
+ // 原来的 src 留在 data-dsh-src(体检比外框指纹时按它:换了张图也认得出)
207
+ .replace(/(<(?:img|source|video|audio|input|image|embed|track)\b[^>]*>)/gi, (tag0) => {
208
+ let orig = null
209
+ let origSet = null
210
+ const tag = tag0
211
+ // 属性值先解实体(a.png?x=1&amp;y=2)再找文件;没换成 data: 的原样不动(不二次转义)
212
+ .replace(/(\s(?:src|poster|href|xlink:href)\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => {
213
+ const raw = unAttr(u)
214
+ const d = dataUrl(key, raw)
215
+ if (d === raw) return m
216
+ if (orig === null && !/poster/i.test(a)) orig = [q, u]
217
+ return `${a}${q}${escAttr(d)}${q}`
218
+ })
219
+ .replace(/(\ssrcset\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => {
220
+ const raw = unAttr(u)
221
+ // 每张只换一次(dataUrl 每调一次都扣总量)
222
+ const conv = srcsetParts(raw).map(([x, dsc]) => [dataUrl(key, x), x, dsc])
223
+ if (conv.every(([dd, x]) => dd === x)) return m
224
+ const d = conv.map(([dd, , dsc]) => [dd, dsc].filter(Boolean).join(' ')).join(', ')
225
+ if (origSet === null) origSet = [q, u]
226
+ return `${a}${q}${escAttr(d)}${q}`
227
+ })
228
+ const extra = (orig ? ` data-dsh-src=${orig[0]}${orig[1]}${orig[0]}` : '') + (origSet ? ` data-dsh-srcset=${origSet[0]}${origSet[1]}${origSet[0]}` : '')
229
+ return extra ? tag.replace(/^<[a-z]+\b/i, (t) => `${t}${extra}`) : tag
230
+ })
185
231
  // <use href="icons.svg#x">:浏览器不认 data: 的 <use>,把那个 SVG 文件放进页面,href 改成 #x
186
- .replace(/<use\b[^>]*>/gi, (tag) => tag.replace(/(\s(?:href|xlink:href)\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => {
232
+ .replace(/<use\b[^>]*>/gi, (tag0) => { let hit = false; const tag = tag0.replace(/(\s(?:href|xlink:href)\s*=\s*)(["'])([^"']*)\2/gi, (m, a, q, u) => {
187
233
  const i = u.indexOf('#')
188
234
  const ref = localRef(u)
189
235
  if (!ref || i < 0) return m
@@ -191,20 +237,27 @@ export function bundle(html, { key, read, tail = '', base = '', lead = '' }) {
191
237
  if (!sprites.has(rel)) {
192
238
  const buf = get(rel)
193
239
  const pre = `dsh${sprites.size + 1}-`
194
- const text = buf ? spriteOf(buf.toString('utf8').replace(/<\?xml[\s\S]*?\?>|<!DOCTYPE[^>]*>/gi, '').trim(), pre) : ''
240
+ const text = buf ? spriteOf(buf.toString('utf8').replace(/^\ufeff/, '').replace(/<\?xml[\s\S]*?\?>|<!DOCTYPE[^>]*>/gi, '').trim(), pre) : ''
195
241
  if (!/^<svg\b/i.test(text) || !spend(text.length)) return m
196
242
  sprites.set(rel, { pre, text })
197
243
  }
198
244
  let id = u.slice(i + 1)
199
245
  try { id = decodeURIComponent(id) } catch { /* 原样 */ }
246
+ hit = true
200
247
  return `${a}${q}#${escAttr(sprites.get(rel).pre + id)}${q}`
201
- }))
248
+ })
249
+ if (!hit) return tag
250
+ // 原来的写法留在 data-dsh-href(href 优先,和浏览器一样):体检比外框指纹时按它,换了别的文件里的同名图标也认得出
251
+ const o = /\shref\s*=\s*(["'])([^"']*)\1/i.exec(tag0) || /\sxlink:href\s*=\s*(["'])([^"']*)\1/i.exec(tag0)
252
+ return o ? tag.replace(/^<use\b/i, (t) => `${t} data-dsh-href=${o[1]}${o[2]}${o[1]}`) : tag
253
+ })
202
254
  // 行内样式里的 url():解开实体再处理,按原来的引号转义回去
203
255
  .replace(/(\sstyle\s*=\s*)(?:"([^"]*)"|'([^']*)')/gi, (m, a, dq, sq) => {
204
256
  const raw = dq ?? sq
205
257
  if (!/url\(/i.test(raw)) return m
206
258
  const v = css(unAttr(raw), key, 0, true).replace(/&/g, '&amp;')
207
- return dq != null ? `${a}"${v.replace(/"/g, '&quot;')}"` : `${a}'${v.replace(/'/g, '&#39;')}'`
259
+ // 原来的写法留在 data-dsh-style(体检比外框指纹时按它)
260
+ return dq != null ? `${a}"${v.replace(/"/g, '&quot;')}" data-dsh-style="${dq}"` : `${a}'${v.replace(/'/g, '&#39;')}' data-dsh-style='${sq}'`
208
261
  })
209
262
  const head = []
210
263
  if (base) head.push(`<base href="${escAttr(base)}">`)