soma-core 1.3.2 → 1.3.3
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.md +42 -0
- package/app/SomaPageClient.tsx +26 -2
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/core/build/compiler/full-manifest-builder.ts +6 -1
- package/core/build/compiler/rollup-plugin-soma.ts +6 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
- package/core/build/compiler/soma-precompile.js +3507 -2387
- package/core/build/compiler/soma-precompile.ts +31 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
- package/core/build/compiler/soma-webpack-loader.js +7 -1
- package/core/build/compiler/vite-plugin-soma.ts +11 -1
- package/core/build/component/package-loader.ts +135 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +139 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +262 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +271 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +235 -16
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +3 -3
- package/core/client/engine/render-engine.ts +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +359 -4
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +159 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/theme-engine.ts +936 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +190 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +10 -0
- package/core/server/index.ts +30 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +129 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +903 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +86 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +71 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +77 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +110 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +5341 -894
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
- package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
- package/dist/types/build/component/package-loader.d.ts +9 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +90 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +49 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +116 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/theme-engine.d.ts +49 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +36 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +5 -0
- package/dist/types/server/index.d.ts +4 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +68 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/global.d.ts +4 -0
- package/next.config.js +76 -1
- package/package.json +32 -11
- package/proxy.ts +62 -0
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/middleware.ts +0 -33
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|