@teleporthq/teleport-plugin-html-base-component 0.42.0-alpha.0 → 0.42.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.
@@ -56,72 +56,350 @@ 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 isValidURL = function (url) {
69
+ try {
70
+ /* tslint:disable:no-unused-expression */
71
+ new URL(url);
72
+ return true;
73
+ }
74
+ catch (error) {
75
+ return false;
76
+ }
77
+ };
78
+ var addNodeToLookup = function (key, tag, nodesLoookup) {
79
+ // In html code-generation we combine the nodes of the component that is being consumed with the current component.
80
+ // As html can't load the component at runtime like react or any other frameworks. So, we merge the component as a standalone
81
+ // component in the current component.
82
+ var currentLookup = nodesLoookup[key];
83
+ if (currentLookup) {
84
+ if (Array.isArray(currentLookup)) {
85
+ Array.isArray(tag) ? currentLookup.push.apply(currentLookup, tag) : currentLookup.push(tag);
86
+ }
87
+ else {
88
+ nodesLoookup[key] = Array.isArray(tag) ? __spreadArray([currentLookup], tag, true) : [currentLookup, tag];
89
+ }
90
+ return;
91
+ }
92
+ nodesLoookup[key] = tag;
93
+ };
94
+ var generateHtmlSyntax = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
95
+ var _a, elementNode, dynamicNode, conditionalNodeComment, _b, staticValue, reference, _c, conditions, matchingCriteria, _d, referenceType, id, _e, refPath, usedProp, defaultValue, _i, refPath_1, path, dynamicConditions, matchCondition, conditionString, isConditionPassing, content, uidlDynamicRef, generatedNode, isProp, uidlDynamicRef, generatedNode, successNode;
96
+ var _f, _g;
97
+ return __generator(this, function (_h) {
98
+ switch (_h.label) {
99
+ case 0:
100
+ _a = node.type;
101
+ switch (_a) {
102
+ case 'inject': return [3 /*break*/, 1];
103
+ case 'raw': return [3 /*break*/, 1];
104
+ case 'static': return [3 /*break*/, 2];
105
+ case 'slot': return [3 /*break*/, 3];
106
+ case 'element': return [3 /*break*/, 4];
107
+ case 'dynamic': return [3 /*break*/, 6];
108
+ case 'conditional': return [3 /*break*/, 8];
109
+ case 'expr': return [3 /*break*/, 9];
110
+ case 'cms-list-repeater': return [3 /*break*/, 14];
111
+ case 'cms-item': return [3 /*break*/, 15];
112
+ case 'cms-list': return [3 /*break*/, 15];
113
+ case 'data-source-item': return [3 /*break*/, 15];
114
+ case 'data-source-list': return [3 /*break*/, 15];
115
+ }
116
+ return [3 /*break*/, 16];
117
+ case 1: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(node.content.toString())];
118
+ case 2: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(teleport_shared_1.StringUtils.encode(node.content.toString()))];
119
+ case 3: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode(node.type)];
120
+ case 4: return [4 /*yield*/, generateElementNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
121
+ case 5:
122
+ elementNode = _h.sent();
123
+ return [2 /*return*/, elementNode];
124
+ case 6: return [4 /*yield*/, generateDynamicNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
125
+ case 7:
126
+ dynamicNode = _h.sent();
127
+ return [2 /*return*/, dynamicNode];
128
+ case 8:
129
+ conditionalNodeComment = teleport_plugin_common_1.HASTBuilders.createTextNode('');
130
+ _b = node.content, staticValue = _b.value, reference = _b.reference, _c = _b.condition, conditions = _c.conditions, matchingCriteria = _c.matchingCriteria;
131
+ if (reference.type !== 'dynamic') {
132
+ return [2 /*return*/, conditionalNodeComment];
133
+ }
134
+ _d = reference.content, referenceType = _d.referenceType, id = _d.id, _e = _d.refPath, refPath = _e === void 0 ? [] : _e;
135
+ switch (referenceType) {
136
+ case 'prop': {
137
+ usedProp = propDefinitions[id];
138
+ if (usedProp === undefined || usedProp.defaultValue === undefined) {
139
+ return [2 /*return*/, conditionalNodeComment];
140
+ }
141
+ defaultValue = usedProp.defaultValue;
142
+ for (_i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) {
143
+ path = refPath_1[_i];
144
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
145
+ }
146
+ // If defaultValue is undefined or null after path traversal, use original default
147
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : usedProp.defaultValue;
148
+ dynamicConditions = createConditionalStatement(staticValue !== undefined ? [{ operand: staticValue, operation: '===' }] : conditions, defaultValue);
149
+ matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
150
+ conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
151
+ try {
152
+ isConditionPassing = new Function("return ".concat(conditionString))();
153
+ if (isConditionPassing) {
154
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(node.content.node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
155
+ }
156
+ }
157
+ catch (error) {
158
+ return [2 /*return*/, conditionalNodeComment];
159
+ }
160
+ return [2 /*return*/, conditionalNodeComment];
161
+ }
162
+ case 'state':
163
+ default:
164
+ return [2 /*return*/, conditionalNodeComment];
165
+ }
166
+ _h.label = 9;
167
+ case 9:
168
+ content = node.content.split('?.');
169
+ if (!(resolvedExpressions && ((_f = resolvedExpressions.expressions) === null || _f === void 0 ? void 0 : _f[content[0] || '']))) return [3 /*break*/, 11];
170
+ uidlDynamicRef = {
171
+ type: 'dynamic',
172
+ content: {
173
+ referenceType: 'prop',
174
+ refPath: __spreadArray([resolvedExpressions.currentIndex.toString()], content.slice(1), true),
175
+ id: content[0],
176
+ },
177
+ };
178
+ return [4 /*yield*/, generateDynamicNode(uidlDynamicRef, compName, nodesLookup, resolvedExpressions.expressions, stateDefinitions, subComponentOptions, structure)];
179
+ case 10:
180
+ generatedNode = _h.sent();
181
+ return [2 /*return*/, generatedNode];
182
+ case 11:
183
+ 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*/, 13];
184
+ isProp = Boolean(propDefinitions === null || propDefinitions === void 0 ? void 0 : propDefinitions[content[0]]);
185
+ uidlDynamicRef = {
186
+ type: 'dynamic',
187
+ content: {
188
+ referenceType: isProp ? 'prop' : 'state',
189
+ refPath: content.slice(1),
190
+ id: content[0],
191
+ },
192
+ };
193
+ return [4 /*yield*/, generateDynamicNode(uidlDynamicRef, compName, nodesLookup, isProp ? propDefinitions : stateDefinitions, stateDefinitions, subComponentOptions, structure)];
194
+ case 12:
195
+ generatedNode = _h.sent();
196
+ return [2 /*return*/, generatedNode];
197
+ case 13: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createComment('Expressions are not supported in HTML')];
198
+ case 14: return [2 /*return*/, generateRepeaterNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
199
+ case 15:
200
+ successNode = (_g = node.content.nodes) === null || _g === void 0 ? void 0 : _g.success;
201
+ if (successNode) {
202
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(successNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
203
+ }
204
+ // If no success node, return empty div
205
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode('div')];
206
+ case 16: throw new teleport_types_1.HTMLComponentGeneratorError("generateHtmlSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2), " "));
83
207
  }
84
- return [2 /*return*/];
85
208
  });
