@teleporthq/teleport-plugin-html-base-component 0.38.0-alpha.0 → 0.38.1

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,64 +56,161 @@ 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
+ var path_1 = require("path");
61
62
  var teleport_plugin_common_1 = require("@teleporthq/teleport-plugin-common");
62
63
  var teleport_shared_1 = require("@teleporthq/teleport-shared");
63
64
  var teleport_uidl_builders_1 = require("@teleporthq/teleport-uidl-builders");
64
65
  var teleport_plugin_css_1 = require("@teleporthq/teleport-plugin-css");
66
+ var teleport_uidl_resolver_1 = require("@teleporthq/teleport-uidl-resolver");
65
67
  var constants_1 = require("./constants");
66
- var generateHtmlSynatx = function (node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure) { return __awaiter(void 0, void 0, void 0, function () {
67
- return __generator(this, function (_a) {
68
- switch (node.type) {
69
- case 'inject':
70
- case 'raw':
71
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(node.content.toString())];
72
- case 'static':
73
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(teleport_shared_1.StringUtils.encode(node.content.toString()))];
74
- case 'slot':
75
- return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode(node.type)];
76
- case 'element':
77
- return [2 /*return*/, generatElementNode(node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure)];
78
- case 'dynamic':
79
- return [2 /*return*/, generateDynamicNode(node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure)];
80
- default:
81
- 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, node, tag, nodesLoookup, hierarchy) {
79
+ if (hierarchy === void 0) { hierarchy = []; }
80
+ // In html code-generation we combine the nodes of the component that is being consumed with the current component.
81
+ // As html can't load the component at runtime like react or any other frameworks. So, we merge the component as a standalone
82
+ // component in the current component.
83
+ if (nodesLoookup[key]) {
84
+ throw new teleport_types_1.HTMLComponentGeneratorError("\n".concat(hierarchy.join(' -> '), " \n\nDuplicate key found in nodesLookup: ").concat(node.content.key, " \n\n\nA node with the same key already exists\n\nReceived \n\n ").concat(JSON.stringify(tag), "\n ").concat(JSON.stringify(node), "\nExisting \n\n ").concat(JSON.stringify(nodesLoookup[key]), " \n\n"));
85
+ }
86
+ nodesLoookup[key] = tag;
87
+ };
88
+ var generateHtmlSyntax = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
89
+ var _a, elementNode, dynamicNode, conditionalNodeComment, _b, staticValue, reference, _c, conditions, matchingCriteria, _d, referenceType, id, refPath, usedProp, defaultValue, _i, refPath_1, path, dynamicConditions, matchCondition, conditionString, isConditionPassing;
90
+ return __generator(this, function (_e) {
91
+ switch (_e.label) {
92
+ case 0:
93
+ _a = node.type;
94
+ switch (_a) {
95
+ case 'inject': return [3 /*break*/, 1];
96
+ case 'raw': return [3 /*break*/, 1];
97
+ case 'static': return [3 /*break*/, 2];
98
+ case 'slot': return [3 /*break*/, 3];
99
+ case 'element': return [3 /*break*/, 4];
100
+ case 'dynamic': return [3 /*break*/, 6];
101
+ case 'conditional': return [3 /*break*/, 8];
102
+ case 'expr': return [3 /*break*/, 9];
103
+ }
104
+ return [3 /*break*/, 10];
105
+ case 1: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(node.content.toString())];
106
+ case 2: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(teleport_shared_1.StringUtils.encode(node.content.toString()))];
107
+ case 3: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createHTMLNode(node.type)];
108
+ case 4: return [4 /*yield*/, generateElementNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
109
+ case 5:
110
+ elementNode = _e.sent();
111
+ return [2 /*return*/, elementNode];
112
+ case 6: return [4 /*yield*/, generateDynamicNode(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
113
+ case 7:
114
+ dynamicNode = _e.sent();
115
+ return [2 /*return*/, dynamicNode];
116
+ case 8:
117
+ conditionalNodeComment = teleport_plugin_common_1.HASTBuilders.createComment('Conditional nodes are not supported in HTML');
118
+ _b = node.content, staticValue = _b.value, reference = _b.reference, _c = _b.condition, conditions = _c.conditions, matchingCriteria = _c.matchingCriteria;
119
+ if (reference.type !== 'dynamic') {
120
+ return [2 /*return*/, conditionalNodeComment];
121
+ }
122
+ _d = reference.content, referenceType = _d.referenceType, id = _d.id, refPath = _d.refPath;
123
+ switch (referenceType) {
124
+ case 'prop': {
125
+ usedProp = propDefinitions[id];
126
+ if (usedProp === undefined || usedProp.defaultValue === undefined) {
127
+ return [2 /*return*/, conditionalNodeComment];
128
+ }
129
+ defaultValue = usedProp.defaultValue;
130
+ for (_i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) {
131
+ path = refPath_1[_i];
132
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
133
+ }
134
+ // Safety measure in case no value is found
135
+ if (!defaultValue) {
136
+ defaultValue = usedProp.defaultValue;
137
+ }
138
+ dynamicConditions = createConditionalStatement(staticValue !== undefined ? [{ operand: staticValue, operation: '===' }] : conditions, defaultValue);
139
+ matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
140
+ conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
141
+ try {
142
+ isConditionPassing = new Function("return ".concat(conditionString))();
143
+ if (isConditionPassing) {
144
+ return [2 /*return*/, (0, exports.generateHtmlSyntax)(node.content.node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
145
+ }
146
+ }
147
+ catch (error) {
148
+ return [2 /*return*/, conditionalNodeComment];
149
+ }
150
+ return [2 /*return*/, conditionalNodeComment];
151
+ }
152
+ case 'state':
153
+ default:
154
+ return [2 /*return*/, conditionalNodeComment];
155
+ }
156
+ _e.label = 9;
157
+ case 9: return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createComment('Expressions are not supported in HTML')];
158
+ case 10: throw new teleport_types_1.HTMLComponentGeneratorError("generateHtmlSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2), " "));
82
159
  }
83
- return [2 /*return*/];
84
160
  });
85
161
  }); };
86
- exports.generateHtmlSynatx = generateHtmlSynatx;
87
- var generatElementNode = function (node, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure) { return __awaiter(void 0, void 0, void 0, function () {
88
- var _a, elementType, children, _b, attrs, _c, style, _d, referencedStyles, dependency, key, elementNode, dependencies, compTag, _i, children_1, child, childTag;
162
+ exports.generateHtmlSyntax = generateHtmlSyntax;
163
+ var createConditionalStatement = function (conditions, leftOperand) {
164
+ return conditions.map(function (condition) {
165
+ var operation = condition.operation, operand = condition.operand;
166
+ if (operand === undefined) {
167
+ return "".concat(teleport_plugin_common_1.ASTUtils.convertToUnaryOperator(operation)).concat(getValueType(operand));
168
+ }
169
+ return "".concat(getValueType(leftOperand), " ").concat(teleport_plugin_common_1.ASTUtils.convertToBinaryOperator(operation), " ").concat(getValueType(operand));
170
+ });
171
+ };
172
+ var getValueType = function (value) {
173
+ var valueType = typeof value;
174
+ switch (valueType) {
175
+ case 'string':
176
+ return "\"".concat(value, "\"");
177
+ case 'number':
178
+ return value;
179
+ case 'boolean':
180
+ return value;
181
+ default:
182
+ throw new teleport_types_1.HTMLComponentGeneratorError("Conditional node received an operand of type ".concat(valueType, " \n\n Received ").concat(JSON.stringify(value)));
183
+ }
184
+ };
185
+ var generateElementNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
186
+ var _a, elementType, children, _b, attrs, _c, style, _d, referencedStyles, dependency, dependencies, compTag, elementNode, _i, children_1, child, childTag;
89
187
  return __generator(this, function (_e) {
90
188
  switch (_e.label) {
91
189
  case 0:
92
- _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;
93
- elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(elementType);
94
- templatesLookUp[key] = elementNode;
190
+ _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;
95
191
  dependencies = structure.dependencies;
96
- if (dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) !== 'local') {
97
- dependencies[dependency.path] = dependency;
98
- }
99
192
  if (!(dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) === 'local')) return [3 /*break*/, 2];
100
- return [4 /*yield*/, generateComponentContent(node, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure)];
193
+ return [4 /*yield*/, generateComponentContent(node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
101
194
  case 1:
102
195
  compTag = _e.sent();
196
+ if ('tagName' in compTag) {
197
+ compTag.children.unshift(teleport_plugin_common_1.HASTBuilders.createComment("".concat(node.content.semanticType, " component")));
198
+ }
103
199
  return [2 /*return*/, compTag];
104
200
  case 2:
201
+ if (dependency && (dependency === null || dependency === void 0 ? void 0 : dependency.type) !== 'local') {
202
+ dependencies[dependency.path] = dependency;
203
+ }
204
+ elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(elementType);
105
205
  if (!children) return [3 /*break*/, 6];
106
206
  _i = 0, children_1 = children;
107
207
  _e.label = 3;
108
208
  case 3:
109
209
  if (!(_i < children_1.length)) return [3 /*break*/, 6];
110
210
  child = children_1[_i];
111
- return [4 /*yield*/, (0, exports.generateHtmlSynatx)(child, templatesLookUp, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure)];
211
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(child, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
112
212
  case 4:
113
213
  childTag = _e.sent();
114
- if (!childTag) {
115
- return [2 /*return*/];
116
- }
117
214
  if (typeof childTag === 'string') {
118
215
  teleport_plugin_common_1.HASTUtils.addTextNode(elementNode, childTag);
119
216
  }
@@ -130,38 +227,47 @@ var generatElementNode = function (node, templatesLookUp, propDefinitions, state
130
227
  var refStyle = referencedStyles[styleRef];
131
228
  if (refStyle.content.mapType === 'inlined') {
132
229
  handleStyles(node, refStyle.content.styles, propDefinitions, stateDefinitions);
133
- return;
134
230
  }
135
231
  });
136
232
  }
137
233
  if (Object.keys(style).length > 0) {
138
234
  handleStyles(node, style, propDefinitions, stateDefinitions);
139
235
  }
140
- if (Object.keys(attrs).length > 0) {
141
- handleAttributes(elementNode, attrs, propDefinitions, stateDefinitions, routeDefinitions);
142
- }
236
+ handleAttributes(elementType, elementNode, attrs, propDefinitions, stateDefinitions, structure.options.projectRouteDefinition, structure.outputOptions);
237
+ addNodeToLookup(node.content.key, node, elementNode, nodesLookup, [compName]);
143
238
  return [2 /*return*/, elementNode];
144
239
  }
145
240
  });
146
241
  }); };
