@prunellejs/ui 0.9.0 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/ui",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Platform-first headless UI primitives and pure CSS @layer pru-ui Style Grammars for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -29,6 +29,6 @@
29
29
  "directory": "packages/ui"
30
30
  },
31
31
  "peerDependencies": {
32
- "prunelle": "^0.9.0"
32
+ "prunelle": "^0.10.0"
33
33
  }
34
34
  }
package/src/Dialog.tsx CHANGED
@@ -75,9 +75,8 @@ function closeButtonAttrs(dialog: Shared, ids: DialogIds) {
75
75
  }
76
76
  }
77
77
 
78
- // Takes `props` without spreading so lazy `get children()` getters evaluate strictly inside `<div data-part="body">`.
79
- function DialogPanel(props: { dialog: Shared, ids: DialogIds }) {
80
- const { dialog, ids } = props
78
+ // Parameter destructuring keeps lazy `get children()` getters evaluating strictly inside `<div data-part="body">`.
79
+ function DialogPanel({ dialog, ids }: { dialog: Shared, ids: DialogIds }) {
81
80
  return (
82
81
  <div data-part="panel">
83
82
  <header data-part="header" data-layout="split">
package/src/static.tsx CHANGED
@@ -132,32 +132,51 @@ function scanOpeningTags(html: string): ScannedTag[] {
132
132
  }
133
133
 
134
134
  const NATIVE_CTRL_RE = /^(?:input|textarea|select)$/i
135
+ const ATTR_RE = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g
136
+ const CTRL_ROLES = new Set(['combobox', 'textbox', 'listbox', 'spinbutton'])
137
+
138
+ interface ScannedAttr {
139
+ name: string
140
+ value: string
141
+ start: number
142
+ end: number
143
+ }
144
+
145
+ /** The attributes of an opening tag by name, so `data-id` or a value holding `id=` is never taken for `id`. */
146
+ function readAttrs(raw: string): ScannedAttr[] {
147
+ return [...raw.matchAll(ATTR_RE)].map(m => ({ name: m[1]!.toLowerCase(), value: m[2] ?? m[3] ?? m[4] ?? '', start: m.index, end: m.index + m[0].length }))
148
+ }
135
149
 
136
150
  function wireFieldHtml(html: string, wiring: FieldControlWiring): string {
137
151
  const tags = scanOpeningTags(html)
138
- const target = tags.find(t => NATIVE_CTRL_RE.test(t.tag) && !(/\btype\s*=\s*(?:"hidden"|'hidden'|hidden\b)/i.test(t.rawAttrs) && /^input$/i.test(t.tag)))
139
- ?? tags.find(t => /\brole\s*=\s*["'](?:combobox|textbox|listbox|spinbutton)["']/i.test(t.rawAttrs))
152
+ const valueOf = (t: ScannedTag, name: string): string | undefined => readAttrs(t.rawAttrs).find(a => a.name === name)?.value.toLowerCase()
153
+ const target = tags.find(t => NATIVE_CTRL_RE.test(t.tag) && !(/^input$/i.test(t.tag) && valueOf(t, 'type') === 'hidden'))
154
+ ?? tags.find(t => CTRL_ROLES.has(valueOf(t, 'role') ?? ''))
140
155
  if (target === undefined)
141
156
  return html
142
157
 
143
158
  let attrs = target.rawAttrs
144
- if (!/\bid\s*=/i.test(attrs))
159
+ const find = (name: string) => readAttrs(attrs).find(a => a.name === name)
160
+ const replace = (a: ScannedAttr, text: string) => { attrs = `${attrs.slice(0, a.start)}${text}${attrs.slice(a.end)}` }
161
+ const id = find('id')
162
+ if (id === undefined)
145
163
  attrs += ` id="${escAttr(wiring.id)}"`
146
- else if (/\bid\s*=\s*"pru-[^"]*"/i.test(attrs))
147
- attrs = attrs.replace(/\bid\s*=\s*"pru-[^"]*"/i, `id="${escAttr(wiring.id)}"`)
164
+ else if (id.value.startsWith('pru-'))
165
+ replace(id, `id="${escAttr(wiring.id)}"`)
148
166
 
149
167
  if (wiring.describedBy) {
150
- const descRe = /(\baria-describedby\s*=\s*")([^"]*)(")/i
151
- attrs = descRe.test(attrs)
152
- ? attrs.replace(descRe, (_, p1: string, cur: string, p3: string) => `${p1}${escAttr(mergeTokenAttr(cur, wiring.describedBy!))}${p3}`)
153
- : `${attrs} aria-describedby="${escAttr(wiring.describedBy)}"`
168
+ const described = find('aria-describedby')
169
+ if (described === undefined)
170
+ attrs += ` aria-describedby="${escAttr(wiring.describedBy)}"`
171
+ else
172
+ replace(described, `aria-describedby="${escAttr(mergeTokenAttr(described.value, wiring.describedBy))}"`)
154
173
  }
155
- if (wiring.invalid && !/\baria-invalid\s*=/i.test(attrs))
174
+ if (wiring.invalid && find('aria-invalid') === undefined)
156
175
  attrs += ' aria-invalid="true"'
157
176
  if (wiring.required) {
158
- if (NATIVE_CTRL_RE.test(target.tag) && !/\brequired(?:\s|=|$)/i.test(attrs))
177
+ if (NATIVE_CTRL_RE.test(target.tag) && find('required') === undefined)
159
178
  attrs += ' required'
160
- if (!/\baria-required\s*=/i.test(attrs))
179
+ if (find('aria-required') === undefined)
161
180
  attrs += ' aria-required="true"'
162
181
  }
163
182
  return `${html.slice(0, target.start)}<${target.tag}${attrs}${target.selfClose}>${html.slice(target.end)}`
@@ -1,5 +1,5 @@
1
1
  // Static scanner detecting which `@prunellejs/ui` component parts a set of source files renders.
2
- // Checks imports and re-exports from `@prunellejs/ui` plus literal `data-ui="..."` / `data-layout="..."` attributes.
2
+ // Checks imports, dynamic imports, requires and re-exports from `@prunellejs/ui` plus literal `data-ui="..."` / `data-layout="..."` attributes.
3
3
 
4
4
  import type { UiComponentPart } from './split'
5
5
  import { existsSync, readFileSync } from 'node:fs'
@@ -19,6 +19,7 @@ const IMPORT_EXPORT_RE = new RegExp(
19
19
  const NAMED_LIST_RE = /\{([\s\S]*?)\}/
20
20
  const NAMESPACE_RE = /^\s*\*(?:\s+as\s+[A-Za-z_$][\w$]*)?\s*$|,\s*\*\s+as\s+[A-Za-z_$][\w$]*/
21
21
  const COMMENT_RE = /\/\/[^\n]*|\/\*[\s\S]*?\*\//g
22
+ const MODULE_CALL_RE = /\b(?:import|require)\s*\(\s*['"]([^'"]+)['"]\s*\)/g
22
23
  const DATA_UI_ATTR_RE = /\bdata-ui\s*=\s*(?:["']([^"']+)["']|\{([^}]*)\})/g
23
24
  const DATA_LAYOUT_ATTR_RE = /\bdata-layout\s*=/
24
25
  const STATIC_QUOTED_RE = /^\s*["']([^"']+)["']\s*$/
@@ -38,6 +39,13 @@ const PRUNELLE_EXPORT_TO_PARTS: Readonly<Record<string, readonly UiComponentPart
38
39
  CookieTable: ['datatable'],
39
40
  }
40
41
 
42
+ function addPrunelleParts(out: Set<UiComponentPart>): void {
43
+ for (const parts of Object.values(PRUNELLE_EXPORT_TO_PARTS)) {
44
+ for (const p of parts)
45
+ out.add(p)
46
+ }
47
+ }
48
+
41
49
  function isUiImportSource(specifier: string): boolean {
42
50
  if (specifier === '@prunellejs/ui')
43
51
  return true
@@ -81,9 +89,12 @@ function scanSingleFile(file: string): FileUiScan {
81
89
  const fromPrunelle = source === 'prunelle'
82
90
  if (!fromPrunelle && !isUiImportSource(source))
83
91
  continue
84
- if (!fromPrunelle && NAMESPACE_RE.test(clause)) {
85
- all = true
86
- break
92
+ if (NAMESPACE_RE.test(clause)) {
93
+ if (!fromPrunelle) {
94
+ all = true
95
+ break
96
+ }
97
+ addPrunelleParts(parts)
87
98
  }
88
99
  const named = NAMED_LIST_RE.exec(clause)
89
100
  if (named !== null) {
@@ -101,6 +112,14 @@ function scanSingleFile(file: string): FileUiScan {
101
112
  }
102
113
  }
103
114
 
115
+ // A dynamic import or a require hands over the whole module, so every part of it counts.
116
+ for (const match of code.matchAll(MODULE_CALL_RE)) {
117
+ if (isUiImportSource(match[1]!))
118
+ all = true
119
+ else if (match[1] === 'prunelle')
120
+ addPrunelleParts(parts)
121
+ }
122
+
104
123
  if (!all) {
105
124
  if (DATA_LAYOUT_ATTR_RE.test(code))
106
125
  parts.add('layout')