86
209
  }); };
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) {
210
+ exports.generateHtmlSyntax = generateHtmlSyntax;
211
+ var createConditionalStatement = function (conditions, leftOperand) {
212
+ return conditions.map(function (condition) {
213
+ var operation = condition.operation, operand = condition.operand;
214
+ if (operand === undefined) {
215
+ return "".concat(teleport_plugin_common_1.ASTUtils.convertToUnaryOperator(operation)).concat(getValueType(operand));
216
+ }
217
+ return "".concat(getValueType(leftOperand), " ").concat(teleport_plugin_common_1.ASTUtils.convertToBinaryOperator(operation), " ").concat(getValueType(operand));
218
+ });
219
+ };
220
+ var getValueType = function (value) {
221
+ var valueType = typeof value;
222
+ switch (valueType) {
223
+ case 'string':
224
+ return "\"".concat(value, "\"");
225
+ case 'number':
226
+ return value;
227
+ case 'boolean':
228
+ return value;
229
+ case 'object':
230
+ // Handle dynamic references (local, prop, state)
231
+ if (value && typeof value === 'object' && 'type' in value) {
232
+ var dynamicValue = value;
233
+ if (dynamicValue.type === 'dynamic' && dynamicValue.content) {
234
+ var _a = dynamicValue.content, referenceType = _a.referenceType, id = _a.id, refPath = _a.refPath;
235
+ if (referenceType === 'local' && id) {
236
+ // Local reference from repeater context
237
+ if (refPath && refPath.length > 0) {
238
+ return "".concat(id, ".").concat(refPath.join('.'));
239
+ }
240
+ return id;
241
+ }
242
+ else if (referenceType === 'prop' || referenceType === 'state') {
243
+ // Prop or state reference
244
+ var key = refPath && refPath.length > 0 ? refPath.join('.') : id;
245
+ return key;
246
+ }
247
+ }
248
+ }
249
+ throw new teleport_types_1.HTMLComponentGeneratorError("Conditional node received an operand of type ".concat(valueType, " \n\n Received ").concat(JSON.stringify(value)));
250
+ default:
251
+ throw new teleport_types_1.HTMLComponentGeneratorError("Conditional node received an operand of type ".concat(valueType, " \n\n Received ").concat(JSON.stringify(value)));
252
+ }
253
+ };
254
+ var generateRepeaterNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
255
+ var nodes, contextId, sourceValue, propDef, parsedSource, elementNode, emptyChildren, _i, emptyChildren_1, child, childTag, listChildren, index, _a, listChildren_1, child, childTag;
256
+ var _b;
257
+ var _c;
258
+ return __generator(this, function (_d) {
259
+ switch (_d.label) {
92
260
  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;
261
+ nodes = node.content.nodes;
262
+ contextId = node.content.renderPropIdentifier;
263
+ sourceValue = node.content.source;
264
+ propDef = sourceValue && typeof sourceValue === 'string'
265
+ ? propDefinitions[Object.keys(propDefinitions).find(function (propKey) { return sourceValue.includes(propKey); }) || '']
266
+ : undefined;
267
+ if (!propDef || !Array.isArray(propDef.defaultValue)) {
268
+ // If no prop is found we might have a static source value
269
+ try {
270
+ parsedSource = JSON.parse(sourceValue);
271
+ propDef = {
272
+ defaultValue: parsedSource,
273
+ id: contextId,
274
+ type: 'array',
275
+ };
276
+ }
277
+ catch (_e) {
278
+ // Silent fail
279
+ }
280
+ }
281
+ // We do the check again to keep typescript happy, otherwise this could be in catch
282
+ if (!propDef || !Array.isArray(propDef.defaultValue)) {
283
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createComment('CMS Array Mapper/Repeater not supported in HTML without a prop source')];
99
284
  }
285
+ propDefinitions[contextId] = propDef;
286
+ elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode('div');
287
+ node.content.nodes.list.content.style = { display: { type: 'static', content: 'contents' } };
288
+ if (node.content.nodes.empty) {
289
+ node.content.nodes.empty.content.style = { display: { type: 'static', content: 'contents' } };
290
+ }
291
+ if (node.content.nodes.loading) {
292
+ node.content.nodes.loading.content.style = { display: { type: 'static', content: 'contents' } };
293
+ }
294
+ if (!(propDef.defaultValue.length === 0)) return [3 /*break*/, 5];
295
+ emptyChildren = (_c = nodes.empty) === null || _c === void 0 ? void 0 : _c.content.children;
296
+ if (!emptyChildren) return [3 /*break*/, 4];
297
+ _i = 0, emptyChildren_1 = emptyChildren;
298
+ _d.label = 1;
299
+ case 1:
300
+ if (!(_i < emptyChildren_1.length)) return [3 /*break*/, 4];
301
+ child = emptyChildren_1[_i];
302
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
303
+ case 2:
304
+ childTag = _d.sent();
305
+ if (typeof childTag === 'string') {
306
+ teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
307
+ }
308
+ else {
309
+ teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
310
+ }
311
+ _d.label = 3;
312
+ case 3:
313
+ _i++;
314
+ return [3 /*break*/, 1];
315
+ case 4:
316
+ if (nodes.empty) {
317
+ addNodeToLookup("".concat(node.content.nodes.empty.content.key), elementNode, nodesLookup);
318
+ }
319
+ return [2 /*return*/, elementNode];
320
+ case 5:
321
+ listChildren = nodes.list.content.children;
322
+ if (!listChildren) return [3 /*break*/, 11];
323
+ index = 0;
324
+ _d.label = 6;
325
+ case 6:
326
+ if (!(index < propDef.defaultValue.length)) return [3 /*break*/, 11];
327
+ _a = 0, listChildren_1 = listChildren;
328
+ _d.label = 7;
329
+ case 7:
330
+ if (!(_a < listChildren_1.length)) return [3 /*break*/, 10];
331
+ child = listChildren_1[_a];
332
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, { currentIndex: index, expressions: (_b = {}, _b[contextId] = propDef, _b) })];
333
+ case 8:
334
+ childTag = _d.sent();
335
+ if (typeof childTag === 'string') {
336
+ teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
337
+ }
338
+ else {
339
+ teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
340
+ }
341
+ _d.label = 9;
342
+ case 9:
343
+ _a++;
344
+ return [3 /*break*/, 7];
345
+ case 10:
346
+ index++;
347
+ return [3 /*break*/, 6];
348
+ case 11:
349
+ addNodeToLookup("".concat(node.content.nodes.list.content.key), elementNode, nodesLookup);
350
+ return [2 /*return*/, elementNode];
351
+ }
352
+ });
353
+ }); };
354
+ var generateElementNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
355
+ var successNode, _a, elementType, children, _b, attrs, _c, style, _d, referencedStyles, dependency, dependencies, compTag, elementNode, _i, children_1, child, childTag;
356
+ var _e;
357
+ return __generator(this, function (_f) {
358
+ switch (_f.label) {
359
+ case 0:
360
+ // Check if this is a wrapped data-source node
361
+ if (node.content &&
362
+ typeof node.content === 'object' &&
363
+ 'type' in node.content &&
364
+ (node.content.type === 'data-source-item' || node.content.type === 'data-source-list')) {
365
+ successNode = (_e = node.content.nodes) === null || _e === void 0 ? void 0 : _e.success;
366
+ if (successNode) {
367
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(successNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
368
+ }
369
+ // If no success node, return empty div
370
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode('div')];
371
+ }
372
+ _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;
373
+ dependencies = structure.dependencies;
100
374
  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)];
375
+ return [4 /*yield*/, generateComponentContent(node, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
102
376
  case 1:
103
- compTag = _e.sent();
377
+ compTag = _f.sent();
378
+ if ('tagName' in compTag) {
379
+ compTag.children.unshift(teleport_plugin_common_1.HASTBuilders.createComment("".concat(node.content.semanticType, " component")));
380
+ }
104
381
  return [2 /*return*/, compTag];
105
382
  case 2:
383
+ if (dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) !== 'local') {
384
+ dependencies[dependency.path] = dependency;
385
+ }
386
+ elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(elementType);
106
387
  if (!children) return [3 /*break*/, 6];
107
388
  _i = 0, children_1 = children;
108
- _e.label = 3;
389
+ _f.label = 3;
109
390
  case 3:
110
391
  if (!(_i < children_1.length)) return [3 /*break*/, 6];
111
392
  child = children_1[_i];
112
- return [4 /*yield*/, (0, exports.generateHtmlSynatx)(child, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, structure)];
393
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
113
394
  case 4:
114
- childTag = _e.sent();
115
- if (!childTag) {
116
- return [2 /*return*/];
117
- }
395
+ childTag = _f.sent();
118
396
  if (typeof childTag === 'string') {
119
397
  teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
120
398
  }
121
399
  else {
122
400
  teleport_plugin_common_1.HASTUtils.addChildNode(elementNode, childTag);
123
401
  }
124
- _e.label = 5;
402
+ _f.label = 5;
125
403
  case 5:
126
404
  _i++;
127
405
  return [3 /*break*/, 3];
@@ -131,38 +409,47 @@ var generatElementNode = function (node, templatesLookUp, propDefinitions, state
131
409
  var refStyle = referencedStyles[styleRef];
132
410
  if (refStyle.content.mapType === 'inlined') {
133
411
  handleStyles(node, refStyle.content.styles, propDefinitions, stateDefinitions);
134
- return;
135
412
  }
136
413
  });
137
414
  }