147
- var generateComponentContent = function (node, propDefinitions, stateDefinitions, subComponentOptions, routeDefinitions, structure) { return __awaiter(void 0, void 0, void 0, function () {
148
- 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;
149
- var _e;
150
- return __generator(this, function (_f) {
151
- switch (_f.label) {
242
+ var createLookupTable = function (component, nodesLookup) {
243
+ var lookup = {};
244
+ for (var _i = 0, _a = Object.keys(nodesLookup); _i < _a.length; _i++) {
245
+ var node = _a[_i];
246
+ lookup[node] = {
247
+ count: 1,
248
+ nextKey: '1',
249
+ };
250
+ }
251
+ (0, teleport_uidl_resolver_1.createNodesLookup)(component, lookup);
252
+ return lookup;
253
+ };
254
+ var generateComponentContent = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
255
+ var externals, plugins, _a, elementType, _b, attrs, _c, children, dependencies, _d, chunks, options, componentName, component, componentClone, lookupTableForCurrentPage, combinedProps, combinedStates, statesForInstance, propsForInstance, _i, _e, propKey, attribute, propFromCurrentComponent, componentWrapper, isExistingNode, componentInstanceToGenerate, compTag, cssPlugin, initialStructure, result;
256
+ var _f, _g;
257
+ return __generator(this, function (_h) {
258
+ switch (_h.label) {
152
259
  case 0:
153
260
  externals = subComponentOptions.externals, plugins = subComponentOptions.plugins;
154
- _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;
261
+ _a = node.content, elementType = _a.elementType, _b = _a.attrs, attrs = _b === void 0 ? {} : _b, _c = _a.children, children = _c === void 0 ? [] : _c;
155
262
  dependencies = structure.dependencies, _d = structure.chunks, chunks = _d === void 0 ? [] : _d, options = structure.options;
156
- comp = teleport_shared_1.UIDLUtils.cloneObject(externals[elementType] || {});
157
- lookUpTemplates = {};
158
- compHasSlots = false;
159
- if (!comp || !(comp === null || comp === void 0 ? void 0 : comp.node)) {
160
- 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)));
263
+ componentName = elementType === 'Component' ? 'AppComponent' : elementType;
264
+ component = externals[componentName];
265
+ if (component === undefined) {
266
+ throw new teleport_types_1.HTMLComponentGeneratorError("".concat(componentName, " is missing from externals object"));
161
267
  }
268
+ componentClone = teleport_shared_1.UIDLUtils.cloneObject(component);
162
269
  if (children.length) {
163
- compHasSlots = true;
164
- teleport_shared_1.UIDLUtils.traverseNodes(comp.node, function (childNode, parentNode) {
270
+ teleport_shared_1.UIDLUtils.traverseNodes(componentClone.node, function (childNode, parentNode) {
165
271
  var _a, _b;
166
272
  if (childNode.type === 'slot' && parentNode.type === 'element') {
167
273
  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'; });
@@ -171,6 +277,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
171
277
  content: {
172
278
  key: 'custom-slot',
173
279
  elementType: 'slot',
280
+ name: componentClone.name + 'slot',
174
281
  style: {
175
282
  display: {
176
283
  type: 'static',
@@ -189,46 +296,115 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
189
296
  */
190
297
  node.content.children = [];
191
298
  }
192
- combinedProps = __assign(__assign({}, propDefinitions), ((comp === null || comp === void 0 ? void 0 : comp.propDefinitions) || {}));
193
- propsForInstance = Object.keys(combinedProps).reduce(function (acc, propKey) {
194
- var _a, _b;
195
- if (attrs[propKey]) {
196
- 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) });
197
- }
198
- else {
199
- acc[propKey] = combinedProps[propKey];
299
+ lookupTableForCurrentPage = createLookupTable(componentClone, nodesLookup);
300
+ (0, teleport_uidl_resolver_1.generateUniqueKeys)(componentClone, lookupTableForCurrentPage);
301
+ combinedProps = __assign(__assign({}, Object.keys(propDefinitions).reduce(function (acc, propKey) {
302
+ var _a;
303
+ if (((_a = propDefinitions[propKey]) === null || _a === void 0 ? void 0 : _a.type) === 'element') {
304
+ return acc;
200
305
  }
306
+ acc[propKey] = propDefinitions[propKey];
201
307
  return acc;
202
- }, {});
203
- combinedStates = __assign(__assign({}, stateDefinitions), ((comp === null || comp === void 0 ? void 0 : comp.stateDefinitions) || {}));
308
+ }, {})), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.propDefinitions) || {}));
309
+ combinedStates = __assign(__assign({}, stateDefinitions), ((componentClone === null || componentClone === void 0 ? void 0 : componentClone.stateDefinitions) || {}));
204
310
  statesForInstance = Object.keys(combinedStates).reduce(function (acc, propKey) {
205
- var _a, _b;
206
- if (attrs[propKey]) {
207
- 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) });
311
+ var _a;
312
+ var attr = attrs[propKey];
313
+ if (attr.type === 'object') {
314
+ throw new Error("Object attributes are not supported in html exports");
315
+ }
316
+ if (attr) {
317
+ acc[propKey] = __assign(__assign({}, combinedStates[propKey]), { defaultValue: (attr === null || attr === void 0 ? void 0 : attr.content) || ((_a = combinedStates[propKey]) === null || _a === void 0 ? void 0 : _a.defaultValue) });
208
318
  }
209
319
  else {
210
320
  acc[propKey] = combinedStates[propKey];
211
321
  }
212
322
  return acc;
213
323
  }, {});
214
- elementNode = teleport_plugin_common_1.HASTBuilders.createHTMLNode(teleport_shared_1.StringUtils.camelCaseToDashCase(elementType));
215
- lookUpTemplates[key] = elementNode;
216
- 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: {
217
- type: 'static',
218
- content: 'contents',
219
- } }) }) }), lookUpTemplates, propsForInstance, statesForInstance, subComponentOptions, routeDefinitions, structure)];
324
+ propsForInstance = {};
325
+ _i = 0, _e = Object.keys(combinedProps);
326
+ _h.label = 1;
220
327
  case 1:
221
- compTag = (_f.sent());
328
+ if (!(_i < _e.length)) return [3 /*break*/, 7];
329
+ propKey = _e[_i];
330
+ attribute = attrs[propKey];
331
+ if (!((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'element')) return [3 /*break*/, 3];
332
+ propsForInstance[propKey] = __assign(__assign({}, combinedProps[propKey]), { defaultValue: attrs[propKey] });
333
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(attrs[propKey], component.name, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
334
+ case 2:
335
+ _h.sent();
336
+ _h.label = 3;
337
+ case 3:
338
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'dynamic') {
339
+ // When we are using a component instance in a component and the attribute
340
+ // that is passed to the component is of dynamic reference.
341
+ // If means, the component is redirecting the prop that is received to the prop of the component that it is consuming.
342
+ // 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.
343
+ // And similary we do the same for the states.
344
+ switch (attribute.content.referenceType) {
345
+ case 'prop':
346
+ propsForInstance[propKey] = combinedProps[propKey];
347
+ break;
348
+ case 'state':
349
+ propsForInstance[propKey] = combinedStates[propKey];
350
+ break;
351
+ default:
352
+ throw new Error("ReferenceType ".concat(attribute.content.referenceType, " is not supported in HTML Export."));
353
+ }
354
+ }
355
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'object') {
356
+ 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) });
357
+ }
358
+ if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'dynamic' &&
359
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'element' &&
360
+ (attribute === null || attribute === void 0 ? void 0 : attribute.type) !== 'object') {
361
+ 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) });
362
+ }
363
+ if (!(attribute === undefined)) return [3 /*break*/, 6];
364
+ propFromCurrentComponent = combinedProps[propKey];
365
+ if (!(propFromCurrentComponent.type === 'element' && propFromCurrentComponent.defaultValue)) return [3 /*break*/, 5];
366
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(propFromCurrentComponent.defaultValue, component.name, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
367
+ case 4:
368
+ _h.sent();
369
+ _h.label = 5;
370
+ case 5:
371
+ propsForInstance[propKey] = propFromCurrentComponent;
372
+ _h.label = 6;
373
+ case 6:
374
+ _i++;
375
+ return [3 /*break*/, 1];
376
+ case 7:
377
+ componentWrapper = teleport_shared_1.StringUtils.camelCaseToDashCase("".concat(componentName, "-wrapper"));
378
+ isExistingNode = nodesLookup[componentWrapper];
379
+ if (isExistingNode !== undefined) {
380
+ componentWrapper = "".concat(componentWrapper, "-").concat(teleport_shared_1.StringUtils.generateRandomString());
381
+ }
382
+ componentInstanceToGenerate = {
383
+ type: 'element',
384
+ content: {
385
+ elementType: componentWrapper,
386
+ key: componentWrapper,
387
+ children: [componentClone.node],
388
+ style: {
389
+ display: {
390
+ type: 'static',
391
+ content: 'contents',
392
+ },
393
+ },
394
+ },
395
+ };
396
+ return [4 /*yield*/, (0, exports.generateHtmlSyntax)(componentInstanceToGenerate, component.name, nodesLookup, propsForInstance, statesForInstance, subComponentOptions, structure)];
397
+ case 8:
398
+ compTag = _h.sent();
222
399
  cssPlugin = (0, teleport_plugin_css_1.createCSSPlugin)({
223
400
  templateStyle: 'html',
224
401
  templateChunkName: constants_1.DEFAULT_COMPONENT_CHUNK_NAME,
225
402
  declareDependency: 'import',
226
- forceScoping: true,
227
- chunkName: comp.name,
403
+ chunkName: componentClone.name,
228
404
  staticPropReferences: true,
229
405
  });
230
406
  initialStructure = {
231
- uidl: __assign(__assign({}, comp), { propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
407
+ uidl: __assign(__assign({}, componentClone), { node: componentInstanceToGenerate, propDefinitions: propsForInstance, stateDefinitions: statesForInstance }),
232
408
  chunks: [
233
409
  {
234
410
  type: teleport_types_1.ChunkType.HAST,
@@ -237,7 +413,7 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
237
413
  linkAfter: [],
238
414
  content: compTag,
239
415
  meta: {
240
- nodesLookup: lookUpTemplates,
416
+ nodesLookup: nodesLookup,
241
417
  },
242
418
  },
243
419
  ],
@@ -255,100 +431,170 @@ var generateComponentContent = function (node, propDefinitions, stateDefinitions
255
431
  }
256
432
  });
257
433
  }); }, Promise.resolve(initialStructure))];
