@teleporthq/teleport-plugin-html-base-component 0.43.0-alpha.0 → 0.43.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.
@@ -56,72 +56,472 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
56
56
  return to.concat(ar || Array.prototype.slice.call(from));
57
57
  };
58
58
  Object.defineProperty(exports, "__esModule", { value: true });
59
- exports.generateHtmlSynatx = void 0;
59
+ exports.generateHtmlSyntax = void 0;
60
60
  var teleport_types_1 = require("@teleporthq/teleport-types");
61
61
  var path_1 = require("path");
62
62
  var teleport_plugin_common_1 = require("@teleporthq/teleport-plugin-common");
63
63
  var teleport_shared_1 = require("@teleporthq/teleport-shared");
64
64
  var teleport_uidl_builders_1 = require("@teleporthq/teleport-uidl-builders");
65
65
  var teleport_plugin_css_1 = require("@teleporthq/teleport-plugin-css");
66
+ var teleport_uidl_resolver_1 = require("@teleporthq/teleport-uidl-resolver");
66
67
  var constants_1 = require("./constants");
67
- var generateHtmlSynatx = function (node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
68
- return __generator(this, function (_a) {
69
- switch (node.type) {
70
- case 'inject':
71
- case 'raw':
72
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(node.content.toString())];
73
- case 'static':
74
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(teleport_shared_1.StringUtils.encode(node.content.toString()))];
75
- case 'slot':
76
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode(node.type)];
77
- case 'element':
78
- return [2 /*return*/, generatElementNode(node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure)];
79
- case 'dynamic':
80
- return [2 /*return*/, generateDynamicNode(node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure)];
81
- default:
82
- throw new teleport_types_1.HTMLComponentGeneratorError("generateHtmlSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2), " "));
68
+ var getTranslation = function (id, options) {
69
+ var _a, _b;
70
+ var i18n = options.internationalization;
71
+ if (!(i18n === null || i18n === void 0 ? void 0 : i18n.translations)) {
72
+ return null;
73
+ }
74
+ var locale = i18n.targetLocale || ((_a = i18n.main) === null || _a === void 0 ? void 0 : _a.locale);
75
+ if (!locale) {
76
+ return null;
77
+ }
78
+ return ((_b = i18n.translations[locale]) === null || _b === void 0 ? void 0 : _b[id]) || null;
79
+ };
80
+ var resolveTranslationText = function (translation) {
81
+ if (translation.type === 'static') {
82
+ return String(translation.content);
83
+ }
84
+ if (translation.type === 'element' && translation.content.children) {
85
+ return translation.content.children
86
+ .map(function (child) {
87
+ if (child.type === 'static') {
88
+ return String(child.content);
89
+ }
90
+ if (child.type === 'element') {
91
+ return resolveTranslationText(child);
92
+ }
93
+ return '';
94
+ })
95
+ .join('');
96
+ }
97
+ return '';
98
+ };
99
+ var isValidURL = function (url) {
100
+ try {
101
+ /* tslint:disable:no-unused-expression */
102
+ new URL(url);
103
+ return true;
104
+ }
105
+ catch (error) {
106
+ return false;
107
+ }
108
+ };
109
+ var addNodeToLookup = function (key, tag, nodesLoookup) {
110
+ // In html code-generation we combine the nodes of the component that is being consumed with the current component.
111
+ // As html can't load the component at runtime like react or any other frameworks. So, we merge the component as a standalone
112
+ // component in the current component.
113
+ var currentLookup = nodesLoookup[key];
114
+ if (currentLookup) {
115
+ if (Array.isArray(currentLookup)) {
116
+ Array.isArray(tag) ? currentLookup.push.apply(currentLookup, tag) : currentLookup.push(tag);
117
+ }
118
+ else {
119
+ nodesLoookup[key] = Array.isArray(tag) ? __spreadArray([currentLookup], tag, true) : [currentLookup, tag];
120
+ }
121
+ return;
122
+ }
123
+ nodesLoookup[key] = tag;
124
+ };
125
+ var generateHtmlSyntax = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
126
+ var _a, rawNode, elementNode, dynamicNode, repeatNode, conditionalNodeComment, _b, staticValue, reference, _c, conditions, matchingCriteria, _d, referenceType, id, _e, refPath, usedProp, defaultValue, _i, refPath_1, path, dynamicConditions, matchCondition, conditionString, isConditionPassing, expressionEntries, localValue, _f, expressionEntries_1, expr, currentItem, _g, refPath_2, path, localConditions, localMatchCondition, localConditionString, isLocalConditionPassing, content, uidlDynamicRef, generatedNode, isProp, uidlDynamicRef, generatedNode, successNode;
127
+ var _h, _j;
128
+ return __generator(this, function (_k) {
129
+ switch (_k.label) {
130
+ case 0:
131
+ _a = node.type;
132
+ switch (_a) {
133
+ case 'inject': return [3 /*break*/, 1];
134
+ case 'raw': return [3 /*break*/, 2];
135
+ case 'static': return [3 /*break*/, 3];
136
+ case 'slot': return [3 /*break*/, 4];
137
+ case 'element': return [3 /*break*/, 5];
138
+ case 'dynamic': return [3 /*break*/, 7];
139
+ case 'repeat': return [3 /*break*/, 9];
140
+ case 'conditional': return [3 /*break*/, 11];
141
+ case 'expr': return [3 /*break*/, 12];
142
+ case 'cms-list-repeater': return [3 /*break*/, 17];
143
+ case 'cms-item': return [3 /*break*/, 18];
144
+ case 'cms-list': return [3 /*break*/, 18];
145
+ case 'data-source-item': return [3 /*break*/, 18];
146
+ case 'data-source-list': return [3 /*break*/, 18];
147
+ }
148
+ return [3 /*break*/, 19];
149
+ case 1: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(node.content.toString())];
150
+ case 2:
151
+ {
152
+ rawNode = node;
153
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode((rawNode.fallback || rawNode.content).toString())];
154
+ }
155
+ _k.label = 3;
156
+ case 3: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(teleport_shared_1.StringUtils.encode(node.content.toString()))];
157
+ case 4: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode(node.type)];
158
+ case 5: return [4 /*yield*/, generateElementNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
159
+ case 6:
160
+ elementNode = _k.sent();
161
+ return [2 /*return*/, elementNode];
162
+ case 7: return [4 /*yield*/, generateDynamicNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
163
+ case 8:
164
+ dynamicNode = _k.sent();
165
+ return [2 /*return*/, dynamicNode];
166
+ case 9: return [4 /*yield*/, (0, exports.generateHtmlSyntax)(node.content.node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
167
+ case 10:
168
+ repeatNode = _k.sent();
169
+ return [2 /*return*/, repeatNode];
170
+ case 11:
171
+ conditionalNodeComment = teleport_plugin_common_1.HASTBuilders.createTextNode('');
172
+ _b = node.content, staticValue = _b.value, reference = _b.reference, _c = _b.condition, conditions = _c.conditions, matchingCriteria = _c.matchingCriteria;
173
+ if (reference.type !== 'dynamic') {
174
+ return [2 /*return*/, conditionalNodeComment];
175
+ }
176
+ _d = reference.content, referenceType = _d.referenceType, id = _d.id, _e = _d.refPath, refPath = _e === void 0 ? [] : _e;
177
+ switch (referenceType) {
178
+ case 'prop': {
179
+ usedProp = propDefinitions[id];
180
+ if (usedProp === undefined || usedProp.defaultValue === undefined) {
181
+ return [2 /*return*/, conditionalNodeComment];
182
+ }
183
+ defaultValue = usedProp.defaultValue;
184
+ for (_i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) {
185
+ path = refPath_1[_i];
186
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
187
+ }
188
+ // If defaultValue is undefined or null after path traversal, use original default
189
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : usedProp.defaultValue;
190
+ dynamicConditions = createConditionalStatement(staticValue !== undefined ? [{ operand: staticValue, operation: '===' }] : conditions, defaultValue);
191
+ matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
192
+ conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
193
+ try {
194
+ isConditionPassing = new Function("return ".concat(conditionString))();
195
+ if (isConditionPassing) {
196
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(node.content.node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
197
+ }
198
+ }
199
+ catch (error) {
200
+ return [2 /*return*/, conditionalNodeComment];
201
+ }
202
+ return [2 /*return*/, conditionalNodeComment];
203
+ }
204
+ case 'local': {
205
+ if (!resolvedExpressions || resolvedExpressions.currentIndex === undefined) {
206
+ return [2 /*return*/, conditionalNodeComment];
207
+ }
208
+ expressionEntries = Object.values(resolvedExpressions.expressions || {});
209
+ localValue = void 0;
210
+ for (_f = 0, expressionEntries_1 = expressionEntries; _f < expressionEntries_1.length; _f++) {
211
+ expr = expressionEntries_1[_f];
212
+ if (expr && Array.isArray(expr.defaultValue)) {
213
+ currentItem = expr.defaultValue[resolvedExpressions.currentIndex];
214
+ if (currentItem !== undefined) {
215
+ localValue = currentItem;
216
+ for (_g = 0, refPath_2 = refPath; _g < refPath_2.length; _g++) {
217
+ path = refPath_2[_g];
218
+ localValue = localValue === null || localValue === void 0 ? void 0 : localValue[path];
219
+ }
220
+ break;
221
+ }
222
+ }
223
+ }
224
+ if (localValue === undefined) {
225
+ return [2 /*return*/, conditionalNodeComment];
226
+ }
227
+ localConditions = createConditionalStatement(staticValue !== undefined ? [{ operand: staticValue, operation: '===' }] : conditions, localValue);
228
+ localMatchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
229
+ localConditionString = localConditions.join(" ".concat(localMatchCondition, " "));
230
+ try {
231
+ isLocalConditionPassing = new Function("return ".concat(localConditionString))();
232
+ if (isLocalConditionPassing) {
233
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(node.content.node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
234
+ }
235
+ }
236
+ catch (error) {
237
+ return [2 /*return*/, conditionalNodeComment];
238
+ }
239
+ return [2 /*return*/, conditionalNodeComment];
240
+ }
241
+ case 'state':
242
+ default:
243
+ return [2 /*return*/, conditionalNodeComment];
244
+ }
245
+ _k.label = 12;
246
+ case 12:
247
+ content = node.content.split('?.');
248
+ if (!(resolvedExpressions && ((_h = resolvedExpressions.expressions) === null || _h === void 0 ? void 0 : _h[content[0] || '']))) return [3 /*break*/, 14];
249
+ uidlDynamicRef = {
250
+ type: 'dynamic',
251
+ content: {
252
+ referenceType: 'prop',
253
+ refPath: __spreadArray([resolvedExpressions.currentIndex.toString()], content.slice(1), true),
254
+ id: content[0],
255
+ },
256
+ };
257
+ return [4 /*yield*/, generateDynamicNode(uidlDynamicRef, compName, nodesLookup, resolvedExpressions.expressions, stateDefinitions, subComponentOptions, structure)];
258
+ case 13:
259
+ generatedNode = _k.sent();
260
+ return [2 /*return*/, generatedNode];
261
+ case 14:
262
+ if (!(content[0] && ((propDefinitions === null || propDefinitions === void 0 ? void 0 : propDefinitions[content[0]]) || (stateDefinitions === null || stateDefinitions === void 0 ? void 0 : stateDefinitions[content[0]])))) return [3 /*break*/, 16];
263
+ isProp = Boolean(propDefinitions === null || propDefinitions === void 0 ? void 0 : propDefinitions[content[0]]);
264
+ uidlDynamicRef = {
265
+ type: 'dynamic',
266
+ content: {
267
+ referenceType: isProp ? 'prop' : 'state',
268
+ refPath: content.slice(1),
269
+ id: content[0],
270
+ },
271
+ };
272
+ return [4 /*yield*/, generateDynamicNode(uidlDynamicRef, compName, nodesLookup, isProp ? propDefinitions : stateDefinitions, stateDefinitions, subComponentOptions, structure)];
273
+ case 15:
274
+ generatedNode = _k.sent();
275
+ return [2 /*return*/, generatedNode];
276
+ case 16: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createComment('Expressions are not supported in HTML')];
277
+ case 17: return [2 /*return*/, generateRepeaterNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
278
+ case 18:
279
+ successNode = (_j = node.content.nodes) === null || _j === void 0 ? void 0 : _j.success;
280
+ if (successNode) {
281
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(successNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
282
+ }
283
+ // If no success node, return empty div
284
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode('div')];
285
+ case 19: throw new teleport_types_1.HTMLComponentGeneratorError("generateHtmlSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2), " "));
83
286
  }
84
- return [2 /*return*/];
85
287
  });
86
288
  }); };
87
- exports.generateHtmlSynatx = generateHtmlSynatx;
88
- var generatElementNode = function (node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
89
- var _a, elementType, children, _b, attrs, _c, style, _d, referencedStyles, dependency, key, elementNode, dependencies, compTag, _i, children_1, child, childTag;
90
- return __generator(this, function (_e) {
91
- switch (_e.label) {
289
+ exports.generateHtmlSyntax = generateHtmlSyntax;
290
+ var createConditionalStatement = function (conditions, leftOperand) {
291
+ return conditions.map(function (condition) {
292
+ var operation = condition.operation, operand = condition.operand;
293
+ if (operand === undefined) {
294
+ return "".concat(teleport_plugin_common_1.ASTUtils.convertToUnaryOperator(operation)).concat(getValueType(operand));
295
+ }
296
+ return "".concat(getValueType(leftOperand), " ").concat(teleport_plugin_common_1.ASTUtils.convertToBinaryOperator(operation), " ").concat(getValueType(operand));
297
+ });
298
+ };
299
+ var getValueType = function (value) {
300
+ var valueType = typeof value;
301
+ switch (valueType) {
302
+ case 'string':
303
+ return "\"".concat(value, "\"");
304
+ case 'number':
305
+ return value;
306
+ case 'boolean':
307
+ return value;
308
+ case 'object':
309
+ // `typeof null === 'object'` — render as the null literal so comparisons
310
+ // against a missing/null default evaluate sensibly.
311
+ if (value === null) {
312
+ return 'null';
313
+ }
314
+ // Handle dynamic references (local, prop, state)
315
+ if (value && typeof value === 'object' && 'type' in value) {
316
+ var dynamicValue = value;
317
+ if (dynamicValue.type === 'dynamic' && dynamicValue.content) {
318
+ var _a = dynamicValue.content, referenceType = _a.referenceType, id = _a.id, refPath = _a.refPath;
319
+ if (referenceType === 'local' && id) {
320
+ // Local reference from repeater context
321
+ if (refPath && refPath.length > 0) {
322
+ return "".concat(id, ".").concat(refPath.join('.'));
323
+ }
324
+ return id;
325
+ }
326
+ else if (referenceType === 'prop' || referenceType === 'state') {
327
+ // Prop or state reference
328
+ var key = refPath && refPath.length > 0 ? refPath.join('.') : id;
329
+ return key;
330
+ }
331
+ }
332
+ }
333
+ // Handle link-type prop default values ({ url, newTab }). Collapse to the
334
+ // url string so comparisons like `mapUrl !== '--'` evaluate sensibly.
335
+ if (value &&
336
+ typeof value === 'object' &&
337
+ 'url' in value &&
338
+ typeof value.url === 'string') {
339
+ return "\"".concat(value.url, "\"");
340
+ }
341
+ throw new teleport_types_1.HTMLComponentGeneratorError("Conditional node received an operand of type ".concat(valueType, " \n\n Received ").concat(JSON.stringify(value)));
342
+ default:
343
+ throw new teleport_types_1.HTMLComponentGeneratorError("Conditional node received an operand of type ".concat(valueType, " \n\n Received ").concat(JSON.stringify(value)));
344
+ }
345
+ };
346
+ var generateRepeaterNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
347
+ var nodes, contextId, sourceValue, propDef, nestedPath, parentContextKey, parentPropDef, pathMatch, parentItem, nestedValue, parsedSource, elementNode, emptyChildren, _i, emptyChildren_1, child, childTag, listChildren, index, _a, listChildren_1, child, childTag;
348
+ var _b;
349
+ var _c;
350
+ return __generator(this, function (_d) {
351
+ switch (_d.label) {
92
352
  case 0:
93
- _a = node.content, elementType = _a.elementType, children = _a.children, _b = _a.attrs, attrs = _b === void 0 ? {} : _b, _c = _a.style, style = _c === void 0 ? {} : _c, _d = _a.referencedStyles, referencedStyles = _d === void 0 ? {} : _d, dependency = _a.dependency, key = _a.key;
94
- elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(elementType);
95
- templatesLookUp[key] = elementNode;
96
- dependencies = structure.dependencies;
97
- if (dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) !== 'local') {
98
- dependencies[dependency.path] = dependency;
353
+ nodes = node.content.nodes;
354
+ contextId = node.content.renderPropIdentifier;
355
+ sourceValue = node.content.source;
356
+ propDef = sourceValue && typeof sourceValue === 'string'
357
+ ? propDefinitions[Object.keys(propDefinitions).find(function (propKey) { return sourceValue.includes(propKey); }) || '']
358
+ : undefined;
359
+ nestedPath = null;
360
+ if (propDef && resolvedExpressions && sourceValue && typeof sourceValue === 'string') {
361
+ parentContextKey = Object.keys(resolvedExpressions.expressions || {}).find(function (key) {
362
+ return sourceValue.includes(key);
363
+ });
364
+ if (parentContextKey) {
365
+ parentPropDef = resolvedExpressions.expressions[parentContextKey];
366
+ pathMatch = sourceValue.match(new RegExp("".concat(parentContextKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "((?:\\?\\.\\w+)+)")));
367
+ if (pathMatch && pathMatch[1]) {
368
+ nestedPath = pathMatch[1].split('?.').filter(Boolean);
369
+ }
370
+ if (nestedPath && parentPropDef && Array.isArray(parentPropDef.defaultValue)) {
371
+ parentItem = parentPropDef.defaultValue[resolvedExpressions.currentIndex];
372
+ if (parentItem && typeof parentItem === 'object' && !Array.isArray(parentItem)) {
373
+ nestedValue = nestedPath.reduce(function (acc, key) {
374
+ return acc && typeof acc === 'object' && !Array.isArray(acc)
375
+ ? acc[key]
376
+ : acc;
377
+ }, parentItem);
378
+ if (Array.isArray(nestedValue)) {
379
+ propDef = {
380
+ defaultValue: nestedValue,
381
+ id: contextId,
382
+ type: 'array',
383
+ };
384
+ }
385
+ }
386
+ }
387
+ }
388
+ }
389
+ if (!propDef || !Array.isArray(propDef.defaultValue)) {
390
+ // If no prop is found we might have a static source value
391
+ try {
392
+ parsedSource = JSON.parse(sourceValue);
393
+ propDef = {
394
+ defaultValue: parsedSource,
395
+ id: contextId,
396
+ type: 'array',
397
+ };
398
+ }
399
+ catch (_e) {
400
+ // Silent fail
401
+ }
402
+ }
403
+ // We do the check again to keep typescript happy, otherwise this could be in catch
404
+ if (!propDef || !Array.isArray(propDef.defaultValue)) {
405
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createComment('CMS Array Mapper/Repeater not supported in HTML without a prop source')];
406
+ }
407
+ propDefinitions[contextId] = propDef;
408
+ elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode('div');
409
+ node.content.nodes.list.content.style = { display: { type: 'static', content: 'contents' } };
410
+ if (node.content.nodes.empty) {
411
+ node.content.nodes.empty.content.style = { display: { type: 'static', content: 'contents' } };
412
+ }
413
+ if (node.content.nodes.loading) {
414
+ node.content.nodes.loading.content.style = { display: { type: 'static', content: 'contents' } };
415
+ }
416
+ if (!(propDef.defaultValue.length === 0)) return [3 /*break*/, 5];
417
+ emptyChildren = (_c = nodes.empty) === null || _c === void 0 ? void 0 : _c.content.children;
418
+ if (!emptyChildren) return [3 /*break*/, 4];
419
+ _i = 0, emptyChildren_1 = emptyChildren;
420
+ _d.label = 1;
421
+ case 1:
422
+ if (!(_i < emptyChildren_1.length)) return [3 /*break*/, 4];
423
+ child = emptyChildren_1[_i];
424
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
425
+ case 2:
426
+ childTag = _d.sent();
427
+ if (typeof childTag === 'string') {
428
+ teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
429
+ }
430
+ else {
431
+ teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
432
+ }
433
+ _d.label = 3;
434
+ case 3:
435
+ _i++;
436
+ return [3 /*break*/, 1];
437
+ case 4:
438
+ if (nodes.empty) {
439
+ addNodeToLookup("".concat(node.content.nodes.empty.content.key), elementNode, nodesLookup);
440
+ }
441
+ return [2 /*return*/, elementNode];
442
+ case 5:
443
+ listChildren = nodes.list.content.children;
444
+ if (!listChildren) return [3 /*break*/, 11];
445
+ index = 0;
446
+ _d.label = 6;
447
+ case 6:
448
+ if (!(index < propDef.defaultValue.length)) return [3 /*break*/, 11];
449
+ _a = 0, listChildren_1 = listChildren;
450
+ _d.label = 7;
451
+ case 7:
452
+ if (!(_a < listChildren_1.length)) return [3 /*break*/, 10];
453
+ child = listChildren_1[_a];
454
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, { currentIndex: index, expressions: (_b = {}, _b[contextId] = propDef, _b) })];
455
+ case 8:
456
+ childTag = _d.sent();
457
+ if (typeof childTag === 'string') {
458
+ teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
459
+ }
460
+ else {
461
+ teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
462
+ }
463
+ _d.label = 9;
464
+ case 9:
465
+ _a++;
466
+ return [3 /*break*/, 7];
467
+ case 10:
468
+ index++;
469
+ return [3 /*break*/, 6];
470
+ case 11:
471
+ addNodeToLookup("".concat(node.content.nodes.list.content.key), elementNode, nodesLookup);
472
+ return [2 /*return*/, elementNode];
473
+ }
474
+ });
475
+ }); };
476
+ var generateElementNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
477
+ var successNode, _a, elementType, children, _b, attrs, _c, style, _d, referencedStyles, dependency, dependencies, compTag, elementNode, _i, children_1, child, childTag;
478
+ var _e;
479
+ return __generator(this, function (_f) {
480
+ switch (_f.label) {
481
+ case 0:
482
+ // Check if this is a wrapped data-source node
483
+ if (node.content &&
484
+ typeof node.content === 'object' &&
485
+ 'type' in node.content &&
486
+ (node.content.type === 'data-source-item' || node.content.type === 'data-source-list')) {
487
+ successNode = (_e = node.content.nodes) === null || _e === void 0 ? void 0 : _e.success;
488
+ if (successNode) {
489
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(successNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
490
+ }
491
+ // If no success node, return empty div
492
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode('div')];
99
493
  }
494
+ _a = node.content, elementType = _a.elementType, children = _a.children, _b = _a.attrs, attrs = _b === void 0 ? {} : _b, _c = _a.style, style = _c === void 0 ? {} : _c, _d = _a.referencedStyles, referencedStyles = _d === void 0 ? {} : _d, dependency = _a.dependency;
495
+ dependencies = structure.dependencies;
100
496
  if (!(dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) === 'local')) return [3 /*break*/, 2];
101
- return [4 /*yield*/, generateComponentContent(node, propDefinitions, stateDefinitions, subComponentOptions, structure)];
497
+ return [4 /*yield*/, generateComponentContent(node, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
102
498
  case 1:
103
- compTag = _e.sent();
499
+ compTag = _f.sent();
500
+ if ('tagName' in compTag) {
501
+ compTag.children.unshift(teleport_plugin_common_1.HASTBuilders.createComment("".concat(node.content.semanticType, " component")));
502
+ }
104
503
  return [2 /*return*/, compTag];
105
504
  case 2:
505
+ if (dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) !== 'local') {
506
+ dependencies[dependency.path] = dependency;
507
+ }
508
+ elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(elementType);
106
509
  if (!children) return [3 /*break*/, 6];
107
510
  _i = 0, children_1 = children;
108
- _e.label = 3;
511
+ _f.label = 3;
109
512
  case 3:
110
513
  if (!(_i < children_1.length)) return [3 /*break*/, 6];
111
514
  child = children_1[_i];
112
- return [4 /*yield*/, (0, exports.generateHtmlSynatx)(child, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure)];
515
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
113
516
  case 4:
114
- childTag = _e.sent();
115
- if (!childTag) {
116
- return [2 /*return*/];
117
- }
517
+ childTag = _f.sent();
118
518
  if (typeof childTag === 'string') {
119
519
  teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
120
520
  }
121
521
  else {
122
522
  teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
123
523
  }
124
- _e.label = 5;
524
+ _f.label = 5;
125
525
  case 5:
126
526
  _i++;
127
527
  return [3 /*break*/, 3];
@@ -130,39 +530,48 @@ var generatElementNode = function (node, templatesLookUp, propDefinitions, state
130
530
  Object.keys(referencedStyles).forEach(function (styleRef) {
131
531
  var refStyle = referencedStyles[styleRef];
132
532
  if (refStyle.content.mapType === 'inlined') {
133
- handleStyles(node, refStyle.content.styles, propDefinitions, stateDefinitions);
134
- return;
533
+ handleStyles(node, refStyle.content.styles, propDefinitions, stateDefinitions, structure.options);
135
534
  }
136
535
  });
137
536
  }
138
537
  if (Object.keys(style).length > 0) {
139
- handleStyles(node, style, propDefinitions, stateDefinitions);
140
- }
141
- if (Object.keys(attrs).length > 0) {
142
- handleAttributes(elementType, elementNode, attrs, propDefinitions, stateDefinitions, structure.options.projectRouteDefinition, structure.outputOptions);
538
+ handleStyles(node, style, propDefinitions, stateDefinitions, structure.options);
143
539
  }
540
+ handleAttributes(elementType, elementNode, attrs, propDefinitions, stateDefinitions, structure.options.projectRouteDefinition, structure.outputOptions, structure.options, resolvedExpressions === null || resolvedExpressions === void 0 ? void 0 : resolvedExpressions.currentIndex);
541
+ addNodeToLookup(node.content.key, elementNode, nodesLookup);
144
542
  return [2 /*return*/, elementNode];
145
543
  }
146
544
  });