138
415
  if (Object.keys(style).length > 0) {
139
416
  handleStyles(node, style, propDefinitions, stateDefinitions);
140
417
  }
141
- if (Object.keys(attrs).length > 0) {
142
- handleAttributes(elementType, elementNode, attrs, propDefinitions, stateDefinitions, structure.options.projectRouteDefinition, structure.outputOptions);
143
- }
418
+ handleAttributes(elementType, elementNode, attrs, propDefinitions, stateDefinitions, structure.options.projectRouteDefinition, structure.outputOptions, resolvedExpressions === null || resolvedExpressions === void 0 ? void 0 : resolvedExpressions.currentIndex);
419
+ addNodeToLookup(node.content.key, elementNode, nodesLookup);
144
420
  return [2 /*return*/, elementNode];
145
421
  }
146
422
  });
147
423
  }); };
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) {
424
+ var createLookupTable = function (component, nodesLookup) {
425
+ var lookup = {};
426
+ for (var _i = 0, _a = Object.keys(nodesLookup); _i < _a.length; _i++) {
427
+ var node = _a[_i];
428
+ lookup[node] = {
429
+ count: 1,
430
+ nextKey: '1',
431
+ };
432
+ }
433
+ (0, teleport_uidl_resolver_1.createNodesLookup)(component, lookup);
434
+ return lookup;
435
+ };
436
+ var generateComponentContent = function (node, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
437
+ var externals, plugins, _a, elementType, _b, attrs, _c, children, dependencies, _d, chunks, options, componentName, component, componentClone, lookupTableForCurrentPage, combinedProps, combinedStates, statesForInstance, propsForInstance, _loop_1, _i, _e, propKey, componentWrapper, isExistingNode, componentInstanceToGenerate, compTag, cssPlugin, initialStructure, result;
438
+ var _f, _g, _h;
439
+ return __generator(this, function (_j) {
440
+ switch (_j.label) {
153
441
  case 0:
154
442
  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;
443
+ _a = node.content, elementType = _a.elementType, _b = _a.attrs, attrs = _b === void 0 ? {} : _b, _c = _a.children, children = _c === void 0 ? [] : _c;
156
444
  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)));
445
+ componentName = elementType === 'Component' ? 'AppComponent' : elementType;
446
+ component = externals[componentName];
447
+ if (component === undefined) {
448
+ throw new teleport_types_1.HTMLComponentGeneratorError("".concat(componentName, " is missing from externals object"));
162
449
  }
450
+ componentClone = teleport_shared_1.UIDLUtils.cloneObject(component);
163
451
  if (children.length) {
164
- compHasSlots = true;
165
- teleport_shared_1.UIDLUtils.traverseNodes(comp.node, function (childNode, parentNode) {
452
+ teleport_shared_1.UIDLUtils.traverseNodes(componentClone.node, function (childNode, parentNode) {
166
453
  var _a, _b;
167
454
  if (childNode.type === 'slot' && parentNode.type === 'element') {
168
455
  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 +459,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
172
459
  content: {
173
460
  key: 'custom-slot',
174
461
  elementType: 'slot',
462
+ name: componentClone.name + 'slot',
175
463
  style: {
176
464
  display: {
177
465
  type: 'static',
@@ -190,46 +478,125 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
190
478
  */
191
479
  node.content.children = [];
192
480
  }
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
- }
481
+ lookupTableForCurrentPage = createLookupTable(componentClone, nodesLookup);
482
+ (0, teleport_uidl_resolver_1.generateUniqueKeys)(componentClone, lookupTableForCurrentPage);
483
+ combinedProps = __assign(__assign({}, Object.keys(propDefinitions).reduce(function (acc, propKey) {
484
+ acc[propKey] = propDefinitions[propKey];
202
485
  return acc;
203
- }, {});
204
- combinedStates = __assign(__assign({}, stateDefinitions), ((comp === null || comp === void 0 ? void 0 : comp.stateDefinitions) || {}));
486
+ }, {})), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.propDefinitions) || {}));
487
+ combinedStates = __assign(__assign({}, stateDefinitions), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.stateDefinitions) || {}));
205
488
  statesForInstance = Object.keys(combinedStates).reduce(function (acc, propKey) {
206
489
  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) });
490
+ var attr = attrs[propKey];
491
+ if ((attr === null || attr === void 0 ? void 0 : attr.type) === 'object') {
492
+ throw new Error("Object attributes are not supported in html exports");
493
+ }
494
+ if (attr) {
495
+ 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
496
  }
210
497
  else {
211
498
  acc[propKey] = combinedStates[propKey];
212
499
  }
213
500
  return acc;
214
501
  }, {});
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)];
502
+ propsForInstance = {};
503
+ _loop_1 = function (propKey) {
504
+ var attribute = attrs[propKey];
505
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'element') {
506
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: attrs[propKey] });
507
+ }
508
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'dynamic') {
509
+ // When we are using a component instance in a component and the attribute
510
+ // that is passed to the component is of dynamic reference.
511
+ // If means, the component is redirecting the prop that is received to the prop of the component that it is consuming.
512
+ // 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.
513
+ // And similary we do the same for the states.
514
+ switch (attribute.content.referenceType) {
515
+ case 'prop':
516
+ propsForInstance[propKey] = combinedProps[attribute.content.id];
517
+ break;
518
+ case 'state':
519
+ propsForInstance[propKey] = combinedStates[attribute.content.id];
520
+ break;
521
+ case 'expr':
522
+ // Ignore expr type attributes in html comp instances for the time being.
523
+ break;
524
+ default:
525
+ throw new Error("ReferenceType ".concat(attribute.content.referenceType, " is not supported in HTML Export."));
526
+ }
527
+ }
528
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'object') {
529
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: (attribute === null || attribute === void 0 ? void 0 : attribute.content) || ((_f = combinedProps[propKey]) === null || _f === void 0 ? void 0 : _f.defaultValue) });
530
+ }
531
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'expr') {
532
+ var _k = attribute.content.split('?.'), ctxId_1 = _k[0], refPath = _k.slice(1);
533
+ var propKeyFromAttr = Object.keys(combinedProps).find(function (key) { return key === ctxId_1; });
534
+ var resolvedValue = combinedProps[propKeyFromAttr];
535
+ var hasRefPath = refPath.length > 0;
536
+ // Build the path using repeater index when available
537
+ var fullRefPath = typeof (resolvedExpressions === null || resolvedExpressions === void 0 ? void 0 : resolvedExpressions.currentIndex) === 'number' && hasRefPath
538
+ ? __spreadArray([resolvedExpressions.currentIndex.toString()], refPath, true) : refPath;
539
+ if (Array.isArray(resolvedValue)) {
540
+ // If the resolved value itself is an array definition, pass it through
541
+ propsForInstance[propKey] = resolvedValue;
542
+ }
543
+ else {
544
+ var defaultVal = resolvedValue === null || resolvedValue === void 0 ? void 0 : resolvedValue.defaultValue;
545
+ var extracted = hasRefPath && defaultVal !== undefined
546
+ ? extractDefaultValueFromRefPath(defaultVal, fullRefPath)
547
+ : defaultVal !== null && defaultVal !== void 0 ? defaultVal : null;
548
+ propsForInstance[propKey] = __assign(__assign({}, resolvedValue), { defaultValue: extracted });
549
+ }
550
+ }
551
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'dynamic' &&
552
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'element' &&
553
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'object' &&
554
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'expr') {
555
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: (_g = attribute === null || attribute === void 0 ? void 0 : attribute.content) !== null && _g !== void 0 ? _g : (_h = combinedProps[propKey]) === null || _h === void 0 ? void 0 : _h.defaultValue });
556
+ }
557
+ if (attribute === undefined) {
558
+ var propFromCurrentComponent = combinedProps[propKey];
559
+ propsForInstance[propKey] = propFromCurrentComponent;
560
+ }
561
+ };
562
+ // this is where we check if the component we are conusming is actually passing any props to the instance.
563
+ // 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
564
+ // the component instance that we are using here.
565
+ for (_i = 0, _e = Object.keys(combinedProps); _i < _e.length; _i++) {
566
+ propKey = _e[_i];
567
+ _loop_1(propKey);
568
+ }
569
+ componentWrapper = teleport_shared_1.StringUtils.camelCaseToDashCase("".concat(componentName, "-wrapper"));
570
+ isExistingNode = nodesLookup[componentWrapper];
571
+ if (isExistingNode !== undefined) {
572
+ componentWrapper = "".concat(componentWrapper, "-").concat(teleport_shared_1.StringUtils.generateRandomString());
573
+ }
574
+ componentInstanceToGenerate = {
575
+ type: 'element',
576
+ content: {
577
+ elementType: componentWrapper,
578
+ key: componentWrapper,
579
+ children: [componentClone.node],
580
+ style: {
581
+ display: {
582
+ type: 'static',
583
+ content: 'contents',
584
+ },
585
+ },
586
+ },
587
+ };
588
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(componentInstanceToGenerate, component.name, nodesLookup, propsForInstance, statesForInstance, subComponentOptions, structure, resolvedExpressions)];
221
589
  case 1:
222
- compTag = (_f.sent());
590
+ compTag = _j.sent();
223
591
  cssPlugin = (0, teleport_plugin_css_1.createCSSPlugin)({
224
592
  templateStyle: 'html',
225
593
  templateChunkName: constants_1.DEFAULT_COMPONENT_CHUNK_NAME,
226
594
  declareDependency: 'import',
227
- forceScoping: true,
228
- chunkName: comp.name,
595
+ chunkName: componentClone.name,
229
596
  staticPropReferences: true,
230
597
  });
231
598
  initialStructure = {
232
- uidl: __assign(__assign({}, comp), { propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
599
+ uidl: __assign(__assign({}, componentClone), { node: componentInstanceToGenerate, propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
233
600
  chunks: [
234
601
  {
235
602
  type: teleport_types_1.ChunkType.HAST,
@@ -238,7 +605,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
238
605
  linkAfter: [],
239
606
  content: compTag,
240
607
  meta: {
241
- nodesLookup: lookUpTemplates,
608
+ nodesLookup: nodesLookup,
242
609
  },
243
610
  },
244
611
  ],
@@ -257,132 +624,220 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
257
624
  });
258
625
  }); }, Promise.resolve(initialStructure))];
259
626
  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);
265
- }
266
- });
627
+ result = _j.sent();
628
+ result.chunks.forEach(function (chunk) {
629
+ if (chunk.fileType === teleport_types_1.FileType.CSS) {
630
+ chunks.push(chunk);
631
+ }
632
+ });
633
+ addNodeToLookup(node.content.key, compTag, nodesLookup);
634
+ return [2 /*return*/, compTag];
635
+ }
636
+ });
637
+ }); };
638
+ var generateDynamicNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions) { return __awaiter(void 0, void 0, void 0, function () {
639
+ var localeTag, commentNode, usedReferenceValue, extracted, elementNode, elementClone, iterElementTag, generatedElementTag, spanTagWrapper, commentNode, spanTag;
640
+ var _a;
641
+ return __generator(this, function (_b) {
642
+ switch (_b.label) {
643
+ case 0:
644
+ if (node.content.referenceType === 'locale') {
645
+ localeTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
646
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for locale ".concat(node.content.id));
647
+ teleport_plugin_common_1.HASTUtils.addChildNode(localeTag, commentNode);
648
+ return [2 /*return*/, localeTag];
267
649
  }
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*/];
274
- }
275
- chunks.push(styleChunk);
650
+ usedReferenceValue = getValueFromReference(node.content.id, node.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
651
+ if ((usedReferenceValue.type === 'object' || usedReferenceValue.type === 'array') &&
652
+ usedReferenceValue.defaultValue) {
653
+ extracted = extractDefaultValueFromRefPath(usedReferenceValue.defaultValue, node.content.refPath);
654
+ if (extracted === undefined || extracted === null) {
655
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode('')];
276
656
  }
657
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(String(extracted))];
277
658
  }
278
- return [2 /*return*/, compTag];
659
+ if (!(usedReferenceValue.type === 'element')) return [3 /*break*/, 5];
660
+ elementNode = usedReferenceValue.defaultValue;
661
+ if (!elementNode) return [3 /*break*/, 4];
662
+ if (!(resolvedExpressions && typeof resolvedExpressions.currentIndex === 'number')) return [3 /*break*/, 2];
663
+ elementClone = teleport_shared_1.UIDLUtils.cloneObject(elementNode);
664
+ if ((_a = elementClone === null || elementClone === void 0 ? void 0 : elementClone.content) === null || _a === void 0 ? void 0 : _a.key) {
665
+ elementClone.content.key = "".concat(elementClone.content.key, "-").concat(resolvedExpressions.currentIndex);
666
+ }
667
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(elementClone, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
668
+ case 1:
669
+ iterElementTag = _b.sent();
670
+ return [2 /*return*/, iterElementTag];
671
+ case 2:
672
+ if (elementNode.content.key in nodesLookup) {
673
+ return [2 /*return*/, nodesLookup[elementNode.content.key]];
674
+ }
675
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(elementNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure, resolvedExpressions)];
676
+ case 3:
677
+ generatedElementTag = _b.sent();
678
+ return [2 /*return*/, generatedElementTag];
679
+ case 4:
680
+ spanTagWrapper = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
681
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for slot ".concat(node.content.id));
682
+ teleport_plugin_common_1.HASTUtils.addChildNode(spanTagWrapper, commentNode);
683
+ return [2 /*return*/, spanTagWrapper];
684
+ case 5:
685
+ spanTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
686
+ teleport_plugin_common_1.HASTUtils.addTextNode(spanTag, String(usedReferenceValue.defaultValue));
687
+ return [2 /*return*/, spanTag];
279
688
  }
280
689
  });