258
- case 2:
259
- result = _f.sent();
260
- if (compHasSlots) {
261
- result.chunks.forEach(function (chunk) {
262
- if (chunk.fileType === teleport_types_1.FileType.CSS) {
263
- chunks.push(chunk);
264
- }
265
- });
266
- }
267
- else {
268
- chunk = chunks.find(function (item) { return item.name === comp.name; });
269
- if (!chunk) {
270
- styleChunk = result.chunks.find(function (item) { return item.fileType === teleport_types_1.FileType.CSS; });
271
- if (!styleChunk) {
272
- return [2 /*return*/];
273
- }
274
- chunks.push(styleChunk);
434
+ case 9:
435
+ result = _h.sent();
436
+ result.chunks.forEach(function (chunk) {
437
+ if (chunk.fileType === teleport_types_1.FileType.CSS) {
438
+ chunks.push(chunk);
275
439
  }
276
- }
440
+ });
441
+ addNodeToLookup(node.content.key, node, compTag, nodesLookup, [compName, component.name]);
277
442
  return [2 /*return*/, compTag];
278
443
  }
279
444
  });
280
445
  }); };
281
- var generateDynamicNode = function (node, _, propDefinitions, stateDefinitions) {
282
- var spanTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
283
- var usedReferenceValue = node.content.referenceType === 'prop'
284
- ? getValueFromReference(node.content.id, propDefinitions)
285
- : getValueFromReference(node.content.id, stateDefinitions);
286
- teleport_plugin_common_1.HASTUtils.addTextNode(spanTag, String(usedReferenceValue));
287
- return spanTag;
288
- };
446
+ var generateDynamicNode = function (node, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure) { return __awaiter(void 0, void 0, void 0, function () {
447
+ var localeTag, commentNode, usedReferenceValue, elementNode, elementTag, spanTagWrapper, commentNode, spanTag;
448
+ return __generator(this, function (_a) {
449
+ switch (_a.label) {
450
+ case 0:
451
+ if (node.content.referenceType === 'locale') {
452
+ localeTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
453
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for locale ".concat(node.content.id));
454
+ teleport_plugin_common_1.HASTUtils.addChildNode(localeTag, commentNode);
455
+ return [2 /*return*/, localeTag];
456
+ }
457
+ usedReferenceValue = getValueFromReference(node.content.id, node.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
458
+ if ((usedReferenceValue.type === 'object' || usedReferenceValue.type === 'array') &&
459
+ usedReferenceValue.defaultValue) {
460
+ // Let's say users are biding the prop to a node using something like this "fields.Title"
461
+ // But the fields in the object is the value where the object is defined either in propDefinitions
462
+ // or on the attrs. So, we just need to parsed the rest of the object path and get the value from the object.
463
+ return [2 /*return*/, teleport_plugin_common_1.HASTBuilders.createTextNode(String(extractDefaultValueFromRefPath(usedReferenceValue.defaultValue, node.content.refPath)))];
464
+ }
465
+ if (!(usedReferenceValue.type === 'element')) return [3 /*break*/, 4];
466
+ elementNode = usedReferenceValue.defaultValue;
467
+ if (!elementNode) return [3 /*break*/, 3];
468
+ if (!(elementNode.content.key in nodesLookup)) return [3 /*break*/, 1];
469
+ return [2 /*return*/, nodesLookup[elementNode.content.key]];
470
+ case 1: return [4 /*yield*/, (0, exports.generateHtmlSyntax)(elementNode, compName, nodesLookup, propDefinitions, stateDefinitions, subComponentOptions, structure)];
471
+ case 2:
472
+ elementTag = _a.sent();
473
+ return [2 /*return*/, elementTag];
474
+ case 3:
475
+ spanTagWrapper = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
476
+ commentNode = teleport_plugin_common_1.HASTBuilders.createComment("Content for slot ".concat(node.content.id));
477
+ teleport_plugin_common_1.HASTUtils.addChildNode(spanTagWrapper, commentNode);
478
+ return [2 /*return*/, spanTagWrapper];
479
+ case 4:
480
+ spanTag = teleport_plugin_common_1.HASTBuilders.createHTMLNode('span');
481
+ teleport_plugin_common_1.HASTUtils.addTextNode(spanTag, String(usedReferenceValue.defaultValue));
482
+ return [2 /*return*/, spanTag];
483
+ }
484
+ });
485
+ }); };
289
486
  var handleStyles = function (node, styles, propDefinitions, stateDefinitions) {
290
487
  Object.keys(styles).forEach(function (styleKey) {
291
- var _a;
488
+ var _a, _b;
292
489
  var style = styles[styleKey];
293
490
  if (style.type === 'dynamic' && ((_a = style.content) === null || _a === void 0 ? void 0 : _a.referenceType) !== 'token') {
294
- if (style.content.referenceType === 'prop') {
295
- style = getValueFromReference(style.content.id, propDefinitions);
296
- }
297
- else if (style.content.referenceType === 'state') {
298
- style = getValueFromReference(style.content.id, stateDefinitions);
491
+ var referencedValue = getValueFromReference(style.content.id, style.content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
492
+ if (referencedValue.type === 'string' || referencedValue.type === 'number') {
493
+ style = String(extractDefaultValueFromRefPath(referencedValue.defaultValue, (_b = style === null || style === void 0 ? void 0 : style.content) === null || _b === void 0 ? void 0 : _b.refPath));
299
494
  }
300
495
  node.content.style[styleKey] = typeof style === 'string' ? (0, teleport_uidl_builders_1.staticNode)(style) : style;
301
496
  }
302
497
  });
303
498
  };
304
- var handleAttributes = function (htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions) {
305
- Object.keys(attrs).forEach(function (attrKey) {
499
+ var handleAttributes = function (elementType, htmlNode, attrs, propDefinitions, stateDefinitions, routeDefinitions, outputOptions) {
500
+ var _loop_1 = function (attrKey) {
306
501
  var attrValue = attrs[attrKey];
307
- if (attrKey === 'href' &&
308
- attrValue.type === 'static' &&
309
- typeof attrValue.content === 'string' &&
310
- attrValue.content.startsWith('/')) {
311
- attrValue =
312
- attrValue.content === '/' ||
313
- attrValue.content ===
314
- "/".concat(teleport_shared_1.StringUtils.camelCaseToDashCase(teleport_shared_1.StringUtils.removeIllegalCharacters((routeDefinitions === null || routeDefinitions === void 0 ? void 0 : routeDefinitions.defaultValue) || '')))
315
- ? (0, teleport_uidl_builders_1.staticNode)('index.html')
316
- : (0, teleport_uidl_builders_1.staticNode)("".concat(attrValue.content.split('/').pop(), ".html"));
317
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(attrValue.content));
318
- return;
319
- }
320
- if (attrValue.type === 'dynamic') {
321
- var value = attrValue.content.referenceType === 'prop'
322
- ? getValueFromReference(attrValue.content.id, propDefinitions)
323
- : getValueFromReference(attrValue.content.id, stateDefinitions);
324
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(value));
325
- return;
326
- }
327
- if (attrValue.type === 'raw') {
328
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, attrValue.content);
329
- return;
330
- }
331
- if (typeof attrValue.content === 'boolean') {
332
- teleport_plugin_common_1.HASTUtils.addBooleanAttributeToNode(htmlNode, attrKey);
333
- return;
334
- }
335
- else if (typeof attrValue.content === 'string' || typeof attrValue.content === 'number') {
336
- teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, teleport_shared_1.StringUtils.encode(String(attrValue.content)));
337
- return;
502
+ var type = attrValue.type, content = attrValue.content;
503
+ switch (type) {
504
+ case 'static': {
505
+ if (attrKey === 'href' && typeof content === 'string' && content.startsWith('/')) {
506
+ var targetLink = void 0;
507
+ var targetRoute = ((routeDefinitions === null || routeDefinitions === void 0 ? void 0 : routeDefinitions.values) || []).find(function (route) { return route.pageOptions.navLink === content; });
508
+ if (targetRoute) {
509
+ targetLink = targetRoute.pageOptions.navLink;
510
+ }
511
+ if (!targetRoute && content === '/home') {
512
+ targetLink = '/';
513
+ }
514
+ if (!targetLink && !targetRoute) {
515
+ targetLink = content;
516
+ }
517
+ var currentPageRoute = path_1.join.apply(void 0, __spreadArray(__spreadArray([], ((outputOptions === null || outputOptions === void 0 ? void 0 : outputOptions.folderPath) || []), false), ['./'], false));
518
+ var localPrefix = (0, path_1.relative)("/".concat(currentPageRoute), "/".concat(targetLink === '/' ? 'index' : targetLink));
519
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, "".concat(localPrefix, ".html"));
520
+ break;
521
+ }
522
+ if (typeof content === 'boolean') {
523
+ htmlNode.properties[attrKey] = content === true ? 'true' : 'false';
524
+ }
525
+ else if (typeof content === 'string' || typeof attrValue.content === 'number') {
526
+ var value = teleport_shared_1.StringUtils.encode(String(attrValue.content));
527
+ /*
528
+ elementType of image is always mapped to img.
529
+ For reference, check `html-mapping` file.
530
+ */
531
+ if (elementType === 'img' && attrKey === 'src' && !isValidURL(value)) {
532
+ /*
533
+ By default we just prefix all the asset paths with just the
534
+ assetPrefix that is configured in the project. But for `html` generators
535
+ we need to prefix that with the current file location.
536
+
537
+ Because, all the other frameworks have a build setup. which serves all the
538
+ assets from the `public` folder. But in the case of `html` here is how it works
539
+
540
+ We load a file from `index.html` the request for the image goes from
541
+ '...url.../public/...image...'
542
+ If it's a nested url, then the request goes from
543
+ '...url/nested/public/...image..'
544
+
545
+ But the nested folder is available only on the root. With this
546
+ The url changes prefixes to
547
+
548
+ ../public/playground_assets/..image.. etc depending on the dept the file is in.
549
+ */
550
+ value = (0, path_1.join)((0, path_1.relative)(path_1.join.apply(void 0, outputOptions.folderPath), './'), value);
551
+ }
552
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, value);
553
+ }
554
+ break;
555
+ }
556
+ case 'dynamic': {
557
+ var value = getValueFromReference(content.id, content.referenceType === 'prop' ? propDefinitions : stateDefinitions);
558
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, String(extractDefaultValueFromRefPath(value.defaultValue, content.refPath)));
559
+ break;
560
+ }
561
+ case 'raw': {
562
+ teleport_plugin_common_1.HASTUtils.addAttributeToNode(htmlNode, attrKey, content);
563
+ break;
564
+ }
565
+ case 'element':
566
+ case 'import':
567
+ case 'expr':
568
+ case 'object':
569
+ break;
570
+ default: {
571
+ throw new teleport_types_1.HTMLComponentGeneratorError("Received ".concat(JSON.stringify(attrValue, null, 2), " \n in handleAttributes for html"));
572
+ }
338
573
  }
339
- });
574
+ };
575
+ for (var _i = 0, _a = Object.keys(attrs); _i < _a.length; _i++) {
576
+ var attrKey = _a[_i];
577
+ _loop_1(attrKey);
578
+ }
340
579
  };
