soma-core 1.3.2 → 1.3.4

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.
Files changed (281) hide show
  1. package/README.md +43 -0
  2. package/app/SomaPageClient.tsx +22 -68
  3. package/app/[...path]/page.tsx +7 -6
  4. package/app/api/[...path]/route.ts +3 -3
  5. package/app/api/soma-registry/[...path]/route.ts +67 -0
  6. package/app/components/SomaHostPage.tsx +3 -8
  7. package/app/components/SomaPageClientBoundary.tsx +13 -0
  8. package/app/layout.tsx +6 -1
  9. package/app/page.tsx +3 -3
  10. package/app/soma/manifest/route.ts +1 -0
  11. package/app/soma/page-object/route.ts +1 -1
  12. package/bin/soma-lsp.cjs +1 -4
  13. package/core/build/compiler/full-manifest-builder.ts +19 -23
  14. package/core/build/compiler/html-include-expander.ts +56 -0
  15. package/core/build/compiler/page-assets.ts +26 -0
  16. package/core/build/compiler/page-declaration-compiler.ts +85 -0
  17. package/core/build/compiler/rollup-plugin-soma.ts +4 -13
  18. package/core/build/compiler/route-builder.ts +0 -2
  19. package/core/build/compiler/rspack-plugin-soma.js +1 -10
  20. package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
  21. package/core/build/compiler/soma-precompile.js +3581 -2387
  22. package/core/build/compiler/soma-precompile.ts +44 -2
  23. package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
  24. package/core/build/compiler/soma-webpack-loader.js +6 -15
  25. package/core/build/compiler/vite-plugin-soma.ts +9 -21
  26. package/core/build/component/pack-from-dir.ts +58 -0
  27. package/core/build/component/package-loader.ts +281 -0
  28. package/core/build/eject/apply-eject.ts +133 -0
  29. package/core/build/eject/plan-eject.ts +133 -0
  30. package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
  31. package/core/build/lsp/extra-completion.cjs +14 -8
  32. package/core/build/lsp/lsp-navigation.cjs +50 -43
  33. package/core/build/lsp/soma-declaration.cjs +85 -0
  34. package/core/build/lsp/ui-yaml-completion.cjs +98 -104
  35. package/core/build/v2/block-compile.ts +200 -0
  36. package/core/build/v2/block-loader.ts +33 -0
  37. package/core/build/v2/html-index.ts +120 -0
  38. package/core/build/v2/migrate-v1.ts +370 -0
  39. package/core/build/v2/soma-file-loader.ts +216 -0
  40. package/core/client/adaptive/flush-scheduler.ts +96 -0
  41. package/core/client/adaptive/hysteresis.ts +56 -0
  42. package/core/client/adaptive/index.ts +23 -0
  43. package/core/client/adaptive/observer-pool.ts +90 -0
  44. package/core/client/base/SomaPageRootBase.ts +50 -0
  45. package/core/client/compiler/adaptive-compile.ts +136 -0
  46. package/core/client/compiler/manifest-compiler.ts +155 -27
  47. package/core/client/component/ai-catalog.ts +277 -0
  48. package/core/client/component/component-definition.types.ts +65 -0
  49. package/core/client/component/component-expander.ts +119 -0
  50. package/core/client/component/component-manager.ts +933 -0
  51. package/core/client/component/component-pack.ts +445 -0
  52. package/core/client/component/component-registry.ts +71 -0
  53. package/core/client/component/contract-compat.ts +132 -0
  54. package/core/client/component/example-v2.ts +132 -0
  55. package/core/client/component/pack-styles.ts +94 -0
  56. package/core/client/component/scoped-adapter.ts +85 -0
  57. package/core/client/component/verify-pack.ts +247 -0
  58. package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
  59. package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
  60. package/core/client/container/SomaContainerAdapter.ts +186 -180
  61. package/core/client/data/data-object-resolver.ts +417 -0
  62. package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
  63. package/core/client/diagnostics/boot-timings.ts +152 -0
  64. package/core/client/engine/asset-resolver.ts +12 -2
  65. package/core/client/engine/dom-renderer.ts +249 -24
  66. package/core/client/engine/emit-event.ts +21 -0
  67. package/core/client/engine/event-engine.ts +34 -4
  68. package/core/client/engine/on-change-engine.ts +1 -1
  69. package/core/client/engine/property-watcher.ts +1 -1
  70. package/core/client/engine/reactive-proxy.ts +11 -10
  71. package/core/client/engine/render-engine.ts +39 -4
  72. package/core/client/engine/runtime-builder.ts +23 -0
  73. package/core/client/engine/soma-bootstrap.ts +380 -7
  74. package/core/client/engine/style-binder.ts +47 -1
  75. package/core/client/observability/error-reporter.ts +173 -0
  76. package/core/client/parser/adaptive-parser.ts +264 -0
  77. package/core/client/parser/data-config-parser.ts +396 -0
  78. package/core/client/parser/pipeline-parser.ts +15 -3
  79. package/core/client/parser/style-config-parser.ts +65 -2
  80. package/core/client/parser/ui-config-parser.ts +52 -3
  81. package/core/client/parser/v2/namespace.ts +182 -0
  82. package/core/client/parser/v2/page-parser.ts +359 -0
  83. package/core/client/parser/v2/use-parser.ts +412 -0
  84. package/core/client/parser/value-lang.ts +340 -0
  85. package/core/client/pipes/pipe-registry.ts +249 -0
  86. package/core/client/quality/SomaQualityRegistry.ts +3 -0
  87. package/core/client/registry/declaration-renderer.ts +161 -0
  88. package/core/client/registry/index.ts +28 -0
  89. package/core/client/registry/page-lifecycle.ts +171 -0
  90. package/core/client/registry/remote-page.ts +139 -0
  91. package/core/client/runtime/SomaRuntimeServices.ts +24 -0
  92. package/core/client/security/attribute-presence.ts +70 -0
  93. package/core/client/security/html-sanitizer.ts +113 -0
  94. package/core/client/security/url-guard.ts +0 -0
  95. package/core/client/theme/harmony.ts +307 -0
  96. package/core/client/theme/scene-tokens.ts +134 -0
  97. package/core/client/theme/theme-axes.ts +228 -0
  98. package/core/client/theme/theme-engine.ts +1196 -0
  99. package/core/client/theme/tone-solver.ts +118 -0
  100. package/core/client/typography/typography-base.ts +34 -0
  101. package/core/client/validation/contract-validator.ts +48 -5
  102. package/core/index.ts +262 -2
  103. package/core/server/api/IService.ts +18 -6
  104. package/core/server/api/load-host-service.ts +3 -3
  105. package/core/server/host/content-security-policy.ts +190 -0
  106. package/core/server/host/csp-nonce-metadata.ts +75 -0
  107. package/core/server/host/resolve-host-request-hook.ts +5 -0
  108. package/core/server/host/server-page-assets.ts +34 -7
  109. package/core/server/host/soma-page-compile.ts +33 -0
  110. package/core/server/index.ts +45 -1
  111. package/core/server/infra/cache/ServerFileCache.ts +1 -1
  112. package/core/server/mock/node/cli.mjs +2 -2
  113. package/core/server/registry/api-registry-store.ts +518 -0
  114. package/core/server/registry/conditional-object-store.ts +29 -0
  115. package/core/server/registry/declaration-precompiler.ts +99 -0
  116. package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
  117. package/core/server/registry/index.ts +142 -0
  118. package/core/server/registry/page-registry-repository.ts +373 -0
  119. package/core/server/registry/page-registry-store.ts +932 -0
  120. package/core/server/registry/registry-error.ts +17 -0
  121. package/core/server/registry/registry-file-lock.ts +173 -0
  122. package/core/server/registry/registry-response.ts +71 -0
  123. package/core/server/registry/registry-subjects.ts +194 -0
  124. package/core/server/registry/registry-write-pipeline.ts +240 -0
  125. package/core/server/registry/request-body-limit.ts +99 -0
  126. package/core/server/registry/services/apis/Get.ts +25 -0
  127. package/core/server/registry/services/apis/List.ts +41 -0
  128. package/core/server/registry/services/apis/Register.ts +39 -0
  129. package/core/server/registry/services/apis/Unregister.ts +24 -0
  130. package/core/server/registry/services/index.ts +40 -0
  131. package/core/server/registry/services/pages/Get.ts +96 -0
  132. package/core/server/registry/services/pages/List.ts +53 -0
  133. package/core/server/registry/services/pages/Register.ts +49 -0
  134. package/core/server/registry/services/pages/Rollback.ts +36 -0
  135. package/core/server/registry/services/pages/Unregister.ts +23 -0
  136. package/core/server/registry/services/service-utils.ts +21 -0
  137. package/core/server/registry/validation/compile-gate.ts +54 -0
  138. package/core/server/registry/validation/component-existence.ts +55 -0
  139. package/core/server/registry/validation/credential-ban.ts +86 -0
  140. package/core/server/registry/validation/cross-check.ts +67 -0
  141. package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
  142. package/core/shared/types/adaptive.types.ts +109 -0
  143. package/core/shared/types/component-contract.types.ts +120 -0
  144. package/core/shared/types/data-config.types.ts +105 -0
  145. package/core/shared/types/domain.types.ts +6 -0
  146. package/core/shared/types/engine.types.ts +41 -35
  147. package/core/shared/types/manifest.types.ts +55 -32
  148. package/core/shared/types/scope-manifest.types.ts +96 -0
  149. package/core/shared/types/style-config.types.ts +9 -0
  150. package/core/shared/types/ui-config.types.ts +118 -98
  151. package/dist/cli/soma.js +20 -5
  152. package/dist/core/index.cjs +7811 -1380
  153. package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
  154. package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
  155. package/dist/types/build/compiler/page-assets.d.ts +3 -0
  156. package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
  157. package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
  158. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  159. package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
  160. package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
  161. package/dist/types/build/component/pack-from-dir.d.ts +17 -0
  162. package/dist/types/build/component/package-loader.d.ts +40 -0
  163. package/dist/types/build/eject/apply-eject.d.ts +22 -0
  164. package/dist/types/build/eject/plan-eject.d.ts +32 -0
  165. package/dist/types/build/v2/block-compile.d.ts +28 -0
  166. package/dist/types/build/v2/block-loader.d.ts +9 -0
  167. package/dist/types/build/v2/html-index.d.ts +37 -0
  168. package/dist/types/build/v2/migrate-v1.d.ts +32 -0
  169. package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
  170. package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
  171. package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
  172. package/dist/types/client/adaptive/index.d.ts +5 -0
  173. package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
  174. package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
  175. package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
  176. package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
  177. package/dist/types/client/component/ai-catalog.d.ts +73 -0
  178. package/dist/types/client/component/component-definition.types.d.ts +54 -0
  179. package/dist/types/client/component/component-expander.d.ts +25 -0
  180. package/dist/types/client/component/component-manager.d.ts +160 -0
  181. package/dist/types/client/component/component-pack.d.ts +110 -0
  182. package/dist/types/client/component/component-registry.d.ts +12 -0
  183. package/dist/types/client/component/contract-compat.d.ts +16 -0
  184. package/dist/types/client/component/example-v2.d.ts +9 -0
  185. package/dist/types/client/component/pack-styles.d.ts +22 -0
  186. package/dist/types/client/component/scoped-adapter.d.ts +18 -0
  187. package/dist/types/client/component/verify-pack.d.ts +14 -0
  188. package/dist/types/client/data/data-object-resolver.d.ts +71 -0
  189. package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
  190. package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
  191. package/dist/types/client/engine/asset-resolver.d.ts +7 -1
  192. package/dist/types/client/engine/dom-renderer.d.ts +51 -1
  193. package/dist/types/client/engine/emit-event.d.ts +6 -0
  194. package/dist/types/client/engine/event-engine.d.ts +6 -0
  195. package/dist/types/client/engine/render-engine.d.ts +25 -1
  196. package/dist/types/client/engine/runtime-builder.d.ts +7 -0
  197. package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
  198. package/dist/types/client/observability/error-reporter.d.ts +51 -0
  199. package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
  200. package/dist/types/client/parser/data-config-parser.d.ts +34 -0
  201. package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
  202. package/dist/types/client/parser/style-config-parser.d.ts +5 -3
  203. package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
  204. package/dist/types/client/parser/v2/namespace.d.ts +52 -0
  205. package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
  206. package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
  207. package/dist/types/client/parser/value-lang.d.ts +63 -0
  208. package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
  209. package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
  210. package/dist/types/client/registry/index.d.ts +10 -0
  211. package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
  212. package/dist/types/client/registry/remote-page.d.ts +52 -0
  213. package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
  214. package/dist/types/client/security/attribute-presence.d.ts +7 -0
  215. package/dist/types/client/security/html-sanitizer.d.ts +37 -0
  216. package/dist/types/client/security/url-guard.d.ts +11 -0
  217. package/dist/types/client/theme/harmony.d.ts +77 -0
  218. package/dist/types/client/theme/scene-tokens.d.ts +25 -0
  219. package/dist/types/client/theme/theme-axes.d.ts +81 -0
  220. package/dist/types/client/theme/theme-engine.d.ts +77 -0
  221. package/dist/types/client/theme/tone-solver.d.ts +47 -0
  222. package/dist/types/client/typography/typography-base.d.ts +15 -0
  223. package/dist/types/client/validation/contract-validator.d.ts +2 -0
  224. package/dist/types/index.d.ts +47 -2
  225. package/dist/types/server/api/IService.d.ts +18 -6
  226. package/dist/types/server/host/content-security-policy.d.ts +53 -0
  227. package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
  228. package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
  229. package/dist/types/server/host/server-page-assets.d.ts +2 -0
  230. package/dist/types/server/host/soma-page-compile.d.ts +7 -0
  231. package/dist/types/server/index.d.ts +6 -1
  232. package/dist/types/server/registry/api-registry-store.d.ts +141 -0
  233. package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
  234. package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
  235. package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
  236. package/dist/types/server/registry/index.d.ts +29 -0
  237. package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
  238. package/dist/types/server/registry/page-registry-store.d.ts +259 -0
  239. package/dist/types/server/registry/registry-error.d.ts +12 -0
  240. package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
  241. package/dist/types/server/registry/registry-response.d.ts +18 -0
  242. package/dist/types/server/registry/registry-subjects.d.ts +59 -0
  243. package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
  244. package/dist/types/server/registry/request-body-limit.d.ts +31 -0
  245. package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
  246. package/dist/types/server/registry/services/apis/List.d.ts +10 -0
  247. package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
  248. package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
  249. package/dist/types/server/registry/services/index.d.ts +3 -0
  250. package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
  251. package/dist/types/server/registry/services/pages/List.d.ts +13 -0
  252. package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
  253. package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
  254. package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
  255. package/dist/types/server/registry/services/service-utils.d.ts +10 -0
  256. package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
  257. package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
  258. package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
  259. package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
  260. package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
  261. package/dist/types/shared/types/adaptive.types.d.ts +88 -0
  262. package/dist/types/shared/types/component-contract.types.d.ts +78 -0
  263. package/dist/types/shared/types/data-config.types.d.ts +81 -0
  264. package/dist/types/shared/types/domain.types.d.ts +6 -0
  265. package/dist/types/shared/types/engine.types.d.ts +6 -0
  266. package/dist/types/shared/types/manifest.types.d.ts +23 -0
  267. package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
  268. package/dist/types/shared/types/style-config.types.d.ts +8 -0
  269. package/dist/types/shared/types/ui-config.types.d.ts +20 -0
  270. package/editor/vscode/package.json +1 -24
  271. package/global.d.ts +5 -11
  272. package/next.config.js +85 -28
  273. package/package.json +33 -11
  274. package/proxy.ts +62 -0
  275. package/tailwind.config.ts +0 -1
  276. package/core/build/lsp/event-yaml-completion.cjs +0 -106
  277. package/core/client/engine/theme-variables.ts +0 -37
  278. package/dist/tsconfig.types.tsbuildinfo +0 -1
  279. package/dist/types/client/engine/theme-variables.d.ts +0 -3
  280. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
  281. package/middleware.ts +0 -33