281
690
  }); };
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
691
  var handleStyles = function (node, styles, propDefinitions, stateDefinitions) {
291
692
  Object.keys(styles).forEach(function (styleKey) {
292
- var _a;
693
+ var _a, _b;
293
694
  var style = styles[styleKey];
294
695
  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);
297
- }
298
- else if (style.content.referenceType === 'state') {
299
- style = getValueFromReference(style.content.id, stateDefinitions);
696
+ var referencedValue = getValueFromReference(style.content.id, style.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
697
+ if (referencedValue.type === 'string' || referencedValue.type === 'number') {
698
+ style = String(extractDefaultValueFromRefPath(referencedValue.defaultValue, (_b = style === null || style === void 0 ? void 0 : style.content) === null || _b === void 0 ? void 0 : _b.refPath));
300
699
  }
301
700
  node.content.style[styleKey] = typeof style === 'string' ? (0, teleport_uidl_builders_1.staticNode)(style) : style;
302
701
  }
303
702
  });
304
703
  };
305
- var handleAttributes = function (elementType, htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions, outputOptions) {
306
- Object.keys(attrs).forEach(function (attrKey) {
704
+ var handleAttributes = function (elementType, htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions, outputOptions, currentIndex) {
705
+ var _loop_2 = function (attrKey) {
307
706
  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;
707
+ var type = attrValue.type, content = attrValue.content;
708
+ switch (type) {
709
+ case 'static': {
710
+ if (attrKey === 'href' && typeof content === 'string' && content.startsWith('/')) {
711
+ var targetLink = void 0;
712
+ var targetRoute = ((routeDefinitions === null || routeDefinitions === void 0 ? void 0 : routeDefinitions.values) || []).find(function (route) { return route.pageOptions.navLink === content; });
713
+ if (targetRoute) {
714
+ targetLink = targetRoute.pageOptions.navLink;
715
+ }
716
+ if (!targetRoute && content === '/home') {
717
+ targetLink = '/';
718
+ }
719
+ if (!targetLink && !targetRoute) {
720
+ targetLink = content;
721
+ }
722
+ var currentPageRoute = path_1.join.apply(void 0, __spreadArray(__spreadArray([], ((outputOptions === null || outputOptions === void 0 ? void 0 : outputOptions.folderPath) || []), false), ['./'], false));
723
+ var localPrefix = (0, path_1.relative)("/".concat(currentPageRoute), "/".concat(targetLink === '/' ? 'index' : targetLink));
724
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, "".concat(localPrefix, ".html"));
725
+ break;
726
+ }
727
+ if (typeof content === 'boolean') {
728
+ htmlNode.properties[attrKey] = content === true ? 'true' : 'false';
729
+ }
730
+ else if (typeof content === 'string' || typeof attrValue.content === 'number') {
731
+ var value = teleport_shared_1.StringUtils.encode(String(attrValue.content));
732
+ /*
733
+ elementType of image is always mapped to img.
734
+ For reference, check `html-mapping` file.
735
+ */
736
+ if (elementType === 'img' && attrKey === 'src' && !isValidURL(value)) {
737
+ /*
738
+ By default we just prefix all the asset paths with just the
739
+ assetPrefix that is configured in the project. But for `html` generators
740
+ we need to prefix that with the current file location.
741
+
742
+ Because, all the other frameworks have a build setup. which serves all the
743
+ assets from the `public` folder. But in the case of `html` here is how it works
744
+
745
+ We load a file from `index.html` the request for the image goes from
746
+ '...url.../public/...image...'
747
+ If it's a nested url, then the request goes from
748
+ '...url/nested/public/...image..'
749
+
750
+ But the nested folder is available only on the root. With this
751
+ The url changes prefixes to
752
+
753
+ ../public/playground_assets/..image.. etc depending on the dept the file is in.
754
+ */
755
+ value = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), value);
756
+ }
757
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, value);
758
+ }
759
+ break;
316
760
  }
317
- if (!targetRoute && attrValue.content === '/home') {
318
- targetLink = '/';
761
+ case 'dynamic': {
762
+ var value = getValueFromReference(content.id, content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
763
+ var extracted = extractDefaultValueFromRefPath(value.defaultValue, content.refPath);
764
+ var extractedValue = String(extracted);
765
+ if ((elementType === 'img' || elementType === 'video') &&
766
+ attrKey === 'src' &&
767
+ !extractedValue.startsWith('http')) {
768
+ var path = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), extractedValue);
769
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, path);
770
+ break;
771
+ }
772
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, extractedValue);
773
+ break;
319
774
  }