341
580
  var getValueFromReference = function (key, definitions) {
342
- var usedReferenceValue = definitions[key.includes('.') ? key.split('.')[0] : key];
581
+ var usedReferenceValue = definitions[key.includes('?.') ? key.split('?.')[0] : key];
343
582
  if (!usedReferenceValue) {
344
583
  throw new teleport_types_1.HTMLComponentGeneratorError("Definition for ".concat(key, " is missing from ").concat(JSON.stringify(definitions, null, 2)));
345
584
  }
346
- if (!usedReferenceValue.hasOwnProperty('defaultValue')) {
585
+ if (['string', 'number', 'object', 'element', 'array'].includes(usedReferenceValue === null || usedReferenceValue === void 0 ? void 0 : usedReferenceValue.type) === false) {
586
+ throw new teleport_types_1.HTMLComponentGeneratorError("Attribute is using dynamic value, but received of type ".concat(JSON.stringify(usedReferenceValue, null, 2)));
587
+ }
588
+ if (usedReferenceValue.type !== 'element' &&
589
+ usedReferenceValue.hasOwnProperty('defaultValue') === false) {
347
590
  throw new teleport_types_1.HTMLComponentGeneratorError("Default value is missing from dynamic reference - ".concat(JSON.stringify(usedReferenceValue, null, 2)));
348
591
  }
349
- if (!['string', 'number', 'object'].includes(usedReferenceValue === null || usedReferenceValue === void 0 ? void 0 : usedReferenceValue.type)) {
350
- throw new teleport_types_1.HTMLComponentGeneratorError("Attribute is using dynamic value, but received of type ".concat(JSON.stringify(usedReferenceValue, null, 2)));
592
+ return usedReferenceValue;
593
+ };
594
+ var extractDefaultValueFromRefPath = function (propDefaultValue, refPath) {
595
+ if (typeof propDefaultValue !== 'object' || !(refPath === null || refPath === void 0 ? void 0 : refPath.length)) {
596
+ return propDefaultValue;
351
597
  }
352
- return String(usedReferenceValue.defaultValue);
598
+ return teleport_shared_1.GenericUtils.getValueFromPath(refPath.join('.'), propDefaultValue);
353
599
  };
354
600
  //# sourceMappingURL=node-handlers.js.map