@teleporthq/teleport-plugin-common 0.45.3-alpha.0 → 0.45.13

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.
Files changed (200) hide show
  1. package/__tests__/builders/html-builders.ts +9 -2
  2. package/__tests__/builders/style-builders.ts +135 -1
  3. package/__tests__/node-handlers/node-to-html/index.ts +2 -0
  4. package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
  5. package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
  6. package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
  7. package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
  8. package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
  9. package/__tests__/node-handlers/node-to-jsx/index.ts +719 -3
  10. package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
  11. package/__tests__/node-handlers/node-to-jsx/utils.ts +635 -3
  12. package/__tests__/utils/ast-utils.ts +188 -0
  13. package/__tests__/utils/content-property-sanitiser.ts +156 -0
  14. package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
  15. package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
  16. package/__tests__/utils/resource-auth-scheme.ts +55 -0
  17. package/__tests__/utils/route-utils.ts +118 -0
  18. package/__tests__/utils/style-utils.ts +59 -0
  19. package/__tests__/utils/template-expression-balance.ts +83 -0
  20. package/__tests__/utils/url-query-writer.ts +239 -0
  21. package/__tests__/utils/url-search-param-sync.ts +138 -0
  22. package/__tests__/utils/url-search-params.ts +48 -0
  23. package/dist/cjs/builders/ast-builders.d.ts +2 -0
  24. package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
  25. package/dist/cjs/builders/ast-builders.js +41 -3
  26. package/dist/cjs/builders/ast-builders.js.map +1 -1
  27. package/dist/cjs/builders/hast-builders.d.ts +1 -0
  28. package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
  29. package/dist/cjs/builders/hast-builders.js +11 -1
  30. package/dist/cjs/builders/hast-builders.js.map +1 -1
  31. package/dist/cjs/builders/style-builders.d.ts +8 -2
  32. package/dist/cjs/builders/style-builders.d.ts.map +1 -1
  33. package/dist/cjs/builders/style-builders.js +80 -33
  34. package/dist/cjs/builders/style-builders.js.map +1 -1
  35. package/dist/cjs/index.d.ts +5 -1
  36. package/dist/cjs/index.d.ts.map +1 -1
  37. package/dist/cjs/index.js +10 -1
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
  40. package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
  41. package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
  42. package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
  43. package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
  44. package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
  45. package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
  46. package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
  47. package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
  48. package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
  49. package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
  50. package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
  51. package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  52. package/dist/cjs/node-handlers/node-to-jsx/index.js +1443 -45
  53. package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
  54. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
  55. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  56. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +80 -7
  57. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  58. package/dist/cjs/node-handlers/node-to-jsx/utils.js +883 -72
  59. package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
  60. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  61. package/dist/cjs/utils/ast-utils.d.ts +163 -11
  62. package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
  63. package/dist/cjs/utils/ast-utils.js +1013 -75
  64. package/dist/cjs/utils/ast-utils.js.map +1 -1
  65. package/dist/cjs/utils/hast-utils.d.ts +8 -6
  66. package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
  67. package/dist/cjs/utils/hast-utils.js +28 -4
  68. package/dist/cjs/utils/hast-utils.js.map +1 -1
  69. package/dist/cjs/utils/nested-selector-commas.d.ts +2 -0
  70. package/dist/cjs/utils/nested-selector-commas.d.ts.map +1 -0
  71. package/dist/cjs/utils/nested-selector-commas.js +81 -0
  72. package/dist/cjs/utils/nested-selector-commas.js.map +1 -0
  73. package/dist/cjs/utils/route-utils.d.ts +117 -0
  74. package/dist/cjs/utils/route-utils.d.ts.map +1 -0
  75. package/dist/cjs/utils/route-utils.js +212 -0
  76. package/dist/cjs/utils/route-utils.js.map +1 -0
  77. package/dist/cjs/utils/style-utils.d.ts +1 -0
  78. package/dist/cjs/utils/style-utils.d.ts.map +1 -1
  79. package/dist/cjs/utils/style-utils.js +181 -2
  80. package/dist/cjs/utils/style-utils.js.map +1 -1
  81. package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
  82. package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
  83. package/dist/cjs/utils/template-expression-balance.js +181 -0
  84. package/dist/cjs/utils/template-expression-balance.js.map +1 -0
  85. package/dist/cjs/utils/types.d.ts +3 -0
  86. package/dist/cjs/utils/types.d.ts.map +1 -0
  87. package/dist/cjs/utils/types.js +3 -0
  88. package/dist/cjs/utils/types.js.map +1 -0
  89. package/dist/cjs/utils/url-query-writer.d.ts +123 -0
  90. package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
  91. package/dist/cjs/utils/url-query-writer.js +260 -0
  92. package/dist/cjs/utils/url-query-writer.js.map +1 -0
  93. package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
  94. package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
  95. package/dist/cjs/utils/url-search-param-sync.js +241 -0
  96. package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
  97. package/dist/cjs/utils/url-search-params.d.ts +48 -0
  98. package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
  99. package/dist/cjs/utils/url-search-params.js +109 -0
  100. package/dist/cjs/utils/url-search-params.js.map +1 -0
  101. package/dist/esm/builders/ast-builders.d.ts +2 -0
  102. package/dist/esm/builders/ast-builders.d.ts.map +1 -1
  103. package/dist/esm/builders/ast-builders.js +40 -4
  104. package/dist/esm/builders/ast-builders.js.map +1 -1
  105. package/dist/esm/builders/hast-builders.d.ts +1 -0
  106. package/dist/esm/builders/hast-builders.d.ts.map +1 -1
  107. package/dist/esm/builders/hast-builders.js +9 -0
  108. package/dist/esm/builders/hast-builders.js.map +1 -1
  109. package/dist/esm/builders/style-builders.d.ts +8 -2
  110. package/dist/esm/builders/style-builders.d.ts.map +1 -1
  111. package/dist/esm/builders/style-builders.js +78 -32
  112. package/dist/esm/builders/style-builders.js.map +1 -1
  113. package/dist/esm/index.d.ts +5 -1
  114. package/dist/esm/index.d.ts.map +1 -1
  115. package/dist/esm/index.js +5 -1
  116. package/dist/esm/index.js.map +1 -1
  117. package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
  118. package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
  119. package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
  120. package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
  121. package/dist/esm/node-handlers/node-to-html/index.js +71 -3
  122. package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
  123. package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
  124. package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
  125. package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
  126. package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
  127. package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
  128. package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
  129. package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  130. package/dist/esm/node-handlers/node-to-jsx/index.js +1442 -47
  131. package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
  132. package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
  133. package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  134. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +80 -7
  135. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  136. package/dist/esm/node-handlers/node-to-jsx/utils.js +875 -70
  137. package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
  138. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  139. package/dist/esm/utils/ast-utils.d.ts +163 -11
  140. package/dist/esm/utils/ast-utils.d.ts.map +1 -1
  141. package/dist/esm/utils/ast-utils.js +989 -75
  142. package/dist/esm/utils/ast-utils.js.map +1 -1
  143. package/dist/esm/utils/hast-utils.d.ts +8 -6
  144. package/dist/esm/utils/hast-utils.d.ts.map +1 -1
  145. package/dist/esm/utils/hast-utils.js +26 -3
  146. package/dist/esm/utils/hast-utils.js.map +1 -1
  147. package/dist/esm/utils/nested-selector-commas.d.ts +2 -0
  148. package/dist/esm/utils/nested-selector-commas.d.ts.map +1 -0
  149. package/dist/esm/utils/nested-selector-commas.js +77 -0
  150. package/dist/esm/utils/nested-selector-commas.js.map +1 -0
  151. package/dist/esm/utils/route-utils.d.ts +117 -0
  152. package/dist/esm/utils/route-utils.d.ts.map +1 -0
  153. package/dist/esm/utils/route-utils.js +179 -0
  154. package/dist/esm/utils/route-utils.js.map +1 -0
  155. package/dist/esm/utils/style-utils.d.ts +1 -0
  156. package/dist/esm/utils/style-utils.d.ts.map +1 -1
  157. package/dist/esm/utils/style-utils.js +179 -1
  158. package/dist/esm/utils/style-utils.js.map +1 -1
  159. package/dist/esm/utils/template-expression-balance.d.ts +58 -0
  160. package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
  161. package/dist/esm/utils/template-expression-balance.js +174 -0
  162. package/dist/esm/utils/template-expression-balance.js.map +1 -0
  163. package/dist/esm/utils/types.d.ts +3 -0
  164. package/dist/esm/utils/types.d.ts.map +1 -0
  165. package/dist/esm/utils/types.js +2 -0
  166. package/dist/esm/utils/types.js.map +1 -0
  167. package/dist/esm/utils/url-query-writer.d.ts +123 -0
  168. package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
  169. package/dist/esm/utils/url-query-writer.js +229 -0
  170. package/dist/esm/utils/url-query-writer.js.map +1 -0
  171. package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
  172. package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
  173. package/dist/esm/utils/url-search-param-sync.js +213 -0
  174. package/dist/esm/utils/url-search-param-sync.js.map +1 -0
  175. package/dist/esm/utils/url-search-params.d.ts +48 -0
  176. package/dist/esm/utils/url-search-params.d.ts.map +1 -0
  177. package/dist/esm/utils/url-search-params.js +81 -0
  178. package/dist/esm/utils/url-search-params.js.map +1 -0
  179. package/package.json +4 -4
  180. package/src/builders/ast-builders.ts +80 -3
  181. package/src/builders/hast-builders.ts +11 -0
  182. package/src/builders/style-builders.ts +121 -43
  183. package/src/index.ts +22 -1
  184. package/src/node-handlers/node-to-html/constants.ts +2 -0
  185. package/src/node-handlers/node-to-html/index.ts +126 -6
  186. package/src/node-handlers/node-to-html/types.ts +6 -0
  187. package/src/node-handlers/node-to-html/utils.ts +228 -14
  188. package/src/node-handlers/node-to-jsx/index.ts +2135 -62
  189. package/src/node-handlers/node-to-jsx/types.ts +75 -32
  190. package/src/node-handlers/node-to-jsx/utils.ts +1173 -84
  191. package/src/utils/ast-utils.ts +1316 -127
  192. package/src/utils/hast-utils.ts +37 -9
  193. package/src/utils/nested-selector-commas.ts +88 -0
  194. package/src/utils/route-utils.ts +214 -0
  195. package/src/utils/style-utils.ts +183 -1
  196. package/src/utils/template-expression-balance.ts +196 -0
  197. package/src/utils/types.ts +25 -0
  198. package/src/utils/url-query-writer.ts +318 -0
  199. package/src/utils/url-search-param-sync.ts +303 -0
  200. package/src/utils/url-search-params.ts +122 -0
