@reidelsaltres/pureper 0.1.164 → 0.1.168
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/out/foundation/Triplet.d.ts.map +1 -1
- package/out/foundation/Triplet.js +5 -6
- package/out/foundation/Triplet.js.map +1 -1
- package/out/foundation/component_api/Component.d.ts +2 -2
- package/out/foundation/component_api/Component.d.ts.map +1 -1
- package/out/foundation/component_api/Component.js.map +1 -1
- package/out/foundation/component_api/UniHtml.d.ts +10 -4
- package/out/foundation/component_api/UniHtml.d.ts.map +1 -1
- package/out/foundation/component_api/UniHtml.js +15 -7
- package/out/foundation/component_api/UniHtml.js.map +1 -1
- package/out/foundation/engine/TemplateEngine.d.ts +4 -93
- package/out/foundation/engine/TemplateEngine.d.ts.map +1 -1
- package/out/foundation/engine/TemplateEngine.js +44 -221
- package/out/foundation/engine/TemplateEngine.js.map +1 -1
- package/out/foundation/engine/TemplateEngine.old.d.ts +96 -0
- package/out/foundation/engine/TemplateEngine.old.d.ts.map +1 -0
- package/out/foundation/engine/TemplateEngine.old.js +235 -0
- package/out/foundation/engine/TemplateEngine.old.js.map +1 -0
- package/out/foundation/engine/TemplateInstance.d.ts +1 -1
- package/out/foundation/engine/TemplateInstance.d.ts.map +1 -1
- package/out/foundation/engine/TemplateInstance.js +3 -5
- package/out/foundation/engine/TemplateInstance.js.map +1 -1
- package/out/foundation/worker/Router.js +1 -1
- package/out/foundation/worker/Router.js.map +1 -1
- package/package.json +1 -1
- package/src/foundation/Triplet.ts +6 -9
- package/src/foundation/component_api/Component.ts +1 -2
- package/src/foundation/component_api/UniHtml.ts +22 -12
- package/src/foundation/engine/TemplateEngine.old.ts +318 -0
- package/src/foundation/engine/TemplateEngine.ts +49 -299
- package/src/foundation/engine/TemplateInstance.ts +3 -3
- package/src/foundation/worker/Router.ts +1 -1
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* Designed to replace legacy Page and Component base classes.
|
|
7
7
|
*/
|
|
8
8
|
import IElementHolder from "../api/ElementHolder.js";
|
|
9
|
-
import { TemplateInstance } from "../engine/TemplateEngine.js";
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -14,6 +13,7 @@ import { TemplateInstance } from "../engine/TemplateEngine.js";
|
|
|
14
13
|
* Use static factory methods for instantiation.
|
|
15
14
|
*/
|
|
16
15
|
export default class UniHtml {
|
|
16
|
+
|
|
17
17
|
/**
|
|
18
18
|
* Unified component lifecycle entrypoint.
|
|
19
19
|
* Loads HTML, then calls preLoadJS, render, and postLoadJS hooks in order.
|
|
@@ -21,27 +21,30 @@ export default class UniHtml {
|
|
|
21
21
|
*/
|
|
22
22
|
public async load(element: HTMLElement | ShadowRoot): Promise<void> {;
|
|
23
23
|
await this.preInit();
|
|
24
|
-
const preHtml:
|
|
25
|
-
const html:
|
|
24
|
+
const preHtml: DocumentFragment = await this._init();
|
|
25
|
+
const html: DocumentFragment = await this._postInit(preHtml);
|
|
26
26
|
|
|
27
|
+
const localRoot = html;
|
|
28
|
+
|
|
29
|
+
const holder : IElementHolder = { element: localRoot };
|
|
27
30
|
|
|
28
31
|
// ВАЖНО: preLoad() вызывается ДО монтирования в DOM/Shadow DOM.
|
|
29
32
|
// Для компонентов (UniHtmlComponent) на этом этапе ещё нельзя полагаться на this.shadowRoot —
|
|
30
33
|
// используйте переданный localRoot для подготовки DOM, данных и навешивания обработчиков.
|
|
31
34
|
// Это предпочтительный этап инициализации для компонентов.
|
|
32
|
-
await this.preLoad(
|
|
35
|
+
await this.preLoad(holder);
|
|
33
36
|
// render() отвечает за помещение содержимого из localRoot в конечную цель (renderTarget).
|
|
34
37
|
// В UniHtmlComponent.render() после вызова базового render() происходит добавление wrapper в shadowRoot.
|
|
35
|
-
await this.render(
|
|
38
|
+
await this.render(holder, element);
|
|
36
39
|
// postLoad() вызывается ПОСЛЕ render(). Для компонентов к этому моменту содержимое уже добавлено
|
|
37
40
|
// внутрь shadowRoot, и можно безопасно работать с this.shadowRoot, измерениями layout и т.п.
|
|
38
|
-
await this.postLoad(
|
|
41
|
+
await this.postLoad(holder);
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
private async _postInit(html:
|
|
44
|
+
private async _postInit(html: DocumentFragment): Promise<DocumentFragment> {
|
|
42
45
|
throw new Error("Method not implemented.");
|
|
43
46
|
}
|
|
44
|
-
private async _init(): Promise<
|
|
47
|
+
private async _init(): Promise<DocumentFragment> {
|
|
45
48
|
throw new Error("Method not implemented.");
|
|
46
49
|
}
|
|
47
50
|
|
|
@@ -52,27 +55,34 @@ export default class UniHtml {
|
|
|
52
55
|
* РЕКОМЕНДАЦИЯ: предпочитайте выполнять основную подготовку, поиск элементов, навешивание обработчиков
|
|
53
56
|
* на узлы из localRoot именно здесь; затем render() вставит их в целевой контейнер/теневой DOM.
|
|
54
57
|
*/
|
|
55
|
-
protected async preLoad(
|
|
58
|
+
protected async preLoad(holder : IElementHolder) { }
|
|
56
59
|
/**
|
|
57
60
|
* Hook after rendering (e.g., event binding).
|
|
58
61
|
* Для компонентов вызывается после того, как содержимое вставлено в shadowRoot (см. UniHtmlComponent.render()).
|
|
59
62
|
* Используйте этот этап только когда необходим доступ к реально смонтированному DOM (layout/measurements,
|
|
60
63
|
* интеграции, требующие присутствия в документе). В остальных случаях предпочитайте preLoad().
|
|
61
64
|
*/
|
|
62
|
-
protected async postLoad(
|
|
65
|
+
protected async postLoad(holder: IElementHolder) { }
|
|
63
66
|
/**
|
|
64
67
|
* Main rendering step. By default, simply inserts HTML into the container.
|
|
65
68
|
* Override in subclasses for custom rendering logic.
|
|
66
69
|
* @param element Target container
|
|
67
70
|
* @param html HTML content
|
|
68
71
|
*/
|
|
69
|
-
protected async render(
|
|
72
|
+
protected async render(holder: IElementHolder, renderTarget: HTMLElement | DocumentFragment): Promise<void> {
|
|
70
73
|
// Clear renderTarget
|
|
71
74
|
while (renderTarget.firstChild) {
|
|
72
75
|
renderTarget.removeChild(renderTarget.firstChild);
|
|
73
76
|
}
|
|
74
|
-
|
|
77
|
+
|
|
78
|
+
// Move all children from holder.element to renderTarget
|
|
79
|
+
const children = Array.from(holder.element.childNodes);
|
|
80
|
+
for (const child of children) {
|
|
81
|
+
renderTarget.appendChild(child);
|
|
82
|
+
}
|
|
75
83
|
|
|
84
|
+
// Update holder to point to renderTarget (now contains the content)
|
|
85
|
+
(holder as { element: HTMLElement | DocumentFragment }).element = renderTarget;
|
|
76
86
|
return Promise.resolve();
|
|
77
87
|
}
|
|
78
88
|
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import Scope from './Scope.js';
|
|
2
|
+
import Expression from './Expression.js';
|
|
3
|
+
import EscapeHandler from './EscapeHandler.js';
|
|
4
|
+
import TemplateInstance, { TemplateSection, FragmentBinding } from './TemplateInstance.js';
|
|
5
|
+
import Rule, { RuleMatch, RuleResult, SyntaxRule, AttributeRule } from './Rule.js';
|
|
6
|
+
import Observable from '../api/Observer.js';
|
|
7
|
+
|
|
8
|
+
// Import rules
|
|
9
|
+
import ExpressionRule from './rules/syntax/ExpressionRule.js';
|
|
10
|
+
import IfRule from './rules/syntax/IfRule.js';
|
|
11
|
+
import ForRule from './rules/syntax/ForRule.js';
|
|
12
|
+
import RefRule from './rules/attribute/RefRule.js';
|
|
13
|
+
import EventRule from './rules/attribute/EventRule.js';
|
|
14
|
+
import InjectionRule from './rules/attribute/InjectionRule.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* TemplateEngineOptions - настройки TemplateEngine
|
|
18
|
+
*/
|
|
19
|
+
export interface TemplateEngineOptions {
|
|
20
|
+
/** Оставлять синтаксис атрибутивных Rule в финальном HTML */
|
|
21
|
+
showAttributeRule?: boolean;
|
|
22
|
+
/** Включить предупреждения отладки */
|
|
23
|
+
debugWarnings?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* ProcessResult - результат обработки шаблона
|
|
28
|
+
*/
|
|
29
|
+
export interface ProcessResult {
|
|
30
|
+
output: string;
|
|
31
|
+
observables: Observable<any>[];
|
|
32
|
+
sections: TemplateSection[];
|
|
33
|
+
/** ID созданного фрагмента (если был создан) */
|
|
34
|
+
fragmentId?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* TemplateEngine - главный класс шаблонизатора.
|
|
39
|
+
* Обрабатывает HTML-шаблон с Rule и создаёт TemplateInstance.
|
|
40
|
+
*/
|
|
41
|
+
export default class TemplateEngine {
|
|
42
|
+
private readonly scope: Scope;
|
|
43
|
+
private readonly options: TemplateEngineOptions;
|
|
44
|
+
private readonly syntaxRules: SyntaxRule[] = [];
|
|
45
|
+
private readonly attributeRules: AttributeRule[] = [];
|
|
46
|
+
|
|
47
|
+
constructor(scope: Scope | object, options?: TemplateEngineOptions) {
|
|
48
|
+
this.scope = scope instanceof Scope ? scope : Scope.from(scope);
|
|
49
|
+
this.options = {
|
|
50
|
+
showAttributeRule: false,
|
|
51
|
+
debugWarnings: true,
|
|
52
|
+
...options
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Register default rules (sorted by priority)
|
|
56
|
+
this.registerDefaultRules();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Зарегистрировать стандартные правила
|
|
61
|
+
*/
|
|
62
|
+
private registerDefaultRules(): void {
|
|
63
|
+
// Syntax rules
|
|
64
|
+
this.syntaxRules.push(new ForRule());
|
|
65
|
+
this.syntaxRules.push(new IfRule());
|
|
66
|
+
this.syntaxRules.push(new ExpressionRule());
|
|
67
|
+
|
|
68
|
+
// Attribute rules
|
|
69
|
+
this.attributeRules.push(new RefRule());
|
|
70
|
+
this.attributeRules.push(new EventRule());
|
|
71
|
+
this.attributeRules.push(new InjectionRule());
|
|
72
|
+
|
|
73
|
+
// Sort by priority
|
|
74
|
+
this.syntaxRules.sort((a, b) => a.priority - b.priority);
|
|
75
|
+
this.attributeRules.sort((a, b) => a.priority - b.priority);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Добавить пользовательское правило
|
|
80
|
+
*/
|
|
81
|
+
public addRule(rule: Rule): void {
|
|
82
|
+
if (rule.type === 'syntax') {
|
|
83
|
+
this.syntaxRules.push(rule as SyntaxRule);
|
|
84
|
+
this.syntaxRules.sort((a, b) => a.priority - b.priority);
|
|
85
|
+
} else {
|
|
86
|
+
this.attributeRules.push(rule as AttributeRule);
|
|
87
|
+
this.attributeRules.sort((a, b) => a.priority - b.priority);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Получить Scope
|
|
93
|
+
*/
|
|
94
|
+
public getScope(): Scope {
|
|
95
|
+
return this.scope;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Обработать шаблон и вернуть TemplateInstance
|
|
100
|
+
*/
|
|
101
|
+
public parse(template: string): TemplateInstance {
|
|
102
|
+
const templateInstance = new TemplateInstance(this.scope);
|
|
103
|
+
const result = this.processTemplateWithFragments(template, this.scope, templateInstance, null);
|
|
104
|
+
|
|
105
|
+
// Устанавливаем корневой фрагмент
|
|
106
|
+
if (result.fragmentId) {
|
|
107
|
+
templateInstance.setRootFragment(result.fragmentId);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Add sections and track observables
|
|
111
|
+
for (const section of result.sections) {
|
|
112
|
+
templateInstance.addSection(section);
|
|
113
|
+
|
|
114
|
+
// Track observables
|
|
115
|
+
for (const observable of section.result.observables || []) {
|
|
116
|
+
templateInstance.trackObservable(observable, section, (s) => {
|
|
117
|
+
return this.processTemplate(s.sourceTemplate, this.scope);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return templateInstance;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Обработать шаблон с созданием фрагментов
|
|
127
|
+
*/
|
|
128
|
+
private processTemplateWithFragments(
|
|
129
|
+
template: string,
|
|
130
|
+
scope: Scope,
|
|
131
|
+
instance: TemplateInstance,
|
|
132
|
+
parentFragmentId: string | null
|
|
133
|
+
): ProcessResult {
|
|
134
|
+
const result = this.processTemplate(template, scope);
|
|
135
|
+
|
|
136
|
+
// Создаём фрагмент для этого результата
|
|
137
|
+
const fragmentId = instance.createFragment(
|
|
138
|
+
result.output,
|
|
139
|
+
template,
|
|
140
|
+
result.sections,
|
|
141
|
+
parentFragmentId
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
...result,
|
|
146
|
+
fragmentId
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Обработать шаблон (внутренний метод, используется Rule для рекурсии)
|
|
152
|
+
*/
|
|
153
|
+
public processTemplate(template: string, scope: Scope): ProcessResult {
|
|
154
|
+
const allObservables: Observable<any>[] = [];
|
|
155
|
+
const allSections: TemplateSection[] = [];
|
|
156
|
+
|
|
157
|
+
// Step 1: Handle @@ escape sequences
|
|
158
|
+
let processed = EscapeHandler.process(template, (escaped) => {
|
|
159
|
+
let result = escaped;
|
|
160
|
+
|
|
161
|
+
// Step 2: Process syntax rules (in priority order)
|
|
162
|
+
for (const rule of this.syntaxRules) {
|
|
163
|
+
result = this.processRule(rule, result, scope, allObservables, allSections);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Step 3: Process attribute rules
|
|
167
|
+
for (const rule of this.attributeRules) {
|
|
168
|
+
// Skip injection - processed last
|
|
169
|
+
if (rule.name === 'injection') continue;
|
|
170
|
+
result = this.processRule(rule, result, scope, allObservables, allSections);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return result;
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Step 4: Process injection rules last
|
|
177
|
+
const injectionRule = this.attributeRules.find(r => r.name === 'injection');
|
|
178
|
+
if (injectionRule) {
|
|
179
|
+
processed = this.processRule(injectionRule, processed, scope, allObservables, allSections);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Step 5: Handle showAttributeRule option
|
|
183
|
+
if (!this.options.showAttributeRule) {
|
|
184
|
+
processed = this.removeAttributeSyntax(processed);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
output: processed,
|
|
189
|
+
observables: allObservables,
|
|
190
|
+
sections: allSections
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Обработать одно правило
|
|
196
|
+
*/
|
|
197
|
+
private processRule(
|
|
198
|
+
rule: Rule,
|
|
199
|
+
template: string,
|
|
200
|
+
scope: Scope,
|
|
201
|
+
observables: Observable<any>[],
|
|
202
|
+
sections: TemplateSection[]
|
|
203
|
+
): string {
|
|
204
|
+
const matches = rule.find(template);
|
|
205
|
+
|
|
206
|
+
// Sort matches by position (reverse to process from end)
|
|
207
|
+
matches.sort((a, b) => b.start - a.start);
|
|
208
|
+
|
|
209
|
+
let result = template;
|
|
210
|
+
|
|
211
|
+
for (const match of matches) {
|
|
212
|
+
try {
|
|
213
|
+
const ruleResult = rule.execute(match, scope, this) as RuleResult;
|
|
214
|
+
|
|
215
|
+
// Track observables
|
|
216
|
+
if (ruleResult.observables) {
|
|
217
|
+
observables.push(...ruleResult.observables);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Create section
|
|
221
|
+
const section: TemplateSection = {
|
|
222
|
+
rule,
|
|
223
|
+
match,
|
|
224
|
+
result: ruleResult,
|
|
225
|
+
sourceTemplate: match.fullMatch,
|
|
226
|
+
children: [],
|
|
227
|
+
subscriptions: []
|
|
228
|
+
};
|
|
229
|
+
sections.push(section);
|
|
230
|
+
|
|
231
|
+
// Replace in template
|
|
232
|
+
result = result.slice(0, match.start) + ruleResult.output + result.slice(match.end);
|
|
233
|
+
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error(`[TemplateEngine] Error processing rule "${rule.name}":`, error);
|
|
236
|
+
// Continue processing other rules
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Удалить синтаксис атрибутивных Rule из финального HTML
|
|
245
|
+
*/
|
|
246
|
+
private removeAttributeSyntax(template: string): string {
|
|
247
|
+
// Remove @[ref], @on[...], @injection[...] patterns that weren't processed
|
|
248
|
+
return template
|
|
249
|
+
.replace(/@\[ref\]\s*=\s*["'][^"']*["']/gi, '')
|
|
250
|
+
.replace(/@on\[[a-zA-Z]+\]\s*=\s*["'][^"']*["']/gi, '')
|
|
251
|
+
.replace(/@injection\[(head|tail)\]\s*=\s*["'][^"']*["']/gi, '');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Добавить новый шаблон в существующий TemplateInstance.
|
|
256
|
+
* Обрабатывает шаблон и добавляет результат как новый фрагмент.
|
|
257
|
+
* Если instance привязан к контейнерам, DOM обновится автоматически.
|
|
258
|
+
*
|
|
259
|
+
* @param instance - существующий TemplateInstance
|
|
260
|
+
* @param template - новый шаблон для добавления
|
|
261
|
+
* @param customScope - опциональный scope для нового шаблона
|
|
262
|
+
* @returns ID созданного фрагмента
|
|
263
|
+
*/
|
|
264
|
+
public appendTemplate(instance: TemplateInstance, template: string, customScope?: Scope | object): string {
|
|
265
|
+
const scope = customScope
|
|
266
|
+
? (customScope instanceof Scope ? customScope : Scope.from(customScope))
|
|
267
|
+
: instance.getScope();
|
|
268
|
+
|
|
269
|
+
// Обрабатываем шаблон
|
|
270
|
+
const result = this.processTemplate(template, scope);
|
|
271
|
+
|
|
272
|
+
// Создаём фрагмент
|
|
273
|
+
const fragmentId = instance.createFragment(
|
|
274
|
+
result.output,
|
|
275
|
+
template,
|
|
276
|
+
result.sections,
|
|
277
|
+
null // без родителя - это отдельный фрагмент
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
// Добавляем секции и отслеживаем Observable
|
|
281
|
+
for (const section of result.sections) {
|
|
282
|
+
instance.addSection(section);
|
|
283
|
+
|
|
284
|
+
for (const observable of section.result.observables || []) {
|
|
285
|
+
instance.trackObservable(observable, section, (s) => {
|
|
286
|
+
return this.processTemplate(s.sourceTemplate, scope);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Вставляем в привязанные контейнеры
|
|
292
|
+
instance.insertAppendedFragment(fragmentId);
|
|
293
|
+
|
|
294
|
+
return fragmentId;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Статический метод для быстрой обработки
|
|
299
|
+
*/
|
|
300
|
+
public static process(template: string, scope: object, options?: TemplateEngineOptions): string {
|
|
301
|
+
const engine = new TemplateEngine(scope, options);
|
|
302
|
+
const TemplateInstance = engine.parse(template);
|
|
303
|
+
return TemplateInstance.getTemplate();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Создать TemplateInstance из шаблона
|
|
308
|
+
*/
|
|
309
|
+
public static create(template: string, scope: object, options?: TemplateEngineOptions): TemplateInstance {
|
|
310
|
+
const engine = new TemplateEngine(scope, options);
|
|
311
|
+
return engine.parse(template);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Re-export useful types
|
|
316
|
+
export { Scope, Expression, TemplateInstance, Rule, SyntaxRule, AttributeRule };
|
|
317
|
+
export { ExpressionRule, IfRule, ForRule, RefRule, EventRule, InjectionRule };
|
|
318
|
+
export * from './exceptions/TemplateExceptions.js';
|