147
545
  }); };
148
- var generateComponentContent = function (node, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
149
- var externals, plugins, _a, elementType, _b, attrs, key, _c, children, dependencies, _d, chunks, options, comp, lookUpTemplates, compHasSlots, combinedProps, propsForInstance, combinedStates, statesForInstance, elementNode, compTag, cssPlugin, initialStructure, result, chunk, styleChunk;
150
- var _e;
151
- return __generator(this, function (_f) {
152
- switch (_f.label) {
546
+ var createLookupTable = function (component, nodesLookup) {
547
+ var lookup = {};
548
+ for (var _i = 0, _a = Object.keys(nodesLookup); _i < _a.length; _i++) {
549
+ var node = _a[_i];
550
+ lookup[node] = {
551
+ count: 1,
552
+ nextKey: '1',
553
+ };
554
+ }
555
+ (0, teleport_uidl_resolver_1.createNodesLookup)(component, lookup);
556
+ return lookup;
557
+ };
558
+ var generateComponentContent = function (node, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
559
+ var externals, plugins, _a, standaloneHtmlComponents, _b, elementType, _c, attrs, _d, children, dependencies, _e, chunks, options, componentName, component, componentClone, lookupTableForCurrentPage, combinedProps, combinedStates, statesForInstance, propsForInstance, _loop_1, _i, _f, propKey, componentWrapper, isExistingNode, wrapperAttrs, componentInstanceToGenerate, compTag, cssPlugin, initialStructure, result;
560
+ var _g, _h, _j;
561
+ return __generator(this, function (_k) {
562
+ switch (_k.label) {
153
563
  case 0:
154
- externals = subComponentOptions.externals, plugins = subComponentOptions.plugins;
155
- _a = node.content, elementType = _a.elementType, _b = _a.attrs, attrs = _b === void 0 ? {} : _b, key = _a.key, _c = _a.children, children = _c === void 0 ? [] : _c;
156
- dependencies = structure.dependencies, _d = structure.chunks, chunks = _d === void 0 ? [] : _d, options = structure.options;
157
- comp = teleport_shared_1.UIDLUtils.cloneObject(externals[elementType] || {});
158
- lookUpTemplates = {};
159
- compHasSlots = false;
160
- if (!comp || !(comp === null || comp === void 0 ? void 0 : comp.node)) {
161
- throw new teleport_types_1.HTMLComponentGeneratorError("".concat(elementType, " is not found from the externals. \n\n Received ").concat(JSON.stringify(Object.keys(externals), null, 2)));
564
+ externals = subComponentOptions.externals, plugins = subComponentOptions.plugins, _a = subComponentOptions.standaloneHtmlComponents, standaloneHtmlComponents = _a === void 0 ? false : _a;
565
+ _b = node.content, elementType = _b.elementType, _c = _b.attrs, attrs = _c === void 0 ? {} : _c, _d = _b.children, children = _d === void 0 ? [] : _d;
566
+ dependencies = structure.dependencies, _e = structure.chunks, chunks = _e === void 0 ? [] : _e, options = structure.options;
567
+ componentName = elementType === 'Component' ? 'AppComponent' : elementType;
568
+ component = externals[componentName];
569
+ if (component === undefined) {
570
+ throw new teleport_types_1.HTMLComponentGeneratorError("".concat(componentName, " is missing from externals object"));
162
571
  }
572
+ componentClone = teleport_shared_1.UIDLUtils.cloneObject(component);
163
573
  if (children.length) {
164
- compHasSlots = true;
165
- teleport_shared_1.UIDLUtils.traverseNodes(comp.node, function (childNode, parentNode) {
574
+ teleport_shared_1.UIDLUtils.traverseNodes(componentClone.node, function (childNode, parentNode) {
166
575
  var _a, _b;
167
576
  if (childNode.type === 'slot' && parentNode.type === 'element') {
168
577
  var nonSlotNodes = (_b = (_a = parentNode.content) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.filter(function (n) { return n.type !== 'slot'; });
@@ -172,6 +581,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
172
581
  content: {
173
582
  key: 'custom-slot',
174
583
  elementType: 'slot',
584
+ name: componentClone.name + 'slot',
175
585
  style: {
176
586
  display: {
177
587
  type: 'static',
@@ -190,46 +600,133 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
190
600
  */
191
601
  node.content.children = [];
192
602
  }
193
- combinedProps = __assign(__assign({}, propDefinitions), ((comp === null || comp === void 0 ? void 0 : comp.propDefinitions) || {}));
194
- propsForInstance = Object.keys(combinedProps).reduce(function (acc, propKey) {
195
- var _a, _b;
196
- if (attrs[propKey]) {
197
- acc[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: ((_a = attrs[propKey]) === null || _a === void 0 ? void 0 : _a.content) || ((_b = combinedProps[propKey]) === null || _b === void 0 ? void 0 : _b.defaultValue) });
198
- }
199
- else {
200
- acc[propKey] = combinedProps[propKey];
201
- }
603
+ lookupTableForCurrentPage = createLookupTable(componentClone, nodesLookup);
604
+ (0, teleport_uidl_resolver_1.generateUniqueKeys)(componentClone, lookupTableForCurrentPage);
605
+ combinedProps = __assign(__assign({}, Object.keys(propDefinitions).reduce(function (acc, propKey) {
606
+ acc[propKey] = propDefinitions[propKey];
202
607
  return acc;
203
- }, {});
204
- combinedStates = __assign(__assign({}, stateDefinitions), ((comp === null || comp === void 0 ? void 0 : comp.stateDefinitions) || {}));
608
+ }, {})), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.propDefinitions) || {}));
609
+ combinedStates = __assign(__assign({}, stateDefinitions), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.stateDefinitions) || {}));
205
610
  statesForInstance = Object.keys(combinedStates).reduce(function (acc, propKey) {
206
611
  var _a, _b;
207
- if (attrs[propKey]) {
208
- acc[propKey] = __assign(__assign({}, combinedStates[propKey]), { defaultValue: ((_a = attrs[propKey]) === null || _a === void 0 ? void 0 : _a.content) || ((_b = combinedStates[propKey]) === null || _b === void 0 ? void 0 : _b.defaultValue) });
612
+ var attr = attrs[propKey];
613
+ if ((attr === null || attr === void 0 ? void 0 : attr.type) === 'object') {
614
+ throw new Error("Object attributes are not supported in html exports");
615
+ }
616
+ if (attr) {
617
+ acc[propKey] = __assign(__assign({}, combinedStates[propKey]), { defaultValue: (_a = attr === null || attr === void 0 ? void 0 : attr.content) !== null && _a !== void 0 ? _a : (_b = combinedStates[propKey]) === null || _b === void 0 ? void 0 : _b.defaultValue });
209
618
  }
210
619
  else {
211
620
  acc[propKey] = combinedStates[propKey];
212
621
  }
213
622
  return acc;
214
623
  }, {});
215
- elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(teleport_shared_1.StringUtils.camelCaseToDashCase(elementType));
216
- lookUpTemplates[key] = elementNode;
217
- return [4 /*yield*/, (0, exports.generateHtmlSynatx)(__assign(__assign({}, comp.node), { content: __assign(__assign({}, comp.node.content), { style: __assign(__assign({}, (((_e = comp.node.content) === null || _e === void 0 ? void 0 : _e.style) || {})), { display: {
218
- type: 'static',
219
- content: 'contents',
220
- } }) }) }), lookUpTemplates, propsForInstance, statesForInstance, subComponentOptions, structure)];
624
+ propsForInstance = {};
625
+ _loop_1 = function (propKey) {
626
+ var attribute = attrs[propKey];
627
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'element') {
628
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: attrs[propKey] });
629
+ }
630
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'dynamic') {
631
+ // When we are using a component instance in a component and the attribute
632
+ // that is passed to the component is of dynamic reference.
633
+ // If means, the component is redirecting the prop that is received to the prop of the component that it is consuming.
634
+ // In this case, we need to pass the value of the prop that is received to the prop of the component that it is consuming.
635
+ // And similary we do the same for the states.
636
+ switch (attribute.content.referenceType) {
637
+ case 'prop':
638
+ propsForInstance[propKey] = combinedProps[attribute.content.id];
639
+ break;
640
+ case 'state':
641
+ propsForInstance[propKey] = combinedStates[attribute.content.id];
642
+ break;
643
+ case 'expr':
644
+ // Ignore expr type attributes in html comp instances for the time being.
645
+ break;
646
+ default:
647
+ throw new Error("ReferenceType ".concat(attribute.content.referenceType, " is not supported in HTML Export."));
648
+ }
649
+ }
650
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'object') {
651
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: (attribute === null || attribute === void 0 ? void 0 : attribute.content) || ((_g = combinedProps[propKey]) === null || _g === void 0 ? void 0 : _g.defaultValue) });
652
+ }
653
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'expr') {
654
+ var _l = attribute.content.split('?.'), ctxId_1 = _l[0], refPath = _l.slice(1);
655
+ var propKeyFromAttr = Object.keys(combinedProps).find(function (key) { return key === ctxId_1; });
656
+ var resolvedValue = combinedProps[propKeyFromAttr];
657
+ var hasRefPath = refPath.length > 0;
658
+ // Build the path using repeater index when available
659
+ var fullRefPath = typeof (resolvedExpressions === null || resolvedExpressions === void 0 ? void 0 : resolvedExpressions.currentIndex) === 'number' && hasRefPath
660
+ ? __spreadArray([resolvedExpressions.currentIndex.toString()], refPath, true) : refPath;
661
+ if (Array.isArray(resolvedValue)) {
662
+ // If the resolved value itself is an array definition, pass it through
663
+ propsForInstance[propKey] = resolvedValue;
664
+ }
665
+ else {
666
+ var defaultVal = resolvedValue === null || resolvedValue === void 0 ? void 0 : resolvedValue.defaultValue;
667
+ var extracted = hasRefPath && defaultVal !== undefined
668
+ ? extractDefaultValueFromRefPath(defaultVal, fullRefPath)
669
+ : defaultVal !== null && defaultVal !== void 0 ? defaultVal : null;
670
+ propsForInstance[propKey] = __assign(__assign({}, resolvedValue), { defaultValue: extracted });
671
+ }
672
+ }
673
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'dynamic' &&
674
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'element' &&
675
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'object' &&
676
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'expr') {
677
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: (_h = attribute === null || attribute === void 0 ? void 0 : attribute.content) !== null && _h !== void 0 ? _h : (_j = combinedProps[propKey]) === null || _j === void 0 ? void 0 : _j.defaultValue });
678
+ }
679
+ if (attribute === undefined) {
680
+ var propFromCurrentComponent = combinedProps[propKey];
681
+ propsForInstance[propKey] = propFromCurrentComponent;
682
+ }
683
+ };
684
+ // this is where we check if the component we are conusming is actually passing any props to the instance.
685
+ // We check if we are passing any props and pick the value from the atrrs, if not we pick the value from the propDefinitions of
686
+ // the component instance that we are using here.
687
+ for (_i = 0, _f = Object.keys(combinedProps); _i < _f.length; _i++) {
688
+ propKey = _f[_i];
689
+ _loop_1(propKey);
690
+ }
691
+ componentWrapper = teleport_shared_1.StringUtils.camelCaseToDashCase("".concat(componentName, "-wrapper"));
692
+ isExistingNode = nodesLookup[componentWrapper];
693
+ if (isExistingNode !== undefined) {
694
+ componentWrapper = "".concat(componentWrapper, "-").concat(teleport_shared_1.StringUtils.generateRandomString());
695
+ }
696
+ wrapperAttrs = {};
697
+ Object.keys(attrs).forEach(function (attrKey) {
698
+ if (attrKey.startsWith('dataNode') || attrKey.startsWith('data-node')) {
699
+ wrapperAttrs[attrKey] = attrs[attrKey];
700
+ }
701
+ });
702
+ componentInstanceToGenerate = {
703
+ type: 'element',
704
+ content: {
705
+ elementType: componentWrapper,
706
+ key: componentWrapper,
707
+ children: [componentClone.node],
708
+ attrs: wrapperAttrs,
709
+ style: {
710
+ display: {
711
+ type: 'static',
712
+ content: 'contents',
713
+ },
714
+ },
715
+ },
716
+ };
717
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(componentInstanceToGenerate, component.name, nodesLookup, propsForInstance, statesForInstance, subComponentOptions, structure, resolvedExpressions)];
221
718
  case 1:
222
- compTag = (_f.sent());
719
+ compTag = _k.sent();
223
720
  cssPlugin = (0, teleport_plugin_css_1.createCSSPlugin)({
224
721
  templateStyle: 'html',
225
722
  templateChunkName: constants_1.DEFAULT_COMPONENT_CHUNK_NAME,
226
- declareDependency: 'import',
227
- forceScoping: true,
228
- chunkName: comp.name,
723
+ declareDependency: standaloneHtmlComponents ? 'none' : 'import',
724
+ chunkName: componentClone.name,
229
725
  staticPropReferences: true,
726
+ standaloneHtmlComponents: standaloneHtmlComponents,
230
727
  });
231
728
  initialStructure = {
232
- uidl: __assign(__assign({}, comp), { propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
729
+ uidl: __assign(__assign({}, componentClone), { node: componentInstanceToGenerate, propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
233
730
  chunks: [
234
731
  {
235
732
  type: teleport_types_1.ChunkType.HAST,
@@ -238,7 +735,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
238
735
  linkAfter: [],
239
736
  content: compTag,
240
737
  meta: {
241
- nodesLookup: lookUpTemplates,
738
+ nodesLookup: nodesLookup,
242
739
  },
243
740
  },
244
741
  ],
@@ -257,132 +754,271 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
257
754
  });
258
755
  }); }, Promise.resolve(initialStructure))];