@@ -1,5 +1,5 @@
1
1
  import * as hastUtils from '../../utils/hast-utils'
2
- import { createHTMLNode } from '../../builders/hast-builders'
2
+ import { createComment, createHTMLNode } from '../../builders/hast-builders'
3
3
  import { UIDLUtils, StringUtils } from '@teleporthq/teleport-shared'
4
4
  import {
5
5
  UIDLRepeatNode,
@@ -8,8 +8,16 @@ import {
8
8
  UIDLNode,
9
9
  UIDLElementNode,
10
10
  HastNode,
11
+ HastText,
12
+ UIDLDynamicReference,
13
+ UIDLRawValue,
11
14
  } from '@teleporthq/teleport-types'
12
- import { createConditionalStatement, handleAttribute, handleEvent } from './utils'
15
+ import {
16
+ createConditionalStatement,
17
+ createInlineConditionalStatement,
18
+ handleAttribute,
19
+ handleEvent,
20
+ } from './utils'
13
21
  import { NodeToHTML } from './types'
14
22
  import { DEFAULT_TEMPLATE_SYNTAX } from './constants'
15
23
 
@@ -80,13 +88,28 @@ const generateElementNode: NodeToHTML<UIDLElementNode, HastNode> = (node, params
80
88
  })
81
89
  }