@@ -0,0 +1,132 @@
1
+ // 契约示例的 v2 派生(REQ-005 F8.4 生态再生)。
2
+ //
3
+ // 315 份契约的 `ai.example` 是 v1 三行式(html / ui / event)。DSL v2 落地后
4
+ // AI 需要看到 v2 写法,否则它学到的永远是旧格式——而"生成质量的知识沉在
5
+ // 通用资产里"这条工程铁律,说的就是这种地方。
6
+ //
7
+ // ── 为什么是派生而不是改 315 个源文件 ──────────────────────────────────
8
+ // 改源文件要动 315 处,且从此 v1/v2 两份示例并存、各自演化、必然漂移。
9
+ // 派生只有一处实现:契约仍是单一真源,v2 示例由 v1 示例机械换算而来,
10
+ // 两者不可能不一致——因为后者是前者的函数。
11
+ //
12
+ // ── 换算规则(v2 的两处结构差异)─────────────────────────────────────
13
+ // 1. 组件实例**自成顶层块**(v2 第一公理:"组件是声明的第一公民"):
14
+ // v1 的 `锚点: { props: {…} }` → v2 的 `锚点:` 块 + `component:` + `ui:`;
15
+ // 2. 实例的 emit 路由并进块内的 `event.on`,不再写在页面级事件文件里。
16
+ //
17
+ // 换不出来的(示例本身不是标准三行式)就不产 v2 行——宁可少给一份示例,
18
+ // 也不要给一份**看着对、抄过去装载不进去**的示例。
19
+
20
+ /** v1 示例的三行前缀 */
21
+ const HTML_PREFIX = 'html:';
22
+ const UI_PREFIX = 'ui:';
23
+ const EVENT_PREFIX = 'event:';
24
+
25
+ interface ParsedExample {
26
+ anchor: string;
27
+ /** props 段原文(花括号内,不解析成对象——原样搬更保真) */
28
+ propsBody: string;
29
+ /** emit 名 → Pipeline 表达式 */
30
+ emits: Array<[string, string]>;
31
+ }
32
+
33
+ /**
34
+ * 从 `start` 起截到与之配平的那个 `}` 之前(不含)。
35
+ *
36
+ * 调用时 `start` 指向 props 花括号**内部**的第一个字符,深度记作 1;
37
+ * 括号配平到 0 时就是 props 段的收口位。返回 null 表示没配平(示例本身写坏了)。
38
+ */
39
+ function balancedSlice(text: string, start: number): string | null {
40
+ let depth = 1;
41
+ for (let i = start; i < text.length; i += 1) {
42
+ const ch = text[i];
43
+ if (ch === '{' || ch === '[') depth += 1;
44
+ else if (ch === '}' || ch === ']') {
45
+ depth -= 1;
46
+ if (depth === 0) return text.slice(start, i).trim();
47
+ }
48
+ }
49
+ return null;
50
+ }
51
+
52
+ /** 取一行里以某前缀开头的内容(去前缀、去两端空白) */
53
+ function lineAfter(lines: readonly string[], prefix: string): string | null {
54
+ const hit = lines.find((line) => line.trimStart().startsWith(prefix));
55
+ return hit ? hit.trimStart().slice(prefix.length).trim() : null;
56
+ }
57
+
58
+ /**
59
+ * 解析 v1 示例的 ui / event 两行。
60
+ *
61
+ * 只认最规整的那一种形状:`锚点: { props: { … } }`。
62
+ * 示例写得更花的(多锚点、嵌套 show、列表作用域)一律返回 null——
63
+ * 那些形状换算成 v2 需要真的理解作用域,不是字符串搬运;
64
+ * 猜错了产出的是能骗过肉眼、装载器却拒收的示例。
65
+ */
66
+ function parseV1Example(example: string): ParsedExample | null {
67
+ const lines = example.split('\n');
68
+ const ui = lineAfter(lines, UI_PREFIX);
69
+ if (!ui) return null;
70
+
71
+ // 头部只匹配到 `锚点: { props: {`,**尾部靠括号配平找**,不用贪婪正则。
72
+ //
73
+ // 实测教训:`/\{([\s\S]*)\}\s*\}/` 在 props 含嵌套花括号时(DetailConsole 的
74
+ // `path_params: { id: … }`、QueryConsole 的 `fields: [{ … }]`)会一路吃到
75
+ // 最后一个 `}`,切出来的 props 段少一层收口——产出的示例 YAML 解析失败。
76
+ // 这类错误最坏的地方是**它只发生在结构最复杂的那几个组件上**,
77
+ // 而那几个恰恰是 AI 最需要示例的。
78
+ const head = /^([A-Za-z_][\w-]*)\s*:\s*\{\s*props\s*:\s*\{/.exec(ui);
79
+ if (!head) return null;
80
+ const [, anchor] = head;
81
+ const propsBody = balancedSlice(ui, head[0].length);
82
+ if (propsBody === null) return null;
83
+
84
+ const emits: Array<[string, string]> = [];
85
+ const event = lineAfter(lines, EVENT_PREFIX);
86
+ if (event && !event.startsWith('(') && event !== '') {
87
+ // `锚点.emit名: 表达式`——只收属于本实例的
88
+ const at = event.indexOf(':');
89
+ if (at > 0) {
90
+ const key = event.slice(0, at).trim();
91
+ const expr = event.slice(at + 1).trim();
92
+ const dot = key.indexOf('.');
93
+ if (dot > 0 && key.slice(0, dot) === anchor) {
94
+ emits.push([key.slice(dot + 1), expr]);
95
+ }
96
+ }
97
+ }
98
+ return { anchor, propsBody: propsBody.trim(), emits };
99
+ }
100
+
101
+ /**
102
+ * 由 v1 示例派生一行 v2 写法;换算不出来时返回 null。
103
+ *
104
+ * 产出的是**块声明**片段(写进 `_ui.yaml` 的 `{{ … }}` 之内),
105
+ * html 那一行 v1 v2 通用,不重复。
106
+ */
107
+ export function deriveV2Example(componentName: string, example: string): string | null {
108
+ const parsed = parseV1Example(example);
109
+ if (!parsed) return null;
110
+
111
+ // props 逐字与 v1 的 ui 行相同——**不重复贴一遍**,写成"同上"。
112
+ //
113
+ // 目录是喂给模型的,字节即成本(体积预算闸门盯着最大单分片)。
114
+ // 重贴一份 props 对 AI 没有增量信息:它要学的是**结构差异**
115
+ // (实例自成顶层块、component 键、事件并进块内),不是同一串 props 抄两遍。
116
+ const lines = [
117
+ `${parsed.anchor}:`,
118
+ ` component: ${componentName}`,
119
+ ' ui: { …同上 props… }',
120
+ ];
121
+ if (parsed.emits.length > 0) {
122
+ lines.push(' event:');
123
+ lines.push(' on:');
124
+ for (const [name, expr] of parsed.emits) lines.push(` ${name}: ${expr}`);
125
+ }
126
+ return lines.join('\n');
127
+ }
128
+
129
+ /** 示例里的 html 行(v1 v2 通用),供 v2 段复用而不重复贴一遍 */
130
+ export function exampleHtmlLine(example: string): string | null {
131
+ return lineAfter(example.split('\n'), HTML_PREFIX);
132
+ }
@@ -0,0 +1,94 @@
1
+ // 组件包的按需样式装配。
2
+ //
3
+ // 机制与 soma-ui 一致(它的 withSomaUiLazyStyles 是同一个套路):把组件的
4
+ // controllerFactory 包一层,**首次实例化**时才注入这个组件的 CSS 分片。
5
+ // 页面付的是"我用到的那几个"的钱,包的规模不再是每个页面的首屏成本。
6
+ //
7
+ // 两个刻意的决定:
8
+ // 1. **默认就包上**(composeComponentPacks 直接返回包好的定义)。
9
+ // 要求第三方包作者记得再调一次注入函数,是个必然被忘记的接线——
10
+ // 忘记的表现是"组件渲染出来但没样式",而且不报错;
11
+ // 2. **注入发生在实例化那一刻**,不在合成时。合成是纯数据操作,要能在 Node 里
12
+ // 跑测试;真正碰 document 的时机永远在浏览器里。
13
+ //
14
+ // 幂等:每个分片只注入一次(注入过的键记在模块级 Set 里,并在 <style> 上盖 data 属性,
15
+ // 这样热更新重复执行也不会堆出十几张同样的 <style>)。
16
+
17
+ import type { SomaComponentDefinition } from './component-definition.types';
18
+ import type { ComposedComponentPacks } from './component-pack';
19
+
20
+ const injected = new Set<string>();
21
+
22
+ export interface PackStyleOptions {
23
+ /** CSP nonce;宿主启用严格 CSP 时必须给 */
24
+ nonce?: string | null;
25
+ }
26
+
27
+ /** 测试用:清空注入记录(jsdom 之间不共享 document,但模块级 Set 会跨用例残留) */
28
+ export function resetPackStyleInjection(): void {
29
+ injected.clear();
30
+ }
31
+
32
+ function injectOnce(key: string, css: string, options: PackStyleOptions): void {
33
+ if (!css.trim() || injected.has(key)) return;
34
+ if (typeof document === 'undefined') return;
35
+ injected.add(key);
36
+ const existing = document.querySelector(`style[data-soma-pack-style="${key}"]`);
37
+ if (existing) return;
38
+ const style = document.createElement('style');
39
+ style.setAttribute('data-soma-pack-style', key);
40
+ if (options.nonce) style.setAttribute('nonce', options.nonce);
41
+ style.textContent = css;
42
+ document.head.appendChild(style);
43
+ }
44
+
45
+ /**
46
+ * 给合成结果里的每个组件接上按需样式。
47
+ *
48
+ * 注入顺序是**先共享分片、再组件分片**:组件分片经常覆盖共享分片里的默认值,
49
+ * 反过来注入会让覆盖失效——而这种失效只在"共享分片恰好晚注入"时出现,
50
+ * 也就是取决于页面先实例化了哪个组件,是最难复现的一类样式 bug。
51
+ */
52
+ export function attachPackStyles(
53
+ composed: ComposedComponentPacks,
54
+ options: PackStyleOptions = {},
55
+ ): Record<string, SomaComponentDefinition> {
56
+ const { components, styles } = composed;
57
+ const result: Record<string, SomaComponentDefinition> = {};
58
+
59
+ for (const [name, definition] of Object.entries(components) as Array<[string, SomaComponentDefinition]>) {
60
+ const own = styles.components[name];
61
+ const deps = styles.deps[name] ?? [];
62
+ if (!own && deps.length === 0) {
63
+ result[name] = definition;
64
+ continue;
65
+ }
66
+ const inner = definition.controllerFactory;
67
+ result[name] = {
68
+ ...definition,
69
+ controllerFactory: (ctx) => {
70
+ for (const dep of deps) {
71
+ const shared = styles.shared[dep];
72
+ if (shared) injectOnce(`shared:${dep}`, shared, options);
73
+ }
74
+ if (own) injectOnce(`component:${name}`, own, options);
75
+ return inner ? inner(ctx) : {};
76
+ },
77
+ };
78
+ }
79
+ return result;
80
+ }
81
+
82
+ /**
83
+ * 注入包级基座 CSS(恒定注入,不等实例化)。
84
+ * 基座是每个页面首屏都要付的钱,所以接入指南里写着"克制使用";
85
+ * 这里按包分别盖戳,便于排查"这段全局样式是谁带来的"。
86
+ */
87
+ export function ensurePackBaseStyles(
88
+ composed: ComposedComponentPacks,
89
+ options: PackStyleOptions = {},
90
+ ): void {
91
+ composed.styles.base.forEach((css, index) => {
92
+ injectOnce(`base:${index}`, css, options);
93
+ });
94
+ }
@@ -0,0 +1,85 @@
1
+ // 组件实例作用域容器适配器。
2
+ //
3
+ // 实例内部锚点使用 data-soma-scoped-id(展开器改写产物),对页面引擎不可见;
4
+ // 实例引擎组(渲染/事件)经本适配器寻址,查询根为宿主元素,天然按实例隔离。
5
+ //
6
+ // 「查询根 = 宿主元素」这一条隔离在**父组件嵌子组件**时不够用:子组件展开后,
7
+ // 它的内部锚点也带 data-soma-scoped-id,且就住在父宿主的子树里——单看属性名
8
+ // 父子锚点无法区分。而插槽投影又要求父能看见"被搬进子组件出口里的那段内容"
9
+ // (那段是父模板写的,归父作用域)。所以隔离维度必须是**作用域归属**而不是"树位置":
10
+ // 展开器给每次展开分配一个 scopeId,本次展开所写下的每个锚点都打上 data-soma-scope,
11
+ // 适配器只认自己那个 scopeId。投影后内容搬了家,归属不变,父照样找得到;
12
+ // 子组件自己的锚点则永远不会被父吸进清单。
13
+ //
14
+ // scopeId 省略时退化为"认所有 scoped 锚点"(旧行为),仅用于不涉及嵌套的场景与测试。
15
+
16
+ import { SomaContainerAdapter } from '../container/SomaContainerAdapter';
17
+ import type { SomaAnchorStats } from '../container/IContainerAdapter';
18
+
19
+ export const SOMA_SCOPED_ID_ATTR = 'data-soma-scoped-id';
20
+ /** 锚点的作用域归属标记(展开器写入,值为该次展开的 scopeId) */
21
+ export const SOMA_SCOPE_ATTR = 'data-soma-scope';
22
+
23
+ export class SomaScopedContainerAdapter extends SomaContainerAdapter {
24
+ /** 本适配器认领的作用域;undefined = 认所有 scoped 锚点 */
25
+ private readonly scopeId?: string;
26
+ /** `[data-soma-scoped-id][data-soma-scope="X"]`,预拼避免每次查询字符串拼接 */
27
+ private readonly anyAnchorSelector: string;
28
+
29
+ constructor(scopeId?: string) {
30
+ super();
31
+ this.scopeId = scopeId;
32
+ this.anyAnchorSelector = scopeId
33
+ ? `[${SOMA_SCOPED_ID_ATTR}][${SOMA_SCOPE_ATTR}="${scopeId}"]`
34
+ : `[${SOMA_SCOPED_ID_ATTR}]`;
35
+ }
36
+
37
+ getScopeId(): string | undefined {
38
+ return this.scopeId;
39
+ }
40
+
41
+ private anchorSelector(somaId: string): string {
42
+ return this.scopeId
43
+ ? `[${SOMA_SCOPED_ID_ATTR}="${somaId}"][${SOMA_SCOPE_ATTR}="${this.scopeId}"]`
44
+ : `[${SOMA_SCOPED_ID_ATTR}="${somaId}"]`;
45
+ }
46
+
47
+ findAnchor(root: ParentNode, somaId: string): HTMLElement | null {
48
+ return root.querySelector<HTMLElement>(this.anchorSelector(somaId));
49
+ }
50
+
51
+ findClosestAnchor(target: EventTarget | null): HTMLElement | null {
52
+ if (!(target instanceof HTMLElement)) {
53
+ return null;
54
+ }
55
+ return target.closest<HTMLElement>(this.anyAnchorSelector);
56
+ }
57
+
58
+ getAnchorId(element: HTMLElement): string | null {
59
+ if (this.scopeId && element.getAttribute(SOMA_SCOPE_ATTR) !== this.scopeId) {
60
+ return null;
61
+ }
62
+ return element.getAttribute(SOMA_SCOPED_ID_ATTR);
63
+ }
64
+
65
+ collectAnchors(container: HTMLElement): SomaAnchorStats {
66
+ const anchors = new Set<string>();
67
+ const counts = new Map<string, number>();
68
+
69
+ container.querySelectorAll<HTMLElement>(this.anyAnchorSelector).forEach((element) => {
70
+ const somaId = element.getAttribute(SOMA_SCOPED_ID_ATTR);
71
+ if (!somaId) return;
72
+ anchors.add(somaId);
73
+ counts.set(somaId, (counts.get(somaId) ?? 0) + 1);
74
+ });
75
+
76
+ const duplicates = new Map<string, number>();
77
+ for (const [somaId, count] of counts.entries()) {
78
+ if (count > 1) {
79
+ duplicates.set(somaId, count);
80
+ }
81
+ }
82
+
83
+ return { anchors, duplicates };
84
+ }
85
+ }
@@ -0,0 +1,247 @@
1
+ // 组件包自检闸门。
2
+ //
3
+ // 它拦的都是**不报错但坏掉**的那类问题——这是选题标准,不是随便挑的规则:
4
+ // 裸色值 → 换肤对你的组件失效,页面照常渲染,没有任何测试会红;
5
+ // 物理方向属性 → RTL 下内边距跑到另一边,同样不报错;
6
+ // 缺 ai 段 → AI 永远不会选你的组件,库形同不存在;
7
+ // 可选 prop 没默认值 → AI 少写一个 prop 就白屏。
8
+ // 会报错的问题(模板语法错、契约类型不对)交给解析器与类型系统,不必在这里重复。
9
+ //
10
+ // 定位是**第三方包的准入线**,不是 soma-ui 的全套约束:视觉验收、i18n 词典、
11
+ // 令牌存在性、按需装配顺序那些与 soma-ui 的资产强绑定,进 soma-ui 时再补
12
+ // (见接入指南 §7 上游路径)。
13
+
14
+ import type { SomaComponentDefinition } from './component-definition.types';
15
+ import type { SomaComponentPack } from './component-pack';
16
+ import { PACK_NAME_PATTERN } from './component-pack';
17
+ import { findNestedMutableContractDefault } from '../../shared/types/component-contract.types';
18
+
19
+ const LAYERS = new Set(['atom', 'composite', 'solution', 'template', 'page', 'token']);
20
+
21
+ /** 颜色只允许出现在 var(--soma-*, 回退值) 的回退位 */
22
+ const NAKED_COLOR = /(#[0-9a-fA-F]{3,8}\b|\b(?:rgba?|hsla?)\s*\()/g;
23
+ /** 物理方向属性:RTL 下会错位,且不报错 */
24
+ const PHYSICAL_DIRECTION = /(?:^|[;{\s])(margin-(?:left|right)|padding-(?:left|right)|border-(?:left|right)(?:-\w+)?|(?:^|\s)(?:left|right)\s*:)/g;
25
+ /** 记名例外:写了这个注释的行放行(物理方向偶尔确实是对的,但要说明理由) */
26
+ const EXCEPTION_MARK = /soma-allow-physical/;
27
+
28
+ export interface VerifyPackOptions {
29
+ /**
30
+ * 关掉颜色/方向的样式检查(默认开)。
31
+ * 只有在包完全不带样式、或样式由宿主统一提供时才该关——关掉意味着
32
+ * 换肤与 RTL 的正确性没有任何自动保障。
33
+ */
34
+ skipStyleChecks?: boolean;
35
+ }
36
+
37
+ /**
38
+ * 自检一个组件包,返回中文问题清单(空数组 = 通过)。
39
+ * 与体系里其它闸门同款约定:命中即拒,清单点名到具体组件与字段。
40
+ */
41
+ export function verifyComponentPack(
42
+ pack: SomaComponentPack,
43
+ options: VerifyPackOptions = {},
44
+ ): string[] {
45
+ const issues: string[] = [];
46
+
47
+ if (!pack || typeof pack !== 'object') return ['[包] 组件包必须是对象'];
48
+ if (typeof pack.name !== 'string' || !PACK_NAME_PATTERN.test(pack.name)) {
49
+ issues.push(`[包] 包名 "${String(pack.name)}" 必须匹配 [a-z][a-z0-9-]*`);
50
+ }
51
+ if (typeof pack.version !== 'string' || !pack.version.trim()) {
52
+ issues.push('[包] 缺少 version');
53
+ }
54
+ if (!pack.components || typeof pack.components !== 'object' || Array.isArray(pack.components)) {
55
+ issues.push('[包] components 必须是 { 组件名: 定义 } 对象');
56
+ return issues;
57
+ }
58
+ if (Object.keys(pack.components).length === 0) {
59
+ issues.push('[包] 一个组件都没有');
60
+ }
61
+
62
+ for (const [key, definition] of Object.entries(pack.components)) {
63
+ issues.push(...verifyDefinition(key, definition));
64
+ }
65
+ issues.push(...verifyStyleRegistration(pack));
66
+ if (!options.skipStyleChecks) {
67
+ issues.push(...verifyStyleDiscipline(pack));
68
+ }
69
+ return issues;
70
+ }
71
+
72
+ function verifyDefinition(key: string, definition: SomaComponentDefinition): string[] {
73
+ const issues: string[] = [];
74
+ const label = `组件 ${key}`;
75
+ if (!definition || typeof definition !== 'object') return [`[契约] ${label} 的定义不是对象`];
76
+
77
+ const contract = definition.contract as unknown as Record<string, unknown> | undefined;
78
+ if (!contract || typeof contract !== 'object') {
79
+ return [`[契约] ${label} 缺少 contract`];
80
+ }
81
+ if (contract.name !== key) {
82
+ issues.push(
83
+ `[契约] ${label} 的契约 name 是 "${String(contract.name)}"——`
84
+ + '注册键与契约名必须一致,否则目录里的名字与页面里写的名字对不上',
85
+ );
86
+ }
87
+ if (typeof contract.name !== 'string' || !/^[A-Z][A-Za-z0-9]*$/.test(String(contract.name))) {
88
+ issues.push(`[契约] ${label} 的 name 必须是 PascalCase`);
89
+ }
90
+ if (typeof contract.layer !== 'string' || !LAYERS.has(String(contract.layer))) {
91
+ issues.push(`[契约] ${label} 的 layer "${String(contract.layer)}" 非法(atom/composite/solution/template)`);
92
+ }
93
+ for (const field of ['version', 'description'] as const) {
94
+ if (typeof contract[field] !== 'string' || !String(contract[field]).trim()) {
95
+ issues.push(`[契约] ${label} 缺少 ${field}`);
96
+ }
97
+ }
98
+
99
+ const props = (contract.props as Record<string, Record<string, unknown>> | undefined) ?? {};
100
+ for (const [propName, prop] of Object.entries(props)) {
101
+ if (!prop || typeof prop !== 'object') {
102
+ issues.push(`[契约] ${label} 的 prop "${propName}" 不是对象`);
103
+ continue;
104
+ }
105
+ if (typeof prop.type !== 'string' || !prop.type) {
106
+ issues.push(`[契约] ${label} 的 prop "${propName}" 缺少 type`);
107
+ }
108
+ if (typeof prop.desc !== 'string' || !prop.desc.trim()) {
109
+ issues.push(
110
+ `[契约] ${label} 的 prop "${propName}" 缺少 desc——没有描述的 prop 对 AI 等于不存在(规则 C1)`,
111
+ );
112
+ }
113
+ if (prop.required !== true && prop.default === undefined) {
114
+ issues.push(
115
+ `[契约] ${label} 的可选 prop "${propName}" 缺少 default——零 props 也要能渲染(规则 C4)`,
116
+ );
117
+ }
118
+ const nestedMutablePath = findNestedMutableContractDefault(prop.default);
119
+ if (nestedMutablePath) {
120
+ issues.push(
121
+ `[契约] ${label} 的 prop "${propName}" default.${nestedMutablePath} 是嵌套可变默认值——运行时只隔离最外层(规则 C4a)`,
122
+ );
123
+ }
124
+ }
125
+
126
+ const ai = contract.ai as Record<string, unknown> | undefined;
127
+ if (!ai || typeof ai !== 'object') {
128
+ issues.push(
129
+ `[目录] ${label} 缺少契约 ai 段——AI 选组件靠 when_to_use,没有它模型只能靠组件名猜`,
130
+ );
131
+ } else {
132
+ if (typeof ai.when_to_use !== 'string' || !ai.when_to_use.trim()) {
133
+ issues.push(`[目录] ${label} 的 ai.when_to_use 为空——写场景而不是功能`);
134
+ }
135
+ if (ai.example === undefined || (typeof ai.example === 'string' && !ai.example.trim())) {
136
+ issues.push(`[目录] ${label} 的 ai.example 为空——给一段能直接用的四件套片段`);
137
+ }
138
+ }
139
+
140
+ const template = definition.templateHtml;
141
+ if (typeof template !== 'string' || !template.trim()) {
142
+ issues.push(`[模板] ${label} 的 templateHtml 为空`);
143
+ } else {
144
+ if (/data-soma-scoped-id=/.test(template)) {
145
+ issues.push(
146
+ `[模板] ${label} 的模板里出现 data-soma-scoped-id——内部锚点写 data-soma-id,`
147
+ + '作用域改写由展开器负责',
148
+ );
149
+ }
150
+ if (/data-soma-host=/.test(template)) {
151
+ issues.push(`[模板] ${label} 的模板里出现 data-soma-host——那是页面的东西,组件模板不该有`);
152
+ }
153
+ const slots = (contract.slots as Record<string, unknown> | undefined) ?? {};
154
+ for (const slotName of Object.keys(slots)) {
155
+ if (!template.includes(`data-soma-slot-outlet="${slotName}"`)) {
156
+ issues.push(
157
+ `[模板] ${label} 声明了插槽 "${slotName}" 但模板里没有对应的 `
158
+ + `data-soma-slot-outlet="${slotName}" 出口`,
159
+ );
160
+ }
161
+ }
162
+ }
163
+ return issues;
164
+ }
165
+
166
+ /** 样式登记的键必须是本包真的提供的组件/分片 */
167
+ function verifyStyleRegistration(pack: SomaComponentPack): string[] {
168
+ const issues: string[] = [];
169
+ const styles = pack.styles;
170
+ if (!styles) return issues;
171
+ const componentNames = new Set(Object.keys(pack.components ?? {}));
172
+ const sharedNames = new Set(Object.keys(styles.shared ?? {}));
173
+
174
+ for (const name of Object.keys(styles.components ?? {})) {
175
+ if (!componentNames.has(name)) {
176
+ issues.push(
177
+ `[样式] 为 "${name}" 登记了组件样式,但包里没有这个组件——`
178
+ + '组件名写错时样式永远不会注入,而页面只是"看起来有点不对"',
179
+ );
180
+ }
181
+ }
182
+ for (const [name, deps] of Object.entries(styles.deps ?? {})) {
183
+ if (!componentNames.has(name)) {
184
+ issues.push(`[样式] 为 "${name}" 登记了共享分片依赖,但包里没有这个组件`);
185
+ }
186
+ for (const dep of deps ?? []) {
187
+ if (!sharedNames.has(dep)) {
188
+ issues.push(`[样式] 组件 "${name}" 依赖的共享分片 "${dep}" 未在 styles.shared 里提供`);
189
+ }
190
+ }
191
+ }
192
+ return issues;
193
+ }
194
+
195
+ /** 颜色纪律与逻辑属性:两条都是"不报错但坏掉"的典型 */
196
+ function verifyStyleDiscipline(pack: SomaComponentPack): string[] {
197
+ const issues: string[] = [];
198
+ const styles = pack.styles;
199
+ if (!styles) return issues;
200
+
201
+ const sources: Array<[string, string]> = [
202
+ ...Object.entries(styles.components ?? {}).map(([name, css]) => [`组件 ${name}`, css] as [string, string]),
203
+ ...Object.entries(styles.shared ?? {}).map(([name, css]) => [`共享分片 ${name}`, css] as [string, string]),
204
+ ...(styles.base ? ([['包基座', styles.base]] as Array<[string, string]>) : []),
205
+ ];
206
+
207
+ for (const [label, css] of sources) {
208
+ if (typeof css !== 'string') continue;
209
+ for (const color of nakedColors(css)) {
210
+ issues.push(
211
+ `[颜色] ${label} 出现裸色值 ${color}——颜色只允许在 var(--soma-*, 回退值) 的回退位;`
212
+ + '裸色值会让换肤对这个组件失效,而且不报错',
213
+ );
214
+ }
215
+ for (const property of physicalDirections(css)) {
216
+ issues.push(
217
+ `[RTL] ${label} 使用物理方向属性 ${property}——请改用逻辑属性`
218
+ + '(margin-inline-start / padding-inline-end / inset-inline-*);'
219
+ + 'RTL 下物理方向会让间距跑到另一边,同样不报错。确实必要就在同一行写 /* soma-allow-physical: 理由 */',
220
+ );
221
+ }
222
+ }
223
+ return issues;
224
+ }
225
+
226
+ /** 收集裸色值:var(--x, #fff) 的回退位不算 */
227
+ function nakedColors(css: string): string[] {
228
+ const found = new Set<string>();
229
+ // 先把合法的 var(--soma-*, 回退) 整段抹掉,剩下的颜色就是裸的
230
+ const stripped = css.replace(/var\(\s*--[\w-]+\s*(?:,[^()]*(?:\([^()]*\)[^()]*)*)?\)/g, ' ');
231
+ for (const match of stripped.matchAll(NAKED_COLOR)) {
232
+ found.add(match[0].replace(/\($/, '(').trim());
233
+ }
234
+ return [...found];
235
+ }
236
+
237
+ /** 收集物理方向属性;带记名例外注释的行放行 */
238
+ function physicalDirections(css: string): string[] {
239
+ const found = new Set<string>();
240
+ for (const line of css.split('\n')) {
241
+ if (EXCEPTION_MARK.test(line)) continue;
242
+ for (const match of line.matchAll(PHYSICAL_DIRECTION)) {
243
+ found.add(match[1].replace(/\s*:$/, '').trim());
244
+ }
245
+ }
246
+ return [...found];
247
+ }
@@ -2,6 +2,8 @@
2
2
  * 小程序容器适配器 — 映射 WXML 风格节点树(示范实现)
3
3
  */
4
4
  import type { IContainerAdapter, SomaAnchorStats } from './IContainerAdapter';
5
+ import { sanitizeAttributeWrite } from '../security/url-guard';
6
+ import { shouldRemoveAttribute } from '../security/attribute-presence';
5
7
 
6
8
  export interface MpElementNode {
7
9
  somaId: string | null;
@@ -113,10 +115,22 @@ export class MiniProgramContainerAdapter implements IContainerAdapter {
113
115
  }
114
116
 
115
117
  setAttribute(element: HTMLElement, attrName: string, value: string): void {
116
- element.setAttribute(attrName, value);
118
+ // 安全闸:伪协议 URL 与事件处理器属性不得经声明写入 DOM
119
+ const safe = sanitizeAttributeWrite(attrName, value);
120
+ if (safe === null) {
121
+ return;
122
+ }
123
+ // 空串在布尔/ARIA 状态属性上意味着「本行不该有这个属性」——删除而非写空串
124
+ if (shouldRemoveAttribute(attrName, safe)) {
125
+ element.removeAttribute(attrName);
126
+ return;
127
+ }
128
+ element.setAttribute(attrName, safe);
117
129
  }
118
130
 
119
131
  setHtml(element: HTMLElement, value: string): void {
132
+ // 小程序容器没有「把字符串当 DOM 解释」的语义,故不接 html-sanitizer:
133
+ // 这里恒等于降级路径的终点(纯文本),不给宿主留下开富文本口子的余地
120
134
  element.textContent = value;
121
135
  }
122
136
 
@@ -3,6 +3,8 @@
3
3
  * 生产环境需对接 react-native 组件;此处用内存节点 + jsdom HTMLElement 桥接测试。
4
4
  */
5
5
  import type { IContainerAdapter, SomaAnchorStats } from './IContainerAdapter';
6
+ import { sanitizeAttributeWrite } from '../security/url-guard';
7
+ import { shouldRemoveAttribute } from '../security/attribute-presence';
6
8
 
7
9
  export interface RNElementNode {
8
10
  somaId: string | null;
@@ -116,10 +118,22 @@ export class ReactNativeContainerAdapter implements IContainerAdapter {
116
118
  }
117
119
 
118
120
  setAttribute(element: HTMLElement, attrName: string, value: string): void {
119
- element.setAttribute(attrName, value);
121
+ // 安全闸:伪协议 URL 与事件处理器属性不得经声明写入 DOM
122
+ const safe = sanitizeAttributeWrite(attrName, value);
123
+ if (safe === null) {
124
+ return;
125
+ }
126
+ // 空串在布尔/ARIA 状态属性上意味着「本行不该有这个属性」——删除而非写空串
127
+ if (shouldRemoveAttribute(attrName, safe)) {
128
+ element.removeAttribute(attrName);
129
+ return;
130
+ }
131
+ element.setAttribute(attrName, safe);
120
132
  }
121
133
 
122
134
  setHtml(element: HTMLElement, value: string): void {
135
+ // RN 容器无 HTML 解析语义,恒走纯文本(等价于 html-sanitizer 的降级终点),
136
+ // 不接 sanitizer 是刻意的:跨端一致性优先于富文本能力
123
137
  element.textContent = value;
124
138
  }
125
139