soma-core 1.5.1 → 2.0.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/app/SomaPageClient.tsx +28 -9
- package/app/soma/manifest/route.ts +3 -0
- package/bin/migrate.cjs +71 -1
- package/bin/soma-lsp.cjs +8 -1
- package/core/build/blocks/block-compile.ts +23 -5
- package/core/build/compiler/full-manifest-builder.ts +15 -0
- package/core/build/compiler/page-assets.ts +17 -1
- package/core/build/compiler/page-declaration-compiler.ts +67 -11
- package/core/build/compiler/precompiled-manifest-loader.ts +7 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +7 -0
- package/core/build/compiler/soma-precompile.js +212 -21
- package/core/build/component/package-loader.ts +35 -4
- package/core/build/component-tree/artifact-codec.ts +192 -0
- package/core/build/component-tree/binding-bridge.ts +272 -0
- package/core/build/component-tree/collection-binding-lowerer.ts +65 -0
- package/core/build/component-tree/definition-preparation.ts +320 -0
- package/core/build/component-tree/migration-files.ts +214 -0
- package/core/build/component-tree/migration.ts +170 -0
- package/core/build/component-tree/parser.ts +362 -0
- package/core/build/component-tree/registry-snapshot.ts +317 -0
- package/core/build/component-tree/source-preflight.ts +132 -0
- package/core/build/component-tree/structure-topology.ts +63 -0
- package/core/build/component-tree/validator.ts +318 -0
- package/core/build/eject/component-eject.ts +1 -0
- package/core/build/lsp/component-tree-service.ts +79 -0
- package/core/build/lsp/component-tree.cjs +42 -0
- package/core/build/lsp/cross-file-diagnostics.cjs +15 -3
- package/core/build/lsp/extra-completion.cjs +7 -0
- package/core/client/adaptive/observer-pool.ts +122 -8
- package/core/client/compiler/manifest-compiler.ts +48 -5
- package/core/client/component/ai-catalog.ts +41 -3
- package/core/client/component/catalog-examples.ts +60 -0
- package/core/client/component/component-defaults.ts +1 -1
- package/core/client/component/component-definition.types.ts +9 -0
- package/core/client/component/component-expander.ts +22 -6
- package/core/client/component/component-manager.ts +881 -45
- package/core/client/component/component-pack.ts +1 -1
- package/core/client/component/component-registry.ts +11 -2
- package/core/client/component/scoped-adapter.ts +29 -2
- package/core/client/component/structure-adapter.ts +207 -0
- package/core/client/component/verify-pack.ts +21 -1
- package/core/client/container/SomaContainerAdapter.ts +103 -2
- package/core/client/data/data-object-resolver.ts +45 -1
- package/core/client/engine/asset-resolver.ts +1 -1
- package/core/client/engine/dom-renderer.ts +712 -26
- package/core/client/engine/event-engine.ts +328 -21
- package/core/client/engine/property-watcher.ts +164 -18
- package/core/client/engine/reactive-proxy.ts +194 -23
- package/core/client/engine/render-engine.ts +120 -11
- package/core/client/engine/row-range.ts +62 -0
- package/core/client/engine/row-scope.ts +77 -0
- package/core/client/engine/runtime-builder.ts +2 -0
- package/core/client/engine/scope-resolver.ts +3 -3
- package/core/client/engine/soma-bootstrap.ts +176 -17
- package/core/client/parser/adaptive-parser.ts +1 -1
- package/core/client/parser/annotation-extractor.ts +1 -1
- package/core/client/parser/blocks/page-parser.ts +53 -3
- package/core/client/parser/blocks/use-parser.ts +21 -6
- package/core/client/parser/data-config-parser.ts +1 -1
- package/core/client/parser/pipeline-parser.ts +252 -15
- package/core/client/parser/style-config-parser.ts +1 -1
- package/core/client/parser/timer-parser.ts +1 -1
- package/core/client/parser/trigger-parser.ts +1 -1
- package/core/client/parser/ui-config-parser.ts +39 -7
- package/core/client/pipes/pipe-registry.ts +45 -4
- package/core/client/registry/declaration-renderer.ts +32 -24
- package/core/client/registry/remote-page.ts +1 -1
- package/core/client/runtime/SomaSnapshotStore.ts +1 -1
- package/core/client/scheduler/SomaScheduler.ts +1 -1
- package/core/client/security/attribute-presence.ts +3 -0
- package/core/client/theme/harmony.ts +135 -13
- package/core/client/theme/scene-tokens.ts +17 -2
- package/core/client/theme/theme-axes.ts +43 -6
- package/core/client/theme/theme-engine.ts +921 -126
- package/core/client/validation/contract-validator.ts +2 -2
- package/core/index.ts +3 -0
- package/core/server/host/page-markup-config.ts +42 -0
- package/core/server/host/published-page-artifact-cache.ts +36 -4
- package/core/server/host/published-page-assets.ts +13 -7
- package/core/server/host/resolve-host-tree-context.ts +43 -0
- package/core/server/host/server-page-assets.ts +23 -4
- package/core/server/host/soma-page-compile.ts +10 -0
- package/core/server/mock/node/cli.mjs +3 -3
- package/core/server/page-declaration.ts +7 -0
- package/core/server/registry/declaration-precompiler.ts +35 -3
- package/core/server/registry/page-registry-repository.ts +31 -3
- package/core/server/registry/page-registry-store.ts +64 -7
- package/core/server/registry/publish-asset-pin.ts +6 -1
- package/core/server/registry/services/pages/Get.ts +11 -6
- package/core/server/registry/validation/compile-gate.ts +10 -1
- package/core/shared/component-tree-artifact.ts +237 -0
- package/core/shared/component-tree-errors.ts +52 -0
- package/core/shared/path-algebra.ts +1 -1
- package/core/shared/types/component-contract.types.ts +51 -0
- package/core/shared/types/component-tree-artifact.types.ts +121 -0
- package/core/shared/types/component-tree.types.ts +170 -0
- package/core/shared/types/engine.types.ts +5 -0
- package/core/shared/types/event.types.ts +8 -4
- package/core/shared/types/manifest.types.ts +4 -0
- package/core/shared/types/ui-config.types.ts +8 -0
- package/dist/cli/soma.js +1 -1
- package/dist/core/index.cjs +14508 -11358
- package/dist/lsp/component-tree.cjs +20997 -0
- package/dist/types/build/blocks/block-compile.d.ts +10 -1
- package/dist/types/build/compiler/full-manifest-builder.d.ts +9 -0
- package/dist/types/build/compiler/page-assets.d.ts +6 -1
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +11 -1
- package/dist/types/build/component/package-loader.d.ts +4 -1
- package/dist/types/build/component-tree/artifact-codec.d.ts +50 -0
- package/dist/types/build/component-tree/binding-bridge.d.ts +19 -0
- package/dist/types/build/component-tree/collection-binding-lowerer.d.ts +18 -0
- package/dist/types/build/component-tree/definition-preparation.d.ts +11 -0
- package/dist/types/build/component-tree/migration-files.d.ts +35 -0
- package/dist/types/build/component-tree/migration.d.ts +28 -0
- package/dist/types/build/component-tree/parser.d.ts +13 -0
- package/dist/types/build/component-tree/registry-snapshot.d.ts +15 -0
- package/dist/types/build/component-tree/source-preflight.d.ts +22 -0
- package/dist/types/build/component-tree/structure-topology.d.ts +7 -0
- package/dist/types/build/component-tree/validator.d.ts +9 -0
- package/dist/types/build/lsp/component-tree-service.d.ts +34 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +9 -2
- package/dist/types/client/compiler/manifest-compiler.d.ts +11 -2
- package/dist/types/client/component/ai-catalog.d.ts +18 -1
- package/dist/types/client/component/catalog-examples.d.ts +18 -0
- package/dist/types/client/component/component-definition.types.d.ts +9 -0
- package/dist/types/client/component/component-expander.d.ts +2 -1
- package/dist/types/client/component/component-manager.d.ts +97 -0
- package/dist/types/client/component/scoped-adapter.d.ts +1 -0
- package/dist/types/client/component/structure-adapter.d.ts +17 -0
- package/dist/types/client/container/SomaContainerAdapter.d.ts +8 -0
- package/dist/types/client/data/data-object-resolver.d.ts +19 -0
- package/dist/types/client/engine/dom-renderer.d.ts +125 -2
- package/dist/types/client/engine/event-engine.d.ts +62 -3
- package/dist/types/client/engine/property-watcher.d.ts +41 -1
- package/dist/types/client/engine/reactive-proxy.d.ts +42 -2
- package/dist/types/client/engine/render-engine.d.ts +55 -3
- package/dist/types/client/engine/row-range.d.ts +13 -0
- package/dist/types/client/engine/row-scope.d.ts +20 -0
- package/dist/types/client/engine/runtime-builder.d.ts +1 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +4 -1
- package/dist/types/client/parser/blocks/use-parser.d.ts +18 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +12 -4
- package/dist/types/client/parser/ui-config-parser.d.ts +2 -0
- package/dist/types/client/pipes/pipe-registry.d.ts +39 -1
- package/dist/types/client/registry/declaration-renderer.d.ts +5 -1
- package/dist/types/client/registry/remote-page.d.ts +1 -1
- package/dist/types/client/theme/harmony.d.ts +44 -2
- package/dist/types/client/theme/scene-tokens.d.ts +11 -1
- package/dist/types/client/theme/theme-axes.d.ts +36 -6
- package/dist/types/index.d.ts +3 -1
- package/dist/types/server/host/page-markup-config.d.ts +6 -0
- package/dist/types/server/host/published-page-artifact-cache.d.ts +9 -1
- package/dist/types/server/host/published-page-assets.d.ts +5 -3
- package/dist/types/server/host/resolve-host-tree-context.d.ts +8 -0
- package/dist/types/server/host/server-page-assets.d.ts +6 -0
- package/dist/types/server/host/soma-page-compile.d.ts +10 -1
- package/dist/types/server/page-declaration.d.ts +6 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +8 -3
- package/dist/types/server/registry/page-registry-repository.d.ts +5 -0
- package/dist/types/server/registry/page-registry-store.d.ts +11 -1
- package/dist/types/server/registry/publish-asset-pin.d.ts +4 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +4 -1
- package/dist/types/shared/component-tree-artifact.d.ts +5 -0
- package/dist/types/shared/component-tree-errors.d.ts +22 -0
- package/dist/types/shared/types/component-contract.types.d.ts +42 -0
- package/dist/types/shared/types/component-tree-artifact.types.d.ts +104 -0
- package/dist/types/shared/types/component-tree.types.d.ts +149 -0
- package/dist/types/shared/types/engine.types.d.ts +5 -0
- package/dist/types/shared/types/event.types.d.ts +8 -4
- package/dist/types/shared/types/manifest.types.d.ts +4 -0
- package/dist/types/shared/types/ui-config.types.d.ts +8 -0
- package/package.json +5 -4
- package/dist/preview-export.cjs +0 -3401
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { parseFragment, type DefaultTreeAdapterMap } from 'parse5';
|
|
2
|
+
import { compilePageDeclaration, type SomaPageDeclarationInput } from '../compiler/page-declaration-compiler';
|
|
3
|
+
import { expandSomaHtmlIncludes } from '../compiler/html-include-expander';
|
|
4
|
+
import { digestTreeData, normalizeRuntimeConfig } from './artifact-codec';
|
|
5
|
+
import { DEFAULT_TREE_LIMITS } from '../../shared/types/component-tree.types';
|
|
6
|
+
|
|
7
|
+
export type ComponentTreeMigrationStatus = 'converted' | 'needs-business-component' | 'unsupported' | 'unchanged';
|
|
8
|
+
export interface ComponentTreeMigrationItem {
|
|
9
|
+
status: 'needs-business-component' | 'unsupported';
|
|
10
|
+
sourcePath: string;
|
|
11
|
+
reason: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ComponentTreeMigrationPlan {
|
|
14
|
+
schemaVersion: 1;
|
|
15
|
+
status: ComponentTreeMigrationStatus;
|
|
16
|
+
sourceDigest: string;
|
|
17
|
+
planDigest: string;
|
|
18
|
+
mappings: Array<{ sourcePath: string; targetPath: string; identity?: string }>;
|
|
19
|
+
manualItems: ComponentTreeMigrationItem[];
|
|
20
|
+
requiredComparisons: string[];
|
|
21
|
+
candidate?: { markup: 'component-tree'; grammarVersion: 1; html: string; uiYaml: string };
|
|
22
|
+
}
|
|
23
|
+
type Node = DefaultTreeAdapterMap['node'];
|
|
24
|
+
type Element = DefaultTreeAdapterMap['element'];
|
|
25
|
+
|
|
26
|
+
function isElement(node: Node): node is Element { return 'tagName' in node; }
|
|
27
|
+
function attributes(element: Element): Record<string, string> {
|
|
28
|
+
return Object.fromEntries(element.attrs.map((attribute) => [attribute.name, attribute.value]));
|
|
29
|
+
}
|
|
30
|
+
function escapeAttribute(value: string): string {
|
|
31
|
+
return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>');
|
|
32
|
+
}
|
|
33
|
+
function meaningful(nodes: Node[]): Node[] {
|
|
34
|
+
return nodes.filter((node) => node.nodeName !== '#comment'
|
|
35
|
+
&& !(node.nodeName === '#text' && 'value' in node && !node.value.trim()));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Pure migration analysis. It never guesses a component for a native business/layout node. */
|
|
39
|
+
export function planComponentTreeMigration(input: SomaPageDeclarationInput): ComponentTreeMigrationPlan {
|
|
40
|
+
const sourceDigest = digestTreeData({ html: input.html, uiYaml: input.uiYaml,
|
|
41
|
+
markup: input.markup ?? 'legacy-html', grammarVersion: input.grammarVersion,
|
|
42
|
+
includes: input.includes ?? {}, externalBlocks: [...(input.externalBlocks ?? [])].sort(([a], [b]) => a.localeCompare(b)),
|
|
43
|
+
});
|
|
44
|
+
const manualItems: ComponentTreeMigrationItem[] = [];
|
|
45
|
+
const mappings: ComponentTreeMigrationPlan['mappings'] = [];
|
|
46
|
+
const requiredComparisons = ['布局尺寸与直接父子关系', '主题/字体/人格视觉基线', '焦点顺序与键盘行为', 'landmark 与屏幕阅读器语义', 'props/事件/生命周期等价'];
|
|
47
|
+
const finish = (status: ComponentTreeMigrationStatus, candidate?: ComponentTreeMigrationPlan['candidate']): ComponentTreeMigrationPlan => {
|
|
48
|
+
const payload = { schemaVersion: 1 as const, status, sourceDigest, mappings, manualItems, requiredComparisons,
|
|
49
|
+
...(candidate ? { candidate } : {}),
|
|
50
|
+
};
|
|
51
|
+
return { ...payload, planDigest: digestTreeData({ ...payload,
|
|
52
|
+
context: input.treeContext ? {
|
|
53
|
+
snapshot: input.treeContext.registrySnapshot.digest,
|
|
54
|
+
policy: input.treeContext.compositionPolicy, sources: input.treeContext.collectionSources,
|
|
55
|
+
} : undefined,
|
|
56
|
+
}) };
|
|
57
|
+
};
|
|
58
|
+
const block = (status: ComponentTreeMigrationItem['status'], sourcePath: string, reason: string): string => {
|
|
59
|
+
manualItems.push({ status, sourcePath, reason });
|
|
60
|
+
return '';
|
|
61
|
+
};
|
|
62
|
+
if (new TextEncoder().encode(input.html + input.uiYaml).byteLength > DEFAULT_TREE_LIMITS.maxSourceBytes) {
|
|
63
|
+
block('unsupported', 'page', '源声明超过迁移资源预算;请先缩小单页声明');
|
|
64
|
+
return finish('unsupported');
|
|
65
|
+
}
|
|
66
|
+
if (input.markup === 'component-tree') {
|
|
67
|
+
try { compilePageDeclaration(input); return finish('unchanged'); }
|
|
68
|
+
catch { block('unsupported', 'page', '已标记树的页面未通过当前编译;保持原文,先修复源或装配上下文'); return finish('unsupported'); }
|
|
69
|
+
}
|
|
70
|
+
let compiledLegacy: ReturnType<typeof compilePageDeclaration>;
|
|
71
|
+
try { compiledLegacy = compilePageDeclaration(input); }
|
|
72
|
+
catch { block('unsupported', 'page', '旧页面无法按当前 canonical/HTML 规则编译;不自动猜格式或丢弃侧车'); return finish('unsupported'); }
|
|
73
|
+
const html = expandSomaHtmlIncludes(input.html, input.includes);
|
|
74
|
+
const fragment = parseFragment(html);
|
|
75
|
+
const bounded: Array<{ node: Node; depth: number }> = [{ node: fragment, depth: 0 }];
|
|
76
|
+
let nodeCount = 0;
|
|
77
|
+
while (bounded.length) {
|
|
78
|
+
const { node, depth } = bounded.pop()!;
|
|
79
|
+
if (++nodeCount > DEFAULT_TREE_LIMITS.maxNodes || depth > DEFAULT_TREE_LIMITS.maxDepth) {
|
|
80
|
+
block('unsupported', 'page', '源 DOM 深度或节点数超过迁移资源预算');
|
|
81
|
+
return finish('unsupported');
|
|
82
|
+
}
|
|
83
|
+
const children = isElement(node) && node.tagName === 'template' && 'content' in node
|
|
84
|
+
? node.content.childNodes : 'childNodes' in node ? node.childNodes : [];
|
|
85
|
+
for (const child of children) bounded.push({ node: child, depth: depth + 1 });
|
|
86
|
+
}
|
|
87
|
+
const rootNodes = meaningful(fragment.childNodes);
|
|
88
|
+
if (rootNodes.length !== 1 || !isElement(rootNodes[0])) {
|
|
89
|
+
block('unsupported', 'page', '必须具有唯一、独立的 Host 根;不能丢弃相邻助手、浮层或业务节点');
|
|
90
|
+
// Still inventory concrete native/behavior nodes for an actionable report.
|
|
91
|
+
}
|
|
92
|
+
const inspectNative = (node: Node, sourcePath: string, underNative = false): void => {
|
|
93
|
+
if (!isElement(node)) return;
|
|
94
|
+
const attrs = attributes(node);
|
|
95
|
+
const native = !attrs['data-soma-host'] && !attrs['data-soma-component'] && !(node.tagName === 'template' && attrs['data-soma-slot']);
|
|
96
|
+
const identity = attrs['data-soma-id'] ?? attrs.id;
|
|
97
|
+
// Report native boundaries and behavior anchors, not every decorative SVG/path/span.
|
|
98
|
+
if (native && (!underNative || identity || attrs['data-soma-preview-mount'])) {
|
|
99
|
+
block('needs-business-component', sourcePath,
|
|
100
|
+
`原生 ${node.tagName}${identity ? `(${identity})` : ''} 保留布局/显隐/挂载或业务语义;需先收敛为真实业务组件`);
|
|
101
|
+
}
|
|
102
|
+
const children = node.tagName === 'template' && 'content' in node ? node.content.childNodes : node.childNodes;
|
|
103
|
+
meaningful(children).forEach((child, index) => inspectNative(child, `${sourcePath}/${node.tagName}[${index}]`, native || underNative));
|
|
104
|
+
};
|
|
105
|
+
rootNodes.forEach((node, index) => inspectNative(node, `page[${index}]`));
|
|
106
|
+
if (rootNodes.length !== 1 || !isElement(rootNodes[0])) return finish(manualItems.some(item => item.status === 'needs-business-component') ? 'needs-business-component' : 'unsupported');
|
|
107
|
+
|
|
108
|
+
const root = rootNodes[0];
|
|
109
|
+
const host = attributes(root);
|
|
110
|
+
if (!host['data-soma-host']) block('unsupported', 'page', '根节点缺少明确的 data-soma-host 身份');
|
|
111
|
+
if (host['data-soma-component']) block('needs-business-component', 'page', 'Host 与组件共址;分离会新增真实 DOM 根,必须先完成布局/焦点/landmark 的人工迁移验证');
|
|
112
|
+
if (!['div', 'main', 'section', 'article'].includes(root.tagName)) block('unsupported', 'page', 'Host 原生语义不在首版安全 Host.as 词表中');
|
|
113
|
+
if (Object.keys(host).some(key => !['data-soma-host', 'data-soma-id', 'data-soma-component'].includes(key))) {
|
|
114
|
+
block('needs-business-component', 'page', 'Host 存在 class/style/其他属性的真实行为,不能在树转换中静默删除');
|
|
115
|
+
}
|
|
116
|
+
if (manualItems.length) return finish(manualItems.some(item => item.status === 'needs-business-component') ? 'needs-business-component' : 'unsupported');
|
|
117
|
+
|
|
118
|
+
const convertComponent = (node: Node, sourcePath: string, targetPath: string): string => {
|
|
119
|
+
if (!isElement(node)) return block('needs-business-component', sourcePath, '裸文本或其他业务内容必须由文本/业务组件持有');
|
|
120
|
+
const attrs = attributes(node);
|
|
121
|
+
const name = attrs['data-soma-component'];
|
|
122
|
+
const id = attrs['data-soma-id'];
|
|
123
|
+
if (!name || !id) return block('needs-business-component', sourcePath, '节点不是带稳定 id 的已知组件;不机械替换为 Box/Fragment');
|
|
124
|
+
if (!input.treeContext?.registrySnapshot.components[name]) return block('unsupported', sourcePath, '组件未存在于实际装配快照,不能按目录名猜测供货');
|
|
125
|
+
if (Object.keys(attrs).some(key => !['data-soma-component', 'data-soma-id'].includes(key))) {
|
|
126
|
+
return block('needs-business-component', sourcePath, '组件宿主上存在未归入契约的属性/样式/行为;需要先收敛业务职责');
|
|
127
|
+
}
|
|
128
|
+
if (node.tagName !== 'div') return block('unsupported', sourcePath, '非 div 组件宿主的原生语义不能在没有显式结构映射时自动消除');
|
|
129
|
+
mappings.push({ sourcePath, targetPath, identity: id });
|
|
130
|
+
const slots = meaningful(node.childNodes).map((slot, index) => {
|
|
131
|
+
const slotPath = `${sourcePath}/slot[${index}]`;
|
|
132
|
+
if (!isElement(slot) || slot.tagName !== 'template' || !('content' in slot)) {
|
|
133
|
+
return block('needs-business-component', slotPath, '组件内容必须是明确具名的 template slot,不能猜默认出口');
|
|
134
|
+
}
|
|
135
|
+
const slotAttrs = attributes(slot);
|
|
136
|
+
if (Object.keys(slotAttrs).length !== 1 || !slotAttrs['data-soma-slot']) {
|
|
137
|
+
return block('unsupported', slotPath, '行模板/带行为的 slot 需要显式稳定 key、唯一 collection owner 与可证明的作用域,不能猜测');
|
|
138
|
+
}
|
|
139
|
+
const name = slotAttrs['data-soma-slot'];
|
|
140
|
+
const children = meaningful(slot.content.childNodes).map((child, childIndex) =>
|
|
141
|
+
convertComponent(child, `${slotPath}/child[${childIndex}]`, `${targetPath}.slots.${name}[${childIndex}]`)).join('');
|
|
142
|
+
return `<soma-slot name="${escapeAttribute(name)}">${children}</soma-slot>`;
|
|
143
|
+
}).join('');
|
|
144
|
+
return `<soma-component component="${escapeAttribute(name)}" id="${escapeAttribute(id)}">${slots}</soma-component>`;
|
|
145
|
+
};
|
|
146
|
+
const children = meaningful(root.childNodes).map((node, index) => convertComponent(node, `page/child[${index}]`, `host.children[${index}]`)).join('');
|
|
147
|
+
if (manualItems.length) return finish(manualItems.some(item => item.status === 'needs-business-component') ? 'needs-business-component' : 'unsupported');
|
|
148
|
+
if (!input.treeContext) { block('unsupported', 'page', '迁移候选需要受信实际快照与页面准入策略'); return finish('unsupported'); }
|
|
149
|
+
const candidate = {
|
|
150
|
+
markup: 'component-tree' as const, grammarVersion: 1 as const,
|
|
151
|
+
html: `<soma-host name="${escapeAttribute(host['data-soma-host'])}"${host['data-soma-id'] ? ` id="${escapeAttribute(host['data-soma-id'])}"` : ''}${root.tagName === 'div' ? '' : ` as="${root.tagName}"`}>${children}</soma-host>`,
|
|
152
|
+
uiYaml: input.uiYaml,
|
|
153
|
+
};
|
|
154
|
+
try {
|
|
155
|
+
const compiledTree = compilePageDeclaration({ ...candidate, treeContext: input.treeContext, externalBlocks: input.externalBlocks });
|
|
156
|
+
const configuration = (page: ReturnType<typeof compilePageDeclaration>) => ({
|
|
157
|
+
uiConfig: page.uiConfig, eventConfig: page.eventConfig, triggers: page.triggers, timers: page.timers,
|
|
158
|
+
dataConfig: page.dataConfig, layoutCss: page.layoutCss, oouiArtifact: page.oouiArtifact,
|
|
159
|
+
});
|
|
160
|
+
if (digestTreeData(normalizeRuntimeConfig(configuration(compiledLegacy))) !== digestTreeData(normalizeRuntimeConfig(configuration(compiledTree)))) {
|
|
161
|
+
block('unsupported', 'canonical', '新旧逻辑配置不等价;保持整份源,不能只替换 HTML');
|
|
162
|
+
return finish('unsupported');
|
|
163
|
+
}
|
|
164
|
+
} catch {
|
|
165
|
+
block('unsupported', 'candidate', '树候选未通过当前语法/契约/策略/作用域或布局编译;不能发布半成品建议');
|
|
166
|
+
return finish('unsupported');
|
|
167
|
+
}
|
|
168
|
+
mappings.unshift({ sourcePath: 'page', targetPath: 'host', identity: host['data-soma-host'] });
|
|
169
|
+
return finish('converted', candidate);
|
|
170
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { SomaComponentTreeError } from '../../shared/component-tree-errors';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_TREE_LIMITS,
|
|
5
|
+
type ComponentNode,
|
|
6
|
+
type FragmentNode,
|
|
7
|
+
type HostNode,
|
|
8
|
+
type NodeMeta,
|
|
9
|
+
type RepeatNode,
|
|
10
|
+
type SlotNode,
|
|
11
|
+
type SomaComponentTree,
|
|
12
|
+
type SourceSpan,
|
|
13
|
+
type TreeLimits,
|
|
14
|
+
type TreeNode,
|
|
15
|
+
} from '../../shared/types/component-tree.types';
|
|
16
|
+
|
|
17
|
+
export interface ParseComponentTreeOptions {
|
|
18
|
+
sourceId?: string;
|
|
19
|
+
limits?: Partial<TreeLimits>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type Tag = 'soma-host' | 'soma-component' | 'soma-slot' | 'soma-fragment' | 'soma-repeat';
|
|
23
|
+
type ErrorCode = ConstructorParameters<typeof SomaComponentTreeError>[0]['code'];
|
|
24
|
+
type ChildContext = 'host' | 'component' | 'slot' | 'repeat';
|
|
25
|
+
|
|
26
|
+
interface Attribute {
|
|
27
|
+
value: string;
|
|
28
|
+
start: number;
|
|
29
|
+
end: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface Frame {
|
|
33
|
+
tag: Tag;
|
|
34
|
+
attributes: Map<string, Attribute>;
|
|
35
|
+
children: TreeNode[];
|
|
36
|
+
start: number;
|
|
37
|
+
meta: NodeMeta;
|
|
38
|
+
context: ChildContext;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const COMPONENT_TREE_ATTRIBUTES: Readonly<Record<Tag, readonly string[]>> = {
|
|
42
|
+
'soma-host': ['name', 'id', 'as'],
|
|
43
|
+
'soma-component': ['component', 'id', 'as', 'mount'],
|
|
44
|
+
'soma-slot': ['name'],
|
|
45
|
+
'soma-fragment': [],
|
|
46
|
+
'soma-repeat': ['source', 'item', 'key'],
|
|
47
|
+
};
|
|
48
|
+
const ID = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
49
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
50
|
+
const COMPONENT_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
51
|
+
const SLOT_NAME = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
52
|
+
const SEMANTIC_TOKEN = /^[a-z][a-z0-9_-]*$/;
|
|
53
|
+
const PATH = /^[A-Za-z_$][A-Za-z0-9_$]*(?:\.[A-Za-z_$][A-Za-z0-9_$]*)*$/;
|
|
54
|
+
const HOST_SEMANTICS = new Set(['root', 'main', 'section', 'article', 'div']);
|
|
55
|
+
const NAMED_ENTITIES: Readonly<Record<string, string>> = Object.freeze({
|
|
56
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Strict build-time syntax parser. It deliberately does not use the browser's
|
|
61
|
+
* error-recovering HTML parser, resolve Registry entries, or inspect live data.
|
|
62
|
+
*/
|
|
63
|
+
export function parseComponentTree(
|
|
64
|
+
source: string,
|
|
65
|
+
options: ParseComponentTreeOptions = {},
|
|
66
|
+
): SomaComponentTree {
|
|
67
|
+
return new ComponentTreeParser(source, options).parse();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class ComponentTreeParser {
|
|
71
|
+
private readonly sourceId: string;
|
|
72
|
+
private readonly limits: TreeLimits;
|
|
73
|
+
private readonly lineStarts: number[] = [0];
|
|
74
|
+
private readonly stack: Frame[] = [];
|
|
75
|
+
private offset = 0;
|
|
76
|
+
private nodeCount = 0;
|
|
77
|
+
private host: HostNode | undefined;
|
|
78
|
+
|
|
79
|
+
constructor(private readonly source: string, options: ParseComponentTreeOptions) {
|
|
80
|
+
this.sourceId = options.sourceId ?? '<component-tree>';
|
|
81
|
+
this.limits = { ...DEFAULT_TREE_LIMITS, ...options.limits };
|
|
82
|
+
for (const [name, value] of Object.entries(this.limits)) {
|
|
83
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
84
|
+
this.fail('CT-LIMIT', 0, 0, '限制必须是正的安全整数', name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Check before building line indexes, tokens or a partial tree.
|
|
88
|
+
if (source.length > this.limits.maxSourceBytes
|
|
89
|
+
|| Buffer.byteLength(source, 'utf8') > this.limits.maxSourceBytes) {
|
|
90
|
+
this.fail('CT-LIMIT', 0, 0, `源文件不得超过 ${this.limits.maxSourceBytes} UTF-8 字节`);
|
|
91
|
+
}
|
|
92
|
+
for (let i = 0; i < source.length; i++) {
|
|
93
|
+
if (source[i] === '\r') {
|
|
94
|
+
if (source[i + 1] === '\n') i++;
|
|
95
|
+
this.lineStarts.push(i + 1);
|
|
96
|
+
} else if (source[i] === '\n') {
|
|
97
|
+
this.lineStarts.push(i + 1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
parse(): SomaComponentTree {
|
|
103
|
+
while (this.offset < this.source.length) {
|
|
104
|
+
if (this.source.startsWith('<!--', this.offset)) this.comment();
|
|
105
|
+
else if (this.source.startsWith('</', this.offset)) this.closeTag();
|
|
106
|
+
else if (this.source[this.offset] === '<') this.openTag();
|
|
107
|
+
else this.whitespace();
|
|
108
|
+
}
|
|
109
|
+
const open = this.stack.at(-1);
|
|
110
|
+
if (open) {
|
|
111
|
+
this.fail('CT-SYNTAX-CLOSE', open.start, this.offset, `必须显式闭合 ${open.tag}`, undefined, open.meta.nodePath);
|
|
112
|
+
}
|
|
113
|
+
if (!this.host) this.fail('CT-SYNTAX-ROOT', 0, this.offset, '需要唯一 soma-host 根');
|
|
114
|
+
return { grammarVersion: 1, host: this.host, sourceId: this.sourceId };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private openTag(): void {
|
|
118
|
+
const start = this.offset++;
|
|
119
|
+
const name = this.readName();
|
|
120
|
+
if (!Object.hasOwn(COMPONENT_TREE_ATTRIBUTES, name)) {
|
|
121
|
+
this.fail('CT-SYNTAX-ELEMENT', start, this.offset, '只允许五种 soma-* 树元素');
|
|
122
|
+
}
|
|
123
|
+
const tag = name as Tag;
|
|
124
|
+
const parent = this.stack.at(-1);
|
|
125
|
+
const nodePath = `${parent?.meta.nodePath ?? ''}/${tag}[${parent ? parent.children.length + 1 : 1}]`;
|
|
126
|
+
this.assertPlacement(tag, parent, start, nodePath);
|
|
127
|
+
if (++this.nodeCount > this.limits.maxNodes || this.stack.length + 1 > this.limits.maxDepth) {
|
|
128
|
+
this.fail('CT-LIMIT', start, this.offset, '树节点数量或深度超出限制', undefined, nodePath);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const attributes = new Map<string, Attribute>();
|
|
132
|
+
for (;;) {
|
|
133
|
+
const separated = this.skipSpaces();
|
|
134
|
+
if (this.source[this.offset] === '>') { this.offset++; break; }
|
|
135
|
+
if (this.source[this.offset] === '/') {
|
|
136
|
+
this.fail('CT-SYNTAX-CLOSE', start, this.offset + 1, 'soma-* 元素必须使用显式结束标签', undefined, nodePath);
|
|
137
|
+
}
|
|
138
|
+
if (this.offset === this.source.length) {
|
|
139
|
+
this.fail('CT-SYNTAX-CLOSE', start, this.offset, '开始标签未结束', undefined, nodePath);
|
|
140
|
+
}
|
|
141
|
+
if (!separated) this.fail('CT-SYNTAX-ATTRIBUTE', this.offset, this.offset + 1, '属性之间必须有空白', undefined, nodePath);
|
|
142
|
+
const attrStart = this.offset;
|
|
143
|
+
const attrName = this.readName(true);
|
|
144
|
+
if (!COMPONENT_TREE_ATTRIBUTES[tag].includes(attrName) || attributes.has(attrName)) {
|
|
145
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', attrStart, this.offset, '属性必须在闭合集合内且不得重复', attrName, nodePath);
|
|
146
|
+
}
|
|
147
|
+
if (attributes.size + 1 > this.limits.maxAttributes) {
|
|
148
|
+
this.fail('CT-LIMIT', attrStart, this.offset, '属性数量超出限制', attrName, nodePath);
|
|
149
|
+
}
|
|
150
|
+
this.skipSpaces();
|
|
151
|
+
if (this.source[this.offset++] !== '=') {
|
|
152
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', attrStart, this.offset, '属性需要等号和引号值', attrName, nodePath);
|
|
153
|
+
}
|
|
154
|
+
this.skipSpaces();
|
|
155
|
+
const quote = this.source[this.offset++];
|
|
156
|
+
if (quote !== '"' && quote !== "'") {
|
|
157
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', attrStart, this.offset, '属性值必须使用单引号或双引号', attrName, nodePath);
|
|
158
|
+
}
|
|
159
|
+
const valueStart = this.offset;
|
|
160
|
+
while (this.offset < this.source.length && this.source[this.offset] !== quote) {
|
|
161
|
+
if (this.source[this.offset] === '<') {
|
|
162
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', attrStart, this.offset + 1, '属性值不得包含未转义的左尖括号', attrName, nodePath);
|
|
163
|
+
}
|
|
164
|
+
this.offset++;
|
|
165
|
+
}
|
|
166
|
+
if (this.offset === this.source.length) {
|
|
167
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', attrStart, this.offset, '属性引号未闭合', attrName, nodePath);
|
|
168
|
+
}
|
|
169
|
+
const raw = this.source.slice(valueStart, this.offset++);
|
|
170
|
+
const value = this.decodeEntities(raw, valueStart, attrName, nodePath);
|
|
171
|
+
attributes.set(attrName, { value, start: attrStart, end: this.offset });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const context: ChildContext = tag === 'soma-fragment'
|
|
175
|
+
? parent!.context
|
|
176
|
+
: tag === 'soma-host' ? 'host'
|
|
177
|
+
: tag === 'soma-component' ? 'component'
|
|
178
|
+
: tag === 'soma-slot' ? 'slot' : 'repeat';
|
|
179
|
+
const frame: Frame = {
|
|
180
|
+
tag, attributes, children: [], start, context,
|
|
181
|
+
meta: { nodeId: `tree:${this.nodeCount}`, nodePath, span: this.span(start, this.offset) },
|
|
182
|
+
};
|
|
183
|
+
this.validateAttributes(frame);
|
|
184
|
+
this.stack.push(frame);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private closeTag(): void {
|
|
188
|
+
const start = this.offset;
|
|
189
|
+
this.offset += 2;
|
|
190
|
+
const name = this.readName();
|
|
191
|
+
this.skipSpaces();
|
|
192
|
+
if (this.source[this.offset++] !== '>') this.fail('CT-SYNTAX-CLOSE', start, this.offset, '结束标签只能包含元素名');
|
|
193
|
+
const frame = this.stack.at(-1);
|
|
194
|
+
if (!frame || frame.tag !== name) {
|
|
195
|
+
this.fail('CT-SYNTAX-CLOSE', start, this.offset, `结束标签必须匹配 ${frame?.tag ?? '已打开的元素'}`);
|
|
196
|
+
}
|
|
197
|
+
frame.meta.span = this.span(frame.start, this.offset);
|
|
198
|
+
const node = this.finish(frame);
|
|
199
|
+
this.stack.pop();
|
|
200
|
+
const parent = this.stack.at(-1);
|
|
201
|
+
if (parent) parent.children.push(node);
|
|
202
|
+
else this.host = node as HostNode;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private finish(frame: Frame): TreeNode {
|
|
206
|
+
const get = (name: string) => frame.attributes.get(name)?.value;
|
|
207
|
+
const meta = frame.meta;
|
|
208
|
+
switch (frame.tag) {
|
|
209
|
+
case 'soma-host':
|
|
210
|
+
return { kind: 'host', name: get('name')!, ...(get('id') === undefined ? {} : { id: get('id')! }),
|
|
211
|
+
as: (get('as') ?? 'root') as HostNode['as'], children: frame.children, meta };
|
|
212
|
+
case 'soma-component':
|
|
213
|
+
return { kind: 'component', component: get('component')!, id: get('id')!,
|
|
214
|
+
...(get('as') === undefined ? {} : { as: get('as')! }),
|
|
215
|
+
mount: (get('mount') ?? 'host') as ComponentNode['mount'], slots: frame.children as SlotNode[], meta };
|
|
216
|
+
case 'soma-slot':
|
|
217
|
+
return { kind: 'slot', name: get('name')!, children: frame.children, meta };
|
|
218
|
+
case 'soma-fragment':
|
|
219
|
+
return { kind: 'fragment', children: frame.children, meta };
|
|
220
|
+
case 'soma-repeat': {
|
|
221
|
+
const child = frame.children[0];
|
|
222
|
+
if (frame.children.length !== 1 || !child
|
|
223
|
+
|| (child.kind === 'fragment' && !hasComponentContent(child))) {
|
|
224
|
+
this.fail('CT-SLOT-CONTRACT', frame.start, this.offset, 'repeat 必须恰好包含一个组件或非空片段', undefined, meta.nodePath);
|
|
225
|
+
}
|
|
226
|
+
return { kind: 'repeat', source: get('source')!, item: get('item')!, key: get('key')!,
|
|
227
|
+
child: child as RepeatNode['child'], meta };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private assertPlacement(tag: Tag, parent: Frame | undefined, start: number, path: string): void {
|
|
233
|
+
if (!parent) {
|
|
234
|
+
if (tag !== 'soma-host' || this.host) this.fail('CT-SYNTAX-ROOT', start, this.offset, '需要唯一 soma-host 根', undefined, path);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (tag === 'soma-host') this.fail('CT-SYNTAX-ROOT', start, this.offset, 'Host 只能作为唯一根', undefined, path);
|
|
238
|
+
const valid = parent.context === 'component' ? tag === 'soma-slot'
|
|
239
|
+
: parent.context === 'slot' ? ['soma-component', 'soma-fragment', 'soma-repeat'].includes(tag)
|
|
240
|
+
: tag === 'soma-component' || tag === 'soma-fragment';
|
|
241
|
+
if (!valid || (tag === 'soma-slot' && parent.tag !== 'soma-component')) {
|
|
242
|
+
this.fail('CT-SLOT-CONTRACT', start, this.offset, '元素不符合父节点的结构上下文', undefined, path);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private validateAttributes(frame: Frame): void {
|
|
247
|
+
const check = (name: string, required: boolean, pattern: RegExp | Set<string>, code: ErrorCode) => {
|
|
248
|
+
const attr = frame.attributes.get(name);
|
|
249
|
+
if ((!attr && required) || (attr && !(pattern instanceof Set ? pattern.has(attr.value) : pattern.test(attr.value)))) {
|
|
250
|
+
this.fail(code, attr?.start ?? frame.start, attr?.end ?? this.offset, `属性 ${name} 缺失或取值不合法`, name, frame.meta.nodePath);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
switch (frame.tag) {
|
|
254
|
+
case 'soma-host':
|
|
255
|
+
check('name', true, IDENTIFIER, 'CT-IDENTITY-HOST');
|
|
256
|
+
check('id', false, ID, 'CT-IDENTITY-ID');
|
|
257
|
+
check('as', false, HOST_SEMANTICS, 'CT-STRUCTURE-MOUNT');
|
|
258
|
+
break;
|
|
259
|
+
case 'soma-component':
|
|
260
|
+
check('component', true, COMPONENT_NAME, 'CT-REGISTRY-COMPONENT');
|
|
261
|
+
check('id', true, ID, 'CT-IDENTITY-ID');
|
|
262
|
+
check('as', false, SEMANTIC_TOKEN, 'CT-STRUCTURE-MOUNT');
|
|
263
|
+
check('mount', false, new Set(['host', 'fragment']), 'CT-STRUCTURE-MOUNT');
|
|
264
|
+
break;
|
|
265
|
+
case 'soma-slot':
|
|
266
|
+
check('name', true, SLOT_NAME, 'CT-SLOT-CONTRACT');
|
|
267
|
+
break;
|
|
268
|
+
case 'soma-repeat': {
|
|
269
|
+
check('source', true, PATH, 'CT-REPEAT-SOURCE');
|
|
270
|
+
check('item', true, IDENTIFIER, 'CT-BINDING-SCOPE');
|
|
271
|
+
check('key', true, PATH, 'CT-REPEAT-KEY');
|
|
272
|
+
const key = frame.attributes.get('key')!;
|
|
273
|
+
if (!key.value.startsWith(`${frame.attributes.get('item')!.value}.`)) {
|
|
274
|
+
this.fail('CT-REPEAT-KEY', key.start, key.end, 'key 必须是当前 item 的字段路径', 'key', frame.meta.nodePath);
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private readName(attribute = false): string {
|
|
282
|
+
const start = this.offset;
|
|
283
|
+
if (!/[A-Za-z]/.test(this.source[this.offset] ?? '')) {
|
|
284
|
+
this.fail(attribute ? 'CT-SYNTAX-ATTRIBUTE' : 'CT-SYNTAX-ELEMENT', start, start + 1, '需要合法名称,不允许声明、脚本或命名空间');
|
|
285
|
+
}
|
|
286
|
+
while (/[A-Za-z0-9_-]/.test(this.source[this.offset] ?? '')) this.offset++;
|
|
287
|
+
return this.source.slice(start, this.offset);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private comment(): void {
|
|
291
|
+
const start = this.offset;
|
|
292
|
+
const end = this.source.indexOf('-->', start + 4);
|
|
293
|
+
if (end < 0) this.fail('CT-SYNTAX-CLOSE', start, this.source.length, '注释未闭合');
|
|
294
|
+
const content = this.source.slice(start + 4, end);
|
|
295
|
+
if (content.startsWith('>') || content.startsWith('->') || content.includes('<!--')
|
|
296
|
+
|| content.includes('--!>') || content.endsWith('<!-')) {
|
|
297
|
+
this.fail('CT-SYNTAX-ELEMENT', start, end + 3, '需要合法且不嵌套的 HTML 注释');
|
|
298
|
+
}
|
|
299
|
+
this.offset = end + 3;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private whitespace(): void {
|
|
303
|
+
if (!this.skipSpaces()) this.fail('CT-SYNTAX-ELEMENT', this.offset, this.offset + 1, '树节点之间只能是空白或注释,业务文字应使用组件 prop');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private skipSpaces(): boolean {
|
|
307
|
+
const start = this.offset;
|
|
308
|
+
while (/[ \t\r\n\f]/.test(this.source[this.offset] ?? '')) this.offset++;
|
|
309
|
+
return this.offset > start;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private decodeEntities(raw: string, start: number, attribute: string, path: string): string {
|
|
313
|
+
return raw.replace(/&[^&;]*;?|&/g, (entity: string, offset: number) => {
|
|
314
|
+
const body = entity.endsWith(';') ? entity.slice(1, -1) : '';
|
|
315
|
+
if (Object.hasOwn(NAMED_ENTITIES, body)) return NAMED_ENTITIES[body];
|
|
316
|
+
if (/^#(?:[0-9]+|[xX][0-9a-fA-F]+)$/.test(body)) {
|
|
317
|
+
const hex = body[1]?.toLowerCase() === 'x';
|
|
318
|
+
const point = Number.parseInt(body.slice(hex ? 2 : 1), hex ? 16 : 10);
|
|
319
|
+
if (point > 0 && point <= 0x10ffff && !(point >= 0xd800 && point <= 0xdfff)) return String.fromCodePoint(point);
|
|
320
|
+
}
|
|
321
|
+
this.fail('CT-SYNTAX-ATTRIBUTE', start + offset, start + offset + entity.length,
|
|
322
|
+
'实体必须为有效数字实体或 amp/lt/gt/quot/apos 且以分号结束', attribute, path);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private span(start: number, end: number): SourceSpan {
|
|
327
|
+
const locate = (offset: number) => {
|
|
328
|
+
let lo = 0;
|
|
329
|
+
let hi = this.lineStarts.length;
|
|
330
|
+
while (lo + 1 < hi) {
|
|
331
|
+
const mid = (lo + hi) >>> 1;
|
|
332
|
+
if (this.lineStarts[mid] <= offset) lo = mid;
|
|
333
|
+
else hi = mid;
|
|
334
|
+
}
|
|
335
|
+
return { line: lo + 1, column: offset - this.lineStarts[lo] + 1 };
|
|
336
|
+
};
|
|
337
|
+
const clampedEnd = Math.min(end, this.source.length);
|
|
338
|
+
const from = locate(start);
|
|
339
|
+
const to = locate(clampedEnd);
|
|
340
|
+
return { sourceId: this.sourceId, start, end: clampedEnd,
|
|
341
|
+
startLine: from.line, startColumn: from.column, endLine: to.line, endColumn: to.column };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private fail(code: ErrorCode, start: number, end: number, expected: string,
|
|
345
|
+
attribute?: string, nodePath = this.stack.at(-1)?.meta.nodePath ?? '/'): never {
|
|
346
|
+
throw new SomaComponentTreeError({ code, phase: 'parse', sourceId: this.sourceId, nodePath,
|
|
347
|
+
span: this.span(start, end), ...(attribute ? { attribute } : {}), expected,
|
|
348
|
+
fixHint: expected });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function hasComponentContent(fragment: FragmentNode): boolean {
|
|
353
|
+
const pending: TreeNode[] = [...fragment.children];
|
|
354
|
+
while (pending.length) {
|
|
355
|
+
const child = pending.pop()!;
|
|
356
|
+
if (child.kind === 'component') return true;
|
|
357
|
+
if (child.kind === 'fragment') {
|
|
358
|
+
for (const descendant of child.children) pending.push(descendant);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return false;
|
|
362
|
+
}
|