glass-easel-devtools-agent 0.10.1 → 0.10.2

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/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "glass-easel-devtools-agent",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "main": "dist/index.js",
5
5
  "peerDependencies": {
6
- "glass-easel": "^0.10.1"
6
+ "glass-easel": "^0.10.2"
7
7
  },
8
8
  "devDependencies": {
9
9
  "@csstools/css-tokenizer": "^2.4.1",
10
10
  "@csstools/selector-specificity": "^3.1.1",
11
11
  "@types/node": "^20.16.5",
12
12
  "devtools-protocol": "^0.0.1319565",
13
- "glass-easel": "^0.10.1",
14
- "glass-easel-template-compiler": "^0.10.1",
13
+ "glass-easel": "^0.10.2",
14
+ "glass-easel-template-compiler": "^0.10.2",
15
15
  "postcss-selector-parser": "^6.1.2"
16
16
  },
17
17
  "scripts": {
package/src/backend.ts CHANGED
@@ -160,7 +160,7 @@ const expectToken = (
160
160
  }
161
161
 
162
162
  const parseNameValueStr = (cssText: string) => {
163
- const ret: { name: string; value: string }[] = []
163
+ const ret: { name: string; value: string; disabled: boolean }[] = []
164
164
  const tokens = tokenize({ css: cssText })
165
165
  if (tokens.length === 0) return null
166
166
  let nameStart = 0
@@ -175,7 +175,7 @@ const parseNameValueStr = (cssText: string) => {
175
175
  if (expectToken(t, TokenType.Semicolon)) break
176
176
  }
177
177
  const value = stringify(...tokens.slice(valueStart, i)).trim()
178
- ret.push({ name, value })
178
+ ret.push({ name, value, disabled: false })
179
179
  nameStart = i + 1
180
180
  }
181
181
  }