259
756
  case 2:
260
- result = _f.sent();
261
- if (compHasSlots) {
262
- result.chunks.forEach(function (chunk) {
263
- if (chunk.fileType === teleport_types_1.FileType.CSS) {
264
- chunks.push(chunk);
757
+ result = _k.sent();
758
+ result.chunks.forEach(function (chunk) {
759
+ if (chunk.fileType === teleport_types_1.FileType.CSS) {
760
+ chunks.push(chunk);
761
+ }
762
+ });
763
+ addNodeToLookup(node.content.key, compTag, nodesLookup);
764
+ return [2 /*return*/, compTag];
765
+ }
766
+ });
767
+ }); };
768
+ var generateDynamicNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
769
+ var translation, localeTag, commentNode, globalTag, commentNode, usedReferenceValue, extracted, elementNode, elementClone, iterElementTag, generatedElementTag, spanTagWrapper, commentNode, spanTag;
770
+ var _a;
771
+ return __generator(this, function (_b) {
772
+ switch (_b.label) {
773
+ case 0:
774
+ if (node.content.referenceType === 'locale') {
775
+ translation = getTranslation(node.content.id, structure.options);
776
+ if (translation) {
777
+ if (translation.type === 'static') {
778
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(String(translation.content))];
265
779
  }
266
- });
267
- }
268
- else {
269
- chunk = chunks.find(function (item) { return item.name === comp.name; });
270
- if (!chunk) {
271
- styleChunk = result.chunks.find(function (item) { return item.fileType === teleport_types_1.FileType.CSS; });
272
- if (!styleChunk) {
273
- return [2 /*return*/];
780
+ if (translation.type === 'element') {
781
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(translation, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
274
782
  }
275
- chunks.push(styleChunk);
276
783
  }
784
+ localeTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
785
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for locale ".concat(node.content.id));
786
+ teleport_plugin_common_1.HASTUtils.addChildNode(localeTag, commentNode);
787
+ return [2 /*return*/, localeTag];
277
788
  }
278
- return [2 /*return*/, compTag];
789
+ if (node.content.referenceType === 'global' ||
790
+ node.content.referenceType === 'globalState') {
791
+ globalTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
792
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Global reference: ".concat(node.content.id));
793
+ teleport_plugin_common_1.HASTUtils.addChildNode(globalTag, commentNode);
794
+ return [2 /*return*/, globalTag];
795
+ }
796
+ usedReferenceValue = getValueFromReference(node.content.id, node.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
797
+ if ((usedReferenceValue.type === 'object' || usedReferenceValue.type === 'array') &&
798
+ usedReferenceValue.defaultValue) {
799
+ extracted = extractDefaultValueFromRefPath(usedReferenceValue.defaultValue, node.content.refPath);
800
+ if (extracted === undefined || extracted === null) {
801
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode('')];
802
+ }
803
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(String(extracted))];
804
+ }
805
+ if (!(usedReferenceValue.type === 'element')) return [3 /*break*/, 5];
806
+ elementNode = usedReferenceValue.defaultValue;
807
+ if (!elementNode) return [3 /*break*/, 4];
808
+ if (!(resolvedExpressions && typeof resolvedExpressions.currentIndex === 'number')) return [3 /*break*/, 2];
809
+ elementClone = teleport_shared_1.UIDLUtils.cloneObject(elementNode);
810
+ if ((_a = elementClone === null || elementClone === void 0 ? void 0 : elementClone.content) === null || _a === void 0 ? void 0 : _a.key) {
811
+ elementClone.content.key = "".concat(elementClone.content.key, "-").concat(resolvedExpressions.currentIndex);
812
+ }
813
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(elementClone, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
814
+ case 1:
815
+ iterElementTag = _b.sent();
816
+ return [2 /*return*/, iterElementTag];
817
+ case 2:
818
+ if (elementNode.content.key in nodesLookup) {
819
+ return [2 /*return*/, nodesLookup[elementNode.content.key]];
820
+ }
821
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(elementNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
822
+ case 3:
823
+ generatedElementTag = _b.sent();
824
+ return [2 /*return*/, generatedElementTag];
825
+ case 4:
826
+ spanTagWrapper = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
827
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for slot ".concat(node.content.id));
828
+ teleport_plugin_common_1.HASTUtils.addChildNode(spanTagWrapper, commentNode);
829
+ return [2 /*return*/, spanTagWrapper];
830
+ case 5:
831
+ spanTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
832
+ teleport_plugin_common_1.HASTUtils.addTextNode(spanTag, String(usedReferenceValue.defaultValue));
833
+ return [2 /*return*/, spanTag];
279
834
  }
280
835
  });
281
836
  }); };