320
- if (!targetLink && !targetRoute) {
321
- targetLink = attrValue.content;
775
+ case 'raw': {
776
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, content);
777
+ break;
322
778
  }
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);
779
+ case 'expr': {
780
+ var fullPath = content.split('?.');
781
+ var prop = propDefinitions[(fullPath === null || fullPath === void 0 ? void 0 : fullPath[0]) || ''];
782
+ if (!prop) {
783
+ break;
784
+ }
785
+ var path = typeof currentIndex === 'number'
786
+ ? __spreadArray([currentIndex.toString()], fullPath.slice(1), true) : fullPath.slice(1);
787
+ var value = extractDefaultValueFromRefPath(prop.defaultValue, path);
788
+ if (!value) {
789
+ break;
790
+ }
791
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(value));
792
+ break;
793
+ }
794
+ case 'element':
795
+ case 'import':
796
+ case 'object':
797
+ break;
798
+ default: {
799
+ throw new teleport_types_1.HTMLComponentGeneratorError("Received ".concat(JSON.stringify(attrValue, null, 2), " \n in handleAttributes for html"));
369
800
  }
370
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, value);
371
- return;
372
801
  }
373
- });
802
+ };
803
+ for (var _i = 0, _a = Object.keys(attrs); _i < _a.length; _i++) {
804
+ var attrKey = _a[_i];
805
+ _loop_2(attrKey);
806
+ }
374
807
  };