@@ -230,10 +230,8 @@ export const getMatchedRules = (
230
230
  rules: glassEasel.CSSRule[],
231
231
  ): glassEasel.CSSRule[] => {
232
232
  const rulesWithSelector = rules.map((rule) => {
233
- if (rule.propertyText !== undefined) {
234
- const edit = styleEditContext.createOrGetRule(ctx, rule, rule.propertyText)
235
- rule.properties = edit.getProps()
236
- }
233
+ const edit = styleEditContext.createOrGetRule(ctx, rule, rule.propertyText)
234
+ rule.properties = edit.getProps()
237
235
  const ps = parser().astSync(rule.selector)
238
236
  const specificity = selectorSpecificity(ps)
239
237
  return [specificity, rule] as const
@@ -268,10 +266,8 @@ export const getMatchedRules = (
268
266
  ctx.getMatchedRules(elem as glassEasel.domlikeBackend.Element, (ret) => {
269
267
  convertAdapterGeneratedRules(ret.rules)
270
268
  ret.rules = calcRuleWeight(ctx, ret.rules)
271
- if (ret.inlineText !== undefined) {
272
- const edit = styleEditContext.createOrGetInline(elem, ret.inlineText)
273
- ret.inline = edit.getProps()
274
- }
269
+ const edit = styleEditContext.createOrGetInline(elem, ret.inline, ret.inlineText)
270
+ ret.inline = edit.getProps()
275
271
  resolve(ret)
276
272
  })
277
273
  } catch (err) {
@@ -289,6 +285,8 @@ export const getMatchedRules = (
289
285
  if ('getMatchedRules' in elem) {
290
286
  ;(elem as glassEasel.backend.Element).getMatchedRules!((ret) => {
291
287
  ret.rules = calcRuleWeight(ctx, ret.rules)
288
+ const edit = styleEditContext.createOrGetInline(elem, ret.inline, ret.inlineText)
289
+ ret.inline = edit.getProps()
292
290
  resolve(ret)
293
291
  })
294
292
  } else {
@@ -298,22 +296,33 @@ export const getMatchedRules = (
298
296
  })
299
297
  }
300
298
 
299
+ const filterCssProperties = (props: glassEasel.CSSProperty[]) => {
300
+ return props.map((prop) => ({
301
+ name: prop.name.trim(),
302
+ value: prop.value.trim(),
303
+ disabled: false,
304
+ important: prop.important,
305
+ }))
306
+ }
307
+
301
308
  export class StyleRuleEdit {
302
- private props: glassEasel.CSSProperty[]
309
+ private props: { name: string; value: string; disabled: boolean; important?: boolean }[]
303
310
 
304
311
  constructor(props: glassEasel.CSSProperty[]) {
305
- this.props = props
312
+ this.props = filterCssProperties(props)
306
313
  }
307
314
 
308
- static fromInlineStyle(s: string) {
315
+ static fromMaybeInlineStyle(props: glassEasel.CSSProperty[], inlineStyle?: string) {
316
+ if (inlineStyle === undefined) return new StyleRuleEdit(props)
309
317
  const ret = new StyleRuleEdit([])
310
- ret.props = parseNameValueStr(s) ?? []
318
+ ret.props = parseNameValueStr(inlineStyle) ?? []
311
319
  return ret
312
320
  }
313
321
 
314
- updateWithProps(props: glassEasel.CSSProperty[]) {
322
+ updateWithProps(props: glassEasel.CSSProperty[], inlineStyle?: string) {
315
323
  const oldProps = this.props
316
- this.props = props
324
+ this.props =
325
+ inlineStyle === undefined ? filterCssProperties(props) : parseNameValueStr(inlineStyle) ?? []
317
326
  let i = 0
318
327
  oldProps.forEach((prop) => {
319
328
  if (prop.disabled) {
@@ -380,10 +389,17 @@ class StyleEditContext {
380
389
  inlineStyleMap = new WeakMap<glassEasel.GeneralBackendElement, StyleRuleEdit>()
381
390
  ruleMap = new WeakMap<glassEasel.GeneralBackendContext, Record<string, StyleRuleEdit>>()
382
391
 
383
- createOrGetInline(elem: glassEasel.GeneralBackendElement, inlineStyle: string): StyleRuleEdit {
392
+ createOrGetInline(
393
+ elem: glassEasel.GeneralBackendElement,
394
+ properties: glassEasel.CSSProperty[],
395
+ inlineStyle?: string,
396
+ ): StyleRuleEdit {
384
397
  const r = this.inlineStyleMap.get(elem)
385
- if (r) return r
386
- const edit = StyleRuleEdit.fromInlineStyle(inlineStyle)
398
+ if (r) {
399
+ r.updateWithProps(properties, inlineStyle)
400
+ return r
401
+ }
402
+ const edit = StyleRuleEdit.fromMaybeInlineStyle(properties, inlineStyle)
387
403
  this.inlineStyleMap.set(elem, edit)
388
404
  return edit
389
405
  }
@@ -391,20 +407,21 @@ class StyleEditContext {
391
407
  createOrGetRule(
392
408
  ctx: glassEasel.GeneralBackendContext,
393
409
  rule: glassEasel.CSSRule,
394
- inlineStyle: string,
410
+ inlineStyle?: string,
395
411
  ): StyleRuleEdit {
396
412
  const key = `${rule.sheetIndex}/${rule.ruleIndex}`
397
413
  const map = this.ruleMap.get(ctx)
398
414
  if (!map) {
399
415
  const map = Object.create(null) as Record<string, StyleRuleEdit>
400
- map[key] = StyleRuleEdit.fromInlineStyle(inlineStyle)
416
+ map[key] = StyleRuleEdit.fromMaybeInlineStyle(rule.properties, inlineStyle)
401
417
  this.ruleMap.set(ctx, map)
402
418
  return map[key]
403
419
  }
404
420
  if (!map[key]) {
405
- map[key] = StyleRuleEdit.fromInlineStyle(inlineStyle)
421
+ map[key] = StyleRuleEdit.fromMaybeInlineStyle(rule.properties, inlineStyle)
406
422
  return map[key]
407
423
  }
424
+ map[key].updateWithProps(rule.properties, inlineStyle)
408
425
  return map[key]
409
426
  }
410
427
 
package/src/overlay.ts CHANGED
@@ -171,6 +171,40 @@ export class OverlayManager {
171
171
  ) as glassEasel.Component<any, any, ComponentExport>
172
172
  this.backendContexts.set(ctx, component)
173
173
 
174
+ // inject a middleware as an event listener
175
+ if (
176
+ ctx.mode === glassEasel.BackendMode.Composed ||
177
+ ctx.mode === glassEasel.BackendMode.Shadow
178
+ ) {
179
+ if (ctx.addPriorEventListener) {
180
+ const isEventUnderRoot = (target: glassEasel.Element): boolean => {
181
+ // detect whether the event is under the root
182
+ if (!(target instanceof glassEasel.Element)) return false
183
+ let cur = target
184
+ for (;;) {
185
+ if (cur === component) return true
186
+ if (cur.parentNode) {
187
+ cur = cur.parentNode
188
+ continue
189
+ }
190
+ if (cur.ownerShadowRoot) {
191
+ cur = cur.ownerShadowRoot.getHostNode()
192
+ continue
193
+ }
194
+ return false
195
+ }
196
+ }
197
+ ctx.addPriorEventListener((target, type, detail, options) => {
198
+ if (!isEventUnderRoot(target)) return undefined
199
+ const ev = new glassEasel.Event(type, detail, options)
200
+ glassEasel.Event.dispatchEvent(target, ev)
201
+ return ev.defaultPrevented()
202
+ ? glassEasel.EventBubbleStatus.NoDefault
203
+ : glassEasel.EventBubbleStatus.Normal
204
+ })
205
+ }
206
+ }
207
+
174
208
  // insert into backend
175
209
  let parentElement: glassEasel.GeneralBackendElement
176
210
  let placeholder: glassEasel.GeneralBackendElement