282
- var generateDynamicNode = function (node, _, propDefinitions, stateDefinitions) {
283
- var spanTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
284
- var usedReferenceValue = node.content.referenceType === 'prop'
285
- ? getValueFromReference(node.content.id, propDefinitions)
286
- : getValueFromReference(node.content.id, stateDefinitions);
287
- teleport_plugin_common_1.HASTUtils.addTextNode(spanTag, String(usedReferenceValue));
288
- return spanTag;
289
- };
290
- var handleStyles = function (node, styles, propDefinitions, stateDefinitions) {
837
+ var handleStyles = function (node, styles, propDefinitions, stateDefinitions, options) {
291
838
  Object.keys(styles).forEach(function (styleKey) {
292
- var _a;
839
+ var _a, _b, _c, _d, _e;
293
840
  var style = styles[styleKey];
294
841
  if (style.type === 'dynamic' && ((_a = style.content) === null || _a === void 0 ? void 0 : _a.referenceType) !== 'token') {
295
- if (style.content.referenceType === 'prop') {
296
- style = getValueFromReference(style.content.id, propDefinitions);
842
+ if (((_b = style.content) === null || _b === void 0 ? void 0 : _b.referenceType) === 'locale') {
843
+ var translation = getTranslation(style.content.id, options);
844
+ var resolvedText = translation
845
+ ? resolveTranslationText(translation)
846
+ : "[locale: ".concat(style.content.id, "]");
847
+ node.content.style[styleKey] = (0, teleport_uidl_builders_1.staticNode)(resolvedText);
848
+ return;
849
+ }
850
+ if (((_c = style.content) === null || _c === void 0 ? void 0 : _c.referenceType) === 'global' ||
851
+ ((_d = style.content) === null || _d === void 0 ? void 0 : _d.referenceType) === 'globalState') {
852
+ node.content.style[styleKey] = (0, teleport_uidl_builders_1.staticNode)("[global: ".concat(style.content.id, "]"));
853
+ return;
297
854
  }
298
- else if (style.content.referenceType === 'state') {
299
- style = getValueFromReference(style.content.id, stateDefinitions);
855
+ var referencedValue = getValueFromReference(style.content.id, style.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
856
+ if (referencedValue.type === 'string' || referencedValue.type === 'number') {
857
+ style = String(extractDefaultValueFromRefPath(referencedValue.defaultValue, (_e = style === null || style === void 0 ? void 0 : style.content) === null || _e === void 0 ? void 0 : _e.refPath));
300
858
  }
301
859
  node.content.style[styleKey] = typeof style === 'string' ? (0, teleport_uidl_builders_1.staticNode)(style) : style;
302
860
  }
303
861
  });
304
862
  };
305
- var handleAttributes = function (elementType, htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions, outputOptions) {
306
- Object.keys(attrs).forEach(function (attrKey) {
863
+ var handleAttributes = function (elementType, htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions, outputOptions, options, currentIndex) {
864
+ var _loop_2 = function (attrKey) {
307
865
  var attrValue = attrs[attrKey];
308
- if (attrKey === 'href' &&
309
- attrValue.type === 'static' &&
310
- typeof attrValue.content === 'string' &&
311
- attrValue.content.startsWith('/')) {
312
- var targetLink = void 0;
313
- var targetRoute = ((routeDefinitions === null || routeDefinitions === void 0 ? void 0 : routeDefinitions.values) || []).find(function (route) { return route.pageOptions.navLink === attrValue.content; });
314
- if (targetRoute) {
315
- targetLink = targetRoute.pageOptions.navLink;
866
+ var type = attrValue.type, content = attrValue.content;
867
+ switch (type) {
868
+ case 'static': {
869
+ if (attrKey === 'href' && typeof content === 'string' && content.startsWith('/')) {
870
+ var targetLink = void 0;
871
+ var targetRoute = ((routeDefinitions === null || routeDefinitions === void 0 ? void 0 : routeDefinitions.values) || []).find(function (route) { return route.pageOptions.navLink === content; });
872
+ if (targetRoute) {
873
+ targetLink = targetRoute.pageOptions.navLink;
874
+ }
875
+ if (!targetRoute && content === '/home') {
876
+ targetLink = '/';
877
+ }
878
+ if (!targetLink && !targetRoute) {
879
+ targetLink = content;
880
+ }
881
+ var currentPageRoute = path_1.join.apply(void 0, __spreadArray(__spreadArray([], ((outputOptions === null || outputOptions === void 0 ? void 0 : outputOptions.folderPath) || []), false), ['./'], false));
882
+ var localPrefix = (0, path_1.relative)("/".concat(currentPageRoute), "/".concat(targetLink === '/' ? 'index' : targetLink));
883
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, "".concat(localPrefix, ".html"));
884
+ break;
885
+ }
886
+ if (typeof content === 'boolean') {
887
+ htmlNode.properties[attrKey] = content === true ? 'true' : 'false';
888
+ }
889
+ else if (typeof content === 'string' || typeof attrValue.content === 'number') {
890
+ var value = teleport_shared_1.StringUtils.encode(String(attrValue.content));
891
+ /*
892
+ elementType of image is always mapped to img.
893
+ For reference, check `html-mapping` file.
894
+ */
895
+ if ((elementType === 'img' || elementType === 'video') &&
896
+ attrKey === 'src' &&
897
+ !isValidURL(value)) {
898
+ /*
899
+ By default we just prefix all the asset paths with just the
900
+ assetPrefix that is configured in the project. But for `html` generators
901
+ we need to prefix that with the current file location.
902
+
903
+ Because, all the other frameworks have a build setup. which serves all the
904
+ assets from the `public` folder. But in the case of `html` here is how it works
905
+
906
+ We load a file from `index.html` the request for the image goes from
907
+ '...url.../public/...image...'
908
+ If it's a nested url, then the request goes from
909
+ '...url/nested/public/...image..'
910
+
911
+ But the nested folder is available only on the root. With this
912
+ The url changes prefixes to
913
+
914
+ ../public/playground_assets/..image.. etc depending on the dept the file is in.
915
+ */
916
+ value = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), value);
917
+ }
918
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, value);
919
+ }
920
+ break;
316
921
  }
317
- if (!targetRoute && attrValue.content === '/home') {
318
- targetLink = '/';
922
+ case 'dynamic': {
923
+ if (content.referenceType === 'locale') {
924
+ var translation = getTranslation(content.id, options);
925
+ var resolvedText = translation
926
+ ? resolveTranslationText(translation)
927
+ : "[locale: ".concat(content.id, "]");
928
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, resolvedText);
929
+ break;
930
+ }
931
+ if (content.referenceType === 'global' ||
932
+ content.referenceType === 'globalState') {
933
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, "[global: ".concat(content.id, "]"));
934
+ break;
935
+ }
936
+ var value = getValueFromReference(content.id, content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
937
+ // A `func` prop has no meaningful static HTML representation; skip
938
+ // rather than emitting the stringified function body as an attribute.
939
+ if (value.type === 'func') {
940
+ break;
941
+ }
942
+ var extracted = extractDefaultValueFromRefPath(value.defaultValue, content.refPath);
943
+ var extractedValue = String(extracted);
944
+ if ((elementType === 'img' || elementType === 'video') &&
945
+ attrKey === 'src' &&
946
+ !extractedValue.startsWith('http')) {
947
+ var path = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), extractedValue);
948
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, path);
949
+ break;
950
+ }
951
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, extractedValue);
952
+ break;
319
953
  }
320
- if (!targetLink && !targetRoute) {
321
- targetLink = attrValue.content;
954
+ case 'raw': {
955
+ var rawAttr = attrValue;
956
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, rawAttr.fallback || rawAttr.content);
957
+ break;
322
958
  }
323
- var currentPageRoute = path_1.join.apply(void 0, __spreadArray(__spreadArray([], ((outputOptions === null || outputOptions === void 0 ? void 0 : outputOptions.folderPath) || []), false), ['./'], false));
324
- var localPrefix = (0, path_1.relative)("/".concat(currentPageRoute), "/".concat(targetLink === '/' ? 'index' : targetLink));
325
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, "".concat(localPrefix, ".html"));
326
- return;
327
- }
328
- if (attrValue.type === 'dynamic') {
329
- var value = attrValue.content.referenceType === 'prop'
330
- ? getValueFromReference(attrValue.content.id, propDefinitions)
331
- : getValueFromReference(attrValue.content.id, stateDefinitions);
332
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(value));
333
- return;
334
- }
335
- if (attrValue.type === 'raw') {
336
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, attrValue.content);
337
- return;
338
- }
339
- if (typeof attrValue.content === 'boolean') {
340
- teleport_plugin_common_1.HASTUtils.addBooleanAttributeToNode(htmlNode, attrKey);
341
- return;
342
- }
343
- else if (typeof attrValue.content === 'string' || typeof attrValue.content === 'number') {
344
- var value = teleport_shared_1.StringUtils.encode(String(attrValue.content));
345
- /*
346
- elementType of image is always mapped to img.
347
- For reference, check `html-mapping` file.
348
- */
349
- if (elementType === 'img' && attrKey === 'src') {
350
- /*
351
- By default we just prefix all the asset paths with just the
352
- assetPrefix that is configured in the project. But for `html` generators
353
- we need to prefix that with the current file location.
354
-
355
- Because, all the other frameworks have a build setup. which serves all the
356
- assets from the `public` folder. But in the case of `html` here is how it works
357
-
358
- We load a file from `index.html` the request for the image goes from
359
- '...url.../public/...image...'
360
- If it's a nested url, then the request goes from
361
- '...url/nested/public/...image..'
362
-
363
- But the nested folder is available only on the root. With this
364
- The url changes prefixes to
365
-
366
- ../public/playground_assets/..image.. etc depending on the dept the file is in.
367
- */
368
- value = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), value);
959
+ case 'expr': {
960
+ var fullPath = content.split('?.');
961
+ var prop = propDefinitions[(fullPath === null || fullPath === void 0 ? void 0 : fullPath[0]) || ''];
962
+ if (!prop) {
963
+ break;
964
+ }
965
+ var path = typeof currentIndex === 'number'
966
+ ? __spreadArray([currentIndex.toString()], fullPath.slice(1), true) : fullPath.slice(1);
967
+ var value = extractDefaultValueFromRefPath(prop.defaultValue, path);
968
+ if (!value) {
969
+ break;
970
+ }
971
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(value));
972
+ break;
973
+ }
974
+ case 'element':
975
+ case 'import':
976
+ case 'object':
977
+ break;
978
+ default: {
979
+ throw new teleport_types_1.HTMLComponentGeneratorError("Received ".concat(JSON.stringify(attrValue, null, 2), " \n in handleAttributes for html"));
369
980
  }