82
90
 
91
+ const renderingConditions = node.content.renderingConditions
92
+ if (renderingConditions) {
93
+ const statement = createInlineConditionalStatement(
94
+ renderingConditions.reference,
95
+ renderingConditions.condition
96
+ )
97
+ if (statement) {
98
+ hastUtils.addAttributeToNode(htmlNode, templateSyntax.conditionalAttr, statement)
99
+ }
100
+ }
101
+
83
102
  templateLookup[key] = htmlNode
84
103
  return htmlNode
85
104
  }
86
105
 
87
106
  export default generateElementNode
88
107
 
89
- const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, templateSyntax) => {
108
+ const generateNode: NodeToHTML<UIDLNode, HastNode | HastText | string> = (
109
+ node,
110
+ params,
111
+ templateSyntax
112
+ ) => {
90
113
  switch (node.type) {
91
114
  case 'inject':
92
115
  if (node?.dependency) {
@@ -102,7 +125,7 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
102
125
  return StringUtils.encode(node.content.toString())
103
126
 
104
127
  case 'dynamic':
105
- return templateSyntax.interpolation(node.content.id)
128
+ return generateDynamicNode(node, params, templateSyntax)
106
129
 
107
130
  case 'element':
108
131
  return generateElementNode(node, params, templateSyntax)
@@ -116,6 +139,10 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
116
139
  case 'slot':
117
140
  return generateSlotNode(node, params, templateSyntax)
118
141
 
142
+ case 'expr':
143
+ return 'Expression nodes are not supported'
144
+ case 'cms-list-repeater':
145
+ return 'CMS repeater nodes are not supported'
119
146
  default:
120
147
  throw new Error(
121
148
  `generateHTMLSyntax encountered a node of unsupported type: ${JSON.stringify(
@@ -127,9 +154,90 @@ const generateNode: NodeToHTML<UIDLNode, HastNode | string> = (node, params, tem
127
154
  }
128
155
  }
129
156
 
157
+ const generateDynamicNode: NodeToHTML<UIDLDynamicReference, HastNode | HastText | string> = (
158
+ node,
159
+ params,
160
+ templateSyntax
161
+ ) => {
162
+ if (
163
+ node.content.referenceType === 'global' ||
164
+ (node.content.referenceType as string) === 'globalState'
165
+ ) {
166
+ const spanNode = createHTMLNode('span')
167
+ return spanNode
168
+ }
169
+
170
+ const usedPropType = params.propDefinitions[node.content.id]
171
+ if (usedPropType && usedPropType.type === 'element') {
172
+ let slotNode = createHTMLNode('slot')
173
+ const commentNode = createComment(`Default content for ${node.content.id}`)
174
+ hastUtils.addChildNode(slotNode, commentNode)
175
+
176
+ // Named slot handling for VueJS
177
+ if (templateSyntax.slotBinding === 'v-slot') {
178
+ hastUtils.addAttributeToNode(slotNode, 'name', node.content.id)
179
+ if (usedPropType.defaultValue) {
180
+ const defaultSlotContent = generateNode(
181
+ usedPropType.defaultValue as UIDLElementNode,
182
+ params,
183
+ templateSyntax
184
+ ) as HastNode
185
+ hastUtils.addChildNode(slotNode, defaultSlotContent)
186
+ }
187
+
188
+ return slotNode
189
+ } else {
190
+ const slotWrapper = createHTMLNode('ng-container')
191
+ slotNode = createHTMLNode('ng-container')
192
+
193
+ if (usedPropType.defaultValue) {
194
+ const defaultSlotTemplateName = StringUtils.dashCaseToCamelCase(
195
+ `default-${node.content.id}`
196
+ )
197
+
198
+ hastUtils.addAttributeToNode(
199
+ slotNode,
200
+ '*ngTemplateOutlet',
201
+ `${node.content.id} ? ${node.content.id} : ${defaultSlotTemplateName}`
202
+ )
203
+
204
+ const templateNode = createHTMLNode('ng-template')
205
+ hastUtils.addBooleanAttributeToNode(templateNode, `#${defaultSlotTemplateName}`)
206
+
207
+ const defaultSlotContent = generateNode(
208
+ usedPropType.defaultValue as UIDLElementNode,
209
+ params,
210
+ templateSyntax
211
+ ) as HastNode
212
+ hastUtils.addChildNode(templateNode, defaultSlotContent)
213
+ hastUtils.addChildNode(slotWrapper, templateNode)
214
+ } else {
215
+ hastUtils.addAttributeToNode(slotNode, '*ngTemplateOutlet', node.content.id)
216
+ }
217
+
218
+ hastUtils.addChildNode(slotWrapper, slotNode)
219
+ return slotWrapper
220
+ }
221
+ }
222
+
223
+ return templateSyntax.interpolation(node.content.id)
224
+ }
225
+
130
226
  const generateRawHTMLNode: NodeToHTML<UIDLNode, HastNode> = (node, params, templateSyntax) => {
131
227
  const attrKey = templateSyntax.domHTMLInjection ? templateSyntax.domHTMLInjection : 'innerHTML'
132
228
  const htmlNode = createHTMLNode('span')
229
+ const rawNode = node as UIDLRawValue
230
+
231
+ if (rawNode.dynamic) {
232
+ const { id, refPath } = rawNode.dynamic.content
233
+ const idWithPath = UIDLUtils.generateIdWithRefPath(id, refPath)
234
+ const fallbackContent = rawNode.fallback || rawNode.content
235
+ const fallbackVarName = `htmlFallback${StringUtils.generateRandomString()}`
236
+ params.dataObject[fallbackVarName] = fallbackContent
237
+ hastUtils.addAttributeToNode(htmlNode, attrKey, `${idWithPath} || ${fallbackVarName}`)
238
+ return htmlNode
239
+ }
240
+
133
241
  const dataObjName = `${node.type}${StringUtils.generateRandomString()}`
134
242
  hastUtils.addAttributeToNode(htmlNode, attrKey, dataObjName)
135
243
  params.dataObject[dataObjName] = node.content.toString()
@@ -159,12 +267,20 @@ const generateRepeatNode: NodeToHTML<UIDLRepeatNode, HastNode> = (node, params,
159
267
  return repeatContentTag
160
268
  }
161
269
 
162
- const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode> = (
270
+ const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode | HastText> = (
163
271
  node,
164
272
  params,
165
273
  templateSyntax
166
274
  ) => {
167
275
  let conditionalTag = generateNode(node.content.node, params, templateSyntax)
276
+
277
+ // We can add attributes only to tags, so in case of a text node we wrap it with a 'span'
278
+ if (typeof conditionalTag === 'object' && !('properties' in conditionalTag)) {
279
+ const wrappingSpan = createHTMLNode('span')
280
+ hastUtils.addTextNode(wrappingSpan, conditionalTag.value)
281
+ conditionalTag = wrappingSpan
282
+ }
283
+
168
284
  // conditional attribute needs to be added on a tag, so in case of a text node we wrap it with
169
285
  // a 'span' which is the less intrusive of all
170
286
  if (typeof conditionalTag === 'string') {
@@ -178,7 +294,11 @@ const generateConditionalNode: NodeToHTML<UIDLConditionalNode, HastNode> = (
178
294
  return conditionalTag
179
295
  }
180
296
 
181
- const generateSlotNode: NodeToHTML<UIDLSlotNode, HastNode> = (node, params, templateSyntax) => {
297
+ const generateSlotNode: NodeToHTML<UIDLSlotNode, HastNode | HastText> = (
298
+ node,
299
+ params,
300
+ templateSyntax
301
+ ) => {
182
302
  const slotNode = createHTMLNode('slot')
183
303
 
184
304
  if (node.content.name) {
@@ -2,6 +2,8 @@ import {
2
2
  UIDLDependency,
3
3
  UIDLEventHandlerStatement,
4
4
  UIDLElementNode,
5
+ UIDLPropDefinition,
6
+ UIDLStateDefinition,
5
7
  } from '@teleporthq/teleport-types'
6
8
 
7
9
  export interface HTMLTemplateGenerationParams {
@@ -11,6 +13,8 @@ export interface HTMLTemplateGenerationParams {
11
13
  // tslint:disable-next-line no-any
12
14
  dataObject: Record<string, any>
13
15
  methodsObject: Record<string, UIDLEventHandlerStatement[]>
16
+ propDefinitions: Record<string, UIDLPropDefinition>
17
+ stateDefinitions: Record<string, UIDLStateDefinition>
14
18
  }
15
19
 
16
20
  export interface HTMLTemplateSyntax {
@@ -25,6 +29,8 @@ export interface HTMLTemplateSyntax {
25
29
  customElementTagName?: (value: string) => string
26
30
  dependencyHandling?: 'import' | 'ignore'
27
31
  domHTMLInjection?: string
32
+ slotBinding?: string
33
+ slotTagName?: string
28
34
  }
29
35
 
30
36
  export type NodeToHTML<NodeType, ReturnType> = (
@@ -1,14 +1,21 @@
1
1
  import * as hastUtils from '../../utils/hast-utils'
2
- import { StringUtils } from '@teleporthq/teleport-shared'
2
+ import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
3
3
  import {
4
4
  UIDLConditionalExpression,
5
+ UIDLConditionExpressionEntry,
6
+ UIDLConditionExpressionGroup,
5
7
  UIDLConditionalNode,
6
8
  HastNode,
7
9
  UIDLAttributeValue,
8
10
  UIDLEventHandlerStatement,
9
11
  UIDLElementNode,
12
+ UIDLExpressionValue,
13
+ UIDLDynamicReference,
14
+ UIDLRawValue,
10
15
  } from '@teleporthq/teleport-types'
11
16
  import { HTMLTemplateGenerationParams, HTMLTemplateSyntax } from './types'
17
+ import { createHTMLNode } from '../../builders/hast-builders'
18
+ import generateElementNode from '../node-to-html'
12
19
 
13
20
  export const handleAttribute = (
14
21
  htmlNode: HastNode,
@@ -33,9 +40,15 @@ export const handleAttribute = (
33
40
  StringUtils.encode(attrValue.content.toString())
34
41
  )
35
42
  break
36
- case 'raw':
43
+ case 'raw': {
44
+ const rawValue = attrValue as UIDLRawValue
45
+ if (rawValue.dynamic) {
46
+ applyDynamicHtmlDirective(htmlNode, rawValue, params, templateSyntax, node)
47
+ break
48
+ }
37
49
  hastUtils.addAttributeToNode(htmlNode, attrKey, attrValue.content.toString())
38
50
  break
51
+ }
39
52
  case 'static':
40
53
  if (Array.isArray(attrValue.content)) {
41
54
  // This handles the cases when arrays are sent as props or passed as attributes
@@ -58,6 +71,35 @@ export const handleAttribute = (
58
71
  hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrValue.content.toString())
59
72
  }
60
73
  break
74
+
75
+ case 'element':
76
+ const templateNode = createHTMLNode(templateSyntax.slotTagName)
77
+ const templateContent = generateElementNode(attrValue, params, templateSyntax)
78
+
79
+ if (templateSyntax.slotBinding === 'v-slot') {
80
+ hastUtils.addBooleanAttributeToNode(
81
+ templateNode,
82
+ `${templateSyntax.slotBinding}:${attrKey}`
83
+ )
84
+ } else {
85
+ hastUtils.addBooleanAttributeToNode(templateNode, `${templateSyntax.slotBinding}${attrKey}`)
86
+ }
87
+
88
+ hastUtils.addChildNode(templateNode, templateContent)
89
+ hastUtils.addChildNode(htmlNode, templateNode)
90
+ break
91
+
92
+ case 'object': {
93
+ dataObject[attrKey] = attrValue.content
94
+ hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrKey)
95
+ break
96
+ }
97
+
98
+ case 'expr':
99
+ // TODO: Check this in the future. Not throwing an error for now
100
+ console.info(`Expressions are not supported in HTML templates`)
101
+ break
102
+
61
103
  default:
62
104
  throw new Error(
63
105
  `generateElementNode could not generate code for attribute of type ${JSON.stringify(
@@ -112,13 +154,33 @@ export const createConditionalStatement = (node: UIDLConditionalNode): string =>
112
154
  const { node: childNode, reference, value, condition } = node.content
113
155
 
114
156
  const expression = standardizeUIDLConditionalExpression(value, condition)
115
- const statement = createConditional(reference.content.id, expression)
157
+ if (reference.type === 'dynamic') {
158
+ const statement = createConditional(reference.content.id, expression)
159
+
160
+ if (childNode.type === 'conditional') {
161
+ return `${statement} && ${createConditionalStatement(childNode)}`
162
+ }
116
163
 
117
- if (childNode.type === 'conditional') {
118
- return `${statement} && ${createConditionalStatement(childNode)}`
164
+ return statement
119
165
  }
120
166
 
121
- return statement
167
+ // expression nodes are not supported in html based templates.
168
+ if (reference.type === 'expr') {
169
+ return ''
170
+ }
171
+ }
172
+
173
+ // Used by the inline `renderingConditions` path on UIDLElement — identical
174
+ // semantics to `createConditionalStatement` except the reference / condition
175
+ // are supplied directly rather than extracted from a UIDLConditionalNode.
176
+ export const createInlineConditionalStatement = (
177
+ reference: UIDLConditionalNode['content']['reference'],
178
+ condition: UIDLConditionalExpression
179
+ ): string => {
180
+ if (reference.type === 'dynamic') {
181
+ return createConditional(reference.content.id, condition)
182
+ }
183
+ return ''
122
184
  }
123
185
 
124
186
  const standardizeUIDLConditionalExpression = (
@@ -132,30 +194,145 @@ const standardizeUIDLConditionalExpression = (
132
194
  return conditionalExpression
133
195
  }
134
196
 
197
+ // An entry carrying its own `reference` compares that reference instead of the
198
+ // expression's inherited key. Dynamic refs contribute their id; expr refs
199
+ // contribute their raw expression, parenthesized (this flavor works in string
200
+ // space, so the expression text slots in as the left side directly).
201
+ const resolveConditionEntryKey = (
202
+ entry: UIDLConditionExpressionEntry,
203
+ inheritedKey: string
204
+ ): string => {
205
+ if (!entry.reference) {
206
+ return inheritedKey
207
+ }
208
+ if (entry.reference.type === 'dynamic') {
209
+ return entry.reference.content.id
210
+ }
211
+ return `(${entry.reference.content})`
212
+ }
213
+
214
+ // A group entry recurses with its own matchingCriteria and comes back already
215
+ // parenthesized; a leaf stringifies its single comparison unwrapped.
216
+ const stringifyConditionEntry = (
217
+ entry: UIDLConditionExpressionEntry | UIDLConditionExpressionGroup,
218
+ inheritedKey: string
219
+ ): string => {
220
+ if (UIDLUtils.isUIDLConditionGroup(entry)) {
221
+ return `(${createConditional(inheritedKey, entry)})`
222
+ }
223
+ return stringifyConditionalExpression(
224
+ resolveConditionEntryKey(entry, inheritedKey),
225
+ entry.operation,
226
+ entry.operand,
227
+ entry.containsField
228
+ )
229
+ }
230
+
135
231
  const createConditional = (
136
232
  conditionalKey: string,
137
- conditionalExpression: UIDLConditionalExpression
138
- ) => {
233
+ conditionalExpression: UIDLConditionalExpression | UIDLConditionExpressionGroup
234
+ ): string => {
139
235
  const { matchingCriteria, conditions } = conditionalExpression
140
236
  if (conditions.length === 1) {
141
- // Separate handling for single condition to avoid unnecessary () around
142
- const { operation, operand } = conditions[0]
143
- return stringifyConditionalExpression(conditionalKey, operation, operand)
237
+ return stringifyConditionEntry(conditions[0], conditionalKey)
144
238
  }
145
239
 
146
- const stringConditions = conditions.map(({ operation, operand }) => {
147
- return `(${stringifyConditionalExpression(conditionalKey, operation, operand)})`
240
+ const stringConditions = conditions.map((conditionEntry) => {
241
+ const entryText = stringifyConditionEntry(conditionEntry, conditionalKey)
242
+ // Groups arrive parenthesized already; leaves get wrapped here.
243
+ return UIDLUtils.isUIDLConditionGroup(conditionEntry) ? entryText : `(${entryText})`
148
244
  })
149
245
 
150
246
  const joinOperator = matchingCriteria === 'all' ? '&&' : '||'
151
247
  return stringConditions.join(` ${joinOperator} `)
152
248
  }
153
249
 
250
+ const stringifyOperandValue = (
251
+ value: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue
252
+ ): string => {
253
+ if (typeof value === 'string') {
254
+ return `'${value}'`
255
+ }
256
+ if (typeof value === 'number' || typeof value === 'boolean') {
257
+ return String(value)
258
+ }
259
+ if (typeof value === 'object' && 'type' in value) {
260
+ if (value.type === 'dynamic') {
261
+ return value.content.id
262
+ }
263
+ if (value.type === 'expr') {
264
+ return value.content
265
+ }
266
+ }
267
+ return String(value)
268
+ }
269
+
270
+ /**
271
+ * Collection operators dereference `.length` / `.includes` on the bound value,
272
+ * which is nullish far more often than it looks (an optional CMS column, a
273
+ * category without an image, a not-yet-hydrated state). Coercing to an empty
274
+ * collection first keeps a hide-this-node condition from throwing
275
+ * `Cannot read properties of null` mid-render. A non-empty string stays
276
+ * truthy, so string semantics are preserved. Mirrors `createNullSafeCollection`
277
+ * in the JSX handler — keep the two in sync.
278
+ */
279
+ const nullSafeCollection = (identifier: string, empty: '[]' | '{}' = '[]') =>
280
+ `(${identifier} || ${empty})`
281
+
154
282
  const stringifyConditionalExpression = (
155
283
  identifier: string,
156
284
  operation: string,
157
- value: string | number | boolean
285
+ value: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue,
286
+ containsField?: string
158
287
  ) => {
288
+ // Array/object operators
289
+ if (operation === 'isEmpty') {
290
+ return `${nullSafeCollection(identifier)}.length === 0`
291
+ }
292
+ if (operation === 'isNotEmpty') {
293
+ return `${nullSafeCollection(identifier)}.length > 0`
294
+ }
295
+ if (operation === 'lengthEquals') {
296
+ return `${nullSafeCollection(identifier)}.length === ${stringifyOperandValue(value)}`
297
+ }
298
+ if (operation === 'lengthGreaterThan') {
299
+ return `${nullSafeCollection(identifier)}.length > ${stringifyOperandValue(value)}`
300
+ }
301
+ if (operation === 'lengthLessThan') {
302
+ return `${nullSafeCollection(identifier)}.length < ${stringifyOperandValue(value)}`
303
+ }
304
+ if (operation === 'contains') {
305
+ const operandStr = stringifyOperandValue(value)
306
+ if (containsField) {
307
+ return `${nullSafeCollection(
308
+ identifier
309
+ )}.some(item => item.${containsField} === ${operandStr})`
310
+ }
311
+ return `${nullSafeCollection(identifier)}.includes(${operandStr})`
312
+ }
313
+ if (operation === 'notContains') {
314
+ const operandStr = stringifyOperandValue(value)
315
+ if (containsField) {
316
+ return `!${nullSafeCollection(
317
+ identifier
318
+ )}.some(item => item.${containsField} === ${operandStr})`
319
+ }
320
+ return `!${nullSafeCollection(identifier)}.includes(${operandStr})`
321
+ }
322
+ if (operation === 'hasKey') {
323
+ return `Object.prototype.hasOwnProperty.call(${nullSafeCollection(
324
+ identifier,
325
+ '{}'
326
+ )}, ${stringifyOperandValue(value)})`
327
+ }
328
+ if (operation === 'notHasKey') {
329
+ return `!Object.prototype.hasOwnProperty.call(${nullSafeCollection(
330
+ identifier,
331
+ '{}'
332
+ )}, ${stringifyOperandValue(value)})`
333
+ }
334
+
335
+ // Standard operators
159
336
  if (typeof value === 'boolean') {
160
337
  return `${value ? '' : '!'}${identifier}`
161
338
  }
@@ -164,5 +341,42 @@ const stringifyConditionalExpression = (
164
341
  return `${identifier} ${operation} '${value}'`
165
342
  }
166
343
 
344
+ if (typeof value === 'number') {
345
+ return `${identifier} ${operation} ${value}`
346
+ }
347
+
348
+ if (value.type === 'dynamic') {
349
+ return `${identifier} ${operation} ${value.content.id}`
350
+ }
351
+
352
+ if (value.type === 'expr') {
353
+ return `${identifier} ${operation} ${value.content}`
354
+ }
355
+
167
356
  return `${identifier} ${operation} ${value}`
168
357
  }
358
+
359
+ const applyDynamicHtmlDirective = (
360
+ htmlNode: HastNode,
361
+ rawValue: UIDLRawValue,
362
+ params: HTMLTemplateGenerationParams,
363
+ templateSyntax: HTMLTemplateSyntax,
364
+ node: UIDLElementNode
365
+ ) => {
366
+ const dynamicRef = rawValue.dynamic
367
+ const { id, refPath } = dynamicRef.content
368
+ const idWithPath = UIDLUtils.generateIdWithRefPath(id, refPath)
369
+ const fallbackContent = rawValue.fallback || rawValue.content
370
+ const fallbackVarName = `htmlFallback${StringUtils.generateRandomString()}`
371
+ params.dataObject[fallbackVarName] = fallbackContent
372
+ const expression = `${idWithPath} || ${fallbackVarName}`
373
+
374
+ const directiveKey = templateSyntax.domHTMLInjection || 'innerHTML'
375
+ htmlNode.tagName = 'span'
376
+ hastUtils.addAttributeToNode(htmlNode, directiveKey, expression)
377
+
378
+ const elementType = node.content.elementType
379
+ if (params.dependencies[elementType]?.path?.includes('dangerous-html')) {
380
+ delete params.dependencies[elementType]
381
+ }
382
+ }