375
808
  var getValueFromReference = function (key, definitions) {
376
- var usedReferenceValue = definitions[key.includes('.') ? key.split('.')[0] : key];
809
+ var usedReferenceValue = definitions[key.includes('?.') ? key.split('?.')[0] : key];
377
810
  if (!usedReferenceValue) {
378
811
  throw new teleport_types_1.HTMLComponentGeneratorError("Definition for ".concat(key, " is missing from ").concat(JSON.stringify(definitions, null, 2)));
379
812
  }
380
- if (!usedReferenceValue.hasOwnProperty('defaultValue')) {
813
+ if (['string', 'number', 'object', 'element', 'array', 'boolean'].includes(usedReferenceValue === null || usedReferenceValue === void 0 ? void 0 : usedReferenceValue.type) === false) {
814
+ throw new teleport_types_1.HTMLComponentGeneratorError("Attribute is using dynamic value, but received of type ".concat(JSON.stringify(usedReferenceValue, null, 2)));
815
+ }
816
+ if (usedReferenceValue.type !== 'element' &&
817
+ usedReferenceValue.hasOwnProperty('defaultValue') === false) {
381
818
  throw new teleport_types_1.HTMLComponentGeneratorError("Default value is missing from dynamic reference - ".concat(JSON.stringify(usedReferenceValue, null, 2)));
382
819
  }
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)));
820
+ return usedReferenceValue;
821
+ };
822
+ var extractDefaultValueFromRefPath = function (propDefaultValue, refPath) {
823
+ if (!refPath || refPath.length === 0) {
824
+ return propDefaultValue;
825
+ }
826
+ // Directly handle array indexing for the first segment when applicable
827
+ if (Array.isArray(propDefaultValue)) {
828
+ var first = refPath[0], rest = refPath.slice(1);
829
+ var idx = Number(first);
830
+ if (!Number.isNaN(idx) && idx >= 0 && idx < propDefaultValue.length) {
831
+ var nextVal = propDefaultValue[idx];
832
+ if (rest.length === 0) {
833
+ return nextVal;
834
+ }
835
+ return extractDefaultValueFromRefPath(nextVal, rest);
836
+ }
837
+ }
838
+ if (typeof propDefaultValue !== 'object') {
839
+ return propDefaultValue;
385
840
  }
386
- return String(usedReferenceValue.defaultValue);
841
+ return teleport_shared_1.GenericUtils.getValueFromPath(refPath.join('.'), propDefaultValue);
387
842
  };
388
843
  //# sourceMappingURL=node-handlers.js.map