370
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, value);
371
- return;
372
981
  }
373
- });
982
+ };
983
+ for (var _i = 0, _a = Object.keys(attrs); _i < _a.length; _i++) {
984
+ var attrKey = _a[_i];
985
+ _loop_2(attrKey);
986
+ }
374
987
  };
375
988
  var getValueFromReference = function (key, definitions) {
376
- var usedReferenceValue = definitions[key.includes('.') ? key.split('.')[0] : key];
989
+ var usedReferenceValue = definitions[key.includes('?.') ? key.split('?.')[0] : key];
377
990
  if (!usedReferenceValue) {
378
991
  throw new teleport_types_1.HTMLComponentGeneratorError("Definition for ".concat(key, " is missing from ").concat(JSON.stringify(definitions, null, 2)));
379
992
  }
380
- if (!usedReferenceValue.hasOwnProperty('defaultValue')) {
993
+ if ((usedReferenceValue === null || usedReferenceValue === void 0 ? void 0 : usedReferenceValue.type) &&
994
+ ['string', 'number', 'object', 'element', 'array', 'boolean', 'link', 'func'].includes(usedReferenceValue.type) === false) {
995
+ throw new teleport_types_1.HTMLComponentGeneratorError("Attribute is using dynamic value, but received of type ".concat(JSON.stringify(usedReferenceValue, null, 2)));
996
+ }
997
+ if (usedReferenceValue.type !== 'element' &&
998
+ usedReferenceValue.hasOwnProperty('defaultValue') === false) {
381
999
  throw new teleport_types_1.HTMLComponentGeneratorError("Default value is missing from dynamic reference - ".concat(JSON.stringify(usedReferenceValue, null, 2)));
382
1000
  }
383
- if (!['string', 'number', 'object'].includes(usedReferenceValue === null || usedReferenceValue === void 0 ? void 0 : usedReferenceValue.type)) {
384
- throw new teleport_types_1.HTMLComponentGeneratorError("Attribute is using dynamic value, but received of type ".concat(JSON.stringify(usedReferenceValue, null, 2)));
1001
+ return usedReferenceValue;
1002
+ };
1003
+ var extractDefaultValueFromRefPath = function (propDefaultValue, refPath) {
1004
+ if (!refPath || refPath.length === 0) {
1005
+ return propDefaultValue;
1006
+ }
1007
+ // Directly handle array indexing for the first segment when applicable
1008
+ if (Array.isArray(propDefaultValue)) {
1009
+ var first = refPath[0], rest = refPath.slice(1);
1010
+ var idx = Number(first);
1011
+ if (!Number.isNaN(idx) && idx >= 0 && idx < propDefaultValue.length) {
1012
+ var nextVal = propDefaultValue[idx];
1013
+ if (rest.length === 0) {
1014
+ return nextVal;
1015
+ }
1016
+ return extractDefaultValueFromRefPath(nextVal, rest);
1017
+ }
1018
+ }
1019
+ if (typeof propDefaultValue !== 'object') {
1020
+ return propDefaultValue;
385
1021
  }
386
- return String(usedReferenceValue.defaultValue);
1022
+ return teleport_shared_1.GenericUtils.getValueFromPath(refPath.join('.'), propDefaultValue);
387
1023
  };
388
1024
  //# sourceMappingURL=node-handlers.js.map